import time

from spyke.fruit import FruitSpawner
from spyke.board import GameBoard
from spyke.snake import Snake
from spyke.ui.text import Text

class Game:
    def __init__(self, stdscr):
        self.stdscr = stdscr

        # Initialize game options and stats.
        self.loop_duration_ms = 120
        self.loop_count = 0
        self.debug_str = ''
        self.score = 0
        self.running = True

        # Initialize game objects.
        self.game_board = GameBoard()
        self.fruit_spawner = FruitSpawner(self.game_board)
        self.snake = Snake()
        self.snake.add_segment(self.game_board.x + 4, self.game_board.y + 2)
        self.snake.add_segment(self.game_board.x + 3, self.game_board.y + 2)
        self.snake.add_segment(self.game_board.x + 2, self.game_board.y + 2)
        self.snake.direction = 'right'
        self.stdscr.addstr('Game loop {0}'.format(self.loop_count))

    def run(self):
        while self.running is True:
            # Calculate the end of the current frame.
            frame_end = self.time_ms() + self.loop_duration_ms

            self.debug_str = 'Score = {0}'.format(self.score)
            self.loop_count = self.loop_count + 1

            # Update the snake's position/size.
            self.snake.update()

            # Handle collisions.
            self.handle_collisions()

            # Render the screen.
            self.render()

            # Handle input until the end of the frame.
            remaining_ms = frame_end - self.time_ms()
            while remaining_ms > 0:
                self.handle_input(remaining_ms)
                remaining_ms = frame_end - self.time_ms()

    def handle_collisions(self):
        head = self.snake.head()

        # Check for collision with fruit.
        if (self.fruit_spawner.fruit is not None and
                head.x == self.fruit_spawner.fruit.x and
                head.y == self.fruit_spawner.fruit.y):
            self.snake.should_grow = True
            self.score = self.score + 1
            self.fruit_spawner.spawn()

        # Check for collision with self.
        for i in range(1, len(self.snake.segments) - 1):
            segment = self.snake.segments[i]
            if head.x == segment.x and head.y == segment.y:
                self.end()

        # Check for collision with walls.
        if (head.x <= self.game_board.x or
                head.x >= self.game_board.x + self.game_board.width - 1 or
                head.y <= self.game_board.y or
                head.y >= self.game_board.y + self.game_board.height - 1):
            self.end()

    def render(self):
        # Clear the screen
        self.stdscr.clear()

        # Print game stats.
        Text.print_at(self.stdscr, 0, 0, self.debug_str)

        # Print game board and objects.
        self.game_board.render(self.stdscr)
        self.snake.render(self.stdscr)
        self.fruit_spawner.render(self.stdscr)

    def handle_input(self, timeout):
        self.stdscr.timeout(timeout)
        c = self.stdscr.getch()

        if c == ord('q'):
            exit()
        if c == ord(' '):
            self.pause()
        elif c == ord('w'):
            self.snake.change_direction("up")
        elif c == ord('a'):
            self.snake.change_direction("left")
        elif c == ord('s'):
            self.snake.change_direction("down")
        elif c == ord('d'):
            self.snake.change_direction("right")

    def pause(self):
        # TODO: This should store the current time and then recalculate the
        # end of the frame once the game is resumed.
        self.stdscr.timeout(-1)

        self.stdscr.clear()
        Text.print_at(self.stdscr, 0, 0,
                      'Game paused; press <SPACE> to continue...')

        while True:
            c = self.stdscr.getch()

            if c == ord(' '):
                break

        self.render()

    def end(self):
        self.running = False
        self.stdscr.clear()
        Text.print_at(self.stdscr, 0, 0,
                      'Game over! Your score was {0}.'.format(self.score))
        Text.print_at(self.stdscr, 0, 1, 'Press <q> to quit....')

        while True:
            c = self.stdscr.getch()

            if c == ord('q'):
                break
    def time_ms(self):
        return int(time.time() * 1000)


import os
from helpers import entropy

__version__ = 1.0
__name__ = "Response Statistics"
__description__ = "Calculate base information for each response object we process."

def process(responseObj, fileToAnalyze):
    module = {}
    raw_response = {}
    # raw_response["module"] = os.path.basename(__file__)
    raw_response["version"] = __version__
    raw_response["entropy"] = entropy(''.join(responseObj))
    raw_response["filepath"] = fileToAnalyze

    core_fields = ["WARC-Date", "WARC-IP-Address", "WARC-Target-URI"]

    for line in responseObj:
        if "WARC-Type: warcinfo" in line:
            return

        for cf in core_fields:
            if cf in line:
                raw_response["{}".format(cf.lower())] = line.replace('{}: '.format(cf), '').strip()

    module["stats"] = raw_response
    return module


import unittest,allure,json,os,pytest,ddt
from common.log import Logger
Path = os.path.abspath(os.path.dirname(__file__))
path=os.path.split(Path)[0]
from common.read_xls_news import Read_xls
from common.http_request_new import Request
from common.conf import Conf
from collections import Counter
import Project_path

import jsonpath  #嵌套字典取值s = jsonpath.jsonpath(dic,'$..name')

test_path=Project_path.TestData_path+"电饭煲自动化案例.xlsx"
conf_path=Project_path.conf_path+"ReplyInfo.ini"
result_path=Project_path.TestResult_path+"电饭煲远程控制-自动化案例_Result.xls"
http_conf_path=Project_path.conf_path+"http.ini"
sn_path=Project_path.conf_path+"sn.ini"

host=Conf(http_conf_path).get_value("HTTP","sit")
testmode=Conf(http_conf_path).get_value("AITEST","mode")
#sn_yb101=Conf(sn_path).get_value("AC","yb101")
#sn_HB=Conf(sn_path).get_value("AC","HB")
sn_ricecooker=Conf(sn_path).get_value("rice_cooker","rice_cooker_4001XM")

r=Read_xls(test_path)
w=r.copy_book()
url="%s/v1/auto_test/control/virtual"%host
data = {
    "version": "1.0",
    "text": "电饭煲开始煮饭 ",
    "uid": "6b07adb88106411fba94fe38c6754baa",
    "homeId": "162681",
    "device": {
        "sn": "0000EA11100000028197271200930000",
        "category": "EA",
        "modelNo": "40"
    },
    "device_info": [
        {
            "id": 408191474334720003,
             "deviceId": "1099511825287",
             "serailNo": "0000EA11100000028197271200930000",
             "enterpriseNo": "0000",
             "categoryNo": "EA",
             "modelNo": "40",
             "uid": "e76b6954ab5d4336ac45cf30d533d308",
             "homeId": "162681",
             "homeName": "9465的家",
             "roomId": "119961",
             "roomName": "厨房",
             "roleId": "1002",
             "deviceName": "电饭煲",
             "onlineStatus": 1,
             "version": 133
        }
    ],
    "query_reply": "query_reply"
}

#yb101_query=json.loads(Conf(conf_path).get_value("AC","yb101"))
#hb_query=json.loads(Conf(conf_path).get_value("AC","HB"))
rice_cooker_query=json.loads(Conf(conf_path).get_value("rice_cooker","rice_cooker_4001XM"))

def Label_Case(sheet_name=None):
    datalist = r.read_data(sheet_name, start_line=2)
    data = []
    if testmode == '0':
        for each in datalist:
            data.append(each)
    else:
        for each in datalist:
            if str(each[1]) in eval(testmode):
                data.append(each)
    return data

@allure.suite("电饭煲")
# @ddt.ddt
class TestRiceCooker():
    def setup_class(self):
        Logger().info("========%s测试开始:========" % __class__.__name__)

    def teardown_class(self):
        r.save_write(w,result_path)
        Logger().info("========%s测试结束!========" % __class__.__name__)

    @allure.feature('RiceCooker_4001XM')
    @pytest.mark.parametrize("tdata",Label_Case('中高端电饭煲'))
    # @pytest.mark.flaky(reruns=3, reruns_delay=redis_case)#重试机制
    def test_ricecooker_400XM(self,tdata):
        test_id = tdata[0]
        test_content=tdata[2]
        data['text'] = tdata[4]
        data['device_info'][0]['serailNo']=sn_ricecooker
        pre_data = tdata[3]
        Expect_nlu=tdata[5]
        Expect_lua=tdata[6]
        Expect_tts=tdata[7]

        try:
            pre_data = json.loads(pre_data)
        except:
            pre_data = '{' + pre_data.replace('&', ',') + '}'
            try:
                pre_data = json.loads(pre_data)
            except Exception as e:
                Logger().error(e)
                raise e
        finally:
            pre_data = dict(rice_cooker_query, **pre_data)
            data["query_reply"] = pre_data

        Response = Request().requests(url, data, "POST").json()
        print("**************")
        print(Response)
        # 校验接口是的请求成功
        try:
            jsonpath.jsonpath(Response, "$..code")[0] == '200'
        except Exception as e:
            Logger().error(e)
            raise e
        nlu = jsonpath.jsonpath(Response, "$..nlu")[0]
        tts = jsonpath.jsonpath(Response, "$..text")[0].replace("，", ",")
        test_row = int(test_id.split('_')[-1])
        try:
            lua = jsonpath.jsonpath(Response, "$..luaData")[0]
        except:
            lua = ''
        finally:
            r.write_onlydata(w, test_row, 8, str(nlu), sheetname='中高端电饭煲')
            r.write_onlydata(w, test_row, 9, str(lua), sheetname='中高端电饭煲')
            r.write_onlydata(w, test_row, 10, str(tts), sheetname='中高端电饭煲')

        try:
            assert str(nlu)==Expect_nlu,"NLU异常"
        except Exception as e:
            Logger().error(e)
            result="NLU异常"
            raise e
        else:
            try:
                assert str(lua) == Expect_lua, "lua命令转换异常"
            except Exception as e:
                Logger().error(e)
                result = "lua命令转换异常"
                raise e
            else:
                try:
                    assert tts in eval(Expect_tts), "TTS返回异常"
                except Exception as e:
                    Logger().error(e)
                    result = "TTS返回异常"
                    raise e
                else:
                    result="P"
                    Logger().info(u"【%s用例%s--%s】:测试通过！" % ("中高端电饭煲", test_id, test_content))
        finally:
            r.write_onlydata(w, test_row , 11, str(result), sheetname='中高端电饭煲')


import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.preprocessing import LabelBinarizer
# from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split

def pict(data, title):
    fig, ax = plt.subplots()
    ax.imshow(data, cmap=plt.cm.gray, interpolation='nearest')
    ax.set_title(title)
    plt.show()

W = (np.random.random(3) - 0.5) * 2
X = np.random.random(3)
print(X.T.shape)
print((X.T * W).shape)

digits = load_digits()  # 载入数据
X = digits.data  # 数据
y = digits.target  # 标签
print(X[0])
pict(X[0],'test')


import torch
from tqdm import tqdm
from torch import nn, optim
from .model import create_model
from .dataloaders import get_three_loaders
from .utils import get_device

def validate(classifier, criterion, valid_loader, device="cpu"):
    eval_acc = 0.0
    eval_loss = 0.0
    for images, labels in tqdm(valid_loader):
        images = images.to(device)
        labels = labels.to(device)
        with torch.no_grad():
            output = classifier(images)
            loss = criterion(output, labels)

        acc = ((output.argmax(dim=1) == labels).float().mean())
        eval_acc += acc
        eval_loss += loss
    return eval_acc / len(valid_loader), eval_loss / len(valid_loader)

def train_one_epoch(classifier, optimizer, criterion, trainloader, device="cpu"):
    train_acc = 0.0
    train_loss = 0.0
    for images, labels in tqdm(trainloader):
        images = images.to(device)
        labels = labels.to(device)
        optimizer.zero_grad()
        output = classifier(images)
        loss = criterion(output, labels)
        loss.backward()
        optimizer.step()
        acc = ((output.argmax(dim=1) == labels).float().mean())
        train_acc += acc
        train_loss += loss
    return train_acc / len(trainloader), train_loss / len(trainloader)

def train(N_EPOCHS, model, train_loader, val_loader=None, save=False, checkpoint=False):
    device = get_device()
    model = model.to(device)

    results = {
        "train_acc": [],
        "train_loss": [],
        "eval_acc": [],
        "eval_loss": []
    }

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.classifier.parameters())

    for epoch in range(N_EPOCHS):
        train_acc, train_loss = train_one_epoch(model, optimizer, criterion,
                                                train_loader, device=device)
        print("")
        print(f"Epoch {epoch + 1} | Train Acc: {train_acc * 100} | Train Loss: {train_loss}")

        if val_loader:
            eval_acc, eval_loss = validate(model, criterion, val_loader, device=device)
            print("")
            print(f"\t Val Acc: {eval_acc * 100} | Val Loss: {eval_loss}")
        print("====" * 8)

if __name__ == "__main__":
    (train_loader, val_loader, test_loader), classes = get_three_loaders(
        "sample_data_dir")
    model = create_model(len(classes), pretrained=True)


import inspect
import argparse
import logging
import pkgutil

from twisted.internet import reactor
from scrapy import signals
from scrapy.crawler import Crawler
from scrapy.utils.project import get_project_settings
from scrapy.xlib.pydispatch import dispatcher

from brightcorp import spiders

spider_classes_length = 0
closed_spiders = []

_log = logging.getLogger(__name__)

def subclassdetector(parent_class):
    spider_classes = set()
    cls_pred = lambda c: inspect.isclass(c) and c.__module__.count('.') == 0 and any([s.__name__ == parent_class for s in inspect.getmro(c)])
    for loader, name, _ in filter(lambda x: not x[2], pkgutil.iter_modules(spiders.__path__)):
        try:
            mod = loader.find_module(name).load_module(name)
            members = inspect.getmembers(mod, cls_pred)
            for x in inspect.getmembers(mod, cls_pred):
                spider_classes.add(x[1])
        except Exception as e:
            pass
    """ the below only appears to work if there are .pyc files for all the python modules
    spider_classes = [cls[1] for m in inspect.getmembers(spiders, inspect.ismodule)
                            for cls in inspect.getmembers(m[1], cls_pred)]
    """
    return spider_classes

def stop_reactor(spider):
    global spider_classes_length
    global closed_spiders

    if spider.name not in closed_spiders:
        closed_spiders.append(spider.name)
        if spider_classes_length <= len(closed_spiders):
            reactor.stop()
            _log.info("SUBSPIDER DETECTOR EXECUTED ALL SPIDERS")

def subspiderdetector(args):
    global spider_classes_length
    base_spider_class_name = args.spider_class
    # get input class and all its sub classes
    spider_classes = subclassdetector(base_spider_class_name)
    spider_objects = []
    if spider_classes:
        spider_classes_length = len(spider_classes)
        _log.info('FOUND THE FOLLOWING SPIDERS FOR THE GIVEN BASE CLASS \n%s' % spider_classes)
        project_settings = get_project_settings()
        dispatcher.connect(stop_reactor, signal=signals.spider_closed)
        # run all the spiders with subspider_detector=True
        for spider in spider_classes:
            spider_obj = spider(url=args.url, max_items_count=args.items_count, valid_cutoff=args.valid_cutoff,
                                subspider_detector=True, extract="1")
            spider_objects.append(spider_obj)
            crawler = Crawler(project_settings)
            crawler.configure()
            crawler.crawl(spider_obj)
            crawler.start()
        reactor.run()
    # after finishing all spiders return spider objects,
    # which have updated 'valid_spider' value
    return spider_objects

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-url', '--url', help='Input url to crawl', required=True)
    parser.add_argument('-spider_class', '--spider_class', help='base spider class name', required=True)
    parser.add_argument('-items_count', '--items_count', help='max items to be scraped', required=True)
    parser.add_argument('-valid_cutoff', '--valid_cutoff', help='min valid jobs count', required=False)
    args = parser.parse_args()
    spider_objects = subspiderdetector(args)
    if spider_objects:
        for spider_obj in spider_objects:
            _log.info(
                "%(spidername)s RETURENED STATUS : %(status)s, AND HAS %(validcount)d JOBS WHICH HAVE ALL FIELDS" % {
                    'spidername': spider_obj.name, 'status': spider_obj.valid_spider,
                    'validcount': spider_obj.valid_job_count})
    else:
        _log.info("NO SPIDERS FOUND")


from multiprocessing import Process, Queue, cpu_count
from random import random
import time
from copy import deepcopy
import random
from our_library import *
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--max_x', help='Give the MAX_X value', default=MAX_X, type=int)
parser.add_argument('--max_y', help='Give the MAX_Y value', default=MAX_Y, type=int)
parser.add_argument('--points', help='Give the number points per side', default=NB_POINT_PER_SIDE, type=int)
parser.add_argument('--nb_anchors', help='Give the number of anchors', default=NB_ANCHORS, type=int)
parser.add_argument('--seed', help='Give the seed value', default=1, type=int)
parser.add_argument('--nb_ind', help='Give the number of individuals', default=10, type=int)
parser.add_argument('--nb_gen', help='Give the number of generations', default=10, type=int)

args = parser.parse_args()

max_x = args.max_x
max_y = args.max_y
n_points = args.points
nb_anchors = args.nb_anchors
seed_value = args.seed
nb_ind = args.nb_ind
nb_gen = args.nb_gen

tics = max_x // (n_points - 1)
print("Running genetic on tics=" + str(tics) + ", anchors=" + str(nb_anchors))
random.seed(seed_value)

"""This next is

Genetic algorithm method
"""

class GA(object):
    """
        Implementation of a genetic algorithm
        for a permutation problem
    """

    def __init__(self, fitness_func,
                 n_individu=10,
                 gen=5,
                 dim=3,
                 CrossThreshold=0.9,
                 MutationThreshold=0.2,
                 MAX_X=4,
                 MAX_Y=4,
                 TICS=1):
        self.dim = dim * 2
        self.TICS = TICS
        self.space = MAX_X // TICS
        self.n_ind = n_individu
        self.gen = gen
        self.pop = []
        self.fitness_func = fitness_func
        self.all = []
        self.CrossThreshold = CrossThreshold
        self.MutationThreshold = MutationThreshold
        self.archive = []
        self.f_archive = []
        self.evolution = []
        for i in range(MAX_X // TICS):
            for j in range(MAX_Y // TICS):
                self.all.append((i, j))
        for i in range(self.n_ind):
            self.pop.append(list(np.random.randint(0, self.space+1, self.dim) * TICS))

    def crossover(self, parents):
        first_parant, second_parent = parents
        index = np.random.randint(1, self.dim)
        child_1 = first_parant[:index] + second_parent[index:]
        child_2 = second_parent[:index] + first_parant[index:]
        return (child_1, child_2)

    def mutation(self, individual):
        new_place = np.random.choice(range(self.space+1)) * self.TICS
        index = np.random.randint(0, self.dim)
        individual[index] = new_place
        return individual

    def selectParents(self, fitness_population):
        while 1:
            parent1 = self.tournament(fitness_population)
            parent2 = self.tournament(fitness_population)
            yield (parent1, parent2)

    def tournament(self, fitness_population):
        fit1, ch1 = fitness_population[random.randint(0, len(fitness_population) - 1)]
        fit2, ch2 = fitness_population[random.randint(0, len(fitness_population) - 1)]
        return ch1 if fit1 < fit2 else ch2

    def fitness(self, individual):
        anchors = [individual[2 * i:2 * (i + 1)] for i in range(self.dim // 2)]
        unique = np.unique(anchors, axis=0)
        unique = [list(e) for e in unique]
        if len(unique) == len(anchors):
            f_ind = self.fitness_func(anchors)
            return f_ind[0]
        else:
            return 99999999

    def eletism(self, rate, fitness_population):
        best = sorted([(x[0], fitness_population.index(x)) for x in fitness_population if x[0] is not None])[
               :int(rate * self.n_ind)]
        return [fitness_population[i[1]][1] for i in best]

    def update(self, fitness_population_):
        fitness_population = deepcopy(fitness_population_)
        allChildren = []
        generator = self.selectParents(fitness_population)
        nb_iterations = 0
        while len(allChildren) < len(fitness_population) and nb_iterations < 100000:
            nb_iterations+=1
            parents = next(generator)
            if random.random() > self.CrossThreshold:
                children = self.crossover(parents)
            else:
                children = parents
            for child in children:
                if random.random() > self.MutationThreshold:
                    ch = self.mutation(child)
                    if ch not in self.archive:
                        allChildren.append(ch)
                else:
                    if child not in self.archive:
                        allChildren.append(child)
        if len(allChildren) < len(fitness_population):
            tmp = []
            for i in range(len(fitness_population)-len(allChildren)):
                tmp.append(list(np.random.randint(0, self.space + 1, self.dim) * TICS))
            return allChildren.extend(tmp)
        return allChildren[:len(fitness_population)]

    def Work(self, anchors, q):
        f_ind = self.fitness(anchors)
        q.put(f_ind)

    def p_fitness(self, individuals):
        Result = []
        results = []
        cores = cpu_count()
        anchors_list = deepcopy(individuals)
        for index in range(len(anchors_list) // cores):
            q = Queue()
            P = []
            j = 0
            for i in range(cores):
                P.append(Process(target=self.Work, args=(anchors_list[index * cores + j], q)))
                j = j + 1
            for i in range(cores):
                P[i].start()

            for i in range(cores):
                results.append(q.get(True))

            for i in range(cores):
                P[i].join()
        i = 0
        for element in results:
            Result.append((element, individuals[i]))
            i += 1
        return Result

    def color(self, current, total):
        if current * 100 / total < 50:
            return "\033[91m %d \033[0m" % current
        if current * 100 / total < 75:
            return "\033[93m %d \033[0m" % current
        return "\033[92m %d \033[0m" % current

    def run(self):
        duration = 0
        for i in tqdm(np.arange(self.gen)):
            start = time.time()
            fitness_population = [(self.fitness(individual), individual) for individual in self.pop]
            self.f_archive = self.f_archive + fitness_population
            self.archive = self.archive + self.pop
            self.pop = self.update(fitness_population)
            end = time.time()
            duration +=  end-start
            self.evolution.append([i, min(self.f_archive)[0], duration])
        self.f_archive.sort()
        optimal_ind = self.f_archive[0][1]
        return [optimal_ind[2 * i:2 * (i + 1)] for i in range(self.dim // 2)]

    def decode(self,optimal_ind):
        return [optimal_ind[2 * i:2 * (i + 1)] for i in range(self.dim // 2)]

# ----------------------------

def Work(anchors):
    l = getAllSubRegions(anchors)
    res = getDisjointSubRegions(l)
    avgRA = getExpectation(res)
    return [avgRA, res, anchors]

ga = GA(
    fitness_func=Work,
    n_individu=nb_ind,
    CrossThreshold=0.2,
    MutationThreshold=0.3,
    gen=nb_gen,
    dim=nb_anchors,
    MAX_X=max_x,
    MAX_Y=max_y,
    TICS=tics
)

start = time.time()
optimal_anchors = ga.run()
end = time.time()

print(optimal_anchors)

l = getAllSubRegions(optimal_anchors)
optimal_areas = getDisjointSubRegions(l)
minAvgRA = getExpectation(optimal_areas)

drawNetwork(optimal_anchors, optimal_areas, algo_="genetic",max_x_=max_x, max_y_=max_y)

print("**Optimal Anchor Pos.:" + str(optimal_anchors), minAvgRA)
print('Runinig Times : ' + str(round((end - start) / 60.0, 2)) + ' (min.)')

f_res = open('./TXT/genetic.txt', 'a')
f_res.write(str(optimal_anchors)+';'+str(minAvgRA)+';'+str(end - start)+';'+str(nb_anchors)+';'+str(tics)+'\n')
f_res.close()

f_evo = open('./TXT/evolution.txt', 'a')
for e in ga.evolution:
    f_evo.write(str(nb_anchors)+';'+str(e[0])+';'+str(e[1])+';'+str(e[2])+'\n')
f_evo.close()


import redis
import frappe
import logging
from zlib import compress
from six import string_types
from pygelf import GelfUdpHandler

def publish_redis_event(queue, compressed):
	client = get_redis_client()
	if not client:
		return
	client.lpush(f'events:queue:{queue}', compressed)

def publish_gelf_event(queue, compressed):
	writer = get_gelf_writer(queue)
	if not writer:
		return
	writer.debug(compressed)

style_map = {
	'redis': publish_redis_event,
	'gelf': publish_gelf_event,
}

def publish_event(event, payload='', doctype='', queue='default', buffer=False):
	event_style = frappe.local.conf.event_style
	event_dict = {
		'event': event,
		'payload': payload if isinstance(payload, string_types) else frappe.as_json(payload),
		'doctype': doctype,
		'queue': queue,
	}
	payload = frappe.as_json(event_dict)

	# compressed = compress(payload.encode())
	compressed = payload
	style_map.get(event_style, publish_gelf_event)(queue, compressed)

redis_client_map = {}

def get_redis_client():
	redis_event_writer = frappe.local.conf.redis_event_writer
	if not redis_event_writer:
		frappe.logger().warn('Redis event fired but writer not configured')
	try:
		return redis_client_map[redis_event_writer]
	except KeyError:
		client = redis.StrictRedis.from_url(redis_event_writer)

		return client

GELF_WRITERS = {}

def get_gelf_writer(queue):
	try:
		return GELF_WRITERS[queue]
	except KeyError:
		pass

	logger = logging.getLogger(f'events:queue:{queue}')
	gelf_config = frappe.local.conf.gelf_event_writer
	if not gelf_config:
		frappe.logger().warn('Gelf event queue is not configured, while events are being fired')
		return

	gelf_host = gelf_config.get('host', '127.0.0.1')
	gelf_port = gelf_config.get('port', 32001)
	handler = GelfUdpHandler(host=gelf_host, port=gelf_port)
	logger.addHandler(handler)
	logger.setLevel(logging.DEBUG)
	logger.propagate = True
	GELF_WRITERS[queue] = logger
	return logger


from django.db.models import Avg
from django.shortcuts import render, get_object_or_404

from .models import Site

def index(request):
    sites = Site.objects.all()
    return render(request, 'ui/index.html', {'sites': sites})

def site_entry(request, site_id):
    site = get_object_or_404(Site, pk=site_id)
    return render(request, 'ui/site-entry.html', {'site': site})

def summary(request):
    sites = Site.objects.all()
    for site in sites:
        entries = site.statistics_entries.all()
        site.aggregation_a = sum(entry.a for entry in entries)
        site.aggregation_b = sum(entry.b for entry in entries)
    return render(request, 'ui/summary.html', {'sites': sites, 'action': 'sum'})

def summary_average(request):
    sites = Site.objects.annotate(aggregation_a=Avg('statistics_entries__a'),
                                  aggregation_b=Avg('statistics_entries__b'))
    return render(request, 'ui/summary.html', {'sites': sites, 'action': 'average'})


### VISUALIZATION DECISIONS AND CUSTOMIZABLE PARAMETERS BELOW ##
# Current parameters based on MNIST LeNet Caffe model #

# SET DIFFERENT TRAINED MODEL AND DEPLOY PROTOTXT HERE #
DEPLOY_FILE = './model_files/lenet.prototxt'
MODEL_FILE = './model_files/lenet_iter_10000.caffemodel'

################## VERSION 1 : 2 CONVOLUTIONS, 1 FULLY CONNECTED ##################

LAYER1 = 'conv1' # Layer 1: convolution or pooling
LAYER1_START = 0 # Specify first feature map
LAYER1_END = 20 # Specify last feature map (# see Options below: LeNet Architecture)
LAYER1_CMAP = 'gray'

LAYER2 = 'conv2' # Layer 2: convolution or pooling
LAYER2_START = 0 # Specify first feature map
LAYER2_END = 49 # Specify last feature map (# see Options below: LeNet Architecture)
LAYER2_CMAP = 'gray'

FC = 'ip1' # Layer 3: inner product (ip) or fully connected (fc) layer
FC_START = 0 # Specify starting neuron
FC_NCELLS = 250 # Specify number of neurons (final number depends on number of rows)
FC_NROWS = 6 # Specify number of rows to show the FC_NCELLS
FC_CMAP = 'viridis'

###############################################################

################## VERSION 2 : FULLY CONNECTED ONLY ##################

# FC_DIGIT = 1 # neurons will be sorted by importance for classifying this digit. MNIST digits 0-9
# FC_START = 0 # Specify starting neuron
# FC_NCELLS = 500 # number of neurons to be displayed. MNIST default all = 500
# FC_NROWS = 22 # on number of rows. MNIST default = 22
# FC_LAYER = 'ip1' # name of inner product/fully connected layer. MNIST default = 'ip2'
# WEIGHTS_BLOB = 'ip2' # weights layer! Note: this must be the weights that are used on FC_LAYER! MNIST default = 'ip2'
#
# FC_CMAP = 'viridis' # cmap choice. See options.

###############################################################

##### OPTIONS #####
# CMAP recommended: gray, viridis
# CMAP others: autumn, bone, cool, copper, flag, hot, hsv, inferno, jet, magma, pink, plasma, prism, spring, summer, winter
# See: https://matplotlib.org/api/pyplot_summary.html for details

# The LeNet MNIST Architecture has the following layers: (#batches, #convolutions (maps), width, height)
# data	(64, 1, 28, 28)
# conv1	(64, 20, 24, 24)
# pool1	(64, 20, 12, 12)
# conv2	(64, 50, 8, 8)
# pool2	(64, 50, 4, 4)
# ip1	(64, 500)
# ip2	(64, 10)
# prob	(64, 10)


__author__ = 'Iacopo'
import renderer
import facial_feature_detector as feature_detection
import camera_calibration as calib
import scipy.io as io
import cv2
import numpy as np
import os
import check_resources as check
import matplotlib.pyplot as plt
import sys
import myutil
import ThreeD_Model
import config
from camera_calibration import get_yaw_pitch_roll
from pyntcloud import PyntCloud
import pandas as pd

def preprocess_config():
    global this_path, opts, pose_models_folder, pose_models
    this_path = os.path.dirname(os.path.abspath(__file__))
    opts = config.parse_for_validation()
    # 3D Models we are gonna use to to the rendering {0, -40, -75}
    newModels = opts.getboolean('renderer', 'newRenderedViews')
    pose_models_folder = '/model_lecturer/'
    if opts.getboolean('renderer', 'newRenderedViews'):
        pose_models = ['model3D_aug_-00_00', ]
    else:
        assert False
        pose_models = ['model3D_aug_-00', 'model3D_aug_-40', 'model3D_aug_-75']
    # In case we want to crop the final image for each pose specified above/
    # Each bbox should be [tlx,tly,brx,bry]
    resizeCNN = opts.getboolean('general', 'resizeCNN')
    cnnSize = opts.getint('general', 'cnnSize')
    if not opts.getboolean('general', 'resnetON'):
        crop_models = [None, None, None, None, None]  # <-- with this no crop is done.
    else:
        # In case we want to produce images for ResNet
        resizeCNN = False  # We can decide to resize it later using the CNN software or now here.
        # The images produced without resizing could be useful to provide a reference system for in-plane alignment
        cnnSize = 224
        crop_models = [[23, 0, 23 + 125, 160], [0, 0, 210, 230], [0, 0, 210, 230]]  # <-- best crop for ResNet

def create_paths(ground_truth):
    if ground_truth is None:
        path_to_images_folder = r"C:\Noam\Code\vision_course\face_pose_estimation\images\valid_set\images"
        image_paths = []
        for file_path in os.listdir(path_to_images_folder):
            if file_path.endswith(".png"):
                image_paths.append(os.path.join(path_to_images_folder, file_path))
        print(f"#images: {len(image_paths)}")

        points_paths = []
        for file_path in os.listdir(path_to_images_folder):
            if file_path.endswith(".pts"):
                points_paths .append(os.path.join(path_to_images_folder, file_path))
        print(f"#points: {len(points_paths )}")

        # cull images that don't have points
        images_paths_with_points = []
        for image_path in image_paths:
            for points_path in points_paths:
                if image_path.split(".png")[0] == points_path.split(".pts")[0]:
                    images_paths_with_points.append(image_path)
                    break

        # cull points that don't have images
        points_paths_with_images = []
        for points_path in points_paths:
            for image_path in image_paths:
                if image_path.split(".png")[0] != points_path.split(".pts")[0]:
                    points_paths_with_images.append(points_path)
                    break

        assert len(images_paths_with_points) == len(points_paths_with_images)
        print(f"#tagged images = {len(images_paths_with_points)}")

        return sorted(images_paths_with_points), sorted(points_paths_with_images)

    else:
        tagged_images_names = sorted(ground_truth['file name'])
        path_to_images_folder = r"C:\Noam\Code\vision_course\face_pose_estimation\images\valid_set\images"
        image_paths = []
        points_paths = []
        files_in_folder = os.listdir(path_to_images_folder)
        for file_path in tagged_images_names:
            if file_path.endswith(".png") and file_path in files_in_folder:
                impath = os.path.join(path_to_images_folder, file_path)
                ptspath = os.path.join(path_to_images_folder, file_path[:-4]+".pts")
                if os.path.isfile(impath) and os.path.isfile(ptspath):
                    image_paths.append(impath)
                    points_paths.append(ptspath)
        print(f"#images: {len(image_paths)}")

        return image_paths, points_paths
        # return sorted(image_paths), sorted(points_paths)

def compare_ground_truth_to_results(images_paths, rvecs_per_image, ground_truth_df):
    # ground_truth_df Index(['Unnamed: 0', 'file name', 'rx', 'ry', 'rz', 'tx', 'ty', 'tz'], dtype='object')
    calculated_images_names = [os.path.basename(path) for path in images_paths]

    truth_images_names = ground_truth_df[['file name']].values

    good_inds = []
    for i, image_name in enumerate(calculated_images_names):
        if image_name in truth_images_names:
            good_inds.append(i)
        else:
            continue
    good_inds = np.array(good_inds)

    rvecs_per_image = rvecs_per_image[good_inds, :]

    truth_yaw_pitch_rolls = ground_truth_df[['rz', 'ry', 'rx']]

    diff = rvecs_per_image - truth_yaw_pitch_rolls

    hi=5

def read_ground_truth_validation():
    ground_truth_file_path = r"C:\Noam\Code\vision_course\face_pose_estimation\images\valid_set\validation_set.csv"
    ground_truth = pd.read_csv(ground_truth_file_path,
                               sep=r'\s*,\s*',
                               header=0,
                               encoding='ascii',
                               engine='python')

    return ground_truth

def demo():
    preprocess_config()
    n_sub = opts.getint('general', 'nTotSub')

    ground_truth = read_ground_truth_validation()
    images_paths, images_points = create_paths(ground_truth)

    rvecs_per_image = np.zeros((len(images_paths), 3))
    for i, (path_to_image, path_to_points) in enumerate(zip(images_paths, images_points)):
        # path_to_image = sys.argv
        file_list, output_folder = myutil.parse([sys.argv[0], path_to_image, path_to_points])

        # check for dlib saved weights for face landmark detection
        # if it fails, dowload and extract it manually from
        # http://sourceforge.net/projects/dclib/files/d.10/shape_predictor_68_face_landmarks.dat.bz2
        check.check_dlib_landmark_weights()

        # Pre loading all the models for speed
        all_models = myutil.preload(this_path, pose_models_folder, pose_models, n_sub)

        for f in file_list:
            if '#' in f:  # skipping comments
                continue
            splitted = f.split(',')
            image_key = splitted[0]
            image_path = splitted[1]
            image_landmarks_path = splitted[2]
            img = cv2.imread(image_path, 1)
            if image_landmarks_path != "None":
                # landmark = np.loadtxt(image_landmarks_path)
                landmark = np.loadtxt(image_landmarks_path, comments=("version:", "n_points:", "{", "}"))
                landmarks = list()
                landmarks.append(landmark)
            else:
                print('> Detecting landmarks')
                landmarks = feature_detection.get_landmarks(img, this_path)

            if len(landmarks) != 0:
                # Copy back original image and flipping image in case we need
                # This flipping is performed using all the model or all the poses
                # To refine the estimation of yaw. Yaw can change from model to model...

                img_display = img.copy()
                img, landmarks, img_yaw = myutil.flipInCase(img, landmarks, all_models)
                # listPose = myutil.decidePose(yaw, opts, new_models)
                list_pose = [0]

                for poseId in list_pose:
                    posee = pose_models[poseId]
                    # Looping over the subjects
                    for subj in range(1, n_sub + 1):
                        pose = posee + '_' + str(subj).zfill(2) + '.mat'
                        print('> Looking at file: ' + image_path + ' with ' + pose)
                        # load detections performed by dlib library on 3D model and Reference Image
                        print("> Using pose model in " + pose)
                        # Indexing the right model instead of loading it each time from memory.
                        model3D = all_models[pose]
                        eye_mask = model3D.eyemask
                        # perform camera calibration according to the first face detected
                        proj_matrix, camera_matrix, rmat, tvec, rvecs = calib.estimate_camera(model3D, landmarks[0])
                        yaw, pitch, roll = get_yaw_pitch_roll(rmat)
                        print(f"yaw = {yaw}, pitch={pitch}, roll={roll}")
                        print(f"rx = {rvecs[0]}, ry={rvecs[1]}, rz={rvecs[2]}")
                        # yaw_pitch_rolls[i, :] = yaw, pitch, roll
                        rvecs_per_image[i, :] = rvecs[:, 0]
    print(rvecs_per_image)

    compare_ground_truth_to_results(images_paths, rvecs_per_image, ground_truth)

if __name__ == "__main__":
    demo()


import unittest

rules = {}
rules['A'] = {'price': 50, 'ammount': 3, 'discount': 130}
rules['B'] = {'price': 30, 'ammount': 2, 'discount': 45}
rules['C'] = {'price': 20}
rules['D'] = {'price': 15}

class checkout():

    def __init__(self, checkout_rules):
        self.checkout_rules = checkout_rules
        self.total = 0
        self.qnt_per_item = rules

        for a in self.qnt_per_item:
            self.qnt_per_item[a]['qnt'] = 0

    @staticmethod
    def new(checkout_rules):
        return checkout(checkout_rules)

    def scan(self, item):
        self.increment_item_list(item)
        self.total += self.checkout_rules[item]['price']
        self.apply_discount(item)
        return self.total

    def increment_item_list(self, item):
        self.qnt_per_item[item]['qnt'] += 1

    def apply_discount(self, item):
        diff = 0
        for a in self.qnt_per_item:
            if 'ammount' in self.checkout_rules[item] and self.qnt_per_item[a]['qnt'] >= int(self.checkout_rules[item]['ammount']):
                diff = self.checkout_rules[item]['price'] - (self.checkout_rules[item]['discount'] / self.checkout_rules[item]['ammount'])
        self.total -= diff * (self.qnt_per_item[item]['qnt'])

    def enable_discount(self, item):
        if 'discount' in self.checkout_rules[item].keys():
            if self.qnt_per_item[item]['qnt'] >= self.checkout_rules[item]['ammount']:
                return True
        return False

class TestStringMethods(unittest.TestCase):

    def runTest(self,items, total):
        co = checkout.new(rules)
        for item in items:
            co.scan(item)

    def test_totals(self):
        self.runTest('A', 50)
        self.runTest('AB', 80)
        self.runTest('CDBA', 115)
        self.runTest('AA', 100)
        self.runTest('AAA', 130)
        self.runTest('AAAA', 180)
        self.runTest('AAAAA', 230)
        self.runTest('AAAAAA', 260)
        self.runTest('AAAAAAAAAAAA', 520)
        self.runTest('AAAB', 160)
        self.runTest('AAABB', 175)
        self.runTest('AAABBD', 190)
        self.runTest('DABABA', 190)

    def test_incremental(self):
        co = checkout.new(rules)
        self.assertEqual(0, co.total)
        co.scan('A')
        self.assertEqual(50, co.total)
        co.scan('B')
        self.assertEqual(80, co.total)
        co.scan('A')
        self.assertEqual(130, co.total)
        co.scan('A')
        self.assertEqual(160, co.total)
        co.scan('B')
        self.assertEqual(175, co.total)

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


import os, sys
import h5py
import numpy as np
import pandas as pd
import os, sys
import h5py
import numpy as np
import pandas as pd

from scipy.interpolate import InterpolatedUnivariateSpline
from scipy import optimize
import matplotlib.pyplot as plt
from scipy import integrate

import scipy.stats

import numba

from scipy.optimize import curve_fit
from scipy.stats import norm

import matplotlib.font_manager as font_manager
from tqdm.autonotebook import tqdm

tdc_bin = 5e-3  ## in ns

def extract_dataset(filename, dataset_name='waveform'):
    f = h5py.File(filename,'r')
    dataset = f[dataset_name]
    attrs_out = dict(dataset.attrs)
    ymults = [dataset.attrs['vertical{0}'.format(i+1)][0] for i in range(4)]
    yzeros = [dataset.attrs['vertical{0}'.format(i+1)][1] for i in range(4)]
    npoints = dataset.attrs['nPt']
    events = dataset.shape[1]//npoints
    chmask = dataset.attrs['chmask']
    data_out = np.zeros(4*dataset.shape[1]).reshape((4, events, npoints))
    ich = 0
    for i in range(4):
        if chmask[i]:
            data_out[i] = (dataset[ich].reshape(events, npoints) - yzeros[i])*ymults[i]
            ich += 1
    f.close()
    return data_out, attrs_out

def const(x, c):
    return c

def calculate_voltages_raw(v_in, pedestal_length=350):
    v_preamp_adjusted = np.copy(v_in)
    for i in range(v_in.shape[0]):
        ped = 0.0
        try:
            popt, pcov = curve_fit(const, np.arange(v_in.shape[1]), v_in[i, :pedestal_length], p0=[ped])
            ped = popt[0]
        except RuntimeError:
            ped = np.mean(v_in[i, :pedestal_length])
        v_preamp_adjusted[i] = v_preamp_adjusted[i] - ped
    return v_preamp_adjusted

def calculate_voltages(v_in, gain_post=-10, pedestal_length=400):
    gain_post_inv = 1.0/gain_post
    v_preamp_adjusted = gain_post_inv * calculate_voltages_raw(v_in, pedestal_length=pedestal_length)
    return v_preamp_adjusted

def calculate_tcross(v_in, percent_thresh, dt, gain_post=-10, pedestal_length=400):
    v_preamp_pedsub = calculate_voltages(v_in, gain_post=gain_post,
                                         pedestal_length=pedestal_length)
    time = np.arange(v_in.shape[1])*dt
    idx_max = np.argmax(v_preamp_pedsub, axis=-1)
    threshold = np.max(v_preamp_pedsub, axis=-1)*percent_thresh
    t0s = np.zeros(v_in.shape[0])

    for evt in range(v_in.shape[0]):
        t0 = -1.0
        if idx_max[evt] > 0:
            spline = InterpolatedUnivariateSpline(time, v_preamp_pedsub[evt])
            start = time[np.argmin(abs(spline(time[:idx_max[evt]])-threshold[evt]))]
            try:
                t0 = optimize.newton(lambda x: spline(x)-threshold[evt], x0=start, maxiter=500)
            except RuntimeError:
                t0 = np.nan
        t0s[evt] = t0

    return t0s

def gaus(x,a,x0,sigma):
    return a*np.exp(-(x-x0)**2/(2*sigma**2))

def moyal(x, norm, loc, scale):
    return norm*scipy.stats.moyal.pdf(x, loc, scale)

def plot_tcross(ich, t0s, trigger_t0s=None, ax=None, dofit=False, num_bins=20):
    mask = t0s > -0.5
    if trigger_t0s is not None:
        mask = (t0s > -0.5) & (trigger_t0s > -0.5)
    clean_t0s = t0s[mask]
    if trigger_t0s is not None:
        clean_trigger_t0s = trigger_t0s[mask]

    to_plot = clean_t0s
    if trigger_t0s is not None:
        to_plot = clean_t0s - clean_trigger_t0s

    mean = np.mean(to_plot)
    sigma = np.std(to_plot, ddof=1)

    range_hist = (np.min(to_plot),np.max(to_plot))
    if ax is None:
        fig, ax = plt.subplots(dpi=400)

    if dofit:
        bins, edges = np.histogram(to_plot, num_bins, density=False)
        centers = 0.5*(edges[1:] + edges[:-1])
        try:
            popt, pcov = curve_fit(gaus,centers,bins,p0=[1,mean,sigma])
            ax.plot(centers, gaus(centers,popt[0], popt[1], popt[2]))
            mean = popt[1]
            sigma = popt[2]
        except:
            pass

    ax.hist(to_plot, num_bins, range=range_hist,
            density=False,
            label='mean = %.3g s\nsigma = %.3g\n#event = %d\n#bin = %d'%(mean,sigma,to_plot.size,num_bins))
    ax.legend()
    ax.grid(which='both')
    if trigger_t0s is None:
        ax.set(xlabel='Time (s)', ylabel='Occurance',
               title='Channel {0}'.format(ich+1))
    else:
        ax.set(xlabel=r'Time_{Trigger} - Time_{Channel} (s)', ylabel='Occurance',
               title='Channel {0}'.format(ich+1))

def plot_time_quan(ich, t0s, trigger_t0s=None, ax=None, dofit=False, num_bins=20):
    mask = t0s > -0.5
    if trigger_t0s is not None:
        mask = (t0s > -0.5) & (trigger_t0s > -0.5)
    clean_t0s = t0s[mask]
    if trigger_t0s is not None:
        clean_trigger_t0s = trigger_t0s[mask]

    to_plot = clean_t0s
    if trigger_t0s is not None:
        to_plot = clean_t0s - clean_trigger_t0s

    mean = np.mean(to_plot)
    sigma = np.std(to_plot, ddof=1)

    range_hist = (np.min(to_plot),np.max(to_plot))
    if ax is None:
        fig, ax = plt.subplots(dpi=400)

    if dofit:
        bins, edges = np.histogram(to_plot, num_bins, density=False)
        centers = 0.5*(edges[1:] + edges[:-1])
        try:
            popt, pcov = curve_fit(gaus,centers,bins,p0=[1,mean,sigma])
            ax.plot(centers, gaus(centers,popt[0], popt[1], popt[2]))
            mean = popt[1]
            sigma = popt[2]
        except:
            pass

    ax.hist(to_plot, num_bins, range=range_hist,
            density=False,
            label='mean = %.3g s\nsigma = %.3g\n#event = %d\n#bin = %d'%(mean,sigma,to_plot.size,num_bins))
    ax.legend()
    ax.grid(which='both')
    if trigger_t0s is None:
        ax.set(xlabel='Time (s)', ylabel='Occurance',
               title='Channel {0}'.format(ich+1))
    else:
        ax.set(xlabel=r'Time_{Trigger} - Time_{Channel} (s)', ylabel='Occurance',
               title='Channel {0}'.format(ich+1))

@numba.jit(nopython=True)
def get_time_index(vals, thresh):
    istep = 0
    while vals[istep] > thresh:
        istep += 1
    return istep

def calculate_time(v_in, dt, th_cfd=0.5, tdc_bin=0.005, tdc_start = 40):
    eventLen = v_in.shape[0]
    time = np.arange(v_in.shape[1])*dt*1e9 #nanoseconds

    t_cfd = np.zeros(eventLen)
    for itrace, trace in enumerate(v_in):
        v_pk = np.argmin(trace)
        v_pk_range = np.linspace(time[max(0, v_pk - 20)], time[min(v_in.shape[1]-1, v_pk+20)], 10000)
        tdc_start = time[max(0, v_pk - 20)]
        tdc_max = time[v_in.shape[1]-1]
        interp_signal = InterpolatedUnivariateSpline(time, trace)
        v_pk = np.min(interp_signal(v_pk_range))
        v_th_cfd = 0 - th_cfd*(0 - v_pk)

        binned_signal = interp_signal(np.linspace(start=tdc_start, stop=tdc_max, num=int((tdc_max-tdc_start)/0.005) + 1))
        t_cfd[itrace] = tdc_start + get_time_index(binned_signal, v_th_cfd)*0.005

    std_t_cfd = np.std(t_cfd, ddof=1)
    std_t_cfd = std_t_cfd * 1e3
    mean_t_cfd = np.mean(t_cfd)
    return t_cfd, std_t_cfd, mean_t_cfd

def calculate_charge(v_in, dt, transCond, gain_post=-10, pedestal_length=400, charge_norm=1e15):
    v_preamp_pedsub = calculate_voltages(v_in, gain_post=gain_post,
                                         pedestal_length=pedestal_length)
    time = np.arange(v_in.shape[1])*dt
    norm = charge_norm/transCond

    return integrate.simps(norm*v_preamp_pedsub, time, axis=-1)

def plot_charge(dataset, ich, dt, transCond, mask=None, ax=None, gain_post=-10,
                pedestal_length=400, charge_norm=1e15, num_bins=200, dofit=True):
    charges = calculate_charge(dataset[ich], dt, transCond,
                               gain_post=gain_post,
                               pedestal_length=pedestal_length,
                               charge_norm=charge_norm)
    Q_avg = np.mean(charges)
    minmax = (np.min(charges),np.max(charges))
    range_hist = (0,20)#(minmax[0]*(0.5 if minmax[0] > 0 else 2.0),minmax[1]*(2.0 if minmax[1] > 0 else 0.5))
    if ax is None:
        fig, ax = plt.subplots(dpi=400)

    scale = np.std(charges, ddof=1)
    loc = np.mean(charges)
    norm = charges.size
    if dofit:
        bins, edges = np.histogram(charges, num_bins, range=range_hist, density=False)
        centers = 0.5*(edges[1:] + edges[:-1])
        try:
            popt, pcov = curve_fit(moyal, centers, bins, p0=[norm, loc,scale])
            ax.plot(centers, moyal(centers, popt[0], popt[1], popt[2]))
            norm = popt[0]
            loc = popt[1]
            scale = popt[2]
        except:
            pass
    #print('landau fit ->', norm, loc, scale)

    #ax.set_yscale('log')
    ax.hist(charges, num_bins, range=range_hist,
             density=False,
             label='peak = %.2f fC\n#event = %d\n#bin = %d'%(loc,dataset.shape[1],num_bins))
    ax.legend()
    ax.grid(which='both')
    ax.set(xlabel='Charge (fC)', ylabel='Occurance',
           title='Channel {0}'.format(ich+1))
    return ax

def plot_amplitude(dataset, ich, ax=None, gain_post=-10,
                   pedestal_length=400, num_bins = 100):
    v_preamp_pedsub = calculate_voltages(dataset[ich], gain_post=gain_post,
                                         pedestal_length=pedestal_length)
    v_pk = np.max(v_preamp_pedsub, axis=-1)

    range_ampl = (0, 0.3)#(np.min(v_pk)*0.5,np.max(v_pk)*2)
    if ax is None:
        fig, ax = plt.subplots(dpi=400)
    ax.hist(v_pk, num_bins, range=range_ampl, density=False,
            label='#event = %d\n#bin = %d'%(dataset.shape[1],num_bins))
    ax.legend()
    ax.grid()
    ax.set(xlabel='Amplitude (V)', ylabel='Occurance',
           title='Channel {0}'.format(ich+1))
    return ax

def plot_waveform(time, voltage, pp, xlable="Time(ns)", ylable="Voltage(V)", title="Raw Data ch1", pdf=False, pic=False):
    fig, ax1 = plt.subplots(dpi=200)
    for ab in tqdm(range(0,len(voltage))):
        ax1.plot(time, voltage[ab])
    # ax1.set_xlim(left=0,right=30)
    # ax1.set_ylim(bottom=0.20,top=0.50)
    ax1.grid()
    ax1.set(xlabel='Time(ns)', ylabel='Voltage(V)',
           title='Raw Data ch1')
    if pdf:
        pp.savefig(fig)
        pp.close()
    if pic:
        plt.show()
    plt.close(fig)

def plot_waveforms(time, v_ch1, v_ch2, v_ch3, v_ch4, pp, xlable="Time(ns)", ylable="Voltage(V)",
                   title="Raw Data ch1", pdf=False, pic=False):
    fig, ax1 = plt.subplots(dpi=200)
    for ab in tqdm(range(0,len(v_ch1))):
        ax1.plot(time, v_ch1[ab])
    # ax1.set_xlim(left=0,right=30)
    # ax1.set_ylim(bottom=0.20,top=0.50)
    ax1.grid()
    ax1.set(xlabel='Time(ns)', ylabel='Voltage(V)',
           title='Raw Data ch1')
    if pdf==True:
        pp.savefig(fig)
    if pic==True:
        plt.show()
    plt.close(fig)

    fig, ax2 = plt.subplots(dpi=200)
    for ab in tqdm(range(0,len(v_ch2))):
        ax2.plot(x*dt*1e9, v_ch2[ab])
    # ax1.set_xlim(left=0,right=30)
    # ax1.set_ylim(bottom=0.20,top=0.50)
    ax2.grid()
    ax2.set(xlabel='Time(ns)', ylabel='Voltage(V)',
           title='Raw Data ch2')
    if pdf:
        pp.savefig(fig)
    if pic:
        plt.show()
    plt.close(fig)

    fig, ax3 = plt.subplots(dpi=200)
    for ab in tqdm(range(0,len(v_ch3))):
        ax3.plot(x*dt*1e9, v_ch3[ab])
    # ax1.set_xlim(left=0,right=30)
    # ax1.set_ylim(bottom=0.20,top=0.50)
    ax3.grid()
    ax3.set(xlabel='Time(ns)', ylabel='Voltage(V)',
           title='Raw Data ch3')
    if pdf:
        pp.savefig(fig)
    if pic:
        plt.show()
    plt.close(fig)

    fig, ax4 = plt.subplots(dpi=200)
    for ab in tqdm(range(0,len(v_ch4))):
        ax4.plot(x*dt*1e9, v_ch4[ab])
    # ax1.set_xlim(left=0,right=30)
    # ax1.set_ylim(bottom=0.20,top=0.50)
    ax4.grid()
    ax4.set(xlabel='Time(ns)', ylabel='Voltage(V)',
           title='Raw Data ch4')
    if pdf:
        pp.savefig(fig)
    if pic:
        plt.show()
    plt.close(fig)

##### signal processing for all channels #############
def plotting_job(afile, scope_config, outfile):
    from matplotlib.backends.backend_pdf import PdfPages
    tc = scope_config['transcond']['lowgain']
    tcucsc = scope_config['transcond']['UCSC']
    data, attrs = extract_dataset(afile)
    pp = PdfPages(outfile)
    trigger_t0s = None
    measure_t0s = {}

    maxv_ch1 = np.max(calculate_voltages(data[0], gain_post=np.sign(scope_config['gains'][0])), axis=-1)
    maxv_ch2 = np.max(calculate_voltages(data[1], gain_post=np.sign(scope_config['gains'][1])), axis=-1)
    maxv_ch3 = np.max(calculate_voltages(data[2], gain_post=np.sign(scope_config['gains'][2])), axis=-1)
    maxv_ch4 = np.max(calculate_voltages(data[3], gain_post=np.sign(scope_config['gains'][3])), axis=-1)

    ch1 = (maxv_ch1 > 0.050) & (maxv_ch1 < 0.272)
    ch2 = (maxv_ch2 > 0.010) & (maxv_ch2 < 0.272)
    ch3 = (maxv_ch3 > 0.030) & (maxv_ch3 < 0.272)
    ch4 = (maxv_ch4 > 0.030) & (maxv_ch3 < 0.272)

    mask = (ch1 & ch2 & ch3 & ch4)
    t0s_simple = []
    for ch in range(4):
        if attrs['chmask'][ch]:
            fig, ax = plt.subplots(dpi=400)
            thegain = scope_config['gains'][ch]
            plot_amplitude(data[:,mask,:], ch, ax=ax, gain_post=np.sign(thegain))
            pp.savefig(fig)
            plt.close(fig)

            fig, ax = plt.subplots(dpi=400)
            plot_charge(data[:,mask,:], ch, attrs['dt'], tcucsc if ch != 1 else tc, ax=ax, gain_post=thegain)
            pp.savefig(fig)
            plt.close(fig)

            t0s_simple.append(calculate_time(data[ch],
                                             attrs['dt'],
                                             th_cfd=scope_config['thresholds'][ch],
                                             tdc_bin=0.005,
                                             tdc_start = 40))

            t0s = calculate_tcross(data[ch], scope_config['thresholds'][ch],
                                    attrs['dt'], gain_post=thegain)
            #fig, ax = plt.subplots(dpi=400)
            #plot_tcross(ch, t0s, ax=ax)
            #pp.savefig(fig)
            #plt.close(fig)
            if ch == scope_config['trigger']:
                trigger_t0s = t0s
            else:
                measure_t0s[ch] = t0s

    fit_plot_range = (-1.500, 1.500)
    nbins = 250

    ################ t1 (reference UCSC-21), t2 (ETROC0 board), t3 (reference UCSC-24) ################
    ## t0s_simple[0][0]: ch1, t0s_simple[1][0]: ch2, t0s_simple[2][0]: ch3
    fig, ax = plt.subplots(1, 1, dpi=400)
    tch21_avg = (t0s_simple[1][0] - t0s_simple[0][0])[mask]
    tch21_mean = np.mean(tch21_avg)
    tch21_sigma = np.std(tch21_avg, ddof=1)
    bins, edges = np.histogram(tch21_avg, nbins, range=fit_plot_range,  density=False)
    centers = 0.5*(edges[1:] + edges[:-1])
    try:
        popt, pcov = curve_fit(gaus,centers,bins,p0=[1,tch21_mean,tch21_sigma])
        ax.plot(centers, gaus(centers,popt[0], popt[1], popt[2]))
        tch21_mean = popt[1]
        tch21_sigma = abs(popt[2])
    except:
        pass
    ax.hist(tch21_avg, nbins, range=fit_plot_range,
            density=False,
            label='mean = %.3g ns\nsigma = %.3g ns\n#event = %d\n#bin = %d'%(tch21_mean,tch21_sigma,tch21_avg.size, nbins))
    ax.legend()
    ax.set(xlabel='t_2 - t_1  (ns)', ylabel='Counts',
           title='TOA for CH2 vs CH1')
    pp.savefig(fig)
    plt.close(fig)

    print('21 from fit', tch21_sigma*1e12)
    #tch21_sigma = norm.fit(tch21_avg)[1]
    print('21 from refit', tch21_sigma*1e12)

    fig, ax = plt.subplots(1, 1, dpi=400)
    tch32_avg = (t0s_simple[2][0] - t0s_simple[1][0])[mask]
    tch32_mean = np.mean(tch32_avg)
    tch32_sigma = norm.fit(tch32_avg)[1]
    bins, edges = np.histogram(tch32_avg, nbins, range=fit_plot_range, density=False)
    centers = 0.5*(edges[1:] + edges[:-1])
    try:
        popt, pcov = curve_fit(gaus,centers,bins,p0=[1,tch32_mean,tch32_sigma])
        ax.plot(centers, gaus(centers,popt[0], popt[1], popt[2]))
        tch32_mean = popt[1]
        tch32_sigma = abs(popt[2])

    except:
        pass
    ax.hist(tch32_avg, nbins, range=fit_plot_range,
            density=False,
            label='mean = %.3g ns\nsigma = %.3g ns\n#event = %d\n#bin = %d'%(tch32_mean,tch32_sigma,tch32_avg.size,nbins))
    ax.legend()
    ax.set(xlabel='t_3 - t_2 (ns)', ylabel='Counts',
           title='TOA for CH2 vs CH3')
    pp.savefig(fig)
    plt.close(fig)

    print('32 from fit', tch32_sigma*1e12)
    print('32 from refit', tch32_sigma*1e12)

    fig, ax = plt.subplots(1, 1, dpi=400)
    tch31_avg = (t0s_simple[2][0] - t0s_simple[0][0])[mask]
    tch31_mean = np.mean(tch31_avg)
    tch31_sigma = np.std(tch31_avg, ddof=1)
    bins, edges = np.histogram(tch31_avg, nbins, range=fit_plot_range, density=False)
    centers = 0.5*(edges[1:] + edges[:-1])
    try:
        popt, pcov = curve_fit(gaus,centers,bins,p0=[1,tch31_mean,tch31_sigma])
        ax.plot(centers, gaus(centers,popt[0], popt[1], popt[2]))
        tch31_mean = popt[1]
        tch31_sigma = abs(popt[2])
    except:
        pass
    ax.hist(tch31_avg, nbins, range=fit_plot_range,
            density=False,
            label='mean = %.3g ns\nsigma = %.3g ns\n#event = %d\n#bin = %d'%(tch31_mean,tch31_sigma,tch31_avg.size,nbins))
    ax.legend()
    ax.set(xlabel='t_3 - t_1 (ns)', ylabel='Counts',
           title='TOA for CH1 vs CH3')
    pp.savefig(fig)
    plt.close(fig)

    print('31 from fit',tch31_sigma*1e12)
    print('31 from refit', tch31_sigma*1e12)

    fig, ax = plt.subplots(1, 1, dpi=400)
    tch2_avg = (0.5*(t0s_simple[0][0]+t0s_simple[2][0]) - t0s_simple[1][0])[mask]
    tch2_mean = np.mean(tch2_avg)
    tch2_sigma = np.std(tch2_avg, ddof=1)
    print('sigma_231 basic', tch2_sigma*1e12)
    bins, edges = np.histogram(tch2_avg, nbins, range=fit_plot_range,  density=False)
    centers = 0.5*(edges[1:] + edges[:-1])
    try:
        popt, pcov = curve_fit(gaus,centers,bins,p0=[1,tch2_mean,tch2_sigma])
        ax.plot(centers, gaus(centers,popt[0], popt[1], popt[2]))
        tch2_mean = popt[1]
        tch2_sigma = abs(popt[2])
    except:
        pass

    print('sigma_231 after fit', tch2_sigma*1e12)

    bayes_info = scipy.stats.bayes_mvs(tch2_avg, alpha=0.68)
    print('231', np.std(tch2_avg, ddof=1), bayes_info[2][0])
    print('231\'', scipy.stats.bayes_mvs(tch2_avg[np.abs(tch2_avg - bayes_info[0][0]) < 5*bayes_info[2][0]]))

    sigma_sens_2 = np.sqrt(0.5*(tch21_sigma**2 - tch31_sigma**2 + tch32_sigma**2))

    ax.hist(tch2_avg, nbins, range=fit_plot_range,
            density=False,
            label='sigma = %.3g ns\nCH2 Jitter = %.3g ns\n#event = %d\n#bin = %d'%(tch2_sigma, sigma_sens_2, tch2_avg.size,nbins))
    ax.legend()
    ax.set(xlabel='0.5*(t_1 + t_3) - t_2 (ns)', ylabel='Counts',
           title='TOA for CH2 vs average of CH1+CH3')
    pp.savefig(fig)
    plt.close(fig)

    ################ t1 (reference UCSC-21), t4 (UCSC), t3 (reference UCSC-24) ################
    ## t0s_simple[0][0]: ch1, t0s_simple[3][0]: ch4, t0s_simple[2][0]: ch3
    fig, ax = plt.subplots(1, 1, dpi=400)
    tch41_avg = (t0s_simple[3][0] - t0s_simple[0][0])[mask]
    tch41_mean = np.mean(tch41_avg)
    tch41_sigma = np.std(tch41_avg, ddof=1)
    bins, edges = np.histogram(tch41_avg, nbins, range=fit_plot_range,  density=False)
    centers = 0.5*(edges[1:] + edges[:-1])
    try:
        popt, pcov = curve_fit(gaus,centers,bins,p0=[1,tch41_mean,tch41_sigma])
        ax.plot(centers, gaus(centers,popt[0], popt[1], popt[2]))
        tch41_mean = popt[1]
        tch41_sigma = abs(popt[2])
    except:
        pass
    ax.hist(tch41_avg, nbins, range=fit_plot_range,
            density=False,
            label='mean = %.3g ns\nsigma = %.3g ns\n#event = %d\n#bin = %d'%(tch41_mean,tch41_sigma,tch41_avg.size, nbins))
    ax.legend()
    ax.set(xlabel='t_4 - t_1  (ns)', ylabel='Counts',
           title='TOA for CH4 vs CH1')
    pp.savefig(fig)
    plt.close(fig)

    print('41 from fit', tch41_sigma*1e12)
    print('41 from refit', tch41_sigma*1e12)

    fig, ax = plt.subplots(1, 1, dpi=400)
    tch34_avg = (t0s_simple[2][0] - t0s_simple[3][0])[mask]
    tch34_mean = np.mean(tch34_avg)
    tch34_sigma = norm.fit(tch34_avg)[1]
    bins, edges = np.histogram(tch34_avg, nbins, range=fit_plot_range, density=False)
    centers = 0.5*(edges[1:] + edges[:-1])
    try:
        popt, pcov = curve_fit(gaus,centers,bins,p0=[1,tch34_mean,tch34_sigma])
        ax.plot(centers, gaus(centers,popt[0], popt[1], popt[2]))
        tch34_mean = popt[1]
        tch34_sigma = abs(popt[2])

    except:
        pass
    ax.hist(tch34_avg, nbins, range=fit_plot_range,
            density=False,
            label='mean = %.3g ns\nsigma = %.3g ns\n#event = %d\n#bin = %d'%(tch34_mean,tch34_sigma,tch34_avg.size,nbins))
    ax.legend()
    ax.set(xlabel='t_3 - t_4 (ns)', ylabel='Counts',
           title='TOA for CH4 vs CH3')
    pp.savefig(fig)
    plt.close(fig)

    print('34 from fit', tch34_sigma*1e12)
    print('34 from refit', tch34_sigma*1e12)

    fig, ax = plt.subplots(1, 1, dpi=400)
    tch4_avg = (0.5*(t0s_simple[0][0]+t0s_simple[2][0]) - t0s_simple[3][0])[mask]
    tch4_mean = np.mean(tch4_avg)
    tch4_sigma = np.std(tch4_avg, ddof=1)
    print('sigma_431 basic', tch4_sigma*1e12)
    bins, edges = np.histogram(tch4_avg, nbins, range=fit_plot_range,  density=False)
    centers = 0.5*(edges[1:] + edges[:-1])
    try:
        popt, pcov = curve_fit(gaus,centers,bins,p0=[1,tch4_mean,tch4_sigma])
        ax.plot(centers, gaus(centers,popt[0], popt[1], popt[2]))
        tch4_mean = popt[1]
        tch4_sigma = abs(popt[2])
    except:
        pass

    print('sigma_431 after fit', tch4_sigma*1e12)

    bayes_info = scipy.stats.bayes_mvs(tch4_avg, alpha=0.68)
    print('431', np.std(tch4_avg, ddof=1), bayes_info[2][0])
    print('431\'', scipy.stats.bayes_mvs(tch4_avg[np.abs(tch4_avg - bayes_info[0][0]) < 5*bayes_info[2][0]]))

    sigma_sens_4 = np.sqrt(0.5*(tch41_sigma**2 - tch31_sigma**2 + tch34_sigma**2))

    ax.hist(tch4_avg, nbins, range=fit_plot_range,
            density=False,
            label='sigma = %.3g ns\nCH4 Jitter = %.3g ns\n#event = %d\n#bin = %d'%(tch4_sigma, sigma_sens_4, tch4_avg.size,nbins))
    ax.legend()
    ax.set(xlabel='0.5*(t_1 + t_3) - t_4 (ns)', ylabel='Counts',
           title='TOA for CH4 vs average of CH1+CH3')
    pp.savefig(fig)
    plt.close(fig)

    nch = len(measure_t0s.keys())
    iax = 0
    for ch, t0s in measure_t0s.items():
        fig, ax = plt.subplots(1, 1, dpi=400)
        plot_tcross(ch, t0s, trigger_t0s=trigger_t0s, ax=ax)
        pp.savefig(fig)
        plt.close(fig)
        iax += 1
    pp.close()

gain_post = -10.0
scope_config = {'trigger': 2,
                'transcond':{'highgain': 15.7e3*0.75, 'lowgain': 4.4e3*0.75, 'UCSC': 4.7e2}, ## 0.75 is a scale factor for ETROC0 v2 sensor board
                'gains': [gain_post, gain_post, gain_post, gain_post],
                'thresholds': [0.5, 0.5, 0.5, 0.5]}

import sys
import glob
import time

data_path = sys.argv[1]
print(data_path.split('/'))
out_path = '/'.join(data_path.split('/')[:-1])
if len(sys.argv) > 2:
    out_path = sys.argv[2]
print('out path ->', out_path)
processed_files = set()

while True:
    files = set(glob.glob(os.path.join(data_path,'*.hdf5')))
    files_to_process = files - processed_files
    if len(files_to_process):
        for afile in tqdm(files_to_process):
            outfile = afile[:afile.rfind('.')].split('/')[-1] + '.pdf'
            fname = os.path.join(out_path, outfile)
            plotting_job(afile, scope_config, fname)
            processed_files.add(afile)
    else:
        time.sleep(1)


import json
import requests

class NetworkInterface(object):

	API_URL='https://api.smash.gg/gql/alpha'

	@staticmethod
	def get_headers():
		return {
			'X-Source': 'smashgg.py',
			'Content-Type': 'application/json',
			'Authorization': 'Bearer {}'.format(TokenHandler.get_token())
		}

	@staticmethod
	def query(query_string: str, variables: dict):
		Logger.debug('NetworkInterface.query: creating query object')
		query = QueryFactory.create(query_string, variables)
		Logger.debug('NetworkInterface.query: created query {}'.format(query))

		Logger.debug('NetworkInterface.query: sending query to queue')
		return QueryQueue.get_instance().add(query)

	@staticmethod
	def execute_query(query):
		log = Logger.get_instance()
		url = NetworkInterface.API_URL
		headers = NetworkInterface.get_headers()
		payload = query.get_query_dict()

		log.debug('NetworkInterface.query: Payload: {}'.format(payload))
		log.debug('NetworkInterface.query: Headers: {}'.format(headers))

		response = requests.post(url=url, headers=headers, json=payload)

		log.debug('NetworkInterface.query: {}'.format(response))
		log.debug('NetworkInterface.query: JSON Response: {}'.format(response.json()))
		return response.json()

	@staticmethod
	def paginated_query(query):
		log = Logger.get_instance()
		first_result = NetworkInterface.query(query)

# Path imports
from src.util.Logger import Logger
from src.util.TokenHandler import TokenHandler
from src.util.QueryFactory import QueryFactory
from src.util.QueryQueue import QueryQueue


import json
import random
import sqlite3
import datetime
import discord
import asyncio
from discord.ext import commands, tasks
import aiohttp
from Naver_Api.Api import Naver
money = sqlite3.connect("animal.db")

money_cur = money.cursor()
premium = sqlite3.connect("premium.db")

premium_cur = premium.cursor()
daily = sqlite3.connect("daily.db")

daily_cur = daily.cursor()
client_id = ""
client_secret = ""

N = Naver(client_id, client_secret)
def daily_embed(name, value):
    D = discord.Embed(colour=discord.Colour.blue())
    D.add_field(name=name, value=value)
    return D
class Core(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.messages = ['ㅌ도움말', '주인| 가위#1111', 'Team Project A']
        asyncio.get_event_loop().create_task(self.on_load())

    @tasks.loop(seconds=10)
    async def status_loop(self):
        await self.bot.change_presence(status=discord.Status.online, activity=discord.Activity(name=self.messages[0],
                                                                                type=discord.ActivityType.playing))
        self.messages.append(self.messages.pop(0))
        await asyncio.sleep(10)

    @tasks.loop(hours=1)
    async def daily_loop(self):
        now = datetime.datetime.now()
        current_time = now.strftime("%H")
        print("daily Current Time =", current_time)
        if (current_time == '07'):
            await self.bot.get_channel(782261035377229845).send('<@300535826088067072>')
            await self.bot.get_channel(782261035377229845).send(embed=daily_embed(name='START!', value=f'데일리 발송을 시작합니다!'))
            daily_cur.execute(f"SELECT * FROM daily")
            sel = daily_cur.fetchall()
            day = discord.Embed(colour=discord.Colour.blue())
            temp = []
            news = []
            sh = []
            num = 0
            fail = 0
            success = 0
            for show in sel:
                sh.append(show[0])
                sh.append(show[1])
                sh.append(show[2])
                url = f"http://sujang.dothome.co.kr/API/weather.php?place={show[1]}"
                async with aiohttp.ClientSession() as cs:
                    async with cs.get(url) as res:
                        pr = await res.read()
                        sid = pr.decode('utf-8')
                        answer = json.loads(sid)
                        temp.append(answer["현재온도"])
                        temp.append(answer["최저온도"])
                        temp.append(answer["최고온도"])
                        temp.append(answer["미세먼지"])
                        temp.append(answer["초미세먼지"])
                        print(temp)
                a = await N.News(query=show[2])
                print(a)
                for i in a["items"][:1]:
                    title = i["title"]
                    tit = str(title).replace("", "")
                    ti = tit.replace("", "")
                    T = ti.replace(""", "")
                    link = i["originallink"]
                    des = i["description"]
                    d_e = des.replace("", "")
                    d = d_e.replace("", "")
                    D = d.replace(""", "")
                    DE = D.replace("&", "")
                    news.append(T)
                    news.append(link)
                    news.append(DE)
                    print(news)
                day.add_field(name=f'지역: {sh[1]}', value="** **", inline=False)
                day.add_field(name=f'현재온도: {temp[0]}', value="** **")
                day.add_field(name=f'최저온도: {temp[1]}', value="** **")
                day.add_field(name=f'최고온도: {temp[2]}', value="** **")
                dust = []
                if int(temp[3][:-3]) <= 30:
                    m = '오늘 미세먼지는 좋아요!'
                    dust.append(m)
                elif int(temp[3][:-3]) >= 31 and int(temp[3][:-3]) <= 80:
                    m = '오늘 미세먼지는 보통이에요!'
                    dust.append(m)
                elif int(temp[3][:-3]) >= 81 and int(temp[3][:-3]) <= 150:
                    m = '오늘 미세먼지는 나쁨이에요!,마스크착용이 필요해요!'
                    dust.append(m)
                elif int(temp[3][:-3]) >= 151:
                    m = '오늘 미세먼지는 매우나빠요!, 마스크를 꼭! 착용하세요!'
                    dust.append(m)
                day.add_field(name=f'미세먼지: {temp[3]}', value=dust[0])
                smalldust = []
                if int(temp[4][:-3]) <= 15:
                    mm = '오늘 초미세먼지는 좋아요!'
                    smalldust.append(mm)
                elif int(temp[4][:-3]) >= 16 and int(temp[4][:-3]) <= 35:
                    mm = '오늘 초미세먼지는 보통이에요!'
                    smalldust.append(mm)
                elif int(temp[4][:-3]) >= 36 and int(temp[4][:-3]) <= 75:
                    mm = '오늘 초미세먼지는 나쁨이에요!,마스크착용이 필요하거나 외출을 자제해주세요!'
                    smalldust.append(mm)
                elif int(temp[4][:-3]) >= 76:
                    mm = '오늘 초미세먼지는 매우나빠요!, 외출을 자제해주세요!'
                    smalldust.append(mm)
                day.add_field(name=f'초미세먼지: {temp[4]}', value=smalldust[0])
                day.add_field(name=f"검색된 주제: {str(sh[2])}", value=f'기사제목- {str(news[0])}', inline=False)
                day.add_field(name="미리보기", value=str(news[2]), inline=False)
                day.add_field(name="** **", value=f"[자세한 내용 보러가기](<{str(news[1])}>)\n{'-----' * 10}", inline=False)
                try:
                    await self.bot.get_user(int(sh[0])).send(embed=day)
                    num += 1
                    success += 1
                    await self.bot.get_channel(782261035377229845).send(embed=daily_embed(name='SUCCESS!', value=f'{str(num)}번째 데일리발송을 성공했습니다!'))
                except:
                    fail += 1
                    num += 1
                    await self.bot.get_channel(782261035377229845).send('<@300535826088067072>')
                    await self.bot.get_channel(782261035377229845).send(embed=daily_embed(name='ERROR!',value=f'{str(num)}번째 데일리 발송중 에러가 발생했습니다.\n발생된 유저ID: {show[0]}'))
                    pass
                temp.clear()
                news.clear()
                sh.clear()
                dust.clear()
                smalldust.clear()
            await self.bot.get_channel(782261035377229845).send('<@300535826088067072>')
            await self.bot.get_channel(782261035377229845).send(embed=daily_embed(name='DONE!', value=f'데일리 발송을 모두 마쳤습니다! \n발송시도한 DM: {str(num)}\n성공: {str(success)}\n실패: {str(fail)}'))

    @tasks.loop(seconds=2)
    async def premium_loop(self):
        premium_cur.execute("SELECT * FROM premium")
        vip = premium_cur.fetchall()
        for i in vip:

            date = datetime.datetime.strptime(i[2], '%Y-%m-%d %H:%M:%S')

            if date < datetime.datetime.now():
                money_cur.execute("DELETE FROM premium WHERE user=" + str(i[0]))
                money.commit()
                print('complete premium deleted!')

    @tasks.loop(hours=1)
    async def automachine_loop(self):
        now = datetime.datetime.now()
        current_time = now.strftime("%H")
        print("automachine Current Time =", current_time)
        await asyncio.sleep(3600)
        if (current_time == '06'):
            print('automachine reload start!')
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(강아지용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(고양이용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(앵무새용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(여우용)'")
            money.commit()
            print('automachine reload complete!')
        elif (current_time == '12'):
            print('automachine reload start!')
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(강아지용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(고양이용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(앵무새용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(여우용)'")
            money.commit()
            print('automachine reload complete!')
        elif (current_time == '18'):
            print('automachine reload start!')
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(강아지용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(고양이용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(앵무새용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(여우용)'")
            money.commit()
            print('automachine reload complete!')
        elif (current_time == '23'):
            print('automachine reload start!')
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(강아지용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(고양이용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(앵무새용)'")
            money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(여우용)'")
            money.commit()
            print('automachine reload complete!')

    async def on_load(self):
        await self.bot.wait_until_ready()
        self.status_loop.start()
        print('statue loop started!')
        self.premium_loop.start()
        print('premium checker started!')
        self.automachine_loop.start()
        print('automachine checker started!')
        self.daily_loop.start()
        print('dailyloop started!')
        print('automachine reload start!')
        money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(강아지용)'")
        money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(고양이용)'")
        money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(앵무새용)'")
        money_cur.execute("UPDATE automachine SET 수량 = 1000 WHERE 물건 = '사료(여우용)'")
        money.commit()
        print('automachine reload complete!')
        print('AD function started!')

def setup(bot):
    bot.add_cog(Core(bot))
    print('Core Loaded!')


import torch
from environment.Protein_Folding import Protein_Folding_Environment
from networks.mpnn import Net
from utilities.parsing import parse_args
from agents.ppo import PPO

def main():

    args = parse_args()

    env = Protein_Folding_Environment(ref_pdb=args.ref_pdb)

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    net = Net(args, device)
    print(f'Using Device: {device}')
    if args.parameters is not None:
        net.load_state_dict(torch.load(args.parameters))
    net.to(device)

    agent = PPO(
        model=net,
        env=env,
        args=args,
        device=device
    )

    agent.train()
    agent.done()

if __name__ == "__main__":
    main()


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

# In[5]:

import random
import math
import pygame
import sys
from pygame.locals import QUIT,Rect, KEYDOWN, K_LEFT, K_RIGHT, K_UP, K_DOWN, K_SPACE

class Total:
    def __init__(self,col,rect,speed = 0): #col:채우는색, rect:그리는 직사각형(위치,크기), speed(이동속도,공만),dir(이동방향, 공만)
        self.col = col
        self.rect = rect
        self.speed = speed
        self.dir = random.randint(-85, 85) +200

    def move(self):#공을 움직인다 , radians를 사용해서 dir을 라디안으로 변환, X축과Y축의 방향성분구분
        self.rect.centerx += math.cos(math.radians(self.dir)) * self.speed
        self.rect.centery -= math.sin(math.radians(self.dir)) * self.speed
    def draw(self):
        pygame.draw.ellipse(SURFACE, self.col, self.rect)

pygame.init()
pygame.display.set_caption("Dodge")
pygame.key.set_repeat(15,15)
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 800
SURFACE = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
FPSCLOCK = pygame.time.Clock()

def main():
    # 게임 진행과 메세지
    game_start = False
    game_over = False

    time = 0
    Small_font = pygame.font.SysFont(None, 40)
    message_start = Small_font.render("Game_Start : Click the SpaceBar", True, (255, 255, 255))

    # player Circle
    C_x = 400
    C_y = 400
    Cir = Total((255, 255, 255), Rect(C_x, C_y, 10, 10))

    # Obstacle
    Ball_x = []
    Ball_y = []
    B_one = []  # i의 반복되는 만큼 1이 저장된다.
    B_point = []  # i가 반복되는 값을 저장한다
    B_velocity = []  # 공 하나당의 속도를 담는다
    for i in range(1,80):
        i *= 10
        B_point.append(i)
        B_one.append(1)
        B_velocity.append(random.randint(2,10))

    while True:
        message_time = Small_font.render("time : %.2f s" %time, True, (255, 255, 255))
        if game_start == True:
            time += 0.035 #시간을 측정
        if game_over == True:
            game_start = False
        SURFACE.fill((0,0,0))
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_LEFT:
                    Cir.rect.centerx -= 10
                if event.key == K_RIGHT:
                    Cir.rect.centerx += 10
                if event.key == K_UP:
                    Cir.rect.centery -= 10
                if event.key == K_DOWN:
                    Cir.rect.centery += 10
                if event.key == K_SPACE:
                    game_start = True
                    game_over = False
                    time = 0
                    Ball_x = []
                    Ball_y = []

        if game_start == False:
            SURFACE.blit(message_start, (180, 350))
            SURFACE.blit(message_time, (200, 430))
        else:
            Cir.draw()
            if Cir.rect.centerx > 795:
                Cir.rect.centerx = 795
            elif Cir.rect.centerx < 5:
                Cir.rect.centerx = 5
            if Cir.rect.centery > 795:
                Cir.rect.centery = 795
            elif Cir.rect.centery < 5:
                Cir.rect.centery = 5
            for i in range(len(B_point)):
                Ball_x.append(Total((255, 255, 0), Rect(B_point[i], B_one[i], 2, 2),B_velocity[i]))
                Ball_y.append(Total((255, 255, 0), Rect(B_one[i], B_point[i], 2, 2),B_velocity[i]))
                Ball_x[i].draw()
                Ball_y[i].draw()
                if Ball_x[i].rect.centery < 1000:
                    Ball_x[i].move()
                if Ball_y[i].rect.centery < 1000:
                    Ball_y[i].move()

                if Ball_x[i].rect.centery < 0 or Ball_x[i].rect.centery >800:
                    Ball_x[i].dir = - Ball_x[i].dir
                elif Ball_x[i].rect.centerx < 0 or Ball_x[i].rect.centerx >800:
                    Ball_x[i].dir = 180 - Ball_x[i].dir
                if Ball_y[i].rect.centery < 0 or Ball_y[i].rect.centery > 800:
                    Ball_y[i].dir = - Ball_y[i].dir
                elif Ball_y[i].rect.centerx < 0 or Ball_y[i].rect.centerx > 800:
                    Ball_y[i].dir = 180 - Ball_y[i].dir

                if Cir.rect.colliderect(Ball_x[i].rect) or Cir.rect.colliderect(Ball_y[i].rect):
                    game_over = True
        pygame.display.update()
        FPSCLOCK.tick(30)

if __name__ == "__main__":
    main()

# In[ ]:


from constants import *
import sys, pygame, player, camera, wall, block, random, main_drop

class States_manager:
    def __init__(self):
        self.running = True
        self.states = ["start", "running", "paused", "dead"]
        self.state = self.states[1]

        self.all_group = pygame.sprite.Group()
        self.player_group = pygame.sprite.Group()
        self.items = pygame.sprite.Group()

        n = 20
        self.player = player.Player()
        # the args for this need to be the w, h, of the world map NOT THE SCREEN SIZE!
        self.camera = camera.Camera(n * BLOCK_SIZE, n * BLOCK_SIZE)

        # self.player_group.add(self.player)

        # adds walls
        for i in range(100):
            r = random.randint(0, GAME_WORLD_W) // BLOCK_SIZE * BLOCK_SIZE
            c = random.randint(0, GAME_WORLD_H) // BLOCK_SIZE * BLOCK_SIZE
            self.all_group.add(wall.Wall(r, c))

        # adds pickups
        self.items.add(main_drop.Main_drop(BLOCK_SIZE * 3, BLOCK_SIZE * 3))

        self.player_group.add(self.player)

    def events(self, events):

        # print(events)
        for event in events:
            # if event.type == pygame.QUIT:
            #     self.running = False

            #Keyboard
            if event.type == pygame.KEYDOWN:
                keys = pygame.key.get_pressed()

                #used to kill outside loop
                if event.key == pygame.K_q:
                    return True

                if event.key == pygame.K_r:
                    # Reset map
                    self.map.reset()

                if event.key == pygame.K_p:

                    if self.state == "paused":
                        self.state = "running"
                    else:
                        self.state = "paused"

            if event.type == pygame.KEYUP:

                if event.key == 32:#SPACE
                    #Start Screen
                    if self.state == "start":
                        self.state = "running"

                    #Dig a block
                    if self.state == "running":
                        for all in self.all_group:
                            all.dig(self.player)

    def draw(self, surface):
        # surface.fill((100, 100, 100))#background

        if self.state == "start":
            surface.fill((100, 100, 255))#background

        elif self.state == "running":

            for wall in self.all_group:
                surface.blit(wall.image, self.camera.move(wall.rect))

            for player in self.player_group:
                surface.blit(player.image, self.camera.move(player.rect))

            for item in self.items:
                surface.blit(item.image, self.camera.move(item.rect))

            self.camera.draw(surface)
            self.draw_grid(surface)

        elif self.state == "paused":
            surface.fill((255, 100, 100))#background

        elif self.state == "dead":
            surface.fill((50, 50, 50))#background

        pygame.display.flip()

    def update(self, surface):
        surface.fill((100, 100, 100))#background
        self.camera.update(self.player)

        if self.state == "start":
            pass
        elif self.state == "running":
            self.camera.update(self.player)
            self.all_group.update()
            self.player_group.update(self.all_group)
            self.items.update(self.player_group)

        elif self.state == "paused":
            pass
        elif self.state == "dead":
            pass

    def draw_grid(self, surface):
        for x in range(0, GAME_WIDTH, BLOCK_SIZE):
            pygame.draw.line(surface, BLUE, (x, 0), (x, GAME_HEIGHT))
        for y in range(0, GAME_HEIGHT, BLOCK_SIZE):
            pygame.draw.line(surface, BLUE, (0, y), (GAME_WIDTH, y))


# -*- encoding: utf-8 -*-
# -*- coding: utf-8 -*-

import numpy
import networkx
import sys, os

import site
site.addsitedir('../clustering/lda')
site.addsitedir('../predict')

from hdplda import HDPLDA
import em2
import train
import sbp

#(K, V) = (8, 10)
(K, V) = (8, 16)
(N, M) = (20, 100)

TRIAL = 10

if len(sys.argv) < 2:
    numpy.random.seed(1)
else:
    numpy.random.seed(int(sys.argv[1]))

H = numpy.random.dirichlet([5] + [1] * (K-1))

W2 = numpy.ones((K, K), dtype = numpy.float64)
W2[0][0] *= 4.0
#W2[1][1] *= 1.0
#W2[0][1] *= 4.0
#W2[1][0] *= 4.0
#W2[1][2] *= 3.0
W2 /= W2.sum()
#print(W2)

G_0 = sbp.sbp_g_0(numpy.random, base = H, gamma = 0.5)
#print(G_0)
#Z = [ sbp.sbp_g_j(numpy.random, base = G_0, alpha = 0.8) for i in range(M) ]
#Z = [ sbp.sbp_g_j(numpy.random, base = G_0, alpha = 0.1) for i in range(M) ]
G = [ sbp.sbp_g_j(numpy.random, base = G_0, alpha = 0.1) for i in range(M) ]
#for g in Z: print(g)

graph = networkx.Graph()

theta = []
theta_p = []
for u in range(M):
    #theta.append( numpy.array([ float(x) for x in numpy.random.multinomial(K, Z[u]) ]) )
    theta.append( numpy.array([ float(x) for x in numpy.random.multinomial(N, G[u]) ]) )
    theta_p.append( theta[-1] / theta[-1].sum() )

    theta_p[-1] += 0.08
    theta_p[-1] /= theta_p[-1].sum()
    #print(theta_p[-1])

for u in range(M):
    for v in range(M):
        if u >= v: continue

        r_uv = 0
        for k in range(K):
            for l in range(K):
                r_uv += theta_p[u][k] * theta_p[v][l] * W2[k][l]

        if r_uv > 0.01:
            make_link = 0
            for trial in range(TRIAL):
                if numpy.random.binomial(1, r_uv) > 0:
                    make_link += 1
            if make_link > 1:
                graph.add_edge(u, v)
                #print('\t' + str((u, v, r_uv)))
            else:
                #print(u, v, r_uv)
                pass

print(len(graph.edges()), ((M * (M-1))/2))

U = graph.nodes()

train, valid = train.train_and_valid(graph.edges(), 0.7, 1)

KVtable = {
        0: [0,1],
        1: [2,3],
        2: [4,5],
        3: [6,7],
        4: [8,9],
        5: [10,11],
        6: [12,13],
        7: [14,15]}

beta = [
        [8, 8, 2, 1, 3, 1, 2, 2, 1, 2],
        [2, 1, 8, 8, 1, 1, 2, 4, 1, 1],
        [1, 1, 4, 1, 8, 8, 2, 1, 2, 1],
        [1, 1, 4, 1, 3, 2, 2, 1, 8, 9],
        [1, 1, 4, 1, 3, 2, 8, 8, 1, 4],
        [1, 1, 8, 1, 3, 2, 2, 1, 8, 2],
        [1, 1, 4, 1, 8, 8, 2, 1, 1, 2],
        [1, 1, 4, 1, 3, 2, 2, 1, 8, 9]]
phi = [ numpy.random.dirichlet(beta[k]) for k in range(K) ]

docs = []
for u in range(M):
    doc = []
    for n in range(N):
        z = numpy.random.binomial(1, theta_p[u]).argmax()
        kouho = KVtable[z]
        x = kouho[ numpy.random.randint(len(kouho)) ]
        doc.append(x)

    docs.append(doc)

lda = HDPLDA(K = 4, gamma = 0.5, alpha = 0.1, base = 0.05, docs = docs, V = V)
for i in range(50):
    lda.inference()
    #print(lda.perplexity())

G_ = lda.topic_prob()
K_ = len(G_[0])

#for g_ in G_: print(g_)

#worddist = lda.worddist()
#K_ = len(worddist)

Z_ = []
for u in range(M):
    #z_ = numpy.array([ float(x) for x in numpy.random.multinomial(K_, G_[u]) ])
    z_ = numpy.array([ float(x) for x in numpy.random.multinomial(N, G_[u]) ])
    z_ += 1.0
    z_ /= z_.sum()
    Z_.append(z_)

#for _ in Z_: print(_)

#W_ = em2.map_estimator2(Z_, train)
W_ = em2.map_estimator3(Z_, train)

#print(W_)
W_ += 0.01
W_ /= W_.sum()
#print(W_)

pairs = []
for u in U:
    for v in U:
        if u == v: continue
        pairs.append( tuple(sorted([u, v])) )
pairs = list(set(pairs))

for pair in pairs:

    u, v = pair
    #r = sum([ Z_[u][k] * Z_[v][k] * W_[k] for k in range(K_) ])
    r = 0
    for k in range(K_):
        for l in range(K_):
            r += Z_[u][k] * Z_[v][l] * W_[k][l]

    if pair in graph.edges():
        print('\t' + str((u, v, r)))
        pass
    else:
        print(u, v, r)
        pass

exit()

for u, vlist in links.items():

    for v in vlist:

        #r = sum([ Z_[u][k] * Z_[v][k] for k in range(K_) ])
        r = sum([ Z_[u][k] * Z_[v][k] * W_[k] for k in range(K_) ])
        print(u, v, r)

    print('')

    for v in U:
        if u == v:
            continue
        #r = sum([ Z_[u][k] * Z_[v][k] for k in range(K_) ])
        r = sum([ Z_[u][k] * Z_[v][k] * W_[k] for k in range(K_) ])
        print(u, v, r)

    print('---')


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import win32com.client
import scipy.io
import numpy as np
import pandas as pd
import csv
import random
import cmath
import math
import matplotlib.pyplot as plt

####### OpenDSS Initialisation #########

dssObj = win32com.client.Dispatch("OpenDSSEngine.DSS")
dssText = dssObj.Text
DSSCircuit = dssObj.ActiveCircuit
DSSLoads=DSSCircuit.Loads;
DSSLines=DSSCircuit.Lines;
dssObj.Start(0)
dssObj.AllowForms=False
DSSCktElement = DSSCircuit.ActiveCktElement

rated_cc=pd.read_excel('rated_CC_stats.xlsx', sheet_name=None)
del rated_cc['BMW_3ph_CC']
del rated_cc['Zoe_3ph_CC']
del rated_cc['DC_CC']
EV_power_cc=pd.Series(index=rated_cc.keys(),data=[6.6,6.6,7.2,7.2,7.2])

rated_de=pd.read_excel('derated_stats.xlsx', sheet_name=None)
del rated_de['bmw_3ph_6A']
del rated_de['bmw_3ph_9A']
del rated_de['bmw_3ph_12A']
del rated_de['bmw_3ph_15A']
del rated_de['zoe_3ph_6A']
del rated_de['zoe_3ph_12A']
del rated_de['zoe_3ph_18A']
del rated_de['zoe_3ph_24A']
EV_power_de=pd.Series(index=rated_de.keys(),data=[1.32,2.64,3.96,5.28,1.32,2.64,3.96,5.28,1.35,2.7,4.05,5.4,1.35,2.7,4.05,5.4,1.35,2.7,4.05,5.4])

g55lims=pd.read_csv('g55limits.csv')
g55lims['L'][0:10]=2
####### OpenDSS Initialisation #########

dssObj = win32com.client.Dispatch("OpenDSSEngine.DSS")
dssText = dssObj.Text
DSSCircuit = dssObj.ActiveCircuit
DSSLoads=DSSCircuit.Loads
DSSTrans=DSSCircuit.Transformers

Vh_ratios=pd.DataFrame()
Ch_ratios=pd.DataFrame()
Pass=pd.DataFrame()
VoltageMin={}
VoltageSrc={}
RSCs=[33,66]
master='Master_S'

pq=[-.6,-0.3,0,0.3,0.6]

cases=[]
for i in rated_cc.keys():
    cases.append(i.split('_')[0])

f_Rsc=pd.Series(dtype=float,index=RSCs,data=[0.78,0.327])
comps=['CC','De_6A','De_12A','De_18A','De_24A']
htch=pd.Series(index=comps,data=['/////','','XXXXX','','*'])
coll=pd.Series(index=comps, data=['w','#eb3636','w','yellow','g'])
thd=pd.Series(index=comps,dtype=(float))
passes=pd.DataFrame(columns=comps)
cc=0
vfig,vax=plt.subplots(figsize=(7,3))
plt.grid(linewidth=0.2)
# vax.text(.4,.9,str(i),
# horizontalalignment='left',
# transform=vax.transAxes)
vax.set_xlabel('h')
vax.set_ylabel('V'r'$_h$(% of V'r'$_{fund}$'')')
vax.set_xlim(1,29)
indexer=0
for c in comps:
    i=cases[indexer+3]
    dssObj.ClearAll()
    dssText.Command="Compile "+str(master)+".dss"
    ## Add lines
    dssText.Command ="New Line.LINE1 Bus1=2 Bus2=3 phases=3 Linecode=D2 Length="+str(f_Rsc[33])+" Units=km"

    ###-- for bmw and zoe
    if i.lower()=='bmw' or i.lower()=='zoe':
        i=i+'_1ph'
    #--- Add Loads

    if c=='CC':
        dssText.Command = "New Load.LOAD1 Phases=1 Status=1 Bus1=3.1 kV=0.230 kW="+str(EV_power_cc[i+'_CC'])+" PF=1 spectrum="+str(i)+'_CC'
        dssText.Command = "New Load.LOAD2 Phases=1 Status=1 Bus1=3.1 kV=0.230 kW="+str(EV_power_cc[i+'_CC'])+" PF=1 spectrum="+str(i)+'_CC'

    if c.split('_')[0]=="De":
        amps=c.split('_')[1]
        n_de=int(round(2*EV_power_cc[i+'_CC']/EV_power_de[i.lower()+'_'+str(amps)],0))
        print('Full Power',EV_power_cc[i+'_CC'],'Derated power ', EV_power_de[i.lower()+'_'+str(amps)] , n_de, 'EVs', 'Total derated', round(n_de*EV_power_de[i.lower()+'_'+str(amps)],2))
        cf=(n_de*EV_power_de[i.lower()+'_'+str(amps)])/(2*EV_power_cc[i+'_CC'])
        for q in range(1,n_de+1):
            indexer=indexer+1
            if indexer>4:
                indexer=0
            i=cases[indexer]
            if i.lower()=='bmw' or i.lower()=='zoe':
                i=i+'_1ph'
            dssText.Command = "New Load.LOAD"+str(q)+" Phases=1 Status=1 Bus1=3.1 kV=0.230 kW="+str(EV_power_de[i.lower()+'_'+str(amps)]/cf)+" PF=1 spectrum="+str(i.lower()+'_'+str(amps))
        print('corrected',round(EV_power_de[i.lower()+'_'+str(amps)]/cf*n_de,2))
    dssText.Command="New monitor.M1 Line.Line1 Terminal=2"

    dssText.Command="Calcvoltagebases"
    dssText.Command="Solve"
    dssText.Command="export voltages"
    dssText.Command="export currents"
    dssText.Command="export powers"

    dssText.Command="Solve Mode=harmonics NeglectLoadY=Yes"
    dssText.Command="export monitors m1"

    res_Reactor=pd.read_csv('LVTest_Mon_m1_1.csv')

    res_bmw=pd.read_csv('LVTest_Mon_m2_1.csv')
    res_zoe=pd.read_csv('LVTest_Mon_m3_1.csv')
    res_kona=pd.read_csv('LVTest_Mon_m4_1.csv')

    Vh_ratios['h']=res_Reactor[' Harmonic']
    Vh_ratios['Lims']=g55lims['L']
    Vh_ratios['V']=res_Reactor[' V1']
    Vh_ratios['V_ratio']=res_Reactor[' V1']/res_Reactor[' V1'][0]*100

    thd[c]=sum(Vh_ratios['V_ratio'][1:]**2)**0.5
    passes[c] = Vh_ratios['V_ratio'][1:]<Vh_ratios['Lims'][1:]
    passes['h']=res_Reactor[' Harmonic']
    x=Vh_ratios['h'][1:]
    y=Vh_ratios['V_ratio'][1:]

    lim=Vh_ratios['Lims'][1:]
    vax.bar(x+pq[cc],y, width=0.4,label=c,edgecolor='black',hatch=htch[c],color=coll[c])   ###--- Plotting the Voltage harmonics
    cc=cc+1
    iline=DSSLines.First
    while iline>0:
        print(DSSLines.Name, 'Bus1=',DSSLines.Bus1,'Bus2=', DSSLines.Bus2,'Length= ',DSSLines.Length)
        iline=DSSLines.Next
    iload=DSSLoads.First
    while iload>0:
        print(DSSLoads.Name, 'Bus=',DSSCktElement.BusNames, 'kW=',DSSLoads.kW,'pf=',DSSLoads.PF,'spectrum=',DSSLoads.spectrum)
        iload=DSSLoads.Next

vax.set_xticks(np.arange(1, 50, 2))
vax.set_ylim(0,2)
for n in x.index:  ###--- Plotting the G5 Limit
    if n<x.index[-1]:
        vax.plot([x[n]-0.5,x[n]+0.5],[lim[n],lim[n]],color='orange')
    if n==x.index[-1]:
        vax.plot([x[n]-0.5,x[n]+0.5],[lim[n],lim[n]],color='orange',label='G5/5 Limit')
vax.legend()

dssText.Command="Solve Mode=Faultstudy"
dssText.Command="export Faultstudy"
dssText.Command="export seqz"

seqz=pd.read_csv('LVTest_EXP_SEQZ.csv')
faults=pd.read_csv('LVTest_EXP_FAULTS.csv')

cc=cc+1
print('Zterminal' ,round(seqz[' Z1'][1],4),'Zend',round(seqz[' Z1'][-1:].values[0],4))
print('Fault End' ,faults['  1-Phase'][-1:].values)
# ress=pd.DataFrame()
# ress['bmw']=res_bmw[' IAngle1']
# ress['kona']=res_kona[' IAngle1']

# def getDifference(b1, b2):
# 	r = (b2 - b1) % 360.0
# 	# Python modulus has same sign as divisor, which is positive here,
# 	# so no need to consider negative case
# 	if r >= 180.0:
# 		r -= 360.0
# 	return r

# ress['newdiff']=ress['bmw']-ress['kona']
# for w in ress['newdiff'].index:
#     ress['newdiff'][w]=getDifference(ress['bmw'][w], ress['kona'][w])

# ress['bmw_orig']=rated_cc['BMW_1ph_CC']['Ih phase mean w.r.t. L1_Ih1']
# ress['kona_orig']=rated_cc['Kona_CC']['Ih phase mean w.r.t. L1_Ih1']
# ress['olddiff']=ress['bmw_orig']-ress['kona_orig']

# for w in ress['olddiff'].index:
#     ress['olddiff'][w]=getDifference(ress['bmw_orig'][w], ress['kona_orig'][w])

plt.tight_layout()
plt.savefig('figs/'+i+'_Derated_MultipleCars_Diversity.png')

print(thd)


import os
import io

def split(filehandler, delimiter=',', row_limit=2000,
    output_name_template='output.csv', output_path='', keep_headers=True):
    """
    Splits a CSV file into multiple pieces.

    A quick bastardization of the Python CSV library.
    Arguments:
        `row_limit`: The number of rows you want in each output file. 10,000 by default.
        `output_name_template`: A %s-style template for the numbered output files.
        `output_path`: Where to stick the output files.
        `keep_headers`: Whether or not to print the headers in each output file.
    Example usage:

        >> from toolbox import csv_splitter;
        >> csv_splitter.split(open('/home/ben/input.csv', 'r'));

    """
import csv
with open('bdata.csv', encoding="utf8") as csv_data:
    reader = csv.reader(csv_data,  delimiter=',')
    current_piece = 1
    current_out_path = os.path.join(
         output_path,
         output_name_template  % current_piece
    )
with io.open('output4.csv', "w", encoding="utf-8") as csvfile:
    writer = csv.writer(csvfile, lineterminator="\n",delimiter=",", quotechar='"')
    current_limit = row_limit
    if keep_headers:
        headers = reader.next()
        writer.writerow(headers)
    for i, row in enumerate(reader):
        if i + 1 > current_limit:
            current_piece += 1
            current_limit = row_limit * current_piece
            current_out_path = os.path.join(
               output_path,
               output_name_template  % current_piece
            )
            writer = csv.writer(open(current_out_path, 'w'), delimiter=',')
            if keep_headers:
                writer.writerow(headers)
        current_out_writer.writerow(row)