import asyncio
import json
from aiohttp import web
from rpc_client import RemoteDictRpcClient
from loguru import logger
import sys

logger.remove()
logger.add(sys.stdout, format="{time:HH:mm:ss} - {level} - {message}", level="INFO")

routes = web.RouteTableDef()

@routes.get('/get_value')
async def get_from_remote_dict(request):
    # make sure 'key' parameter exists
    try:
        key = request.query['key']
        message = {"command": "get", 'key': key}
    except KeyError as ke:
        msg = "Missing query parameter: {}".format(ke)
        response = {"error": msg}
        logger.error(msg)
        return web.json_response(response)

    # use existing rabbit connection
    rpc_client = request.app['rpc_client']

    # send get task to remote server via rabbit and wait for response no longer than 2 secs
    try:
        response = await asyncio.wait_for(rpc_client.call(message), timeout=2.0)
        return web.json_response(json.loads(response))
    except asyncio.TimeoutError:
        msg = "Timeout on waiting for server response"
        response = {"error": msg}
        logger.error(msg)
        return web.json_response(response)
    except Exception as e:
        msg = "An error occurred during request processing"
        response = {"error": msg}
        logger.error(e)
        return web.json_response(response)

@routes.post("/set_value")
async def set_to_remote_dict(request):
    # make sure 'key' and 'value' params are passed, validate value - only floats accepted
    try:
        key = request.query['key']
        value = float(request.query['value'])
        message = {"command": "set", 'key': key, 'value': value}
    except KeyError as ke:
        response = {"error": "Missing query parameter: {}".format(ke)}
        return web.json_response(response)

    except ValueError as e:
        response = {"error": "Incorrect type of value parameter: {}".format(e)}
        return web.json_response(response)

    # use existing rabbit connection
    rpc_client = request.app['rpc_client']

    # send set task to remote server via rabbit and wait for response no longer than 2 secs
    try:
        response = await asyncio.wait_for(rpc_client.call(message), timeout=2.0)
        return web.json_response(json.loads(response))
    except asyncio.TimeoutError:
        msg = "Timeout on waiting for server response"
        response = {"error": msg}
        logger.error(msg)
        return web.json_response(response)
    except Exception as e:
        msg = "An error occurred during request processing"
        response = {"error": msg}
        logger.error(e)
        return web.json_response(response)

async def on_startup(_app):
    logger.info("Application startup...")
    client = await RemoteDictRpcClient().connect()
    _app['rpc_client'] = client

async def on_shutdown(_app):
    logger.info("Gracefully shutting client app down")
    await _app['rpc_client'].disconnect()

if __name__ == "__main__":
    app = web.Application()
    app.on_startup.append(on_startup)
    app.on_shutdown.append(on_shutdown)
    app.add_routes(routes)
    web.run_app(app, port=8080)


# 测试VOC2007数据集的读取
import cv2
import numpy as np
import os
import pandas as pd
import torch
import albumentations as A

from PIL import Image, ImageFile
from torch.utils.data import Dataset, DataLoader

ImageFile.LOAD_TRUNCATED_IMAGES = True

class YOLODataset(Dataset):
    def __init__(
        self,
        csv_file,
        img_dir,
        label_dir,
        image_size=416,
        transforms=None,
    ):
        self.annotations = pd.read_csv(csv_file)
        self.img_dir = img_dir
        self.label_dir = label_dir
        self.image_size = image_size
        self.transform = transforms

    def __len__(self):
        return len(self.annotations)

    def __getitem__(self, index):
        label_path = os.path.join(self.label_dir, self.annotations.iloc[index, 1])
        # 改变labels信息排列
        bboxes = np.roll(np.loadtxt(fname=label_path, delimiter=" ", ndmin=2), 4, axis=1).tolist()
        img_path = os.path.join(self.img_dir, self.annotations.iloc[index, 0])
        image = cv2.imread(img_path)

        # 图像格式转换
        if self.transform:
            image = self.transform(image=image)['image']
        bboxes = np.array(bboxes)
        return image, bboxes # [3,416,416] (0/1，x, y, w, h, classes)等

def test():
    transforms = A.Compose(
        [
            # 保持宽高比缩放图片，使最大边等于max_size,即IMAGE_SIZE
            A.LongestMaxSize(max_size=416),
            # padding填充图像
            A.PadIfNeeded(
                min_height=416, min_width=416, border_mode=cv2.BORDER_CONSTANT
            ),
            A.Normalize(mean=[0, 0, 0], std=[1, 1, 1], max_pixel_value=255, ),
            #ToTensorV2(),
        ],
        # bounding box参数检查
        #bbox_params=A.BboxParams(format="pascal_voc", min_visibility=0.4, label_fields=[]),
    )
    dataset = YOLODataset(
        csv_file="./VOC2007/train.csv",
        img_dir="./VOC2007/JPEGImages/",
        label_dir="./VOC2007/Labels/",
        transform=transforms,
    )
    loader = DataLoader(dataset=dataset, batch_size=1, shuffle=False)
    for batch_idx, (x, y) in enumerate(loader):         # iamge 和 三个不同的特征图
        print(y)

if __name__ == '__main__':
    test()


"""
预测年收入是否超过 5W 美元
参考: https://github.com/tensorflow/models/blob/r1.8.1/official/wide_deep/wide_deep.py
python -m tutorials.wide_deep --help
"""
import os.path as osp
import shutil
import sys
from dataclasses import dataclass

import pandas as pd
from absl import app
from tensorflow import keras

from utils import logger, tf_settings

_CSV_COLUMNS = [
    'age', 'workclass', 'fnlwgt', 'education', 'education_num',
    'marital_status', 'occupation', 'relationship', 'race', 'gender',
    'capital_gain', 'capital_loss', 'hours_per_week', 'native_country',
    'income_bracket'
]

@dataclass
class Context:
    batch_size: int
    train_epochs: int
    epochs_between_evals: int
    model_type: str
    model_dir: str = '/data/models/census_model'
    base_url: str = 'https://archive.ics.uci.edu/ml/machine-learning-databases/adult'
    train_file: str = 'adult.data'
    test_file: str = 'adult.test'

def parse_args():
    app.define_help_flags()
    app.flags.DEFINE_integer('batch_size', 32, 'Batch size for training and evaluation.')
    app.flags.DEFINE_integer('train_epochs', 40, 'The number of epochs used to train.')
    app.flags.DEFINE_integer('epochs_between_evals', 2, 'The number of training epochs to run between evaluations.')
    app.flags.DEFINE_enum('model_type', 'wide_deep', ['wide', 'deep', 'wide_deep'], 'Select model topology.')
    app.parse_flags_with_usage(sys.argv)
    args = app.FLAGS
    return Context(
        batch_size=args.batch_size,
        train_epochs=args.train_epochs,
        epochs_between_evals=args.epochs_between_evals,
        model_type=args.model_type
    )

def preprocess_data(input_file):
    output_file = input_file + '.md'
    with open(output_file, mode='w', encoding='utf-8') as writer:
        with open(input_file, mode='r', encoding='utf-8') as reader:
            for line in reader:
                line = line.strip().replace(', ', ',')
                if not line or ',' not in line:
                    continue
                if line.endswith('.'):
                    line = line[:-1]
                writer.write(line + '\n')
    return output_file

def maybe_download():
    train_path = keras.utils.get_file(ctx.train_file, osp.join(ctx.base_url, ctx.train_file))
    test_path = keras.utils.get_file(ctx.test_file, osp.join(ctx.base_url, ctx.test_file))
    return preprocess_data(train_path), preprocess_data(test_path)

def load_data(y_name=_CSV_COLUMNS[-1]):
    train_path, test_path = maybe_download()
    train = pd.read_csv(train_path, names=_CSV_COLUMNS)
    train_x = dict(train)
    train_y = train_x.pop(y_name)

    test = pd.read_csv(test_path, names=_CSV_COLUMNS)
    test_x = dict(test)
    test_y = test_x.pop(y_name)
    return (train_x, train_y), (test_x, test_y)

def input_fn(inputs, num_epochs, shuffle, batch_size):
    dataset = tf.data.Dataset.from_tensor_slices(inputs)
    if shuffle:
        dataset = dataset.shuffle(1000)
    dataset = dataset.repeat(num_epochs)
    dataset = dataset.batch(batch_size)
    return dataset

def build_feature_columns():
    age = tf.feature_column.numeric_column('age')
    education_num = tf.feature_column.numeric_column('education_num')
    capital_gain = tf.feature_column.numeric_column('capital_gain')
    capital_loss = tf.feature_column.numeric_column('capital_loss')
    hours_per_week = tf.feature_column.numeric_column('hours_per_week')

    education = tf.feature_column.categorical_column_with_vocabulary_list(
        'education', [
            'Bachelors', 'HS-grad', '11th', 'Masters', '9th', 'Some-college',
            'Assoc-acdm', 'Assoc-voc', '7th-8th', 'Doctorate', 'Prof-school',
            '5th-6th', '10th', '1st-4th', 'Preschool', '12th'])

    marital_status = tf.feature_column.categorical_column_with_vocabulary_list(
        'marital_status', [
            'Married-civ-spouse', 'Divorced', 'Married-spouse-absent',
            'Never-married', 'Separated', 'Married-AF-spouse', 'Widowed'])

    relationship = tf.feature_column.categorical_column_with_vocabulary_list(
        'relationship', [
            'Husband', 'Not-in-family', 'Wife', 'Own-child', 'Unmarried',
            'Other-relative'])

    workclass = tf.feature_column.categorical_column_with_vocabulary_list(
        'workclass', [
            'Self-emp-not-inc', 'Private', 'State-gov', 'Federal-gov',
            'Local-gov', '?', 'Self-emp-inc', 'Without-pay', 'Never-worked'])

    occupation = tf.feature_column.categorical_column_with_hash_bucket(
        'occupation', hash_bucket_size=1000)

    age_buckets = tf.feature_column.bucketized_column(
        age, boundaries=[18, 25, 30, 35, 40, 45, 50, 55, 60, 65])

    base_columns = [
        education, marital_status, relationship, workclass, occupation,
        age_buckets,
    ]

    crossed_columns = [
        tf.feature_column.crossed_column(
            [education, 'occupation'], hash_bucket_size=1000),
        tf.feature_column.crossed_column(
            [age_buckets, education, 'occupation'], hash_bucket_size=1000),
    ]

    wide_columns = base_columns + crossed_columns

    deep_columns = [
        age,
        education_num,
        capital_gain,
        capital_loss,
        hours_per_week,
        tf.feature_column.indicator_column(workclass),
        tf.feature_column.indicator_column(education),
        tf.feature_column.indicator_column(marital_status),
        tf.feature_column.indicator_column(relationship),
        # To show an example of embedding
        tf.feature_column.embedding_column(occupation, dimension=8),
    ]

    return wide_columns, deep_columns

def build_model():
    wide_columns, deep_columns = build_feature_columns()
    hidden_units = [100, 75, 50, 25]
    if ctx.model_type == 'wide':
        return tf.estimator.LinearClassifier(
            feature_columns=wide_columns,
            model_dir=ctx.model_dir)
    elif ctx.model_type == 'deep':
        return tf.estimator.DNNClassifier(
            feature_columns=deep_columns,
            hidden_units=hidden_units,
            model_dir=ctx.model_dir)
    else:
        return tf.estimator.DNNLinearCombinedClassifier(
            linear_feature_columns=wide_columns,
            dnn_feature_columns=deep_columns,
            dnn_hidden_units=hidden_units,
            model_dir=ctx.model_dir)

def main():
    logger.info(ctx)
    shutil.rmtree(ctx.model_dir, ignore_errors=True)
    (train_x, train_y), (test_x, test_y) = load_data()

    def train_input_fn():
        label = tf.equal(train_y, '>50K')
        return input_fn((train_x, label), ctx.epochs_between_evals, True, ctx.batch_size)

    def eval_input_fn():
        label = tf.equal(test_y, '>50K')
        return input_fn((test_x, label), 1, False, ctx.batch_size)

    model = build_model()
    template = 'step: {global_step:d}, accuracy: {accuracy:0.4f}, precision: {precision:0.4f}, recall: {recall:0.4f}, auc: {auc:0.4f}, loss: {loss:0.2f}'
    for n in range(ctx.train_epochs // ctx.epochs_between_evals):
        model.train(input_fn=train_input_fn)
        eval_result = model.evaluate(input_fn=eval_input_fn)
        logger.info(template.format(**eval_result))

if __name__ == '__main__':
    ctx = parse_args()
    tf = tf_settings()
    main()


from tqdm import tqdm
import json
import os

lang = 'fortran'

rel_constrcuts = ['do' if lang == 'fortran' else 'for']
rel_clauses = ['private', 'reduction', 'simd', '_']

data_dir = '/home/1010/talkad/Downloads/OMP_Dataset/fortran/source'

counter = {construct:{clause:0 for clause in rel_clauses} for construct in rel_constrcuts}

for file in [os.path.join(data_dir, 'total_uniq.jsonl')]:

    with open(os.path.join(data_dir, file), 'r') as f:
        for line in tqdm(f):
            js = json.loads(line.strip())

            clauses = js['pragma'].split()
            clauses_key = [clause[:clause.find('(')] if '(' in clause else clause for clause in clauses]

            for k,v in counter.items():
                if k in clauses_key:
                    counter[k]['_'] += 1

                for vv in v:
                    if k in clauses_key and vv in clauses_key:
                        counter[k][vv] += 1

    print(counter)

# fortran: {'do': {'private': 422, 'reduction': 51, 'simd': 5, '_': 1062}}
# c: {'for': {'private': 1035, 'reduction': 216, 'simd': 122, '_': 3351}}
# cpp: {'for': {'private': 509, 'reduction': 431, 'simd': 171, '_': 5220}}


#!/usr/bin/env python3
# consumed by pre-push test 009
# confirms if response from delegator is valid json

import sys
import json
import traceback

try:
	filepath = sys.argv[1]
except:
	print("must supply file path as argv[1]")
	exit(2)

try:
	with open(filepath) as f:
		try:
			string = f.read()
			jsonstring = json.loads(string)
			exit(0)
		except Exception as error:
			exit(1)
except Exception as err:
	string  = "could not open " + filepath
	print(string)
	print(traceback.format_exc())
	exit(2)


# encoding: utf-8
"""
通过fp-growth算法寻找购物篮数据当中的频繁项集，算法逻辑来源于：Tan, Pang-Ning, Michael Steinbach, and Vipin Kumar.
Introduction to Data Mining. 1st ed. Boston: Pearson / Addison Wesley, 2006. (pp. 363-370)

"""

# original author information
__copyright__ = 'Copyright © 2022 ERSSLE'
__license__ = 'MIT License'

from collections import defaultdict

class FPTree(object):
    """
    创建一颗FP树，算法的逻辑来源于 Tan, Pang-Ning, Michael Steinbach, and Vipin Kumar.
    Introduction to Data Mining. 1st ed. Boston: Pearson / Addison Wesley, 2006. (pp. 363-370)
    """
    def __init__(self,reverse=True):
        """
        初始化：reverse指定树生长时的排序方式，默认从高频项到低频项，也可反转（False）
        """
        self._reverse = reverse
        self._nodes_level = defaultdict(lambda: set())
        self._nodes_cluster = defaultdict(lambda: set())
        self._nodes_parent = defaultdict(lambda: None)
        self._nodes_children = defaultdict(lambda: set())
        self._nodes_count = defaultdict(lambda: 0)

        self._nodes_level['lv_0'].add(0)
        self._nodes_cluster['root'].add(0)
        self._nodes_parent[0] = None
        self._nodes_children[0] = set()
        self._nodes_count[0] = None

    @property
    def node_datas(self):
        """
        返回所含所有fp树结构信息的数据
        """
        return dict(
            nodes_level = dict(self._nodes_level),
            nodes_cluster = dict(self._nodes_cluster),
            nodes_parent = dict(self._nodes_parent),
            nodes_children = dict(self._nodes_children),
            nodes_count = dict(self._nodes_count)
        )

    def create_tree(self,node_datas):
        """
        根据结构数据直接构建fp树
        node_datas:一个字典结构数据，其形式与node_datas返回结果相同
        """
        self._nodes_level = node_datas['nodes_level']
        self._nodes_cluster = node_datas['nodes_cluster']
        self._nodes_parent = node_datas['nodes_parent']
        self._nodes_children = node_datas['nodes_children']
        self._nodes_count = node_datas['nodes_count']

    def add(self,transaction):
        """
        逐条添加
        """
        last_node = 0
        for i,item in enumerate(transaction):
            node = self._nodes_cluster[item].intersection(self._nodes_children[last_node])
            if node:
                node = list(node)[0]
                self._nodes_count[node] += 1
            else:
                node = self._next_node
                self._nodes_level['lv_%s' % (i+1)].add(node)
                self._nodes_cluster[item].add(node)
                self._nodes_parent[node] = last_node
                self._nodes_children[last_node].add(node)
                self._nodes_count[node] += 1
            last_node = node
    def adds(self,transactions,support=1):
        """
        输入数据条目，生长fp树
        transactions: 双层python链表，内嵌层每一项元素代表一条数据
        support:项的最小支持度
        """
        items = defaultdict(lambda: 0)
        for transaction in transactions:
            for item in transaction:
                assert item != 'root','数据当中不可存在root默认的根节点标识，尝试替换成其它标识后再进行'
                items[item] += 1
        items = dict((item, spt) for item, spt in items.items() if spt >= support)
        self.item_list = [k for k in items.keys()]
        self.item_list.sort(key=lambda k: items[k],reverse=self._reverse)
        self.item_list.append(None)  # 添加虚拟待检查节点，使fp全树的频繁项集查找和子图统一
        def clean_transaction(transaction):
            transaction = filter(lambda v: v in items, transaction)
            transaction_list = list(transaction)
            transaction_list.sort(key=lambda v: self.item_list[::-1].index(v), reverse=self._reverse)
            return transaction_list
        for transaction in map(clean_transaction,transactions):
            self.add(transaction)

    @property
    def _next_node(self):
        """
        返回下一个节点id号，每一个节点有一个唯一的id号,root节点id=0
        """
        return max(self._nodes_parent) + 1
    @property
    def maxdepth(self):
        """排除根节点树结构的最大深度"""
        return len(self._nodes_level) - 1
    @property
    def node_num(self):
        """排除根节点树的节点总数"""
        return len(self._nodes_parent) - 1

def find_child_tree(tree,item,node_to_item=None):
    """
    根据结尾项从源树获取子树，使子树的叶节点均含有类型相同的项
    tree: 源树
    item: 子树的叶节点类型
    node_to_item: 一个从节点到节点类型的映射字典，如果为None自动查找
    """
    nodes_cluster = tree.node_datas['nodes_cluster']
    nodes_parent = tree.node_datas['nodes_parent']
    nodes_count = tree.node_datas['nodes_count']
    if node_to_item is None:
        node_to_item = dict((v,k) for k,vs in nodes_cluster.items() for v in vs)
    leaf_nodes = nodes_cluster[item]

    child_tree = FPTree()
    new_node_datas = dict(
            nodes_level = defaultdict(lambda: set()),
            nodes_cluster = defaultdict(lambda: set()),
            nodes_parent = defaultdict(lambda: None),
            nodes_children = defaultdict(lambda: set()),
            nodes_count = defaultdict(lambda: 0)
    )
    for node in leaf_nodes:
        nodes = [node]
        while node:
            node = nodes_parent[node]
            nodes.append(node)
        for i,node in enumerate(nodes):
            new_node_datas['nodes_level']['lv_%s' % (len(nodes) - nodes.index(node)-1)].add(node)
            new_node_datas['nodes_cluster'][node_to_item[node]].add(node)
            new_node_datas['nodes_parent'][node] = nodes_parent[node]
            if node > 0:
                new_node_datas['nodes_children'][nodes_parent[node]].add(node)
                new_node_datas['nodes_count'][node] += nodes_count[nodes[0]]
            else:
                new_node_datas['nodes_count'][node] = None
    child_tree.create_tree(new_node_datas)
    child_tree._reverse = tree._reverse
    child_tree.item_list = [item for item in tree.item_list if item in child_tree._nodes_cluster]
    return child_tree

def find_counts(tree,minimum_support,node_to_item=None):
    """
    从树结构当中递归查找频繁项的统计值，即支持度。
    返回结果是一个层叠的字典
    tree: fp树
    minimum_support: 支持度
    node_to_item: 一个从节点到节点类型的映射字典，如果为None自动查找
    """
    if node_to_item is None:
        node_to_item = dict((v,k) for k,vs in tree.node_datas['nodes_cluster'].items() for v in vs)
    def get_count(tree,item):
        return sum(tree.node_datas['nodes_count'][node] for node in tree.node_datas['nodes_cluster'][item])
    def find_trees_count(tree):
        trees = []
        for item in tree.item_list[-2::-1]:
            child_tree = find_child_tree(tree,item,node_to_item)
            if get_count(tree,item) >= minimum_support:
                trees.append(child_tree)
            else:
                None
        return {tr.item_list[-1]:(
            get_count(tr,tr.item_list[-1]),
            find_trees_count(tr)
            ) for tr in trees
            }
    return find_trees_count(tree)

def find_frequent_itemsets_alpha(cnt):
    """解析find_count结果，生成频繁项集"""
    itemsets = []
    def find_itemsets(cnt,suffix=[]):
        cnts = []
        for k,vs in cnt.items():
            itemset = ([k] + suffix,vs[0])
            itemsets.append(itemset)
            cnts.append(vs[1])
            find_itemsets(vs[1],[k]+suffix)
    find_itemsets(cnt)
    return itemsets

def find_frequent_itemsets(datas,minimum_support,reverse=True):
    """
    基于给定的支持度，查找频繁项集
    datas: 双层python链表，每一项元素代表一条数据
    minimum_support: 支持度
    reverse: 指定树生长时的排序方式，默认从高频项到低频项，也可反转（False）
    """
    tree = FPTree(reverse=reverse)
    tree.adds(datas,support=minimum_support)
    node_to_item = dict((v,k) for k,vs in tree.node_datas['nodes_cluster'].items() for v in vs)
    counts = find_counts(tree,minimum_support,node_to_item)
    return find_frequent_itemsets_alpha(counts)

try:
    import networkx as nx
    import matplotlib.pyplot as plt

    def plot_fptree(tree,width_depth_rate = 1.5,add_labels=True,ax=None):
        """
        根据给定的fp树绘制成图形
        tree: 树
        add_labels: 添加节点类型与计数标签
        """
        node_datas = tree.node_datas
        node_to_item = dict((v,k) for k,vs in tree._nodes_cluster.items() for v in vs)
        depth = tree.maxdepth
        max_width = max(len(nodes)for nodes in node_datas['nodes_level'].values())
        graph = nx.Graph()
        graph_width = (max_width * width_depth_rate) / depth
        graph_depth = 1.0
        row_num = 1
        nodes_xy = {}
        nodes_labels = []
        def add_nodes(nodes,row_num = row_num):
            width = len(nodes)
            xs = [graph_width * (i+1) / (width+1) for i in range(width)]
            ys = [1 - (graph_depth * row_num) / (depth+2)] * width
            row_num += 1
            nodes_xy.update(dict((node,(x,y)) for node,x,y in zip(nodes,xs,ys) if node is not None))
            for node,x,y in zip(nodes,xs,ys):
                if node is not None:
                    txt = '%s:%s' % (node_to_item[node],node_datas['nodes_count'][node])
                    nodes_labels.append((txt,(x,y)))
            new_nodes = []
            for node in nodes:
                if node in node_datas['nodes_children']:
                    children = sorted(list(node_datas['nodes_children'][node]))
                    if children:
                        new_nodes = new_nodes + children
                    else:
                        new_nodes.append(None)
                else:
                    new_nodes.append(None)
            if set(new_nodes) == set([None]):
                return 0
            else:
                add_nodes(new_nodes,row_num)
        add_nodes([0])
        graph.add_nodes_from(nodes_xy)
        for k,vs in node_datas['nodes_children'].items():
            graph.add_edges_from((k,v) for v in vs)
        if ax is None:
            fig = plt.figure(figsize=(6*graph_width,6))
            ax = fig.add_subplot()
        nx.draw(graph,nodes_xy,with_labels=True,node_size=1000,node_color='r',ax=ax)
        if add_labels:
            for txt,xy in nodes_labels:
                _ = ax.text(xy[0],xy[1]-0.05,txt,fontdict={'fontsize':15,'color':'b'})
except ModuleNotFoundError:
    None

if __name__ == '__main__':
    datas = [
        ['a','b'],
        ['b','c','d'],
        ['a','c','d','e'],
        ['a','d','e'],
        ['a','b','c'],
        ['a','b','c','d'],
        ['a'],
        ['a','b','c'],
        ['a','b','d'],
        ['b','c','e']
    ]
    print('datas频繁项集:','\n',find_frequent_itemsets(datas,2))
    if 'plot_fptree' in dir():
        tree = FPTree()
        tree.adds(datas,2)
        plot_fptree(tree)
        plt.show()


from setuptools import setup
import os

def read(*parts):
    retval = ''
    with open(os.path.join(*parts), 'r') as f:
        retval = f.read()

    return retval

def requirements():
    return read('requirements.txt').split()

setup(
    name='yggdrasil',
    url='https://github.com/Moguri/yggdrasil',
    license='Apache Software License',
    install_requires=requirements(),
)


import unittest
import wx
import os
import sys
from mockito import mock
from nose.tools import assert_equal, assert_true

from robotide.robotapi import Variable
from robotide.controller import DataController
from robotide.controller.robotdata import NewTestCaseFile
from robotide.controller.settingcontrollers import VariableController
from robotide.controller.tablecontrollers import VariableTableController
from robotide.editor import EditorCreator
from robotide.editor.editors import TestCaseFileEditor, WelcomePage
# Workaround for relative import in non-module
# see https://stackoverflow.com/questions/16981921/relative-imports-in-python-3
PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(),
                                              os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
try:
    from fakeplugin import FakePlugin
except ImportError:  # Python 3
    from .fakeplugin import FakePlugin

from resources import PYAPP_REFERENCE

DATADIR = 'fake'
DATAPATH = '%s/path' % DATADIR
TestCaseFileEditor._populate = lambda self: None

class EditorCreatorTest(unittest.TestCase):

    def setUp(self):
        self._registered_editors = {}
        self.creator = EditorCreator(self._register)
        self.creator.register_editors()

    def tearDown(self):
        if os.path.exists(DATADIR):
            os.rmdir(DATADIR)

    def _register(self, iclass, eclass):
        self._registered_editors[iclass] = eclass

    def test_registering_editors_for_model_objects(self):
        assert_equal(len(self._registered_editors), len(self.creator._EDITORS))

    def test_creating_editor_for_datafile_controller(self):
        plugin = self._datafile_plugin()
        editor = self._editor_for(plugin)
        assert_true(isinstance(editor, TestCaseFileEditor))

    def _editor_for(self, plugin):
        return self.creator.editor_for(plugin, wx.Frame(None), None)

    def test_creating_editor_with_variable(self):
        plugin = self._variable_plugin()
        editor = self._editor_for(plugin)
        assert_true(isinstance(editor, TestCaseFileEditor))

    def test_creating_welcome_page_when_no_item(self):
        plugin = self._no_item_selected_plugin()
        editor = self._editor_for(plugin)
        assert_true(isinstance(editor, WelcomePage))

    def test_same_welcome_page_editor_instance_is_returned_if_called_multiple_times(self):
        plugin = self._no_item_selected_plugin()
        editor = self._editor_for(plugin)
        editor2 = self._editor_for(plugin)
        assert_equal(editor, editor2)

    def test_same_testcasefile_editor_instance_is_returned_if_called_multiple_times(self):
        plugin = self._variable_plugin()
        editor = self._editor_for(plugin)
        editor2 = self._editor_for(plugin)
        assert_equal(editor, editor2)

    def test_editor_is_recreated_when_controller_changes(self):
        p1 = self._datafile_plugin()
        p2 = self._datafile_plugin()
        e1 = self._editor_for(p1)
        e2 = self._editor_for(p2)
        assert_true(e1 is not e2)

    def test_editor_is_destroyed_when_new_is_created(self):
        ed = self._datafile_editor()
        ed.destroy = mock()
        self._datafile_editor()
        assert_true(ed.destroy.called)

    def _datafile_editor(self):
        return self.creator.editor_for(self._datafile_plugin(),
                                       wx.Frame(None), None)

    def _datafile_plugin(self):
        return FakePlugin(self._registered_editors,
                          self._datafile_controller())

    def _variable_plugin(self):
        return FakePlugin(self._registered_editors,
                          VariableController(VariableTableController(
                              self._datafile_controller(), None),
                              Variable(None, '','')))

    def _no_item_selected_plugin(self):
        return FakePlugin(self._registered_editors, None)

    def _datafile_controller(self):
        return DataController(NewTestCaseFile(DATAPATH), None)


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score

from sklearn.model_selection import cross_val_score

import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import precision_score, recall_score, f1_score, confusion_matrix, plot_confusion_matrix

# Load the dataset
column_names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class']
data = pd.read_csv('data/bezdekIris.data', names=column_names)

# Encode class labels as integers
le = LabelEncoder()
data['class'] = le.fit_transform(data['class'])

# Split the dataset into features (X) and labels (y)
X = data.drop('class', axis=1)
y = data['class']

# Split the dataset into training, validation, and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, test_size=0.5, random_state=42)

# Hyperparameter tuning using GridSearchCV
knn_params = {'n_neighbors': list(range(1, 31))}
dt_params = {'max_depth': list(range(1, 11))}
svm_params = {'C': [0.1, 1, 10, 100], 'kernel': ['linear', 'rbf']}

knn = GridSearchCV(KNeighborsClassifier(), knn_params, cv=5)
dt = GridSearchCV(DecisionTreeClassifier(random_state=42), dt_params, cv=5)
svm = GridSearchCV(SVC(random_state=42), svm_params, cv=5)

# Train the models
knn.fit(X_train, y_train)
dt.fit(X_train, y_train)
svm.fit(X_train, y_train)

# Evaluate the models on the validation set
knn_val_preds = knn.predict(X_val)
dt_val_preds = dt.predict(X_val)
svm_val_preds = svm.predict(X_val)

knn_val_acc = accuracy_score(y_val, knn_val_preds)
dt_val_acc = accuracy_score(y_val, dt_val_preds)
svm_val_acc = accuracy_score(y_val, svm_val_preds)

print(f"k-NN Validation Accuracy: {knn_val_acc}")
print(f"Decision Tree Validation Accuracy: {dt_val_acc}")
print(f"SVM Validation Accuracy: {svm_val_acc}")

# Perform 5-fold cross-validation for each model
knn_cv_scores = cross_val_score(KNeighborsClassifier(**knn.best_params_), X, y, cv=5)
dt_cv_scores = cross_val_score(DecisionTreeClassifier(**dt.best_params_, random_state=42), X, y, cv=5)
svm_cv_scores = cross_val_score(SVC(**svm.best_params_, random_state=42), X, y, cv=5)

# Calculate the mean cross-validation accuracy for each model
knn_cv_mean_acc = knn_cv_scores.mean()
dt_cv_mean_acc = dt_cv_scores.mean()
svm_cv_mean_acc = svm_cv_scores.mean()

print(f"k-NN 5-Fold Cross-Validation Mean Accuracy: {knn_cv_mean_acc}")
print(f"Decision Tree 5-Fold Cross-Validation Mean Accuracy: {dt_cv_mean_acc}")
print(f"SVM 5-Fold Cross-Validation Mean Accuracy: {svm_cv_mean_acc}")

# Calculate metrics for each model using test set
for model, name in [(knn, "k-NN"), (dt, "Decision Tree"), (svm, "SVM")]:
    preds = model.predict(X_test)
    precision = precision_score(y_test, preds, average='macro')
    recall = recall_score(y_test, preds, average='macro')
    f1 = f1_score(y_test, preds, average='macro')

    print(f"{name} Test Precision: {precision}")
    print(f"{name} Test Recall: {recall}")
    print(f"{name} Test F1-score: {f1}")
    print()

    # Plot confusion matrix
    plot_confusion_matrix(model, X_test, y_test)
    plt.title(f"{name} Confusion Matrix")
    plt.show()

# Function to plot decision boundaries for 2D data
def plot_decision_boundary(model, X, y, ax, title):
    h = .02
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

    Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)

    ax.contourf(xx, yy, Z, alpha=0.8)
    scatter = ax.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', marker='o', s=50)
    ax.set_xlim(X[:, 0].min() - 0.5, X[:, 0].max() + 0.5)
    ax.set_ylim(X[:, 1].min() - 0.5, X[:, 1].max() + 0.5)
    ax.set_xticks(())
    ax.set_yticks(())
    ax.set_title(title)
    ax.legend(*scatter.legend_elements(), title="Classes")

# Select two features for visualization
X_train_2d = X_train.iloc[:, :2].values
X_test_2d = X_test.iloc[:, :2].values

# Train models with 2D data
knn_2d = KNeighborsClassifier(**knn.best_params_).fit(X_train_2d, y_train)
dt_2d = DecisionTreeClassifier(**dt.best_params_, random_state=42).fit(X_train_2d, y_train)
svm_2d = SVC(**svm.best_params_, random_state=42).fit(X_train_2d, y_train)

# Plot decision boundaries
fig, axes = plt.subplots(1, 3, figsize=(18, 5))
plot_decision_boundary(knn_2d, X_test_2d, y_test.values, axes[0], "k-NN Decision Boundary")
plot_decision_boundary(dt_2d, X_test_2d, y_test.values, axes[1], "Decision Tree Decision Boundary")
plot_decision_boundary(svm_2d, X_test_2d, y_test.values, axes[2], "SVM Decision Boundary")

plt.show()


# #################################################################################
# # GJI final pub specs                                                           #
# import matplotlib                                                               #
# from matplotlib import rc                                                       #
# matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{timet,amsmath}"]      #
# rc('font',**{'family':'serif','serif':['timet']})                               #
# rc('font',**{'size':8})                                                         #
# rc('text', usetex=True)                                                         #
# # converts pc that GJI is defined in to inches                                  #
# # In GJI \textwidth = 42pc                                                      #
# #        \columnwidth = 20pc                                                    #
# def pc2in(pc):                                                                  #
#     return pc*12/72.27                                                          #
# #################################################################################

#from GJIPlot import *

import numpy as np
import matplotlib.pyplot as plt
#from invertColours import *
from akvo.tressel.decay import *
from scipy import signal

def quadrature(T, vL, wL, dt, xn, DT, t):
        # decimate
    # blind decimation
    # 1 instead of T
    irsamp = int(T) * int(  (1./vL) / dt) # real
    iisamp =       int(  ((1./vL)/ dt) * ( .5*np.pi / (2.*np.pi) ) ) # imaginary

    dsamp =  int( DT / dt) # real

    iisamp += dsamp

    ############################################################
    # simple quadrature-detection via sampling
    xr = xn[dsamp::irsamp]
    xi = xn[iisamp::irsamp]
    phase = np.angle( xr + 1j*xi )
    abse = np.abs( xr + 1j*xi )

    # times
    #ta = np.arange(0, TT, dt)
    #te = np.arange(DT, TT, TT/len(abse))

    #############################################################
    # hilbert transform
    ht = signal.hilbert(xn) #, 100))
    he = np.abs(ht)         #, 100))
    hp = ((np.angle(ht[dsamp::irsamp])))

    #############################################################
    # Resample ht
    #htd = signal.decimate(he, 100, ftype='fir')
    #td = signal.decimate(t, 100, ftype='fir')
    #[htd, td] = signal.resample(he, 21, t)
    #toss first, and use every third
    #htd = htd[1::3]
    #td = td[1::3]

    #############################################################
    # Pre-envelope
    #gplus = xn + 1j*ht

    #############################################################
    # Complex envelope
    #gc = gplus / np.exp(1j*wL*t)

    #############################################################
    ## Design a low-pass filter
    FS = 1./dt                                           # sampling rate
    FC = 10.05/(0.5*FS)                                 # cutoff frequency at 0.05 Hz
    N = 11                                               # number of filter taps
    a = [1]                                              # filter denominator
    b = signal.firwin(N, cutoff=FC, window='hamming')    # filter numerator

    #############################################################
    ## In-phase
    #2*np.cos(wL*t)
    dw = 0 # -2.*np.pi*2
    Q = signal.filtfilt(b, a, xn*2*np.cos((wL+dw)*t))  # X
    I = signal.filtfilt(b, a, xn*2*np.sin((wL+dw)*t))  # Y

    ###############################################
    # Plots
    #plt.plot(ht.real)
    #plt.plot(ht.imag)
    #plt.plot(np.abs(ht))
    #plt.plot(gc.real)
    #plt.plot(gc.imag)

    #plt.plot(xn)
    #plt.plot(xn)
    #plt.plot(ta, xn)
    #plt.plot(te, abse, '-.', linewidth=2, markersize=10)
    #plt.plot(ta, he, '.', markersize=10 )
    #plt.plot(td, htd, color='green', linewidth=2)
    # Phase Plots
    #ax2 = plt.twinx()
    #ax2.plot(te, hp, '.', markersize=10, color='green' )
    #ax2.plot(te, phase, '-.', linewidth=2, markersize=10, color='green')

    return Q[N:-N], I[N:-N], t[N:-N]

#     #####################################################################
#     # regress raw signal
#
#     #[peaks, times, ind] = peakPicker(xn, wL, dt)
#     #[a0,b0,rt20] =  regressCurve(peaks,  times) #,sigma2=1,intercept=True):
#
#     dsamp = int( DT / dt) # real
#     # regress analytic signal
#     [a0,b0,rt20] =  regressCurve(he[dsamp::],  t[dsamp::], intercept=True) #,sigma2=1,intercept=True):
#     #[b0,rt20] =  regressCurve(he[dsamp::],  t[dsamp::], intercept=False) #,sigma2=1,intercept=True):
#     #[a0,b0,rt20] =  regressCurve(he,  t) #,sigma2=1,intercept=True):
#
#     # regress downsampled
#     [a,b,rt2] =  regressCurve(abse,  t[dsamp::irsamp], intercept=True) #,sigma2=1,intercept=True):
#     #[b,rt2] =  regressCurve(htd,  td, intercept=False) #,sigma2=1,intercept=True):
#
#     return irsamp, iisamp, htd, b0, rt20, ta, b, rt2, phase, td, he, dsamp
#     #return irsamp, iisamp, abse, a0, b0, rt20, times, a, b, rt2, phase

def RotateAmplitude(X, Y, zeta, df, t):
    V = X + 1j*Y
    return np.abs(V) * np.exp( 1j * ( np.angle(V) - zeta - 2.*np.pi*df*t ) )
    #return np.abs(V) * np.exp( 1j * ( np.angle(V) - zeta - df*t ) )

def bootstrapWindows(N, nboot, isum, adapt=False):
    """ Bootstraps noise as a function of gate width
        N = input noise signal
        nboot = number of boostrap windows to perform
        isum = length of windows (L_i)
        adapt = reduce nboot as window size increases
    """
    nc = np.shape(N)[0]
    Means = {}

    if adapt:
        Means = -9999*np.ones((len(isum), nboot//isum[0])) # dummy value
        for ii, nwin in enumerate(isum):
            for iboot in range(nboot//isum[ii]):
                cs = np.random.randint(0,nc-nwin)
                Means[ii,iboot] = np.mean( N[cs:cs+nwin] )
        Means = np.ma.masked_less(Means, -9995)

    else:
        Means = np.zeros((len(isum), nboot))
        for ii, nwin in enumerate(isum):
            for iboot in range(nboot):
                cs = np.random.randint(0,nc-nwin)
                Means[ii,iboot] = np.mean( N[cs:cs+nwin] )

    return Means #, np.array(isum)

def gateIntegrate(T2D, T2T, gpd, sigma, stackEfficiency=2.):
    """ Gate integrate the signal to gpd, gates per decade
        T2D = the time series to gate integrate, complex
        T2T = the abscissa values
        gpd = gates per decade
        sigma = estimate of standard deviation for theoretical gate noise
        stackEfficiency = exponential in theoretical gate noise, 2 represents ideal stacking
    """

    # use artificial time gates so that early times are fully captured
    T2T0 = T2T[0]
    T2TD = T2T[0] - (T2T[1]-T2T[0])
    T2T -= T2TD

    #####################################
    # calculate total number of decades #
    # windows edges are approximate until binning but will be adjusted to reflect data timing, this
    # primarily impacts bins with a few samples
    nd = np.log10(T2T[-1]/T2T[0])
    tdd = np.logspace( np.log10(T2T[0]), np.log10(T2T[-1]), (int)(gpd*nd)+1, base=10, endpoint=True)
    tdl = tdd[0:-1]                 # approximate window left edges
    tdr = tdd[1::]                  # approximate window right edges
    td = (tdl+tdr) / 2.             # approximate window centres

    Vars = np.zeros( len(td) )
    htd = np.zeros( len(td), dtype=complex )
    isum = np.zeros( len(td), dtype=int )

    ii = 0
    for itd in range(len(T2T)):
        if ( round(T2T[itd], 4) > round(tdr[ii], 4) ):
            ii += 1
            # correct window edges to centre about data
            tdr[ii-1] = (T2T[itd-1]+T2T[itd])*.5
            tdl[ii  ] = (T2T[itd-1]+T2T[itd])*.5
        isum[ii] += 1
        htd[ii] += T2D[ itd ]
        Vars[ii] += sigma**2

    td = (tdl+tdr) / 2.             # actual window centres
    sigma2 = np.sqrt( Vars * ((1/(isum))**stackEfficiency) )

    # Reset abscissa where isum == 1
    # when there is no windowing going on
    td[isum==1] = T2T[0:len(td)][isum==1]

    tdd = np.append(tdl, tdr[-1])

    htd /= isum # average
    T2T += T2TD
    return td+T2TD, htd, tdd+T2TD, sigma2, isum  # centre abscissa, data, window edges, error

if __name__ == "__main__":

    dt = 1e-4
    TT = 1.5
    t = np.arange(0, TT, dt)
    vL = 2057.
    wL =  2.*np.pi*vL
    wL2 = 2.*np.pi*(vL-2.5) #-2) #-2.2) # 3 Hz off
    zeta = -np.pi/6. #4.234
    t2 = .150

    xs = np.exp(-t/t2) * np.cos(wL2*t + zeta)
    xe = np.exp(-t/t2)
    xn = xs + np.random.normal(0,.1,len(xs))# + (np.sign(xs)
            #    np.random.random_integers(-1,1,len(xs))*0.6*np.random.lognormal(0, .35, len(xs)) + \
            #    np.random.random_integers(-1,1,len(xs))*.004*np.random.weibull(.25, len(xs)), 60)))

    # quadrature detection downsampling
    T = 50    # sampling period, grab every T'th oscilation
    DT = .002 #85  # dead time ms
    #[irsamp, iisamp, abse, b0, rt20, times, b, rt2, phase, tdec, he, dsamp] = quadDetect(T, vL, wL, dt, xn, DT)

    [Q, I, tt] = quadrature(T, vL, wL, dt, xn, DT, t)
    [E0,df,phi,T2] = quadratureDetect(Q, I, tt)
    print("df", df)
    D = RotateAmplitude(I, Q, phi, df, tt)

    fig = plt.figure(figsize=[pc2in(20), pc2in(14)]) #
    ax1 = fig.add_axes([.125,.2,.8,.7])
    #ax1.plot(tt*1e3, np.exp(-tt/t2), linewidth=2, color='black', label="actual")
    ax1.plot(tt*1e3, D.imag, label="CA", color='red')
    ax1.plot(t*1e3, xn, color='blue', alpha=.25)
    ax1.plot(tt*1e3, I, label="inphase", color='blue')
    ax1.plot(tt*1e3, Q, label="quadrature", color='green')

    #ax1.plot(tt*1e3, np.angle( Q + 1j*I), label="angle", color='purple')

    GT, GD = gateIntegrate( D.imag, tt, 10  )
    GT, GDR = gateIntegrate( D.real, tt, 10  )
    GT, GQ = gateIntegrate( Q, tt, 10  )
    GT, GI = gateIntegrate( I, tt, 10  )
    #ax1.plot(tt*1e3, np.arctan( Q/I), label="angle", color='purple')
    #ax1.plot(GT*1e3, np.real(GD), 'o', label="GATE", color='purple')
    #ax1.plot(GT*1e3, np.real(GDR), 'o', label="GATE Real", color='red')
    #ax1.plot(GT*1e3, np.arctan( np.real(GQ)/np.real(GI)), 'o',label="GATE ANGLE", color='magenta')

    ax1.set_xlabel(r"time [ms]")
    ax1.set_ylim( [-1.25,1.65] )

    #light_grey = np.array([float(248)/float(255)]*3)
    legend = plt.legend( frameon=True, scatterpoints=1, numpoints=1, labelspacing=0.2  )
    #rect = legend.get_frame()
    fixLeg(legend)
    #rect.set_color('None')
    #rect.set_facecolor(light_grey)
    #rect.set_linewidth(0.0)
    #rect.set_alpha(0.5)

    # Remove top and right axes lines ("spines")
    spines_to_remove = ['top', 'right']
    for spine in spines_to_remove:
        ax1.spines[spine].set_visible(False)
    #ax1.xaxis.set_ticks_position('none')
    #ax1.yaxis.set_ticks_position('none')
    ax1.get_xaxis().tick_bottom()
    ax1.get_yaxis().tick_left()

    plt.savefig('rotatetime.pdf',dpi=600)
    plt.savefig('rotatetime.eps',dpi=600)

    # phase part
    plt.figure()
    plt.plot( tt*1e3, D.real, label="CA", color='red' )

    plt.show()
    exit()


# -*- coding: utf-8 -*-
"""
Created on Sun Nov 10 15:10:01 2019

@author: Rajdeep
@author: Mtajic
"""

import pydotplus
import numpy as np

import matplotlib.pyplot as plt

from sklearn import svm, tree, datasets
from sklearn.utils import shuffle
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.externals.six import StringIO
from sklearn.model_selection import cross_validate

from skimage import io
from skimage.filters import gaussian
from skimage.transform import resize

from IPython.display import SVG, display
from graphviz import Source
from ipywidgets import interactive

from IPython.display import Image
from sklearn.tree import export_graphviz

from IPython.display import Image

class DataSet:
    def __init__(self, inputs=None, targets=None):
        self.inputs = [] if inputs is None else inputs
        self.targets = [] if targets is None else targets

if __name__ == '__main__':
    dataset = DataSet()
    print('Loading Training Data...')
    labels = ['01_palm', '02_l', '03_fist', '04_fist_moved', '05_thumb',
              '06_index', '07_ok', '08_palm_moved', '09_c', '10_down', ]

    # TODO: need to shuffle data before partitioning
    # 75% to train
    for i in range(10):
        for label in labels:
            for j in range(1, 151):  # 201):
                label.split()
                image = io.imread('data/leapGestRecog/0' +
                                  str(i) + '/' +
                                  label + '/frame_0' +
                                  str(i) + '_' +
                                  label[0:2] + '_' +
                                  str(j).zfill(4) + '.png')
                # TODO: should we flatten or resize here?
                img_blurred = gaussian(image, sigma=1.65)
                dataset.inputs.append(resize(img_blurred, (60, 160), preserve_range=True).flatten())
                dataset.targets.append(label)

    # shuffle data
    x_train, y_train = shuffle(dataset.inputs, dataset.targets)

    # Model Selection

    print('Model Selection...')
    testScores = {}
    myMaxScore = 0
    bestFunctionPrameters = ""
    clf_cv5 = ""
    for i in [6, 7, 8, 9, 10]:
        for j in ["gini", "entropy"]:
            clf = tree.DecisionTreeClassifier(criterion=j, random_state=100, max_depth=i, max_features=240)

            # perform 5-fold cross validation for each model
            scores = cross_validate(clf, x_train, y_train, scoring='precision_macro', cv=5, return_estimator=True)
            testScores[j + "_" + str(i)] = scores["test_score"]
            myScore = np.max(scores["test_score"], axis=0)
            print(j + "_" + str(i), " Score: ", str(myScore))
            if myScore > myMaxScore:
                clf_cv5 = scores["estimator"][np.argmax(scores["test_score"], axis=0)]
                myMaxScore = myScore
                bestFunctionPrameters = j + "_" + str(i)

    # Training
    print('Training...')
    bst_dt = clf_cv5

    labels = ['01_palm', '02_l', '03_fist', '04_fist_moved', '05_thumb',
              '06_index', '07_ok', '08_palm_moved', '09_c', '10_down', ]
    print('Loading Testing Data...')

    # 25% to test
    test_data = DataSet()
    for i in range(10):
        for label in labels:
            for j in range(151, 201):  # 1, 201):
                label.split()
                image = io.imread('data/leapGestRecog/0' +
                                  str(i) + '/' +
                                  label + '/frame_0' +
                                  str(i) + '_' +
                                  label[0:2] + '_' +
                                  str(j).zfill(4) + '.png')
                img_blurred = gaussian(image, sigma=1.65)
                test_data.inputs.append(resize(img_blurred, (60, 160), preserve_range=True).flatten())
                test_data.targets.append(label)

    print('Testing...')
    predicted = bst_dt.predict(test_data.inputs)
    print("\nClassification Report")

    print(classification_report(test_data.targets, predicted))
    print("\nConfusion Matrix")
    print(confusion_matrix(test_data.targets, predicted))

    # Visualizing the Decision Tree Graph
    print('Visualizing the Decision Tree Graph')

    dot_data = StringIO()
    tree.export_graphviz(bst_dt, out_file=dot_data
                         , feature_names=[f"X{i},{j}" for i in range(60) for j in range(160)]
                         , class_names=['01_palm', '02_l', '03_fist', '04_fist_moved', '05_thumb',
                                        '06_index', '07_ok', '08_palm_moved', '09_c', '10_down', ]
                         , filled=True, rounded=True
                         , special_characters=True)

    graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
    Image(graph.create_png())

    # creating the box plot for model-selection
    s = testScores.values()
    labels = testScores.keys()

    fig = plt.figure(figsize=(8, 6))

    plt.boxplot([x for x in s], 0, '', 1)
    plt.xticks([y + 1 for y in range(len([x for x in s]))], labels, rotation=70)
    plt.xlabel('Models(metric and max depth)')
    plt.ylabel('Accuracy')
    t = plt.title('Box plot of HyperParameters with 5-fold cross validation')
    plt.show()


import webapp2
import jinja2
import os
from google.appengine.ext import ndb
from twilio.rest import TwilioRestClient
from time import time
import json
import logging
from common import make_template
from private import account_sid, auth_token

class User(ndb.Model):
    """Model for the user db"""
    fullname = ndb.StringProperty(indexed=True)
    phone_number = ndb.StringProperty(indexed=True)
    phone_worker = ndb.BooleanProperty()
    can_text = ndb.BooleanProperty()
    PAB = ndb.BooleanProperty()

class Call(ndb.Model):
    """model for the calls db"""
    calls = ndb.TextProperty(indexed=False)

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True)

class NewCall(webapp2.RequestHandler):
    """This gives a page to make a call to any specified users"""
    def get(self):
        template_values = make_template(self)

        info = ndb.gql("SELECT * FROM User")
        user_list = []
        for i in info:
            to_append = {}
            to_append['fullname'] = i.fullname
            to_append['phone_number'] = i.phone_number
            to_append['can_text'] = "disabled" if (i.can_text==False) else ""
            user_list.append(to_append)
        template_values['user_list'] = user_list

        template = JINJA_ENVIRONMENT.get_template('newcall.jinja')
        self.response.write(template.render(template_values))

class MakeCall(webapp2.RequestHandler):
    """Actually does call and text type stuff.  Yay."""
    def get(self):
        template_values = make_template(self)
        if template_values['admin']:
            client = TwilioRestClient(account_sid, auth_token)

            to_text = self.request.get_all('text')
            if self.request.get('PAB_text')=="on":
                info = ndb.gql("SELECT * FROM User WHERE PAB=True")
                for i in info:
                    if i.can_text and i.fullname not in to_text:
                        to_text.append(i.fullname)
            if self.request.get('ALL_text')=="on":
                info = ndb.gql("SELECT * FROM User WHERE can_text=True")
                for i in info:
                    if i.fullname not in to_text:
                        to_text.append(i.fullname)
            for i in to_text:
                infos = User.query(User.fullname==i).fetch(1, projection=[User.phone_number])
                for info in infos:
                    message_out = client.messages.create(to=info.phone_number, from_='2065576875', body=self.request.get('smstext'))
                    logging.debug(message_out)

            call_id=int(time())
            call = Call(key=ndb.Key(Call, call_id))
            to_call = []
            for i in self.request.get_all('call'):
                info = ndb.gql("SELECT * FROM User WHERE fullname=:1", i)
                for i in info:
                    to_call.append(i.phone_number)
            if self.request.get('PAB_call')=="on":
                info = ndb.gql("SELECT * FROM User WHERE PAB=True")
                for i in info:
                    if i.phone_number not in to_call:
                        to_call.append(i.phone_number)
            if self.request.get('ALL_call')=='on':
                info = ndb.gql("SELECT * FROM User")
                for i in info:
                    if i.phone_number not in to_call:
                        to_call.append(i.phone_number)
            call.calls = str(json.dumps(to_call))
            call_id = call.put()
            call_id = call_id.id()
            call_out = client.calls.create(to=self.request.get('your_phone'),
                                       from_='2065576875',
                                       url='https://teen-link.appspot.com/twiml?to_call=%s' % (str(call_id)),
                                       method='GET')
            logging.debug(call_out)
        template = JINJA_ENVIRONMENT.get_template('makecall.jinja')
        self.response.write(template.render(template_values))

app = webapp2.WSGIApplication([
                               ('/action/newcall', NewCall),
                               ('/action/makecall', MakeCall)],
                              debug=True)


import math
import re
from math import *  # noqa: F401, F403

from sympy import Eq, solve, symbols

from .calculate import Calculate

def Solve(equations_str):
    try:
        equations_str = equations_str.replace(' ', '')
        equations_ori = re.split(r'[,;]+', equations_str)
        equations_str = equations_str.replace('^', '**')
        equations_str = re.sub(r'(\(.*\))([a-zA-Z])', r'\1 * \2',
                               equations_str)
        equations_str = re.sub(r'(\d+)([a-zA-Z])', r'\1 * \2', equations_str)
        equations_str = equations_str.replace('pi', str(math.pi))
        equations = re.split(r'[,;]+', equations_str)
        vars_list = list(set(re.findall(r'[a-zA-Z]+', equations_str)))
        vars = {var: symbols(var) for var in vars_list}

        output = ''
        eqs = []
        for eq in equations:
            if '=' in eq:
                left, right = eq.split('=')
                eqs.append(
                    Eq(
                        eval(left.strip(), {}, vars),
                        eval(right.strip(), {}, vars)))
        solutions = solve(eqs, vars, dict=True)

        vars_values = {var: [] for var in vars_list}
        if isinstance(solutions, list):
            for idx, solution in enumerate(solutions):
                for var, sol in solution.items():
                    output += f'{var}_{idx} = {sol}\n'
                    vars_values[str(var)].append(sol)
        else:
            for var, sol in solutions.items():
                output += f'{var} = {sol}\n'
                vars_values[str(var)].append(sol)
        for eq, eq_o in zip(equations, equations_ori):
            if '=' not in eq:
                for var in vars_list:
                    need_note = True if len(vars_values[var]) > 1 else False
                    for idx, value in enumerate(vars_values[var]):
                        eq_to_calc = eq.replace(var, str(value))
                        calc_result = Calculate(eq_to_calc)
                        if need_note:
                            eq_name = eq_o.replace(var, f'{var}_{idx}')
                        else:
                            eq_name = eq_o
                        if calc_result != 'No results.':
                            output += f'{eq_name} = {calc_result}\n'

        return output.strip()
    except Exception:
        return 'No result.'


# 新建登录测试类,继承 unittest.Testcase
import unittest

from parameterized import parameterized

from base.get_driver import GetDriver
from page.page_login import PageLogin

# from base.base import base_click
from tools.read_txt import read_txt
from base.get_logger import GetLogger

log = GetLogger().get_logger()

def get_data():
    # 逆向,正向
    # return [("13800138003", "123456", "8888", "账号不存在!", False,),
    #         ("13800138006", "123456", "8888", "", True,)]
    # return [("13800138003", "123456", "8888", "账号不存在!", False,)]
    arrs = []
    for data in read_txt("login.txt"):
        # 列表添加元组
        arrs.append(tuple(data.strip().split(",")))
    # 返回列表下标为1之外的所有元素
    return arrs[2:]

class TestLogin(unittest.TestCase):
    driver = None
    login = None

    @classmethod
    def setUpClass(cls):
        try:
            # 实例化并获取driver
            cls.driver = GetDriver().get_driver()
            # 操作都封装在 PageLogin 类中,先实例化
            cls.login = PageLogin(cls.driver)
            # 点击登录链接 (放在这里很重要,无论正向,逆向 先后 都可以走)
            cls.login.page_click_login_link()
        except Exception as e:
            log.error("错误:", e)

    @classmethod
    def tearDownClass(cls):
        # 关闭driver 驱动对象
        GetDriver().quit_driver()

    # 第三个方法,  新建登录测试方法
    @parameterized.expand(get_data())
    def test_login(self, username, pwd, verify_code, expect_result, status):
        try:
            # 调用组合业务方法
            # @parameterized.expand(username,pwd,verify_code)
            self.login.page_login(username, pwd, verify_code)
            # 判断正向:
            # 根据文本文件中的状态字符串判断
            if status == "true":
                #     断言
                # print(self.login.page_if_login_success())
                self.assertTrue(self.login.page_if_login_success())
                #     点击安全退出
                self.login.page_click_logout_link()
                #     点击登录链接
                self.login.page_click_login_link()
            # 判断逆向:
            else:
                #     获取错误信息
                msg = self.login.page_get_error_msg()
                print(msg)
                # 点击弹窗确定按钮
                self.login.page_click_error_alert()
                try:
                    self.assertEqual(msg, expect_result)
                except Exception as e:
                    # 出错的地方,都需要 log.error
                    log.error("错误:", e)
                    self.login.base_get_screen_shot()
        except Exception as e:
            log.error("错误:", e)
            self.login.base_get_screen_shot()

        # 无论正向,逆向 先后 都可以走


from sqlalchemy.exc import IntegrityError
from src.config.database import SessionLocal
from ..config.models import Timezones, ZoneDetails, ErrorLog

def populate_timezones_table(timezones):
    with SessionLocal() as db:
        try:
            db.query(Timezones).delete()
            for tz in timezones:
                timezone = Timezones(
                    countrycode=tz['countrycode'],
                    countryname=tz['countryname'],
                    zonename=tz['zonename'],
                    gmtoffset=tz['gmtoffset']
                )
                db.add(timezone)
            db.commit()
        except IntegrityError as e:
            db.rollback()
            error_message = f"Integrity error occurred while populating timezones table: {str(e)}"
            save_error_log(error_message)

def populate_zone_details_table(detail):
    with SessionLocal() as db:
        # Check if a row with the same zonename, zonestart, and zoneend already exists
        existing_row = db.query(ZoneDetails).filter_by(
            zonename=detail['zonename'],
            zonestart=detail['zonestart'],
            zoneend=detail['zoneend']
        ).first()

        if not existing_row:
            zone_detail = ZoneDetails(
                countrycode=detail['countrycode'],
                countryname=detail['countryname'],
                zonename=detail['zonename'],
                gmtoffset=detail['gmtoffset'],
                dst=detail['dst'],
                zonestart=detail['zonestart'],
                zoneend=detail['zoneend']
            )

            try:
                db.add(zone_detail)
                db.commit()
            except IntegrityError as e:
                db.rollback()
                print(f"IntegrityError occurred: {str(e)}")

def save_error_log(error_message):
    with SessionLocal() as db:
        error_log = ErrorLog(error_message=error_message)
        db.add(error_log)
        db.commit()


"""A growing set of tests designed to ensure when isort implements a feature described in a ticket
it fully works as defined in the associated ticket.
"""
from functools import partial
from io import StringIO

import pytest

import isort
from isort import Config, exceptions

def test_semicolon_ignored_for_dynamic_lines_after_import_issue_1178():
    """Test to ensure even if a semicolon is in the decorator in the line following an import
    the correct line spacing determination will be made.
    See: https://github.com/pycqa/isort/issues/1178.
    """
    assert isort.check_code(
        """
import pytest

@pytest.mark.skip(';')
def test_thing(): pass
""",
        show_diff=True,
    )

def test_isort_automatically_removes_duplicate_aliases_issue_1193():
    """Test to ensure isort can automatically remove duplicate aliases.
    See: https://github.com/pycqa/isort/issues/1281
    """
    assert isort.check_code("from urllib import parse as parse\n", show_diff=True)
    assert (
        isort.code("from urllib import parse as parse", remove_redundant_aliases=True)
        == "from urllib import parse\n"
    )
    assert isort.check_code("import os as os\n", show_diff=True)
    assert isort.code("import os as os", remove_redundant_aliases=True) == "import os\n"

def test_isort_enables_floating_imports_to_top_of_module_issue_1228():
    """Test to ensure isort will allow floating all non-indented imports to the top of a file.
    See: https://github.com/pycqa/isort/issues/1228.
    """
    assert (
        isort.code(
            """
import os

def my_function_1():
    pass

import sys

def my_function_2():
    pass
""",
            float_to_top=True,
        )
        == """
import os
import sys

def my_function_1():
    pass

def my_function_2():
    pass
"""
    )

    assert (
        isort.code(
            """
import os

def my_function_1():
    pass

# isort: split
import sys

def my_function_2():
    pass
""",
            float_to_top=True,
        )
        == """
import os

def my_function_1():
    pass

# isort: split
import sys

def my_function_2():
    pass
"""
    )

    assert (
        isort.code(
            """
import os

def my_function_1():
    pass

# isort: off
import b
import a
def y():
    pass

# isort: on
import b

def my_function_2():
    pass

import a
""",
            float_to_top=True,
        )
        == """
import os

def my_function_1():
    pass

# isort: off
import b
import a
def y():
    pass

# isort: on
import a
import b

def my_function_2():
    pass
"""
    )

def test_isort_provides_official_api_for_diff_output_issue_1335():
    """Test to ensure isort API for diff capturing allows capturing diff without sys.stdout.
    See: https://github.com/pycqa/isort/issues/1335.
    """
    diff_output = StringIO()
    isort.code("import b\nimport a\n", show_diff=diff_output)
    diff_output.seek(0)
    assert "+import a" in diff_output.read()

def test_isort_warns_when_known_sections_dont_match_issue_1331():
    """Test to ensure that isort warns if there is a mismatch between sections and known_sections.
    See: https://github.com/pycqa/isort/issues/1331.
    """
    assert (
        isort.place_module(
            "bot_core",
            config=Config(
                known_robotlocomotion_upstream=["bot_core"],
                sections=["ROBOTLOCOMOTION_UPSTREAM", "THIRDPARTY"],
            ),
        )
        == "ROBOTLOCOMOTION_UPSTREAM"
    )
    with pytest.warns(UserWarning):
        assert (
            isort.place_module(
                "bot_core",
                config=Config(
                    known_robotlocomotion_upstream=["bot_core"],
                    sections=["ROBOTLOOMOTION_UPSTREAM", "THIRDPARTY"],
                ),
            )
            == "THIRDPARTY"
        )
    with pytest.warns(UserWarning):
        assert (
            isort.place_module(
                "bot_core", config=Config(known_robotlocomotion_upstream=["bot_core"])
            )
            == "THIRDPARTY"
        )

def test_isort_supports_append_only_imports_issue_727():
    """Test to ensure isort provides a way to only add imports as an append.
    See: https://github.com/pycqa/isort/issues/727.
    """
    assert isort.code("", add_imports=["from __future__ import absolute_imports"]) == ""
    assert (
        isort.code("import os", add_imports=["from __future__ import absolute_imports"])
        == """from __future__ import absolute_imports

import os
"""
    )

    # issue 1838: don't append in middle of class
    assert isort.check_code(
        '''class C:
    """a

    """
    # comment
''',
        append_only=True,
        add_imports=["from __future__ import annotations"],
        show_diff=True,
    )

def test_isort_supports_shared_profiles_issue_970():
    """Test to ensure isort provides a way to use shared profiles.
    See: https://github.com/pycqa/isort/issues/970.
    """
    assert isort.code("import a", profile="example") == "import a\n"  # shared profile
    assert isort.code("import a", profile="black") == "import a\n"  # bundled profile
    with pytest.raises(exceptions.ProfileDoesNotExist):
        assert isort.code("import a", profile="madeupfake") == "import a\n"  # non-existent profile

def test_treating_comments_as_code_issue_1357():
    """Test to ensure isort provides a way to treat comments as code.
    See: https://github.com/pycqa/isort/issues/1357
    """
    assert (
        isort.code(
            """# %%
import numpy as np
np.array([1,2,3])

# %%
import pandas as pd
pd.Series([1,2,3])

# %%
# This is a comment on the second import
import pandas as pd
pd.Series([4,5,6])""",
            treat_comments_as_code=["# comment1", "# %%"],
        )
        == """# %%
import numpy as np

np.array([1,2,3])

# %%
import pandas as pd

pd.Series([1,2,3])

# %%
# This is a comment on the second import
import pandas as pd

pd.Series([4,5,6])
"""
    )
    assert (
        isort.code(
            """# %%
import numpy as np
np.array([1,2,3])

# %%
import pandas as pd
pd.Series([1,2,3])

# %%
# This is a comment on the second import
import pandas as pd
pd.Series([4,5,6])""",
            treat_comments_as_code=["# comment1", "# %%"],
            float_to_top=True,
        )
        == """# %%
import numpy as np
# This is a comment on the second import
import pandas as pd

np.array([1,2,3])

# %%
pd.Series([1,2,3])

# %%
pd.Series([4,5,6])
"""
    )
    assert (
        isort.code(
            """# %%
import numpy as np
np.array([1,2,3])

# %%
import pandas as pd
pd.Series([1,2,3])

# %%
# This is a comment on the second import
import pandas as pd
pd.Series([4,5,6])""",
            treat_all_comments_as_code=True,
        )
        == """# %%
import numpy as np

np.array([1,2,3])

# %%
import pandas as pd

pd.Series([1,2,3])

# %%
# This is a comment on the second import
import pandas as pd

pd.Series([4,5,6])
"""
    )
    assert (
        isort.code(
            """import b

# these are special imports that have to do with installing X plugin
import c
import a
""",
            treat_all_comments_as_code=True,
        )
        == """import b

# these are special imports that have to do with installing X plugin
import a
import c
"""
    )

def test_isort_allows_setting_import_types_issue_1181():
    """Test to ensure isort provides a way to set the type of imports.
    See: https://github.com/pycqa/isort/issues/1181
    """
    assert isort.code("from x import AA, Big, variable") == "from x import AA, Big, variable\n"
    assert (
        isort.code("from x import AA, Big, variable", constants=["variable"])
        == "from x import AA, variable, Big\n"
    )
    assert (
        isort.code("from x import AA, Big, variable", variables=["AA"])
        == "from x import Big, AA, variable\n"
    )
    assert (
        isort.code(
            "from x import AA, Big, variable",
            constants=["Big"],
            variables=["AA"],
            classes=["variable"],
        )
        == "from x import Big, variable, AA\n"
    )

def test_isort_enables_deduping_section_headers_issue_953():
    """isort should provide a way to only have identical import headings show up once.
    See: https://github.com/pycqa/isort/issues/953
    """
    isort_code = partial(
        isort.code,
        config=Config(
            import_heading_firstparty="Local imports.",
            import_heading_localfolder="Local imports.",
            dedup_headings=True,
            known_first_party=["isort"],
        ),
    )

    assert (
        isort_code("from . import something")
        == """# Local imports.
from . import something
"""
    )
    assert (
        isort_code(
            """from isort import y

from . import something"""
        )
        == """# Local imports.
from isort import y

from . import something
"""
    )
    assert isort_code("import os") == "import os\n"

def test_isort_doesnt_remove_as_imports_when_combine_star_issue_1380():
    """Test to ensure isort will not remove as imports along side other imports
    when requested to combine star imports together.
    See: https://github.com/PyCQA/isort/issues/1380
    """
    test_input = """
from a import a
from a import *
from a import b
from a import b as y
from a import c
"""
    assert (
        isort.code(
            test_input,
            combine_star=True,
        )
        == isort.code(test_input, combine_star=True, force_single_line=True)
        == isort.code(
            test_input,
            combine_star=True,
            force_single_line=True,
            combine_as_imports=True,
        )
        == """
from a import *
from a import b as y
"""
    )

def test_isort_support_custom_groups_above_stdlib_that_contain_stdlib_modules_issue_1407():
    """Test to ensure it is possible to declare custom groups above standard library that include
    modules from the standard library.
    See: https://github.com/PyCQA/isort/issues/1407
    """
    assert isort.check_code(
        """
from __future__ import annotations
from typing import *

from pathlib import Path
""",
        known_typing=["typing"],
        sections=["FUTURE", "TYPING", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"],
        no_lines_before=["TYPING"],
        show_diff=True,
    )

def test_isort_intelligently_places_noqa_comments_issue_1456():
    assert isort.check_code(
        """
from my.horribly.long.import.line.that.just.keeps.on.going.and.going.and.going import (  # noqa
    my_symbol,
)
""",
        force_single_line=True,
        show_diff=True,
        multi_line_output=3,
        include_trailing_comma=True,
        force_grid_wrap=0,
        use_parentheses=True,
        line_length=79,
    )

    assert isort.check_code(
        """
from my.horribly.long.import.line.that.just.keeps.on.going.and.going.and.going import (
    my_symbol,
)
""",
        force_single_line=True,
        show_diff=True,
        multi_line_output=3,
        include_trailing_comma=True,
        force_grid_wrap=0,
        use_parentheses=True,
        line_length=79,
    )

    assert isort.check_code(
        """
from my.horribly.long.import.line.that.just.keeps.on.going.and.going.and.going import (  # noqa
    my_symbol
)
""",
        force_single_line=True,
        use_parentheses=True,
        multi_line_output=3,
        line_length=79,
        show_diff=True,
    )

    assert isort.check_code(
        """
from my.horribly.long.import.line.that.just.keeps.on.going.and.going.and.going import (
    my_symbol
)
""",
        force_single_line=True,
        use_parentheses=True,
        multi_line_output=3,
        line_length=79,
        show_diff=True,
    )

    # see: https://github.com/PyCQA/isort/issues/1415
    assert isort.check_code(
        "from dials.test.algorithms.spot_prediction."
        "test_scan_static_reflection_predictor import (  # noqa: F401\n"
        "    data as static_test,\n)\n",
        profile="black",
        show_diff=True,
    )

def test_isort_respects_quiet_from_sort_file_api_see_1461(capsys, tmpdir):
    """Test to ensure isort respects the quiet API parameter when passed in via the API.
    See: https://github.com/PyCQA/isort/issues/1461.
    """
    settings_file = tmpdir.join(".isort.cfg")
    custom_settings_file = tmpdir.join(".custom.isort.cfg")
    tmp_file = tmpdir.join("file.py")
    tmp_file.write("import b\nimport a\n")
    isort.file(tmp_file)

    out, error = capsys.readouterr()
    assert not error
    assert "Fixing" in out

    # When passed in directly as a setting override
    tmp_file.write("import b\nimport a\n")
    isort.file(tmp_file, quiet=True)
    out, error = capsys.readouterr()
    assert not error
    assert not out

    # Present in an automatically loaded configuration file
    settings_file.write(
        """
[isort]
quiet = true
"""
    )
    tmp_file.write("import b\nimport a\n")
    isort.file(tmp_file)
    out, error = capsys.readouterr()
    assert not error
    assert not out

    # In a custom configuration file
    settings_file.write(
        """
[isort]
quiet = false
"""
    )
    custom_settings_file.write(
        """
[isort]
quiet = true
"""
    )
    tmp_file.write("import b\nimport a\n")
    isort.file(tmp_file, settings_file=str(custom_settings_file))
    out, error = capsys.readouterr()
    assert not error
    assert not out

    # Reused configuration object
    custom_config = Config(settings_file=str(custom_settings_file))
    isort.file(tmp_file, config=custom_config)
    out, error = capsys.readouterr()
    assert not error
    assert not out

def test_isort_should_warn_on_empty_custom_config_issue_1433(tmpdir):
    """Feedback should be provided when a user provides a custom settings file that has no
    discoverable configuration.
    See: https://github.com/PyCQA/isort/issues/1433
    """
    settings_file = tmpdir.join(".custom.cfg")
    settings_file.write(
        """
[settings]
quiet = true
"""
    )
    with pytest.warns(UserWarning):
        assert not Config(settings_file=str(settings_file)).quiet

    settings_file.write(
        """
[isort]
quiet = true
"""
    )
    with pytest.warns(None) as warning:  # type: ignore
        assert Config(settings_file=str(settings_file)).quiet
    assert not warning

def test_float_to_top_should_respect_existing_newlines_between_imports_issue_1502():
    """When a file has an existing top of file import block before code but after comments
    isort's float to top feature should respect the existing spacing between the top file comment
    and the import statements.
    See: https://github.com/PyCQA/isort/issues/1502
    """
    assert isort.check_code(
        """#!/bin/bash
'''My comment'''

import a

x = 1
""",
        float_to_top=True,
        show_diff=True,
    )
    assert isort.check_code(
        """#!/bin/bash
'''My comment'''

import a

x = 1
""",
        float_to_top=True,
        show_diff=True,
    )
    assert (
        isort.code(
            """#!/bin/bash
'''My comment'''

import a

x = 1
""",
            float_to_top=True,
            add_imports=["import b"],
        )
        == """#!/bin/bash
'''My comment'''

import a
import b

x = 1
"""
    )

    assert (
        isort.code(
            """#!/bin/bash
'''My comment'''

def my_function():
    pass

import a
""",
            float_to_top=True,
        )
        == """#!/bin/bash
'''My comment'''
import a

def my_function():
    pass
"""
    )

    assert (
        isort.code(
            """#!/bin/bash
'''My comment'''

def my_function():
    pass
""",
            add_imports=["import os"],
            float_to_top=True,
        )
        == """#!/bin/bash
'''My comment'''
import os

def my_function():
    pass
"""
    )

def test_api_to_allow_custom_diff_and_output_stream_1583(capsys, tmpdir):
    """isort should provide a way from the Python API to process an existing
    file and output to a stream the new version of that file, as well as a diff
    to a different stream.
    See: https://github.com/PyCQA/isort/issues/1583
    """
    tmp_file = tmpdir.join("file.py")
    tmp_file.write("import b\nimport a\n")

    isort_diff = StringIO()
    isort_output = StringIO()

    isort.file(tmp_file, show_diff=isort_diff, output=isort_output)

    _, error = capsys.readouterr()
    assert not error

    isort_diff.seek(0)
    isort_diff_content = isort_diff.read()
    assert "+import a" in isort_diff_content
    assert " import b" in isort_diff_content
    assert "-import a" in isort_diff_content

    isort_output.seek(0)
    assert isort_output.read().splitlines() == ["import a", "import b"]

    # should still work with no diff produced
    tmp_file2 = tmpdir.join("file2.py")
    tmp_file2.write("import a\nimport b\n")

    isort_diff2 = StringIO()
    isort_output2 = StringIO()

    isort.file(tmp_file2, show_diff=isort_diff2, output=isort_output2)

    _, error = capsys.readouterr()
    assert not error

    isort_diff2.seek(0)
    assert not isort_diff2.read()

def test_autofix_mixed_indent_imports_1575():
    """isort should automatically fix import statements that are sent in
    with incorrect mixed indentation.
    See: https://github.com/PyCQA/isort/issues/1575
    """
    assert (
        isort.code(
            """
import os
  import os
  """
        )
        == """
import os
"""
    )
    assert (
        isort.code(
            """
def one():
    import os
import os
    """
        )
        == """
def one():
    import os

import os
"""
    )
    assert (
        isort.code(
            """
import os
    import os
        import os
    import os
import os
"""
        )
        == """
import os
"""
    )

def test_indented_import_headings_issue_1604():
    """Test to ensure it is possible to toggle import headings on indented import sections
    See: https://github.com/PyCQA/isort/issues/1604
    """
    assert (
        isort.code(
            """
import numpy as np

def function():
    import numpy as np
""",
            import_heading_thirdparty="External imports",
        )
        == """
# External imports
import numpy as np

def function():
    # External imports
    import numpy as np
"""
    )
    assert (
        isort.code(
            """
import numpy as np

def function():
    import numpy as np
""",
            import_heading_thirdparty="External imports",
            indented_import_headings=False,
        )
        == """
# External imports
import numpy as np

def function():
    import numpy as np
"""
    )

def test_isort_auto_detects_and_ignores_invalid_from_imports_issue_1688():
    """isort should automatically detect and ignore incorrectly written from import statements
    see: https://github.com/PyCQA/isort/issues/1688
    """
    assert (
        isort.code(
            """
from package1 import alright
from package2 imprt and_its_gone
from package3 import also_ok
"""
        )
        == """
from package1 import alright

from package2 imprt and_its_gone
from package3 import also_ok
"""
    )

def test_isort_allows_reversing_sort_order_issue_1645():
    """isort allows reversing the sort order for those who prefer Z or longer imports first.
    see: https://github.com/PyCQA/isort/issues/1688
    """
    assert (
        isort.code(
            """
from xxx import (
    g,
    hi,
    def,
    abcd,
)
""",
            profile="black",
            reverse_sort=True,
            length_sort=True,
            line_length=20,
        )
        == """
from xxx import (
    abcd,
    def,
    hi,
    g,
)
"""
    )

def test_isort_can_push_star_imports_above_others_issue_1504():
    """isort should provide a way to push star imports above other imports to avoid explicit
    imports from being overwritten.
    see: https://github.com/PyCQA/isort/issues/1504
    """
    assert (
        (
            isort.code(
                """
from ._bar import Any, All, Not
from ._foo import a, *
""",
                star_first=True,
            )
        )
        == """
from ._foo import *
from ._foo import a
from ._bar import All, Any, Not
"""
    )

def test_isort_can_combine_reverse_sort_with_force_sort_within_sections_issue_1726():
    """isort should support reversing import order even with force sort within sections turned on.
    See: https://github.com/PyCQA/isort/issues/1726
    """
    assert (
        isort.code(
            """
import blaaa
from bl4aaaaaaaaaaaaaaaa import r
import blaaaaaaaaaaaa
import bla
import blaaaaaaa
from bl1aaaaaaaaaaaaaa import this_is_1
from bl2aaaaaaa import THIIIIIIIIIIIISS_is_2
from bl3aaaaaa import less
""",
            length_sort=True,
            reverse_sort=True,
            force_sort_within_sections=True,
        )
        == """
from bl2aaaaaaa import THIIIIIIIIIIIISS_is_2
from bl1aaaaaaaaaaaaaa import this_is_1
from bl4aaaaaaaaaaaaaaaa import r
from bl3aaaaaa import less
import blaaaaaaaaaaaa
import blaaaaaaa
import blaaa
import bla
"""
    )

def test_isort_can_turn_off_import_adds_with_action_comment_issue_1737():
    assert (
        isort.code(
            """
import os
""",
            add_imports=[
                "from __future__ import absolute_imports",
                "from __future__ import annotations",
            ],
        )
        == """
from __future__ import absolute_imports, annotations

import os
"""
    )

    assert isort.check_code(
        """
# isort: dont-add-imports
import os
""",
        show_diff=True,
        add_imports=[
            "from __future__ import absolute_imports",
            "from __future__ import annotations",
        ],
    )

    assert (
        isort.code(
            """
# isort: dont-add-import: from __future__ import annotations
import os
""",
            add_imports=[
                "from __future__ import absolute_imports",
                "from __future__ import annotations",
            ],
        )
        == """
# isort: dont-add-import: from __future__ import annotations
from __future__ import absolute_imports

import os
"""
    )

def test_sort_configurable_sort_issue_1732() -> None:
    """Test support for pluggable isort sort functions."""
    test_input = (
        "from bob2.apples2 import aardvark as aardvark2\n"
        "from bob.apples import aardvark \n"
        "import module9\n"
        "import module10\n"
        "import module200\n"
    )
    assert isort.code(test_input, sort_order="native") == (
        "import module10\n"
        "import module200\n"
        "import module9\n"
        "from bob.apples import aardvark\n"
        "from bob2.apples2 import aardvark as aardvark2\n"
    )
    assert (
        isort.code(test_input, sort_order="natural")
        == isort.code(test_input)
        == (
            "import module9\n"
            "import module10\n"
            "import module200\n"
            "from bob2.apples2 import aardvark as aardvark2\n"
            "from bob.apples import aardvark\n"
        )
    )
    assert (
        isort.code(test_input, sort_order="natural_plus")
        == isort.code(test_input)
        == (
            "import module9\n"
            "import module10\n"
            "import module200\n"
            "from bob2.apples2 import aardvark as aardvark2\n"
            "from bob.apples import aardvark\n"
        )
    )
    with pytest.raises(exceptions.SortingFunctionDoesNotExist):
        isort.code(test_input, sort_order="round")

def test_cython_pure_python_imports_2062():
    """Test to ensure an import form a cython.cimports remains import, not cimport.
    See: https://github.com/pycqa/isort/issues/2062.
    """
    assert isort.check_code(
        """
import cython
from cython.cimports.libc import math

def use_libc_math():
    return math.ceil(5.5)
""",
        show_diff=True,
    )


import os
import re
import psutil
from time import process_time
import csv

# Start time
startTime = process_time()

#variable for unique list of words that was replace
uniqueWord = []
#variable for no. of times a word replace
wordFrequency = []

def createRequiredWordDictionary(enWord, frWord):
    """function to create french dictionary for only find words """
    dictionary_dict = {}
    if(enWord == frWord.split(",")[0]):
        dictionary_dict[enWord] = frWord.split(",")[1]
    return dictionary_dict

def readFile(file):
    """ read convert english words text file """
    findWordFile = open(file, "r")
    enlines = findWordFile.read()
    listOfLines = enlines.splitlines()
    return listOfLines

def freqency(content,findword):
    """Function used to find frequency of a word in given text"""
    freqList={}
    for key in findword:
        if content.count(key):
            freqList[key]= content.count(key)
            uniqueWord.append(key)
    return freqList

# read  content text
file = open("t8.shakespeare.txt", "r")
content = file.read()
#read find words file
listOfenLines=readFile("find_words.txt")
#read french dictionary file
listOffrLines=readFile("french_dictionary.csv")

findWordDict={}
for sub in list(map(createRequiredWordDictionary, listOfenLines, listOffrLines)):
    for key in sub:
        findWordDict[key]=sub[key]

wordFreq=freqency(content,listOfenLines)

for key,value in wordFreq.items():
    replacetxt=re.sub(key, findWordDict[key], content)
    content=replacetxt
    wordFrequency.append([key,findWordDict[key],value])

# write  new created text file after change english into french words
file_handle = open("t8.shakespeare.translate.txt", "w")
file_handle.write(content)
file_handle.close()

# Stop the time
stopTime = process_time()

print(uniqueWord)

# print(wordFrequency)

timeToProcress=stopTime-startTime

memoryUsed=psutil.Process(os.getpid()).memory_info().rss / 1024 ** 2

# save time to process and memory into performance.txt
file_handle = open("performance.txt", "w")
file_handle.write("Time to process: "+str(timeToProcress)+" seconds\nMemory used: "+str(memoryUsed)+" MB")
file_handle.close()

#Create csv file for store english word with its french word and also frequency count
with open('frequency.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(["English word", "French word", "Frequency"])
    for element in wordFrequency:
        writer.writerow(element)


#!/usr/bin/env/python3

import logging
import argparse
import time
import queue
import json
from datetime import datetime

from cellscan.panel import PanelThread
from cellscan.radio import RadioThread
from cellscan.gnss import GnssThread
from cellscan.data import saveCellSite, db, Cellsite, Location
from cellscan.upload import UploadThread

def __main__():
    # Note: The Telit modem in use (LE910C1) exposes multiple USB serial devices for different purposes,
    # and the composition is configurable (see the LE9xx AT reference manual). Here is what seems to be
    # the case for the default configuration:
    # ttyUSB0 (???)  ttyUSB1 (NMEA)  ttyUSB2 (???)  ttyUSB3 (Hayes AT)  ttyUSB4 (???)
    # TODO: load config in some smart way. Possibly derive ID from SIM card and server from
    # DNS.
    config = {
        'ATtty': '/dev/ttyUSB3',
        'NMEAtty': '/dev/ttyUSB1',
        'server': '104.225.250.115',
        'id': 'FFT-01'
    }
    runner = Runner(config)
    runner.setup()
    runner.uploadData()
    while True:
        runner.step()

class Runner(object):
    def __init__(self, config):
        parser = argparse.ArgumentParser(description='CellScan service.')
        parser.add_argument('-l', '--log', dest='logLevel', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], default='INFO', help='Set the logging level')
        self.args = parser.parse_args()

        logging.basicConfig(level=getattr(logging, self.args.logLevel))
        self.log = logging.getLogger("cellscan")

        self.locn = None
        self.q = queue.Queue()

        self.radio = None
        self.gnss = None
        self.panel = None
        self.upload = None
        self.config = config

        self.radioShouldBeRunning = False

        db.connect()
        db.create_tables([Cellsite, Location])

    def setup(self):
        self.panel = PanelThread(self.q)
        self.panel.start()
        self.gnss = GnssThread(self.q, self.config['NMEAtty'])
        self.gnss.start()
        # we DON'T start the radio thread here because it'll be started when the upload finishes.

    def step(self):
        event = self.q.get(block=True)
        self.log.debug(f"Received {event[0]}: {event[1]}")

        # Handle events which can occur
        if event[0] == 'LocationFix':
            self.handleLocationFix(event)
        elif event[0] == "NetworkData":
            self.handleNetworkData(event)
        elif event[0] == "PanelEvent":
            self.handlePanelEvent(event)
        elif event[0] == "UploadComplete":
            self.handleUploadComplete(event)

    def handleLocationFix(self, event):
            # Stash the new most recent fix for future use
            self.locn = event[1]

    def handleNetworkData(self, event):
            # New network scan result
            now = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
            if self.locn != None:
                # Slightly dumb check to prevent setting LED back to on if the radio thread is
                # shutting down.
                if self.radioShouldBeRunning:
                    self.panel.setLed('on')

                sites = event[1]
                for bsn in sites:
                    bsn['lat'] = self.locn['lat']
                    bsn['lon'] = self.locn['lon']
                    bsn['alt'] = self.locn['alt']
                    bsn['time'] = now
                    saveCellSite(bsn)

    def handlePanelEvent(self, event):
            if event[1]['type'] == 'CtlButton' and event[1]['time'] < 1:
                # User pressed a button
                self.uploadData()

    def handleUploadComplete(self, event):
        self.log.info("Upload complete. Waiting for modem to reset then returning to normal mode.")
        self.radioShouldBeRunning = True
        # Reset the location because the upload process often causes GPS fix to drop
        self.locn = None
        self.panel.setLed("off")
        time.sleep(5) # Needed to avoid race condition around inhibiting
        self.radio = RadioThread(self.q, self.config['ATtty'])
        self.radio.start()

    def uploadData(self):
        # Stop the scanning, this will block until it's closed out
        self.log.info("Starting data upload")
        self.panel.setLed("blink")
        self.radioShouldBeRunning = False

        if self.radio != None and self.radio.is_alive():
            self.log.debug("Asking scanner to stop")
            self.radio.stop()
            self.radio.join()
            self.log.debug("Scanner stopped, waiting for modem to uninhibit")
            time.sleep(5) # Needed to avoid race condition around inhibiting

        upload = UploadThread(self.q, self.config['server'], self.config['id'])
        upload.run()

if __name__ == "__main__":
    __main__()


#!/usr/bin/env python2

import json
import math
import re
import sys

def entropy(seq):
    bins = {}
    total = 0
    for x in seq:
        bins[x] = 1 + bins.get(x,0)
        total += 1

    return 0 - sum((float(p) / total) * math.log(float(p) / total, 2) for p in bins.itervalues())

if __name__ == "__main__":
  bins = {}
  for line in sys.stdin:
    try:
        tweet = json.loads(line)
        e = entropy(tweet['text'])
        if e > 6.6:
            sys.stdout.write(line)

    except KeyError:
        continue
        # text was missing
    except ValueError:
        # json decoding failed ?
        continue


"""
Kártyák definíciója
"""
from enum import IntEnum, unique
from functools import total_ordering

@unique
class Szinek(IntEnum):
    """
    Francia kártya színek
    """
    KARO = 0
    PIKK = 1
    KOR = 2
    TREFF = 3
    JOKER = 4

    def __str__(self):
        """
        Stringgé alakítja az értéket.
        :return: Szín Unicode karakterként
        """
        return ['\u2662', '\u2660', '\u2661', '\u2663', '*'][self.value]

# Default konstruktor átdefiniálása
# https://stackoverflow.com/questions/24105268/is-it-possible-to-override-new-in-an-enum-to-parse-strings-to-an-instance
def _szinek_konstruktor(cls, value):
    if not isinstance(value, str):
        # forward call to 'Szinek' superclass (enum.Enum)
        return super(Szinek, cls).__new__(cls, value)
    else:
        # map strings to enum values, default to Unknown
        return {'\u2662': Szinek.KARO,
                '\u2660': Szinek.PIKK,
                '\u2661': Szinek.KOR,
                '\u2663': Szinek.TREFF,
                'D': Szinek.KARO,
                'C': Szinek.PIKK,
                'H': Szinek.KOR,
                'S': Szinek.TREFF,
                '*': Szinek.JOKER}.get(value)

setattr(Szinek, '__new__', _szinek_konstruktor)

@unique
class Ertekek(IntEnum):
    """
    Francia kártya értékek
    """
    C2 = 0  # 2-es lap ....
    C3 = 1
    C4 = 2
    C5 = 3
    C6 = 4
    C7 = 5  # 7-es lap
    C8 = 6
    C9 = 7
    C0 = 8  # 10
    CJ = 9  # Jumi
    CQ = 10  # Dáma
    CK = 11  # Király
    CA = 12  # Ász
    CO = 13  # Joker

    def __str__(self):
        """
        Stringgé alakítja az értéket.
        :return: Érték Unicode karakterként
        """
        return '234567890JQKA*'[self.value]

def _ertekek_konstruktor(cls, value):
    if not isinstance(value, str):
        # forward call to 'Szinek' superclass (enum.Enum)
        return super(Ertekek, cls).__new__(cls, value)
    else:
        # map strings to enum values, default to Unknown
        ertek_dict = {'234567890JQKA*'[k]: Ertekek(k) for k in range(14)}
        ertek_dict['O'] = Ertekek.CO
        return ertek_dict.get(value)

setattr(Ertekek, '__new__', _ertekek_konstruktor)

@total_ordering
class MocsarCard(object):
    """
    Kártya lapokat reprezentáló osztály
    """
    suit: Szinek
    rank: Ertekek  # Ez az effektív érték, Kinevezett Joker esetén Joker pl., Körönként változhat
    displ_rank: Ertekek  # Ez a kártya lap megjelenítendő értéke, nem változik
    ACTION_PASS = 0  # Pass akció

    def __init__(self, krty):
        """
        Egy kártya lapot példányosít
        :param krty: Vagy egy kettő hosszú str ('♣3'), vagy egy két tagú tuple (0,8) Szín, Érték
        """
        # Stringből vagy tuple-ből konstruálunk, ugyanaz!
        self.suit = Szinek(krty[0])
        self.rank = Ertekek(krty[1])
        self.displ_rank = self.rank

    def __str__(self) -> str:
        return self.suit.__str__() + self.displ_rank.__str__()

    def __repr__(self):
        return "Card(Suit:{}, Rank:{}, Displ:{})".format(self.suit, self.rank, self.displ_rank)

    def __eq__(self, other):
        if type(other) is type(self):
            return self.rank == other.rank
        return False

    def __lt__(self, other):
        if type(other) is type(self):
            return self.rank < other.rank
        return NotImplemented

    def map_joker(self, ertek: Ertekek):
        """
        Joker átnevezést végez, Ha törölni akarjuk, akkor Ertekek.CO hívás kell
        :param ertek: ez a kártya lap lesz még Joker
        :type ertek: Ertekek
        """
        if self.displ_rank == ertek:
            # Kell Joker átnevezés, pont ez egy Joker
            self.rank = Ertekek.CO
        else:
            self.rank = self.displ_rank
        return self

    def get_index(self):
        """ Get index of a card.

        Returns:
            string: the combination of suit and rank of a card. Eg: 1S, 2H, AD, BJ, RJ...
        """
        return self.__str__()