from scene import *

import time
import ui
import sound
import globals

class ShopRubScene(Scene):
    def setup(self):

        self.fixed_time_step = 'Nill'
        self.size_of_screen_x = self.size.x
        self.size_of_screen_y = self.size.y
        self.screen_center_x = self.size_of_screen_x/2
        self.screen_center_y = self.size_of_screen_y/2

        self.buy1_label_down = False
        self.buy2_label_down = False
        self.buy3_label_down = False
        self.buy4_label_down = False
        self.buy5_label_down = False
        self.buy6_label_down = False
        self.counter = 0
        # this method is called, when user moves to this scene

        # create timer, so that after 2 seconds move to next scene
        self.start_time = time.time()

        # add MT blue background color
        self.background = SpriteNode('assets/sprites/background.JPG',
                                     position = self.size / 2,
                                     parent = self,
                                     scale = 1.25)

        self.game_label = LabelNode(text = 'Shop',
                                     font=('Markerfelt-Wide', 40),
                                     parent = self,
                                     position = (self.size_of_screen_x - 50, self.size_of_screen_y - 40),
                                     color = 'grey')

        self.back_button = SpriteNode('assets/sprites/backw.PNG',
                                       parent = self,
                                       position = (75, self.size_of_screen_y - 110),
                                       scale = 0.17,
                                       color = 'grey')

        self.back1_square1 = SpriteNode('./assets/sprites/shop/backsquare.PNG',
                                     parent = self,
                                     color = 'grey',
                                     scale = 0.65,
                                     position = (self.screen_center_x - self.screen_center_x/2, self.screen_center_y + self.screen_center_y/1.6))
        self.critchance_rune = SpriteNode('./assets/sprites/runes/runeGrey_slab_032.png',
                                     parent = self,
                                     color = '#a4a4a4',
                                     scale = 0.65,
                                     position = (self.screen_center_x - self.screen_center_x/2, self.screen_center_y + self.screen_center_y/1.57))
        self.critchance_label = LabelNode(text = 'CRIT CHANCE',
                                     font=('CopperPlate-Light', 12),
                                     parent = self,
                                     position = (self.screen_center_x - self.screen_center_x/2, self.screen_center_y + self.screen_center_y/1.79),
                                     color = 'grey')
        self.back1_square2 = SpriteNode('./assets/sprites/shop/backsquare.PNG',
                                     parent = self,
                                     color = 'grey',
                                     scale = 0.65,
                                     position = (self.screen_center_x - self.screen_center_x/2, self.screen_center_y + self.screen_center_y/2.7))
        self.critdamage_rune = SpriteNode('./assets/sprites/runes/runeGrey_slab_010.png',
                                     parent = self,
                                     color = '#a4a4a4',
                                     scale = 0.65,
                                     position = (self.screen_center_x - self.screen_center_x/2, self.screen_center_y + self.screen_center_y/2.6))
        self.critdamage_label = LabelNode(text = 'CRIT DAMAGE',
                                     font=('CopperPlate-Light', 12),
                                     parent = self,
                                     position = (self.screen_center_x - self.screen_center_x/2, self.screen_center_y + self.screen_center_y/3.3),
                                     color = 'grey')
        self.back2_square1 = SpriteNode('assets/sprites/game/gamestats.PNG',
                                     parent = self,
                                     color = 'lightgrey',
                                     size = (self.size_of_screen_x/2, self.size_of_screen_y/10),
                                     scale = 0.65,
                                     position = (self.screen_center_x, self.screen_center_y + self.screen_center_y/1.6))
        self.price1_label = LabelNode(text = str(globals.price1),
                                     font=('CopperPlate-Light', 25),
                                     parent = self,
                                     position = (self.screen_center_x, self.screen_center_y + self.screen_center_y/1.6),
                                     color = '#ff4a4a')
        self.back2_square2 = SpriteNode('assets/sprites/game/gamestats.PNG',
                                     parent = self,
                                     color = 'lightgrey',
                                     size = (self.size_of_screen_x/2, self.size_of_screen_y/10),
                                     scale = 0.65,
                                     position = (self.screen_center_x, self.screen_center_y + self.screen_center_y/2.7))
        self.price2_label = LabelNode(text = str(globals.price2),
                                     font=('CopperPlate-Light', 25),
                                     parent = self,
                                     position = (self.screen_center_x, self.screen_center_y + self.screen_center_y/2.7),
                                     color = '#ff4a4a')
        self.back3_square2 = SpriteNode('./assets/sprites/shop/backsquare.PNG',
                                     parent = self,
                                     color = 'grey',
                                     scale = 0.65,
                                     position = (self.screen_center_x + self.screen_center_x/2, self.screen_center_y + self.screen_center_y/2.7))
        self.back3_square1 = SpriteNode('./assets/sprites/shop/backsquare.PNG',
                                     parent = self,
                                     color = 'grey',
                                     scale = 0.65,
                                     position = (self.screen_center_x + self.screen_center_x/2, self.screen_center_y + self.screen_center_y/1.6))
        self.buy1_label = LabelNode(text = 'Buy',
                                     font=('CopperPlate-Light', 35),
                                     parent = self,
                                     position = (self.screen_center_x + self.screen_center_x/2, self.screen_center_y + self.screen_center_y/1.6),
                                     color = 'grey')
        self.buy2_label = LabelNode(text = 'Buy',
                                     font=('CopperPlate-Light', 35),
                                     parent = self,
                                     position = (self.screen_center_x + self.screen_center_x/2, self.screen_center_y + self.screen_center_y/2.7),
                                     color = 'grey')

        self.rubyz = globals.rubies
        self.ruby_label = LabelNode(text = str(self.rubyz),
                                     font = ('CopperPlate-Bold', 25),
                                     parent = self,
                                     anchor_point = (0, 0.5),
                                     position = (50, self.size_of_screen_y - 30),
                                     color = '#ff0043')
        self.ruby_img = SpriteNode('assets/sprites/gem.PNG',
                                     position = (30, self.size_of_screen_y - 30),
                                     parent = self,
                                     scale = 0.15)
    def update(self):
        # this method is called, hopefully, 60 times a seco
        self.counter = self.counter + 1

        if self.counter >= 500:
            self.counter = 0
            self.rubyz = globals.rubies
            self.ruby_label.text = str(self.rubyz)
        # after 2 seconds, move to main menu scene
        self.price1_label.text = str(globals.rubprice1) + ' [Lv' + str(globals.rublv1) + ']'
        self.price2_label.text = str(globals.rubprice2) + ' [Lv' + str(globals.rublv2) + ']'
    def touch_began(self, touch):
        # this method is called, when user touches the screen
        pass

    def touch_moved(self, touch):
        # this method is called, when user moves a finger around on the screen
        pass

    def touch_ended(self, touch):
        # this method is called, when user releases a finger from the screen
        if self.back_button.frame.contains_point(touch.location):
            self.dismiss_modal_scene()

        if self.buy1_label.frame.contains_point(touch.location) or self.back3_square1.frame.contains_point(touch.location):
            self.buy1_label_down = True
            if globals.rubies >= globals.rubprice1:
                self.counter = 500
                globals.rubies = globals.rubies - globals.rubprice1
                globals.rublv1 = globals.rublv1 + 1
                globals.rubprice1 = int(round(globals.rubprice1*2))
                globals.playercritchance = int(round(globals.playercritchance*1.2))
                sound.play_effect('assets/Ding_1.caf')
            else:
                pass

        if self.buy2_label.frame.contains_point(touch.location) or self.back3_square2.frame.contains_point(touch.location):
            self.buy2_label_down = True
            if globals.rubies >= globals.rubprice2:
                self.counter = 500
                globals.rubies = globals.rubies - globals.rubprice2
                globals.rublv2 = globals.rublv2 + 1
                globals.rubprice2 = int(round(globals.rubprice2*2))
                globals.playercritdmg = int(round(globals.playercritdmg*1.04))
                sound.play_effect('assets/Ding_1.caf')
            else:
                pass

    def did_change_size(self):
        # this method is called, when user changes the orientation of the screen
        # thus changing the size of each dimension
        pass

    def pause(self):
        # this method is called, when user touches the home button
        # save anything before app is put to background
        pass

    def resume(self):
        # this method is called, when user place app from background
        # back into use. Reload anything you might need.
        pass


# coding=utf-8

"""
ComicStreamer blacklist manager thread class
"""

import threading
import select
import sys
import logging
import platform
import Queue
import datetime

import utils
import os

from folders import AppFolders
import database
from library import Library

class Blacklist(threading.Thread):
    def __init__(self, dm):
        super(Blacklist, self).__init__()
        self.queue = Queue.Queue(0)
        self.quit = False
        self.dm = dm

    def stop(self):
        self.quit = True
        self.join()

    def setBlacklist(self, comic_id, pagenum):
        # for now, don't defer the blacklist setting, maybe it's not needed
        self.actualSetBlacklist( comic_id, pagenum)
        #self.queue.put((comic_id, pagenum))

    def run(self):
        logging.debug("Blacklist: Started")
        pagenum = 0
        while True:
            try:
                (comic_id, pagenum) = self.queue.get(block=True, timeout=1)
            except:
                comic_id = None

            self.actualSetBlacklist(comic_id, pagenum)

            if self.quit:
                break

        logging.debug("Blacklist: Stopped")

    def actualSetBlacklist(self, comic_id, pagenum):
        if comic_id is not None:
            library = Library(self.dm.Session)
            if pagenum == 'clear':
                library.comicBlacklist(comic_id)
            else:
                library.comicBlacklist(comic_id, pagenum)


# -*- encoding: utf-8 -*-
__author__ = 'cyclu'
__Created__ = '2015.10.15'

import time
from selenium import webdriver
import threading
from time import ctime,sleep

driver = webdriver.Ie()
driver.get('http://www.baidu.com'); #http://www.google.com/xhtml
# time.sleep(1) # Let the user actually see something!
# sleep(2)
search = '厦门国贸集团股份有限公司'
search2 = search.decode('utf-8')
driver.find_element_by_name('wd').send_keys(search2) #u'厦门国贸集团股份有限公司'
driver.find_element_by_name('wd').submit()
print(driver.title)
# sleep(3)
driver.quit()

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# from os import environ
# from selenium import webdriver
# iedriver = 'D:\Python27\IEDriverServer.exe'
# environ["webdriver.ie.driver"] = iedriver
# browser = webdriver.Ie(iedriver)
# browser.get("http://www.baidu.com")


from bs4 import BeautifulSoup
import datetime
import requests

class Epicurious:
    def __init__(self, url):
        self.source = 'Epicurious'
        self.url = url
        self.created_at = datetime.datetime.now()
        self.parse()
        self.name()
        self.image_url()
        self.servings()
        self.calories()
        self.cooking_time()
        self.ingredients()
        self.steps()

    def request(self):
        self.response = requests.get(self.url)

    def parse(self):
        self.request()
        self.soup = BeautifulSoup(self.response.text, 'html.parser')

    def name(self):
        self.name = self.soup.findAll('h1', {'itemprop': 'name'})[0].text.strip()

    def image_url(self):
        try:
            self.image_url = self.soup.findAll('div', {'class': 'social-img'})[0].img['gumby-media'].split('|')[1].split(',')[0]
        except:
            self.image_url = None

    def servings(self):
        try:
            servings = self.soup.findAll('dd', {'itemprop': 'recipeYield'})[0].text
            self.servings = filter(str.isdigit, str(servings))
        except:
            self.servings = None

    def calories(self):
        try:
            self.calories = self.soup.findAll('span', {'itemprop': 'calories'})[0].text
        except:
            self.calories = None

    def cooking_time(self):
        try:
            self.cooking_time = self.soup.findAll('dd', {'class': 'total-time'})[0].text
        except:
            self.cooking_time = None

    def ingredients(self):
        self.ingredients = []
        for ingredient in self.soup.findAll('li', {'class': 'ingredient'}):
            self.ingredients.append(ingredient.text)

    def steps(self):
        self.steps = []
        for step in self.soup.findAll('li', {'class': 'preparation-step'}):
            self.steps.append(step.text.strip())


from typing import List
from typing_extensions import TypedDict

from playhouse.shortcuts import model_to_dict, update_model_from_dict

from ... import auth
from ...api.socket.constants import GAME_NS
from ...app import app, sio
from ...logs import logger
from ...models import Group, PlayerRoom, Shape
from ...state.game import game_state

class ServerGroup(TypedDict):
    uuid: str
    character_set: List[str]
    creation_order: str

class MemberBadge(TypedDict):
    uuid: str
    badge: int

class GroupJoin(TypedDict):
    group_id: str
    members: List[MemberBadge]

class LeaveGroup(TypedDict):
    uuid: str
    group_id: str

@sio.on("Group.Info.Get", namespace=GAME_NS)
@auth.login_required(app, sio, "game")
async def get_group_info(sid: str, group_id: str):
    try:
        group = Group.get_by_id(group_id)
    except Group.DoesNotExist:
        logger.exception(f"Could not retrieve group information for {group_id}")
        data = {}
    else:
        data = model_to_dict(group)

    await sio.emit("Group.Info", data=data, room=sid, namespace=GAME_NS)

@sio.on("Group.Update", namespace=GAME_NS)
@auth.login_required(app, sio, "game")
async def update_group(sid: str, group_info: ServerGroup):
    pr: PlayerRoom = game_state.get(sid)

    try:
        group = Group.get_by_id(group_info["uuid"])
    except Group.DoesNotExist:
        logger.exception(
            f"Could not retrieve group information for {group_info['uuid']}"
        )
    else:
        update_model_from_dict(group, group_info)
        group.save()

    for psid, _ in game_state.get_users(room=pr.room):
        await sio.emit(
            "Group.Update",
            group_info,
            room=psid,
            skip_sid=sid,
            namespace=GAME_NS,
        )

@sio.on("Group.Members.Update", namespace=GAME_NS)
@auth.login_required(app, sio, "game")
async def update_group_badges(sid: str, member_badges: List[MemberBadge]):
    pr: PlayerRoom = game_state.get(sid)

    for member in member_badges:
        try:
            shape = Shape.get_by_id(member["uuid"])
        except Shape.DoesNotExist:
            logger.exception(
                f"Could not update shape badge for unknown shape {member['uuid']}"
            )
        else:
            shape.badge = member["badge"]
            shape.save()

    for psid, player in game_state.get_users(room=pr.room):
        await sio.emit(
            "Group.Members.Update",
            member_badges,
            room=psid,
            skip_sid=sid,
            namespace=GAME_NS,
        )

@sio.on("Group.Create", namespace=GAME_NS)
@auth.login_required(app, sio, "game")
async def create_group(sid: str, group_info: ServerGroup):
    pr: PlayerRoom = game_state.get(sid)

    try:
        Group.get_by_id(group_info["uuid"])
        logger.exception(f"Group with {group_info['uuid']} already exists")
        return
    except Group.DoesNotExist:
        Group.create(**group_info)

    for psid, _ in game_state.get_users(room=pr.room):
        await sio.emit(
            "Group.Create",
            group_info,
            room=psid,
            skip_sid=sid,
            namespace=GAME_NS,
        )

@sio.on("Group.Join", namespace=GAME_NS)
@auth.login_required(app, sio, "game")
async def join_group(sid: str, group_join: GroupJoin):
    pr: PlayerRoom = game_state.get(sid)

    group_ids = set()

    for member in group_join["members"]:
        try:
            shape = Shape.get_by_id(member["uuid"])
        except Shape.DoesNotExist:
            logger.exception(
                f"Could not update shape group for unknown shape {member['uuid']}"
            )
        else:
            if shape.group is not None and shape.group != group_join["group_id"]:
                group_ids.add(shape.group)
            shape.group = group_join["group_id"]
            shape.badge = member["badge"]
            shape.save()

    # Group joining can be the result of a merge or a split and thus other groups might be empty now
    for group_id in group_ids:
        await remove_group_if_empty(group_id)

    for psid, _ in game_state.get_users(room=pr.room):
        await sio.emit(
            "Group.Join",
            group_join,
            room=psid,
            skip_sid=sid,
            namespace=GAME_NS,
        )

@sio.on("Group.Leave", namespace=GAME_NS)
@auth.login_required(app, sio, "game")
async def leave_group(sid: str, client_shapes: List[LeaveGroup]):
    pr: PlayerRoom = game_state.get(sid)

    group_ids = set()

    for client_shape in client_shapes:
        try:
            shape = Shape.get_by_id(client_shape["uuid"])
        except Shape.DoesNotExist:
            logger.exception(
                f"Could not remove shape group for unknown shape {client_shape['uuid']}"
            )
        else:
            group_ids.add(client_shape["group_id"])
            shape.group = None
            shape.show_badge = False
            shape.save()

    for group_id in group_ids:
        await remove_group_if_empty(group_id)

    for psid, _ in game_state.get_users(room=pr.room):
        await sio.emit(
            "Group.Leave",
            client_shapes,
            room=psid,
            skip_sid=sid,
            namespace=GAME_NS,
        )

@sio.on("Group.Remove", namespace=GAME_NS)
@auth.login_required(app, sio, "game")
async def remove_group(sid: str, group_id: str):
    pr: PlayerRoom = game_state.get(sid)

    for shape in Shape.filter(group_id=group_id).select():
        shape.group = None
        shape.show_badge = False
        shape.save()

    # check if group still has members
    await remove_group_if_empty(group_id)

    for psid, _ in game_state.get_users(room=pr.room):
        await sio.emit(
            "Group.Remove",
            group_id,
            room=psid,
            skip_sid=sid,
            namespace=GAME_NS,
        )

async def remove_group_if_empty(group_id: str):
    try:
        group = Group.get_by_id(group_id)
    except Group.DoesNotExist:
        return

    if Shape.filter(group=group_id).count() == 0:
        group.delete_instance(True)


import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objects as go

from data import df
from layouts.cards import get_cards

df_cidade = df[(df['place_type'] == 'state') & (df['is_repeated'] == False)]
fig = go.Figure()

for uf in df_cidade['state'].unique():
    df_uf = df_cidade[df_cidade['state'] == uf]
    df_uf.set_index(pd.DatetimeIndex(df_uf.last_available_date), inplace=True)
    df_uf = df_uf.resample('D', origin='start').bfill()
    df_uf.last_available_date = df_uf.index.values
    fig.add_trace(go.Scatter(
        x=df_uf['last_available_date'],
        y=df_uf['last_available_death_rate'] * 100,
        name=uf
    ))

fig.update_xaxes(
    dtick="M1",
    tickformat="%m/%Y"
)
fig.update_layout(
    plot_bgcolor='white',
    xaxis={
        'title': {'text': 'Data'},
        'showgrid': True,
        'gridcolor': 'rgba(100, 100, 100, 0.5)'
    },
    yaxis={
        'title': {'text': 'Taxa de mortalidade (%)'},
        'showgrid': True,
        'gridcolor': 'rgba(100, 100, 100, 0.5)',

    },
)

evolucao_mortalidade = html.Div(get_cards(
    html.Div(
        [
            html.Div(dcc.Graph(id='grafico_evolucao_cidade', figure=fig))
        ],
        id='evolucao_mortalidade_por_estado'
    ), header='Comparativo de mortalidade por dia'
), className='col-6')


import graphadjacencylist

def main():

    print("----------------")
    print("| codedrome.com |")
    print("| GraphTheory   |")
    print("----------------\n")

    g = create_graph()

    print(g)

    # g = edit_graph(g)

    # print(g)

def create_graph():

    g = graphadjacencylist.GraphAdjacencyList()

    g.add_vertex("Annapolis")
    g.add_vertex("Washington D.C.")
    g.add_vertex("Frederick")
    g.add_vertex("Baltimore")

    g.add_edge("Annapolis", "Washington D.C.", directed = False, weight = 32)
    g.add_edge("Annapolis", "Baltimore", directed = False, weight = 31)
    g.add_edge("Annapolis", "Frederick", directed = False, weight = 75)

    g.add_edge("Washington D.C.", "Baltimore", directed = False, weight = 40)
    g.add_edge("Washington D.C.", "Frederick", directed = False, weight = 50)

    g.add_edge("Frederick", "Baltimore", directed = False, weight = 48)

    return g

main()


import aiosqlite
from aiohttp import web
from aiohttp_swagger import *
from aiojobs.aiohttp import setup, spawn, get_scheduler_from_app
from core.matcher import MQTTMatcher
from hbmqtt.broker import Broker
from hbmqtt.client import MQTTClient
from hbmqtt.mqtt.constants import QOS_1

from core.websocket import websocket_handler

TEST_DB = "test.db"
c = MQTTClient()
import asyncio

matcher = MQTTMatcher()

config = {
    'listeners': {
        'default': {
            'type': 'tcp',
            'bind': '0.0.0.0:1885',
        },
        'my-ws-1': {
            'bind': '0.0.0.0:8888',
            'type': 'ws'
        }
    },

    'sys_interval': 10,
    'auth': {
        'allow-anonymous': True,
    }
}

broker = Broker(config, plugin_namespace="hbmqtt.test.plugins")

async def test2(name):
    while True:
        print(name)
        await asyncio.sleep(1)

async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name

    return web.Response(text=text)

async def test_connection():
    async with aiosqlite.connect(TEST_DB) as db:

        assert isinstance(db, aiosqlite.Connection)

app = web.Application()

async def listen_to_redis(app):
    while True:
        await asyncio.sleep(1)

        #for w in _ws:
        #    pass
            # await w.send_str("HALLO")

            # print(w)

async def myjob(app):
    while True:
        await asyncio.sleep(1)

def ok_msg(msg):
    pass

def ok_msg1(msg):
    pass

def ok_msg2(msg):
    pass

mqtt_methods = {"test": ok_msg, "test/+/ab": ok_msg1, "test/+": ok_msg2}

async def on_message():
    while True:
        message = await c.deliver_message()
        matched = False
        packet = message.publish_packet
        print(message.topic)
        print(message.topic.split('/'))
        data = packet.payload.data.decode("utf-8")

        for callback in matcher.iter_match(message.topic):

            callback(data)
            matched = True

        if matched == False:
            print("NO HANDLER", data)

        #for w in _ws:
        #    await w.send_str(data)

async def start_background_tasks(app):
    app['redis_listener'] = app.loop.create_task(listen_to_redis(app))

async def start_broker(app):

    await broker.start()

    await c.connect('mqtt://localhost:1885')

    for k, v in mqtt_methods.items():
        print(k, v)
        await c.subscribe([(k, QOS_1)])

        matcher[k] = v
    # await c.subscribe([('/test', QOS_1),('/hallo', QOS_1)])

    await get_scheduler_from_app(app).spawn(on_message())

job = None

async def start_task(request):
    global job
    job = await spawn(request, myjob(app))
    await test_connection()
    return web.Response(text="OK")

async def stop_task(request):
    await job.close()
    return web.Response(text="OK")

async def stats(request):
    s = get_scheduler_from_app(app)

    return web.Response(text="%s" % s.active_count)

setup(app)

def start_bg(app, name, method):
    print("HALLO111")

    async def start(app):
        app[name] = app.loop.create_task(method(name))

    app.on_startup.append(start)

# start_bg(app, "test", test2)
# start_bg(app, "test2", test2)

#app.on_startup.append(start_background_tasks)
app.on_startup.append(start_broker)

app.add_routes([web.get('/', handle),
                web.get('/stop', stop_task),
                web.get('/start', start_task),
                web.get('/stats', stats),
                web.get('/ws', websocket_handler),
                web.get('/{name}', handle)

                ])

setup_swagger(app)

web.run_app(app)


"""
	String sampling
"""
from RANDOM import unif_int

def unif_string(A, k):
	"Sample a uniform k-mer over alphabet A"
	N = len(A)
	kmer = [A[unif_int(0, N)] for _ in range(k)]
	return ''.join(kmer)

def main():
	A = input()
	n, k = map(int, input().split())
	for _ in range(n):
		print(unif_string(A, k))

if __name__ == '__main__': main()


import tensorflow as tf
import os

def pb2txt(pb_file, txt_file=None):
    if txt_file is None:
        txt_file = pb_file + '.txt'
    with tf.gfile.GFile(pb_file, "rb") as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            tf.train.write_graph(graph_def,os.path.dirname(txt_file), txt_file, True)
    return txt_file


import cv2
import numpy as np

DEBUG = True # set to True to show more debug figures

class Settings:
  source = cv2.imread("./pics/blemish.png", cv2.IMREAD_COLOR)
  h,w,c = source.shape
  crop_zone = None
  click_pos = (0, 0)
  orig_shift = [0, 0]
  radius = 15
  block_size = 2*radius

def tile_sub_blocks(sub_blocks ,bsize):
  ''' Debug function
      Show complete sub blocks
  '''
  padding = 1
  sub_blk = sub_blocks.copy()
  for i in range(9):
    bg = np.zeros((bsize+2*padding,bsize+2*padding,3),
      dtype=sub_blk[i].dtype)
    try:
      bg[padding:bsize+padding,padding:bsize+padding] = sub_blk[i]
    except:
      pass
    sub_blk[i] = bg

  blocks_tile = np.vstack((
    np.hstack((sub_blk[0],sub_blk[1],sub_blk[2])),
    np.hstack((sub_blk[3],sub_blk[4],sub_blk[5])),
    np.hstack((sub_blk[6],sub_blk[7],sub_blk[8])),
    ))
  cv2.imshow('debug: tile sub blocks', blocks_tile)

def get_freq():
  ''' TODO
  Maybe use frequency methods as a LPF to make selected patch smoother...
  '''
  pass

def get_gradients(blocks, bsize):
  '''
  Calculate Gxy of (blurred) blocks)
  '''
  gradients = {}
  for i, blk in enumerate(blocks):
    if blk.shape == (bsize, bsize, 3):
      blk = cv2.cvtColor(blk, cv2.COLOR_BGR2GRAY)
      gx = np.mean(cv2.Sobel(blk,cv2.CV_32F,1,0,ksize=3))
      gy = np.mean(cv2.Sobel(blk,cv2.CV_32F,0,1,ksize=3))
      gxy = np.sqrt(gx*gx+gy*gy)
      gradients[i] = gxy
    else:
      print(i, 'incomplete block with shape of {}'.format(blk.shape))
  return gradients

def apply_patch(sub_blocks, gradients):
  '''
  Sort the gradients of sub blocks,
  and then choose the one with the least Gxy value as the patch.
  Finally, clone the patch to the source image.
  '''
  sorted_gradients = sorted(gradients, key=gradients.get)
  patch_idx = sorted_gradients[0] if sorted_gradients[0] != 4 else sorted_gradients[1]
  patch = sub_blocks[patch_idx]
  target = Settings.source #sub_blocks[4]
  center = Settings.click_pos #(Settings.radius, Settings.radius)
  if DEBUG:
    cv2.imshow('patch', patch)
  patch_mask = np.zeros(patch.shape, patch.dtype)
  patch_mask = cv2.circle(patch_mask,(Settings.radius, Settings.radius),
                          Settings.radius, (255,255,255), -1)
  Settings.source = cv2.seamlessClone(patch, target, patch_mask, center, cv2.NORMAL_CLONE)

def get_subzones(blurred_roi):
  '''
  According to the click position, divide the cropped ROI into sub blocks.
  If the whole ROI were within the source image, then there'll be 3x3 tiled blocks.
  The center one should contain the user clicked blemish.
  The `tile_sub_blocks()` shows all complete blocks.
  '''
  bsize = Settings.block_size
  shift = Settings.orig_shift
  idx = np.array([[shift[1]+i*bsize,shift[1]+(i+1)*bsize,
                   shift[0]+j*bsize,shift[0]+(j+1)*bsize]
                   for i in range(3) for j in range(3)])
  idx[idx <= 0] = 0 #set negative idx to zero

  blurred_blocks = [blurred_roi[idx[b][0]:idx[b][1], idx[b][2]:idx[b][3]]
                for b in range(9)]
  sub_blocks = [Settings.crop_zone[idx[b][0]:idx[b][1], idx[b][2]:idx[b][3]]
                for b in range(9)]

  # Use blurred blocks to find the usable patch
  if DEBUG:
    tile_sub_blocks(blurred_blocks, bsize)
  gradients = get_gradients(blurred_blocks, bsize)
  # Apply the original (un-blurred) patch to the source image
  apply_patch(sub_blocks, gradients)

def get_roi(action, x, y, flags, userdata):
  radius3 = Settings.radius*3
  if action==cv2.EVENT_LBUTTONDOWN:
    Settings.click_pos = (x, y)

    xmin = x - radius3
    if xmin < 0:
      Settings.orig_shift[0] = xmin
      xmin = 0
    else:
      Settings.orig_shift[0] = 0
    ymin = y - radius3
    if ymin < 0:
      Settings.orig_shift[1] = ymin
      ymin = 0
    else:
      Settings.orig_shift[1] = 0

    xmax = x + radius3
    if xmax > Settings.w:
      xmax = Settings.w
    ymax = y + radius3
    if ymax > Settings.h:
      ymax = Settings.h

    Settings.crop_zone = Settings.source[ymin:ymax,xmin:xmax].copy()
    # Filter out salt-and-pepper noise
    blurred_roi = Settings.crop_zone.copy()
    blurred_roi = cv2.medianBlur(blurred_roi, ksize=3)
    #blurred_roi = cv2.GaussianBlur(blurred_roi, ksize=(3,3), sigmaX=0)
    get_subzones(blurred_roi)

  elif action==cv2.EVENT_RBUTTONDOWN:
    Settings.crop_zone = None # reset
    cv2.destroyWindow("ROI")

  if Settings.crop_zone is not None and DEBUG:
    cv2.imshow("ROI", Settings.crop_zone)

def main():
  cv2.namedWindow("Source Image")
  cv2.setMouseCallback("Source Image", get_roi)

  while True:
    cv2.imshow("Source Image", Settings.source)

    if cv2.waitKey(1) & 0xFF == ord('q'):
      break

  cv2.destroyAllWindows()

if __name__ == "__main__":
  main()


from azure.storage.queue import QueueService
import requests
import json

# variables – needs azure queue name, key, and docker api url
storagacct = "<storage acct>"
queue = "<queue>"
queuekey = "<queue key>"
docker = "http://<docekr host>:2375/containers/"
image = "neilpeterson/domain-search"

while True:
    # set up azure queue
    queue_service = QueueService(account_name=storagacct, account_key=queuekey)

    # get messages from azure queue
    messages = queue_service.get_messages(queue, num_messages=2)

    # delete from queue, create container, start container
    for message in messages:

        # get domain and construct json
        d = message.content
        data = json.loads('{"Image": "' + image + '", "Cmd": ["--domain=' + d + '"]}')

        # delete message from azure queue
        queue_service.delete_message(queue, message.id, message.pop_receipt)

        # create and start docker container
        headers = {'Content-Type': 'application/json'}
        r = requests.post(docker + "create", data=json.dumps(data), headers=headers)
        b = json.loads(r.text)
        x = requests.post(docker + b['Id'] + "/start")


terms = {
    "lol": "Laugh out loud",
    "wth": "What the hell",
    "idk": "I don't know",
}

while True:
    key = input("Your term? ")
    if key in terms:
        print(terms[key])
    else:
        print("Not found")
        contribute = input("Do you want to contribute? ").upper()
        if contribute == "Y":
            value = input("Your explanation? ")
            terms[key] = value
            print("Added")
    print()


"""
The typical macro ratio for keto looks like the following:

    5% of calories coming from carbs
    25% of calories coming from protein
    70% of calories coming from fat

    This script assumes you are using the 4-4-9 method which is not the most precise but still a good indicator
    of how you are doing. Essentially it is the net carbs * 4, protein * 4, and fat * 9

    I also convert everything to ounces because USA #1
    Food macros from https://www.nutritionix.com
"""

class Food:
    def __init__(self, name, fat, protein, total_carbohydrates, dietary_fiber, units, unit_measurement):
        # Assume that everything but the units are in grams
        self.name = name
        self.fat = fat * 9
        self.protein = protein * 4
        self.total_carbohydrates = total_carbohydrates
        self.dietary_fiber = dietary_fiber
        self.net_carbohydrates = (total_carbohydrates - dietary_fiber) * 4
        if unit_measurement == 'gram':
            self.units = units * 0.035274
        elif unit_measurement == 'cup':
            self.units = units / 8
        elif unit_measurement == 'liter':
            self.units = units / 33.814
        elif unit_measurement == 'tablespoon':
            self.units == units * .5
        else:
            self.units = units
        self.unit_measurement = unit_measurement
        self.calories = self.fat + self.protein + self.net_carbohydrates

    def calculate_ratios(self):
        print(f"""Calories: {self.calories} | Fiber {self.dietary_fiber}
Fat / Protein / Net Carb ratios:
{round(self.fat/self.calories, 2)} / {round(self.protein/self.calories, 2)} / {round(self.net_carbohydrates/self.calories, 2)}""")

def calculate_meal_macros(*ingredients):
    # takes in tuples of foods and how many ounces you are eating of it
    calories = 0
    fat = 0
    protein = 0
    fiber = 0
    net_carbs = 0
    for ingredient in ingredients:
        ratio = ingredient[1] / ingredient[0].units
        calories += ingredient[0].calories * ratio
        fat += ingredient[0].fat * ratio
        protein += ingredient[0].protein * ratio
        fiber += ingredient[0].dietary_fiber * ratio
        net_carbs += ingredient[0].net_carbohydrates * ratio
    fat_ratio = round(fat/calories, 2)
    protein_ratio = round(protein/calories, 2)
    net_carb_ratio = round(net_carbs/calories, 2)
    print(f"Calories: {round(calories, 2)} | Fiber {round(fiber, 2)}")
    print("Fat / Protein / Net Carb ratios:")
    print(f"{fat_ratio} / {protein_ratio} / {net_carb_ratio}")

avocado = Food("avocado",
          fat=29,
          protein=4,
          total_carbohydrates=17,
          dietary_fiber=13,
          units=201,
          unit_measurement='gram',
)

bacon = Food(
    name="bacon",
    fat=12,
    protein=12,
    total_carbohydrates=.6,
    dietary_fiber=0,
    units=35,
    unit_measurement='gram',
)

large_egg = Food(
    name="egg",
    fat=4.8,
    protein=6.3,
    total_carbohydrates=.4,
    dietary_fiber=0,
    units=50,
    unit_measurement='gram',
)

almonds = Food(
    name="almonds",
    fat=.7,
    protein=.3,
    total_carbohydrates=.3,
    dietary_fiber=.1,
    units=1.3,
    unit_measurement='gram',
)

brocolus = Food(
    name="brocolus",
    fat=.1,
    protein=.9,
    total_carbohydrates=2.7,
    dietary_fiber=1.2,
    units=37,
    unit_measurement='gram',
)

chicken_breast = Food(
    name="chicken_breast",
    fat=4.3,
    protein=37,
    total_carbohydrates=0,
    dietary_fiber=0,
    units=120,
    unit_measurement='gram',
)

cauliflower = Food(
    name="cauliflower",
    fat=2.6,
    protein=11,
    total_carbohydrates=24,
    dietary_fiber=13,
    units=575,
    unit_measurement='gram',
)
calculate_meal_macros(
    (avocado, 6),
    (bacon, 3),
    (large_egg, 1),
    (almonds, 4),
    (chicken_breast, 5),
    (cauliflower, 4),
    (brocolus, 4),
)


from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MinLengthValidator, MinValueValidator, MaxLengthValidator, MaxValueValidator
from django.utils.translation import gettext_lazy as _

class Post(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    title_en = models.CharField(_('Title (en)'),
                                max_length=64,
                                help_text=_('Add the english title of your post'),
                                null=False, blank=False,
                                validators=[
                                    MaxLengthValidator(64)
                                ]
                                )
    title_de = models.CharField(_('Title (de)'),
                                max_length=64,
                                help_text=_('Add the german title of your post'),
                                null=True, blank=True,
                                validators=[
                                    MaxLengthValidator(64)
                                ]
                                )
    date = models.DateTimeField(_('Date'),
                                help_text=_('posted on'),
                                auto_now_add=True,
                                editable=False)
    edited = models.DateTimeField(_('Edited'),
                                  help_text=_('edited on'),
                                  null=True, blank=True,
                                  )
    content_en = models.TextField(_('Content (en)'),
                                  help_text=_('Write the english translation of your post'),
                                  null=False, blank=False,
                                  validators=[
                                      MaxLengthValidator(4096)
                                  ]
                                  )
    content_de = models.TextField(_('Content (de)'),
                                  help_text=_('Write the german translation of your post'),
                                  null=True, blank=True,
                                  validators=[
                                      MaxLengthValidator(4096)
                                  ]
                                  )
    likes = models.IntegerField(_('Likes'),
                                default=0,
                                validators=[
                                    MinValueValidator(0)
                                ]
                                )

    def __repr__(self):
        return self.title_en

    def __str__(self):
        return self.title_en

    class Meta:
        ordering = ['-date']
        verbose_name = _('Post')
        verbose_name_plural = _('Posts')

class Comment(models.Model):
    pass

    class Meta:
        verbose_name = _('Comment')
        verbose_name_plural = _('Comments')

class PostImage(models.Model):
    pass

    class Meta:
        verbose_name = _('Post image')
        verbose_name_plural = _('Post images')


import httplib2
import os
import sys
import argparse
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser
from oauth2client import tools

    # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
    # the OAuth 2.0 information for this application, including its client_id and
    # client_secret. You can acquire an OAuth 2.0 client ID and client secret from
    # the Google Cloud Console at
    # https://cloud.google.com/console.
    # Please ensure that you have enabled the YouTube Data API for your project.
    # For more information about using OAuth2 to access the YouTube Data API, see:
    #   https://developers.google.com/youtube/v3/guides/authentication
    # For more information about the client_secrets.json file format, see:
    #   https://developers.google.com/api-client-library/python/guide/aaa_client_secrets

CLIENT_SECRETS_FILE = "API/client_secret.json"

    # This variable defines a message to display if the CLIENT_SECRETS_FILE is
    # missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0

To make this sample run you will need to populate the client_secrets.json file
found at:

%s

with information from the Cloud Console
https://cloud.google.com/console

For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
                               CLIENT_SECRETS_FILE))

    # This OAuth 2.0 access scope allows for full read/write access to the
    # authenticated user's account.
YOUTUBE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

tools.argparser.add_argument('-v', '--video-id', type=str, required=True, help='The id of the video')
tools.argparser.add_argument('-p', '--playlist-id', type=str, required=True,
                             help='The id of the playlist')
args = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()

def get_authenticated_service():
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)

    storage = Storage("API/oauth2.json")
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        credentials = tools.run_flow(flow, storage,  tools.argparser.parse_args())

    return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
        http=credentials.authorize(httplib2.Http()))

def add_video_to_playlist(youtube,videoID,playlistID):
    add_video_request=youtube.playlistItems().insert(
    part="snippet",
    body={
        'snippet': {
            'playlistId': playlistID,
            'resourceId': {
                    'kind': 'youtube#video',
                'videoId': videoID
            }
            #'position': 0
        }
}
).execute()

if __name__ == '__main__':
    youtube = get_authenticated_service()
    video = sys.argv[1]
    playlist = sys.argv[2];
    add_video_to_playlist(youtube,args.video_id,args.playlist_id);


"""
Create and initialize the admin or site application, based on the
environment.

:Copyright: 2014-2023 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

import os

from byceps.application import create_admin_app, create_site_app
from byceps.config import ConfigurationError
from byceps.util.sentry import configure_sentry_from_env

configure_sentry_from_env()

app_mode = os.environ.get('APP_MODE')
if app_mode == 'admin':
    app = create_admin_app()
elif app_mode == 'site':
    app = create_site_app()
else:
    raise ConfigurationError(
        'Unknown or no app mode configured for configuration key "APP_MODE".'
    )


import cv2
import glob
import os
import sys
import numpy as np
from time import sleep, time

normal_cnt = 0
broken_cnt = 0

dataPath = "/home/team/project/GAN/bean/data"
normal_imgs = glob.glob(os.path.join(dataPath+"/normal_data/normal_third_processed_data",'*.jpg'))
normal_imgs = glob.glob(os.path.join(dataPath+"/broken_data/broken_third_processed_data",'*.jpg'))
# normal_imgs = glob.glob(os.path.join(dataPath+"/normal_data/normal_rotated_data",'*.jpg'))

for x in normal_imgs:
    img = cv2.imread(x, cv2.IMREAD_COLOR)
    imgray = cv2.imread(x, cv2.IMREAD_GRAYSCALE)
    bitwise = cv2.bitwise_not(imgray)
    _, thr = cv2.threshold(bitwise, 50, 255, cv2.THRESH_BINARY)
    # 검은 배경 생성
    _, white = cv2.threshold(imgray, 0, 255, cv2.THRESH_BINARY)
    # black을 다시 3 channel로 변경 => 초록 타원을 씌우기 위함
    white = cv2.cvtColor(white, cv2.COLOR_GRAY2BGR)

    # cv2.imshow("img", img)
    # cv2.imshow("imgray", imgray)
    # cv2.imshow("black", black)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    _, contour, _ = cv2.findContours(thr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # contour 중에서 최대로 많은 것 탐색
    max_iter = 0
    max_len = 0
    for i in range(len(contour)):
        if len(contour[i]) > max_len:
            max_len = len(contour[i])
            max_iter = i
    # sleep(10)
    con = contour[max_iter]
    (x, y), r = cv2.minEnclosingCircle(con)
    # cv2.circle(img, (int(x), int(y)), int(r), (0, 0, 255), 2)
    ellipse = cv2.fitEllipse(con)
    # black에 ellipse를 그림
    cv2.ellipse(white, ellipse, (0, 255, 0), -1)

    # cv2.imshow("white",white)
    # cv2.imshow("img",img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    start = time()
    check_num = 0
    white_mat = white[0][0]
    is_broken = False

    # normal => 12 정도 limit
    # broken => 들쭉날쭉 ..
    limit_cnt = 0
    for i in range(white.shape[0]):
        for j in range(white.shape[1]):
            r_number = white[i][j][0]
            if r_number == check_num and \
               img[i][j][0] == 255 and \
               img[i][j][1] == 255 and \
               img[i][j][2] == 255:
                is_broken = True
                limit_cnt += 1
                # break
        # if is_broken:
        #     break
    # print("end : " + str( time() - start ))
    # sleep(10)
    if is_broken:
        broken_cnt += 1
    else:
        normal_cnt += 1

    print("broken cnt : %d \nnormal cnt : %d"%(broken_cnt, normal_cnt))
    print("limit_cnt : " + str(limit_cnt))

    # cv2.imshow("black", black)
    # cv2.imshow('img', img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    # 녹색으로 다 채운(-1)
    # 녹색 픽셀 획득
    # bgr:  [0, 255, 0]
    # hsv:  [ 60 255 255]
    # 녹색 픽셀 중 배경색(흰색)과 같은 픽셀이 있다 => 브로큰!!


class Solution:
    """
    @param: s: A string
    @param: wordDict: A set of words.
    @return: All possible sentences.
    """
    def wordBreak(self, s, wordDict):
        # write your code here
        results = []
        dp = self.calc(s, wordDict)
        self.dfs(s, wordDict, 0, [], results, dp)
        return results

    def dfs(self, s, dict, start, candidates, res, dp):
        n = len(s)
        if start == n:
            res.append(' '.join(candidates))
            return

        for i in range(start,n+1):
            if i<n and not dp[i]:
                continue
            prefix = s[start:i]
            if prefix not in dict or prefix == '':
                continue
            candidates.append(prefix)
            self.dfs(s, dict, i, candidates, res, dp)
            candidates.pop()
        return

    def calc(self, s, dict):
        # write your code here
        n = len(s)
        f = [False] * (n+1)
        f[0] = True

        for i in range(1, n+1):
            for j in range(i):
                f[i] = f[j] and s[j:i] in dict
                if f[i]:
                    break

        return f
"""
1. compare with return value edition
2. Add DP
3. string slicing
"""


language = input('Выберите язык ("ру" - русский, иначе английский): ') == 'ру'
alphabet = 'абвгдежзийклмнопрстуфхцчшщъыьэюя' if language else 'abcdefghijklmnopqrstuvwxyz'

direction = input('Выберите направление кодирования ("ш" - шифрование, иначе дешифрование): ') == 'ш'
length = len(alphabet)

rot = input(f'Выберите шаг сдвига от 1 до {length - 1}: ')
while not (rot.isdigit() and 1 <= int(rot) < length):
    rot = input(f'Введите натуральное число от 1 до {length - 1}: ')
rot = int(rot)

if direction:
    key = alphabet[rot:] + alphabet[:rot]
else:
    key = alphabet[-rot:] + alphabet[:-rot]

message = list(input('Введите сообщение:\n'))

for i, c in enumerate(message):
    if c.isalpha():
        d = key[alphabet.find(c.lower())]
        if c.isupper():
            d = d.upper()
        message[i] = d

print('Шифрованное' if direction else 'Дешифрованное', 'сообщение:')
print(''.join(message))

BEEGEEK. Python generation. Beginners/Caesar_cipher.py


#!/usr/bin/python
# -*- coding: utf-8 -*-
from collections import defaultdict

__author__ = 'kang'

# from kang_dev.NB import Pool
from kang_dev.NB1 import Pool
from parsers import string_parser
import os
import random
import re
Classes = ["clinton",  "lawyer",  "math",  "medical",  "music",  "sex"]
path = 'learn_and_test\learn\\'
p = Pool()

'''the k-folds to test the accuracy'''
result = {}
k_folds_random = {}
for c in Classes:
    result[c] = 0
k = 10
k_folds_obj = {}
for i in range(0, k):
    k_folds_obj[i] = {}

# put all the files together
cate = defaultdict(list)
for i in Classes:
    k_folds_random.update(p.k_folds(path + i, i))
    cate[i].append('')
# split them into k folds
for (key, v) in k_folds_random.items():
    number = random.randint(0, k-1)
    k_folds_obj[number][key] = v

counter = defaultdict()
count = defaultdict()
for cc in Classes:
    count[cc] = 0
    counter[cc] = 0

# the k times loop
for times in range(0, k-1):
    # the 1/k of the whole files used to test
    the_rest = k_folds_obj.pop(times)

    #compute the numbers of the test sample in every category
    for (key, v) in the_rest.items():
        counter[v] += 1
    print (counter)

    # before to learn
    for (dic, value) in k_folds_obj.items():
        for (key, v) in value.items():
            cate[v].append(key)

    # to learn
    for (key2, value2) in cate.items():
        p.learn_2(path + key2, key2, value2)

    # test
    print (the_rest)
    for (key, v) in the_rest.items():
        # name = re.match(r'^[a-zA-Z]+', key)
        res = p.Probability(path + v + "\\" + key)
        name = res[0][0]
        if name == v:
            count[v] += 1

for (key1, value1) in counter.items():
    print ('the ' + str(key1) + ' is {}'.format(float(count[key1])/float(value1)))

'''to learn and test'''
# for i in Classes:
#     p.learn(path + i, i)

# base = 'learn_and_test\\test\\'
# for i in Classes:
#     dir = os.listdir(base + i)
#     count = 0
#     for file in dir:
#         res = p.Probability(base + i + "\\" + file)
#         name = res[0][0]
#
#         if name == i:
#             count += 1
#         print(i + ": " + file + ": " + str(res))
#
#     print(float(count)/float(len(dir)))


"""
Copyright (c) 2004-Present Pivotal Software, Inc.

This program and the accompanying materials are made available under
the terms of the under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from mpp.models import MPPTestCase

from gppylib.commands.base import Command
from mpp.gpdb.tests.storage.walrepl.run import StandbyRunMixin

class gpstart(StandbyRunMixin, MPPTestCase):
    def test_start(self):
        """
        Verify if gpstart stops the standby and gpstart starts it
        correctly.
        """

        # set up standby
        res = self.standby.create()
        self.assertEqual(res, 0)
        res = self.standby.start()
        self.assertTrue(res.wasSuccessful())

        # stop the whole cluster including standby
        cmd = Command("gpstop", "gpstop -a")
        cmd.run()
        self.assertEqual(cmd.get_results().rc, 0, str(cmd))

        # check if standby is shut down
        cmd = Command("pg_ctl status",
                      "pg_ctl status -D {0}".format(self.standby.datadir))
        cmd.run()
        # pg_ctl status returns 1 if it's not running
        self.assertEqual(cmd.get_results().rc, 1, str(cmd))

        # bring up the cluster.  gpstart should bring up the standby, too.
        cmd = Command("gpstart", "gpstart -a")
        cmd.run()
        self.assertTrue(cmd.get_results().rc in (0, 1), str(cmd))

        # wait for the standby to connect to the primary
        num_walsender = self.wait_for_walsender()
        self.assertEqual(num_walsender, 1)

        # make sure pg_ctl also agrees it's running
        cmd = Command("pg_ctl status",
                      "pg_ctl status -D {0}".format(self.standby.datadir))
        cmd.run()
        self.assertEqual(cmd.get_results().rc, 0, str(cmd))


# -*- coding: iso8859-1 -*-
import os
import sys
import string
class Translate:
	items = []

	def __init__(self):
		caminho = os.environ['PYTHONPATH'].split(os.pathsep)[0] + "/language/qLanguage_en.txt"

		file = open( caminho, "r")
		lines = map(string.strip,file.readlines())
		for line in lines:
			dic = line.split("#")
			self.items.append({dic[0]:dic[1]})

		file.close()

	def translateById(self, id):
		for x in self.items:
			if id == x.keys()[0]:
				return x.values()[0]


import os
import subprocess
import string
from LatinoAnalysis.Tools.commonTools import *

# samples

#samples = {}

################################################
################# SKIMS ########################
################################################

#skim=''
skim='__wwSel'
#skim='__topSel'
#skim='__topSel'
#skim='__vh3lSel'
#skim='__sfSel'
#skim='__vbsSel'
#skim='__ssSel'
skimFake=skim

##############################################
###### Tree Directory according to site ######
##############################################
xrootdPath  = 'root://eoscms.cern.ch/'
treeBaseDir = '/eos/cms/store/group/phys_higgs/cmshww/amassiro/Full2016_Apr17/'

directory = treeBaseDir+'Apr2017_summer16/lepSel__MCWeights__bSFLpTEffMulti__cleanTauMC__l2loose__hadd__l2tightOR__LepTrgFix__dorochester__formulasMC'+skim+'/'

################################################
############ BASIC MC WEIGHTS ##################
################################################

XSWeight      = 'XSWeight'
SFweight      = 'SFweight2l'
GenLepMatch   = 'GenLepMatch2l'
################################################
############### B-Tag  WP ######################
################################################

bAlgo='cmvav2'
#bAlgo='csvv2ivf'
#bAlgo='DeepCSVB'

bWP='L'
#bWP='M'
#bWP='T'

# ... bPog SF and b veto

bSF='1.'
bVeto='1'
if   bAlgo == 'cmvav2' :
 bSF='bPogSF_CMVA'+bWP
 bVeto='bveto_CMVA'+bWP
elif bAlgo == 'csvv2ivf' :
 bSF='bPogSF_CSV'+bWP
 bVeto='bveto_CSV'+bWP
elif bAlgo == 'DeepCSVB' :
 bSF='bPogSF_deepCSV'+bWP
 bVeto='bveto_deepCSV'+bWP

SFweight += '*'+bSF

################################################
############### Lepton WP ######################
################################################

#... Electron:

#eleWP='cut_WP_Tight80X'
#eleWP='cut_WP_Tight80X_SS'
#eleWP='mva_80p_Iso2015'
#eleWP='mva_80p_Iso2016'
#eleWP='mva_90p_Iso2015'
eleWP='mva_90p_Iso2016'

#... Muon:

muWP='cut_Tight80x'

#... Build formula

LepWPCut        = 'LepCut2l__ele_'+eleWP+'__mu_'+muWP
LepWPweight     = 'LepSF2l__ele_'+eleWP+'__mu_'+muWP

SFweight += '*'+LepWPweight+'*'+LepWPCut

fakeW = 'fakeW2l_ele_'+eleWP+'_mu_'+muWP

################################################
############   MET  FILTERS  ###################
################################################

METFilter_MC   = 'METFilter_MC'
METFilter_DATA = 'METFilter_DATA'

################################################
############ DATA DECLARATION ##################
################################################

DataRun = [
            ['B','Run2016B-03Feb2017_ver2-v2'] ,
            ['C','Run2016C-03Feb2017-v1'] ,
            ['D','Run2016D-03Feb2017-v1'] ,
            ['E','Run2016E-03Feb2017-v1'] ,
            ['F','Run2016F-03Feb2017-v1'] ,
            ['G','Run2016G-03Feb2017-v1'] ,
            ['H','Run2016H-03Feb2017_ver2-v1'] ,
            ['H','Run2016H-03Feb2017_ver3-v1'] ,
          ]

DataSets = ['MuonEG','DoubleMuon','SingleMuon','DoubleEG','SingleElectron']

DataTrig = {
            'MuonEG'         : ' trig_EleMu' ,
            'DoubleMuon'     : '!trig_EleMu &&  trig_DbleMu' ,
            'SingleMuon'     : '!trig_EleMu && !trig_DbleMu &&  trig_SnglMu' ,
            'DoubleEG'       : '!trig_EleMu && !trig_DbleMu && !trig_SnglMu &&  trig_DbleEle' ,
            'SingleElectron' : '!trig_EleMu && !trig_DbleMu && !trig_SnglMu && !trig_DbleEle &&  trig_SnglEle' ,
           }

###########################################
#############  BACKGROUNDS  ###############
###########################################

###### DY #######

### These weights were evaluated on ICHEP16 MC -> Update ?
ptllDYW_NLO = '(0.876979+gen_ptll*(4.11598e-03)-(2.35520e-05)*gen_ptll*gen_ptll)*(1.10211 * (0.958512 - 0.131835*TMath::Erf((gen_ptll-14.1972)/10.1525)))*(gen_ptll<140)+0.891188*(gen_ptll>=140)'
ptllDYW_LO  = '(8.61313e-01+gen_ptll*4.46807e-03-1.52324e-05*gen_ptll*gen_ptll)*(1.08683 * (0.95 - 0.0657370*TMath::Erf((gen_ptll-11.)/5.51582)))*(gen_ptll<140)+1.141996*(gen_ptll>=140)'

samples['DY'] = {    'name'   :     getSampleFiles(directory,'DYJetsToLL_M-10to50')
                                  + getSampleFiles(directory,'DYJetsToTT_MuEle_M-50_ext1') \
                                  + getSampleFiles(directory,'DYJetsToLL_M-10to50'),
                     'weight' : XSWeight+'*'+SFweight+'*'+GenLepMatch+'*'+METFilter_MC ,
                     'FilesPerJob' : 2 ,
                 }

addSampleWeight(samples,'DY','DYJetsToLL_M-10to50',ptllDYW_NLO)
addSampleWeight(samples,'DY','DYJetsToTT_MuEle_M-50_ext1',ptllDYW_NLO)

###### Top #######
Top_pTrw = '(TMath::Sqrt( TMath::Exp(0.0615-0.0005*topLHEpt) * TMath::Exp(0.0615-0.0005*antitopLHEpt) ) )'

samples['top'] = {   'name'     :   getSampleFiles(directory,'TTTo2L2Nu')
                                  + getSampleFiles(directory,'ST_tW_antitop')
                                  + getSampleFiles(directory,'ST_tW_top')
                                  + getSampleFiles(directory,'ST_t-channel_antitop')
                                  + getSampleFiles(directory,'ST_t-channel_top')
                                  + getSampleFiles(directory,'ST_s-channel')
                             ,
                      'weight' : XSWeight+'*'+SFweight+'*'+GenLepMatch+'*'+METFilter_MC ,
                      'FilesPerJob' : 2 ,
                  }

addSampleWeight(samples,'top','TTTo2L2Nu',Top_pTrw)

###### WW ########

samples['WW']  = {    'name'   : getSampleFiles(directory,'WWTo2L2Nu') ,
                      'weight' : XSWeight+'*'+SFweight+'*'+GenLepMatch+'*'+METFilter_MC + '*nllW' ,
                 }

samples['ggWW']  = {  'name'   : getSampleFiles(directory,'GluGluWWTo2L2Nu_MCFM'),
                      'weight' : XSWeight+'*'+SFweight+'*'+GenLepMatch+'*'+METFilter_MC ,
                      'isData': ['0'],
                   }

######### VZ #########

samples['VZ']  = {    'name':   getSampleFiles(directory,'ZZTo2L2Nu')
                              + getSampleFiles(directory,'WZTo2L2Q')
                              + getSampleFiles(directory,'ZZTo2L2Q')
                              # Should we include this as well here:
                              # + getSampleFiles(directory,'tZq_ll')
                              ,
                      'weight' : XSWeight+'*'+SFweight+'*'+GenLepMatch+'*'+METFilter_MC + '*1.11' ,
                      'FilesPerJob' : 6 ,
                  }

### 1.11 normalisation was measured in 3-lepton

########## VVV #########

samples['VVV'] = {    'name':   getSampleFiles(directory,'ZZZ')
                              + getSampleFiles(directory,'WZZ')
                              + getSampleFiles(directory,'WWZ')
                              + getSampleFiles(directory,'WWW')
                              ,
                      'weight' : XSWeight+'*'+SFweight+'*'+GenLepMatch+'*'+METFilter_MC ,
                  }

#### ggH -> WW

samples['ggH_hww']  = {  'name'  : getSampleFiles(directory,'GluGluHToWWTo2L2NuPowheg_M125') ,
                         'weight': XSWeight+'*'+SFweight+'*'+GenLepMatch+'*'+METFilter_MC+'*weight2MINLO' ,
                      }

#### VBF H->WW

samples['qqH_hww']  = {   'name' : getSampleFiles(directory,'VBFHToWWTo2L2NuPowheg_M125') ,
                         'weight': XSWeight+'*'+SFweight+'*'+GenLepMatch+'*'+METFilter_MC ,
                      }

samples['H_htt']    = {   'name' :   getSampleFiles(directory,'GluGluHToTauTau_M125')
                                     + getSampleFiles(directory,'VBFHToTauTau_M125')
                                     + getSampleFiles(directory,'HZJ_HToTauTau_M125')
                                     + getSampleFiles(directory,'HWplusJ_HToTauTau_M125')
                                     + getSampleFiles(directory,'HWminusJ_HToTauTau_M125') ,
                           'weight': XSWeight+'*'+SFweight+'*'+GenLepMatch+'*'+METFilter_MC ,
                           'suppressNegative' :['all'],
                           'suppressNegativeNuisances' :['all'],
                        }

###########################################
################## FAKE ###################
###########################################

samples['Fake_em']  = {'name': [ ] ,
                       'weight' : fakeW+'*veto_EMTFBug'+'*'+METFilter_DATA+'*(abs(std_vector_lepton_flavour[0])==11 && abs(std_vector_lepton_flavour[1])==13)',              #   weight/cut
                       'weights' : [ ] ,
                       'isData': ['all'],
                       'FilesPerJob' : 6 ,
                     }

samples['Fake_me']  = {'name': [ ] ,
                       'weight' : fakeW+'*veto_EMTFBug'+'*'+METFilter_DATA+'*(abs(std_vector_lepton_flavour[0])==13 && abs(std_vector_lepton_flavour[1])==11)',              #   weight/cut
                       'weights' : [ ] ,
                       'isData': ['all'],
                       'FilesPerJob' : 6 ,
                     }

for Run in DataRun :
  directory = treeBaseDir+'Apr2017_Run2016'+Run[0]+'_RemAOD/lepSel__EpTCorr__TrigMakerData__cleanTauData__l2loose__dorochester__multiFakeW__formulasFAKE__hadd'+skimFake+'/'
  for DataSet in DataSets :
    FileTarget = getSampleFiles(directory,DataSet+'_'+Run[1],True)
    for iFile in FileTarget:
      samples['Fake_em']['name'].append(iFile)
      samples['Fake_em']['weights'].append(DataTrig[DataSet])
      samples['Fake_me']['name'].append(iFile)
      samples['Fake_me']['weights'].append(DataTrig[DataSet])

###########################################
################## DATA ###################
###########################################

samples['DATA']  = {   'name': [ ] ,
                       'weight' : 'veto_EMTFBug'+'*'+METFilter_DATA+'*'+LepWPCut,
                       'weights' : [ ],
                       'isData': ['all'],
                       'FilesPerJob' : 6 ,
                  }

for Run in DataRun :
  directory = treeBaseDir+'Apr2017_Run2016'+Run[0]+'_RemAOD/lepSel__EpTCorr__TrigMakerData__cleanTauData__l2loose__hadd__l2tightOR__dorochester__formulasDATA'+skim+'/'
  for DataSet in DataSets :
    FileTarget = getSampleFiles(directory,DataSet+'_'+Run[1],True)
    for iFile in FileTarget:
      samples['DATA']['name'].append(iFile)
      samples['DATA']['weights'].append(DataTrig[DataSet])


import pygame
from pygame.locals import *
import sys
import random

pygame.init()
pygame.mixer.init()
clock = pygame.time.Clock()

# Creates the screen
WIDTH = 1280
HEIGHT = 720
screen = pygame.display.set_mode([WIDTH, HEIGHT])

# Loads the image
bridge = pygame.image.load('images/Bridge.jpg')
# Scales image to fit screen
bridge = pygame.transform.scale(bridge, (WIDTH, HEIGHT))
wreckage = pygame.image.load('images/wreckage.jpg')
wreckage = pygame.transform.scale(wreckage, (WIDTH, HEIGHT))
biodome = pygame.image.load('images/biodome.jpg')
biodome = pygame.transform.scale(biodome, (WIDTH, HEIGHT))

def darker():
    """Makes the image darker by dividing the pixels by 3"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            pxarray[X, Y] = (red/3, green/3, blue/3)

def lighter():
    """Makes the image lighter"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            if red < 86 and green < 86 and blue < 86:
                pxarray[X, Y] = (red*3, green*3, blue*3)

def invert():
    """inverts the image colours"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b

            red = 255 - red
            green = 255 - green
            blue = 255 - blue
            pxarray[X, Y] = (red, green, blue)

def greyscale():
    """converts the image to greyscale"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b

            grey = (red + green + blue)/3
            pxarray[X, Y] = (grey, grey, grey)

def bridge_lights():
    """Makes the lights turn off (go dark)"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            if red > 150 and green > 150 and blue > 150:
                pxarray[X, Y] = (red/3, green/3, blue/3)

def bridge_lights_red():
    """Makes the lights red"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            if red > 150 and green > 150 and blue > 150:
                red = red
                green /= 2
                blue /= 2
                pxarray[X, Y] = (red, green, blue)

def bridge_lights_red_effect():
    """Makes the bridge more"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            if 120 > red > 15 and 100 > green > 15 and 100 > blue > 15:
                # - 10 from each rgb value to make it slightly darker

                pxarray[X, Y] = (red + 35, green - 15, blue - 15)

def bridge_lights_effect():
    """Makes the bridge slightly darker"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            if 100 > red > 15 and 100 > green > 15 and 100 > blue > 15:
                # - 10 from each rgb value to make it slightly darker
                pxarray[X, Y] = (red - 15, green - 15, blue - 15)

def draw_bridge():
    """blits the images to the screen"""
    screen.blit(bridge, [0, 0])
    bridge.set_alpha(alpha)

def draw_wreckage():
    screen.blit(wreckage, [0, 0])
    wreckage.set_alpha(alpha)

def draw_biodome():
    screen.blit(biodome, [0, 0])
    biodome.set_alpha(alpha)

# status of bridge lights (on)
status = 1

# Setting different transparency values
alpha = 255
alphas = [255, 210, 170, 130, 100, 75]

# Setting different background colours
back_colour = (255, 255, 255)
back_colours = [(255, 255, 255), (255, 0, 0), (0, 188, 255), (0, 0, 0), (5, 255, 43), (242, 252, 45), (247, 27, 247)]

# wreckage image functions

updates = 0

def darken_all(n):
    update = n
    for y in xrange(1, HEIGHT - 1):
        for x in xrange(1, WIDTH - 1):
            if update <= 10:
                    darken_pixel(x, y, update)
            else:
                new_updates = (20 - update)
                red_updates = (updates - 10)
                darken_pixel(x, y, new_updates)
                redden_pixel(x, y, red_updates)

def darken_pixel(x, y, n):
    darken_amount = 10 * n
    red = screen.get_at((x, y)).r
    green = screen.get_at((x, y)).g
    blue = screen.get_at((x, y)).b
    if red > darken_amount:
        red -= darken_amount
    if green > darken_amount:
        green -= darken_amount
    if blue > darken_amount:
        blue -= darken_amount
    screen.set_at((x, y), (red, green, blue))

def redden_pixel(x, y, n):
    amount = 10 * n
    limit = 255 - amount
    red = screen.get_at((x, y)).r
    green = screen.get_at((x, y)).g
    blue = screen.get_at((x, y)).b
    if red <= limit:
        red += amount
    screen.set_at((x, y), (red, green, blue))

def alien_torch():
    (mouse_pos_x, mouse_pos_y) = pygame.mouse.get_pos()
    torch_radius = 40
    for y in xrange(1, HEIGHT - 1):
        for x in xrange(1, WIDTH - 1):
            if ((x - mouse_pos_x)**2 + (y - mouse_pos_y)**2) <= (torch_radius**2):
                screen.set_at((x, y), (wreckage.get_at((x, y))))

# Main loop
while True:
    key_pressed = pygame.key.get_pressed()

    # Press 1, 2, and 3 to display each image
    if key_pressed[pygame.K_1]:
        # Resets alpha to 255 (no transparency)
        alpha = 255
        draw_biodome()
    if key_pressed[K_2]:
        alpha = 255
        draw_bridge()
    if key_pressed[K_3]:
        alpha = 255
        draw_wreckage()

    pxarray = pygame.PixelArray(screen)

    # Resets the bridge image
    if key_pressed[K_SPACE]:
        # deletes pxarray to unlock the surface
        del pxarray
        draw_bridge()
        pxarray = pygame.PixelArray(screen)

    # Press/hold down to make image darker
    if key_pressed[K_DOWN]:
        darker()

    # Press/hold down to make image lighter
    if key_pressed[K_UP]:
        lighter()

    # Hold down right to flicker lights on bridge
    if key_pressed[K_RIGHT]:
        if status == 1:
            status = 0
            del pxarray
            draw_bridge()
            pxarray = pygame.PixelArray(screen)
            clock.tick(4)
        elif status == 0:
            status = 1
            bridge_lights()
            bridge_lights_effect()
            clock.tick(25)

    # Hold down left to flicker red lights on bridge
    if key_pressed[K_LEFT]:
        if status == 1:
            status = 0
            del pxarray
            draw_bridge()
            pxarray = pygame.PixelArray(screen)
            clock.tick(4)
        elif status == 0:
            status = 1
            bridge_lights_red()
            bridge_lights_red_effect()
            clock.tick(25)

    # Press i to invert image colours, press i again to undo invert effect
    if key_pressed[pygame.K_i]:
        invert()

    # Press g to convert image to greyscale
    if key_pressed[pygame.K_g]:
        greyscale()

    del pxarray

    # Hold down 4 to see wreckage effect and use alien torch
    if key_pressed[K_4]:
        clock.tick(60)
        screen.blit(wreckage, (0, 0))
        updates += 1
        darken_all(updates)
        alien_torch()

    # Hold down a to see flashing effect on biodome
    if key_pressed[pygame.K_a]:
        # Selects a random background colour and fills the screen with it.
        back_colour = random.choice(back_colours)
        screen.fill(back_colour)
        # Selects a random alpha variable and draws biodome with new alpha value
        alpha = random.choice(alphas)
        draw_biodome()
        # changes how frequently the colours and transparency changes
        clock.tick(5)

    # Hold down a to see flashing effect on bridge
    if key_pressed[pygame.K_s]:
        back_colour = random.choice(back_colours)
        screen.fill(back_colour)
        alpha = random.choice(alphas)
        draw_bridge()
        clock.tick(5)

    # Hold down a to see flashing effect on wreckage
    if key_pressed[pygame.K_d]:
        back_colour = random.choice(back_colours)
        screen.fill(back_colour)
        alpha = random.choice(alphas)
        draw_wreckage()
        clock.tick(5)

    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    # Press esc to exit
    if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            exit()


import optunity
import pandas as pd
import numpy as np
from script import plotter
import logging
import sys
import random
from script import testing
import matplotlib.pyplot as plt

modlog = logging.getLogger('initialize.rxnprng')

##Place holder function.  This can be fit to change the distribution of points at a later time. For now
## the method returns only the sobol value unmodified.  The sampling in each input x1, x2, x3 should be evenly distributed
## and unaltered.  (Blank function)
def f(x1):
    return 0.0

def initialrdf(volmax, volmin, reagent, wellnum):
    _, info_random, _ = optunity.minimize(f, num_evals=wellnum, x1=[volmin, volmax], solver_name='random search')
    ##Create quasi-random data spread
    reagentlist=info_random.call_log['args']['x1'] #Create reagent amounts (mmol), change x variable to change range, each generated from unique sobol index
    reagentname = "Reagent%s (ul)" %reagent
    rdf = pd.DataFrame({reagentname:reagentlist}).astype(int)
    return(rdf)
    #Generate a range of Amine concentrations dependent upon the range possible due to the presence of amine in Stock solution A \

def rdfbuilder(rvolmaxdf, rvolmindf, reagent, wellnum):
    count = 0
    reagentname = "Reagent%s (ul)" %reagent
    reagentlist = []
    while count < wellnum:
        volmax = rvolmaxdf[count]
        volmin = rvolmindf[count]
#        _, info_random, _ = optunity.minimize(f, num_evals=1, x1=[volmin, volmax], solver_name='random search')
#        reagentitem=info_random.call_log['args']['x1'] #Create reagent amounts (mmol), change x variable to change range, each generated from unique sobol index
        reagentitem = random.randint(volmin,volmax)
        reagentlist.append(reagentitem)
        count+=1
    rdf = pd.DataFrame({reagentname:reagentlist}).astype(int)
    return(rdf)

#very similar to vollimtcont
def calcvollimit(userlimits, rdict, volmax, volmin, experiment, reagentlist, reagent, wellnum):
    rdata = rdict['%s'%reagent]
    chemicals = rdata.chemicals
    possiblemaxvolumes = []
    possiblemaxvolumes.append(volmax)
    possibleminvolumes = []
    possibleminvolumes.append(volmin)
    # Determine if any user constraints override the maximum concentrations dependent on the volume limits
    for chemical in chemicals:
        #The code will update the maximum volume to meet the upper bound set by the user
        try:
            maxconc = maxconcdet(rdict, reagent, chemical)
            absmaxconc = absmaxcondet(rdict, reagent, chemical, reagentlist)
            userdefinedmax = (userlimits['chem%s_molarmax'%chemical])
            # Update the constraints / limits on the total volume of this reagent to fit within the restrictions that the user might have set
            if userdefinedmax <= maxconc:
                volmaxnew = userdefinedmax / maxconc * volmax
                possiblemaxvolumes.append(int(volmaxnew))
            # If other reagents in the portion of the reaction can access higher concentrations, don't error out
            elif userdefinedmax <= absmaxconc:
               pass
            else:
                #In the case where this upperbound cannot be met the user has overconstrained the system
                modlog.error("User defined concentration greater than physically possible for chemical%s in experiment %s" %(chemical, experiment))
                sys.exit()
        except Exception:
            pass
        #The code will update the minimum volume to meet the lower bound set by the user
        try:
            reagconc = (rdata.concs['conc_chem%s' %chemical])
            userdefinedmin = (userlimits['chem%s_molarmin'%chemical])
            absmaxconc = absmaxcondet(rdict, reagent, chemical, reagentlist)
            if userdefinedmin >= 0:
                if reagconc < absmaxconc:
                    pass
                else:
                    volminnew = userdefinedmin / reagconc * volmax
                    possibleminvolumes.append(int(volminnew))
            elif userdefinedmax <= absmaxconc:
                pass
            else: pass
        except Exception:
            pass
    # Take the values determined from the user constraints and pass along the relevant value to the calling function
    if (min(possiblemaxvolumes)) != volmax:
        # Flag that there are constraints so that the user is aware they are limiting the total physical space artificially
        modlog.warning("User has constrained the maximum search space by lowering the maximum value for a chemical in experiment %s reagent %s, be sure this is intentional" % (experiment, reagent))
        volmax = min(possiblemaxvolumes)
    if (max(possibleminvolumes)) != 0:
        modlog.warning("User has constrained the maximum search space by raising the minimum value for a chemical in experiment %s reagent %s, be sure this is intentional" % (experiment, reagent))
        volmin = max(possibleminvolumes)
    else: pass
    # Testing functions to ensure that all requirements / conditions for the volume are being met appropriately regardless of user specification
#    testing.reagenttesting(volmax,volmin)
    return(volmax, volmin)

def totalmmolchemicals(chemicals, usedchems, mmoldf):
    finalsummedmmols = pd.DataFrame()
    for chemical in chemicals:
        headerlist = []
        cfullname = 'chemical%s' %chemical
        for header in usedchems:
            if cfullname in header:
                headerlist.append(header)
        tempdf = pd.DataFrame()
        for header in headerlist:
            tempdf = pd.concat([tempdf, mmoldf[header]], axis=1)
        summedmmols = pd.DataFrame(tempdf.sum(axis=1))
        summedmmols.columns = ['mmol_%s' %cfullname]
        finalsummedmmols = pd.concat([finalsummedmmols, summedmmols], axis=1)
    finalsummedmmols.fillna(value=0, inplace=True) # Total mmmols added of each chemical in previous reagent additions
    return(finalsummedmmols)

def calcvollimitdf(rdf, mmoldf, userlimits, rdict, volmax, volmin, experiment, reagentlist, reagent, wellnum):
    # Create dataframes which will hold the final comparisons for the max and min
    finalvolmaxdf = pd.DataFrame()
    # Get the maximum volumes possible for this particular reagent based on user constraints
    rvolmax, rvolmin = calcvollimit(userlimits, rdict, volmax, volmin, experiment, reagentlist, reagent, wellnum)
    # get maximum values based on experimental constraints (well volume primarily)
    testing.reagenttesting(volmax,volmin)
    # Generate an upward limit for the chemical in dataframe based on the maximum user specified well volume and all previously used reagents
    volmaxdf = volmax - rdf.sum(axis=1)
    finalvolmaxdf = pd.concat([finalvolmaxdf, volmaxdf], axis=1)
    #replace values of experimental constraints with values of the reagent constraints (tighter) if experiment is looser
    volmaxdf[volmaxdf > rvolmax] = rvolmax
    finalvolmaxdf = pd.concat([finalvolmaxdf, volmaxdf], axis=1)
    #remove reagent volume to adjust for constraints set by user for final concentration limits
    rdata = rdict['%s'%reagent]
    chemicals = rdata.chemicals
    usedchemsobj = mmoldf.columns
    usedchems = []
    # Blank dataframe of minimums of appropriate length
    blanklist = [0]*wellnum
    finalvolmindf = pd.DataFrame(blanklist)
    # build a list of headers of chemicals used previously
    for chem in usedchemsobj:
        usedchems.append(chem)
    #pile chemicals (and how much) from past reaction into a single frame to ensure constraints are met
    finalsummedmmols = totalmmolchemicals(chemicals, usedchems, mmoldf)
    for chemical in chemicals:
        try:
            header = 'mmol_chemical%s' %chemical
            chemconc = rdata.concs['conc_chem%s' %chemical]
#            print(chemconc, rvolmax)
            volmaxdf = rvolmax-(finalsummedmmols[header] / chemconc * 1000)
            finalvolmaxdf = pd.concat([finalvolmaxdf, volmaxdf], axis=1)
        except Exception:
            pass
    # do it all again for the minimums
        try:
            header = 'mmol_chemical%s' %chemical
            chemconc = rdata.concs['conc_chem%s' %chemical]
            volmindf = rvolmin-(finalsummedmmols[header] / chemconc * 1000)
            volmindf[volmindf < 0] = 0
            finalvolmindf = pd.concat([finalvolmindf, volmindf], axis=1)
        except Exception:
            pass
    # Take the minimum of the maximums for the volumes calculated throughout this process
    outvolmaxdf = finalvolmaxdf.min(axis=1)
    # Take the maximum minimum ...
    outvolmindf =  finalvolmindf.max(axis=1)
    # Return the relevant datasets as int values (robot can't dispense anything smaller so lose the unsignificant figures /s)
    return(outvolmaxdf.astype(int), outvolmindf.astype(int))

# Determine the absolute maximum concentration for a particular chemical across all reagents in a portion of the experiment
def absmaxcondet(rdata, rnum, currchemical, reagentlist):
    maxconc = 0
    for reagent in reagentlist:
        conc = (maxconcdet(rdata, reagent, currchemical))
        if conc > maxconc:
            maxconc =  conc
    return(maxconc)

# Determine the maximum concentration for a particular chemical in a reagent
def maxconcdet(rdata, rnum, currchemical):
    maxconc = 0
    chemname = 'conc_chem%s' %currchemical
    for chemid,conc in rdata['%s' %rnum].concs.items():
        if chemname in chemid:
            if conc > maxconc:
                maxconc = conc
    return(maxconc)

def mmolextension(reagentdf, rdict, experiment, reagent):
    mmoldf = (pd.DataFrame(reagentdf))
    portionmmoldf = pd.DataFrame()
    for chemical, conc in (rdict['%s' %reagent].concs.items()):
        chemname = chemical.split('m')[1]
        newmmoldf = mmoldf * conc / 1000
        newmmoldf.rename(columns={'Reagent%s (ul)'%reagent:'mmol_experiment%s_reagent%s_chemical%s' %(experiment, reagent, chemname)}, inplace=True)
        portionmmoldf = pd.concat([portionmmoldf, newmmoldf], axis=1)
    return(portionmmoldf)

def ensuremin(rvolmindf, finalrdf, finalvolmin):
    currvoldf = finalrdf.sum(axis=1)
    trumindf = finalvolmin - currvoldf
    rvolmindf[rvolmindf < trumindf] = trumindf
    return(rvolmindf)

def portiondataframe(expoverview, rdict, vollimits, rxndict, wellnum, userlimits, experiment):
    portionnum = 0
    prdf = pd.DataFrame()
    prmmoldf = pd.DataFrame()
    for portion in expoverview:
        # need the volume minimum and maximum and well count
        reagentcount = 1
        reagenttotal = len(portion)
        # Determine from the chemicals and the remaining volume the maximum and minimum volume possible for the sobol method
        volmax = vollimits[portionnum][1]
        # unoptimized code that ensure that the previous reagents are considered and that the final reagent accurately fills to the minimum volume set by the users "fill to" requirement
        rdf = pd.DataFrame()
        mmoldf = pd.DataFrame()
        finalrdf = pd.DataFrame()
        finalmmoldf = pd.DataFrame()
        for reagent in portion:
            finalvolmin = vollimits[portionnum][0]
            volmin = 0
            # Unique set of requirements for the first entry
            if reagentcount == 1:
                # since all of the volume limits for the first draw are the same these can be treated as a bounded search sequence
                rvolmax, rvolmin = calcvollimit(userlimits, rdict, volmax, volmin, experiment, portion, reagent, wellnum)
                # Returns datafram of volumes of each reagent added to each experiment
                rdf = initialrdf(rvolmax, rvolmin, reagent, wellnum)
                # Returns mmol specified dataframe for each experiment, reagent, and chemical (<-- values are in the header) have been generated for this portion of the experiment
                mmoldf = mmolextension((rdf['Reagent%s (ul)' %reagent]), rdict, experiment, reagent)
                reagentcount+=1
                pass
            #operate within the available ranges taken from the previous constraints
            elif reagentcount < reagenttotal:
                # The constraints on the middle draws are more complicated and are dependent upon the first, a different sampling strategy must be used (i.e. this is not going to use sobol as the ranges are different for each)
                # Constrain the range based on volumne, reagent-chemical concentrations and user constraints
                rvolmaxdf, rvolmindf = calcvollimitdf(finalrdf, mmoldf, userlimits, rdict, volmax, volmin, experiment, portion, reagent, wellnum)
                # Since each volume maximum is different, need to sample the remaining reagents independently (thus different sampling)
                rdf = rdfbuilder(rvolmaxdf, rvolmindf, reagent, wellnum)
                mmoldf = mmolextension((rdf['Reagent%s (ul)' %reagent]), rdict, experiment, reagent)
                reagentcount+=1
                pass
            # Ensure that the final round meets the lower bounds and upper bound total fill volume requirements of the user
            elif reagentcount == reagenttotal:
                if vollimits[portionnum][0] == vollimits[portionnum][1]:
#                    print(finalrdf.sum(axis=1))
                    rvolmaxdf, rvolmindf = calcvollimitdf(finalrdf, mmoldf, userlimits, rdict, volmax, volmin, experiment, portion, reagent, wellnum)
                    reagentname = "Reagent%s (ul)" %reagent
                    rdf = pd.DataFrame(rvolmaxdf, columns=[reagentname])
                    mmoldf = mmolextension((rdf['Reagent%s (ul)' %reagent]), rdict, experiment, reagent)
                else:
                    rvolmaxdf, rvolmindf = calcvollimitdf(finalrdf, mmoldf, userlimits, rdict, volmax, volmin, experiment, portion, reagent, wellnum)
                    rvolmindf = ensuremin(rvolmindf, finalrdf, finalvolmin)
                    rdf = rdfbuilder(rvolmaxdf, rvolmindf, reagent, wellnum)
                    mmoldf = mmolextension((rdf['Reagent%s (ul)' %reagent]), rdict, experiment, reagent)
                reagentcount+=1
            else:
                modlog.error("Fatal error.  Unable to effectively parse reagent%s in portion %s.  Please make sure that the selected values make chemical sense!" %(reagent, portion))
            finalrdf = pd.concat([finalrdf, rdf], axis=1)
            finalmmoldf = pd.concat([finalmmoldf, mmoldf], axis=1)
        prdf = pd.concat([prdf, finalrdf], axis=1)
        prmmoldf = pd.concat([prmmoldf, finalmmoldf], axis=1)
        portionnum+=1
    return(prdf, prmmoldf)

def chemicallist(rxndict):
    chemicallist = []
    for k,v in rxndict.items():
        if 'abbreviation' in k:
            name = k.split('m')[1].split('_')[0]
            chemicallist.append(name)
    return(chemicallist)

def finalmmolsums(chemicals, usedchems, mmoldf):
    finalsummedmmols = pd.DataFrame()
    for chemical in chemicals:
        cname = 'chemical%s' %chemical
        coutname = 'chemical%s [M]' %chemical  # The technical output of this function is a mmol, simplier to rename the columns here
        tempdf = pd.DataFrame()
        for header in mmoldf.columns:
            if cname in header:
                tempdf = pd.concat([tempdf, mmoldf[header]], axis=1)
        summedmmols = pd.DataFrame(tempdf.sum(axis=1))
        summedmmols.columns = [coutname]
        finalsummedmmols = pd.concat([finalsummedmmols, summedmmols], axis=1)
    finalsummedmmols.fillna(value=0, inplace=True) # Total mmmols added of each chemical in previous reagent additions
    return(finalsummedmmols)

def preprocess(chemdf, rxndict, edict, rdict, climits):
    experiment = 1
    modlog.info('Making a total of %s unique experiments on the tray' %rxndict['totalexperiments'])
    erdf = pd.DataFrame()
    ermmoldf = pd.DataFrame()
    while experiment < rxndict['totalexperiments']+1:
        modlog.info('Initializing dataframe construction for experiment %s' %experiment)
        experimentname = 'exp%s' %experiment
        for k,v in edict.items():
            if experimentname in k:
                if 'wells' in k:
                    wellnum = int(v)
                if 'vols' in k:
                    vollimits=(v)
                else:
                    pass
        modlog.info('Building reagent constraints for experiment %s using reagents %s for a total of %s wells' %(experiment, edict[experimentname], wellnum) )
        prdf,prmmoldf = portiondataframe(edict[experimentname], rdict, vollimits, rxndict, wellnum, climits, experiment)
        erdf = pd.concat([erdf, prdf], axis=0, ignore_index=True, sort=True)
        ermmoldf = pd.concat([ermmoldf, prmmoldf], axis=0, ignore_index=True, sort=True)
        # Return the reagent data frame with the volumes for that particular portion of the plate
        modlog.info('Succesfully built experiment %s which returned.... ' %(experiment))
        experiment+=1
    #Final reagent volumes dataframe
    erdf.fillna(value=0, inplace=True)
    #Final reagent mmol dataframe broken down by experiment, protion, reagent, and chemical
    ermmoldf.fillna(value=0, inplace=True)
    clist = (chemicallist(rxndict))
    # Final nominal molarity for each reagent in each well
    emsumdf = finalmmolsums(clist, clist, ermmoldf) # Returns incorrectly labeled columns, we used these immediately and convert to the correct units
    emsumdf = emsumdf.divide(erdf.sum(axis=1), axis='rows')*1000
    if rxndict['plotter_on'] == 1:
        if 1 <= rxndict['ExpWorkflowVer'] < 2:
            plotter.plotmewf1(emsumdf, rxndict)
        else:
            modlog.warning("Plot has been enabled, but no workflow specific plot has been programmed.  Not plot will be shown")
    else:
        pass
#    plotter.plotme(ReagentmmList[0],ReagentmmList[1], hold.tolist())
    #combine the experiments for the tray into one full set of volumes for all the wells on the plate
    modlog.info('Begin combining the experimental volume dataframes')
#    for chemical in rdict['2'].chemicals:
#        print(rxndict['chem%s_abbreviation' %chemical])
    return(erdf,ermmoldf,emsumdf)


from elasticsearch import Elasticsearch

import random
import datetime
import argparse
import sys

def get_index_path(collection_name, namespace, lcp_project_id):
    index_path = "{}_{}_{}".format(
        lcp_project_id, namespace, collection_name
    )

    return index_path.lower()

def get(es, id, index, doc_type):
    document = es.get(
        doc_type=doc_type,
        id=id,
        index=index
    )

    return document.get('_source')

def index(es, body, id, index, doc_type):
    body['id'] = id

    return es.index(body=body, id=id, index=index, doc_type=doc_type)

def search(es, body, index):
    response = es.search(body=body, index=index)

    search_hits = response.get('hits')

    documents = [hit.get('_source') for hit in search_hits.get('hits')]

    return documents, search_hits.get('total')

argument_parser = argparse.ArgumentParser(
    usage='{} recommendation_data_loader '
    '--command <Command> '
    '--elasticsearch-hostname <Elasticsearch Hostname> (Optional) '
    '--model-name <model Name> '
    '--lcp-project-id <LCP Project ID>'.format(sys.argv[0])
)

argument_parser.add_argument('--command', required=True)
argument_parser.add_argument('--elasticsearch-hostname', default='http://localhost:9200')
argument_parser.add_argument('--lcp-project-id', required=True)
argument_parser.add_argument('--model-name', required=True)
argument_parser.add_argument('--output-version-id', required=False)
argument_parser.add_argument('--output-version-status', required=False, default="RUNNING")
argument_parser.add_argument('--output-version-events', required=False, default=random.randrange(1000, 10000))
argument_parser.add_argument('--output-version-items', required=False, default=random.randrange(500, 1000))

args = argument_parser.parse_args()

es = Elasticsearch(args.elasticsearch_hostname)

documents, total = search(
    es,
    {
        "query": {
            "term": {
                "name" : args.model_name
            }
        }
    }, get_index_path('jobs', 'osbasahfaroinfo', args.lcp_project_id)
)

if total == 0:
    sys.exit('No such model with name "{}"'.format(args.model_name))

job = documents[0]

if args.command == 'add_output_version':
    now = datetime.datetime.utcnow()
    now_str = now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + 'Z'

    id = int(now.timestamp())
    job_runs_index = get_index_path('job-runs', 'osbasahfaroinfo', args.lcp_project_id)

    body = {
        'context': {},
        'createdDate': now_str,
        'job': {
            'id': job.get('id') ,
            'type': job.get('type')
        } ,
        'lastUpdatedDate': now_str,
        'status': args.output_version_status,
        'step': 'DATA_PREPARATION',
        'trigger': 'MANUAL'
    }

    if args.output_version_status in ['COMPLETED', 'PUBLISHED']:
        body['completedDate'] = now_str
        body['context']['itemsDatasetCount'] = args.output_version_items
        body['context']['userItemInteractionsDatasetCount'] = args.output_version_events

    index(
        es, body, id, job_runs_index, 'job-runs'
    )

    print("Output Version {}".format(get(es, id, job_runs_index, 'job-runs')))


# Copyright 2018 Shengyang Sun
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tensorflow as tf
from scipy.sparse.linalg import eigsh
import numpy as np

from . import settings

def vectorize(mat):
    return tf.reshape(tf.transpose(mat), [-1, 1])

def devectorize(vec, shape):
    return tf.transpose(tf.reshape(vec, shape[::-1]))

def batch_vectorize(mat):
    return tf.reshape(tf.transpose(mat), [-1, tf.shape(mat)[-1]])

def batch_devectorize(vec, shape):
    return tf.transpose(tf.reshape(vec, shape[::-1]))

class Kronecker(object):

    def __init__(self, vars):
        self.vars = vars
        self.sizes = [tf.shape(v)[0] for v in self.vars]

    def matrix_inverse_root(self, k=10):
        roots = []
        for v in self.vars:
            v = v.numpy() if isinstance(v, tf.Tensor) else v
            eigvals, eigvecs = eigsh(v, k=min(k, v.shape[0]-1))
            eigvals = np.maximum(eigvals, 1e-10)
            eigvals = tf.diag(1. / eigvals ** 0.5)
            root = tf.matmul(eigvecs, tf.matmul(eigvals, eigvecs.transpose()))
            roots.append(root)
        return self.__class__(roots)

    def matrix_root(self, k=10):
        roots = []
        for v in self.vars:
            v = v.numpy() if isinstance(v, tf.Tensor) else v
            eigvals, eigvecs = eigsh(v, k=min(k, v.shape[0]-1))
            eigvals = np.maximum(eigvals, 1e-10)
            eigvals = tf.diag(eigvals ** 0.5)
            root = tf.matmul(eigvecs, tf.matmul(eigvals, eigvecs.transpose()))
            roots.append(root)
        return self.__class__(roots)

    def matmul_vec(self, vec):
        res = vec
        for v in self.vars:
            res = tf.transpose(devectorize(res, [-1, tf.shape(v)[0]]))
            factor = tf.matmul(v, res)
            res = tf.reshape(tf.transpose(factor), [-1, 1])
        res = devectorize(res, [-1, tf.shape(vec)[-1]])
        return res

class testKronecker(tf.test.TestCase):
    def test_2elements(self):
        with self.test_session() as sess:
            var1 = tf.random_normal(shape=[4, 4])
            var2 = tf.random_normal(shape=[3, 3])
            var1 = tf.matmul(var1, var1, transpose_b=True)
            var2 = tf.matmul(var2, var2, transpose_b=True)
            kron = Kronecker([var1, var2])

            v = tf.random_normal(shape=[12, 1])
            v_mat = devectorize(v, [3, 4])

            kv = kron.matmul_vec(v)
            kp = tf.contrib.kfac.utils.kronecker_product
            real_kv = tf.matmul(kp(var1, var2), v)
            # real_kv = vectorize(tf.matmul(tf.matmul(var2, v_mat), var1, transpose_b=True))

            aa, bb = sess.run([kv, real_kv])
            print(aa)
            print(bb)
            self.assertAllClose(aa, bb)

    def test_3elements(self):
        with self.test_session() as sess:
            var1 = tf.random_normal(shape=[3, 3])
            var2 = tf.random_normal(shape=[4, 4])
            var3 = tf.random_normal(shape=[5, 5])
            var1 = tf.matmul(var1, var1, transpose_b=True)
            var2 = tf.matmul(var2, var2, transpose_b=True)
            var3 = tf.matmul(var3, var3, transpose_b=True)
            kron = Kronecker([var1, var2, var3])

            v = tf.random_normal(shape=[12 * 5, 1])
            kv = kron.matmul_vec(v)

            kp = tf.contrib.kfac.utils.kronecker_product
            M = kp(var1, kp(var2, var3))
            real_kv = tf.matmul(M, v)

            aa, bb = sess.run([kv, real_kv])
            print(aa.squeeze())
            print(bb.squeeze())
            self.assertAllClose(aa, bb, rtol=1e-5, atol=1e-4)

    def test_3elements_batch(self):
        with self.test_session() as sess:
            var1 = tf.random_normal(shape=[3, 3])
            var2 = tf.random_normal(shape=[4, 4])
            var3 = tf.random_normal(shape=[5, 5])
            var1 = tf.matmul(var1, var1, transpose_b=True)
            var2 = tf.matmul(var2, var2, transpose_b=True)
            var3 = tf.matmul(var3, var3, transpose_b=True)
            kron = Kronecker([var1, var2, var3])

            v = tf.random_normal(shape=[12 * 5, 5])
            kv = kron.matmul_vec(v)

            kp = tf.contrib.kfac.utils.kronecker_product
            M = kp(var1, kp(var2, var3))
            real_kv = tf.matmul(M, v)

            aa, bb = sess.run([kv, real_kv])
            print(aa.squeeze())
            print('------------------')
            print(bb.squeeze())
            self.assertAllClose(aa, bb, rtol=1e-5, atol=1e-4)