import irc3

from random import shuffle
from datetime import datetime, timedelta

from . import utils

from .config import TimerStrategy
from .moderator import ModerationDecision, Moderator, FloodModerator
from . import twitch_message

config = None

@irc3.plugin
class TwitchBot:
    def __init__(self, bot: irc3.IrcBot):
        self.config = config
        self.messages_stack = []
        self.bot = bot
        self.log = self.bot.log
        self.last_timer_date = datetime.now()
        self.nb_messages_since_timer = 0

    def connection_made(self):
        print('connected')

    def server_ready(self):
        print('ready')

    def connection_lost(self):
        print('connection lost')

    @staticmethod
    def _parse_variables(in_str: str, **kwargs):
        for key in kwargs:
            value = kwargs[key]
            in_str = in_str.replace('{%s}' % key, value)

        return in_str

    # @irc3.event(r'^(?P<data>.+)$')
    # def on_all(self, data):
    #     print(data)

    @irc3.event(irc3.rfc.PRIVMSG)
    def on_msg(self, mask: str = None, target: str = None, data: str = None, tags: str = None, **_):
        author = mask.split('!')[0]
        command = self.config.find_command(data.lower().split(' ')[0])
        tags_dict = utils.parse_tags(tags)

        if command is not None:
            print('%s: %s%s' % (author, self.config.command_prefix, command.name))
            self.bot.privmsg(target, self._parse_variables(command.message, author=author))
        elif tags_dict.get('mod') == '0':
            self.moderate(tags_dict, data, author, target)

        self.nb_messages_since_timer += 1
        self.play_timer()

    @irc3.event(twitch_message.USERNOTICE)
    def on_user_notice(self, tags: str, **_):
        tags = utils.parse_tags(tags)
        if tags.get('msg-id', None) == 'raid':
            # Notice the Flood moderator a raid has just happened
            for moderator in self.config.moderators:
                if isinstance(moderator, FloodModerator) and moderator.raid_cooldown is not None:
                    print("Raid received from %s. Disabling the Flood moderator." % tags.get('display-name'))
                    moderator.declare_raid()
                    break

    def play_timer(self):
        if not self.messages_stack:
            print('Filling the timer messages stack in')
            self.messages_stack = self.config.timer.pool.copy()
            if self.config.timer.strategy == TimerStrategy.SHUFFLE:
                print('Shuffle!')
                shuffle(self.messages_stack)

        if self.nb_messages_since_timer < self.config.timer.msgs_between or \
            datetime.now() < self.last_timer_date + timedelta(minutes=self.config.timer.time_between):
            return

        command = self.messages_stack.pop(0)

        print("Timer: %s" % command.message)
        self.bot.privmsg('#%s' % self.config.channel, command.message)

        self.nb_messages_since_timer = 0
        self.last_timer_date = datetime.now()

    def moderate(self, tags: {str: str}, msg: str, author: str, channel: str):
        def delete_msg(mod: Moderator):
            print("[DELETE (reason: %s)] %s: %s" % (mod.get_name(), author, msg))
            self.bot.privmsg(
                channel,
                "/delete %s" % tags['id']
            )

        def timeout(mod: Moderator):
            print("[TIMEOUT (reason: %s)] %s: %s" % (mod.get_name(), author, msg))
            self.bot.privmsg(
                channel,
                "/timeout %s %d %s" % (
                    author,
                    mod.timeout_duration,
                    self._parse_variables(mod.message, author=author)
                )
            )

        # Ignore emotes-only messages
        if tags.get('emote-only', '0') == '1':
            return

        message_to_moderate = msg

        # Remove emotes from message before moderating
        for emote in tags.get('emotes', '').split('/'):
            if emote == '':
                break

            for indices in emote.split(':')[1].split(','):
                [first, last] = indices.split('-')
                first, last = int(first), int(last)
                if first == 0:
                    message_to_moderate = message_to_moderate[last + 1:]
                else:
                    message_to_moderate = message_to_moderate[:first - 1] + message_to_moderate[last + 1:]

        for moderator in self.config.moderators:
            vote = moderator.vote(message_to_moderate, author)
            if vote == ModerationDecision.ABSTAIN:
                continue
            if vote == ModerationDecision.DELETE_MSG:
                delete_msg(moderator)
            if vote == ModerationDecision.TIMEOUT_USER:
                timeout(moderator)

            self.bot.privmsg(channel, self._parse_variables(moderator.message, author=author))
            break

    @irc3.event(irc3.rfc.JOIN)
    def on_join(self, mask, channel, **_):
        print('JOINED %s as %s' % (channel, mask))

    @irc3.event(irc3.rfc.CONNECTED)
    def on_connected(self, **_):
        for line in [
            "CAP REQ :twitch.tv/commands",
            "CAP REQ :twitch.tv/tags"
        ]:
            self.bot.send_line(line)

        self.bot.join('#%s' % self.config.channel)


import datetime
import sys, os
import math
import numpy as np
import pandas as pd
import networkx as nx # ネットワークグラフ
import matplotlib.pyplot as plt # ネットワークグラフ描画
import pdb

class GraphAnalysis:
    """ 2つのグラフ（隣接行列）を比較し、類似度を判定する機能を持つクラス """
    graphs1: list # グラフ（隣接行列）のリスト
    graphs2: list # グラフ（隣接行列）のリスト
    graph_type: str # エッジの分布の過程

    def __init__(self, graphs1, graphs2, graph_type):
        self.graphs1 = graphs1
        self.graphs2 = graphs2
        self.graph_type = graph_type
        return

    def measure_similarity(self, focused_index):
        """ グラフの非類似度をスコア化する
            focused_index: 隣接行列の何番目に注目するか（グラフから注目ノードから到達できる部分グラフを探索し比較するために必要）"""
        # 隣接行列から注目ノードのベクトルのリストを作成する
        x0_list = self._extract_column(self.graphs1 + self.graphs2, focused_index)
        param0 = self._estimate_params(x0_list)
        x1_list = self._extract_column(self.graphs1, focused_index)
        param1 = self._estimate_params(x1_list)
        x2_list = self._extract_column(self.graphs2, focused_index)
        param2 = self._estimate_params(x2_list)
        dissimilarity = self._caluculate_dissimilarity(x1_list, x2_list, param0, param1, param2)
        return dissimilarity

    def _extract_column(self, graphs, index):
        ret = []
        for graph in graphs:
            ret.append(graph[:, index]) # 1次元になる
        return ret

    def _estimate_params(self, x_list):
        """ 最尤推定を行い，エッジ確率を求める """
        mu = np.zeros(len(x_list[0])) # 各ノードごとにエッジ確率を求める
        for x in x_list: # リストごとに
            mu += x
        mu /= len(x_list)
        return mu

    def _caluculate_dissimilarity(self, x1_list, x2_list, param0, param1, param2):
        """ パターン（部分グラフ）からグラフ間の非類似度を算出する（算出式については確定ではなく、変更時にはこのメソッド内を変更するとする）
            x1_list, x2_list: list  ベクトルの系列
            param0, param1, param2: np.array    パラメータ（各エッジの生起確率をベクトル化したもの） """
        log_K = 0
        for x in x1_list:
            log_K += self._calculate_log_prob(x, param1) - self._calculate_log_prob(x, param0)
        for x in x2_list:
            log_K += self._calculate_log_prob(x, param2) - self._calculate_log_prob(x, param0)
        return log_K # expにすると数が大きくなりすぎてスコアが分かりにくくなる（論文でもlogのままだった）

    def _calculate_log_prob(self, x, param):
        """ 1時刻分の生起確率の対数を計算する (Bernoulli and Poisson)
            Parameter
            x: np.array ベクトル（隣接行列のi列目を1次元にしたものになる）
            param: np.array ベクトル（xの生起確率）"""
        log_p = 0
        if (self.graph_type == "Bernoulli"):
            for x_j, mu_j in zip(x, param):
                log_p += np.log((mu_j ** x_j) * ((1 - mu_j) ** (1 - x_j))) # ベルヌーイ
        elif (self.graph_type == "Poisson"):
            for x_j, lam_j in zip(x, param):
                try:
                    if (lam_j > 0):
                        log_p += x_j * np.log(lam_j) - self._log_factorial(int(x_j)) + (-1 * lam_j) # ポアソン
                    else:
                        log_p += 1 # パラメータが0の場合，重みがつく確率は0，すなわちx=0となる確率は1
                except:
                    pdb.set_trace()
        return log_p

    def _log_factorial(self, x):
        """ log(math.factorial(x))を計算する（オーバーフロー対策） """
        if (x == 0):
            return 1 # 0の階乗は1
        else:
            return sum(map(lambda y: math.log(y), range(1,x+1))) # logをとってから足し合わせる = log(x!)

    def visualize_graph(self, graph, focused_index, label_list, save_path, filename):
        cut_graph1 = self._cut_graph(graph, focused_index)
        color_list = [1 if graph[focused_index, j] > 0 else 0 for j in range(len(label_list))]
        self._visualize_graph(cut_graph1, label_list, color_list, save_path, filename, weighted=True)
        return

    def _cut_graph(self, graph, focused_index):
        """ 注目ノードの列のみを残してグラフを切り出す（他のノードの重みを0に置き換える） """
        ret_graph = np.zeros((len(graph), len(graph.T)))
        for i in range(len(graph)):
            for j in range(len(graph)):
                if (i == focused_index or j == focused_index):
                    ret_graph[i,j] = graph[i,j]
        return ret_graph

    def _visualize_graph(self, graph, label_list, color_list:list, save_path:str, filename:str, weighted=False):
        """ インタラクショングラフを描画する
            graph:  np.array
            label_list: list    ラベルに付与するリスト
            color_list: list    ノード数分の長さのリスト．node_colorsのインデックスの数字が入る
            save_path: str      セーブするファイルのパス．この中ではパスが通るかの確認はしない
            filename: str       セーブするファイルの名前
            weighted: bool      重み付きを表示するかどうか """
        g = self._create_nxgraph(label_list, graph)
        num_nodes = len(g.nodes)
        node_colors = [(1,0,0,0.0), (1,0,0,1.0), (1,0,0,0.5), (1,0,0,0.4), (1,0,0,0.3), (1,0,0,0.2), (1,0,0,0.15), (1,0,0,0.1), (1,0,0,0.05),(1,0,0,0.0)]
        color_list = [node_colors[int(i%len(node_colors))] for i in color_list]
        plt.figure(figsize=(19.2, 9.67))
        # 円状にノードを配置
        pos = {
            n: (np.cos(2*i*np.pi/num_nodes), np.sin(2*i*np.pi/num_nodes))
            for i, n in enumerate(g.nodes)
        }
        #ノードとエッジの描画
        nx.draw_networkx_edges(g, pos, edge_color='y')
        nx.draw_networkx_nodes(g, pos, node_color=color_list) # alpha: 透明度の指定
        nx.draw_networkx_labels(g, pos, font_size=10) #ノード名を付加
        if (weighted):
            edge_labels = {(i, j): w['weight'] for i, j, w in g.edges(data=True)}
            nx.draw_networkx_edge_labels(g, pos, edge_labels=edge_labels)
        plt.axis('off') #X軸Y軸を表示しない設定
        plt.savefig(save_path + filename)
        plt.close()
        return

    def _create_nxgraph(self, label_list, matrix):
        """ 隣接行列から無向グラフを作成する
            label_list: list    ノードに付与するラベル
            matrix: np.array     隣接行列のグラフ（2次元行列で表現） """
        graph = nx.Graph()
        edges = []
        # ノードメンバを登録
        for i, label1 in enumerate(label_list):
            graph.add_node(label1)
            for j, label2 in enumerate(label_list):
                if (matrix[i,j] > 0):
                    edges.append((label1, label2, math.floor(matrix[i,j] * 100) / 100))
        # エッジを追加
        graph.add_weighted_edges_from(edges)
        return graph

if __name__ == "__main__":
    graph1 = np.array([[0, 1, 1, 1], [1, 0, 1, 0], [1, 1, 0, 1], [1, 0, 1, 0]])
    graph2 = np.array([[0, 1, 1, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 1, 1], [0, 0, 1, 0, 1], [0, 0, 1, 1, 0]])
    graph3 = np.array([[0, 1, 1, 0, 0, 1], [1, 0, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1], [0, 1, 1, 0, 0, 0], [1, 0, 0, 1, 0, 0]])
    graph4 = np.array([[0, 1, 1, 0, 0, 0], [1, 0, 0, 1, 1, 0], [1, 0, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1], [0, 1, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0]])
    ga = GraphAnalysis(graph3, graph4, "Bernoulli")
    # distance =ga.measure_similarity(graph, 0)
    # print("distance: ", distance)
    ga.visualize_graph(graph3, 0, ["10", "20", "30", "40", "50", "60"], "./", "graph.png")


import openpyxl
from openpyxl import Workbook, load_workbook
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["GRE"]
source = mydb["grevocabs"]

# path = "./apparels_maininfo.xlsx"
path = "./GRE_words.xlsx"

wb_obj = openpyxl.load_workbook(path)

sheet_obj = wb_obj.active

inserter = {}

p = 0

count = 0

# for x in source.find():

#     # print(x["page_color_link"])

#     # print(count)

#     # commented code

#     buffer = []

for dress in range(3, (sheet_obj.max_row)+1):
    m = sheet_obj.cell(row=dress, column=1)
    print(m.value)

    inserter = {}

    word = str(m.value)

    vocab, typeofvocab = word.split("(")

    # print(vocab.strip(" "), len(vocab.strip(" ")))

    # print(word.split("("))

    # print(typeofvocab[:-2])

    inserter["wordname"] = vocab

    inserter["typeofvocab"] = typeofvocab.split(")")[0]

    meaning = sheet_obj.cell(row=dress, column=2)
    inserter["word_meaning"] = meaning.value

    mnemonic = sheet_obj.cell(row=dress, column=3)
    inserter["word_mnemonic"] = mnemonic.value

    word_example = sheet_obj.cell(row=dress, column=4)
    inserter["word_example"] = word_example.value

    inserter["origin_of_word"] = "Magoosh_GRE_High_frequency_list"
    source.insert_one(inserter)

    print(inserter)

# print("ok")


import os
import numpy as np
import pandas as pd
import re
from datetime import datetime as dt
import pytz
import time
import json

from django.shortcuts import render, HttpResponse, redirect
from django.http import JsonResponse
from django.contrib.contenttypes.models import ContentType

from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import permission_required
from django.views.decorators.csrf import csrf_protect, csrf_exempt

from bowlero_backend.settings import TIME_ZONE, BASE_DIR
from .models.models import *

from DAO.DataDAO import *
from DAO.DataReviseDAO import *

from utils.UString import UString
from utils.UDatetime import UDatetime

# Create your views here.
# from .utils.LoadConfigData import LoadConfigData

EST = pytz.timezone(TIME_ZONE)

@staff_member_required(login_url='/login/')
@csrf_protect
def index(request, *args, **kwargs):
    template_name = 'Bundle/BundleIndex.html'
    return render(request, template_name)

#this class isn't being used in Bundles
class Panel1:
    class Form1:

        # result_path = os.path.join(BASE_DIR, 'media/RM/Centers/centers.xlsx')

        @staticmethod
        def submit(request, *args, **kwargs):
            action = request.GET.get('action')
            date = request.GET.get('date')
            price_type = request.GET.get('price_type')
            categories = request.GET.get('categories')
            categories = categories.split(',')
            categories = [int(category) for category in categories if category]

            # data init
            if not date:
                date = UDatetime.now_local().date()
            if not price_type:
                price_type = 'regular'

            if action == 'bulk pricing':
                if not categories:
                    return JsonResponse({'status': 1, 'msg': ''})
                price_symbol = request.GET.get('price-symbol')
                price = request.GET.get('price')
                price_unit = request.GET.get('price-unit')
                start = request.GET.get('start')
                end = request.GET.get('end')
                tiers = request.GET.getlist('tiers')
                if not tiers:
                    tiers = get_tiers(date, price_type)
                if start:
                    start = UDatetime.datetime_str_init(start).date()
                else:
                    start = None
                if end:
                    end = UDatetime.datetime_str_init(end).date()
                else:
                    end = None

                price = [
                    {
                     'price_symbol': price_symbol,
                     'price_delta': UString.str2float(price),
                     'price_unit': price_unit,
                    }
                ]

                AlcoholReviseDAO.bulk_pricing(start, end, price, date, price_type, categories, tiers, request.user,
                                              tracking_id=None)

            return JsonResponse({'status': 1, 'msg': ''})

        @classmethod
        def get_food_menu(cls, request, *args, **kwargs):

            menu_list = Menu.objects \
                .filter(status='active') \
                .values_list('name', flat=True)

            result = \
                [
                    {
                        "id": menu,
                        "text": menu
                    }
                    for menu in menu_list
                ]

            return JsonResponse({'status': 1, 'msg': '', 'results': result})

        @classmethod
        def fileupload(cls, request, *args, **kwargs):
            files = request.FILES.getlist('FileUpload')

            file_path = os.path.join(BASE_DIR, 'media/RM/Centers/centers.xlsx')

            if request.method == 'POST' and files:
                for file in files:
                    data = pd.read_excel(file)
                    data.to_excel(file_path)

            return JsonResponse({})

class Panel2:
    class Table1:

        @staticmethod
        def create_columns(request, *args, **kwargs):

            date = request.GET.get('date')
            price_type = request.GET.get('price_type')

            columns = [
                {
                    'field': 'bundle_id', 'title': 'Bundle id', 'sortable': True, 'editable': False,
                    'align': 'center', 'vlign': 'center', 'filter': {'type': 'input'}, 'visible': False
                },
                {
                    'field': 'bundle_name', 'title': 'Promotions / Package Name', 'sortable': True, 'editable': False,
                    'align': 'center', 'vlign': 'center', 'filter': {'type': 'input'}
                },
                {
                    'field': 'products', 'title': 'Package Components', 'sortable': True, 'editable': False,
                    'align': 'center', 'vlign': 'center', 'filter': {'type': 'input'}
                },
            ]

            return JsonResponse({'status': 1, 'msg': '', 'columns': columns})

        @staticmethod
        def create_details_columns(request, *args, **kwargs):

            date = request.GET.get('date')
            price_type = request.GET.get('price_type')

            columns = [
                {
                    'field': 'bundle_id', 'title': 'Bundle id', 'sortable': True, 'editable': False,
                    'align': 'center', 'vlign': 'center', 'filter': {'type': 'input'}, 'visible': False
                },
                {
                    'field': 'product_id', 'title': 'product id', 'sortable': True, 'editable': False,
                    'align': 'center', 'vlign': 'center', 'filter': {'type': 'input'}, 'visible': False
                },
                {
                    'field': 'product_num', 'title': 'Product Num', 'sortable': True, 'editable': False,
                    'align': 'center', 'vlign': 'center', 'filter': {'type': 'input'}
                },
                {
                    'field': 'product_name', 'title': 'Product Name', 'sortable': True, 'editable': False,
                    'align': 'center', 'vlign': 'center', 'filter': {'type': 'input'}
                },
            ]

            return JsonResponse({'status': 1, 'msg': '', 'columns': columns})

        @staticmethod
        def create(request, *args, **kwargs):
            page_size = int(request.GET.get('limit'))
            offset = int(request.GET.get('offset'))
            filters = request.GET.get('filter')
            sort = request.GET.get('sort')
            order = request.GET.get('order')

            data, num = BundleGet.get_bundle(
                                         pagination=True,
                                         page_size=page_size,
                                         offset=offset,
                                         filters=filters,
                                         sort=sort,
                                         order=order,
                                         )

            if not data.empty:
                data = data.where((pd.notnull(data)), "-")

                data_response = data.to_dict(orient='records')
                response = {
                    'total': num,
                    'rows': data_response
                }
            else:
                response = {
                    'total': 0,
                    'rows': []
                }
            return JsonResponse(response, safe=False)

        @staticmethod
        def create_details(request, *args, **kwargs):
            page_size = int(request.GET.get('limit'))
            offset = int(request.GET.get('offset'))
            filters = request.GET.get('filter')
            sort = request.GET.get('sort')
            order = request.GET.get('order')
            bundle_id = request.GET.get('bundle_id')

            data, num = BundleGet.get_bundle_detail(
                                         bundle_id,
                                         pagination=True,
                                         page_size=page_size,
                                         offset=offset,
                                         filters=filters,
                                         sort=sort,
                                         order=order,
                                         )

            if not data.empty:
                data = data.where((pd.notnull(data)), "-")

                data_response = data.to_dict(orient='records')
                response = {
                    'total': num,
                    'rows': data_response
                }
            else:
                response = {
                    'total': 0,
                    'rows': []
                }
            return JsonResponse(response, safe=False)

        @staticmethod
        def edit(request, *args, **kwargs):

            current_user = request.user
            category_id = request.GET.get('category_id')
            category = request.GET.get('category')
            level = request.GET.get('level')
            price_type = request.GET.get('price_type')
            date = request.GET.get('date')
            field = request.GET.get('field')
            new_value = request.GET.get(field)
            old_value = request.GET.get('old_value')

            if not date:
                date = UDatetime.now_local().date()
            if not price_type:
                price_type = 'regular'

            if field.isdigit():
                new_value = new_value.replace('$', '')
                old_value = old_value.replace('$', '')
                new_value = round(float(new_value), 2)
                old_value = round(float(old_value), 2)
                if new_value == '-':
                    new_value = None
                if old_value == '-':
                    old_value = None

                # Tracking
                # tracking_type = TrackingType.objects.get(type='retail food tier price change')
                tracking_type = None
                content_type = ContentType.objects.get_for_model(AlcoholTier)
                input_params = {'new_price': new_value, 'old_price': old_value, 'category': category, 'level': level,
                                'price_typ': price_type, 'start': str(date), 'tier': field}
                tracking_id = Tracking.objects.create(
                    username=current_user,
                    tracking_type=tracking_type,
                    content_type=content_type,
                    input_params=input_params
                )

                category_id = AlcoholCategory.objects.get(category_id=category_id)

                AlcoholTier.objects \
                    .update_or_create(
                        category_id=category_id,
                        price_type=price_type,
                        tier=field,
                        defaults={
                            'price': new_value,
                            'action_user': current_user,
                            'tracking_id': tracking_id
                        }
                    )

                # Tracking Change Report
                description = 'Change category "{category}" level "{level}" tier "{tier}" price ' \
                              'from "${price_old}" to "${price_new}"'\
                    .format(category=category, level=level, tier=field, price_old=old_value, price_new=new_value)

                AlcoholChangeReport.objects \
                    .update_or_create\
                        (
                            tracking_id=tracking_id,
                            username=current_user,
                            action_time=UDatetime.now_local(),
                            product_id=None,
                            menu=None,
                            category=category,
                            level=level,
                            tier=field,
                            description=description,
                            price_old=old_value,
                            price_new=new_value,
                            start=date,
                            end=None
                        )

            return JsonResponse({})


import matplotlib.pyplot as plt
import Code.faser as faser
import numpy as np

# Initialize
HIO = faser.Faser('example.ini')
beta = 1
ER = faser.Faser('example.ini')

# Plot
ax_HIO = plt.subplot(1,2,1)
img_HIO = plt.imshow(np.abs(HIO.g))
plt.title('HIO, n = %d' % (HIO.n))
ax_ER = plt.subplot(1,2,2)
img_ER = plt.imshow(np.abs(ER.g))
plt.title('ER, n = %d' % (ER.n))
plt.pause(1)
plt.draw()

# Run phasing
for ii in range(10):

    for jj in range(10):
        # Do one step
        HIO.HIO_one_iteration(beta)
        ER.ER_one_iteration()

    plt.sca(ax_HIO)
    img_HIO.set_data(np.abs(HIO.g)*HIO.support)
    plt.title('HIO, n = %d' % (HIO.n))
    plt.sca(ax_ER)
    img_ER.set_data(np.abs(ER.g)*ER.support)
    plt.title('ER, n = %d' % (ER.n))
    plt.pause(1)
    plt.draw()
    # Update support
    ER.UpdateSupport()
    HIO.UpdateSupport()

plt.show()


import time
from task_utility import *
import pyautogui

click_use()
time.sleep(0.3)

dim = get_dimensions()

skull_x = dim[0] + round(dim[2] / 4.025)
skull_y = dim[1] + round(dim[3] / 3.64)

skull_x2 = dim[0] + round(dim[2] / 2.26)
skull_y2 = dim[1] + round(dim[3] / 3.51)

crystal_x = dim[0] + round(dim[2] / 3.75)
crystal_y = dim[1] + round(dim[3] / 2.48)

crystal_x2 = dim[0] + round(dim[2] / 1.83)
crystal_y2 = dim[1] + round(dim[3] / 2.62)

leaf_x = dim[0] + round(dim[2] / 3.87)
leaf_y = dim[1] + round(dim[3] / 1.77)

leaf_x2 = dim[0] + round(dim[2] / 2.24)
leaf_y2 = dim[1] + round(dim[3] / 1.83)

gem_x = dim[0] + round(dim[2] / 3.95)
gem_y = dim[1] + round(dim[3] / 1.49)

gem_x2 = dim[0] + round(dim[2] / 1.83)
gem_y2 = dim[1] + round(dim[3] / 1.48)

pyautogui.moveTo(skull_x, skull_y)
pyautogui.dragTo(skull_x2, skull_y2, 0.8)

pyautogui.moveTo(crystal_x, crystal_y)
pyautogui.dragTo(crystal_x2, crystal_y2, 1)

pyautogui.moveTo(leaf_x, leaf_y)
pyautogui.dragTo(leaf_x2, leaf_y2, 0.8)

pyautogui.moveTo(gem_x, gem_y)
pyautogui.dragTo(gem_x2, gem_y2, 1)


import unittest
import os
import importlib

import sys
sys.path.append(".\\src")
sys.path.append(".\\tests")
sys.path.append(".\\tests\\reqs")

## test imports ----------------------------------------------------------------
from uofi_gui import GUIController
from uofi_gui.uiObjects import ExUIDevice
from uofi_gui.systemHardware import SystemHardwareController
from uofi_gui.deviceControl import AudioController, CameraController, DisplayController
from uofi_gui.sourceControls import Destination
import test_settings as settings

from extronlib.device import UIDevice
from extronlib.ui import Button, Label, Level, Slider
from extronlib.system import MESet, File
## -----------------------------------------------------------------------------

class CameraController_TestClass(unittest.TestCase): # rename for module to be tested
    def setUp(self) -> None:
        self.TestCtls = ['CTL001']
        self.TestTPs = ['TP001']
        importlib.reload(settings)
        self.TestGUIController = GUIController(settings, self.TestCtls, self.TestTPs)
        self.TestGUIController.Initialize()
        self.TestUIController = self.TestGUIController.TP_Main
        self.TestCamController = self.TestUIController.CamCtl
        return super().setUp()

    def tearDown(self):
        if os.path.exists('./tests/reqs/emFS/SFTP/user/states/camera_presets.json'):
            os.remove('./tests/reqs/emFS/SFTP/user/states/camera_presets.json')

    def test_CameraController_Init_BadCamHardwareMatch(self):
        importlib.reload(settings)
        settings.cameras.append({'Id':'CAM003', 'Name':'Bad Camera', 'Input': 3})
        with self.assertRaises(KeyError):
            BadGUIController = GUIController(settings, self.TestCtls, self.TestTPs)
            BadGUIController.Initialize()
        settings.cameras.pop()

    def test_CameraController_Type(self):
        self.assertIsInstance(self.TestCamController, CameraController)

    def test_CameraController_Properties(self):
        # UIHost
        with self.subTest(param='UIHost'):
            self.assertIsInstance(self.TestCamController.UIHost, (UIDevice, ExUIDevice))

        # GUIHost
        with self.subTest(param='GUIHost'):
            self.assertIsInstance(self.TestCamController.GUIHost, GUIController)

        # Cameras
        with self.subTest(param='Cameras'):
            self.assertIsInstance(self.TestCamController.Cameras, dict)
            for key, value in self.TestCamController.Cameras.items():
                with self.subTest(key=key, value=value):
                    self.assertIsInstance(key, str)
                    self.assertIsInstance(value, dict)

    def test_CameraController_PRIV_Properties(self):
        # __PresetsFilePath
        with self.subTest(param='__PresetsFilePath'):
            self.assertIsInstance(self.TestCamController._CameraController__PresetsFilePath, str)

        # __Switcher
        with self.subTest(param='__Switcher'):
            self.assertIsInstance(self.TestCamController._CameraController__Switcher, SystemHardwareController)

        # __DefaultCamera
        with self.subTest(param='__DefaultCamera'):
            self.assertIsInstance(self.TestCamController._CameraController__DefaultCamera, Button)

        # __SelectBtns
        with self.subTest(param='__SelectBtns'):
            self.assertIsInstance(self.TestCamController._CameraController__SelectBtns, MESet)

        # __PresetBtns
        with self.subTest(param='__PresetBtns'):
            self.assertIsInstance(self.TestCamController._CameraController__PresetBtns, list)
            for value in self.TestCamController._CameraController__PresetBtns:
                with self.subTest(value=value):
                    self.assertIsInstance(value, Button)

        # __HomeBtn
        with self.subTest(param='__HomeBtn'):
            self.assertIsInstance(self.TestCamController._CameraController__HomeBtn, Button)

        # __ControlsBtns
        with self.subTest(param='__ControlsBtns'):
            self.assertIsInstance(self.TestCamController._CameraController__ControlsBtns, list)
            for value in self.TestCamController._CameraController__ControlsBtns:
                with self.subTest(value=value):
                    self.assertIsInstance(value, Button)

        # __EditorTitle
        with self.subTest(param='__EditorTitle'):
            self.assertIsInstance(self.TestCamController._CameraController__EditorTitle, Label)

        # __EditorName
        with self.subTest(param='__EditorName'):
            self.assertIsInstance(self.TestCamController._CameraController__EditorName, Button)

        # __EditorHome
        with self.subTest(param='__EditorHome'):
            self.assertIsInstance(self.TestCamController._CameraController__EditorHome, Button)

        # __EditorSave
        with self.subTest(param='__EditorSave'):
            self.assertIsInstance(self.TestCamController._CameraController__EditorSave, Button)

        # __EditorCancel
        with self.subTest(param='__EditorCancel'):
            self.assertIsInstance(self.TestCamController._CameraController__EditorCancel, Button)

    def test_CameraController_EventHandler_CamSelectBtnHandler(self):
        btnList = self.TestCamController._CameraController__SelectBtns.Objects
        actList = ['Pressed', 'Released']

        for btn in btnList:
            for act in actList:
                with self.subTest(button=btn.Name, action=act):
                    try:
                        self.TestCamController._CameraController__CamSelectBtnHandler(btn, act)
                    except Exception as inst:
                        self.fail('__CamSelectBtnHandler raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_EventHandler_PresetBtnHandler(self):
        btnList = self.TestCamController._CameraController__PresetBtns
        actList = ['Pressed', 'Tapped', 'Held']

        for btn in btnList:
            for act in actList:
                with self.subTest(button=btn.Name, action=act):
                    try:
                        self.TestCamController._CameraController__PresetBtnHandler(btn, act)
                    except Exception as inst:
                        self.fail('__PresetBtnHandler raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_EventHandler_HomeBtnHandler(self):
        presetList = self.TestCamController._CameraController__PresetBtns
        btnList = [self.TestCamController._CameraController__HomeBtn]
        actList = ['Pressed', 'Tapped', 'Held']

        for pre in presetList:
            for btn in btnList:
                for act in actList:
                    with self.subTest(button=btn.Name, action=act, preset=pre):
                        self.TestCamController._CameraController__PresetBtnHandler(pre, 'Held')
                        try:
                            self.TestCamController._CameraController__HomeBtnHandler(btn, act)
                        except Exception as inst:
                            self.fail('__HomeBtnHandler raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_EventHandler_CamCtlHandler(self):
        btnList = self.TestCamController._CameraController__ControlsBtns
        actList = ['Pressed', 'Released']

        for btn in btnList:
            for act in actList:
                with self.subTest(button=btn.Name, action=act):
                    try:
                        self.TestCamController._CameraController__CamCtlHandler(btn, act)
                    except Exception as inst:
                        self.fail('__CamCtlHandler raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_Eventhandler_EditorNameHandler(self):
        presetList = self.TestCamController._CameraController__PresetBtns
        btnList = [self.TestCamController._CameraController__EditorName]
        actList = ['Pressed', 'Released']

        for pre in presetList:
            for btn in btnList:
                for act in actList:
                    with self.subTest(button=btn.Name, action=act, preset=pre.Name):
                        self.TestCamController._CameraController__PresetBtnHandler(pre, 'Held')
                        try:
                            self.TestCamController._CameraController__EditorNameHandler(btn, act)
                        except Exception as inst:
                            self.fail('__EditorNameHandler raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_EventHandler_EditorHomeHandler(self):
        presetList = self.TestCamController._CameraController__PresetBtns
        btnList = [self.TestCamController._CameraController__EditorHome]
        actList = ['Pressed', 'Released']

        for pre in presetList:
            for btn in btnList:
                for act in actList:
                    with self.subTest(button=btn.Name, action=act, preset=pre.Name):
                        self.TestCamController._CameraController__PresetBtnHandler(pre, 'Held')
                        try:
                            self.TestCamController._CameraController__EditorHomeHandler(btn, act)
                        except Exception as inst:
                            self.fail('__EditorHomeHandler raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_EventHandler_EditorSaveHandler(self):
        presetList = self.TestCamController._CameraController__PresetBtns
        btnList = [self.TestCamController._CameraController__EditorSave]
        actList = ['Pressed', 'Released']

        for pre in presetList:
            for btn in btnList:
                for act in actList:
                    with self.subTest(button=btn.Name, action=act, preset=pre.Name):
                        self.TestCamController._CameraController__PresetBtnHandler(pre, 'Held')
                        try:
                            self.TestCamController._CameraController__EditorSaveHandler(btn, act)
                        except Exception as inst:
                            self.fail('__EditorSaveHandler raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_EventHandler_EditorCancelHandler(self):
        presetList = self.TestCamController._CameraController__PresetBtns
        btnList = [self.TestCamController._CameraController__EditorCancel]
        actList = ['Pressed', 'Released']

        for pre in presetList:
            for btn in btnList:
                for act in actList:
                    with self.subTest(button=btn.Name, action=act, preset=pre.Name):
                        self.TestCamController._CameraController__PresetBtnHandler(pre, 'Held')
                        try:
                            self.TestCamController._CameraController__EditorCancelHandler(btn, act)
                        except Exception as inst:
                            self.fail('__EditorCancelHandler raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_SavePresetStates(self):
        import shutil

        with self.subTest(condition='Save - No Existing Presets'):
            if os.path.exists(self.TestCamController._CameraController__PresetsFilePath):
                os.remove(self.TestCamController._CameraController__PresetsFilePath)
            try:
                self.TestCamController.SavePresetStates()
            except Exception as inst:
                self.fail('SavePresetStates raised {} unexpectedly!'.format(type(inst)))

        with self.subTest(condition='Save - Existing Presets'):
            shutil.copyfile('./tests/reqs/emFS/SFTP/user/states/test_states/test_camera_presets_1.json', './tests/reqs/emFS/SFTP/user/states/test_camera_presets_write.json')
            self.TestCamController._CameraController__PresetsFilePath = '/user/states/test_camera_presets_write.json'
            try:
                self.TestCamController.SavePresetStates()
            except Exception as inst:
                self.fail("__SaveSchedule() raised {} unexpectedly!".format(type(inst)))
            os.remove('./tests/reqs/emFS/SFTP/user/states/test_camera_presets_write.json')

        with self.subTest(condition='Save - Some Existing Presets'):
            if os.path.exists('./tests/reqs/emFS/SFTP/user/states/test_camera_presets_write.json'):
                os.remove('./tests/reqs/emFS/SFTP/user/states/test_camera_presets_write.json')

            shutil.copyfile('./tests/reqs/emFS/SFTP/user/states/test_states/test_camera_presets_2.json', './tests/reqs/emFS/SFTP/user/states/test_camera_presets_write.json')
            self.TestCamController._CameraController__PresetsFilePath = '/user/states/test_camera_presets_write.json'
            try:
                self.TestCamController.SavePresetStates()
            except Exception as inst:
                self.fail("__SaveSchedule() raised {} unexpectedly!".format(type(inst)))
            os.remove('./tests/reqs/emFS/SFTP/user/states/test_camera_presets_write.json')

    def test_CameraController_LoadPresetStates_NoFile(self):
        try:
            self.TestCamController.LoadPresetStates()
        except Exception as inst:
            self.fail('LoadPresetStates raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_LoadPresetStates_TestFile(self):
        self.TestCamController._CameraController__PresetsFilePath = '/user/states/test_states/test_camera_presets_1.json'
        try:
            self.TestCamController.LoadPresetStates()
        except Exception as inst:
            self.fail('LoadPresetStates raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_UpdatePreset(self):
        try:
            self.TestCamController.UpdatePreset('Test Text', self.TestCamController._CameraController__EditorName)
        except Exception as inst:
            self.fail('UpdatePreset raised {} unexpectedly!'.format(type(inst)))
        self.assertEqual(self.TestCamController._CameraController__EditorName.PresetText, 'Test Text')

    def test_CameraController_UpdatePresetButtons(self):
        try:
            self.TestCamController.UpdatePresetButtons()
        except Exception as inst:
            self.fail('UpdatePresetButtons raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_SelectDefaultCamera(self):
        try:
            self.TestCamController.SelectDefaultCamera()
        except Exception as inst:
            self.fail('SelectDefaultCamera raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_SendCameraHome(self):
        camList = list(self.TestCamController.Cameras.values())

        for cam in camList:
            with self.subTest(camera=cam['Name'], method='byId'):
                try:
                    self.TestCamController.SendCameraHome(cam['Id'])
                except Exception as inst:
                    self.fail('SendCameraHome (id) raised {} unexpectedly!'.format(type(inst)))
            with self.subTest(camera=cam['Name'], method='byHw'):
                try:
                    self.TestCamController.SendCameraHome(cam['Hw'])
                except Exception as inst:
                    self.fail('SendCameraHome raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_SendCameraHome_AllCamera(self):
        try:
            self.TestCamController.SendCameraHome()
        except Exception as inst:
            self.fail('SendCameraHome raised {} unexpectedly!'.format(type(inst)))

    def test_CameraController_SendCameraHome_BadCamera(self):
        with self.assertRaises(KeyError):
            self.TestCamController.SendCameraHome('CAM005')

    def test_CameraController_SendCameraHome_BadType(self):
        contextList = [True, 1, 2.5, ['list'], {'dict': 'value'}]
        for con in contextList:
            with self.subTest(context=con):
                with self.assertRaises(TypeError):
                    self.TestCamController.SendCameraHome(con)

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


import functools
import os
import requests
import shutil
import traceback

from git import Repo
from git.exc import GitCommandError

from polkadot.globber import glob
from polkadot.logging import logger, log_operation

def filer(fn):
    """
    decorator that makes file operations lazy and builds a dependency tree.
    functions decorated with this generator must always yield, even in the case of failure.

    :param fn: function performing some side effect
    """
    @functools.wraps(fn)
    def inner(path, *args, deps = None, **kwargs):
        """
        generator wrapper that receives the polkadot config as the ``DOTFILES`` are iterated.

        if relative paths are provided in ``path``, it is assumed that the home directory is where the files are being added.

        .. note:: if a generator fails to yield, the whole pipeline will stop.

        :param str path: required part of the destination file:
        :param tupls args: optional positional arguments
        :param list deps: nested dependencies, e.g. mkdir before touching a file
        :param dict kwargs: optional keyword arguments which allow for support of different operations
        """
        config = (yield)

        if deps:
            for dep in deps:
                yield from dep

        if not path.startswith('/'):
            path = os.path.join(config['DOTFILES_OUTPUT_DIRECTORY'], path)

        if config['DOTFILES_DRY_RUN']:
            yield log_operation(fn, path, args, kwargs)
        else:
            yield from fn(path, *args, config = config, **kwargs)

    return inner

@filer
def copy(dest, source, config = None, template = True):
    """
    open a file, template it, write the rendered output to the destination and set file metadata

    .. note:: accepts wildcards for both arguments

    :param str dest: string or wildcard pattern for destination file(s)
    :param str source: string or wildcard pattern for source file(s)
    :param bool template: if False, skip jinja templating
    :param dict config: polkadot config dictionary
    """
    sources = list(glob(source, config['DOTFILES_WORKING_DIRECTORY']))

    if source.endswith('*') and not dest.endswith('*'):
        logger.error("Globbed sources must be copied to a globbed directory.")
        return
    else:
        dest = os.path.dirname(dest)

    prefix = len(source.split(os.path.sep)) - 1

    for source in sources:
        realdest = os.path.join(dest, *(source.split(os.path.sep)[prefix:]))
        logger.debug("copy %s to %s" % (source, realdest))

        if template:
            try:
                template = config['DOTFILES_JINJA_ENV'].get_template(source)
                output = template.render(config).encode('utf-8')
            except UnicodeDecodeError:
                logger.warning("couldn't template non-text file: %s" % (source))
                with open(source, 'rb') as binary_source:
                    output = binary_source.read()

            with open(realdest, 'wb') as d:
                d.write(output)
            yield shutil.copystat(source, realdest)
        else:
            yield shutil.copy2(source, realdest)

@filer
def touch(dest, config = None):
    """
    touch a file, creating if it doesn't exist and setting the access time.

    :param str dest: string or wildcard pattern for destination file
    :param dict config: polkadot config dictionary
    """
    logger.debug("touch %s" % dest)
    yield open(dest, 'a').close()
    yield os.utime(dest, None)

@filer
def mkdir(dest, config = None):
    """
    make a directory like ``mkdir -p``

    :param str dest: string or wildcard pattern for destination directory
    :param dict config: polkadot config dictionary
    """
    logger.debug("mkdir %s" % dest)
    yield os.makedirs(dest, exist_ok = True)

@filer
def mode(dest, octal, config = None):
    """
    set a file mode on ``dest``

    :param str dest: string or wildcard pattern for destination file
    :param int octal: octal representation (0o744) for chmod
    :param dict config: polkadot config dictionary
    """
    logger.debug("chmod %s %s" % (dest, oct(octal)))
    yield os.chmod(dest, octal)

@filer
def gitclone(dest, source, branch = 'master', config = None, git_kwargs = None):
    """
    clone a git repository

    .. note:: this will skip the repo if it already exists, it will not run ``git pull``

    :param str dest: string or wildcard pattern for destination directory
    :param str source: git url for source repository
    :param str branch: branch to check out when repository is cloned
    :param dict config: polkadot config dictionary
    """
    logger.debug("git clone %s into %s on '%s'" % (source, dest, branch))
    try:
        yield Repo().clone_from(
            source, dest,
            branch = branch,
            **(git_kwargs or {})
        )
    except GitCommandError:
        logger.debug(traceback.format_exc())
        logger.info("skipped git clone for existing repository in %s" % (dest))
        yield None

@filer
def download(dest, source, config = None):
    """
    streams a file using requests

    :param str dest: string or destination file
    :param str source: remote url for source file
    :param dict config: polkadot config dictionary
    """
    logger.debug("downloading file from %s into %s" % (source, dest))
    try:
        request = requests.get(source, stream = True)
        with open(dest, 'wb') as output:
            for chunk in request.iter_content(chunk_size = 1024):
                if chunk:
                    yield output.write(chunk)
    except:
        logger.error('failed to download file')
        logger.error(traceback.format_exc())
        yield None


#!/usr/bin/env python

import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv('benchmarks.csv')

base_time = df['mean'][0]
df['speedups'] = df['mean'].apply(lambda x: base_time/x)
df.plot(x='parameter_threads',
        y='speedups',
        xlabel='Num of Threads',
        ylabel='Speedup',
        legend=False,
        ylim=(0,6),
        title='Line Graph for Speedup')
plt.savefig('speedup_graph.png',bbox_inches='tight')


#!/usr/bin/env python

import contextlib
import fnmatch
import glob
from HTMLParser import HTMLParser
import optparse
import os
import pdb
import pexpect
import re
import requests
import shutil
import sys
import time

# -----------------------------------------------------------------------------
def main(args):
    """
    Generate hpssic rpm and an rpm for each of its requirements
    """
    global opt
    (opt, a) = argz(args, [{'names': ['-d', '--debug'],
                            'action': 'store_true',
                            'default': False,
                            'dest': 'debug',
                            'help': 'run the debugger'
                            },
                           {'names': ['-o', '--only'],
                            'action': 'append',
                            'default': [],
                            'dest': 'only',
                            'help': 'steps to do'
                            },
                           {'names': ['-r', '--reset'],
                            'action': 'store_true',
                            'default': False,
                            'dest': 'reset',
                            'help': 'clear the build area'
                            },
                           {'names': ['-s', '--skip'],
                            'action': 'append',
                            'default': [],
                            'dest': 'skips',
                            'help': 'steps to skip'
                            },
                           {'names': ['-t', '--test'],
                            'action': 'store_true',
                            'default': False,
                            'dest': 'test',
                            'help': 'run the test routine'
                            },
                           {'names': ['-n', '--dry-run'],
                            'action': 'store_true',
                            'default': False,
                            'dest': 'dryrun',
                            'help': 'report without doing'
                            }
                           ])

    (dstdir, blddir) = prepdir(['dist', 'build'])
    reqs = {#'ibmdb': {'name': 'ibm_db',
            #          'cmds': [set_ibm_home, set_cflags, edit_setup],
            #          },
            # 'pytest': {'name': 'pytest',
            #            'cmds': [],
            #            },
            # 'mysql': {'name': 'MySQL-python',
            #           'cmds': [prep_mysql],
            #           },
            # 'pexpect': {'name': 'pexpect',
            #             'cmds': []
            #             },
            }

    if os.getenv('VIRTUAL_ENV') is not None:
        raise SystemExit("Please deactivate any virtual environment first")

    everything = reqs.keys() + ['hpssic']
    if opt.only:
        reqlist = [x for x in opt.only if x in everything]
    elif opt.skips:
        reqlist = [x for x in everything if x not in opt.skips]
    else:
        reqlist = everything

    # set_ibm_home()
    if opt.test:
        test_build_sh()
    elif opt.reset:
        shutil.rmtree(blddir)
        shutil.rmtree(dstdir)
    else:
        run(output="build/build.sh.out")
        if 'hpssic' in reqlist:
            run("python setup.py bdist_rpm")
            reqlist.remove('hpssic')
        with cd(blddir):
            for req in reqlist:
                make_rpm(blddir, dstdir, reqs[req])

# -----------------------------------------------------------------------------
def argz(argv, plist):
    """
    Parse the command line
    """
    p = optparse.OptionParser(epilog='For -o and -s, recognized arguments'
                              " are 'ibmdb', 'mysql', 'pexpect', and 'pytest'")
    debug_present = False
    for arg in plist:
        if '--debug' in arg['names']:
            debug_present = True
        names = arg['names']
        k = dict((q, arg[q]) for q in arg if q != 'names')
        p.add_option(*arg['names'], **k)

    if not debug_present:
        p.add_option('-d', '--debug',
                     action='store_true', default=False, dest='debug',
                     help='run the debugger')

    (o, a) = p.parse_args(argv)

    if o.debug:
        pdb.set_trace()

    return(o, a)

# -----------------------------------------------------------------------------
@contextlib.contextmanager
def cd(path):
    origin = os.getcwd()
    os.chdir(path)

    yield

    os.chdir(origin)

# -----------------------------------------------------------------------------
def compute_url(reqname):
    """
    Figure out the url to retrieve a .tar.gz file for *reqname*
    """
    durl = ("https://pypi.python.org/packages/source/%s/%s/" %
            (reqname[0], reqname))
    d = requests.get(durl)
    h = html()
    h.feed(d.content)
    recent = h.result()
    purl = durl + recent
    return(purl)

# -----------------------------------------------------------------------------
def deliver_rpms(target):
    """
    copy any rpms generated up to the target directory
    """
    for item in glob.glob("dist/*.rpm"):
        shutil.copy(item, target)

# -----------------------------------------------------------------------------
def expand_manifest(in_name, out_name):
    """
    Expand MANIFEST.in to MANIFEST
    """
    with open(in_name, 'r') as r:
        with open(out_name, 'w') as w:
            for line in r:
                if line.startswith("recursive-include"):
                    (c, rd, pat) = line.strip().split()
                    for r, d, f in os.walk(rd):
                        for name in fnmatch.filter(f, pat):
                            w.write("%s/%s\n" % (r, name))
                elif line.startswith("include"):
                    (c, fname) = line.strip().split()
                    w.write(fname + '\n')
                else:
                    w.write(line.rstrip() + "\n")

# -----------------------------------------------------------------------------
def fetch(req):
    """
    Retrieve a package from the web
    """
    url = compute_url(req)
    rval = os.path.basename(url)
    run("wget --no-check-certificate %s" % url)
    return rval

# -----------------------------------------------------------------------------
def make_rpm(bld, dst, rdict):
    """
    Retrieve requirement *req*, unpack it, use python setup.py to generate rpms
    and move them to *target*. Clean up after yourself.
    """
    filename = fetch(rdict['name'])
    p = unpack(filename)
    with cd(p):
        for func in rdict['cmds']:
            func()
        run("python setup.py bdist_rpm")
        deliver_rpms(dst)

# -----------------------------------------------------------------------------
def prep_mysql():
    """
    Get mysql ready for bdist_rpm. Things we've found that have to be done so
    far:
     - comment the doc_files line in mysql's setup.cfg
    """
    filename = 'setup.cfg'
    with open(filename, 'r') as r:
        with open(filename + '.new', 'w') as w:
            for line in r:
                if 'doc_files' in line:
                    w.write("# " + line)
                else:
                    w.write(line)
    os.rename(filename, filename + '.old')
    os.rename(filename + '.new', filename)

# -----------------------------------------------------------------------------
def prepdir(dl):
    """
    Make sure *path* exists and is a directory. Unless opt.reset is true. In
    that case, we're deleting the directories if they exist, so don't create
    them.
    """
    global opt
    rval = []
    for dname in dl:
        path = os.path.join(os.getcwd(), dname)
        if not os.path.exists(path) and not opt.reset:
            os.mkdir(path)
        elif not os.path.isdir(path):
            raise SystemExit("%s is not a directory" % path)
        rval.append(path)
    return rval

# -----------------------------------------------------------------------------
def run(cmd=None, output=None):
    """
    Run *cmd* or just display it, depending on the dryrun option
    """
    global opt

    if output:
        run._output = open(output, 'w')

    try:
        x = run._output
    except:
        run._output = sys.stdout

    if cmd is not None:
        print(cmd)
        if run._output != sys.stdout:
            run._output.write("$ " + cmd + "\n")
        if not opt.dryrun:
            run._output.write(pexpect.run(cmd, logfile=run._output))

# -----------------------------------------------------------------------------
def edit_setup():
    """
    In setup.py, we have to change './CHANGES' to '$PWD/CHANGES' in the
    data_files list so the rpm build can find the file.
    """
    rgx = '\./(README|CHANGES|LICENSE)'
    with open('setup.py', 'r') as r:
        with open('setup.py.new', 'w') as w:
            for line in r:
                if re.findall(rgx, line):
                    # w.write(re.sub(rgx, '%s/\g<1>' % os.getcwd(), line))
                    w.write("# " + line)
                else:
                    w.write(line)
    os.rename('setup.py', 'setup.py.old')
    os.rename('setup.py.new', 'setup.py')

# -----------------------------------------------------------------------------
def set_cflags():
    """
    Have to add the current directory to the list of include files. We do this
    by writing a new setup.cfg file.
    """
    with open('setup.cfg', 'w') as f:
        f.write("[build_ext]\n")
        f.write("include_dirs = %s\n" % os.getcwd())
        # f.write("\n")
        # f.write('[files]\n')
        # f.write('extra_files =\n')
        # f.write('    %s/README\n' % os.getcwd())
        # f.write('    %s/CHANGES\n' % os.getcwd())
        # f.write('    %s/LICENSE\n' % os.getcwd())

# -----------------------------------------------------------------------------
def set_ibm_home():
    """
    Set the environment variable IBM_DB_HOME
    """
    l = glob.glob("/opt/ibm/db2/*")
    bn = [os.path.basename(q) for q in l]
    f = [q.lstrip('vV') for q in bn]
    d = dict((float(k), v) for k, v in zip(f, l))
    i = sorted(d.keys())[-1]
    os.environ['IBM_DB_HOME'] = d[i]

# -----------------------------------------------------------------------------
def test_build_sh():
    """
    Test routine for html parsing (or anything else we want to try out)
    """
    with cd("build/MySQL-python-1.2.5"):
        expand_manifest("MANIFEST.in", "MANIFEST")

# -----------------------------------------------------------------------------
def unpack(filename):
    """
    Unpack a file, either .tar.gz or .zip
    """
    if filename.endswith('.tar.gz'):
        run("tar xf %s" % filename)
        rval = filename.replace('.tar.gz', '')
    elif filename.endswith('.zip'):
        run("unzip %s" % filename)
        rval = filename.replace('.zip', '')
    else:
        print("I don't know how to unpack %s" % filename)
        rval = ''
    return rval

# -----------------------------------------------------------------------------
class html(HTMLParser, object):
    def __init__(self, *args, **kw):
        super(html, self).__init__(*args, **kw)
        self._html_result = []
        self.current = None

    def handle_starttag(self, tag, attrs):
        if tag == 'a':
            ad = dict(attrs)
            if 'href' in ad:
                name = ad['href']
                if re.findall("(.tar.gz|.zip)$", name):
                    self.current = {'name': name}
                    self._html_result.append(self.current)
                else:
                    self.current = None

    def handle_data(self, data):
        # print("handle_data got '%s'" % data)
        if self.current:
            try:
                d = ' '.join(data.split()[0:2])
                tm = time.strptime(d.strip(), "%d-%b-%Y %H:%M")
                self.current['date'] = time.strftime("%Y.%m%d %H:%M", tm)
            except ValueError:
                pass

    def result(self):
        if 0 < len(self._html_result):
            rval = sorted(self._html_result,
                          lambda a,b: cmp(a['date'], b['date']))[-1]['name']
        else:
            rval = ''
        return rval

# -----------------------------------------------------------------------------
if __name__ == '__main__':
    main(sys.argv)


""" Specify imagenet model.

"""
import logging

import matplotlib.pyplot as plt
import numpy as np
import rawpy
import imageio
import tensorflow as tf
from tensorflow.python.framework import ops
import tensorflow.contrib.slim as slim

#from train_Sony import network

logger = logging.getLogger(__name__)

def create_placeholders(n_H, n_W, n_C):
    """
    Creates the placeholders for the tensorflow session.

    Note that the placeholders will most likely vary by input dataset.

    Arguments:
    n_H -- scalar, height of an input image
    n_W -- scalar, width of an input image
    n_C -- scalar, number of channels of the input

    Returns:
    X -- placeholder for the data input, of shape [None, n_H, n_W, n_C] and dtype "int"
    Y -- placeholder for the input labels, of shape and dtype equal to X
    """

    X = tf.placeholder(tf.float32, shape=(None, n_H, n_W, n_C))
    Y = tf.placeholder(tf.float32, shape=(None, n_H, n_W, n_C))

    return X,Y

def initialize_parameters():
    """
    Initializes weight parameters to build a neural network with tensorflow.
    The shapes are:
        []

    Returns:
    parameters -- a dictionary of tensors containing W1,...,WN
    """
    tf.set_random_seed(1)

    # Block 1

    W1 = tf.get_variable("W1", [3,3,3,32],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))
    W2 = tf.get_variable("W2", [3,3,32,32],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))

    # Block 2

    W3 = tf.get_variable("W3", [3,3,32,64],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))
    W4 = tf.get_variable("W4", [3,3,64,64],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))

    # Block 3

    W5 = tf.get_variable("W5", [3,3,64,128],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))
    W6 = tf.get_variable("W6", [3,3,128,128],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))

    # Block 4

    W7 = tf.get_variable("W7", [3,3,128,256],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))
    W8 = tf.get_variable("W8", [3,3,256,256],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))

    # Block 5

    W9 = tf.get_variable("W9", [3,3,256, 512],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))
    W10 = tf.get_variable("W10", [32,32,512,512],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))

    # Block 6

    W11 = tf.get_variable("W11", [3,3,512, 256],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))
    W12 = tf.get_variable("W12", [3,3,256, 256],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))

    # Block 7

    W13 = tf.get_variable("W13", [3,3,256,128],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))
    W14 = tf.get_variable("W14", [3,3,128,128],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))

    # Block 8

    W15 = tf.get_variable("W15", [3,3,128,64],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))
    W16 = tf.get_variable("W16", [3,3,64,64],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))

    # Block 9

    W17 = tf.get_variable("W17", [3,3,64,32],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))
    W18 = tf.get_variable("W18", [3,3,32,32],
                         initializer=\
                         tf.contrib.layers.xavier_initializer(seed = 0))

    # Block 10

    W19 = tf.get_variable('W19', [1,1,32, 12],
                          initializer=\
                          tf.contrib.layers.xavier_initializer(seed = 0))

    parameters = {
        'W1': W1, 'W2': W2, 'W3': W3, 'W4': W4, 'W5': W5, 'W6': W6,
        'W7': W7, 'W8': W8, 'W9': W9, 'W10':W10, 'W11': W11, 'W12': W12,
        'W13': W13, 'W14': W14, 'W15': W15, 'W16': W16, 'W17': W17,
        'W18': W18, 'W19': W19,
    }

    return parameters

def pool_block(X, Wa, Wb):
    """ This is like a mini forward propagation module. """
    Za = tf.nn.conv2d(X, Wa, strides=(1,1,1,1), padding='SAME')
    Aa = tf.nn.leaky_relu(Za, alpha=0.2)
    Zb = tf.nn.conv2d(Aa, Wb, strides=(1,1,1,1), padding='SAME')
    Ab = tf.nn.leaky_relu(Zb, alpha=0.2)
    P = tf.nn.max_pool(Ab, ksize=[1,2,2,1],
                        strides=[1,1,1,1], padding='SAME')
    return P

def conv_block(X, Wa, Wb):
    Za = tf.nn.conv2d(X, Wa, strides=(1,1,1,1), padding='SAME')
    Aa = tf.nn.leaky_relu(Za, alpha=0.2)
    Zb = tf.nn.conv2d(Aa, Wb, strides=(1,1,1,1), padding='SAME')
    Ab = tf.nn.leaky_relu(Zb, alpha=0.2)
    return Ab

def upsample_and_concat(x1, x2, output_channels, in_channels):
    pool_size = 2
    deconv_filter = tf.Variable(
        tf.truncated_normal([pool_size, pool_size, output_channels,
                             in_channels], stddev=0.02)
    )
    deconv = tf.nn.conv2d_transpose(x1, deconv_filter, tf.shape(x2),
                                    strides=[1, pool_size, pool_size, 1])

    deconv_output = tf.concat([deconv, x2], 3)
    deconv_output.set_shape([None, None, None, output_channels * 2])

    return deconv_output

def upsample_block(B, Bp, output_channels, in_channels, Wa, Wb):

    U = upsample_and_concat(B, Bp, output_channels, in_channels)
    Za = tf.nn.conv2d(U, Wa, strides=(1,1,1,1), padding='SAME')
    Aa = tf.nn.leaky_relu(Za, alpha=0.2)
    Zb = tf.nn.conv2d(Aa, Wb, strides=(1,1,1,1), padding='SAME')
    Ab = tf.nn.leaky_relu(Zb, alpha=0.2)
    return Ab

def output_block(X, W):

    Z = tf.nn.conv2d(X, W, strides=(1,1,1,1), padding='SAME')
    logger.debug("output block shape Z: {}".format(Z.get_shape()))
    Out = tf.depth_to_space(Z, 2)
    logger.debug("output block transform: {}".format(Out.get_shape()))
    return Out

def forward_propagation(X, parameters):

    # Block 1
    W1, W2 = parameters['W1'], parameters['W2']
    B1 = pool_block(X, W1, W2)
    logger.debug('B1 shape: {}'.format(B1.get_shape()))

    # Block 2
    W3, W4 = parameters['W3'], parameters['W4']
    B2 = pool_block(B1, W3, W4)
    logger.debug('B2 shape: {}'.format(B2.get_shape()))

    # Block 3
    W5, W6 = parameters['W5'], parameters['W6']
    B3 = pool_block(B2, W5, W6)
    logger.debug('B3 shape: {}'.format(B3.get_shape()))

    # Block 4
    W7, W8 = parameters['W7'], parameters['W8']
    B4 = pool_block(B3, W7, W8)
    logger.debug('B4 shape: {}'.format(B4.get_shape()))

    # Block 5
    W9, W10 = parameters['W9'], parameters['W10']
    B5 = conv_block(B4, W9, W10)
    logger.debug('B5 shape: {}'.format(B5.get_shape()))

    # Block 6
    W11, W12 = parameters['W11'], parameters['W12']
    B6 = upsample_block(B5, B4, 256, 512, W11, W12)
    logger.debug('B6 shape: {}'.format(B6.get_shape()))

    # Block 7
    W13, W14 = parameters['W13'], parameters['W14']
    B7 = upsample_block(B6, B3, 128, 256, W13, W14)
    logger.debug('B7 shape: {}'.format(B7.get_shape()))

    # Block 8
    W15, W16 = parameters['W15'], parameters['W16']
    B8 = upsample_block(B7, B2, 64, 128, W15, W16)
    logger.debug('B8 shape: {}'.format(B8.get_shape()))

    # Block 9
    W17, W18 = parameters['W17'], parameters['W18']
    B9 = upsample_block(B8, B1, 32, 64, W17, W18)
    logger.debug('B9 shape: {}'.format(B9.get_shape()))

    # Block 10
    W19 = parameters['W19']
    Bout = output_block(B9, W19)
    logger.debug('Bout shape: {}'.format(Bout.get_shape()))

    return Bout

def lrelu(x):
    return tf.maximum(x * 0.2, x)

def sony_network(input):
    conv1 = slim.conv2d(input, 32, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv1_1')
    conv1 = slim.conv2d(conv1, 32, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv1_2')
    pool1 = slim.max_pool2d(conv1, [2, 2], padding='SAME')
    logger.debug("pool1: {}".format(pool1.get_shape()))

    conv2 = slim.conv2d(pool1, 64, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv2_1')
    conv2 = slim.conv2d(conv2, 64, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv2_2')
    pool2 = slim.max_pool2d(conv2, [2, 2], padding='SAME')
    logger.debug("pool2: {}".format(pool2.get_shape()))

    conv3 = slim.conv2d(pool2, 128, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv3_1')
    conv3 = slim.conv2d(conv3, 128, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv3_2')
    pool3 = slim.max_pool2d(conv3, [2, 2], padding='SAME')
    logger.debug("pool3: {}".format(pool3.get_shape()))

    conv4 = slim.conv2d(pool3, 256, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv4_1')
    conv4 = slim.conv2d(conv4, 256, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv4_2')
    pool4 = slim.max_pool2d(conv4, [2, 2], padding='SAME')
    logger.debug("pool4: {}".format(pool4.get_shape()))

    conv5 = slim.conv2d(pool4, 512, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv5_1')
    conv5 = slim.conv2d(conv5, 512, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv5_2')
    logger.debug('conv5: {}'.format(conv5.get_shape()))

    up6 = upsample_and_concat(conv5, conv4, 256, 512)
    conv6 = slim.conv2d(up6, 256, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv6_1')
    conv6 = slim.conv2d(conv6, 256, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv6_2')
    logger.debug('conv6: {}'.format(conv6.get_shape()))

    up7 = upsample_and_concat(conv6, conv3, 128, 256)
    conv7 = slim.conv2d(up7, 128, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv7_1')
    conv7 = slim.conv2d(conv7, 128, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv7_2')
    logger.debug('conv7: {}'.format(conv7.get_shape()))

    up8 = upsample_and_concat(conv7, conv2, 64, 128)
    conv8 = slim.conv2d(up8, 64, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv8_1')
    conv8 = slim.conv2d(conv8, 64, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv8_2')
    logger.debug('conv8: {}'.format(conv8.get_shape()))

    up9 = upsample_and_concat(conv8, conv1, 32, 64)
    conv9 = slim.conv2d(up9, 32, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv9_1')
    conv9 = slim.conv2d(conv9, 32, [3, 3], rate=1, activation_fn=lrelu, scope='g_conv9_2')
    logger.debug('conv9: {}'.format(conv9.get_shape()))

    conv10 = slim.conv2d(conv9, 12, [1, 1], rate=1, activation_fn=None, scope='g_conv10')
    out = tf.depth_to_space(conv10, 2)
    logger.debug('out shape: {}'.format(out.get_shape()))
    return out

def compute_cost(Z, Y):
    """
    Computes the cost

    Arguments:
    Z -- output of forward propagation
    Y -- "true" image as a label, same shape as Z

    Returns:
    cost - Tensor of the cost function
    """
    cost = tf.reduce_mean(tf.abs(Z - Y))
    return cost

def random_mini_batches(m, minibatch_size, seed):
    """ Generator yielding minibatches """
    ids = [i for i in range(m)]
    np.random.seed(seed)
    np.random.shuffle(ids)

    for id_val in ids:
        yield id_val

def process_minibatch(minibatch_X, minibatch_Y, ps=16):

    mb_X = np.expand_dims(minibatch_X, axis=0)
    mb_Y = np.expand_dims(minibatch_Y, axis=0)

    _, H, W, _ = mb_X.shape

    xx = np.random.randint(0, W - ps)
    yy = np.random.randint(0, H - ps)

    X_patch = mb_X[:,yy:yy + ps, xx:xx + ps, :]
    Y_patch = mb_Y

    return X_patch, Y_patch

def cifar_model(X_train, Y_train, X_test, Y_test, learning_rate=1e-4,
                num_epochs=1, minibatch_size=32, print_cost=True):
    """
    Implements Learn-to-See-in-the-Dark for CIFAR dataset

    Arguments:
    X_train -- training set, of shape (None, 32, 32, 3)
    Y_train -- test set, of shape (None, 32, 32, 3)
    X_test -- training set, of shape (None, 32, 32, 3)
    Y_test -- test set, of shape (None, 32, 32, 3)
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print cost every 100 epochs

    Returns:
    parameters -- parameters learnt by the model. They can be used to predict.
    costs -- list of costs generated by the model
    learning_rate -- scalar real number, learning rate used by model
    """
    ops.reset_default_graph() # be able to rerun the model
    tf.set_random_seed(42)
    seed = 42
    m = X_train.shape[0]
    assert X_train.shape == Y_train.shape
    costs = []

    #X,Y = create_placeholders(n_H, n_W, n_C)
    #parameters = initialize_parameters()
    #Z = forward_propagation(X, parameters)

    X = tf.placeholder(tf.float32, [None, None, None, 3])
    Y = tf.placeholder(tf.float32, [None, None, None, 3])

    Z = sony_network(X)

    cost = compute_cost(Z, Y)

    # Backpropagation

    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

    # Initialize all the variables globally
    init = tf.global_variables_initializer()

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:

        # Run the initialization
        sess.run(init)

        # Do the training loop

        minibatches = random_mini_batches(m, minibatch_size, seed)

        logger.info("Starting epochs")
        for epoch in range(num_epochs):

            minibatch_cost = 0
            num_minibatches = int(m / minibatch_size)
            logger.debug('num minibatches: {}'.format(num_minibatches))
            seed = seed + 1
            minibatches = random_mini_batches(m, minibatch_size, seed)

            for minibatch in minibatches:

                # Select a minibatch
                minibatch_X = X_train[minibatch,...]
                minibatch_Y = Y_train[minibatch,...]

                minibatch_X, minibatch_Y = process_minibatch(minibatch_X, minibatch_Y)

                _, temp_cost, output = sess.run([optimizer, cost, Z],
                                        feed_dict={X: minibatch_X,
                                                   Y: minibatch_Y})

                minibatch_cost += temp_cost / num_minibatches

            # Print the cost every epoch
            if print_cost == True and epoch % 5 == 0:
                logger.info("Cost after epoch %i: %f" % (epoch,
                                                         minibatch_cost))
            if print_cost == True and epoch % 1 == 0:
                costs.append(minibatch_cost)

    # Write out image
    output = np.minimum(np.maximum(output, 0), 1)
    logger.info("output shape: {}".format(output.shape))
    imageio.imwrite("sony_network.jpg", output[0,...])
    parameters = {}

    return (parameters, costs, learning_rate)

def plot_costs(costs, learning_rate, filepath):
    """ plot the cost from forward propagation """
    plt.plot(np.squeeze(costs))
    plt.ylabel('cost')
    plt.xlabel('iterations (per tens)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.savefig(filepath)


import tensorflow as tf
import json

bn_decay = 0.997
bn_epsilon = 1e-5

def build_model(inputs, num_classes, is_training, model_conf):
    filters = json.loads(model_conf.filters)
    net = inputs
    strides = json.loads(model_conf.strides)
    pool_size = json.loads(model_conf.pool_size)
    pool_stride = json.loads(model_conf.pool_stride)
    filter_size = json.loads(model_conf.filter_size)
    for i in range(model_conf.num_layers):
        net = tf.layers.conv2d(net, filter_size[i], filters[i], strides=strides[i], padding='valid',
                               kernel_initializer=tf.variance_scaling_initializer(), name="conv" + str(i))

        if model_conf.bn:
            net = tf.layers.batch_normalization(inputs=net, axis=3, momentum=bn_decay, epsilon=bn_epsilon,
                                                center=True, scale=True, training=True, fused=True,
                                                name="batch" + str(i))
        if model_conf.pooling:
            net = tf.layers.max_pooling2d(net, pool_size[i], pool_stride[i], name="maxpool" + str(i))
        net = tf.nn.relu(net, name="relu" + str(i))

        tf.summary.histogram('activations_%d' % i, net)

    output_size = net.get_shape()[1]
    net = tf.layers.conv2d(net, 1, 1, 1, padding='same', kernel_initializer=tf.variance_scaling_initializer,
                           name="last_conv")

    net = tf.reshape(net, [-1, output_size * output_size], name="net_vectorize")
    return tf.layers.dense(net, num_classes, name="dense")

build_model.default_image_size = 224


# -*- coding: utf-8 -*-
# Project: local-spark
# Author: chaoxu create this file
# Time: 2018/2/6
# Company : Maxent
# Email: chao.xu@maxent-inc.com

from __future__ import division, print_function
import keras.backend as K
import numpy as np
from itertools import product

def w_categorical_crossentropy(y_true, y_pred, weights):
    nb_cl = len(weights)
    final_mask = K.zeros_like(y_pred[:, 0])
    y_pred_max = K.max(y_pred, axis=1)
    y_pred_max = K.expand_dims(y_pred_max, 1)
    y_pred_max_mat = K.equal(y_pred, y_pred_max)
    for c_p, c_t in product(range(nb_cl), range(nb_cl)):
        final_mask += (K.cast(weights[c_t, c_p], K.floatx()) *
                       K.cast(y_pred_max_mat[:, c_p], K.floatx()) *
                       K.cast(y_true[:, c_t], K.floatx())
                       )
    return final_mask

if __name__ == '__main__':
    w_array = np.ones((2, 2))
    w_array[1, 0] = 1.2
    w_array[0, 1] = 1.2
    a = K.variable(np.array([1, 0, 0]))
    b = K.variable(np.array([1, 1, 1]))
    s = K.eval(w_categorical_crossentropy(a, b, w_array))
    print(s)


import pandas as pd
import matplotlib.pyplot as plt
from getProductList import getProductList
import numpy as np

def extract(x):
    x=x.replace("."," ")
    x=x.replace("+"," ")
    driver=getProductList()
    x=driver.preprocess(x)
    return x

df=pd.read_csv("customerRequest.csv",engine='python')
df["Products"]=df["Products"].apply(lambda x:extract(x))
df['Products'].replace('', np.nan, inplace=True)
df=df.dropna(how='any')
plt.title("Products Viewed by Customers")
plt.xlabel("Products")
plt.ylabel("Number of Customers")
plt.hist(df['Products'].values)
plt.show()


from __future__ import print_function

import os
import urllib.request

from selenium import webdriver

print("Testing Internet Connection")
print()
try:
    urllib.request.urlopen(
        "http://google.com", timeout=2
    )  # Tests if connection is up and running
    print("Internet is working fine!")
    print()
    question = input("Do you want to open a website? (Y/N): ")
    if question == "Y":
        print()
        search = input("Input website to open (http://website.com) : ")
    else:
        os._exit(0)

except urllib.error.URLError:
    print("No internet connection!")  # Output if no connection

browser = webdriver.Firefox()
browser.get(search)
os.system("cls")  # os.system('clear') if Linux
print("[+] Website " + search + " opened!")
browser.close()


import numpy as np
import argparse, json, sys, re

from coupled import COUPLED
from zverev import ZVEREV
from lowpass import LOWPASS

from ness import (
    prototype_qk, denormalize_qk, coupling_g, db, chebyshev,   # helpers
    nodal_delay_transmission, nodal_insertionloss,             # measurements at fo
    nodal_returnloss, nodal_delay_bandwidth, nodal_bandwidth,  # approximations
    lowpass_groupdelay, lowpass_bandwidth,                     # approximations
    groupdelay_maqu, groupdelay_tdqu,   # lossy
    groupdelay_qk,                      # lossless
    qequ_groupdelay, k12_groupdelay,    # validation
    fn_nodal_transmission, groupdelay,  # when re != zo
)

def parse_args():
    parser = argparse.ArgumentParser(formatter_class=
             argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("-l", "--list", action="store_true")
    parser.add_argument("-p", "--predistorted", action="store_true",
                        help="use Zverev's predistorted filters")
    parser.add_argument("-g", "--g", action="store_true",
                        help="use lowpass prototype table")
    parser.add_argument("-u", "--qu", type=float, default=np.inf,
                        help='unloaded quality factor')
    parser.add_argument("-n", "--number", type=int, default=None,
                        help="number of filter poles")
    parser.add_argument("-f", "--frequency", type=float, help='center frequency')
    parser.add_argument("-b", "--bandwidth", type=float, help='bandwidth')
    parser.add_argument("--zo", type=float, default=50.0, help='line impedance')
    parser.add_argument("--re", type=float, default=50.0, help='filter impedance')
    parser.add_argument("--butterworth", action="store_true", help='use a Butterworth filter')
    parser.add_argument("--bessel", action="store_true", help='use a Bessel filter')
    parser.add_argument("--legendre", action="store_true", help='use a Lengendre filter')
    parser.add_argument("--chebyshev", type=float, help='use a Chebyshev filter')
    parser.add_argument("--gaussian", type=float, help='use a Gaussian filter')
    parser.add_argument("--linear-phase", type=float, help='use a Linear phase filter')
    parser.add_argument("--max-ripple", type=float, help='use Chebyshev filter of given ripple')
    parser.add_argument("--max-swr", type=float, help='use Chebyshev filter of given SWR')
    parser.add_argument("--max-rc", type=float, help='use Chebyshev filter of given reflection coefficient')
    parser.add_argument("--validate", action='store_true', help='validate results against k12')
    parser.add_argument("--lowpass", action='store_true', help='predicted lowpass characteristics')
    parser.add_argument("--qequ", nargs=2, metavar=('<RL1(dB)>', '<TD1(ns)>',), type=float,
                        help='calculate QE and QU using resonator 1 group delay and return loss')
    parser.add_argument("--k12", nargs=3, metavar=('<RL1(dB)>', '<TD1(ns)>', '<TD2(ns)>'), type=float,
                        help='calculate k12 using resonator 1 and 2 group delay and return loss')
    return parser.parse_args()

def list_qk(qk, bw, fo):
    QK = denormalize_qk(qk, bw, fo)
    for i in range(len(QK)):
        name1, name2, name3 = f'k{i}{i+1}', f'K{i}{i+1}', f'BW{i}{i+1}'
        if i == 0: name1, name2, name3 = 'q1', 'Q1', 'BW1'
        if i == len(QK)-1: name1, name2, name3 = f'q{i}', f'Q{i}', f'BW{i}'
        print('  {:4s} {:11.6f}   |   {:4s} {:11.6f}   |   {:4s} {:11.5f} MHz'
              .format(name1, qk[i], name2, QK[i], name3, qk[i] * bw / 1e6))

def list_g(g):
    for i in range(len(g)):
        print('  {:4s} {:11.6f}'.format(f'g{i}', g[i]))

def list_groupdelays(TD1, TD2, MA1, MA2):
    N = len(TD1)
    width = len(' '.join([ str(i) for i in range(1, N+1) ]))
    for n in range(N):
        res1 = ' '.join([ str(i) for i in range(1, n+2) ])
        res2 = ' '.join([ str(i) for i in range(N, N-n-1, -1) ])
        print('  {} {:9.3f} ns {:7.3f} dB   |   {} {:9.3f} ns {:7.3f} dB'.format(
              res1.ljust(width), TD1[n] * 1e9, db(1/MA1[n]),
              res2.ljust(width), TD2[n] * 1e9, db(1/MA2[n])))

def find_filter(table, name, value=None):
    name = name.lower()
    for key, data in table.items():
        m = re.search('[\d\.]+', key)
        if name == key.lower() or (
           name in key.lower() and m and float(m.group(0)) == value):
            return key, data

def main():
    bw = args.bandwidth
    fo = args.frequency
    qu = args.qu

    if (args.qequ or args.k12) and not fo:
        print("Center frequency not set.")
        return

    if args.qequ:
        ma1 = 10**(-args.qequ[0] / 20)
        td1 = args.qequ[1] * 1e-9
        qe, qu = qequ_groupdelay(fo, td1, ma1)
        print('QU = {:11.3f}'.format(qu))
        print('QE = {:11.3f}'.format(qe))
        return

    if args.k12:
        ma1 = 10**(-args.k12[0] / 20)
        td1 = args.k12[1] * 1e-9
        td2 = args.k12[2] * 1e-9
        qe, qu = qequ_groupdelay(fo, td1, ma1)
        k12 = k12_groupdelay(fo, td1, td2, ma1)
        print('QU  = {:11.3f}'.format(qu))
        print('QE  = {:11.3f}'.format(qe))
        print('K12 = {:11.6f}'.format(k12))
        return

    if args.g:
        table = LOWPASS
    elif args.predistorted:
        table = ZVEREV
    else:
        table = COUPLED

    if args.list:
        for name in table:
            print(name)
        return

    if not args.number:
        print("Number of poles not set.")
        return

    # pull tables
    if args.butterworth:
        res = find_filter(table, 'BUTTERWORTH')
    elif args.bessel:
        res = find_filter(table, 'BESSEL')
    elif args.legendre:
        res = find_filter(table, 'LEGENDRE')
    elif args.chebyshev:
        res = find_filter(table, 'CHEBYSHEV', args.chebyshev)
    elif args.gaussian:
        res = find_filter(table, 'GAUSSIAN', args.gaussian)
    elif args.linear_phase:
        res = find_filter(table, 'LINEAR PHASE', args.linear_phase)
    elif args.max_ripple or args.max_swr or args.max_rc:
        args.g = True
        if args.max_swr:
            swr = args.max_swr
            rc = (swr - 1) / (swr + 1)
            ripple = -10 * np.log10(1 - rc**2)
        if args.max_rc:
            rc = args.max_rc
            ripple = -10 * np.log10(1 - rc**2)
        if args.max_ripple:
            ripple = args.max_ripple
        res = ('Chebyshev {:.4g} dB'.format(ripple),
               [chebyshev(args.number, ripple)])
    else:
        print('No filter type specified.')
        return

    # collect data
    name, values = res
    data = []
    for row in values:
        d = {}
        d['name'] = name
        if args.g:
            if args.predistorted:
                print('No predistorted lowpass prototypes.')
                return
            g = row
            d['g'] = g
            d['qk'] = coupling_g(g)
            n = len(g) - 2
        else:
            if args.predistorted:
                d['qo'] = row[0]
                row = row[2:]
            qk = row[:1] + row[2:] + row[1:2]
            d['qk'] = qk
            d['g'] = prototype_qk(qk)
            n = len(qk) - 1
        if args.number is None or args.number == n:
            data.append(d)

    # analyze filters
    count = 0
    for d in data:
        name = d.get('name')
        qo = d.get('qo')
        qk = d['qk']
        g = d['g']
        N = len(g) - 2

        if count: print()
        count += 1
        print('---------------------------------------')
        print('{:^39}'.format('{} Pole {}'.format(N, name)))
        print('---------------------------------------')

        print('Normalized Lowpass Coefficients gi')
        list_g(g)

        if qo:
            print('Predistored q0      = {:>15}'.format(str(qo)))
        if fo:
            print('Center Frequency    = {:15.5f} MHz'.format(fo / 1e6))
        if args.lowpass:
            fp, td = lowpass_groupdelay(g, fo, qu)
            print('Ness Group Delay of Low Pass Filter (QU={})'.format(qu))
            for i in range(len(fp)):
                print('  TD{}  {:11.3f} ns     peak at {:11.5f} MHz '
                      .format(i+2, td[i] * 1e9, fp[i] / 1e6))
            fpeak, tdpeak = lowpass_bandwidth(g, fo, qu)
            print('Group Delay Peak Of Terminated Low Pass Filter (QU={})'.format(qu))
            print('       {:11.3f} ns     peak at {:11.5f} MHz '
                  .format(tdpeak * 1e9, fpeak / 1e6))
            continue

        if bw:
            print('Design Bandwidth    = {:15.5f} MHz'.format(bw / 1e6))
        if bw and fo:
            bwtd = nodal_delay_bandwidth(qk, bw, fo, qu) # step
            print('Delay Bandwidth     = {:15.5f} MHz'.format(bwtd / 1e6))
            bwdb = nodal_bandwidth(qk, bw, fo, qu) # step
            print('3dB Bandwidth       = {:15.5f} MHz'.format(bwdb / 1e6))
            td = nodal_delay_transmission(qk, bw, fo, qu)
            print('Transmission Delay  = {:15.3f} ns'.format(td * 1e9))
            rl = nodal_returnloss(qk, bw, fo, qu) # step
            print('Minimum Return Loss = {:15.3f} dB'.format(rl))
            il = nodal_insertionloss(qk, bw, fo, qu) # step
            print('Insertion Loss      = {:15.3f} dB'.format(il))
            print('Loaded QL           = {:15.3f}'.format(fo / bw))
            print('Unloaded QU         = {:15.3f}'.format(qu))
            print('Normalized q0       = {:15.3f}'.format(qu / (fo / bw)))
            print('Normalized and Denormalized qi, kij, and Coupling Bandwidths')
            list_qk(qk, bw, fo)

        if bw:
            print('Lossless Ness Group Delay and Return Loss')
            TD1 = groupdelay_qk(qk, bw)
            TD2 = groupdelay_qk(qk[::-1], bw)
            MA = np.ones(len(TD1))
            list_groupdelays(TD1, TD2, MA, MA)

        if bw and fo and not np.isinf(qu):
            print('Ness Group Delay and Return Loss (QU={})'.format(qu))
            MA1 = groupdelay_maqu(g, bw, fo, qu)
            TD1 = groupdelay_tdqu(g, bw, fo, qu)
            MA2 = groupdelay_maqu(g[::-1], bw, fo, qu)
            TD2 = groupdelay_tdqu(g[::-1], bw, fo, qu)
            list_groupdelays(TD1, TD2, MA1, MA2)

            if args.re != args.zo:
                print('Filter Termination and Line Impedance Mismatch Results (QU={})'.format(qu))

                gcopy = g.copy()
                gcopy[0] *= args.zo / args.re
                MA1 = groupdelay_maqu(gcopy, bw, fo, qu)
                TD1 = groupdelay_tdqu(gcopy, bw, fo, qu)
                gcopy = g[::-1]
                gcopy[0] *= args.zo / args.re
                MA2 = groupdelay_maqu(gcopy, bw, fo, qu)
                TD2 = groupdelay_tdqu(gcopy, bw, fo, qu)
                list_groupdelays(TD1, TD2, MA1, MA2)

                QE1 = fo / bw * qk[0] * args.zo / args.re
                QE2 = fo / bw * qk[-1] * args.zo / args.re
                print('  Line Impedance           {:15.3f} ohm'.format(args.zo))
                print('  Termination Resistance   {:15.3f} ohm'.format(args.re))
                fn = fn_nodal_transmission(qk, bw, fo, re=args.zo/args.re)
                td = groupdelay(fn, fo, qu)
                print('  Transmission Delay       {:15.3f} ns'.format(td * 1e9))
                il = -db(fn(fo, qu))
                print('  Insertion Loss           {:15.3f} dB'.format(il))
                print('  Empirical QE{}            {:15.3f}'.format(1, QE1))
                print('  Empirical QE{}            {:15.3f}'.format(N, QE2))

            if args.validate:
                print('Validation')
                qe1, qu1 = qequ_groupdelay(fo, TD1[0], MA1[0])
                qe2, qu2 = qequ_groupdelay(fo, TD2[0], MA2[0])
                print('  QU{}={:14.6f}'.format(1, qu1))
                print('  QU{}={:14.6f}'.format(N, qu2))
                print('  QE{}={:14.6f}'.format(1, qe1))
                print('  QE{}={:14.6f}'.format(N, qe2))
                k12 = k12_groupdelay(fo, TD1[0], TD1[1], MA1[0])
                print('  K12={:14.6f}'.format(k12))
                k12 = k12_groupdelay(fo, TD2[0], TD2[1], MA2[0])
                print('  K{}{}={:14.6f}'.format(N-1, N, k12))

if __name__ == '__main__':
    args = parse_args()
    try:
        main()
    except KeyboardInterrupt:
        pass


import urllib.request              #import packages
from tkinter import *
from bs4 import BeautifulSoup
#=============GUI FORM==================

root=Tk()
root.geometry("1200x1200")
root.title("Current Weather")

TopFrame=Frame(root,width=1350,height=10,bd=14,relief='raise')
TopFrame.pack(side=TOP)

BottomFrame=Frame(root,width=350,height=50,bd=14,relief='raise')
BottomFrame.pack(side=BOTTOM)
LeftMidFrame=Frame(root,width=1350,height=1150,bd=14,relief="raise")
LeftMidFrame.pack(side=LEFT)

#======================TEXT FIELDS AND LEBLES===================
global city,weather;

city=StringVar()
weather=StringVar()
Title=Label(TopFrame,font=('arial',24,'bold'),text="Check the current temperature",bd=10,width=45,justify='center')
Title.grid(row=1,column=1)

enter=Label(LeftMidFrame,font=('arial',18,'bold'),text="Enter the name of a proper city in India :",bd=1,width=80,justify='left')
enter.grid(row=0,column=0)

cityname=Entry(LeftMidFrame,font=('arial',14,'bold'),textvariable=city,bd=10,width=30,relief="sunken")
cityname.grid(row=1,column=0)
output=Label(LeftMidFrame,font=('arial',14,'bold'),textvariable=weather,bd=10,width=50,relief="sunken")
output.grid(row=2,column=0)

#============function of button=============

def set_val():
    city=cityname.get()
    print(city)
    quote_page = "https://www.timeanddate.com/weather/india/"+city
    page = urllib.request.urlopen(quote_page)
    soup = BeautifulSoup(page, "html.parser")                                    #====scraping=======#
    name_box = soup.find("header",{'class':'fixed'})
    name = name_box.text.strip()
    temp_box = soup.find("div",{'class':'h2'})
    temp = temp_box.text.strip()
    outlook = soup.find("p")
    out = outlook.text.strip()
    time = soup.find("span",{'id':'wtct'})
    ti= time.text.strip()
    total=name+"\n"+"temperature :"+temp+"\n"+"Outlook :"+out+"\n"+"current date and time :"+ti
    print(total)
    weather.set(total)                                                                #---set value to the text field

btn=Button(BottomFrame,font=('arial',18,'bold'),text='Submit',bd=2,command=set_val)
btn.grid(row=1,column=0)