import re
import json
import ftfy
from functools import lru_cache
from malaya.text.rules import rules_normalizer
from malaya.text.regex import _expressions
from malaya.text.english.words import words as _english_words
from malaya.tokenizer import Tokenizer
from malaya.path import PATH_PREPROCESSING, S3_PATH_PREPROCESSING
from malaya.function import check_file, validator
from typing import List

_annotate = [
    'hashtag',
    'allcaps',
    'elongated',
    'repeated',
    'emphasis',
    'censored',
]

_normalize = list(_expressions.keys())

def get_normalize():
    return _normalize

def get_annotate():
    return _annotate

def _case_of(text):
    return (
        str.upper
        if text.isupper()
        else str.lower
        if text.islower()
        else str.title
        if text.istitle()
        else str
    )

def unpack_english_contractions(text):
    """
    Replace *English* contractions in ``text`` str with their unshortened forms.
    N.B. The "'d" and "'s" forms are ambiguous (had/would, is/has/possessive),
    so are left as-is.
    Important Note: The function is taken from textacy (https://github.com/chartbeat-labs/textacy).
    """

    text = re.sub(
        r"(\b)([Aa]re|[Cc]ould|[Dd]id|[Dd]oes|[Dd]o|[Hh]ad|[Hh]as|[Hh]ave|[Ii]s|[Mm]ight|[Mm]ust|[Ss]hould|[Ww]ere|[Ww]ould)n't",
        r'\1\2 not',
        text,
    )
    text = re.sub(
        r"(\b)([Hh]e|[Ii]|[Ss]he|[Tt]hey|[Ww]e|[Ww]hat|[Ww]ho|[Yy]ou)'ll",
        r'\1\2 will',
        text,
    )
    text = re.sub(
        r"(\b)([Tt]hey|[Ww]e|[Ww]hat|[Ww]ho|[Yy]ou)'re", r'\1\2 are', text
    )
    text = re.sub(
        r"(\b)([Ii]|[Ss]hould|[Tt]hey|[Ww]e|[Ww]hat|[Ww]ho|[Ww]ould|[Yy]ou)'ve",
        r'\1\2 have',
        text,
    )
    text = re.sub(r"(\b)([Cc]a)n't", r'\1\2n not', text)
    text = re.sub(r"(\b)([Ii])'m", r'\1\2 am', text)
    text = re.sub(r"(\b)([Ll]et)'s", r'\1\2 us', text)
    text = re.sub(r"(\b)([Ww])on't", r'\1\2ill not', text)
    text = re.sub(r"(\b)([Ss])han't", r'\1\2hall not', text)
    text = re.sub(r"(\b)([Yy])(?:'all|a'll)", r'\1\2ou all', text)
    return text

def _get_expression_dict():
    return {
        k.lower(): re.compile(_expressions[k]) for k, v in _expressions.items()
    }

class Preprocessing:
    def __init__(
        self,
        normalize=[
            'url',
            'email',
            'percent',
            'money',
            'phone',
            'user',
            'time',
            'date',
            'number',
        ],
        annotate=[
            'allcaps',
            'elongated',
            'repeated',
            'emphasis',
            'censored',
            'hashtag',
        ],
        lowercase=True,
        fix_unidecode=True,
        expand_english_contractions=True,
        translator=None,
        speller=None,
        segmenter=None,
        stemmer=None,
    ):
        self._fix_unidecode = fix_unidecode
        self._normalize = normalize
        self._annotate = annotate
        self._regexes = _get_expression_dict()
        self._tokenizer = Tokenizer(lowercase=lowercase).tokenize
        self._expand_contractions = expand_english_contractions
        self._all_caps_tag = 'wrap'
        self._translator = translator
        self._speller = speller
        self._segmenter = segmenter
        if self._segmenter:
            self._expand_hashtags = True
        else:
            self._expand_hashtags = False
        self._stemmer = stemmer

    def _add_special_tag(self, m, tag, mode='single'):

        if isinstance(m, str):
            text = m
        else:
            text = m.group()

        if mode == 'single':
            return ' {} <{}> '.format(text, tag)
        elif mode == 'wrap':
            return ' '.join([' <{}> {} </{}> '.format(tag, text, tag)]) + ' '
        elif mode == 'every':
            tokens = text.split()
            processed = ' '.join([' {} <{}> '.format(t, tag) for t in tokens])
            return ' ' + processed + ' '

    @lru_cache(maxsize=65536)
    def _handle_hashtag_match(self, m):
        expanded = m.group()[1:]
        if self._expand_hashtags:
            expanded = self._segmenter.segment([expanded])[0]
            expanded = ' '.join(expanded.split('-'))
            expanded = ' '.join(expanded.split('_'))

        if 'hashtag' in self._annotate:
            expanded = self._add_special_tag(expanded, 'hashtag', mode='wrap')

        return expanded

    @lru_cache(maxsize=65536)
    def _handle_repeated_puncts(self, m):
        text = m.group()
        text = ''.join(sorted(set(text), reverse=True))

        if 'repeated' in self._annotate:
            text = self._add_special_tag(text, 'repeated')

        return text

    @lru_cache(maxsize=65536)
    def _handle_generic_match(self, m, tag, mode='every'):
        text = m.group()
        text = self._add_special_tag(text, tag, mode=mode)

        return text

    def _handle_elongated_match(self, m):
        text = m.group()
        text = self._regexes['normalize_elong'].sub(r'\1\1', text)
        if self._speller and text.lower() not in _english_words:
            if hasattr(self._speller, 'normalize_elongated'):
                text = _case_of(text)(
                    self._speller.normalize_elongated(text.lower())
                )
            else:
                text = _case_of(text)(self._speller.correct(text.lower()))
        if 'elongated' in self._annotate:
            text = self._add_special_tag(text, 'elongated')
        return text

    @lru_cache(maxsize=65536)
    def _handle_emphasis_match(self, m):
        text = m.group().replace('*', '')
        if 'emphasis' in self._annotate:
            text = self._add_special_tag(text, 'emphasis')

        return text

    def _dict_replace(self, wordlist, _dict):
        return [_dict.get(w, w) for w in wordlist]

    @staticmethod
    def text(wordlist):
        in_hashtag = False
        _words = []
        for word in wordlist:

            if word == '<hashtag>':
                in_hashtag = True
            elif word == '</hashtag>':
                in_hashtag = False
            elif word in {'<allcaps>', '</allcaps>'} and in_hashtag:
                continue

            _words.append(word)

        return _words

    def process(self, text):
        text = re.sub(r' +', ' ', text)
        if self._fix_unidecode:
            text = ftfy.fix_text(text)

        for item in self._normalize:
            text = self._regexes[item].sub(
                lambda m: ' ' + '<' + item + '>' + ' ', text
            )

        text = self._regexes['hashtag'].sub(
            lambda w: self._handle_hashtag_match(w), text
        )

        if 'allcaps' in self._annotate:
            text = self._regexes['allcaps'].sub(
                lambda w: self._handle_generic_match(
                    w, 'allcaps', mode=self._all_caps_tag
                ),
                text,
            )
        if 'elongated' in self._annotate:
            text = self._regexes['elongated'].sub(
                lambda w: self._handle_elongated_match(w), text
            )
        if 'repeated' in self._annotate:
            text = self._regexes['repeat_puncts'].sub(
                lambda w: self._handle_repeated_puncts(w), text
            )
        if 'emphasis' in self._annotate:
            text = self._regexes['emphasis'].sub(
                lambda w: self._handle_emphasis_match(w), text
            )
        if 'censored' in self._annotate:
            text = self._regexes['censored'].sub(
                lambda w: self._handle_generic_match(w, 'censored'), text
            )
        if self._expand_contractions:
            text = unpack_english_contractions(text)

        text = re.sub(r' +', ' ', text)
        text = self.text(text.split())
        text = ' '.join(text)
        text = self._tokenizer(text)
        text = self._dict_replace(text, rules_normalizer)
        if self._translator:
            text = self._dict_replace(text, self._translator)
        if self._stemmer:
            rejected = ['<', '</', '>', '>']
            text = [
                self._stemmer.stem(w)
                if (
                    w not in _english_words
                    and all([r not in w for r in rejected])
                )
                else w
                for w in text
            ]

        return text

def preprocessing(
    normalize: List[str] = [
        'url',
        'email',
        'percent',
        'money',
        'phone',
        'user',
        'time',
        'date',
        'number',
    ],
    annotate: List[str] = [
        'allcaps',
        'elongated',
        'repeated',
        'emphasis',
        'censored',
        'hashtag',
    ],
    lowercase: bool = True,
    fix_unidecode: bool = True,
    expand_english_contractions: bool = True,
    translate_english_to_bm: bool = True,
    speller=None,
    segmenter=None,
    stemmer=None,
    **kwargs,
):
    """
    Load Preprocessing class.

    Parameters
    ----------
    normalize: list
        normalizing tokens, can check all supported normalizing at `malaya.preprocessing.get_normalize()`.
    annotate: list
        annonate tokens <open></open>,
        only accept ['hashtag', 'allcaps', 'elongated', 'repeated', 'emphasis', 'censored'].
    lowercase: bool
    fix_unidecode: bool
    expand_english_contractions: bool
        expand english contractions
    translate_english_to_bm: bool
        translate english words to bahasa malaysia words
    speller: object
        spelling correction object, need to have a method `correct`
    segmenter: object
        segmentation object, need to have a method `segment`.
        If provide, it will expand hashtags, #mondayblues == monday blues
    stemmer: object
        stemmer object, need to have a method `stem`.
        If provide, it will stem or lemmatize the string.

    Returns
    -------
    result : malaya.preprocessing.Preprocessing class
    """

    if any([e not in _normalize for e in normalize]):
        raise ValueError(
            'normalize element not able to recognize, supported normalization can check at get_normalize()'
        )
    if any([e not in _annotate for e in annotate]):
        raise ValueError(
            "annotate only accept ['hashtag', 'allcaps', 'elongated', 'repeated', 'emphasis', 'censored']"
        )
    validator.validate_object_methods(
        speller, ['correct', 'normalize_elongated'], 'speller'
    )
    validator.validate_object(segmenter, 'segment', 'segmenter')
    validator.validate_object(stemmer, 'stem', 'stemmer')

    if translate_english_to_bm:
        path = check_file(
            PATH_PREPROCESSING['english-malay'],
            S3_PATH_PREPROCESSING['english-malay'],
            **kwargs,
        )
        try:
            with open(path['model']) as fopen:
                translator = json.load(fopen)
        except BaseException:
            raise Exception(
                "failed to load english-malay vocab, please run `malaya.utils.delete_cache('preprocessing/english-malay')`")
    else:
        translator = None

    return Preprocessing(
        normalize=normalize,
        annotate=annotate,
        lowercase=lowercase,
        fix_unidecode=fix_unidecode,
        expand_english_contractions=expand_english_contractions,
        translator=translator,
        speller=speller,
        segmenter=segmenter,
        stemmer=stemmer,
    )


import data_conversion1
import knn_kdtree1
import svm
from numpy import *
import uuid
from flask import Flask, abort, request, jsonify
import face_recognition
import constant
import knn_analysis
from time import time
import os
import data_preprocess

UPLOAD_FOLDER = constant.UPLOAD_PATH
ALLOW_EXTENSIONS = set(['jpg', 'png', 'pgm', 'tiff'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
#判断文件夹是否存在，如果不存在则创建
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)
else:
    pass
# 判断文件后缀是否在列表中
def allowed_file(filename):
    return '.' in filename and \
            filename.rsplit('.', 1)[1] in ALLOW_EXTENSIONS

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/knn-find-face', methods=['POST'])
def knn_findFace():
	'''
	post 传输接受参数例子
	if not request.json or 'id' not in request.json or 'info' not in request.json:
        abort(400)
    task = {
        'id': request.json['id'],
        'info': request.json['info']
    }
	'''
	if not request.files or 'file' not in request.files:
		return buildResponse({}, 400, 'no file upload')
	file_path = ''
	#获取post过来的文件名称，从name=file参数中获取
	file = request.files['file']
	if file and allowed_file(file.filename):
		filename = file.filename
		file_name = str(uuid.uuid4()) + '.' + filename.rsplit('.', 1)[1]
		file.save(app.config['UPLOAD_FOLDER'] + constant.SLASH + file_name)
		file_path = app.config['UPLOAD_FOLDER'] + constant.SLASH + file_name
	if file_path == '':
		return buildResponse({}, 400, 'no file upload')

	# 对上传图片进行人脸检测
	draw_face = face_recognition.saveFaces(file_path, app.config['UPLOAD_FOLDER'])
	if draw_face == '':
		return buildResponse({}, 400, 'no face in upload file')
	start_time = time()

	path = constant.JAFFE['last_path']
	# 获取样本图片原始数据
	train_data, train_labels = data_conversion1.loadDataSet(path)
	data_train_new,data_mean,V = data_conversion1.pca(train_data, knn_analysis.BEST_DIM)
	test_path = data_preprocess.doPreprocess(file_path, constant.JAFFE['last_height'], constant.JAFFE['last_weight']) # 图片预处理
	test_face = data_conversion1.img2vector(test_path, (constant.JAFFE['last_height'], constant.JAFFE['last_weight']))
	num_test = test_face.shape[0]
	temp_face = test_face - tile(data_mean,(num_test,1))
	data_test_new = temp_face*V # 得到测试脸在特征向量下的数据
	data_test_new = array(data_test_new)
	outputLabel = knn_kdtree1.findSimilarLable(data_train_new, train_labels, data_test_new[0,:], knn_analysis.BEST_K)
	return buildResponse({'draw_face_path': draw_face, 'face_name': outputLabel, 'cost_time': round(time() - start_time, 2)})

@app.route('/knn-accucency')
def knn_accucency():
	precision, cost_time = knn_analysis.precisionWithBestKAndDim()
	return buildResponse({'precision': round(precision, 3), 'cost_time': round(cost_time, 2)})

@app.route('/svm-accucency')
def svm_accucency():
	precision, cost_time = svm.pcaAndSvmFaceFindAnalysis()
	return buildResponse({'precision': round(precision, 3), 'cost_time': round(cost_time, 2)})

@app.route('/svm-find-face', methods=['POST'])
def svm_findFace():
	if not request.files or 'file' not in request.files:
		return buildResponse({}, 400, 'no file upload')
	file_path = ''
	#获取post过来的文件名称，从name=file参数中获取
	file = request.files['file']
	if file and allowed_file(file.filename):
		filename = file.filename
		file_name = str(uuid.uuid4()) + '.' + filename.rsplit('.', 1)[1]
		file.save(app.config['UPLOAD_FOLDER'] + constant.SLASH + file_name)
		file_path = app.config['UPLOAD_FOLDER'] + constant.SLASH + file_name
	if file_path == '':
		return buildResponse({}, 400, 'no file upload')

	# 对上传图片进行人脸检测
	draw_face = face_recognition.saveFaces(file_path, app.config['UPLOAD_FOLDER'])
	if draw_face == '':
		return buildResponse({}, 400, 'no face in upload file')
	path = constant.JAFFE['last_path']
	outputLabel, cost_time = svm.pcaAndSvmFaceFind(path, file_path)
	return buildResponse({'draw_face_path': draw_face ,'face_name': outputLabel, 'cost_time': round(cost_time, 2)})

def buildResponse(data, code = 0, message = 'success'):
	return jsonify({'code': code, 'data': data,'message': message})

if __name__ == '__main__':
    app.run(debug=True)


#WAP to reverse a 4-digit number using mathematical operator.
a=int(input("Enter your number :"))
rev=0
while(a>0):
    rem=a%10
    rev=(rev*10)+rem
    a=a//10
print(f"Reverse Number : {rev}")
# output :-
# Enter your number :6777
# Reverse Number : 7776


import re

r_space = r"\s*"
r_date = r"(?P<date>\d\d-\d\d)"
r_time = r"(?P<time>\d\d:\d\d:\d\d.\d\d\d)"
r_pid = r"(\d+)"
r_level = r"(?P<level>.)"
# This needs to be greedy
r_prefix = r"(?P<prefix>.*?\s*:)"
r_message = r"(?P<message>.*)"
# Get last occurance
r_json = r"(?P<pre_json>.*)(?P<json>^\{.*\}$)(?P<post_json>.*)"

line_regex_parts = [r_date, r_time, r_pid, r_pid, r_level, r_prefix, r_message]
line_regex = r_space.join(line_regex_parts)
line_regex = re.compile(r_space.join(line_regex_parts))
json_regex = re.compile(r_json)

# meta
r_meta_line = r".*\"meta\":.*(?P<level>INFO|DEBUG|WARN|ERROR) (?P<name>.*): (?P<message>.*) (?P<json>\{.*)$"
meta_regex = re.compile(r_meta_line)


from typing import List
from collections import defaultdict, deque
import sys

class Solution:
    def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
        if not edges:
            return [0]

        connections = defaultdict(list)

        for a, b in edges:
            connections[a].append(b)
            connections[b].append(a)

        nl = n
        leaves = []

        for node in connections.keys():
            if len(connections[node]) == 1:
                leaves.append(node)

        while nl > 2:
            nl -= len(leaves)
            new_leaves = []

            for leaf in leaves:
                cur = connections[leaf].pop()
                connections[cur].remove(leaf)

                if len(connections[cur]) == 1:
                    new_leaves.append(cur)

            leaves = new_leaves

        return leaves

print(Solution().findMinHeightTrees(4,
[[1,0],[1,2],[1,3]]))


class Chars:
    ADD = "+"  # addition
    SUB = "-"  # subtraction
    MUL = "*"  # multiplication
    DIV = "/"  # integer division
    MOD = "%"  # modulo
    NOT = "!"  # NOT
    GRT = "`"  # greater than
    M_R = ">"  # move right
    M_D = "v"  # move down
    M_L = "<"  # move left
    M_U = "^"  # move up
    RAN = "?"  # random direction
    REZ = "_"  # horizontal if condition
    DEZ = "|"  # vertical if condition
    SSM = "\""  # switch stringmode
    DUP = ":"  # duplicate top element of stack
    SWP = "\\"  # swap top elements of stack
    PAD = "$"  # pop and discard top element of stack
    POI = "."  # pop and output top element of stack as integer
    POC = ","  # pop and output top element of stack as character
    BRD = "#"  # bridge
    PUT = "p"  # put
    GET = "g"  # get
    INI = "&"  # input integer
    INC = "~"  # input character
    END = "@"  # end program

    texmap = {
        ADD: '\\texttt{+}',
        SUB: '\\texttt{-}',
        MUL: '\\texttt{*}',
        DIV: '\\texttt{/}',
        MOD: '\\texttt{\\%}',
        NOT: '\\texttt{!}',
        GRT: '\\texttt{`}',
        M_R: '\\texttt{>}',
        M_D: '\\lor',
        M_L: '\\texttt{<}',
        M_U: '\\land',
        RAN: '\\texttt{?}',
        REZ: '\\texttt{\\_}',
        DEZ: '\\texttt{|}',
        SSM: '\\texttt{"}',
        DUP: '\\texttt{:}',
        SWP: '\\setminus',
        PAD: '\\texttt{\\$}',
        POI: '\\texttt{.}',
        POC: '\\texttt{,}',
        BRD: '\\texttt{\\#}',
        PUT: '\\texttt{p}',
        GET: '\\texttt{g}',
        INI: '\\texttt{\\&}',
        INC: '\\sim',
        END: '\\texttt{@}',
        '1': '\\texttt{1}',
        '2': '\\texttt{2}',
        '3': '\\texttt{3}',
        '4': '\\texttt{4}',
        '5': '\\texttt{5}',
        '6': '\\texttt{6}',
        '7': '\\texttt{7}',
        '8': '\\texttt{8}',
        '9': '\\texttt{9}',
        ' ': '\\texttt{" "}'
    }


import torch
from torch.optim import Adam, Adagrad, Adadelta, RMSprop, SGD

from yolo_trainer import YoloTrainer
from yolo import Yolov3
import config
from utils import getLoaders, getValLoader

if torch.cuda.is_available():
    torch.cuda.empty_cache()

# ------------------------------------------------------
def run(model: Yolov3, optimizer: torch.optim):

    t = YoloTrainer()
    container = {'architecture': config.yolo_config}
    # container = YoloTrainer.loadModel('rms_normal_loss')
    # t.trainNet(model, optimizer, load=container)

    try:
        t.trainNet(model, optimizer=algorithm)
        # t.trainNet(model, optimizer, load=container)

    except KeyboardInterrupt as e:
        print('[YOLO TRAINER]: KeyboardInterrupt', e)

    except Exception as e:
        print(e)

    finally:
        savestr = f'optim_{type(optimizer).__name__}'
        YoloTrainer.saveModel(savestr, t.model, t.optimizer, t.supervisor)

# ------------------------------------------------------
def createModel():

    return  Yolov3(config.yolo_config)

# # ------------------------------------------------------
# def createSamples():

#     samples = dict()

#     adam_model = Yolov3(config.yolo_config)
#     adam = Adam(
#         [
#             {'params': adam_model.yolo.parameters()},
#             {'params': adam_model.darknet.parameters()}
#         ],
#         lr=config.LEARNING_RATE
#     )

#     adagrad_model = Yolov3(config.yolo_config)
#     adagrad = Adagrad(adagrad_model.parameters(), lr=config.LEARNING_RATE)

#     adadelta_model = Yolov3(config.yolo_config)
#     adadelta = Adadelta(adadelta_model.parameters(), lr=config.LEARNING_RATE)

#     rmsprop_model = Yolov3(config.yolo_config)
#     rmsprop = RMSprop(rmsprop_model.parameters(), lr=config.LEARNING_RATE)

#     sgd_model = Yolov3(config.yolo_config)
#     sgd = SGD(sgd_model.parameters(), lr=config.LEARNING_RATE)

#     samples = {
#         'adam': [adam_model, adam],
#         'adagrad': [adagrad_model, adagrad],
#         'adadelta': [adadelta_model, adadelta],
#         'rmsprop': [rmsprop_model, rmsprop],
#         'sgd': [sgd_model, sgd]
#     }

#     return samples

# ------------------------------------------------------
if __name__ == "__main__":

    # adam_model = Yolov3(config.yolo_config)
    # adam = Adam(
    #     [
    #         {'params': adam_model.yolo.parameters()},
    #         {'params': adam_model.darknet.parameters()}
    #     ],
    #     lr=config.LEARNING_RATE
    # )

    # adagrad_model = Yolov3(config.yolo_config).to(config.DEVICE)
    # adagrad = Adagrad(
    #     [
    #         {'params': adagrad_model.yolo.parameters()},
    #         {'params': adagrad_model.darknet.parameters()}
    #     ],
    #     lr=config.LEARNING_RATE
    # )

    rmsprop_model = Yolov3(config.yolo_config).to(config.DEVICE)
    rmsprop = RMSprop(
        [
            {'params': rmsprop_model.yolo.parameters()},
            {'params': rmsprop_model.darknet.parameters()}
        ],
        lr=config.LEARNING_RATE
    )

    run(rmsprop_model, rmsprop)

    # sgd_model = Yolov3(config.yolo_config).to(config.DEVICE)
    # sgd = SGD(
    #     [
    #         {'params': sgd_model.yolo.parameters()},
    #         {'params': sgd_model.darknet.parameters()}
    #     ],
    #     lr=config.LEARNING_RATE
    # )

    # run(sgd_model, sgd)

    # adadelta_model = Yolov3(config.yolo_config).to(config.DEVICE)
    # adadelta = Adadelta(
    #     [
    #         {'params': adadelta_model.yolo.parameters()},
    #         {'params': adadelta_model.darknet.parameters()}
    #     ],
    #     lr=config.LEARNING_RATE
    # )

    # run(rmsprop_model, rmsprop)

    # rmsprop_model = Yolov3(config.yolo_config)
    # rmsprop = RMSprop(rmsprop_model.yolo.parameters(), lr=config.LEARNING_RATE)
    # rmsprop = Adam(rmsprop_model.yolo.parameters(), lr=config.LEARNING_RATE)

    # t = YoloTrainer((getValLoader([25, 30, 35, 40, 45]), getValLoader([25, 30, 35, 40, 45])))
    # t = YoloTrainer()

    # container = {'architecture': config.yolo_config}
    # container = YoloTrainer.loadModel('ultralytics_focal_loss')

    # # t.trainNet(rmsprop_model, rmsprop)
    # # t.trainNet(rmsprop_model, rmsprop, load=container)

    # try:
    #     t.trainNet(rmsprop_model, rmsprop)
    #     # t.trainNet(rmsprop_model, rmsprop, load=container)

    # except KeyboardInterrupt as e:
    #     print('[YOLO TRAINER]: KeyboardInterrupt', e)

    # except Exception as e:
    #     print(e)

    # finally:
    #     savestr = f'optim_{type(t.optimizer).__name__}'
    #     YoloTrainer.saveModel(savestr, t.model, t.optimizer, t.supervisor)


import random
import tkinter

a = random.randint(1,2)
b = input("Орёл или решка?: ")

if b == "орёл" and a == 1:
  print("Орёл, вы выйграли!!!")

if b == "решка" and a == 2:
  print("Решка, вы выйграли!!!")

if b == "орёл" and a == 2:
  print("Решка, вы проиграли! Не огорчайтесь, повезёт в следуйщий раз")

if b == "решка" and a == 2:
  print("Орёл, вы проиграли! Не огорчайтесь, повезёт в следуйщий раз")


# Create your views here.
from django.shortcuts import render_to_response
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.template.context import RequestContext
from models import *
from itertools import chain
from collections import defaultdict

def mzml(request):
    allexp = Experiment.objects.all()
    paginator = Paginator(allexp, 1)  # show 10 mzml per page

    page = request.GET.get('page')
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        contacts = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        contacts = paginator.page(paginator.num_pages)

    return render_to_response('mzml.html', {"exp": contacts},
                                context_instance=RequestContext(request))

def pepxml(request):
    allpepxml = pepXML.objects.all()
    paginator = Paginator(allpepxml, 1)  # show 10 mzml per page

    page = request.GET.get('page')
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        contacts = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        contacts = paginator.page(paginator.num_pages)

    return render_to_response('pepxml.html', {"pepxml": contacts},
                                context_instance=RequestContext(request))

def index(request):
    expcount = Experiment.objects.count()
    allmass = SelectedIon.objects.count()
    allpep = pepXML.objects.count()
    lastexp = Experiment.objects.latest('id')
    #lastpep = pepXML.objects.latest('id')
    #cid = request.POST['cnum']
    #fid = request.FILES['myfile']
    errors = []

    """
    1) check whether post is from input text and it's not blank.
    2) get all forms request values mz, searchdate, search space, charge state
    and ppm.
    3) if charge state is none (default) then calculate lower range and upper
    ppm range.
    4) if charge state is provided in the request then protonated it by
    multiplying with 1.00727638 and caculate lower and upper ppm mass range.
    """
    if 'textmz' in request.POST and request.POST['textmz'] != '' and \
    'myfile' not in request.FILES:
        cid = request.POST['textmz']
        searchdate = request.POST['fdate1']
        sspace = request.POST['r']
        cstate = request.POST['ch']
        ppm = request.POST.__getitem__('ppm')

        if request.POST['ch'] == 'none':
            defmass = float(ppm) / int(1000000)
            massvalue = float(cid)
            masscon = defmass * massvalue
            highrange = massvalue + masscon
            lowrange = massvalue - masscon
            prec = request.POST['prec']
        elif request.POST['ch'] == 'neutral':
            prec = request.POST['prec']
            masscon, mass_with_charges = [], []

            for charge in range(1, 6):
                mass_with_charges.append((float(cid) + (charge * 1.00727638)) / charge)
            defmass = float(5) / int(1000000)
            [masscon.append(defmass * item) for item in mass_with_charges]
            upper_mass_range = [sum(pair) for pair in zip(mass_with_charges, masscon)]
            lower_mass_range = [a - b for a, b in zip(mass_with_charges, masscon)]
        else:
            defmass = float(ppm) / 1000000
            charge = int(cstate) * float(1.00727638)
            cm = float(cid)
            massvalue = (cm + charge) / int(cstate)
            masscon = defmass * massvalue
            highrange = massvalue + masscon
            lowrange = massvalue - masscon
            prec = request.POST['prec']
        # check all requests/input and perform query
        if searchdate == "" and sspace == 'all':
            if request.POST['ch'] == 'none':
                myquery = SelectedIon.objects.select_related(). \
                            filter(monoiso__range=(lowrange, highrange)). \
                            order_by('monoiso')
                querycount = myquery.count()
            elif request.POST['ch'] == 'neutral':
                myquery, queryresult = [], []
                for x, y in zip(lower_mass_range, upper_mass_range):
                    result = SelectedIon.objects.select_related() \
                                .filter(monoiso__range=(x, y)) \
                                .order_by('monoiso')
                    queryresult.append(result)
                # remove empty item list from the myquery list
                myquery = [x for x in queryresult if x]
                # make a flat list from a list of lists
                myquery = [item for sublist in myquery for item in sublist]
                querycount = len(myquery)
                myquerylist = list(myquery)
                chargecount, masses= [], []
                mass_with_charge = []
                for chargemass in myquerylist:
                    charge = str(chargemass.chargestate)
                    chargecount.append(chargemass.chargestate)
                    masses.append(chargemass.monoiso)
                    mass_with_charge.append([int(chargemass.chargestate), chargemass.monoiso],)
                #mass_dict = {{t[0]:t[1:],} for t in mass_with_charge}
                mass_dict = dict(mass_with_charge)
                d1 = defaultdict(list)
                for k, v in mass_with_charge:
                    d1[k].append(v)
                d = dict((k, tuple(v)) for k ,v in d1.iteritems())
                mytype = type(d)

            else:
                myquery = SelectedIon.objects.select_related() \
                            .filter(chargestate__iexact=cstate) \
                            .filter(monoiso__range=(lowrange, highrange)) \
                            .order_by('monoiso')
                querycount = myquery.count()

        elif searchdate and sspace == 'all':
            if request.POST['ch'] == 'none':
                myquery = SelectedIon.objects.select_related() \
                            .filter(monoiso__range=(lowrange, highrange)) \
                            .filter(sele_fk__exp__uploaddate__iexact=searchdate) \
                            .order_by('monoiso')
                querycount = myquery.count()
            else:
                myquery = SelectedIon.objects.select_related() \
                            .filter(chargestate__iexact=cstate) \
                            .filter(monoiso__range=(lowrange, highrange)) \
                            .filter(sele_fk__exp__uploaddate__iexact=searchdate) \
                            .order_by('monoiso')
                querycount = myquery.count()
        elif searchdate and sspace == 'recent':
            if request.POST['ch'] == 'none':
                myquery1 = SelectedIon.objects.select_related() \
                            .filter(monoiso__range=(lowrange, highrange)) \
                            .filter(sele_fk__exp__uploaddate__iexact=searchdate) \
                            .reverse()[:5]
                myquery = sorted(myquery1, key=lambda o: o.monoiso)
                querycount = sum(1 for result in myquery)
            else:
                myquery1 = SelectedIon.objects.select_related() \
                .filter(chargestate__iexact=cstate) \
                .filter(monoiso__range=(lowrange, highrange)) \
                .filter(sele_fk__exp__uploaddate__iexact=searchdate) \
                .reverse()[:5]
                myquery = sorted(myquery1, key=lambda o: o.monoiso)
                querycount = sum(1 for result in myquery)

        elif searchdate == "" and sspace == 'recent':
            if request.POST['ch'] == 'none':
                myquery1 = SelectedIon.objects.select_related() \
                            .filter(monoiso__range=(lowrange, highrange))[:5]
                myquery = sorted(myquery1, key=lambda o: o.monoiso)
                querycount = sum(1 for result in myquery)
            else:
                myquery1 = SelectedIon.objects.select_related() \
                            .filter(chargestate__iexact=cstate) \
                            .filter(monoiso__range=(lowrange, highrange))[:5]
                myquery = sorted(myquery1, key=lambda o: o.monoiso)
                querycount = sum(1 for result in myquery)
        if myquery:
            template = 'result.html'
            #'high': highrange,'low': lowrange,'qcount': querycount,
            #'masses': masses,'mass_with_charge': mass_with_charge,
            #'mass_dict':mass_dict,
            #'dict':d, 'mytype': mytype
            return render_to_response(template, {'prec': prec, 'query': cid,
                                                'sections': myquery, 'qcount': querycount,
                                                'cs': cstate, 'ppm': ppm,
                                                'defm': defmass, 'ss': sspace,
                                                'sd': searchdate,
                                                },
                                                context_instance=RequestContext(request))
        else:
            errors.append("*Didn't match with any precursor ion mass in database!!")
            #'pep': lastpep,
            return render_to_response('index.html', {'exp': expcount,
                                        'mass': allmass, 'latest': lastexp, 'pep': allpep,
                                        'errmsg': errors,
                                         },
                                        context_instance=RequestContext(request))
    # check whether post is from UPLOAD FILE and TEXT input is blank.
    # get all forms request values mz, searchdate, search space, charge state
    # and ppm.
    # if charge state is none (default) then calculate lower range and upper
    # ppm range.
    # if charge state is provided in the request then protonate it by multiplying
    # with 1.00727638 and caculate lower and upper ppm mass range
    elif 'myfile' in request.FILES and request.POST['textmz'] == '':
        searchdate = request.POST['fdate1']
        sspace = request.POST['r']
        cstate = request.POST['ch']
        ppm = request.POST.__getitem__('ppm')
        prec = request.POST['prec']
        mfile = request.FILES['myfile']
        # split file name and check if extension is txt
        if mfile.name.split('.')[1] == "txt":
            if request.POST['ch'] == 'none':
                fileid = []
                searchid = []
                masscon = []
                defmass = float(ppm) / int(1000000)
                fread = mfile.readlines()
                [searchid.append(float(item)) for item in fread]
                [fileid.append(float(item)) for item in fread]
                [masscon.append(defmass * item) for item in fileid]
                newlist = [sum(pair) for pair in zip(fileid, masscon)]
                newlist1 = [a - b for a, b in zip(fileid, masscon)]
                #newlist2 = [[x, y] for x, y in zip(newlist, newlist1)]
            else:
                defmass = float(ppm) / int(1000000)
                charge = int(cstate) * float(1.00727638)
                prec = request.POST['prec']
                fileid = []
                searchid = []
                masscon = []
                defmass = float(ppm) / int(1000000)
                fread = mfile.readlines()
                [searchid.append(float(item)) for item in fread]
                [fileid.append(float(item)) for item in fread]
                [masscon.append(defmass * item) for item in fileid]
                newlist = [sum(pair) for pair in zip(fileid, masscon)]
                newlist1 = [a - b for a, b in zip(fileid, masscon)]
                #newlist2 = [[x, y] for x, y in zip(newlist, newlist1)]

            if searchdate == "" and sspace == 'all':
                if request.POST['ch'] == 'none':
                    myresult = []
                    for x, y in zip(newlist1, newlist):
                        myquery = SelectedIon.objects.select_related() \
                                    .filter(monoiso__range=(x, y)) \
                                    .order_by('monoiso')
                        myresult.append(myquery)
                else:
                    myresult = []
                    for x, y in zip(newlist1, newlist):
                        myquery = SelectedIon.objects.select_related() \
                                    .filter(chargestate__iexact=cstate) \
                                    .filter(monoiso__range=(x, y)) \
                                    .order_by('monoiso')
                        myresult.append(myquery)
            elif searchdate and sspace == 'all':
                myresult = []
                if request.POST['ch'] == 'none':
                    for x, y in zip(newlist1, newlist):
                        myquery = SelectedIon.objects.select_related() \
                                    .filter(monoiso__range=(x, y)) \
                                    .filter(sele_fk__exp__uploaddate__iexact=searchdate) \
                                    .order_by('monoiso')
                        myresult.append(myquery)
                else:
                    for x, y in zip(newlist1, newlist):
                        myquery = SelectedIon.objects.select_related() \
                        .filter(chargestate__iexact=cstate) \
                        .filter(monoiso__range=(x, y)) \
                        .filter(sele_fk__exp__uploaddate__iexact=searchdate) \
                        .order_by('monoiso')
                        # end of queryset
                        myresult.append(myquery)
            elif searchdate and sspace == 'recent':
                myresult = []
                if request.POST['ch'] == 'none':
                    for x, y in zip(newlist1, newlist):
                        myquery1 = SelectedIon.objects.select_related() \
                                    .filter(monoiso__range=(x, y)) \
                                    .filter(sele_fk__exp__uploaddate__iexact=searchdate) \
                                    .reverse()[:5]
                        # end of queryset
                        myquery = sorted(myquery1, key=lambda o: o.monoiso)
                        myresult.append(myquery)
                else:
                    for x, y in zip(newlist1, newlist):
                        myquery1 = SelectedIon.objects.select_related(). \
                                    filter(chargestate__iexact=cstate). \
                                    filter(monoiso__range=(x, y)) \
                                    .filter(sele_fk__exp__uploaddate__iexact=searchdate) \
                                    .reverse()[:5]
                        # end of queryset
                        myquery = sorted(myquery1, key=lambda o: o.monoiso)
                        myresult.append(myquery)
            elif searchdate == "" and sspace == 'recent':
                myresult = []
                if request.POST['ch'] == 'none':
                    for x, y in zip(newlist1, newlist):
                        myquery1 = SelectedIon.objects.select_related(). \
                                    filter(monoiso__range=(x, y))[:5]
                        # end of queryset
                        myquery = sorted(myquery1, key=lambda o: o.monoiso)
                        myresult.append(myquery)
                else:
                    for x, y in zip(newlist1, newlist):
                        myquery1 = SelectedIon.objects.select_related() \
                                    .filter(chargestate__iexact=cstate) \
                                    .filter(monoiso__range=(x, y))[:5]
                        # end of queryset
                        myquery = sorted(myquery1, key=lambda o: o.monoiso)
                        myresult.append(myquery)

            # count the length of the myresult queryset list
            querycount = len([num for elem in myresult for num in elem])
            if querycount > 1:
                template = 'result.html'
                return render_to_response(template, {'prec': prec,
                                                    'filesection': myresult,
                                                    'qcount': querycount,
                                                    'range': range(1, (querycount + 1)),
                                                    'cs': cstate, 'ppm': ppm,
                                                    'ss': sspace, 'sd': searchdate,
                                                    'file': mfile,
                                                    'search_id': searchid,
                                                    'fileid': fileid, },
                                                    context_instance=RequestContext(request))
            else:
                errors.append("*Didn't match with any precursor ion masses in database!!!!")
                return render_to_response('index.html', {'exp': expcount,
                                        'mass': allmass, 'latest': lastexp, 'pep': allpep,
                                        'errmsg': errors,
                                         },
                                        context_instance=RequestContext(request))
        else:
            errors.append("*Only text file is suppored!!")
            return render_to_response('index.html', {'exp': expcount, 'mass': allmass,
                                                    'latest': lastexp, 'pep': allpep,
                                                    'errmsg': errors,
                                                    },
                                                     context_instance=RequestContext(request))

    elif 'myfile' in request.FILES and 'textmz' in request.POST:
        errors.append("*Search doesn't accept multiple inputs!!!")
        return render_to_response('index.html', {'exp': expcount, 'mass': allmass, 'pep': allpep,
                                                'latest': lastexp, 'errmsg': errors,
                                                 },
                                                context_instance=RequestContext(request))

    elif ('textmz' in request.POST and request.POST['textmz'] == '') \
        or ('myfile' in request.FILES and request.FILES['myfile'] == ''):
        errors.append("*Search requires one input!!!")
        #'pep': allpep,
        return render_to_response('index.html', {'exp': expcount, 'mass': allmass,
                                                'latest': lastexp, 'errmsg': errors,
                                                 'pep': allpep,},
                                                context_instance=RequestContext(request))

    else:
    	#'pep': allpep,
        return render_to_response('index.html', {'exp': expcount, 'mass': allmass,
                                            'latest': lastexp,
                                            'user': request.user, 'pep': allpep,},
                                             context_instance=RequestContext(request))

def detail(request, match, tab):
    if request.is_ajax():
        spect = Spectrum.objects.filter(spec_fk__id__iexact=match).select_related()
        spect_cv = Spectrum_CV.objects.filter(spec_cv_fk__spec_fk__id__iexact=match)
        scanwin = ScanWindow.objects.filter(scanw_fk__spec_fk__id__iexact=match)
        scan = Scan.objects.filter(scan_fk__spec_fk__id__iexact=match)
        expfile = SelectedIon.objects.get(id=match)
        expname = expfile.sele_fk.exp.filename
        scanlist = list(scan)
        template = 'default.html'
        return render_to_response(template, {'spect': spect, 'spect_cv': spect_cv,
                                            'expname': expname, 'scanw': scanwin,
                                            'scan': scan,'scanlist':scanlist, },
                                            context_instance=RequestContext(request))

def matchdetail(request, match_id, tab_id):
    if request.is_ajax():
        spect = Spectrum.objects.filter(spec_fk__id__iexact=match_id).select_related()
        spect_cv = Spectrum_CV.objects.filter(spec_cv_fk__spec_fk__id__iexact=match_id)
        scanwin = ScanWindow.objects.filter(scanw_fk__spec_fk__id__iexact=match_id)
        scan = Scan.objects.filter(scan_fk__spec_fk__id__iexact=match_id)
        expfile = SelectedIon.objects.get(id=match_id)
        expname = expfile.sele_fk.exp.filename
        scanlist = list(scan)
        # spectid = Spectrum.objects.get(spec_fk__id__iexact=match_id)
        # searchspect_id = spectid.spec_index + 1
        template = 'tab1.html'
        return render_to_response(template, {'spect': spect, 'spect_cv': spect_cv,
                                            'expname': expname, 'scanw': scanwin,
                                            'scan': scan, 'scanlist':scanlist,},
                                            context_instance=RequestContext(request))

def tabdetail(request, tab_id, match_id):
    if request.is_ajax():
        tabvalue = tab_id
        if tabvalue == '1':
            spect = Spectrum.objects.filter(spec_fk__id__iexact=match_id).select_related()
            spect_cv = Spectrum_CV.objects.filter(spec_cv_fk__spec_fk__id__iexact=match_id)
            scanwin = ScanWindow.objects.filter(scanw_fk__spec_fk__id__iexact=match_id)
            scan = Scan.objects.filter(scan_fk__spec_fk__id__iexact=match_id).values()
            scanlist = list(scan)

            template = 'tab1.html'
            return render_to_response(template, {
                                                'scanlist': scanlist, 'spect': spect, 'spect_cv': spect_cv,
                                                'scan': scan, 'scanw': scanwin,},
                                                context_instance=RequestContext(request))
        # end of tab 1
        elif tabvalue == '2':
            spect = Spectrum.objects.get(spec_fk__id__iexact=match_id)
            searchspect_id = spect.spec_index + 1
            expfile = SelectedIon.objects.get(id=match_id)
            expname = expfile.sele_fk.exp.filename
            result = Searchspectrum_Query.objects.filter(spec_query_fk__filename=expname) \
                        .filter(spectrum=searchspect_id)
            # end of queryset
            for i in result:
                spect_id = i.id
            pepsearch = Search_Hit.objects \
                        .filter(search_hit_fk__spec_query_fk__filename=expname) \
                        .filter(search_hit_fk__spectrum=searchspect_id).order_by('hit_rank')
            # end of queryset
            if pepsearch:
                altp = Alternative_Protein.objects \
                        .filter(altpro_fk__spec_query_fk__filename=expname) \
                        .filter(altpro_fk=spect_id)
                # end of queryset
                template = 'tab2.html'
                return render_to_response(template, {'spectquery': result,
                                                    'pepsearch': pepsearch,
                                                    'alt_p': altp, 'spect': spect, },
                                                     context_instance=RequestContext(request))
            else:
                template = 'tab2.html'
                return render_to_response(template, {'error': 'No match found', },
                                        context_instance=RequestContext(request))
        # end of tab 2
        elif tabvalue == '4':
            data = SelectedIon.objects.filter(id=match_id)
            mzml_query = SelectedIon.objects.get(id=match_id)
            mzml_id = str(mzml_query.sele_fk.id)
            sf = Source_File.objects.filter(mzml_fk__id__iexact=mzml_id)
            source = Source_file_CV.objects.filter(sf_fk__id__iexact=mzml_id)
            soft = Software.objects.filter(soft_fk__id__iexact=mzml_id)
            result_list = list(chain(sf, source))
            software = Software_CV.objects.filter(soft_cv_fk__id__iexact=mzml_id).select_related()
            datap = DataProcessing.objects.filter(data_fk__id__iexact=mzml_id)
            data_p = Data_Pro_CV.objects.filter(pro_meth_fk__data_p_fk__data_fk__id__iexact=mzml_id).select_related()
            inss = InsConSource.objects.filter(inss_fk_id__id__iexact=mzml_id)
            insa = InsConAnalyzer.objects.filter(insa_fk_id__id__iexact=mzml_id)
            insd = InsConDetector.objects.filter(insd_fk_id__id__iexact=mzml_id)
            insid = InstrumentConfiguration.objects.filter(ins_pro_fk__id__iexact=mzml_id)
            instru_list = list(chain(inss, insa, insd))
            template = 'tab4.html'
            return render_to_response(template, {'instrument_list': instru_list,
                                                'ins_id': insid, 'results': data,
                                                'source': result_list, 'sf': sf,
                                                'scv': source, 'software': software,
                                                'datap': datap, 'datapro': data_p,
                                                'inss': inss, 'insa': insa,
                                                'insd': insd, 'soft': soft, },
                                                context_instance=RequestContext(request))
        # end of tab 4
        else:
            spect = Spectrum.objects.get(spec_fk__id__iexact=match_id)
            searchspect_id = spect.spec_index + 1
            expfile = SelectedIon.objects.get(id=match_id)
            expname = expfile.sele_fk.exp.filename
            result = Searchspectrum_Query.objects.filter(spec_query_fk__filename=expname).filter(spectrum=searchspect_id)
            for i in result:
                spect_id = i.id
            setting = Search_Setting.objects.filter(search_fk__filename=expname)
            template = 'tab5.html'
            return render_to_response(template, {'ssetting': setting, },
                                        context_instance=RequestContext(request))


import sys
input = sys.stdin.readline

dwarfs = []
for _ in range(9):
    dwarfs.append(int(input()))

total = sum(dwarfs)
for i in range(9):
    for j in range(i + 1, 9):
        if total - (dwarfs[i] + dwarfs[j]) == 100:
            dwarf1 = dwarfs[i]
            dwarf2 = dwarfs[j]
            dwarfs.remove(dwarf1)
            dwarfs.remove(dwarf2)
            break
    if len(dwarfs) < 9:
        break

for dwarf in dwarfs:
    print(dwarf)


from PyQt5 import QtWidgets
from PyQt5 import uic
import os
import sys
import logging as log
log.basicConfig(level=log.INFO)
logging = log.getLogger(__name__)

class ScriptHelpWidget(QtWidgets.QWidget):
    def __init__(self, selfself):
        super(ScriptHelpWidget, self).__init__()
        if getattr(sys, 'frozen', False):
            # frozen
            packagedir = os.path.dirname(sys.executable)+'/RTOC/RTOC_GUI'
        else:
            # unfrozen
            packagedir = os.path.dirname(os.path.realpath(__file__))
        uic.loadUi(packagedir+"/ui/scriptHelpWidget.ui", self)
        self.self = selfself
        self.logger = self.self.logger

        self.listWidget.clicked.connect(self.addFuncToScriptEdit)
        self.signalListWidget.clicked.connect(self.addSignalToScriptEdit)
        self.listWidget_default.clicked.connect(self.addDefaultToScriptEdit)
        self.listWidget_default_2.clicked.connect(self.addDefaultToScriptEdit2)

    def addFuncToScriptEdit(self):
        text = self.listWidget.selectedItems()[0].text()
        self.self.addTextToScriptEdit(text)

    def addSignalToScriptEdit(self):
        text = self.signalListWidget.selectedItems()[0].text()
        self.self.addTextToScriptEdit(text)

    def addDefaultToScriptEdit(self):
        text = self.listWidget_default.selectedItems()[0].text()
        self.self.addTextToScriptEdit(text)

    def addDefaultToScriptEdit2(self):
        text = self.listWidget_default_2.selectedItems()[0].text()
        self.self.addTextToScriptEdit(text)

    def updateListWidget(self):
        self.listWidget.clear()
        self.signalListWidget.clear()
        self.signalListWidget.addItem("clock")
        for element in self.logger.pluginParameters.keys():
            if self.logger.pluginStatus[element.split('.')[0]] == True:
                if element.split(".")[1] not in ["run", "smallGUI", 'widget', 'samplerate','lockPerpetialTimer']:
                    self.listWidget.addItem(element)
        for element in self.logger.pluginFunctions.keys():
            if self.logger.pluginStatus[element.split('.')[0]] == True:
                if element.split(".")[1] not in ["loadGUI", "updateT", "stream", "plot", "event", "createTCPClient", "close", "cancel", "start", "setSamplerate","setDeviceName",'setPerpetualTimer','setInterval','getDir']:
                    parStr = ', '.join(self.logger.pluginFunctions[element][1])
                    self.listWidget.addItem(element+"("+parStr+")")
        for element in self.logger.database.signalNames():
            self.signalListWidget.addItem(".".join(element))

    def closeEvent(self, event, *args, **kwargs):
        self.self.helpButton.setChecked(False)
        super(ScriptHelpWidget, self).closeEvent(event)


import sys
'''
    0 --- 1
    |    / \
    |   /   4
    |  /   /
    2 --- 3
    0->2 : 1    2->3 : 4
    0->1 : 4    3->4 : 4
    1->2 : 2    1->4 : 4
'''
class Graph:
    def __init__(self,vert):
        self.vert = vert
        self.graph = [[0 for i in range(self.vert)]for j in range(self.vert)]
    def MinDist(self,dist,visited):
        min = sys.maxsize
        for v in range(self.vert):
            if dist[v]<min and visited[v] == False:
               min = dist[v]
               vert = v
        return vert
    def Dijksrra(self,start_node):
        dist = [sys.maxsize] * self.vert
        visited = [False] * self.vert
        dist[start_node] = 0
        for i in range(self.vert):
            u = self.MinDist(dist,visited)
            visited[u] = True
            for j in range(self.vert):
                if self.graph[u][j]>0 and visited[j] == False and dist[j] > dist[u] + self.graph[u][j]:
                    dist[j] = dist[u] + self.graph[u][j]
        self.print_sol(dist)
    def print_sol(self,dist):
        print("Distance from source to :-")
        for vert in range(self.vert):
            print(vert," = ",dist[vert])
g = Graph(5)
g.graph = [[0,4,1,0,0],
           [4,0,2,0,4],
           [1,2,0,4,0],
           [0,0,4,0,4],
           [0,4,0,4,0]]
g.Dijksrra(0)


import os
import datetime

# Define function to create a unique filename and write to it
def create_unique_file(code):
    # Get the current timestamp and format it to a string
    timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")

    # Create a base filename and append the timestamp to create a unique filename
    base_filename = "output"
    filename = f"{base_filename}_{timestamp}.py"

    # Check if a file already exists with the same name
    counter = 1
    while os.path.isfile(filename):
        # If a file exists, append an additional number to the filename to make it unique
        filename = f"{base_filename}_{timestamp}_{counter}.py"
        counter += 1

    # Open the file with the unique filename in write mode
    with open(filename, "w") as file:
        # Write the extracted code to the file
        file.write(code)

    # Return the unique filename
    return filename

def read_from_file(subdirectory, filename):
    """
    Reads the content of a file from the specified subdirectory and filename.
    :param subdirectory: The subdirectory inside the 'prompts' directory where the file is located.
    :param filename: The name of the file to read.
    :return: The content of the file as a string.
    """
    path = os.path.join('prompts', subdirectory, filename)
    with open(path, 'r') as file:
        return file.read().strip()

def write_to_file(subdirectory, filename, content):
    """
    Writes content to a file in the specified subdirectory and filename.
    :param subdirectory: The subdirectory inside the 'prompts' directory where the file will be created/overwritten.
    :param filename: The name of the file to write.
    :param content: The content to write to the file.
    """
    path = os.path.join('prompts', subdirectory, filename)
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, 'w') as file:
        file.write(content)

# Additional utility functions for file handling can be added here.

def save_conversation(messages, filename='saved_conversations.txt'):
    while True:
        save_input = input("Would you like to save any portions of the conversation? (Y/N/EXIT): ")
        if save_input.upper() == 'Y':
            for message in messages:
                print(f"\n[Message Type]: {message['role']}\n[Content]: {message['content']}")
                save_message = input("Would you like to save this message? (Y/N): ")
                if save_message.upper() == 'Y':
                    with open(filename, 'a') as file:
                        file.write(f"[Message Type]: {message['role']}\n[Content]: {message['content']}\n\n")
        elif save_input.upper() == 'EXIT':
            break


import socket

url = input("Enter URL: ")
url_divided = url.split("/")
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((url_divided[2], 80))
cmd = 'GET {} HTTP/1.0\r\n\r\n'.format(url).encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
    print(data.decode(), end='')

mysock.close()

pybook/chapter 12- Networked Programs/168-1/168-1.py


#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#\|
#=#| Author: Danny Ly MugenKlaus|RedKlouds
#=#| File:   helper_methods.py
#=#| Date:   3/10/2018
#=#|
#=#| Program Desc: This is a group of multiple helper methods
#=#|
#=#| Usage: Assist in the program flow
#=#|
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#\|
import numpy as np
from network import Network
import random

def downsample(image):
    """
    reduce the resolution of the image
    """
    return image[::2, ::2, :]
def remove_color(image):
    """
    Removes all the Color of the raw image
    """
    return image[:, :, 0]

def remove_background(image):
    """
    :param image:
    :return:
    """
    image[image == 144] = 0
    image[image == 109] = 0
    return image

def choose_action(probability):

    random_value = np.random.uniform()
    #print("probability" + str(random_value))
    if random_value < probability:
        # signifies up in openai gym
        return 2
    else:
        # signifies down in openai gym
        return 3

def preprocess_observation(input_observation, prev_processed_observation, input_dimensions):
    """ convert the 210x160x3 uint8 frame into a 6400 float vector """
    processed_observation = input_observation[35:195]  # crop
    processed_observation = downsample(processed_observation)
    processed_observation = remove_color(processed_observation)
    processed_observation = remove_background(processed_observation)
    processed_observation[processed_observation != 0] = 1  # everything else (paddles, ball) just set to 1
    # Convert from 80 x 80 matrix to 1600 x 1 matrix
    processed_observation = processed_observation.astype(np.float).ravel()

    # subtract the previous frame from the current one so we are only processing on changes in the game
    if prev_processed_observation is not None:
        input_observation = processed_observation - prev_processed_observation
    else:
        input_observation = np.zeros(input_dimensions)
    # store the previous frame so we can subtract from it next time
    prev_processed_observations = processed_observation

    return input_observation, prev_processed_observations

def _makeDerivativeMatrix(self, index, a):
    """
        Computes the corresponding jaccbian matrix for the derivative matrix
        F^1(n-1)

        Precondition: feed forward must've been run prior to this function call
        :param index:
        :return: None
        """
    num_neurons = a
    jaccob_matrix = np.zeros(shape=(num_neurons, num_neurons))  # ie S=3, shape 3X3
    # dx_func = self.__getDerivative(self.layers[index]['trans_func'])
    dx_func = self._getTransFunc(self.layers[index]['trans_func']).derivative
    for i in range(num_neurons):
        # diagonal matrix
        a_val = self.layers[index]['a_output'][i]
        jaccob_matrix[i][i] = dx_func(a_val)
    return jaccob_matrix

def update_weights(weights, exp_g_sq, g_dict, decay_r, learn_r):
    """
    Standard Konnen Weight updating
    :param weights: Dict/Hash, weights of the network trying to update
    :param exp_g_sq: Numpy, Vector, of the gradients squared
    :param g_dict: Numpy,Vector, gradient values for each frame
    :param decay_r: Float, Decay Rate to be applied
    :param learn_r: Float, Learning Rate to be applied
    :return: None
    """
    eps = 1e-5
    for layer in weights.keys():
        g = g_dict[layer]
        exp_g_sq[layer] = decay_r * exp_g_sq[layer] + (1 - decay_r) * g ** 2

        weights[layer] += (learn_r * g) / (np.sqrt(exp_g_sq[layer] + eps))

        g_dict[layer] = np.zeros_like(weights[layer])
def ReLu(vector):
    """
    ReLu activation function
    :param vector: Vector, Numpy
    :return: Vector
    """
    vector[vector < 0] = 0
    return vector

def compute_gradient(gradient_log_p, hidden_layer_values, observation_values, weights):
    """
    Compute the gradient, using backprop given the reward/error values for each frame within
    the playing field
    :param gradient_log_p: Numpy, Vector Array
    :param hidden_layer_values:  Numpy, Matrix Array of the weights for the hidden layer for
    each frame
    :param observation_values: Numpy,Matrix each raw frame by frame values
    :param weights: Numpy, Matrix, the weights of the respective neural network
    :return: Dict of the gradients used to update the network
    """
    #propogate the output values+rewards+errors
    delta_L = gradient_log_p
    #propogate then with the hidden input layer
    dC_dw2 = np.dot(hidden_layer_values.T, delta_L).ravel()
    #find the cross entropy of the input values with the hidden outvalues
    delta_l2 = np.outer(delta_L, weights['2'])
    delta_l2 = ReLu(delta_l2)
    dC_dw1 = np.dot(delta_l2.T, observation_values)
    return {
        '1': dC_dw1,
        '2': dC_dw2
    }

def discount_reward(reward, gamma):
    """Discount ensures that actions that were taken in the beginning weight less of an
        importance to the result than actions that were taken 2 steps prior to this result"""
    discount_r = np.zeros_like(reward)
    r_total = 0
    for _ in reversed(range(0, reward.size)):
        if reward[_] != 0:
            r_total = 0
        r_total = r_total * gamma + reward[_]
        discount_r[_] = r_total
    return discount_r

def discount_with_rewards(gradient_log, ep_rewards, gamma):
    """
    This method takes the gradient and applies a reward discount
    :param gradient_log: the gradient array/list, from action to expected value
    :param ep_rewards: the running frames reward array/list
    :param gamma: amount of gamma coefficient to discount to the reward
    :return: List/Array of the newly discounted reward array with gradient constant(multiplied into the reward, used for
    our gradient backprop to learn)
    """
    discount_ep_reward = discount_reward(ep_rewards, gamma) # make an array that applies a discount to actions that are
    # more relevant,
    discount_ep_reward -= np.mean(discount_ep_reward) # normalize this reward vector
    discount_ep_reward /= np.std(discount_ep_reward)
    return gradient_log * discount_ep_reward # apply the gradient score to the respective rewards, this tells us the gradient(error to learn from)

def mutate(network):
    """
    randomly select a parameter to change and randomize that parameter, may need to change more than 1 parameter at a time
    :param network: Network Object
    :return: None
    """

    for param in network.getHyperParam().keys():
        #for each hyper parameter lets introduce some randomization
        if param == 'learning_rate' or param == 'decay_rate':
            network.getHyperParam()[param] = random.random()
        elif param == 'weights' or param == 'num_hidden_neuron':
            # if we run into hidden layer change OR weight change we need to reinitialize the weights , because the dimensions will need to be updated
            network.randomizeWeights(random.randint(5, 400), 80 * 80)

def generateChildren(parents,num_children):
    """
    return a list of networks that are a combinations of the array of parents given, using
    random selection process
    :param parents: Neural networks in which we want to inherit properties (parameters) from
    :param num_children: the number of spawm we want to generate
    :return: an array/list of spawn
    """
    print("Calling Generating children")
    spawn = []
    for i in range(num_children):
        _param = {} # temp hyper parameters for the child network
        for param in parents[0].getHyperParam().keys():
            #for each paramter in the parent, choose which parent attribute to inherit from
            _param[param] = random.choice(parents).getHyperParam()[param]
        #create the child network
        _net = Network(_param)
        #add it to the running children population
        spawn.append(_net)
    return spawn


import numpy as np
import tensorflow as tf
from gpflow.kernels import Linear

from tsipy.fusion.kernels import MultiWhiteKernel
from tsipy.fusion.utils import build_and_concat_label_mask

def test_kernel() -> None:
    """Tests implementation of the custom white kernel.

    It checks output tensor shapes and values.
    """
    np.random.seed(0)

    x_a = np.random.randint(0, 5, 3)
    x_b = np.random.randint(0, 5, 2)
    x_c = np.random.randint(0, 5, 1)

    x_a = build_and_concat_label_mask(x_a, label=1)
    x_b = build_and_concat_label_mask(x_b, label=2)
    x_c = build_and_concat_label_mask(x_c, label=3)

    # Concatenate signals and sort by x[:, 0]
    x = np.vstack((x_a, x_b, x_c))

    x = tf.convert_to_tensor(x, dtype=tf.float64)
    x2 = x[:-1, :]

    # White kernel
    noise_variances = tf.convert_to_tensor(
        0.1 * np.array([1.0, 2.0, 3.0]), dtype=tf.float64
    )

    linear_kernel = Linear(active_dims=[0])
    multi_kernel = MultiWhiteKernel(
        labels=(1, 2, 3), variances=noise_variances, active_dims=[1]
    )

    n = 3
    variances = tf.zeros((x.shape[0],), dtype=tf.float64)
    for i in range(n):
        mask = tf.cast(tf.equal(x[:, -1], i + 1), tf.float64)
        mask_variances = mask * noise_variances[i]
        variances = variances + mask_variances

    # Test dimensions
    assert np.array_equal(linear_kernel(x).numpy().shape, multi_kernel(x).numpy().shape)
    assert np.array_equal(
        linear_kernel(x2).numpy().shape, multi_kernel(x2).numpy().shape
    )

    assert np.array_equal(
        linear_kernel(x, x2).numpy().shape, multi_kernel(x, x2).numpy().shape
    )
    assert np.array_equal(
        linear_kernel(x2, x).numpy().shape, multi_kernel(x2, x).numpy().shape
    )

    # Test variances
    assert np.linalg.norm(multi_kernel(x).numpy() - np.diag(variances)) < 1e-9


# -*- coding: utf-8 -*-
import os
from http.server import HTTPServer, SimpleHTTPRequestHandler
from pathlib import Path

import click

from behappy.core.conf import settings
from behappy.core.main import BeHappy, BeHappyFile, BeHappySync
from behappy.core.utils import timeit

@click.group()
def main():
    pass

@main.command()
@click.option('--target', default='target', help='Path to build folder')
@click.option('--conf', default='behappy.ini', help='Path to config')
@click.option('--tags', default='', help='Filter albums by tags')
@timeit
def build(target, conf, tags):
    """
    Build static site
    """
    settings.load(conf)

    tags = set([i.strip() for i in tags.split(',') if i.strip()])
    blog = BeHappy(target, tags)
    blog.build()

@main.command()
@click.argument('videos', nargs=-1)
@click.option('--preview', default='0', help='Preview time in seconds')
@timeit
def convert_video(videos, preview):
    """
    Convert videos to mp4 and generate preview
    """
    for video in videos:
        print('# Starting {}'.format(video))
        src = Path(video)
        dst_video = src.with_suffix('.mp4')
        dst_preview = src.with_suffix('.jpg')

        convert_template = 'ffmpeg -i {0} -vcodec libx264 -preset slow -crf 28 -movflags faststart {1}'
        preview_template = 'ffmpeg -i {0} -vframes 1 -an -ss {1} {2}'
        exif_template = 'exiftool -overwrite_original -TagsFromFile {0} "-EXIF:all>EXIF:all" {1}'

        if not dst_video.exists():
            os.system(convert_template.format(src.as_posix(), dst_video.as_posix()))
        if dst_preview.exists():
            dst_preview.unlink()
        os.system(preview_template.format(src.as_posix(), int(preview), dst_preview))
        os.system(exif_template.format(src.as_posix(), dst_preview.as_posix()))
        print('# End {}\n\n'.format(video))

@main.command()
@click.option('--target', default='target', help='Path to build folder')
@click.option('--port', default='8000', help='Path to build folder')
def server(target, port):
    """
    Run test web server
    """
    if not os.path.exists(target):
        os.mkdir(target)
    os.chdir(target)

    httpd = HTTPServer(('0.0.0.0', int(port)), SimpleHTTPRequestHandler)
    print('# server at http://127.0.0.1:{0}'.format(port))
    httpd.serve_forever()

@main.command()
def new():
    """
    Create new album.
    """
    folder = Path('.').absolute()
    file = BeHappyFile(folder)
    file.new()

@main.command()
@click.option('--target', default='target', help='Path to build folder')
@click.option('--profile', default=None, help='AWS profile')
@click.option('--endpoint', default=None, help='S3 endpoint url')
@click.option('--bucket', help='S3 bucket name')
@click.option('--cloudfront', default=None, help='AWS cloudfront distribution id')
@timeit
def sync(target, profile, endpoint, bucket, cloudfront):
    """
    Run test web server
    """
    folder = Path(target)
    be_sync = BeHappySync(folder, profile, endpoint, bucket)
    print('Sync S3')
    be_sync.s3()
    if cloudfront:
        print('Invalidate CloudFront')
        be_sync.cloudfront_invalidate(cloudfront)

if __name__ == '__main__':
    main()


import sys
import os
BIN = '../../'
sys.path.append(BIN)
from my_nn_modules import AE_basic, AE_bn_LeakyReLU
from utils import plot_activations
from my_nn_modules import get_data, RMSELoss
from fastai import train as tr
from fastai.callbacks import ActivationStats
from fastai import basic_train, basic_data
from fastai.callbacks.tracker import SaveModelCallback
from torch.utils.data import TensorDataset
import torch.utils.data
import torch.nn as nn
import torch
import utils
from scipy import stats
import my_matplotlib_style as ms
import datetime
import pickle
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

plt.close('all')
# import torch.optim as optim

mpl.rc_file(BIN + 'my_matplotlib_rcparams')

# Load AOD data
# Smaller dataset fits in memory on Kebnekaise
train = pd.read_pickle(BIN + 'processed_data/aod/custom_normalized_train_10percent.pkl')
test = pd.read_pickle(BIN + 'processed_data/aod/custom_normalized_test_10percent.pkl')
train = train[train['m'] > 1e-3]
test = test[test['m'] > 1e-3]

bs = 1024
# Create TensorDatasets
train_ds = TensorDataset(torch.tensor(train.values, dtype=torch.float),
                         torch.tensor(train.values, dtype=torch.float))
valid_ds = TensorDataset(torch.tensor(test.values, dtype=torch.float),
                         torch.tensor(test.values, dtype=torch.float))
# Create DataLoaders
train_dl, valid_dl = get_data(train_ds, valid_ds, bs=bs)
# Return DataBunch
db = basic_data.DataBunch(train_dl, valid_dl)

module_name = 'AE_bn_LeakyReLU'
module = AE_bn_LeakyReLU
latent_dim = 14
grid_search_folder = module_name + '_AOD_grid_search_custom_normalization_1500epochs/'
grid_search_folder = module_name + '_AOD_grid_search_custom_normalization_1500epochs_12D10D8D/'
model_folder = 'AE_27_200_200_200_%d_200_200_200_27' % latent_dim
train_folder = 'AE_bn_LeakyReLU_bs4096_lr1e-02_wd1e-02_ppNA'    # 14D 18D 20D
# train_folder = 'AE_bn_LeakyReLU_bs4096_lr3e-02_wd1e-04_ppNA'  # 16D
# train_folder = 'AE_bn_LeakyReLU_bs4096_lr1e-03_wd1e-01_ppNA'  # 12D
# train_folder = 'AE_bn_LeakyReLU_bs4096_lr1e-03_wd1e-02_ppNA'  # 10D
# train_folder = 'AE_bn_LeakyReLU_bs4096_lr1e-02_wd1e-04_ppNA'  # 8D
save = True

loss_func = nn.MSELoss()

plt.close('all')
tmp = train_folder.split('bs')[1]
param_string = 'bs' + tmp
save_dict_fname = 'save_dict' + param_string + '.pkl'
path_to_save_dict = grid_search_folder + model_folder + '/' + train_folder + '/' + save_dict_fname
saved_model_fname = 'best_' + module_name + '_' + param_string.split('_pp')[0]
path_to_saved_model = grid_search_folder + model_folder + '/' + 'models/' + saved_model_fname
curr_save_folder = grid_search_folder + model_folder + '/' + train_folder + '/'

with open(path_to_save_dict, 'rb') as f:
    curr_save_dict = pickle.load(f)

train_losses = curr_save_dict[module_name][param_string]['train_losses']
val_losses = curr_save_dict[module_name][param_string]['val_losses']

# # Plot losses
# batches = len(train_losses)
# epochs = len(val_losses)
# val_iter = (batches / epochs) * np.arange(1, epochs + 1, 1)
# # loss_name = str(loss_func).split("(")[0]
# plt.figure()
# plt.plot(train_losses, label='Train')
# plt.plot(val_iter, val_losses, label='Validation', color='orange')
# plt.yscale(value='log')
# plt.legend()
# plt.ylabel('MSE')
# plt.xlabel('Batches processed')
# if save:
#     fig_name = 'new_losses.png'
#     plt.savefig(curr_save_folder + fig_name)

nodes = model_folder.split('AE_')[1].split('_')
nodes = [int(x) for x in nodes]
model = module(nodes)
learn = basic_train.Learner(data=db, model=model, loss_func=loss_func, true_wd=True, bn_wd=False,)
learn.model_dir = grid_search_folder + model_folder + '/' + 'models/'
learn.load(saved_model_fname)
learn.model.eval()

plt.close('all')
unit_list = ['[GeV]', '[rad]', '[rad]', '[GeV]']
variable_list = [r'$p_T$', r'$\eta$', r'$\phi$', r'$E$']
line_style = ['--', '-']
colors = ['orange', 'c']
markers = ['*', 's']

idxs = (0, -1)  # Pick events to compare
data = torch.tensor(test[idxs[0]:idxs[1]].values, dtype=torch.float)
pred = model(data).detach().numpy()
data = data.detach().numpy()

data_df = pd.DataFrame(data, columns=test.columns)
pred_df = pd.DataFrame(pred, columns=test.columns)

# Unnormalize
unnormalized_data_df = utils.custom_unnormalize(data_df)
# unnormalized_data_df = data_df
unnormalized_pred_df = utils.custom_unnormalize(pred_df)

# Handle variables with discrete distributions
unnormalized_pred_df['N90Constituents'] = unnormalized_pred_df['N90Constituents'].round()
uniques = unnormalized_data_df['ActiveArea'].unique()
utils.round_to_input(unnormalized_pred_df, uniques, 'ActiveArea')

hist_groups = [
    ['pt', 'eta', 'phi', 'm', 'LeadingClusterPt', 'LeadingClusterCenterLambda', 'LeadingClusterSecondLambda', 'LeadingClusterSecondR'],
    ['ActiveArea', 'ActiveArea4vec_eta', 'ActiveArea4vec_phi', 'ActiveArea4vec_pt', 'ActiveArea4vec_m', 'Timing', 'OotFracClusters5', 'OotFracClusters10'],
    ['AverageLArQF', 'NegativeE', 'CentroidR', 'DetectorEta', 'EMFrac', 'HECFrac', 'HECQuality', 'LArQuality'],
    ['Width', 'WidthPhi', 'N90Constituents'],
]

mpl.rcParams['xtick.labelsize'] = 16
mpl.rcParams['ytick.labelsize'] = 16
mpl.rcParams['axes.labelsize'] = 18

group = hist_groups[0]

for i_group, group in enumerate(hist_groups):
    group_data_df = unnormalized_data_df[group]
    group_pred_df = unnormalized_pred_df[group]
    data = group_data_df.values
    pred = group_pred_df.values

    nrows = 3
    ncols = 3
    if i_group == 3:
        nrows = 1
    fig, axs = plt.subplots(nrows=nrows, ncols=ncols)

    # Histograms
    alph = 0.8
    n_bins = 200
    for ii in np.arange(nrows):
        for kk in np.arange(ncols):
            pp = nrows * ii + kk
            if i_group == 3:
                fig.set_size_inches(12, 3, forward=True)
                ax = axs[kk]
            else:
                ax = axs[ii, kk]
            if i_group != 3:
                if pp == ncols * nrows - 1:
                    fig.delaxes(ax)
                    break
            if pp == 0:
                n_hist_data, bin_edges, _ = ax.hist(
                    data[:, pp], color=colors[1], label='Input', alpha=1, bins=n_bins, density=False)
                n_hist_pred, _, _ = ax.hist(pred[:, pp], color=colors[0],
                                            label='Output', alpha=alph, bins=n_bins, density=False)
            else:
                n_hist_data, bin_edges, _ = ax.hist(
                    data[:, pp], color=colors[1], alpha=1, bins=n_bins, density=False)
                n_hist_pred, _, _ = ax.hist(pred[:, pp], color=colors[0],
                                            alpha=alph, bins=n_bins, density=False)
            ax.set_xlabel(group[pp])
            # plt.xlabel(test.columns[kk])
            # plt.ylabel('Number of jets')
            # ax.ticklabel_format(style='sci', scilimits=(0, 0), axis='y')
            ax.set_yscale('log')
    plt.tight_layout()
    if i_group != 3:
        fig.legend(bbox_to_anchor=(.76, 0.08, 0.2, 0.2), fontsize=30)

    fig_name = 'hist_%d_group%d' % (latent_dim, i_group)
    if save:
        plt.savefig(curr_save_folder + fig_name)

if not save:
    plt.show()


import pygame

WHITE = 255, 255, 255

class Paddle(object):
    def __init__(self, pos=[0,0], dimensions=(20,100), speed=5,
            up=pygame.K_UP, dwn=pygame.K_DOWN, direc=0):
        self.surface = pygame.Surface(dimensions)
        self.surface.fill(WHITE)
        self.rect = self.surface.get_rect().move(pos)
        self.up = up
        self.dwn = dwn
        self.speed = speed
        self.direc = direc

    def move(self, frect):
        if self.direc == 1 and self.rect.bottom + self.speed > frect.bottom:
            self.direc = 0
        elif self.direc == -1 and self.rect.top + self.speed < frect.top:
            self.direc = 0
        self.rect = self.rect.move([0, self.speed * self.direc])


import sys
import os
import urllib.parse
import codecs
from optparse import OptionParser

from .url_opener import URLOpener
from .terra_html_parser import TerraHTMLParser
from .slide_formatter import SlideFormatter
from . import sysconfig

def find_parser(netloc):
    netlocs = []

    # put all parsers here
    netlocs.extend(TerraHTMLParser.list_netlocs())

    # FIXME: iterate trough parsers and find right parser
    return TerraHTMLParser()

class Schemes:
    LOCAL = 0
    EXTERN = 1
    UNKNOWN = 2

def find_method(scheme):
    schemes = {
            '': Schemes.LOCAL,
            'file': Schemes.LOCAL,
            'http': Schemes.EXTERN,
            'ftp': Schemes.EXTERN,
            'https': Schemes.EXTERN
            }
    ret = schemes.get(scheme, Schemes.UNKNOWN)
    if ret == Schemes.UNKNOWN:
        raise Exception("Unknown address")

    return ret

def parse_options():
    usage = "%prog [options] <address|file>"
    parser = OptionParser(usage=usage)
    parser.add_option('-c', '--max-cols',
                      action='store', type='int', dest='max_cols',
                      help='Max number of cols per slide. If a certain row ' \
                              'is larger than this number, it will be split ' \
                              'in the best possible manner.')
    parser.add_option('-r', '--max-rows',
                      action='store', type='int', dest='max_rows',
                      help='Max number of rows per slide. If a verse conttains ' \
                              'more rows than this number, it will be split in ' \
                              'slides.')
    parser.add_option('-d', '--no-line-smart',
                      action='store_false', dest='smart_line', default=True,
                      help='Turn off the smart line wrapping')
    parser.add_option('-e', '--no-verse-smart',
                      action='store_false', dest='smart_verse', default=True,
                      help='Turn off the smart verse wrapping')
    parser.add_option('-o', '--output-dir',
                      action='store', type='string', dest='output_dir',
                      help='Directory in which the generated latex file will be let. ' \
                              'Default is to output to stdout. If a directory is ' \
                              'specified, it will have the same name as input file')
    parser.add_option('-s', '--save-txt',
                      action='store_true', dest='save_txt',
                      help='Save a copy of the text to output dir.')
    parser.add_option('-T', '--template-dir',
                      action='store', type='string', dest='template_dir',
                      help='Use TEMPLATE_DIR in as a directory containing templates. ' \
                              'It will be put in precedence to other default '
                              'directories.')
    parser.add_option('-t', '--template',
                      action='store', type='string', dest='template',
                      help='Use TEMPLATE instead of the default one.')
    parser.add_option('-u', '--no-upper',
                      action='store_false', dest='toupper', default=True,
                      help='Do not convert input text to uppercase letters')

    (options, args) = parser.parse_args()
    if len(args) != 1:
        print('ERROR: you must specify a file or internet address')
        parser.print_help()
        sys.exit(1)

    if not options.output_dir and options.save_txt:
        print('ERROR: --save-txt options demands that an output directory is given')
        parser.print_help()
        sys.exit(1)

    return options, args

def main(*args):
    (options, args) = parse_options()
    sysconfig.option_parser = options
    sysconfig.initialize()

    addr = args[0]
    parsed_text = []
    addr_parse_result = urllib.parse.urlparse(addr)
    outfile = None
    outfiletxt = None
    if find_method(addr_parse_result.scheme) != Schemes.LOCAL:
        html = URLOpener.open(addr)
        parser = find_parser(addr_parse_result.netloc)
        parsed_text = parser.run(html)
        if sysconfig.option_parser.output_dir:
            outfile = parser.title.replace(' ', '-').lower() + '.tex'
            outfiletxt = outfile[0:-4] + '.txt'
    else:
        if sysconfig.option_parser.output_dir:
            (tmp, outfile) = os.path.split(addr_parse_result.path)
            (outfile, root) = os.path.splitext(outfile)
            outfile += '.tex'
        with codecs.open(addr_parse_result.path, encoding='utf-8', mode='r') as f:
            parsed_text = f.readlines()

    slidefmt = SlideFormatter()
    fmt_text = slidefmt.format(parsed_text)
    if outfile:
        with codecs.open(os.path.join(sysconfig.option_parser.output_dir,
                  outfile), mode='w', encoding='utf-8') as f:
            f.writelines([l for l in fmt_text])
    else:
        for line in fmt_text:
            print(line, end=' ')

    if outfiletxt:
        with codecs.open(os.path.join(sysconfig.option_parser.output_dir,
                outfiletxt), mode='w', encoding='utf-8') as f:
            f.writelines([l for l in parsed_text])

if __name__ == "__main__":
    sys.exit(main(*sys.argv))


import os
import csv
import pandas as pd
import numpy as np
from scipy import stats
from pathlib import Path
from cliffs_delta import cliffs_delta

def read_data(dataset, dataset_path, metric):
    resample_balancers = ['SMOTE', 'ROS', 'ADASYN', 'BSMOTE', 'RUS', 'NM', 'CNN', 'TL', 'ENN']

    datas = {}
    for balancer in os.listdir(dataset_path):
        if '_result.csv' in balancer:
            continue

        data_path = ''
        if balancer in resample_balancers:
            if dataset == 'DataClass':
                data_path = dataset_path + '%s/NB/metrics_result.csv' % balancer
            if dataset == 'FeatureEnvy':
                data_path = dataset_path + '%s/LogisticRegression/metrics_result.csv' % balancer
            if dataset == 'GodClass':
                data_path = dataset_path + '%s/NB/metrics_result.csv' % balancer
            if dataset == 'LongMethod':
                data_path = dataset_path + '%s/LogisticRegression/metrics_result.csv' % balancer
        else:
            continue

        raw_datas = pd.read_csv(data_path)
        raw_datas = raw_datas[metric].values

        datas[balancer] = raw_datas

    return datas

def process_data(datas):
    metric_datas = []
    balancers = []

    baseline_data = datas['SMOTE']
    metric_datas.append(baseline_data)

    for key, value in datas.items():
        if key == 'SMOTE':
            continue
        metric_datas.append(value)
        balancers.append(key)

    return metric_datas, balancers

def wilcoxon(l1, l2):
    w, p_value = stats.wilcoxon(l1, l2, correction=False)
    return p_value

def Wilcoxon_signed_rank_test(metric_datas, balancers, dataset, metric):
    pvalues = []
    sortpvalues = []
    bhpvalues = []
    cliffs = []
    medians = []
    labels = []

    for i in range(1, len(metric_datas)):
        print(balancers[i-1])
        pvalue = wilcoxon(metric_datas[0], metric_datas[i])
        pvalues.append(pvalue)
        sortpvalues.append(pvalue)

        cliff, _ = cliffs_delta(metric_datas[i], metric_datas[0])
        cliffs.append(abs(cliff))

        median = np.median(metric_datas[i]) - np.median(metric_datas[0])
        medians.append(median)

    sortpvalues.sort()

    for i in range(len(pvalues)):
        bhpvalue = pvalues[i]*(len(pvalues))/(sortpvalues.index(pvalues[i])+1)
        bhpvalues.append(bhpvalue)

    for i in range(len(balancers)):
        if bhpvalues[i] < 0.05 and cliffs[i] > 0.147:
            if medians[i] > 0:
                labels.append('significant improvement')
            if medians[i] < 0:
                labels.append('significant decrease')
            if medians[i] == 0:
                labels.append('non-significant')
        else:
            labels.append('non-significant')

    output_path = 'results/resampling_significant/'
    Path(output_path).mkdir(parents=True, exist_ok=True)
    file_path = output_path + '{}_{}.csv'.format(dataset, metric)

    csv_file = open(file_path, 'w', newline='', encoding='utf-8')
    csv_write = csv.writer(csv_file)
    csv_write.writerow(['balancer', 'pvalue', 'cliff', 'significant'])

    for i in range(len(balancers)):
        csv_write.writerow([balancers[i], bhpvalues[i], cliffs[i], labels[i]])

    csv_file.close()

if __name__ == '__main__':
    metrics = ['Accuracy', 'Precision', 'Recall', 'F1', 'Kappa', 'AUC', 'MCC']

    result_path = 'results/OurDataset/'
    for dataset in os.listdir(result_path):
        if dataset in ['LongParameterList', 'SwitchStatements']:
            continue
        dataset_path = result_path + '%s/' % dataset
        for metric in metrics:
            print("Doing Wilcoxon signed rank test in %s_%s ..." % (dataset, metric))
            datas = read_data(dataset, dataset_path, metric)
            metric_datas, balancers = process_data(datas)
            Wilcoxon_signed_rank_test(metric_datas, balancers, dataset, metric)


def origin_urls():
    url = {}
    url["1"] = "https://dtliving-pre.alicdn.com/live_hp/259ae09c-8fbc-4ccb-9c71-6b35bf8b4928_merge.m3u8?app_type=win&auth_key=1615360924-0-0-e91b024fc860281496aaf64ed6023610&cid=e5b012ef6c10389b49d0c369f0759688&token=4f355a3038792537a90424b3feddc603CTG9CbgW1Q0gMoj1b4wOOMf6911yAq-nsJ2rqV-QIQmzrP1XD-RmCWYBzMHPBu_M7nYaFRKrzRFMQ7dK4ctJ6Qib3of5TtJXK9Df5FzORzs=&token2=c108c5a384656a6c1ee212fe244699bbVxu0gmtGdA1n_1LKeioVSrVZZxDtLio7emdn27OOUDVfMgUlJbAGvMxe70G-TVBeQ_h2GhjGbVFxOuyeRlNbTvyZWY96MmDpVJpGyYb2gEA&version=5.3.6-Release.3896"
    url["2"] = ""
    url["3"] = ""
    url["4"] = ""
    url["5"] = ""
    url["6"] = ""
    url["a1"] = ""
    url["7-1"] = ""
    url["7-2"] = ""
    url["8"] = ""
    url["9"] = ""
    url["10"] = ""
    url["a2"] = ""
    url["11"] = ""
    url["12"] = ""
    url["a3"] = ""
    url["13"] = ""
    url["14"] = ""
    url["a4"] = ""
    url["15-1"] = ""
    url["15-2"] = ""
    url["16"] = ""
    url["a5"] = ""
    url["17"] = ""
    url["18"] = ""
    url["19-1"] = ""
    url["19-2"] = ""
    url["20"] = ""
    url["a6"] = ""
    url["21"] = ""
    url["22"] = ""
    url["23"] = ""
    url["24"] = ""
    url["25"] = ""
    url["26"] = ""
    url["27"] = ""
    url["a7"] = ""
    url["a8"] = ""
    url["29"] = ""
    url["30"] = ""
    url["a9"] = ""
    return url

def module():
    url = "https://dtliving-pre.alicdn.com/live_hp/66e15aa2-b972-4b6d-8ff6-7bf66228e195_merge.m3u8?app_type=mac&auth_key=1613788275-0-0-da9dea77749748ee2152f6eef864acf3&cid=e5b012ef6c10389b49d0c369f0759688&token=6cbe504a7f44bd0ceea1597e391cf098uq3CvoQbEcURzyijZWPj34gngP9FnjP7IsP0ml2O8yGZp-jNhrFzuncRJDm99plgE6XjRH2GvuL6CKX6_aRmtUJt7fXNjrBVsAy_bg6Gp9A=&token2=72dccd20be006fa0027abac2f5dc89edB358jo2IZy9H8ikjmV9c4b15nUcPKHCexl1env6oMF2mwgTcIZXPndMcAmwXux1UmP8y0J7C--3gS-9yIi7jPk2Eo7JvOwu9GRwRbzp_AJA&version=5.1.39"
    for key in url.split("&"):
        print(key)

def buildUrl(vidio_id, auth_key, cid, token, token2):
    return "https://dtliving-pre.alicdn.com/live_hp/%smerge.m3u8?app_type=mac&auth_key=%s&cid=%s&token=%s&token2=%s&version=5.1.39" % (vidio_id, auth_key, cid, token, token2)

def getParam(url):
    # for key in url.split("&"):
    #     print(key)

    live_hp = url.split("merge.m3u8")[0].split("Flive_hp%2F")[1]
    print(live_hp)

    auth_key = url.split("auth_key%3D")[1].split("%26cid")[0]
    print(auth_key)

    cid = url.split("cid%3D")[1].split("%3D%26token")[0]
    print(cid)

    token = url.split("token%3D")[1].split("%3D%26token2")[0]
    print(token)

    token2 = url.split("token2%3D")[1].split("%26version")[0]
    print(token2)

    return live_hp, auth_key, cid, token, token2

if __name__ == "__main__":
    module()

    # url = buildUrl(1, 2, 2, 2, 3, 2)
    # print(url)

    print()
    urls = origin_urls()
    for url in urls:
        if urls[url] != "":
            live_hp, auth_key, cid, token, token2 = getParam(url)

            print()
            download_url = buildUrl(live_hp, auth_key, cid, token, token2)
            print(download_url)


import os
import csv
from init import initialize
from mirrors import all_mirrors
from test_mirror import test_mirror

def main():
    initialize()

    print('Start testing...')
    results = {}
    for mirror in all_mirrors:
        if os.path.isfile('results/{}.txt'.format(mirror)):
            print(f'Skipping {mirror}')
        else:
            print('Testing {}'.format(all_mirrors[mirror]['name']))
            final_points, ping_result, tiny_files_speed_str, large_files_speed_str, richness_result = test_mirror(
                mirror)
            results[mirror] = {
                'final_points': final_points,
                'ping_result': '{:.3f}'.format(ping_result) if ping_result != 'Failed' else ping_result,
                'tiny_files_speed_str': tiny_files_speed_str,
                'large_files_speed_str': large_files_speed_str,
                'richness_result': richness_result
            }
            with open('results/{}.txt'.format(mirror), 'w') as f:
                f.write('Final points: {}\n'.format(final_points))
                f.write('Ping result: {}\n'.format(ping_result))
                f.write('Tiny files speed: {}\n'.format(tiny_files_speed_str))
                f.write('Large files speed: {}\n'.format(large_files_speed_str))
                f.write('Richness result: {}\n'.format(richness_result))

    with open('results/results.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerow(['Repo', 'Final points', 'Ping result', 'Tiny files speed', 'Large files speed', 'Richness result'])
        for mirror in results:
            writer.writerow([
                mirror,
                results[mirror]['final_points'],
                results[mirror]['ping_result'],
                results[mirror]['tiny_files_speed_str'],
                results[mirror]['large_files_speed_str'],
                results[mirror]['richness_result']
            ])

    print('Done!')

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        exit(1)


#!/usr/bin/env python from __future__ import division, print_function
import sys
import random, math, copy
import pdb
import csv

sys.path.insert(0, '..')
sys.path.append('.')

from tbpsite import settings
from django.core.management import setup_environ

setup_environ(settings)

from tutoring.models import Tutoring
from constants import TUTORING_DAY_CHOICES, TUTORING_HOUR_CHOICES
csvFileName = sys.argv[1]

MAX_TUTORS_PER_HOUR = 5
MIN_TUTORS_PER_HOUR = 2  # Want this
ENFORCED_MIN_TUTORS_PER_HOUR = 1  # Enforce this

TUTORING_START = 10  # 10AM
TUTORING_END = 16  # 1 hr before 5pm

tutoringHours = {}
for d in TUTORING_DAY_CHOICES:
    for h in TUTORING_HOUR_CHOICES:
        tutoringHours[( int(d[0]), int(h[0]) + TUTORING_START )] = set()

for t in Tutoring.current.all():
    if not t.hidden:
        tutoringHours[(int(t.day_1), int(t.hour_1) + 10)] |= set(map(lambda x: str(x), list(t.profile.classes.all())))
        tutoringHours[(int(t.day_2), int(t.hour_2) + 10)] |= set(map(lambda x: str(x), list(t.profile.classes.all())))

for key in tutoringHours:
    tutoringHours[key] = list(tutoringHours[key])

data = tutoringHours

days = {0: "Monday", 1: "Tuesday", 2: "Wednesday", 3: "Thursday", 4: "Friday"}

subjects = set()

for ls in data.values():
  for item in ls:
    subject = item.split(' ')[0]
    if subject not in subjects:
      subjects.add(subject)

subjects = sorted(list(subjects))

def format_classes(classes_lst):
    formatted_classes_lst = [','.join([class_str.split(' ')[1] for class_str in classes_lst if subject in class_str]) for subject in subjects]
    return formatted_classes_lst

with open(csvFileName, 'wb') as csvFile:
    writer = csv.writer(csvFile)
    writer.writerow(['Hour', 'Class'] + days.values())
    for hour in range(TUTORING_START, TUTORING_END + 1):
        classes = []
        for day in days:
            classes.append(format_classes(data[(day, hour)]))
        rows = zip(*classes)
        for index, row in enumerate(rows):
            writer.writerow([str(hour) if not index else '', subjects[index]] + list(row))


import logging
from collections import OrderedDict
from dordis.primitives.differential_privacy import (
    ddgauss, dskellam
)
from dordis.config import Config

registered_dict = OrderedDict([
    ('ddgauss', ddgauss.Handler),
    ('dskellam', dskellam.Handler)
])

def get():
    handler_type = Config().agg\
        .differential_privacy.mechanism

    if handler_type in registered_dict:
        logging.info("Differential privacy: %s", handler_type)
        registered_handler = registered_dict[handler_type]()
    else:
        raise ValueError(
            f"No such DP mechanism: {handler_type}")

    return registered_handler


import json
from bson import json_util

from flask import render_template, request, Response

import query
from app import app
from utils import datetime_range

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html')

@app.route('/api/player', methods=['GET'])
def player():
    """
    The start of an API route, returns a player's data using ?name='x'.
    """
    name = request.args.get('name')
    player_dict = query.query_specific_player(name)
    player_dict.pop("_id", None)

    return Response(json.dumps(player_dict, default=json_util.default),
                    mimetype='application/json')

@app.route('/api/gamelogs', methods=['GET'])
def gamelogs():
    gamelogs_query = {}

    name = request.args.get('name')
    if name:
        gamelogs_query.update({'Player': name})
    start = request.args.get('start')
    end = request.args.get('end')
    if start:
        gamelogs_query.update(datetime_range(start, end))
    active = request.args.get('active')

    if not active:
        gamelogs_list = query.query_games(gamelogs_query)
    else:
        gamelogs_list = query.query_games(gamelogs_query, active=True)

    if gamelogs_list:
        for gamelog_dict in gamelogs_list:
            gamelog_dict.pop("_id", None)

    return Response(json.dumps(gamelogs_list, default=json_util.default),
                    mimetype='application/json')


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

from numpy.distutils.core import setup, Extension

module1 = Extension('position_constraints', sources=['src/plantenergy/position_constraints.f90',
                                       'src/plantenergy/adStack.c',
                                       'src/plantenergy/adBuffer.f'],
                    extra_compile_args=['-O2', '-c'])

setup(
    name='PlantEnergy',
    version='0.0.1',
    description='Wind farm optimization interface allowing wake models to be switched out',
    install_requires=['openmdao>=1.7.3', 'florisse'],
    package_dir={'': 'src'},
    dependency_links=['http://github.com/OpenMDAO/OpenMDAO.git@master', 'https://github.com/WISDEM/FLORISSE.git@develop'],
    packages=['plantenergy'],
    ext_modules=[module1],
    license='Apache License, Version 2.0',
)


__author__ = 'kovaka'

class Initializer():

    @staticmethod
    def init_file(board, filename):
        with open(filename) as f:
            lines = f.readlines()
        Initializer.place_cells(board, lines)

    @staticmethod
    def place_cells(board, row_strings, offset_x = 0, offset_y = 0):
        coords = Initializer.get_coordinates(row_strings)
        avgLoc = Initializer.findAvgLoc(coords)
        for x, y in coords:
            board.birth(x + offset_x - avgLoc[0], y + offset_y - avgLoc[0])

    @staticmethod
    def findAvgLoc(coords):
        avgX = 0
        avgY = 0
        for x, y in coords:
            avgX += x
            avgY += y
        avgX = avgX / len(coords)
        avgY = avgY / len(coords)
        return (avgX, avgY)

    @staticmethod
    def get_coordinates(rows):
        """translate an array of strings into a list of tuples containing the
        coordinates of living cells
        """
        coords = []
        y = len(rows)
        for index, row_string  in enumerate(rows):
            if row_string[0] in ('#', '!'):
                continue

            for x, char in enumerate(row_string):
                if char in ('*', 'O'):
                    coords.append((x + 1, y))

            y -= 1
        return coords