import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
import os

#from OneHotCategorical import *
#from RelaxedOneHotCategorical import *

slim=tf.contrib.slim
Bernoulli = tf.contrib.distributions.Bernoulli
OneHotCategorical = tf.contrib.distributions.OneHotCategorical
RelaxedOneHotCategorical = tf.contrib.distributions.RelaxedOneHotCategorical
Categorical = tf.contrib.distributions.Categorical
# In[ ]:

batch_size = 100
tau0=1.0 # initial temperature

K=10 # number of classes
N=200//K # number of categorical distributions
straight_through=True # if True, use Straight-through Gumbel-Softmax
kl_type='relaxed' # choose between ('relaxed', 'categorical')
learn_temp=False

# In[ ]:

x0 = tf.placeholder(tf.float32, shape=(batch_size,784), name='x')
x = tf.to_float(x0 > .5)
#net = tf.cast(tf.random_uniform(tf.shape(x)) < x, x.dtype) # dynamic binarization
net = slim.stack(x,slim.fully_connected,[512,256])
logits_y = tf.reshape(slim.fully_connected(net,K*N,activation_fn=None),[-1,N,K])
tau = tf.Variable(tau0,name="temperature",trainable=learn_temp)
q_y = RelaxedOneHotCategorical(tau,logits_y)
y = q_y.sample()
if straight_through:
  y_hard = tf.cast(tf.one_hot(tf.argmax(y,-1),K), y.dtype)
  y = tf.stop_gradient(y_hard - y) + y
net1 = slim.flatten(y)
net1 = slim.stack(net1,slim.fully_connected,[256,512])
logits_x = slim.fully_connected(net1,784,activation_fn=None)
p_x = Bernoulli(logits=logits_x)

recons = tf.reduce_sum(p_x.log_prob(x),1)
logits_py = tf.ones_like(logits_y) * 1./K #uniform

if kl_type=='categorical' or straight_through:
  # Analytical KL with Categorical prior
  p_cat_y = Categorical(logits=logits_py)
  q_cat_y = Categorical(logits=logits_y)
  KL_qp = tf.contrib.distributions.kl(q_cat_y, p_cat_y)
else:
  # Monte Carlo KL with Relaxed prior
  p_y = RelaxedOneHotCategorical(tau,logits=logits_py)
  KL_qp = q_y.log_prob(y) - p_y.log_prob(y)

lr=tf.constant(0.0001)

KL = tf.reduce_sum(KL_qp,1)
mean_recons = tf.reduce_mean(recons)
mean_KL = tf.reduce_mean(KL)
loss = -tf.reduce_mean(recons - KL)

train_op=tf.train.AdamOptimizer(learning_rate=lr).minimize(loss)
init_op=tf.global_variables_initializer()

#%%
mnist = input_data.read_data_sets(os.getcwd()+'/MNIST', one_hot=True)
train_data = mnist.train
test_data = mnist.test
valid_data = mnist.validation

directory = os.getcwd()+'/discrete_out/'
if not os.path.exists(directory):
    os.makedirs(directory)

total_points = mnist.train.num_examples
total_batch = int(total_points / batch_size)
total_test_batch = int(mnist.test.num_examples / batch_size)
total_valid_batch = int(mnist.validation.num_examples / batch_size)

training_epochs = 1000
display_step = total_batch

#%%
def get_loss(sess,data,total_batch):
    cost_eval = []
    for j in range(total_batch):
        xs,_ = data.next_batch(batch_size)
        cost_eval.append(sess.run(KL-recons,{x:xs}))
    return np.mean(cost_eval)

EXPERIMENT = 'MNIST_Cat_VAE_gumbel'
print('Training stats....',EXPERIMENT)

sess=tf.InteractiveSession()
sess.run(init_op)
record = [];step = 0

import time
start = time.time()
COUNT=[]; COST=[]; TIME=[];COST_TEST=[];COST_VALID=[];epoch_list=[];time_list=[]

for epoch in range(training_epochs):
    np_lr = 0.0001
    for i in range(total_batch):
        train_xs,_ = train_data.next_batch(batch_size)
        _,cost,_ = sess.run([train_op,loss,tau],{x0:train_xs,lr:np_lr})
        record.append(cost)
        step += 1

    if epoch%1 == 0:
        valid_loss = get_loss(sess,valid_data,total_valid_batch)
        COUNT.append(step); COST.append(np.mean(record)); TIME.append(time.time()-start)
        COST_VALID.append(valid_loss)
        print(epoch,'valid_cost=',valid_loss,'with std=',np.std(record))
    if epoch%5 == 0:
        COST_TEST.append(get_loss(sess,test_data,total_test_batch))
        epoch_list.append(epoch)
        time_list.append(time.time()-start)
    record=[]

#after training, calculate ELBO on training and testing respectively
cost_test = get_loss(sess,test_data,total_test_batch)
print("Final test elbo per point is", np.mean(cost_test))

cost_train = get_loss(sess,train_data,total_batch)
print("Final train elbo per point is", np.mean(cost_train))

print(EXPERIMENT)

import cPickle
all_ = [COUNT,COST,TIME,COST_TEST,COST_VALID,epoch_list,time_list]
cPickle.dump(all_, open(directory+EXPERIMENT, 'w'))


import os
import time
from hrc import HRCAuto
from hhparser import HHParser

DIR = '/mnt/hands'
RESULT_DIR = '~/1results'

def watch_directory(path):

    while True:
        dir_contents = set(os.listdir(DIR))

        time.sleep(2)
        current_dir_contents = set(os.listdir(DIR))
        diff = current_dir_contents - dir_contents
        if diff:
            for d in diff:
                yield os.path.join(DIR, d)

def read_hh_files(files):
    for file in files:
        with open(file, encoding='utf-8') as f:
            try:
                s = f.read()
                s = s.split('\n\n')
                for ss in s:
                    if ss == '\n\n' or ss == None or ss == '':
                        continue
                    yield ss

            except:
                continue

def update_index():
    pass

calc = HRCAuto()
for history in read_hh_files(watch_directory(DIR)):

    hand = HHParser(history)
    print(hand)
    fn = os.path.join(RESULT_DIR, '-'.join([hand.tid, hand.hid, hand.hero_cards]))
    calc.calculate_basic(hand.hand_history, fn)


# -*- coding: utf-8 -*-
"""
This file contains the Qudi hardware implementation for the Red Pitaya counter.

Qudi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Qudi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Qudi. If not, see <http://www.gnu.org/licenses/>.

Copyright (c) the Qudi Developers. See the COPYRIGHT.txt file at the
top-level directory of this distribution and at <https://github.com/Ulm-IQO/qudi/>
"""

import numpy as np

import socket
import random
import time
import visa

from core.module import Base
from interface.slow_counter_interface import SlowCounterInterface
from interface.slow_counter_interface import SlowCounterConstraints
from interface.slow_counter_interface import CountingMode
from interface.odmr_counter_interface import ODMRCounterInterface

class RedPitayaCounter(Base, SlowCounterInterface, ODMRCounterInterface):

    """This is the Interface class to define the controls for the Red Pitaya counter.
    """
    _modclass = 'RedPitayaCounter'
    _modtype = 'hardware'

    def __init__(self, config, **kwargs):
        super().__init__(config=config, **kwargs)

        self.log.info('The following configuration was found.')

        # checking for the right configuration
        for key in config.keys():
            self.log.info('{0}: {1}'.format(key, config[key]))

    def on_activate(self):
        """ Initialisation performed during activation of the module.
        """

        config = self.getConfiguration()

        if 'ip_address' in config.keys():
            self._ip_address = config['ip_address']
        else:
            self.log.error('No paramerter "ip_address" configured in '
                    'Red Pitaya Counter. This parameter is mandatory.')

        if 'clock_frequency' in config.keys():
            self._clock_frequency=config['clock_frequency']
        else:
            self._clock_frequency = 100
            self.log.warning('No parameter "clock_frequency" configured in '
                    'Red Pitaya Counter, taking the default value of {0} Hz '
                    'instead.'.format(self._clock_frequency))

        if 'samples_number' in config.keys():
            self._samples_number = config['samples_number']
        else:
            self._samples_number = 1
            self.log.warning('No parameter "samples_number" configured in '
                    'Red Pitaya Counter, taking the default value of {0} '
                    'instead.'.format(self._samples_number))

        self.source_channels = 2
        self.odmr_length = 100

        self._connect()

        # Initialize Counter
        # NOTE: Do not do that here! This messes up the counts (why?)
        #self._send_command('COUNT:StopOutput')
        #self._send_command('COUNT:time {:f}'.format(1./self._clock_frequency))
        #self.log.info("Red Pitaya Counter: COUNT:time is "+str(1./self._clock_frequency))
        #self._send_command('COUNT:CountOutput 1000000,0')

        self.log.info('RedPitayaCounter initialized and connected to hardware.')
        return

    def _connect(self):
        try:
            self._ip_connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self._ip_connection.connect((self._ip_address, 5000))
        except:
            self.log.error('This is RedPitayaCounter: could not connect to RedPitaya on IP address {0}'
                           ''.format(self._ip_address))
            raise

    def _disconnect(self):
        self._ip_connection.shutdown(socket.SHUT_RDWR)
        self._ip_connection.close()

    def on_deactivate(self):
        """ Deinitialisation performed during deactivation of the module.
        """
        self._disconnect()
        return

    def get_constraints(self):
        """ Return a constraints class for the slow counter."""
        constraints = SlowCounterConstraints()
        constraints.min_count_frequency = 5e-5
        constraints.max_count_frequency = 5e5
        constraints.counting_mode = [
            CountingMode.CONTINUOUS,
            CountingMode.GATED,
            CountingMode.FINITE_GATED]

        return constraints

    def get_odmr_channels(self):
        """ Return a list of channel names.

        @return list(str): channels recorded during ODMR measurement
        """
        return ['Counts']

    def set_up_clock(self, clock_frequency=None, clock_channel=None):
        """ Configures the hardware clock of the NiDAQ card to give the timing.

        @param float clock_frequency: if defined, this sets the frequency of the clock
        @param string clock_channel: if defined, this is the physical channel of the clock

        @return int: error code (0:OK, -1:error)
        """

        # NOTE: Do not do that here, it messes up the counts! (why?)
        """
        if clock_frequency is not None and float(clock_frequency) != self._clock_frequency:
            self._clock_frequency = float(clock_frequency)
            self._send_command('COUNT:StopOutput')
            self._send_command('COUNT:time {:f}'.format(1./self._clock_frequency))
            self.log.info("Red Pitaya Counter: COUNT:time is "+str(1./self._clock_frequency))
            self._send_command('COUNT:CountOutput 1000000,0')
        """

        self.log.info("Count frequency set to %fHz (=%fs)" % (clock_frequency, 1./clock_frequency))
        self._send_command('COUNTER:TIME {:f}'.format(1./clock_frequency))
        self._clock_frequency = clock_frequency

        return 0

    def set_up_counter(self,
                       counter_channels=None,
                       sources=None,
                       clock_channel=None,
                       counter_buffer=None):
        """ Configures the actual counter with a given clock.

        @param string counter_channel: if defined, this is the physical channel of the counter
        @param string photon_source: if defined, this is the physical channel where the photons are to count from
        @param string clock_channel: if defined, this specifies the clock for the counter

        @return int: error code (0:OK, -1:error)
        """

        return 0

    def get_counter(self, samples=None):
        """ Returns the current counts per second of the counter.

        @param int samples: if defined, number of samples to read in one go

        @return float: the photon counts per second
        """

        if samples is None:
            samples = int(self._samples_number)
        else:
            samples = int(samples)

        #self._count_time = float(self._query_command("COUNT:time?"))
        count_data = np.empty([self.source_channels+1, samples], dtype=np.uint32)
        for i in range(samples):
            counts = [float(x) for x in self._query_command('COUNTER:COUNTS?').split(',')]

            counts += [sum(counts)]
            if len(counts) < self.source_channels:
                self.log.error('Red Pitaya Counter got counts from {0} channels, but {1} '
                               'were expected configured.'.format(len(counts), self.source_channels))
                return 0

            for j in range(self.source_channels+1):
                count_data[j, i] = counts[j]

        return count_data

    def get_counter_channels(self):
        """ Returns the list of counter channel names.
        @return tuple(str): channel names
        Most methods calling this might just care about the number of channels, though.
        """
        return ['APD{0}'.format(i) for i in range(self.source_channels)]+["Sum"]

    def _check_error_response(self, resp):
        if resp.startswith("ERR:"):
            raise RuntimeError(resp[4:].strip())

    def _send_command(self, cmd):
        self._query_command(cmd)

    def _query_command(self, cmd):
        try:
            if not cmd.endswith("\r\n"):
                cmd += "\r\n"
            cmd = cmd.encode("ascii")

            # Sending command
            totalsent = 0
            while totalsent < len(cmd):
                sent = self._ip_connection.send(cmd[totalsent:])
                if sent == 0:
                    self.log.error("Connection error to redpitaya...Reconnecting")
                    self._connect()
                else:
                    totalsent += sent

            # Receiving response
            chunks = []
            while True:
                chunk = self._ip_connection.recv(2048)
                chunks.append(chunk)
                if chunk == b'':
                    self.log.error("Connection error to redpitaya...Reconnecting")
                    self._connect()
                elif len(chunk) >= 1 and chunk[-1] == 0xa and chunk[-2] == 0xd:
                    break
            resp = b''.join(chunks).decode("ascii")

            self._check_error_response(resp)
            return resp
        except:
            raise

    def close_counter(self):
        """ Closes the counter and cleans up afterwards.

        @return int: error code (0:OK, -1:error)
        """

        return 0

    def close_clock(self,power=0):
        """ Closes the clock and cleans up afterwards.

        @return int: error code (0:OK, -1:error)
        """

        return 0

    #### ODMR counter interface

    def set_up_odmr_clock(self, clock_frequency=None, clock_channel=None):
        """ Configures the hardware clock of the NiDAQ card to give the timing.
        @param float clock_frequency: if defined, this sets the frequency of the
                                      clock
        @param str clock_channel: if defined, this is the physical channel of
                                  the clock
        @return int: error code (0:OK, -1:error)
        """
        return self.set_up_clock(clock_frequency, clock_channel)

    def set_up_odmr(self, counter_channel=None, photon_source=None,
                    clock_channel=None, odmr_trigger_channel=None):
        """ Configures the actual counter with a given clock.
        @param str counter_channel: if defined, this is the physical channel of
                                    the counter
        @param str photon_source: if defined, this is the physical channel where
                                  the photons are to count from
        @param str clock_channel: if defined, this specifies the clock for the
                                  counter
        @param str odmr_trigger_channel: if defined, this specifies the trigger
                                         output for the microwave
        @return int: error code (0:OK, -1:error)
        """
        return 0

    def set_odmr_length(self, length=100):
        """Set up the trigger sequence for the ODMR and the triggered microwave.
        @param int length: length of microwave sweep in pixel
        @return int: error code (0:OK, -1:error)
        """
        self._odmr_length = length
        return 0

    def count_odmr(self, length = 100):
        """ Sweeps the microwave and returns the counts on that sweep.
        @param int length: length of microwave sweep in pixel
        @return float[]: the photon counts per second
        """
        self.set_odmr_length(length)
        counts = self.get_counter(length)
        return counts[0,:]+counts[1,:] # Return the sum of APD1+APD2

    def close_odmr(self):
        """ Close the odmr and clean up afterwards.
        @return int: error code (0:OK, -1:error)
        """
        return 0

    def close_odmr_clock(self):
        """ Close the odmr and clean up afterwards.
        @return int: error code (0:OK, -1:error)
        """
        return 0


from __future__ import print_function
import json, itertools
from binascii import hexlify
import requests
from six.moves.urllib_parse import urlencode
from twisted.trial import unittest
from twisted.internet import protocol, reactor, defer
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.internet.threads import deferToThread
from twisted.internet.endpoints import clientFromString, connectProtocol
from twisted.web.client import getPage, Agent, readBody
from autobahn.twisted import websocket
from .. import __version__
from .common import ServerBase
from ..server import rendezvous, transit_server
from ..twisted.eventsource import EventSource

class Reachable(ServerBase, unittest.TestCase):

    def test_getPage(self):
        # client.getPage requires bytes URL, returns bytes
        url = self.relayurl.replace("wormhole-relay/", "").encode("ascii")
        d = getPage(url)
        def _got(res):
            self.failUnlessEqual(res, b"Wormhole Relay\n")
        d.addCallback(_got)
        return d

    def test_agent(self):
        url = self.relayurl.replace("wormhole-relay/", "").encode("ascii")
        agent = Agent(reactor)
        d = agent.request(b"GET", url)
        def _check(resp):
            self.failUnlessEqual(resp.code, 200)
            return readBody(resp)
        d.addCallback(_check)
        def _got(res):
            self.failUnlessEqual(res, b"Wormhole Relay\n")
        d.addCallback(_got)
        return d

    def test_requests(self):
        # requests requires bytes URL, returns unicode
        url = self.relayurl.replace("wormhole-relay/", "")
        def _get(url):
            r = requests.get(url)
            r.raise_for_status()
            return r.text
        d = deferToThread(_get, url)
        def _got(res):
            self.failUnlessEqual(res, "Wormhole Relay\n")
        d.addCallback(_got)
        return d

def unjson(data):
    return json.loads(data.decode("utf-8"))

class WebAPI(ServerBase, unittest.TestCase):
    def build_url(self, path, appid, channelid):
        url = self.relayurl+path
        queryargs = []
        if appid:
            queryargs.append(("appid", appid))
        if channelid:
            queryargs.append(("channelid", channelid))
        if queryargs:
            url += "?" + urlencode(queryargs)
        return url

    def get(self, path, appid=None, channelid=None):
        url = self.build_url(path, appid, channelid)
        d = getPage(url.encode("ascii"))
        d.addCallback(unjson)
        return d

    def post(self, path, data):
        url = self.relayurl+path
        d = getPage(url.encode("ascii"), method=b"POST",
                    postdata=json.dumps(data).encode("utf-8"))
        d.addCallback(unjson)
        return d

    def check_welcome(self, data):
        self.failUnlessIn("welcome", data)
        self.failUnlessEqual(data["welcome"], {"current_version": __version__})

    def test_allocate_1(self):
        d = self.get("list", "app1")
        def _check_list_1(data):
            self.check_welcome(data)
            self.failUnlessEqual(data["channelids"], [])
        d.addCallback(_check_list_1)

        d.addCallback(lambda _: self.post("allocate", {"appid": "app1",
                                                       "side": "abc"}))
        def _allocated(data):
            data.pop("sent", None)
            self.failUnlessEqual(set(data.keys()),
                                 set(["welcome", "channelid"]))
            self.failUnlessIsInstance(data["channelid"], int)
            self.cid = data["channelid"]
        d.addCallback(_allocated)

        d.addCallback(lambda _: self.get("list", "app1"))
        def _check_list_2(data):
            self.failUnlessEqual(data["channelids"], [self.cid])
        d.addCallback(_check_list_2)

        d.addCallback(lambda _: self.post("deallocate",
                                          {"appid": "app1",
                                           "channelid": str(self.cid),
                                           "side": "abc"}))
        def _check_deallocate(res):
            self.failUnlessEqual(res["status"], "deleted")
        d.addCallback(_check_deallocate)

        d.addCallback(lambda _: self.get("list", "app1"))
        def _check_list_3(data):
            self.failUnlessEqual(data["channelids"], [])
        d.addCallback(_check_list_3)

        return d

    def test_allocate_2(self):
        d = self.post("allocate", {"appid": "app1", "side": "abc"})
        def _allocated(data):
            self.cid = data["channelid"]
        d.addCallback(_allocated)

        # second caller increases the number of known sides to 2
        d.addCallback(lambda _: self.post("add",
                                          {"appid": "app1",
                                           "channelid": str(self.cid),
                                           "side": "def",
                                           "phase": "1",
                                           "body": ""}))

        d.addCallback(lambda _: self.get("list", "app1"))
        d.addCallback(lambda data:
                      self.failUnlessEqual(data["channelids"], [self.cid]))

        d.addCallback(lambda _: self.post("deallocate",
                                          {"appid": "app1",
                                           "channelid": str(self.cid),
                                           "side": "abc"}))
        d.addCallback(lambda res:
                      self.failUnlessEqual(res["status"], "waiting"))

        d.addCallback(lambda _: self.post("deallocate",
                                          {"appid": "app1",
                                           "channelid": str(self.cid),
                                           "side": "NOT"}))
        d.addCallback(lambda res:
                      self.failUnlessEqual(res["status"], "waiting"))

        d.addCallback(lambda _: self.post("deallocate",
                                          {"appid": "app1",
                                           "channelid": str(self.cid),
                                           "side": "def"}))
        d.addCallback(lambda res:
                      self.failUnlessEqual(res["status"], "deleted"))

        d.addCallback(lambda _: self.get("list", "app1"))
        d.addCallback(lambda data:
                      self.failUnlessEqual(data["channelids"], []))

        return d

    UPGRADE_ERROR = "Sorry, you must upgrade your client to use this server."
    def test_old_allocate(self):
        # 0.4.0 used "POST /allocate/SIDE".
        # 0.5.0 replaced it with "POST /allocate".
        # test that an old client gets a useful error message, not a 404.
        d = self.post("allocate/abc", {})
        def _check(data):
            self.failUnlessEqual(data["welcome"]["error"], self.UPGRADE_ERROR)
        d.addCallback(_check)
        return d

    def test_old_list(self):
        # 0.4.0 used "GET /list".
        # 0.5.0 replaced it with "GET /list?appid="
        d = self.get("list", {}) # no appid
        def _check(data):
            self.failUnlessEqual(data["welcome"]["error"], self.UPGRADE_ERROR)
        d.addCallback(_check)
        return d

    def test_old_post(self):
        # 0.4.0 used "POST /CID/SIDE/post/MSGNUM"
        # 0.5.0 replaced it with "POST /add (json body)"
        d = self.post("1/abc/post/pake", {})
        def _check(data):
            self.failUnlessEqual(data["welcome"]["error"], self.UPGRADE_ERROR)
        d.addCallback(_check)
        return d

    def add_message(self, message, side="abc", phase="1"):
        return self.post("add",
                         {"appid": "app1",
                          "channelid": str(self.cid),
                         "side": side,
                          "phase": phase,
                          "body": message})

    def parse_messages(self, messages):
        out = set()
        for m in messages:
            self.failUnlessEqual(sorted(m.keys()), sorted(["phase", "body"]))
            self.failUnlessIsInstance(m["phase"], type(u""))
            self.failUnlessIsInstance(m["body"], type(u""))
            out.add( (m["phase"], m["body"]) )
        return out

    def check_messages(self, one, two):
        # Comparing lists-of-dicts is non-trivial in python3 because we can
        # neither sort them (dicts are uncomparable), nor turn them into sets
        # (dicts are unhashable). This is close enough.
        self.failUnlessEqual(len(one), len(two), (one,two))
        for d in one:
            self.failUnlessIn(d, two)

    def test_message(self):
        # exercise POST /add
        d = self.post("allocate", {"appid": "app1", "side": "abc"})
        def _allocated(data):
            self.cid = data["channelid"]
        d.addCallback(_allocated)

        d.addCallback(lambda _: self.add_message("msg1A"))
        def _check1(data):
            self.check_welcome(data)
            self.failUnlessEqual(data["messages"],
                                 [{"phase": "1", "body": "msg1A"}])
        d.addCallback(_check1)
        d.addCallback(lambda _: self.get("get", "app1", str(self.cid)))
        d.addCallback(_check1)
        d.addCallback(lambda _: self.add_message("msg1B", side="def"))
        def _check2(data):
            self.check_welcome(data)
            self.failUnlessEqual(self.parse_messages(data["messages"]),
                                 set([("1", "msg1A"),
                                      ("1", "msg1B")]))
        d.addCallback(_check2)
        d.addCallback(lambda _: self.get("get", "app1", str(self.cid)))
        d.addCallback(_check2)

        # adding a duplicate message is not an error, is ignored by clients
        d.addCallback(lambda _: self.add_message("msg1B", side="def"))
        def _check3(data):
            self.check_welcome(data)
            self.failUnlessEqual(self.parse_messages(data["messages"]),
                                 set([("1", "msg1A"),
                                      ("1", "msg1B")]))
        d.addCallback(_check3)
        d.addCallback(lambda _: self.get("get", "app1", str(self.cid)))
        d.addCallback(_check3)

        d.addCallback(lambda _: self.add_message("msg2A", side="abc",
                                                 phase="2"))
        def _check4(data):
            self.check_welcome(data)
            self.failUnlessEqual(self.parse_messages(data["messages"]),
                                 set([("1", "msg1A"),
                                      ("1", "msg1B"),
                                      ("2", "msg2A"),
                                      ]))
        d.addCallback(_check4)
        d.addCallback(lambda _: self.get("get", "app1", str(self.cid)))
        d.addCallback(_check4)

        return d

    def test_watch_message(self):
        # exercise GET /get (the EventSource version)
        # this API is scheduled to be removed after 0.6.0
        return self._do_watch("get")

    def test_watch(self):
        # exercise GET /watch (the EventSource version)
        return self._do_watch("watch")

    def _do_watch(self, endpoint_name):
        d = self.post("allocate", {"appid": "app1", "side": "abc"})
        def _allocated(data):
            self.cid = data["channelid"]
            url = self.build_url(endpoint_name, "app1", self.cid)
            self.o = OneEventAtATime(url, parser=json.loads)
            return self.o.wait_for_connection()
        d.addCallback(_allocated)
        d.addCallback(lambda _: self.o.wait_for_next_event())
        def _check_welcome(ev):
            eventtype, data = ev
            self.failUnlessEqual(eventtype, "welcome")
            self.failUnlessEqual(data, {"current_version": __version__})
        d.addCallback(_check_welcome)
        d.addCallback(lambda _: self.add_message("msg1A"))
        d.addCallback(lambda _: self.o.wait_for_next_event())
        def _check_msg1(ev):
            eventtype, data = ev
            self.failUnlessEqual(eventtype, "message")
            data.pop("sent", None)
            self.failUnlessEqual(data, {"phase": "1", "body": "msg1A"})
        d.addCallback(_check_msg1)

        d.addCallback(lambda _: self.add_message("msg1B"))
        d.addCallback(lambda _: self.add_message("msg2A", phase="2"))
        d.addCallback(lambda _: self.o.wait_for_next_event())
        def _check_msg2(ev):
            eventtype, data = ev
            self.failUnlessEqual(eventtype, "message")
            data.pop("sent", None)
            self.failUnlessEqual(data, {"phase": "1", "body": "msg1B"})
        d.addCallback(_check_msg2)
        d.addCallback(lambda _: self.o.wait_for_next_event())
        def _check_msg3(ev):
            eventtype, data = ev
            self.failUnlessEqual(eventtype, "message")
            data.pop("sent", None)
            self.failUnlessEqual(data, {"phase": "2", "body": "msg2A"})
        d.addCallback(_check_msg3)

        d.addCallback(lambda _: self.o.close())
        d.addCallback(lambda _: self.o.wait_for_disconnection())
        return d

class OneEventAtATime:
    def __init__(self, url, parser=lambda e: e):
        self.parser = parser
        self.d = None
        self._connected = False
        self.connected_d = defer.Deferred()
        self.disconnected_d = defer.Deferred()
        self.events = []
        self.es = EventSource(url, self.handler, when_connected=self.connected)
        d = self.es.start()
        d.addBoth(self.disconnected)

    def close(self):
        self.es.cancel()

    def wait_for_next_event(self):
        assert not self.d
        if self.events:
            event = self.events.pop(0)
            return defer.succeed(event)
        self.d = defer.Deferred()
        return self.d

    def handler(self, eventtype, data):
        event = (eventtype, self.parser(data))
        if self.d:
            assert not self.events
            d,self.d = self.d,None
            d.callback(event)
            return
        self.events.append(event)

    def wait_for_connection(self):
        return self.connected_d
    def connected(self):
        self._connected = True
        self.connected_d.callback(None)

    def wait_for_disconnection(self):
        return self.disconnected_d
    def disconnected(self, why):
        if not self._connected:
            self.connected_d.errback(why)
        self.disconnected_d.callback((why,))

class WSClient(websocket.WebSocketClientProtocol):
    def __init__(self):
        websocket.WebSocketClientProtocol.__init__(self)
        self.events = []
        self.errors = []
        self.d = None
        self.ping_counter = itertools.count(0)
    def onOpen(self):
        self.factory.d.callback(self)
    def onMessage(self, payload, isBinary):
        assert not isBinary
        event = json.loads(payload.decode("utf-8"))
        if event["type"] == "error":
            self.errors.append(event)
        if self.d:
            assert not self.events
            d,self.d = self.d,None
            d.callback(event)
            return
        self.events.append(event)

    def next_event(self):
        assert not self.d
        if self.events:
            event = self.events.pop(0)
            return defer.succeed(event)
        self.d = defer.Deferred()
        return self.d

    def send(self, mtype, **kwargs):
        kwargs["type"] = mtype
        payload = json.dumps(kwargs).encode("utf-8")
        self.sendMessage(payload, False)

    @inlineCallbacks
    def sync(self):
        ping = next(self.ping_counter)
        self.send("ping", ping=ping)
        # queue all messages until the pong, then put them back
        old_events = []
        while True:
            ev = yield self.next_event()
            if ev["type"] == "pong" and ev["pong"] == ping:
                self.events = old_events + self.events
                returnValue(None)
            old_events.append(ev)

class WSFactory(websocket.WebSocketClientFactory):
    protocol = WSClient

class WSClientSync(unittest.TestCase):
    # make sure my 'sync' method actually works

    @inlineCallbacks
    def test_sync(self):
        sent = []
        c = WSClient()
        def _send(mtype, **kwargs):
            sent.append( (mtype, kwargs) )
        c.send = _send
        def add(mtype, **kwargs):
            kwargs["type"] = mtype
            c.onMessage(json.dumps(kwargs).encode("utf-8"), False)
        # no queued messages
        sunc = []
        d = c.sync()
        d.addBoth(sunc.append)
        self.assertEqual(sent, [("ping", {"ping": 0})])
        self.assertEqual(sunc, [])
        add("pong", pong=0)
        yield d
        self.assertEqual(c.events, [])

        # one,two,ping,pong
        add("one")
        add("two", two=2)
        sunc = []
        d = c.sync()
        d.addBoth(sunc.append)
        add("pong", pong=1)
        yield d
        m = yield c.next_event()
        self.assertEqual(m["type"], "one")
        m = yield c.next_event()
        self.assertEqual(m["type"], "two")
        self.assertEqual(c.events, [])

        # one,ping,two,pong
        add("one")
        sunc = []
        d = c.sync()
        d.addBoth(sunc.append)
        add("two", two=2)
        add("pong", pong=2)
        yield d
        m = yield c.next_event()
        self.assertEqual(m["type"], "one")
        m = yield c.next_event()
        self.assertEqual(m["type"], "two")
        self.assertEqual(c.events, [])

        # ping,one,two,pong
        sunc = []
        d = c.sync()
        d.addBoth(sunc.append)
        add("one")
        add("two", two=2)
        add("pong", pong=3)
        yield d
        m = yield c.next_event()
        self.assertEqual(m["type"], "one")
        m = yield c.next_event()
        self.assertEqual(m["type"], "two")
        self.assertEqual(c.events, [])

class WebSocketAPI(ServerBase, unittest.TestCase):
    def setUp(self):
        self._clients = []
        return ServerBase.setUp(self)

    def tearDown(self):
        for c in self._clients:
            c.transport.loseConnection()
        return ServerBase.tearDown(self)

    @inlineCallbacks
    def make_client(self):
        f = WSFactory(self.rdv_ws_url)
        f.d = defer.Deferred()
        reactor.connectTCP("127.0.0.1", self.rdv_ws_port, f)
        c = yield f.d
        self._clients.append(c)
        returnValue(c)

    def check_welcome(self, data):
        self.failUnlessIn("welcome", data)
        self.failUnlessEqual(data["welcome"], {"current_version": __version__})

    @inlineCallbacks
    def test_welcome(self):
        c1 = yield self.make_client()
        msg = yield c1.next_event()
        self.check_welcome(msg)
        self.assertEqual(self._rendezvous._apps, {})

    @inlineCallbacks
    def test_allocate_1(self):
        c1 = yield self.make_client()
        msg = yield c1.next_event()
        self.check_welcome(msg)
        c1.send(u"bind", appid=u"appid", side=u"side")
        yield c1.sync()
        self.assertEqual(list(self._rendezvous._apps.keys()), [u"appid"])
        app = self._rendezvous.get_app(u"appid")
        self.assertEqual(app.get_allocated(), set())
        c1.send(u"list")
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], u"channelids")
        self.assertEqual(msg["channelids"], [])

        c1.send(u"allocate")
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], u"allocated")
        cid = msg["channelid"]
        self.failUnlessIsInstance(cid, int)
        self.assertEqual(app.get_allocated(), set([cid]))
        channel = app.get_channel(cid)
        self.assertEqual(channel.get_messages(), [])

        c1.send(u"list")
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], u"channelids")
        self.assertEqual(msg["channelids"], [cid])

        c1.send(u"deallocate")
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], u"deallocated")
        self.assertEqual(msg["status"], u"deleted")
        self.assertEqual(app.get_allocated(), set())

        c1.send(u"list")
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], u"channelids")
        self.assertEqual(msg["channelids"], [])

    @inlineCallbacks
    def test_allocate_2(self):
        c1 = yield self.make_client()
        msg = yield c1.next_event()
        self.check_welcome(msg)
        c1.send(u"bind", appid=u"appid", side=u"side")
        yield c1.sync()
        app = self._rendezvous.get_app(u"appid")
        self.assertEqual(app.get_allocated(), set())
        c1.send(u"allocate")
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], u"allocated")
        cid = msg["channelid"]
        self.failUnlessIsInstance(cid, int)
        self.assertEqual(app.get_allocated(), set([cid]))
        channel = app.get_channel(cid)
        self.assertEqual(channel.get_messages(), [])

        # second caller increases the number of known sides to 2
        c2 = yield self.make_client()
        msg = yield c2.next_event()
        self.check_welcome(msg)
        c2.send(u"bind", appid=u"appid", side=u"side-2")
        c2.send(u"claim", channelid=cid)
        c2.send(u"add", phase="1", body="")
        yield c2.sync()

        self.assertEqual(app.get_allocated(), set([cid]))
        self.assertEqual(channel.get_messages(), [{"phase": "1", "body": ""}])

        c1.send(u"list")
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], u"channelids")
        self.assertEqual(msg["channelids"], [cid])

        c2.send(u"list")
        msg = yield c2.next_event()
        self.assertEqual(msg["type"], u"channelids")
        self.assertEqual(msg["channelids"], [cid])

        c1.send(u"deallocate")
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], u"deallocated")
        self.assertEqual(msg["status"], u"waiting")

        c2.send(u"deallocate")
        msg = yield c2.next_event()
        self.assertEqual(msg["type"], u"deallocated")
        self.assertEqual(msg["status"], u"deleted")

        c2.send(u"list")
        msg = yield c2.next_event()
        self.assertEqual(msg["type"], u"channelids")
        self.assertEqual(msg["channelids"], [])

    @inlineCallbacks
    def test_allocate_and_claim(self):
        c1 = yield self.make_client()
        msg = yield c1.next_event()
        self.check_welcome(msg)
        c1.send(u"bind", appid=u"appid", side=u"side")
        c1.send(u"allocate")
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], u"allocated")
        cid = msg["channelid"]
        c1.send(u"claim", channelid=cid)
        yield c1.sync()
        # there should no error
        self.assertEqual(c1.errors, [])

    @inlineCallbacks
    def test_allocate_and_claim_different(self):
        c1 = yield self.make_client()
        msg = yield c1.next_event()
        self.check_welcome(msg)
        c1.send(u"bind", appid=u"appid", side=u"side")
        c1.send(u"allocate")
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], u"allocated")
        cid = msg["channelid"]
        c1.send(u"claim", channelid=cid+1)
        yield c1.sync()
        # that should signal an error
        self.assertEqual(len(c1.errors), 1, c1.errors)
        msg = c1.errors[0]
        self.assertEqual(msg["type"], "error")
        self.assertEqual(msg["error"], "Already bound to channelid %d" % cid)
        self.assertEqual(msg["orig"], {"type": "claim", "channelid": cid+1})

    @inlineCallbacks
    def test_message(self):
        c1 = yield self.make_client()
        msg = yield c1.next_event()
        self.check_welcome(msg)
        c1.send(u"bind", appid=u"appid", side=u"side")
        c1.send(u"allocate")
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], u"allocated")
        cid = msg["channelid"]
        app = self._rendezvous.get_app(u"appid")
        channel = app.get_channel(cid)
        self.assertEqual(channel.get_messages(), [])

        c1.send(u"watch")
        yield c1.sync()
        self.assertEqual(len(channel._listeners), 1)
        self.assertEqual(c1.events, [])

        c1.send(u"add", phase="1", body="msg1A")
        yield c1.sync()
        self.assertEqual(channel.get_messages(),
                         [{"phase": "1", "body": "msg1A"}])
        self.assertEqual(len(c1.events), 1) # echo should be sent right away
        msg = yield c1.next_event()
        self.assertEqual(msg["type"], "message")
        self.assertEqual(msg["message"], {"phase": "1", "body": "msg1A"})
        self.assertIn("sent", msg)
        self.assertIsInstance(msg["sent"], float)

        c1.send(u"add", phase="1", body="msg1B")
        c1.send(u"add", phase="2", body="msg2A")

        msg = yield c1.next_event()
        self.assertEqual(msg["type"], "message")
        self.assertEqual(msg["message"], {"phase": "1", "body": "msg1B"})

        msg = yield c1.next_event()
        self.assertEqual(msg["type"], "message")
        self.assertEqual(msg["message"], {"phase": "2", "body": "msg2A"})

        self.assertEqual(channel.get_messages(), [
            {"phase": "1", "body": "msg1A"},
            {"phase": "1", "body": "msg1B"},
            {"phase": "2", "body": "msg2A"},
            ])

        # second client should see everything
        c2 = yield self.make_client()
        msg = yield c2.next_event()
        self.check_welcome(msg)
        c2.send(u"bind", appid=u"appid", side=u"side")
        c2.send(u"claim", channelid=cid)
        # 'watch' triggers delivery of old messages, in temporal order
        c2.send(u"watch")

        msg = yield c2.next_event()
        self.assertEqual(msg["type"], "message")
        self.assertEqual(msg["message"], {"phase": "1", "body": "msg1A"})

        msg = yield c2.next_event()
        self.assertEqual(msg["type"], "message")
        self.assertEqual(msg["message"], {"phase": "1", "body": "msg1B"})

        msg = yield c2.next_event()
        self.assertEqual(msg["type"], "message")
        self.assertEqual(msg["message"], {"phase": "2", "body": "msg2A"})

        # adding a duplicate is not an error, and clients will ignore it
        c1.send(u"add", phase="2", body="msg2A")

        # the duplicate message *does* get stored, and delivered
        msg = yield c2.next_event()
        self.assertEqual(msg["type"], "message")
        self.assertEqual(msg["message"], {"phase": "2", "body": "msg2A"})

class Summary(unittest.TestCase):
    def test_summarize(self):
        c = rendezvous.Channel(None, None, None, None, False, None, None)
        A = rendezvous.ALLOCATE
        D = rendezvous.DEALLOCATE

        messages = [{"when": 1, "side": "a", "phase": A}]
        self.failUnlessEqual(c._summarize(messages, 2),
                             (1, "lonely", 1, None))

        messages = [{"when": 1, "side": "a", "phase": A},
                    {"when": 2, "side": "a", "phase": D, "body": "lonely"},
                    ]
        self.failUnlessEqual(c._summarize(messages, 3),
                             (1, "lonely", 2, None))

        messages = [{"when": 1, "side": "a", "phase": A},
                    {"when": 2, "side": "b", "phase": A},
                    {"when": 3, "side": "c", "phase": A},
                    ]
        self.failUnlessEqual(c._summarize(messages, 4),
                             (1, "crowded", 3, None))

        base = [{"when": 1, "side": "a", "phase": A},
                {"when": 2, "side": "a", "phase": "pake", "body": "msg1"},
                {"when": 10, "side": "b", "phase": "pake", "body": "msg2"},
                {"when": 11, "side": "b", "phase": "data", "body": "msg3"},
                {"when": 20, "side": "a", "phase": "data", "body": "msg4"},
                ]
        def make_moods(A_mood, B_mood):
            return base + [
                {"when": 21, "side": "a", "phase": D, "body": A_mood},
                {"when": 30, "side": "b", "phase": D, "body": B_mood},
                ]

        self.failUnlessEqual(c._summarize(make_moods("happy", "happy"), 41),
                             (1, "happy", 40, 9))

        self.failUnlessEqual(c._summarize(make_moods("scary", "happy"), 41),
                             (1, "scary", 40, 9))
        self.failUnlessEqual(c._summarize(make_moods("happy", "scary"), 41),
                             (1, "scary", 40, 9))

        self.failUnlessEqual(c._summarize(make_moods("lonely", "happy"), 41),
                             (1, "lonely", 40, 9))
        self.failUnlessEqual(c._summarize(make_moods("happy", "lonely"), 41),
                             (1, "lonely", 40, 9))

        self.failUnlessEqual(c._summarize(make_moods("errory", "happy"), 41),
                             (1, "errory", 40, 9))
        self.failUnlessEqual(c._summarize(make_moods("happy", "errory"), 41),
                             (1, "errory", 40, 9))

        # scary trumps other moods
        self.failUnlessEqual(c._summarize(make_moods("scary", "lonely"), 41),
                             (1, "scary", 40, 9))
        self.failUnlessEqual(c._summarize(make_moods("scary", "errory"), 41),
                             (1, "scary", 40, 9))

        # older clients don't send a mood
        self.failUnlessEqual(c._summarize(make_moods(None, None), 41),
                             (1, "quiet", 40, 9))
        self.failUnlessEqual(c._summarize(make_moods(None, "happy"), 41),
                             (1, "quiet", 40, 9))
        self.failUnlessEqual(c._summarize(make_moods(None, "happy"), 41),
                             (1, "quiet", 40, 9))
        self.failUnlessEqual(c._summarize(make_moods(None, "scary"), 41),
                             (1, "scary", 40, 9))

class Accumulator(protocol.Protocol):
    def __init__(self):
        self.data = b""
        self.count = 0
        self._wait = None
    def waitForBytes(self, more):
        assert self._wait is None
        self.count = more
        self._wait = defer.Deferred()
        self._check_done()
        return self._wait
    def dataReceived(self, data):
        self.data = self.data + data
        self._check_done()
    def _check_done(self):
        if self._wait and len(self.data) >= self.count:
            d = self._wait
            self._wait = None
            d.callback(self)
    def connectionLost(self, why):
        if self._wait:
            self._wait.errback(RuntimeError("closed"))

class Transit(ServerBase, unittest.TestCase):
    def test_blur_size(self):
        blur = transit_server.blur_size
        self.failUnlessEqual(blur(0), 0)
        self.failUnlessEqual(blur(1), 10e3)
        self.failUnlessEqual(blur(10e3), 10e3)
        self.failUnlessEqual(blur(10e3+1), 20e3)
        self.failUnlessEqual(blur(15e3), 20e3)
        self.failUnlessEqual(blur(20e3), 20e3)
        self.failUnlessEqual(blur(1e6), 1e6)
        self.failUnlessEqual(blur(1e6+1), 2e6)
        self.failUnlessEqual(blur(1.5e6), 2e6)
        self.failUnlessEqual(blur(2e6), 2e6)
        self.failUnlessEqual(blur(900e6), 900e6)
        self.failUnlessEqual(blur(1000e6), 1000e6)
        self.failUnlessEqual(blur(1050e6), 1100e6)
        self.failUnlessEqual(blur(1100e6), 1100e6)
        self.failUnlessEqual(blur(1150e6), 1200e6)

    @defer.inlineCallbacks
    def test_basic(self):
        ep = clientFromString(reactor, self.transit)
        a1 = yield connectProtocol(ep, Accumulator())
        a2 = yield connectProtocol(ep, Accumulator())

        token1 = b"\x00"*32
        a1.transport.write(b"please relay " + hexlify(token1) + b"\n")
        a2.transport.write(b"please relay " + hexlify(token1) + b"\n")

        # a correct handshake yields an ack, after which we can send
        exp = b"ok\n"
        yield a1.waitForBytes(len(exp))
        self.assertEqual(a1.data, exp)
        s1 = b"data1"
        a1.transport.write(s1)

        exp = b"ok\n"
        yield a2.waitForBytes(len(exp))
        self.assertEqual(a2.data, exp)

        # all data they sent after the handshake should be given to us
        exp = b"ok\n"+s1
        yield a2.waitForBytes(len(exp))
        self.assertEqual(a2.data, exp)

        a1.transport.loseConnection()
        a2.transport.loseConnection()

    @defer.inlineCallbacks
    def test_bad_handshake(self):
        ep = clientFromString(reactor, self.transit)
        a1 = yield connectProtocol(ep, Accumulator())

        token1 = b"\x00"*32
        # the server waits for the exact number of bytes in the expected
        # handshake message. to trigger "bad handshake", we must match.
        a1.transport.write(b"please DELAY " + hexlify(token1) + b"\n")

        exp = b"bad handshake\n"
        yield a1.waitForBytes(len(exp))
        self.assertEqual(a1.data, exp)

        a1.transport.loseConnection()

    @defer.inlineCallbacks
    def test_impatience(self):
        ep = clientFromString(reactor, self.transit)
        a1 = yield connectProtocol(ep, Accumulator())

        token1 = b"\x00"*32
        # sending too many bytes is impatience.
        a1.transport.write(b"please RELAY NOWNOW " + hexlify(token1) + b"\n")

        exp = b"impatient\n"
        yield a1.waitForBytes(len(exp))
        self.assertEqual(a1.data, exp)

        a1.transport.loseConnection()


from collections import Counter
from itertools import combinations
from operator import mul
from functools import reduce
from numpy import dot

def process_input(inputs):
    output = []
    for input in inputs:
        m, primes = input

        #sum primes
        sum_prime = 0
        prime_list = []
        for prime in primes:
            p, f = prime
            p = int(p)
            f = int(f)
            sum_prime += p*f
            for _ in range(f):
                prime_list.append(p)
        previous_solutions = set()
        max=0
        for i in range(len(prime_list)):
            for combination in combinations(prime_list, i+1):
                product = reduce(mul, combination, 1)
                if product>sum_prime:
                    continue #this is an obvious bad option, we skip
                elif product in previous_solutions:
                    continue #we've already explored it
                else:
                    c1 = Counter(prime_list)
                    c2 = Counter(combination)

                    diff = c1 - c2

                    if dot(list(diff.values()), list(diff.keys()))==product:
                        if product>max:
                            max = product

                    previous_solutions.add(product)
        output.append(max)

    return output

if __name__ == "__main__":
    T = int(input())
    inputs = []
    for _ in range(T):
        N = int(input())
        x = []
        for _ in range(N):

            str = input().split(' ')
            x.append(str)
        inputs.append([N,x])

    output_lines = process_input(inputs)

    i = 1
    for l in output_lines:
        print("Case #{}: {}".format(i, l))
        i += 1


import cv2
import numpy as np
import random

width = 501
height = 501
dim = (height, width, 3)
center = (int(dim[0]/2), int(dim[1]/2))
value = 255

# прави случаен избор на распоредот на нијансите на сликата
def rnd_shades(result, lst_occurr, lst_shades):
    if sum(lst_occurr) == 0:
        return result
    else:
        non_zeros = [i for i in range(len(lst_occurr)) if lst_occurr[i] != 0]
        i = random.choice(non_zeros)
        result.append(lst_shades[i])
        lst_occurr[i] -= 1
        return rnd_shades(result, lst_occurr, lst_shades)

# 'hue' - боја
# 'num_shades' - број на различни нијанси во сликата
# 'min_diff' - минимална разлика на заситеност меѓу нијансите
# 'min_occurr' - минимален број на појавувања од секоја нијанса
# 'max_occurr' - максимален број на појавувања од секоја нијанса
# 'smlst_rad' - радиусот на најмалиот круг
def create_image(hue, num_shades, min_diff, min_occurr, max_occurr, smlst_rad):
    shades_values = random.sample(range(0, 256, min_diff), num_shades)
    num_occurr = [random.randint(min_occurr, max_occurr)
                  for _ in range(num_shades)]
    num_circles = sum(num_occurr)
    print('Број на појавувања од секоја нијанса:')
    print(num_occurr)
    k = int((width/2-smlst_rad)/(num_circles-1))
    shades_values = rnd_shades([], num_occurr, shades_values)
    shades = [[hue, s, value] for s in shades_values]
    img = np.zeros(dim, dtype=np.uint8)
    print('Распоред на нијанси (од најнадворешниот круг на внатре):')
    print(shades_values)
    for i, sh in enumerate(shades):
        sh = np.array([[sh]], dtype=np.uint8)
        bgr_sh = cv2.cvtColor(sh, cv2.COLOR_HSV2BGR)
        bgr_sh = bgr_sh.flatten()
        col = (int(bgr_sh[0]), int(bgr_sh[1]), int(bgr_sh[2]))
        img = cv2.circle(img, center, int(width/2-i*k), col, -1, 5)
    cv2.imwrite('{}-{}.jpg'.format(hue, num_shades), img)

# 70 140 210
create_image(180, 4, 50, 2, 3, 60)


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import RandomizedSearchCV, GroupKFold
import xgboost as xgb
from scipy import stats
import pickle
from sklearn.model_selection import cross_val_score
from pathlib import Path
import matplotlib
import Model_XGBoost.model_utils as mu
mu.set_figure_art()
matplotlib.rc('text', usetex=True)
abs_path = Path(__file__).parent

file_name = 'ems_optimization_2.1_1000_yes.csv'
data_scenarios = pd.read_csv(abs_path.parents[0] / ('Model_optimization/' + file_name))
scenario_list = np.round(np.linspace(10, data_scenarios['Scenario'].max() + 1, 10))

# RETRAIN_ALL_MODELS = False
# if RETRAIN_ALL_MODELS:
#     model_results = {'best_model': list(),
#                      'mean_norm_rmse_fold': list(),
#                      'std_norm_rmse_fold': list()}
#     xgb_models_trained = list()
#     for scenario_number, scenario_threshold in enumerate(scenario_list):
#         print(f'Scenario: {scenario_number + 1} of {(len(scenario_list))} -- Scenario threshold: {scenario_threshold}')
#
#         idx = data_scenarios['Scenario'] <= scenario_threshold
#         data_scenarios_selected = data_scenarios.loc[idx,:]
#
#         columns_drop=['Scenario','v_1', ' storage_Q']
#         columns_predict=[' storage_P']
#         data_train_clean = data_scenarios_selected.drop(columns=columns_drop)
#
#         x_train = data_train_clean.drop(columns=columns_predict)
#         y_train = data_train_clean.loc[:, columns_predict]
#
#         param_dist = {'n_estimators': stats.randint(50, 1000),
#                       'learning_rate': stats.uniform(0.01, 0.1),
#                       'subsample': stats.uniform(0.5, 1),
#                       'max_depth': [3, 4, 5, 6, 7, 8, 9, 10],
#                       'colsample_bytree': stats.uniform(0.5, 0.45),
#                       'min_child_weight': [1, 2, 3]}
#
#         group_kfold_inner = GroupKFold(n_splits=10)
#         groups_train = data_scenarios_selected['Scenario']
#
#         xgb_regressor_model = xgb.XGBRegressor(objective='reg:squarederror')
#         xgb_regressor_search = RandomizedSearchCV(xgb_regressor_model,
#                                                   param_distributions=param_dist,
#                                                   n_iter=50,
#                                                   scoring='neg_mean_squared_error',
#                                                   cv=group_kfold_inner.split(x_train, groups=groups_train),
#                                                   refit=1,
#                                                   n_jobs=4)
#         xgb_regressor_search.fit(x_train, y_train)
#
#         # Calculate the normalized error and standard deviation from the folds
#         cv_results = pd.DataFrame(xgb_regressor_search.cv_results_)
#         cv_results = cv_results.sort_values(by='rank_test_score')
#
#         norm_factor = np.max(y_train.max()) - np.min(y_train.min())
#
#         folds_results = cv_results.filter(regex='split', axis=1)
#         norm_mse_splits = ((np.sqrt(-folds_results) / norm_factor) * 100)  # Values in percentage
#
#         mean_norm_mse = norm_mse_splits.mean(axis=1)[0]  # Mean performance of best model
#         std_norm_mse = norm_mse_splits.std(axis=1, ddof=0)[0]  # Standard deviation of performance of best model
#
#         # Save results
#         model_results['best_model'].append(xgb_regressor_search.best_estimator_)
#
#         model_results['mean_norm_rmse_fold'].append(mean_norm_mse)
#         model_results['std_norm_rmse_fold'].append(std_norm_mse)
#
#         print(f'Normalized RMSE (%): {round(mean_norm_mse, 2)}')
#
#     pickle.dump((scenario_list, model_results), open(abs_path / 'XGBoost_scenario_analysis.dat', 'wb'))
# else:
#     (scenario_list, model_results) = pickle.load(open(abs_path / 'XGBoost_scenario_analysis.dat', 'rb'))
#
# #%%
# fig, ax = plt.subplots(1, 1, figsize=(4, 2))
# ax.errorbar(scenario_list, model_results['mean_norm_rmse_fold'],
#             yerr=model_results['std_norm_rmse_fold'],
#             marker='o',  markersize=0, linewidth=0.3, color='k')
# ax.set_ylabel('NRMSE [\%]', fontsize=7)
# ax.set_xlabel('Number of scenarios', fontsize='small')
# ax.set_xticks(scenario_list)
# ax.tick_params(axis='both', labelsize=7)
# ax.set_xlim((0, 800))
# plt.tight_layout()
#
# #%% GET THE SAME ANALYSIS THAT THE PREVIOUS LOOP, BUT WITH ONLY ONE MODEL PARAMETER.
# # Analyze the parameters found by the best model found by the sweep of scenarios and random hyperparameter search:
# idx_ = np.argsort(model_results['mean_norm_rmse_fold'])
# opt_params = model_results['best_model'][idx_[0]].get_params()

# Load the best hyperparameters from the nested cross validation on 200 scenarios.
opt_params = pickle.load(open(abs_path / 'XGBoost_optimal_params.dat', 'rb'))
scenario_list_final = np.round(np.linspace(10, data_scenarios['Scenario'].max() + 1, 100))

RETRAIN_ALL_MODELS = False
if RETRAIN_ALL_MODELS:
    cross_val_results = {'norm_rmse_scores': list(),
                         'mean_norm_rmse_scores': list(),
                         'std_norm_rmse_scores': list()}

    # Re-train the model with the optimal parameters
    for scenario_number, scenario_threshold in enumerate(scenario_list_final):
        print(f'Scenario: {scenario_number + 1} of {(len(scenario_list_final))} -- Scenario threshold: {scenario_threshold}')

        idx = data_scenarios['Scenario'] <=scenario_threshold
        data_scenarios_selected = data_scenarios.loc[idx, :]

        columns_drop = ['Scenario', 'v_1', ' storage_Q']
        columns_predict = [' storage_P']
        data_train_clean = data_scenarios_selected.drop(columns=columns_drop)
        x_train = data_train_clean.drop(columns=columns_predict)
        y_train = data_train_clean.loc[:, columns_predict]

        group_kfold_inner = GroupKFold(n_splits=10)
        groups_train = data_scenarios_selected['Scenario']

        xgb_regressor_model = xgb.XGBRegressor(**opt_params)
        norm_factor = np.max(y_train.max()) - np.min(y_train.min())

        scores = cross_val_score(xgb_regressor_model,
                                 x_train, y_train,
                                 cv=group_kfold_inner.split(x_train, groups=groups_train),
                                 scoring='neg_mean_squared_error',
                                 n_jobs=4)

        norm_rmse_scores = (np.sqrt(-scores)/norm_factor) * 100
        cross_val_results['norm_rmse_scores'].append(norm_rmse_scores)
        cross_val_results['mean_norm_rmse_scores'].append(np.mean(norm_rmse_scores))
        cross_val_results['std_norm_rmse_scores'].append(np.std(norm_rmse_scores))

    pickle.dump((scenario_list_final, cross_val_results), open(abs_path / 'XGBoost_scenario_model.dat', 'wb'))
else:
    (scenario_list_final, cross_val_results) = pickle.load(open(abs_path / 'XGBoost_scenario_model.dat', 'rb'))

mean_values = np.array(cross_val_results['mean_norm_rmse_scores'])
std_values = np.array(cross_val_results['std_norm_rmse_scores'])

#%%
fig, ax = plt.subplots(1, 1, figsize=(3.5, 1.5))
plt.subplots_adjust(bottom=0.22, top=0.98, right=0.98, left=0.1)
ax.errorbar(np.array(scenario_list_final)[::2], mean_values[::2],
            yerr=std_values[::2],
            marker='o',
            markersize=0,
            linewidth=0.9,
            color='r',
            elinewidth=0.4,
            capsize=2,
            capthick=0.1, ecolor='k')
ax.set_ylabel('NRMSE [\%]', fontsize=7)
ax.set_xlabel('Number of scenarios', fontsize=7)
# ax.set_xticks(np.array(scenario_list_final)[::2])
ax.tick_params(axis='both', labelsize=8)
ax.axvline(x=200, color='b', linewidth=0.8, linestyle='--')
# plt.tight_layout()


# Дан текст на языке племени Мумба-Юмба. Выведите все слова, встречающиеся в тексте, разделяя их пробелом. Слова должны
# быть отсортированы по убыванию их количества появления в тексте, а при одинаковой частоте появления — в алфавитном
# порядке.
#
# Подсказка. После того, как вы создадите словарь всех слов, вам захочется отсортировать его по частоте встречаемости
# слова. Желаемого можно добиться, если создать список, элементами которого будут кортежи из двух элементов: частота
# встречаемости слова и само слово. Например, [(2, 'hi'), (1, 'what'), (3, 'is')]. Тогда стандартная сортировка будет
# сортировать список кортежей, при этом кортежи сравниваются по первому элементу, а если они равны — то по второму. Это
# почти то, что требуется в задаче, а чтобы сделать то что нужно — вспомните про параметр key в сортировке.

def get_key(key):
    return key[1]

f_in = open('inputs/input03.txt', 'r', encoding='utf8')
f_out = open('outputs/output03.txt', 'w', encoding='utf8')

all_words = {}

for line in f_in:
    tmp_list = list(line.split())
    for word in tmp_list:
        if word in all_words:
            all_words[word] += 1
        else:
            all_words[word] = 1

ord_words = [(k, v) for k, v in all_words.items()]
ord_words.sort()
ord_words.sort(reverse=True, key=get_key)

for word in ord_words:
    print(word[0], file=f_out, end=' ')

f_in.close()
f_out.close()

Week 6 Text files, csv and spreadsheets/Task03.py


import numpy as np
import cv2
import imutils

scaling_factorx=0.5
scaling_factory=0.5

cap = cv2.VideoCapture(0)
#cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
#cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))

print(cap.get(3))
print(cap.get(4))

writer = cv2.VideoWriter('filename.avi', cv2.VideoWriter_fourcc(*"MJPG"), 20, (720, 480))

print(cap.get(3))
print(cap.get(4))

while (True):
    # Capture frame-by-frame
    ret, frame = cap.read()  # ret = 1 if the video is captured; frame is the image

    #frame = cv2.resize(frame, None, fx=scaling_factorx, fy=scaling_factory, interpolation=cv2.INTER_AREA)
    #frame = imutils.resize(frame, width=320)
    # Our operations on the frame come here
    #img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    writer.write(frame)
    # Display the resulting image
    cv2.imshow('Video Capture', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):  # press q to quit
        status = cv2.imwrite('img.jpg', frame)
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()


"""
This script shows how you can open and extract data from a fits file in python, you need astropy
which is included in the anaconda distribution. The fits files for lyman alpha luminsoity are on
the box.
"""
from astropy.io import fits
from astropy.table import Table

filename = 'Sobral2018_Integrals_SFRDs_individual.fits' # the file you want to open
hdu_list = fits.open(filename) # open the file, returns an array containing raw data and metadata
evt_data = Table(hdu_list[1].data) # The raw data is contained in the first element
rho_Lya = evt_data.field(3)[0:] # get a list of the all the data from fourth field
for i in rho_Lya:
    print(i)


# **********************************************************************************
# * Project: Eskapade - A python-based package for data analysis                   *
# * Class  : MongoMoveCollection                                                   *
# * Created: 2016/11/08                                                            *
# * Description:                                                                   *
# *      Algorithm to do...(fill in here)                                          *
# *                                                                                *
# * Authors:                                                                       *
# *      KPMG Big Data team, Amstelveen, The Netherlands                           *
# *                                                                                *
# * Redistribution and use in source and binary forms, with or without             *
# * modification, are permitted according to the terms listed in the file          *
# * LICENSE.                                                                       *
# **********************************************************************************

import pandas as pd

from escore import process_manager, ConfigObject, DataStore, Link, StatusCode
from mongodbtools import MongoConnection

class MongoMoveCollection(Link):
    """
    Moves data from a mongo source collection to one or more mongo target collections.
    """

    def __init__(self, name='mongoCollectionMover'):
        """
        Store the configuration of link MongoMoveCollection

        :param str name: name of link
        :param str source_collection: mongo collection name of the source collection
        :param list target_collections: mongo collection name(s) of the target collecion(s)
        :param dict columnsToAdd: columns to add to the pandas.DataFrame before storage. key = column name,
            value = column value
        :param dict filter: pymongo filter for the query on the source collection
        :param bool copy: if True data in the source collection will not be removed
        """

        Link.__init__(self, name)

        self.source_collection = None
        self.target_collections = []
        self.columnsToAdd = None
        self.filter = None
        self.copy = False

        return

    def initialize(self):
        """ Initialize MongoMoveCollection """

        if self.filter is not None:
            self.logger.debug('Applying filter: {filter!s}.', filter=self.filter)

        return StatusCode.Success

    def execute(self):
        """ Execute MongoMoveCollection """

        process_manager.service(MongoConnection).set_config_info(process_manager.service(ConfigObject))
        self.mdb = process_manager.service(MongoConnection).database
        # check if collection names are in database
        colls = self.mdb.collection_names()
        if self.source_collection not in colls:
            raise Exception("%s is not a collection in the mongo database" % self.source_collection)

        if self.filter is not None:
            if isinstance(self.filter, dict):
                pass
            elif isinstance(self.filter, str):
                ds = process_manager.service(DataStore)
                assert self.filter in ds, 'Filter key <%s> not found in datastore.' % self.filter
                self.filter = ds[self.filter]
                assert isinstance(self.filter, dict), 'Filter with key <%s> is not a dict.' % self.filter
            else:
                raise Exception('Given filter of incorrect type.')

        if self.filter is not None:
            data = self.mdb[self.source_collection].find(self.filter)
        else:
            data = self.mdb[self.source_collection].find()
        df = pd.DataFrame(list(data))

        if len(df) == 0:
            self.logger.info('Source collection <{collection}> has zero length. Nothing to move.',
                                collection=self.source_collection)
            return StatusCode.Success

        if self.columnsToAdd is not None:
            for k, v in self.columnsToAdd.items():
                df[k] = v
        docs = list(df.T.to_dict().values())

        s = []
        for coll in self.target_collections:
            try:
                self.mdb[coll].insert_many(docs)
                s.append(coll)
                appliedstr = 'Copied' if self.copy else 'Moved'
                self.logger.info('{action} collection <{collection}> with length <{length}> to <{target}>.',
                                 action=appliedstr, collection=self.source_collection, length=len(docs), target=coll)
            except:
                for c in s:
                    self.mdb[c].delete_many(({'_id': {"$in": list(df._id)}}))
                raise Exception('Error in move: insertion in target collection %s failed' % coll)

        if not self.copy:
            try:
                self.mdb[self.source_collection].delete_many({'_id': {"$in": list(df._id)}})
            except:
                for c in s:
                    self.mdb[c].delete_many(({'_id': {"$in": list(df._id)}}))
                raise Exception('Error in move: deletion from source collection %s failed' % self.source_collection)

        return StatusCode.Success

    def finalize(self):
        """ Finalizing MongoMoveCollection """

        return StatusCode.Success


import torch
import torch.nn as nn
from DataInit import DataInit
from WordVecs import WordVecs
from Models.SimpleModel import SimpleModel
from Models.AttnModel import AttnModel
from Trainer import Trainer
from Plotter import Plotter
import argparse

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-ep', '--emdpath',
                        help="where the embeddings file is located",
                        default='Data/fasttext.vec')
    parser.add_argument('-dp', '--datapath',
                        help="where the data file is located",
                        default='Data/train.csv')
    parser.add_argument('-model', '--model',
                        help="which model to use",
                        default='simple')
    parser.add_argument('-lr', '--lr',
                        help="learning rate to use",
                        default=0.0001, type=float)
    parser.add_argument('-batch_size', '--batch_size',
                        help="size of batch",
                        default=128, type=int)
    parser.add_argument('-epochs', '--epochs',
                        help="number of epochs",
                        default=250, type=int)
    parser.add_argument('-test_ratio', '--test_ratio',
                        help="test/train ratio",
                        default=0.1, type=float)
    parser.add_argument('-cuda', '--cuda',
                        help="use cuda",
                        default=True, type=bool)
    args = parser.parse_args()

    # Consts
    print("Init Consts")
    data_file_path = args.datapath
    emmbedings_file_path = args.emdpath
    batch_size = args.batch_size
    epochs = args.epochs
    learning_rate = args.lr
    test_ratio = args.test_ratio
    cuda = args.cuda

    print("Start DataInit")
    data_init = DataInit(data_file_path)

    train_X, test_X, train_y, test_y = data_init.get_train_test_data(test_ratio)

    print("Importing embeddings")
    src_vecs = WordVecs(emmbedings_file_path)

    if args.model == 'simple':
        print("Init simple Model")
        is_attn = False
        model = SimpleModel(src_vecs, cuda)
    else:
        print("Init attention Model")
        is_attn = True
        model = AttnModel(src_vecs, batch_size, cuda)

    print("Model parametrs ", model)
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
    criterion = nn.BCEWithLogitsLoss(reduce=True, size_average=False)

    print("Init Trainer")
    trainer = Trainer(model, optimizer, learning_rate, criterion, epochs, batch_size, is_attn, cuda)

    print("Start training with parameters:")
    print("batch_size: {} | epochs: {} | learning_rate: {} | train_ratio: {}".format(batch_size, epochs, learning_rate,
                                                                                test_ratio))
    train_losses, test_losses = trainer.train(train_X, train_y, test_X, test_y)

    plotter = Plotter(train_losses, test_losses)
    plotter.plot()

if __name__ == '__main__':
    main()


from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView

urlpatterns = patterns(
    'web02.views',
    # url(r'^home/$', TemplateView.as_view(template_name="web02/home.html")),
    url(r'^home/$', 'home'),
    url(r'^product/$', 'product'),
    url(r'^product/(?P<id>[0-9]+)/$', 'product_detail'),
    url(r'^course/$', 'course'),
    url(r'^course/(?P<id>[0-9]+)/$', 'course_detail'),
    url(r'^accounts/', include('web02.urls_auth')),
)


import unittest

from so_components.pcb_managment import PCB, NEW_STATUS
from so_components.scheduling_algorithms.priority_scheduling import PCBInPriorityReadyQueue

class PCBTest(unittest.TestCase):

    def test_a_base_pcb_have_base_and_limit_dir_and_start_with_new_status(self):
        pcb = PCB(0, 0, 4, 'first_pcb.exe', 8)

        self.assertEqual(pcb.status, NEW_STATUS)
        self.assertEqual(pcb.base_dir, 0)
        self.assertEqual(pcb.limit, 4)

    def test_a_pcb_from_priority_algorithm_start_with_priority(self):
        pcb = PCB(0, 0, 5, 'first_pcb.exe', 4)

        self.assertEqual(pcb.priority, 4)

    def test_a_pcb_in_ready_queue_is_comparable_by_temporal_priority(self):
        pcb = PCB(0, 0, 4, 'first_pcb.exe', 8)
        pcb2 = PCB(0, 0, 4, 'first_pcb.exe', 3)

        pcb_in_rq1 = PCBInPriorityReadyQueue(pcb)
        pcb_in_rq2 = PCBInPriorityReadyQueue(pcb2)

        self.assertTrue(pcb_in_rq1 > pcb_in_rq2)

        pcb_in_rq1.temp_priority -=6
        self.assertFalse(pcb_in_rq1 > pcb_in_rq2)

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


#coding:utf8
import re
import cv2
import os
import numpy as np
import cv2.cv as cv
from keras.models import Sequential
from keras.layers.core import Dense,Activation,Flatten
from keras.layers.convolutional import Convolution2D,MaxPooling2D
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from keras.layers.advanced_activations import LeakyReLU

from keras.utils import np_utils

#构建卷积神经网络
def cnn_model(train_data,train_label,test_data,test_label):
    model = Sequential()
    model.add(Convolution2D(
        nb_filter = 12,
        nb_row = 3,
        nb_col = 3,
        border_mode = 'valid',
        dim_ordering = 'th',
        input_shape = (1,128,192)))
    model.add(Activation('relu'))#激活函数使用修正线性单元
    model.add(MaxPooling2D(
        pool_size = (2,2),
        strides = (2,2),
        border_mode = 'valid'))
    model.add(Convolution2D(
        24,
        3,
        3,
        border_mode = 'valid',
        dim_ordering = 'th'))
    model.add(Activation('relu'))
#池化层 24×29×29
    model.add(MaxPooling2D(
        pool_size = (2,2),
        strides = (2,2),
        border_mode = 'valid'))
    model.add(Convolution2D(
        48,
        3,
        3,
        border_mode = 'valid',
        dim_ordering = 'th'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(
        pool_size = (2,2),
        strides =(2,2),
        border_mode = 'valid'))
    model.add(Flatten())
    model.add(Dense(20))
    model.add(Activation(LeakyReLU(0.3)))
    #model.add(Dropout(0.5))
    model.add(Dense(20))
    model.add(Activation(LeakyReLU(0.3)))
    #model.add(Dropout(0.4))
    model.add(Dense(5,init = 'normal'))
    model.add(Activation('softmax'))
    adam = Adam(lr = 0.001)
    model.compile(optimizer = adam,
            loss =  'categorical_crossentropy',
            metrics = ['accuracy'])
    print('----------------training-----------------------')
    model.fit(train_data,train_label,batch_size = 12,nb_epoch = 35,shuffle = True,show_accuracy = True,validation_split = 0.1)
    print('----------------testing------------------------')
    loss,accuracy = model.evaluate(test_data,test_label)
    print('\n test loss:',loss)
    print('\n test accuracy',accuracy)

cnn_model(train_data,train_label,test_data,test_label)


import json
import os
from config import UPLOAD_DIR
from worker import celery
import celery.states as states
from bottle import request, route, view, get, post, template, redirect
from tasks import add

"""
All routes for application that managing UI side.
example
1) user can upload and submit 7 main files that we use for update sales tax in Tract.
2) user can update sales tax by hitting single "Start sales tax Task" button
3)
"""

@get('/')
@get('/upload')
def upload():
    return template('upload' , message="")

@post('/upload', method='POST')
def do_upload():
    """Handle file upload form"""
    message = ""
    if request.method == 'POST':
        uploads = request.files.getall('newfile')
        for file in uploads:

            # only allow upload of text files
            if file.filename not in  ['FLATT.txt','LOCALT.txt','STATET.txt','FLAT.txt','LOCAL.txt','STATE.txt', 'TaxEligibility.txt']:
                message = "Error Saving"
                return template('upload' , message=message)

            save_path = os.path.join(UPLOAD_DIR, file.filename)
            if os.path.isfile(save_path):
                os.remove(save_path)
            file.save(save_path)

    # redirect to home page if it all works ok
    return redirect('/upload')

@get('/sales_task')
def sales_task():
    task = celery.send_task('tasks.sales_tax')
    return template("task", id=task.task_id)

@get('/task_eligibility')
def task_eligibility():
    task = celery.send_task('tasks.tax_eligibility')
    return template("task", id=task.task_id)

# @route('/background_job/<task_id>')
# def background_job(task_id=''):
#     task = sales_tax_task.AsyncResult(task_id)
#     ret = {}
#     if task.state == "PENDING":
#         ret['progress'] = 0
#     elif task.state == "PROGRESS":
#         ret['progress'] = "PROGRESS"
#     else:
#         ret['progress'] = task.state
#     return ret

@route('/background_job/<task_id>')
def background_job(task_id= ""):
    task = celery.AsyncResult(task_id)
    ret = {}
    ret['progress'] = task.state
    return ret


import sys
import os

activate_this = '/home/ubuntu/.local/share/virtualenvs/mian-tNCgafuV/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

sys.path.insert(0, '/var/www/html/mian')

import json
import logging.config

def setup_logging(
    default_path='logging.json',
    default_level=logging.INFO,
    env_key='LOG_CFG'
):
    """Setup logging configuration

    """
    path = default_path
    value = os.getenv(env_key, None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, 'rt') as f:
            config = json.load(f)
        logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=default_level)

print("App Startup")

from mian.main import app as application

application.secret_key = 'Twilight Sparkle'
application.config['SESSION_TYPE'] = 'filesystem'


import numpy as np
import cv2

hls_dict = dict(H=0, L=1, S=2)
rgb_dict = dict(R=0, G=1, B=2)
luv_dict = dict(l=0, u=1, v=2)

def sobel_filter(img, orient="x", sobel_kernel=3):
    """
    Helper function for applying Sobel filter

    Returns:
        sobel: Sobel filtered image using specified kernel size
    """
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Take the derivative in x or y given orient = 'x' or 'y'
    if orient=="x":
        sobel = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    elif orient=="y":
        sobel = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    else:
        raise ValueError("orient has to be x or y!!!")
    return sobel

def abs_sobel_thresh(img, orient='x', thresh=(0, 255), sobel_kernel=3):
    """
    Apply Sobel Filter on image gradient

    Returns:
        binary_output: binary image after applying sobel filter in x or y direction
    """
    sobel = sobel_filter(img, orient=orient, sobel_kernel=sobel_kernel)
    # Take the absolute value of the derivative or gradient
    abs_sobel = np.absolute(sobel)
    # Scale to 8-bit (0 - 255) then convert to type = np.uint8
    scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel))
    # Create mask of ones
    binary_output = np.zeros_like(scaled_sobel)
    binary_output[(scaled_sobel >= thresh[0]) & (scaled_sobel <= thresh[1])] = 1  # ToDo: use min max instead of index

    return binary_output

def mag_thresh(img, thresh=(0, 255), sobel_kernel=3):
    """
    Apply Filter on gradient magnitude

    Returns:
        binary_output: binary image filtered on gradient magnitude
    """
    sobelx = sobel_filter(img, orient="x", sobel_kernel=sobel_kernel)
    sobely = sobel_filter(img, orient="y", sobel_kernel=sobel_kernel)
    # Calculate the magnitude
    abs_sobelxy = np.sqrt(sobelx**2 + sobely**2)
    # Scale to 8-bit (0 - 255) and convert to type = np.uint8
    scaled_sobel = np.uint8(255*abs_sobelxy/np.max(abs_sobelxy))
    # Create a binary mask where mag thresholds are met
    binary_output = np.zeros_like(scaled_sobel)
    binary_output[(scaled_sobel >= thresh[0]) & (scaled_sobel <= thresh[1])] = 1
    return binary_output

def dir_thresh(img, thresh=(0, np.pi/2), sobel_kernel=3):
    """
    Apply Filter on gradient direction

    Returns:
        binary_output: binary image filtered on gradient direction
    """

    sobelx = sobel_filter(img, orient="x", sobel_kernel=sobel_kernel)
    sobely = sobel_filter(img, orient="y", sobel_kernel=sobel_kernel)
    # Take the absolute value of the x and y gradients
    abs_sobelx = np.abs(sobelx)
    abs_sobely = np.abs(sobely)
    # Use np.arctan2(abs_sobely, abs_sobelx) to calculate the direction of the gradient
    direction = np.arctan2(abs_sobely, abs_sobelx)
    # Create a binary mask where direction thresholds are met
    binary_output = np.zeros_like(sobelx)
    binary_output[(direction >= thresh[0]) & (direction <= thresh[1])] = 1

    return binary_output

def hls_thresh(img, hls_channel="S", thresh=(0, 255)):
    """
    Apply Filter on image in HLS color space

    Returns:
        binary_output: binary image filtered on specified channel in HLS color space
    """
    # Convert to HLS color space
    hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
    # Apply a threshold to channel
    channel_num = hls_dict[hls_channel]
    channel_dim = hls[:,:,channel_num]
    # Return a binary image of threshold result
    binary_output = np.zeros_like(channel_dim)
    binary_output[(channel_dim > thresh[0]) & (channel_dim <= thresh[1])] = 1

    return binary_output

def rgb_thresh(img, rgb_channel="R", thresh=(0, 255)):
    """
    Apply Filter on image in RGB color space

    Returns:
        binary_output: binary image filtered on specified channel in RGB color space
    """
    rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    channel_num = rgb_dict[rgb_channel]
    channel_dim = rgb[:,:,channel_num]
    binary_output = np.zeros_like(channel_dim)
    binary_output[(channel_dim > thresh[0]) & (channel_dim <= thresh[1])] = 1

    return binary_output


__doc__ = """
John Brezovec 2018

Helper script to build the relevant finite difference equations
for a 2-d conduction problem. These equations can be solved and
visualized using multidim_conduction_solver.py
"""

class node(object):
    """
    very simple node class, just contains index of the node, the coordinates
    of the node, as well as the equation for the particular node
    """

    def __init__(self, index, col, row):
        self.index = index
        self.col = col
        self.row = row
        self.leftside = []
        self.rightside = []
        self.type = None

    def __repr__(self):
        if self.type in (None, 0):
            return str(0)
        leftstring = ""
        rightstring = ""
        for n in self.leftside:
            leftstring += str(n)
        for n in self.rightside:
            if rightstring == "":
                rightstring += str(n)
            else:
                rightstring += " + " + str(n)
        return leftstring + " == " + rightstring

    def init_eq(self):
        if self.type == 0:
            pass
        if self.type == 'outer':
            self.leftside.append("To")
            self.rightside.append("T{}".format(self.index))
        if self.type == 'inner':
            self.leftside.append("Ti")
            self.rightside.append("T{}".format(self.index))
        if self.type == 'bulk':
            self.leftside.append("0")
            # constructing right side of equation
            rightside = []
            above = [0, None]
            below = [0, None]
            left = [0, None]
            right = [0, None]
            # look above
            try:
                if self.row - 1 < 0:
                    raise IndexError
                above[1] = nodalSys[self.row - 1][self.col].index
                above[0] += 1
            except IndexError:
                below[0] += 1
            try:
                below[1] = nodalSys[self.row + 1][self.col].index
                below[0] += 1
            except IndexError:
                above[0] += 1
            try:
                right[1] = nodalSys[self.row][self.col + 1].index
                right[0] += 1
            except IndexError:
                left[0] += 1
            try:
                if self.col - 1 < 0:
                    raise IndexError
                left[1] = nodalSys[self.row][self.col - 1].index
                left[0] += 1
            except IndexError:
                right[0] += 1
            if above[1] is not None:
                rightside.append("{}T{}".format(above[0], above[1]))
            if below[1] is not None:
                rightside.append("{}T{}".format(below[0], below[1]))
            if left[1] is not None:
                rightside.append("{}T{}".format(left[0], left[1]))
            if right[1] is not None:
                rightside.append("{}T{}".format(right[0], right[1]))
            rightside.append("-4T{}".format(self.index))
            self.rightside.extend(rightside)

def initNodalSys(nrows, ncols):
    # makes a nrows x ncols 'matrix' of lists
    return [[[] for _ in range(ncols)] for _ in range(nrows)]

def initNodes(nodalSys):
    index = 1
    for row in range(len(nodalSys)):
        for col in range(len(nodalSys[0])):
            if nodalSys[row][col] == []:
                nodalSys[row][col] = node(index, col, row)
                index += 1
            else:
                pass
    # no need to return anything since we're directly mutating the list

# initializing the node types for my particular setup
nrows = 50
ncols = 50
nodalSys = initNodalSys(nrows, ncols)

rowcounter = 0
for i in range(nrows):
    for n in range(int(ncols * 0.6) + rowcounter, nrows):
        nodalSys[i][n] = 0
    rowcounter += 1

initNodes(nodalSys)
# assign outer nodes
for n in range(nrows):
    nodalSys[n][0].type = 'outer'
# assign inner nodes
i = 0
for n in range(int(0.6 * ncols) - 1, ncols):
    print(i, n, nodalSys[i][n].index)
    nodalSys[i][n].type = "inner"
    i += 1

# make the rest bulk
for row in range(len(nodalSys)):
    for col in range(len(nodalSys[0])):
        try:
            if nodalSys[row][col].type is None:
                nodalSys[row][col].type = 'bulk'
            nodalSys[row][col].init_eq()
        except AttributeError:
            pass

# now store the results
with open("equations.txt", "w") as f:
    f.write("# CONSTANT # To = 30\n")
    f.write("# CONSTANT # Ti = 230\n")
    f.write("## START EQUATIONS ##\n")
    for n in nodalSys:
        for i in n:
            if str(i) == "0":
                continue
            f.write(str(i) + "\n")
    f.write("## END ##")

# now create the node coordinate file
with open("index_coordinates.csv", "w") as f:
    f.write("index, x, y\n")
    for n in nodalSys:
        for i in n:
            if str(i) == "0":
                continue
            index = str(i.index)
            x = str(i.col)
            y = str(nrows - i.row - 1)
            f.write(index + ", " + x + ", " + y + "\n")


import json
import time

import requests
from slugify import slugify

_ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
headers = {"Content-Type": "application/json", "User-agent": _ua}
api_url = "https://www.ocado.com/webshop/api/v1/products?skus="

def get_api(sku):
    response = requests.get(api_url + sku, headers=headers)
    time.sleep(2)
    if response.status_code == 200:
        return json.loads(response.content.decode("utf-8"))
    else:
        print("status code: ", response.status_code)
        return None

def product_url(sku):
    products_info = get_api(sku)

    try:
        product_name = products_info[0]["name"]
        product_url = slugify(product_name, lowercase=False)
        product_url = f"https://www.ocado.com/webshop/product/{product_url}/{sku}"

        return product_url
    except:
        return None


from xai.brain.wordbase.nouns._bozo import _BOZO

#calss header
class _BOZOS(_BOZO, ):
	def __init__(self,):
		_BOZO.__init__(self)
		self.name = "BOZOS"
		self.specie = 'nouns'
		self.basic = "bozo"
		self.jsondata = {}


import random
import operator
from decimal import Decimal

transitionTable = {"R": {"S": 0, "P": 0, "R": 0}, "S": {"S": 0, "P": 0, "R": 0}, "P": {"S": 0, "P": 0, "R": 0}}

playCount = {"R": 0, "P": 0, "S": 0}

choices = ["R", "P", "S"]

moveHistory = []

def updater(moveHistory, transitionTable):
    playCount[moveHistory[-1]] += 1

    if len(moveHistory) > 1:
        slice1 = moveHistory[-2]
        slice2 = moveHistory[-1]

        editDict = transitionTable[slice1]

        editDict[slice2] += 1

def picker():
    editDict = transitionTable[moveHistory[-1]]
    pick = ""
    largest = max(editDict.items(), key=operator.itemgetter(1))[0]

    if editDict[largest] == 0:
        return choices[random.randint(0,2)]
    else:
        pick = largest

    if pick is "R":
        return "P"
    elif pick is "P":
        return "S"
    elif pick is "S":
        return "R"

if input is not "":
    moveHistory.append(input)
    updater(moveHistory, transitionTable)

if len(moveHistory) == 0:
    output = choices[random.randint(0, 2)]
else:
    output = picker()


import discord
from discord.ext import commands
from settings import API_URL
from utils import get_gql_client
from queries import get_profile
from mutations import set_player

# Ultima edição feita por @Grievon

client = commands.Bot(command_prefix='r/')

@client.event
async def on_ready():
    print("Let's to work!")

@client.command()
async def cadastrar(bot, usr=None):
    """
    Use registrar para adicionar novos jogadores no server!
    """

    if not usr:
        return await bot.send(f'{bot.author} o comando não pode ser vazio!')

    is_pc = True if usr[2] == '!' else False
    if is_pc:
        author = '<@!' + str(bot.author.id) + '>'
    else:
        author = '<@' + str(bot.author.id) + '>'

    if author != usr:
        return await bot.send(f'{bot.author} por favor fornecer o @SeuNickName correto!')
    else:

        if usr[2] != '!':
            p1, p2 = usr[:2], usr[2:]
            usr = f'{p1}!{p2}'

        payload = set_player(usr)
        api_client = get_gql_client(API_URL)
        response = api_client.execute(payload)
        print(response)
        return await bot.send(f'O usuário {usr} foi cadastrado')

@client.command()
async def perfil(bot, usr=None):
    """
    Visualisar perfil do jogador e seu personagem!
    """

    if not usr:
        return await bot.send(f'{bot.author} por favor informe o @NickName')
    else:

        return await bot.send(f'''Nick:{usr}
''')

@client.command()
async def ajuda(bot):
    return await bot.send('''
Olá, meu nome é Regis e estou aqui para te ajudar!
Seu assistente de registro e monitoramento de Players, caso você precise dos meus serviços basta me informar o comando desejado!

```Para se cadastrar no servidor basta dar o comando: r/cadastrar @SeuNickName

Para verificar o perfil de um(a) jogador(a) basta dar o comando: r/perfil @NickName```

Caso precise de uma ajuda que não esteja nesse tópico entre em contato com a equipe de desenvolvedores. Eles poderão lhe dar mais informações!

Epero ter ajudado o/
    ''')

@client.command()
async def teste(bot, usr):

    print(usr)
    return await bot.send(usr)


#coding:utf-8
import hashlib as hasher
import datetime

def create_genesis_block():
    '''Constructs a block with index 0 and arbitrary previous hash'''
    return Block(0, datetime.datetime.now(), "Genesis Block" ,"0","0","0","0")
#操作的是区块对象
class Block():
    def __init__(self, index, timestamp, software,company, addr,cite,previous_hash):
        self.index = index
        self.timestamp = timestamp
        self.software = software
        self.company = company
        self.addr = addr
        self.cite = cite #引用字段
        self.previous_hash = previous_hash
        self.hash = self.hash_block()

    def  hash_block(self):
        sha = hasher.sha256()
        sha.update(str(self.index).encode("utf-8")
            + str(self.timestamp).encode("utf-8")
            + str(self.software).encode("utf-8")
            + str(self.company).encode("utf-8")
            + str(self.addr).encode("utf-8")
            + str(self.cite).encode("utf-8")
            + str(self.previous_hash).encode("utf-8")
        )
        return sha.hexdigest()

# Create the blockchain and add genesis block.
blockchain = [create_genesis_block()]
#print(blockchain[0].cite)
previous_block = blockchain[0]
#print(previous_block)

# How many blocks to add after the genesis block?
#num_of_blocks_to_add = 10

def next_block(last_block):
    this_index = last_block.index + 1
    this_timestamp = datetime.datetime.now()
    #this_data = "Hey! I'm block " + str(this_index)
    this_software = input("请输入第 #{} 个区块的软件名 : ".format(this_index))
    this_company = input("请输入第 #{} 个区块的公司名 : ".format(this_index))
    this_addr = input("请输入第 #{} 个区块的软件地址 : ".format(this_index))
    this_cite = input("请输入第 #{} 个区块的引用记录 : ".format(this_index))
    return Block(this_index, this_timestamp, this_software, this_company,this_addr,this_cite,last_block.hash)

# Add blocks to the chain.
#for i in range(num_of_blocks_to_add):
while 1:
    block_to_add = next_block(previous_block)
    blockchain.append(block_to_add)
    previous_block = block_to_add
    # Tell everyone about it!
    print("区块 #{} 成功上链！".format(block_to_add.index))
    print("软件名 :{}".format(block_to_add.software))
    print("公司名 :{}".format(block_to_add.company))
    print("软件地址 :{}".format(block_to_add.addr))
    print("引用记录: {}".format(block_to_add.cite))
    print("哈希值: {}".format(block_to_add.hash))
    print("时间戳: {}\n".format(block_to_add.timestamp))
    #存入数据库
# print(blockchain[0].previous_hash)


'''
Дан треугольник на плоскости, заданный координатами своих вершин. Для этого треугольника вычислить:
- длины сторон;
- периметр;
- площадь;
- величины углов в градусах.
'''

# 1. Импортировать функцию для перевода радиан в градусы, арккосинуса и квадратного корня.
from math import sqrt, acos, degrees

# 2. Реализовать функцию для вычисления длины отрезка с координатами концов (x0, y0) и (x1, y1).
def compute_len(x_0,y_0,x_1,y_1):
    len_line = sqrt((x_1 - x_0) ** 2 + (y_1 - y_0) ** 2)

    return len_line

# 3. Реализовать функцию для вычисления площади треугольника по трем сторонам a_1, a_2, a_3.
def compute_area(a_1, a_2, a_3):
    p = (a_1 + a_2 + a_3) / 2

    area = sqrt(p * (p - a_1) * (p - a_2) * (p - a_3))

    return area

# 4. Реализовать функцию для вычисления угла треугольника в градусах, если известны длины сторон a_1, a_2, a_3.
def compute_angle(a_1, a_2, a_3):
    angle_rad = acos((a_1 ** 2 + a_2 ** 2 - a_3 ** 2)/
                     (2 * a_1 * a_2))

    return degrees(angle_rad)

# 5. Ввести координаты вершин треугольника с подсказкой пользователю.
x_a = float(input("x_a = "))
y_a = float(input("y_a = "))
x_b = float(input("x_b = "))
y_b = float(input("y_b = "))
x_c = float(input("x_c = "))
y_c = float(input("y_c = "))

# 6. Вычислить длины сторон треугольника.
c = compute_len(x_a, y_a, x_b, y_b)
a = compute_len(x_b, y_b, x_c, y_c)
b = compute_len(x_a, y_a, x_c, y_c)

# 7. Проверить, существует ли треугольник, построенный по заданным точкам, если нет - выдать сообщение пользователю.
if a + b <= c or b + c <= a or a +c <= b:
    print("Треугольник не существует")

# 8. В противном случае вычислить площадь, периметр, величины углов.
else:
    s = compute_area(a, b,c)
    p = a + b + c
    angle_A = compute_angle(c, b, a)
    angle_B = compute_angle(c, a, b)
    angle_C = compute_angle(a, b, c)

# 9. Вывести результаты вычисления на экран, округлив их до 3-х знаков после запятой.
    print("Стороны : ", round(a, 3), round(b, 3), round(c, 3))
    print("Площадь : ", round(s,3))
    print("Периметр : ", round(p,3))
    print("Углы : ", round(angle_A, 3), round(angle_B, 3), round(angle_C, 3))


from collections import deque

def groupby(func, seq):
    result = {}

    for x in seq:
        result[func(x)] = [y for y in seq if func(y) == func(x)]

    return result

def zip_with(func, *iterables):
    zip_iterables = zip(*iterables)
    while True:
        yield func(*next(zip_iterables))

def composer(f, g):
    return lambda x: f(g(x))

def iterate(func):
    yield lambda x: x
    composition = func
    while True:
        yield composition
        composition = composer(func, composition)

def cache(func, cache_size):
    values = {}
    cache_order = deque()

    def func_cashed(*args):
        if args in values:
            return values[args]

        result = func(*args)

        if cache_size:
            if len(values) == cache_size:
                values.pop(cache_order.popleft())
            cache_order.append(args)
            values[args] = result

        return result
    return func_cashed