'''
Creating Mapping Dictionary for Cuisine Classification

Nak Won Rim, Anqi Hu, Chia-yun Chang
'''

import requests
import re
import pickle
from bs4 import BeautifulSoup
from time import sleep
from collections import Counter

CORE_URL = 'https://www.allmenus.com/il/chicago/-/'

def get_all_cusines(CORE_URL):
    '''
    Extract all cuisine categories for all restaurant in Chicago registered
    in allmenu.com.

    Input: CORE_URL (str): the url for the allmenu.com Chicago directory

    Returns: cusine_set (set): a set containing all cusine categories for all
                              restaruant in Chicago registered in allmenus.com
    '''

    rqst = requests.get(CORE_URL)
    soup = BeautifulSoup(rqst.text, 'html.parser')
    cusine_container = soup.find('div', class_='cuisine-container')
    cuisine_set = set()
    for div in cusine_container.find_all('div', class_='s-checkbox-group'):
        cuisine_set.add(clean_cuisine_name(div.text))
    return cuisine_set

def clean_cuisine_name(cuisine):
    '''
    Clean the cuisine name to standardize

    Input:
      cuisine (str): the cuisine name to be standardized

    Returns:
      cuisine (str): the standardized cuisine name
    '''

    cuisine = re.sub("[/\s\)]", "", cuisine.strip().lower())
    cuisine = re.sub('&|[\(&]', "-", cuisine)
    if cuisine == "delifood":
        cuisine = "deli"
    elif cuisine == "eclectic-international":
        cuisine = "eclectic"
    elif cuisine == "noodles":
        cuisine = "noodle-bar"
    elif cuisine == "potatoes":
        cuisine = "potato"
    elif cuisine == "subs":
        cuisine = "sub"
    return cuisine

def get_num_rest_and_combi(cuisine_set, CORE_URL):
    '''
    Extract the number of restaurant for all the cuisine categories in the
    cuisine_set and store it in a dictionary

    Input:
      cusine_set (set): a set containing all cusine categories for all
                        restaruant in Chicago registered in allmenus.com
      CORE_URL (str): the url for the allmenu.com Chicago directory

    Returns:
      num_rest (dict): dictionary containing the number of restaurant for
                       cuisine categories
      cui_combi (list of list): a list of lists of unique cusine combinations
                                ex) [[american, chicagogrill], [italian]]
    '''

    num_rest = {}
    cui_combi = []
    for cuisine in cuisine_set:
        sleep(4)
        rqst = requests.get(CORE_URL + cuisine)
        if rqst.status_code != 200:
            print(cuisine, "is not a valid cuisine")
            continue
        soup = BeautifulSoup(rqst.text, "html.parser")
        num = len(soup.find_all("li", class_="restaurant-list-item"))
        for cuisine_com in soup.find_all('p', class_='cousine-list'):
            if cuisine_com not in cui_combi:
                combi = cuisine_com.text.split(', ')
                if combi not in cui_combi:
                    cui_combi.append(combi)
        if num == 0:
            continue
        num_rest[cuisine] = num
    return num_rest, cui_combi

def get_top_40(num_rest):
    '''
    Get the top 40 elements that has highest value in the num_rest. This
    method was picked up from:
    https://www.geeksforgeeks.org/python-program-to-find-the-highest-3-values-in-a-dictionary/

    Input:
      num_rest (dict): dictionary containing the number of restaurant for
                       cuisine categories
    Returns:
      (set) set of top 40 elements most frequent in the dataset
    '''

    return {c[0] for c in Counter(num_rest).most_common(40)}

def get_combi_to_map(cui_combi, top_40):
    '''
    get cuisine combination that does not contain at least one top 40 cuisines

    Input:
      cui_combi (list of list): a list of lists of unique cusine combinations
                                ex) [[american, chicagogrill], [italian]]
      top_40 (set): set of top 40 cuisines ex) {italian, american}
    '''

    combi_to_map = []
    for combi in cui_combi:
        in_top_40 = False
        for cuisine in combi:
            cuisine = clean_cuisine_name(cuisine)
            if cuisine in top_40:
                in_top_40 = True
                break
        if not in_top_40:
            combi_to_map.append(combi)
    return combi_to_map

def exploration(test):
    '''
    Explore the cuisines and associated restaruant in Chicago. Find out the
    top 40 restaurants that could independently be a category. Also find out
    all cuisine combination that does not include any of the top 40 cusines
    so we can create a mapping dictionary that groups some cuisines together
    in a way that all restaurants are classified with at least one cuisine.

    Input:
      test (bool): if True, we will use the stored data instead of scraping
                   data
    Output:
      if test was False, we will scrape the data and store the set of top 40
      cuisines and list of lists of unique cuisine combination as pickle.
    '''

    if not test:
        with open('data/num_rest_for_cui.pkl', 'rb') as pkl:
            num_rest = pickle.load(pkl)
        top_40 = get_top_40(num_rest)
        with open('data/cui_combi.pkl', 'rb') as pkl:
            cui_combi = pickle.load(pkl)
        combi_to_map = get_combi_to_map(cui_combi, top_40)
    else:
        cuisine_set = get_all_cusines(CORE_URL)
        num_rest, cui_combi = get_num_rest_and_combi(cuisine_set, CORE_URL)
        top_40 = get_top_40(num_rest)
        combi_to_map = get_combi_to_map(cui_combi, top_40)
        with open('data/num_rest_for_cui.pkl', 'wb') as pkl:
            pickle.dump(num_rest, pkl)
        with open('data/cui_combi.pkl', 'wb') as pkl:
            pickle.dump(cui_combi, pkl)
    print('The top 40 cuisines are: \n', top_40,
          '\nThe cuisine combination that needs mapping are: \n',
          combi_to_map)

def validation():
    '''
    Make sure that all cuisine combination has at least one component that is
    mapped in the mapping_dict
    '''

    with open('data/cui_combi.pkl', 'rb') as pkl:
        cui_combi = pickle.load(pkl)
    with open('mapping_dict.pkl', 'rb') as pkl:
        mapping_dict = pickle.load(pkl)
    # restaurants with following combination were all invalid (didn't have
    # proper menu)
    cui_combi.remove(['Pub Food'])
    cui_combi.remove(['Crepes'])
    cui_combi.remove(['Subs'])
    covered = mapping_dict.keys()
    not_covered = []
    for combi in cui_combi:
        combi_covered = False
        for cuisine in combi:
            if clean_cuisine_name(cuisine) in covered:
                combi_covered = True
                break
        if not combi_covered:
            not_covered.append(combi)
    if not not_covered:
        print('All cuisine categories were classified!')
    else:
        print('Following cuisine was not classified:', not_covered)

if __name__ == "__main__":
    msg = 'enter 0 for finding the top 40 cuisines by the number of ' + \
          'restaurants and all cuisine combination not having one of the ' + \
          'top 40 cuisines\nenter 1 for validating that all restaurants' + \
          'got at least one classification: '
    validate = input(msg)
    while validate not in ['0', '1']:
        validate = input('valid input are 0 or 1. Try Again: ')
    validate = int(validate)
    if validate:
        validation()
    else:
        test = input('enter 0 if you want to use scraped data \n' + \
                     'enter 1 if you want to scrap the website again: ')
        while test not in ['0', '1']:
            test = input('valid input are 0 or 1. Try Again: ')
        test = int(test)
        exploration(bool(test))


# Copyright 2022 Google LLC
#
# 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
#
#     https://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 numpy as np
import sys
import glob
import string
import tensorflow as tf
from keras.models import Sequential
from keras.layers.core import Dense, Flatten
from flag import flag
import signal

signal.alarm(120)

tf.compat.v1.disable_eager_execution()

image_data = []
for f in sorted(glob.glob("/images/*.png")):
    im = tf.keras.preprocessing.image.load_img(
        f, grayscale=False, color_mode="rgb", target_size=None, interpolation="nearest"
    )
    im = tf.keras.preprocessing.image.img_to_array(im)
    im = im.astype("float32") / 255
    image_data.append(im)

image_data = np.array(image_data, "float32")

# The network is pretty tiny, as it has to run on a potato.
model = Sequential()
model.add(Flatten(input_shape=(16,16,3)))
# I'm sure we can compress it all down to four numbers.
model.add(Dense(4, activation='relu'))
model.add(Dense(128, activation='softmax'))

print("Train this neural network so it can read text!")

wt = model.get_weights()
while True:
    print("Menu:")
    print("0. Clear weights")
    print("1. Set weight")
    print("2. Read flag")
    print("3. Quit")
    inp = int(input())
    if inp == 0:
        wt[0].fill(0)
        wt[1].fill(0)
        wt[2].fill(0)
        wt[3].fill(0)
        model.set_weights(wt)
    elif inp == 1:
        print("Type layer index, weight index(es), and weight value:")
        inp = input().split()
        value = float(inp[-1])
        idx = [int(c) for c in inp[:-1]]
        wt[idx[0]][tuple(idx[1:])] = value
        model.set_weights(wt)
    elif inp == 2:
        results = model.predict(image_data)
        s = ""
        for row in results:
            k = "?"
            for i, v in enumerate(row):
                if v > 0.5:
                    k = chr(i)
            s += k
        print("The neural network sees:", repr(s))
        if s == flag:
            print("And that is the correct flag!")
    else:
        break


from collections import Counter, defaultdict, OrderedDict, deque
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from typing import List
import itertools
import math
import heapq
import string
true = True
false = False
MIN, MAX, MOD = -0x3f3f3f3f, 0x3f3f3f3f, 1000000007
#
# @lc app=leetcode id=1542 lang=python3
#
# [1542] Find Longest Awesome Substring
#
# https://leetcode.com/problems/find-longest-awesome-substring/description/
#
# algorithms
# Hard (26.70%)
# Total Accepted:    1.9K
# Total Submissions: 6.4K
# Testcase Example:  '"3242415"'
#
# Given a string s. An awesome substring is a non-empty substring of s such
# that we can make any number of swaps in order to make it palindrome.
#
# Return the length of the maximum length awesome substring of s.
#
#
# Example 1:
#
#
# Input: s = "3242415"
# Output: 5
# Explanation: "24241" is the longest awesome substring, we can form the
# palindrome "24142" with some swaps.
#
#
# Example 2:
#
#
# Input: s = "12345678"
# Output: 1
#
#
# Example 3:
#
#
# Input: s = "213123"
# Output: 6
# Explanation: "213123" is the longest awesome substring, we can form the
# palindrome "231132" with some swaps.
#
#
# Example 4:
#
#
# Input: s = "00"
# Output: 2
#
#
#
# Constraints:
#
#
# 1 <= s.length <= 10^5
# s consists only of digits.
#
#
#

class Solution:
    def longestAwesome(self, s: str) -> int:
        '''Use bitmask.
        '''
        res, mask = 1, 0
        cc = [len(s)] * 1024
        cc[0] = -1
        for i, c in enumerate(s):
            mask ^= 1 << int(c)
            res = max(res, i - cc[mask])
            for j in range(10):
                res = max(res, i - cc[mask ^ (1 << j)])
            cc[mask] = min(cc[mask], i)

        return res

sol = Solution()

s = "3242415"
s = "12345678"
# s = "213123"
# s = "00"
# s = '51224'
print(sol.longestAwesome(s))


from coralillo import Engine
from itacate import Config
import os
import pytest
import pymongo

from cacahuate.models import bind_models as bimo

def ismybirthday(value):
    if value.month == 5 and value.day == 10:
        return 'Today is my birthday!'
    return 'Is not my birthday'

TESTING_SETTINGS = {
    'CUSTOM_LOGIN_PROVIDERS': {
        'hardcoded': 'test.hardcoded_login',
    },
    'CUSTOM_HIERARCHY_PROVIDERS': {
        'hardcoded': 'test.hardcoded_hierarchy',
        'noparam': 'test.noparam_hierarchy',
    },
    'PROCESS_ENV': {
        'FOO': 'var',
    },
    'TEMPLATE_PATH': os.path.join(os.path.dirname(__file__), 'templates'),
    'JINJA_FILTERS': {
        'ismybirthday': ismybirthday,
    },
}

@pytest.fixture
def config():
    ''' Returns a fully loaded configuration dict '''
    con = Config(
        os.path.join(
            os.path.dirname(os.path.realpath(__file__)), '..'
        )
    )

    con.from_object('cacahuate.settings')
    con.from_mapping(TESTING_SETTINGS)

    return con

@pytest.fixture(autouse=True)
def clear_mongo(config):
    client = pymongo.MongoClient()
    uri = config['MONGO_URI']
    database_name = pymongo.uri_parser.parse_uri(uri)['database']
    db = client[database_name]

    collection = db[config['POINTER_COLLECTION']]
    collection.drop()

    collection_execution = db[config['EXECUTION_COLLECTION']]
    collection_execution.drop()

@pytest.fixture(autouse=True)
def bind_models(config):
    ''' Binds the models to a coralillo engine, returns nothing '''
    engine = Engine(
        host=config['REDIS_HOST'],
        port=config['REDIS_PORT'],
        db=config['REDIS_DB'],
    )
    engine.lua.drop(args=['*'])

    bimo(engine)

@pytest.fixture
def client():
    ''' makes and returns a testclient for the flask application '''
    from cacahuate.http.wsgi import app

    app.config.from_mapping(TESTING_SETTINGS)

    return app.test_client()

@pytest.fixture
def mongo(config):
    client = pymongo.MongoClient()
    uri = config['MONGO_URI']
    database_name = pymongo.uri_parser.parse_uri(uri)['database']
    db = client[database_name]

    return db


import getpass
import os
import subprocess
import tempfile
import time
from enum import Enum
from typing import Any

from helpers import ROOT, Settings, nix_build

class StorageKind(Enum):
    NATIVE = 1
    LKL = 2
    SPDK = 3

def get_total_memory() -> int:
    with open("/proc/meminfo") as f:
        for line in f:
            name, raw_size = line.split(":")
            if name.strip() != "MemTotal":
                continue
            size, unit = raw_size.strip().split()
            return int(size) * 1024
    raise Exception("MemTotal entry not found in /proc/meminfo")

class Mount:
    def __init__(self, kind: StorageKind, dev: str) -> None:
        self.kind = kind
        self.dev = dev

    def __enter__(self) -> str:
        assert self.kind == StorageKind.NATIVE
        self.mountpoint = tempfile.TemporaryDirectory(prefix="iotest-mnt")
        subprocess.run(["sudo", "mount", self.dev, self.mountpoint.name])
        subprocess.run(["sudo", "chown", "-R", getpass.getuser(), self.mountpoint.name])

        return self.mountpoint.name

    def __exit__(self, type: Any, value: Any, traceback: Any) -> None:
        subprocess.run(["sudo", "umount", self.mountpoint.name])

def get_hugepages_num(kind: StorageKind) -> int:
    if kind != StorageKind.SPDK:
        return 0
    total_memory = get_total_memory()
    # leave 5 GB for the system
    gigabyte = 1024 * 1024 * 1024
    spdk_memory = total_memory - 5 * gigabyte
    if spdk_memory < gigabyte:
        raise Exception("Get more memory dude!")

    return int(spdk_memory / 2048 / 1024)

class Storage:
    def __init__(self, settings: Settings) -> None:
        self.settings = settings
        self.image = nix_build("iotest-image")

    def setup(self, kind: StorageKind) -> Mount:
        subprocess.run(
            ["sudo", ROOT.joinpath("..", "..", "spdk", "scripts", "setup.sh"), "reset"]
        )
        time.sleep(2)  # wait for device to appear

        spdk_device = self.settings.spdk_device()

        dev = f"/dev/{spdk_device}"

        while not os.path.exists(dev):
            print(".")
            time.sleep(1)

        # TRIM for optimal performance
        subprocess.run(["sudo", "blkdiscard", dev])
        subprocess.run(["sudo", "dd", f"if={self.image}", f"of={dev}"])
        subprocess.run(["sudo", "resize2fs", dev])

        if kind == StorageKind.SPDK:
            subprocess.run(
                [
                    "sudo",
                    ROOT.joinpath("..", "..", "spdk", "scripts", "setup.sh"),
                    "config",
                ]
            )
        elif kind == StorageKind.LKL:
            subprocess.run(["sudo", "chown", getpass.getuser(), dev])

        num_hugepages = get_hugepages_num(kind)
        # spdk setup.sh seems to reset number of pages
        subprocess.run(
            [
                "sudo",
                "sh",
                "-c",
                "echo $0 > /sys/devices/system/node/node0/hugepages/hugepages-2048kB/nr_hugepages",
                str(num_hugepages),
            ],
            check=True
        )
        # delete existing hugepages to actually free up the memory
        subprocess.run(
            [
                "sudo",
                "find",
                "/dev/hugepages",
                "-name",
                "spdk*map_*",
                "-type",
                "f",
                "-delete",
            ],
            check=True
        )

        return Mount(kind, dev)


from django.conf.urls import url
from deposit_and_credit import views

urlpatterns = [
    url('test', views.test),
    url(r'overviewbranch\.view', views.viewOverViewBranch, name='viewOverViewBranch'),
    url(r'overviewbranch\.ajax', views.ajaxOverViewBranch, name='ajaxOverViewBranch'),
    url(r'annotatedeposit\.ajax', views.ajaxAnnotateDeposit, name='ajaxAnnotateDeposit'),
    url(r'contribution\.view', views.viewContribution, name='viewContribution'),
    url(r'contributiontable\.view', views.viewContributionTable, name='viewContributionTable'),
    url(r'contribution\.ajax', views.ajaxContribution, name='ajaxContribution'),
    url(r'contributiondata\.download', views.downloadContributionData, name='downloadContributionData'),
    url(r'deptorder\.ajax', views.ajaxDeptOrder, name='ajaxDeptOrder'),
    url(r'staff\.ajax', views.ajaxStaff, name='ajaxStaffName'),
    url(r'customercontributionhistory\.view', views.viewCustomerContributionHistory, name='viewCustomerContributionHistory'),
    url(r'seriescontributionhistory\.view', views.viewSeriesContributionHistory, name='viewSeriesContributionHistory'),
    url(r'customercredithistory\.ajax', views.ajaxCustomerCreditHistory, name='ajaxCustomerCreditHistory'),
    url(r'departmentcontributionhistory\.view', views.viewDepartmentContributionHistory, name='viewDepartmentContributionHistory'),
    url(r'expireprompt\.view', views.viewExpirePrompt, name='viewExpirePrompt'),
    url(r'expireprompttable\.view', views.viewExpirePromptTable, name='viewExpirePromptTable'),
    url(r'expireprompt\.edit', views.editExpirePrompt, name='editExpirePrompt'),
    url(r'expireprompt\.finish', views.finishExpirePrompt, name='finishExpirePrompt'),
    url(r'expireexplain\.view', views.viewExpireExplain, name='viewExpireExplain'),
    url(r'redcard\.reset', views.resetRedCard, name='resetRedCard'),
    url(r'progress\.update', views.updateProgress, name='updateProgress'),
]


import logging
import asyncio
from aiohttp.web import Application
from aiohttp_json_rpc import JsonRpc, RpcInvalidParamsError
from rgbwindow import config, strip
from rgbwindow.modes import modes

class Controller:
    def __init__(self, leds):
        self.log = logging.getLogger(__class__.__name__)
        self.leds = leds

        self.running_mode_task = None

        self.loop = asyncio.get_event_loop()

        rpc = JsonRpc()
        rpc.add_methods(
            ('', self.mode)
        )
        app = Application(loop=self.loop)
        app.router.add_route('*', '/', rpc)
        handler = app.make_handler()
        server = self.loop.create_server(handler, '0.0.0.0', 8080)
        self.loop.run_until_complete(server)
        self.loop.run_forever()

    async def mode(self, request):
        try:
            requested_mode = request.params.get("mode")
            given_mode = modes[requested_mode]
        except KeyError:
            return RpcInvalidParamsError

        new_mode = given_mode(self.leds)
        if self.running_mode_task:
            self.running_mode_task.cancel()
            print("here")
            try:
                await self.running_mode_task
            except asyncio.CancelledError:
                print("cancelled")

        self.running_mode_task = self.loop.c
        self.running_mode_task = self.loop.create_task(new_mode.iteration())

if __name__ == "__main__":
    leds = strip.Strip(config.size, brightness=config.brightness)
    leds.begin()
    Controller(leds)


from .utils import format_string, format_value

class Line(object):
    def __init__(self, name, fields, tags={}, timestamp=None):
        # name of the actual measurement
        self.measurement = name
        # single value or a dict of value_name: value pairs
        self.fields = fields
        # dict of tags if any
        self.tags = tags
        # user provided timestamp, if any
        self.timestamp = timestamp

    def get_output_fields(self):
        """Return an escaped string of comma separated value_name: value
        pairs.
        """

        # Handle primitive fields here and implicitly convert them to a dict
        # because it allows the API to be simpler.
        # Also influxDB mandates that each value also has a name so the default
        # name for any non-dict value is "value".
        if not isinstance(self.fields, dict):
            metric_fields = {'value': self.fields}
        else:
            metric_fields = self.fields

        # sort the fields lexicographically by value name
        sorted_fields = sorted(metric_fields.items())

        return ','.join('{0}={1}'.format(format_string(k), format_value(v))
                        for k, v in sorted_fields if v is not None)

    def get_output_tags(self):
        """Return an escaped string of comma separated tag_name: tag_value
        pairs. Tags should be sorted by key before being sent for best
        performance.
        The sort should match that from the Go bytes.
        Compare function (http://golang.org/pkg/bytes/#Compare).
        """

        # sort the tags lexicographically by tag name
        sorted_tags = sorted(self.tags.items())

        # finally render, escape and return the tag string
        return ','.join('{0}={1}'.format(format_string(k), format_string(v))
                        for k, v in sorted_tags)

    def to_line_protocol(self):
        """Converts the given metrics as a single line of InfluxDB
        line protocol.
        """

        tags = self.get_output_tags()

        return '{measurement}{tags} {fields}{timestamp}'.format(
            measurement=format_string(self.measurement),
            tags=',' + tags if tags else '',
            fields=self.get_output_fields(),
            timestamp=' {0}'.format(self.timestamp) if self.timestamp else ''
        )


from urllib.parse import urljoin

from test_tool.settings import (
    ADMIN_ANCHORS, DEFAULT_WAIT, DEFAULT_BLOG, LONG_AJAX, LIVEBLOG_URL,
    MAX_FOR_AJAX)
from test_tool.helpers.selenium_stuff import (
    navigate_admin, wait_for_visible_text_by_css, wait_for_visible_by_css,
    accept_js_alert, id_generator, parent)
from test_tool.actions.common import (
    wait_for_async_load)

def goto_blog_feeds(browser, blog_id=DEFAULT_BLOG):
    browser.get(
        navigate_admin(ADMIN_ANCHORS['live-blog-feeds'].format(blog_id)))
    wait_for_visible_text_by_css(
        browser, LONG_AJAX,
        'section.configure-blog header h3', "feeds", partial=True)
    wait_for_async_load(browser)

def goto_chained_blogs(browser, blog_id=DEFAULT_BLOG):
    goto_blog_feeds(browser, blog_id)
    chain_blog_link = browser.find_element_by_css_selector(
        'ul.nav li a[href="#chainblog"]')
    chain_blog_link.click()

def remove_all_providers(browser):
    provider_links = browser.find_elements_by_css_selector(
        '.chain-source-header div.dropdown a.dropdown-toggle')
    for provider_link in provider_links:
        provider_link.click()
        remove_link = wait_for_visible_by_css(
            browser, DEFAULT_WAIT,
            '.chain-source-header div.dropdown span.remove')
        remove_link.click()
        accept_js_alert(browser)

def add_new_provider(browser, provider_name=None):
    if provider_name is None:
        provider_name = id_generator()
    provider_link = urljoin(LIVEBLOG_URL, '/resources/LiveDesk/Blog')
    new_provider_button = browser.find_element_by_css_selector(
        'a[href="#AddProvider"]')
    new_provider_button.click()
    title_field = wait_for_visible_by_css(
        browser, MAX_FOR_AJAX, '#provider-title')
    title_field.send_keys(provider_name)
    url_field = browser.find_element_by_id('provider-url')
    url_field.send_keys(provider_link)
    save_button = browser.find_element_by_css_selector(
        'span.btn[ng-click="editProvider();"]')
    save_button.click()
    wait_for_visible_text_by_css(
        browser, LONG_AJAX, 'div.chain-source-header h5', provider_name,
        partial=True)

def add_chained_blog(browser, title):
    blog_title_element = wait_for_visible_text_by_css(
        browser, LONG_AJAX, 'label', title)
    blog_element = parent(parent(parent(blog_title_element)))
    checkbox_element = blog_element.find_element_by_css_selector(
        'span.sf-checkbox-custom')
    checkbox_element.click()


# GW2 Authenticated Endpoints
## Account
ACCOUNT = '/v2/account'
ACCOUNT_ACHIEVEMENTS = '/v2/account/achievements'
ACCOUNT_BANK = '/v2/account/bank'
ACCOUNT_DUNGEONS = '/v2/account/dungeons'
ACCOUNT_DYES = '/v2/account/dyes'
ACCOUNT_FINISHERS = '/v2/account/finishers'
ACCOUNT_GLIDERS = '/v2/account/gliders'
ACCOUNT_HOME_CATS = '/v2/account/home/cats'
ACCOUNT_HOME_NODES = '/v2/account/home/nodes'
ACCOUNT_INVENTORY = '/v2/account/inventory'
ACCOUNT_MAILCARRIERS = '/v2/account/mailcarriers'
ACCOUNT_MASTERIES = '/v2/account/masteries'
ACCOUNT_MATERIALS = '/v2/account/materials'
ACCOUNT_MINIS = '/v2/account/minis'
ACCOUNT_MOUNTS_SKINS = '/v2/account/mounts/skins'
ACCOUNT_MOUNTS_TYPE ='/v2/account/mounts/types'
ACCOUNT_OUTFITS = '/v2/account/outfits'
ACCOUNT_PVP_HEROES = '/v2/account/pvp/heroes'
ACCOUNT_RAIDS = '/v2/account/raids'
ACCOUNT_RECIPES = '/v2/account/recipes'
ACCOUNT_SKINS = '/v2/account/skins'
ACCOUNT_TITLES = '/v2/account/titles'
ACCOUNT_WALLET = '/v2/account/wallet'
## Characters
CHARACTERS = '/v2/characters/'
CHARACTER_EQUIPMENT = '/v2/characters/{id}/equipment'
CHARACTER_INVENTORY = '/v2/characters/{id}/inventory'
## Commerce
COMMERCE_TRANSACTIONS = '/v2/commerce/transactions'
## PvP
PVP_STATS = '/v2/pvp/stats'
PVP_GAMES = '/v2/pvp/games'
PVP_STANDINGS = '/v2/pvp/standings'
## TokenInfo
TOKENINFO = '/v2/tokeninfo'

# GW2 Achievement Endpoints
ACHIEVEMENTS = '/v2/achievements'
ACHIEVEMENTS_DAILY = '/v2/achievements/daily'
ACHIEVEMENTS_DAILY_TOMORROW = '/v2/achievements/daily/tomorrow'
ACHIEVEMENTS_GROUPS = '/v2/achievements/groups'
ACHIEVEMENTS_CATEGORIES = '/v2/achievements/categories'

# GW2 Backstory Endpoints
BACKSTORY_ANSWERS = '/v2/backstory/answers'
BACKSTORY_QUESTIONS = '/v2/backstory/questions'

# GW2 Build Endpoints
BUILD = '/v2/build'

# GW2 Colors Endpoints
COLORS = '/v2/colors'

# GW2 Commerce Endpoints
COMMERCE_EXCHANGE = '/v2/commerce/exchange'
COMMERCE_EXCHANGE_COINS = '/v2/commerce/exchange/coins'
COMMERCE_EXCHANGE_GEMS = '/v2/commerce/exchange/gems'
COMMERCE_LISTINGS = '/v2/commerce/listings'
COMMERCE_PRICES = '/v2/commerce/prices'

CONTINENTS = '/v2/continents'

CURRENCIES = '/v2/currencies'

DUNGEONS = '/v2/dungeons'

EMBLEM = '/v2/emblem'

FILES = '/v2/files'

FINISHERS = '/v2/finishers'

GLIDERS = '/v2/gliders'

# GW2 Basic Guild Endpoint
GUILD = '/v2/guild/{id}'
GUILD_LOG = '/v2/guild/{}/log'
GUILD_INVENTORY = '/v2/guild/{id}/inventory'
GUILD_MEMBERS = '/v2/guild/{id}/members'
GUILD_RANKS = '/v2/guild/{id}/ranks'
GUILD_STASH = '/v2/guild/{id}/stash'
GUILD_TEAMS = '/v2/guild/{id}/teams'
GUILD_TREASURY = '/v2/guild/{id}/treasury'
GUILD_UPGRADES = '/v2/guild/{id}/upgrades'

ALL_GUILD_PERMISSIONS = '/v2/guild/permissions'
ALL_GUILD_SEARCH = '/v2/guild/search'
ALL_GUILD_UPGRADES = '/v2/guild/upgrades'

ITEMS = '/v2/items'
ITEMSTATS = '/v2/itemstats'
LEGENDS = '/v2/legends'

LEADERBOARDS = '/v2/leaderboards'

MAPS = '/v2/maps'

MASTERIES = '/v2/masteries'

MATERIALS = '/v2/materials'

MINIS = '/v2/minis'

OUTFITS = '/v2/outfits'

PETS = '/v2/pets'

PROFESSIONS = '/v2/professions'

# GW2 PvP Endpoints
PVP = '/v2/pvp'
PVP_AMULETS = '/v2/pvp/amulets'
PVP_HEROES = '/v2/pvp/heroes'
PVP_RANKS = '/v2/pvp/ranks'
PVP_SEASONS = '/v2/pvp/seasons'
PVP_SEASONS_LEADERBOARDS = '/v2/pvp/seasons/leaderboards'

QUAGGANS = '/v2/quaggans'

RACES = '/v2/races'

RAIDS = '/v2/raids'

RECIPES = '/v2/recipes'
RECIPES_SEARCH = '/v2/recipes/search'

SKILLS = '/v2/skills'

SPECIALIZATIONS = '/v2/specializations'

STORIES = '/v2/stories'
STORIES_SEASONS = '/v2/stories/seasons'

TITLES = '/v2/titles'

TRAITS = '/v2/traits'

WORLDS = '/v2/worlds'

WVW = '/v2/wvw'
WVW_ABILITIES = '/v2/wvw/abilities'
WVW_MATCHES = '/v2/wvw/matches'
WVW_MATCHES_TEAMS = '/v2/wvw/matches/stats/{id}/teams'
WVW_OBJECTIVES = '/v2/wvw/objectives'
WVW_RANKS = '/v2/wvw/ranks'
WVW_UPGRADES = '/v2/wvw/upgrades'

EVENTS = '/v2/events'
EVENTS_STATE = '/v2/events-state'


def fuoco_para(a, b, c):
    fx = (1-b**2)+(4*a*c)
    fy = -b/(2*a)
    return fx, fy

def vertice_para(a,b,c):
    vx = -b/(2*a)
    vy = (4*a*c-b**2)/(4*a)
    return vx, vy

a = int(input("Quanto vale a? "))
b = int(input("Quanto vale b? "))
c = int(input("Quanto vale c? "))

Fuoco = fuoco_para(a, b, c)
Vertice = vertice_para(a,b,c)
print("Fuoco",Fuoco," Vertice", Vertice)


import configs
import covid.util as util
import covid.models.SEIRD_variable_detection
import numpy as np
import pandas as pd

data = util.load_data()

config_names = ['SEIRD', 'SEIRD_incident', 'SEIRD_variable_detection']
forecast_dates = ["2020-04-04", "2020-04-11",  "2020-04-18", "2020-04-25"]

places = ['KS', 'SC', 'IL', 'WI', 'LA', 'NE', 'FL', 'DE', 'ID', 'RI', 'CA', 'SD', 'MA', 'AK', 'MT', 'MD', 'KY', 'NH', 'NJ', 'AZ', 'GA', 'MO', 'NV', 'WA', 'CO', 'NC', 'TX', 'AL', 'DC', 'CT', 'OK', 'MS', 'TN', 'WY', 'IN', 'UT', 'VA', 'MN', 'ND', 'ME', 'OH', 'HI', 'NY', 'VT', 'AR', 'PA', 'IA', 'WV', 'NM', 'OR', 'MI']

eval_date = '2020-05-02'
root='results'

print(f"model,forecast_date,place,err")

for config_name in config_names:
     for forecast_date in forecast_dates:

          forecast_start = pd.to_datetime(forecast_date) + pd.Timedelta("1d")
          prefix = f"{root}/{config_name}/{forecast_date}"

          config = getattr(configs, config_name)
          errs = []

          for place in places:

               err = util.score_forecasts(start=forecast_start,
                                          place=place,
                                          data=data,
                                          model_type=config['model'],
                                          eval_date=eval_date,
                                          prefix=prefix)

               print(f"{config_name},{forecast_date},{place},{err:.2f}")

               errs.append(err)

          mae = np.mean(np.abs(np.array(errs)))
          print(f"{config_name},{forecast_date},MAE,{mae:.2f}")


"""
Deep Learning Assignment II
Task 2
"""
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

tf.logging.set_verbosity(tf.logging.INFO)

def main(args):
    mnist = input_data.read_data_sets("data/", reshape=False, one_hot=True, validation_size=0)

    learning_rate = 0.001
    training_iterations = 5000
    batch_size = 100
    display_iterations = 250

    input_layer = tf.placeholder(tf.float32, [None, 28, 28, 1])
    # input_layer = tf.reshape(raw_data, [-1, 28, 28, 1])
    output_layer = tf.placeholder(tf.float32, [None, 10])

    conv1 = tf.layers.conv2d(
        inputs=input_layer,
        filters=32,
        kernel_size=[5, 5],
        padding="same",
        activation=tf.nn.relu)

    pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)

    conv2 = tf.layers.conv2d(
        inputs=pool1,
        filters=64,
        kernel_size=[5, 5],
        padding="same",
        activation=tf.nn.relu)

    pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)

    pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64])

    dense1 = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)

    dense2 = tf.layers.dense(inputs=dense1, units=1024, activation=tf.nn.relu)

    logits = tf.layers.dense(inputs=dense2, units=10)

    with tf.name_scope('Model'):
        predictions = tf.nn.softmax(logits)

    with tf.name_scope('Loss'):
        cost = tf.reduce_mean(-tf.reduce_sum(output_layer * tf.log(predictions), reduction_indices=1))

    with tf.name_scope('Accuracy'):
        accuracy = tf.equal(tf.argmax(predictions, 1), tf.argmax(output_layer, 1))
        accuracy = tf.reduce_mean(tf.cast(accuracy, tf.float32))

    with tf.name_scope('SGD'):
        optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)
        avg_cost = 0.
        for iteration in range(training_iterations):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            _, c = sess.run([optimizer, cost],
                            feed_dict={input_layer: batch_xs, output_layer: batch_ys})
            avg_cost += c / display_iterations
            if (iteration + 1) % display_iterations == 0:
                tf.logging.info("Iteration: %s\tcost=%s", (iteration + 1), avg_cost)
                avg_cost = 0.

        tf.logging.info("Accuracy: %s", accuracy.eval({input_layer: mnist.test.images, output_layer: mnist.test.labels}))

if __name__ == "__main__":
    tf.app.run()


import numpy as np
from PyQt4 import uic, QtGui, QtCore
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import (
    FigureCanvasQTAgg as FigureCanvas,
    NavigationToolbar2QT as NavigationToolbar)
import matplotlib.patches as mpatches
import pkg_resources
import time as ttime
import math
import bluesky.plans as bp
from subprocess import call

from isstools.trajectory.trajectory  import trajectory
from isstools.trajectory.trajectory import trajectory_manager
from isstools.xasdata import xasdata
from isstools.xiaparser import xiaparser
from isstools.elements import elements
from isstools.dialogs import UpdateUserDialog
from isstools.dialogs import UpdateAngleOffset
from isstools.conversions import xray
import os
from os import listdir
from os.path import isfile, join
import inspect
import re
import sys

ui_path = pkg_resources.resource_filename('isstools', 'ui/XLive.ui')

# def my_plan(dets, some, other, param):
#	...

def auto_redraw_factory(fnc):

    def stale_callback(fig, stale):
        if fnc is not None:
            fnc(fig, stale)
        if stale and fig.canvas:
            fig.canvas.draw_idle()

    return stale_callback

class ScanGui(*uic.loadUiType(ui_path)):
    shutters_sig = QtCore.pyqtSignal()
    es_shutter_sig = QtCore.pyqtSignal()
    progress_sig = QtCore.pyqtSignal()

    def __init__(self, plan_funcs, tune_funcs, prep_traj_plan, RE, db, hhm, detectors, es_shutter, parent=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
        #self.fig = fig = self.figure_content()
        self.addCanvas()
        self.run_start.clicked.connect(self.run_scan)
        self.prep_traj_plan = prep_traj_plan
        self.RE = RE
        self.RE.last_state = ''
        self.db = db
        self.hhm = hhm
        self.hhm.trajectory_progress.subscribe(self.update_progress)
        self.progress_sig.connect(self.update_progressbar)
        self.progressBar.setValue(0)
        self.abs_parser = xasdata.XASdataAbs()
        self.flu_parser = xasdata.XASdataFlu()
        self.push_update_user.clicked.connect(self.update_user)
        self.label_angle_offset.setText('{0:.4f}'.format(float(RE.md['angle_offset'])))
        self.es_shutter = es_shutter

        # Write metadata in the GUI
        self.label_6.setText('{}'.format(RE.md['year']))
        self.label_7.setText('{}'.format(RE.md['cycle']))
        self.label_8.setText('{}'.format(RE.md['PROPOSAL']))
        self.label_9.setText('{}'.format(RE.md['SAF']))
        self.label_10.setText('{}'.format(RE.md['PI']))

        # Initialize 'trajectory' tab
        self.traj = trajectory()
        self.traj_manager = trajectory_manager(hhm)
        self.trajectory_path = '/GPFS/xf08id/trajectory/'
        #self.get_traj_names()
        self.comboBox_2.addItems(['1', '2', '3', '4', '5', '6', '7', '8', '9'])
        self.comboBox_3.addItems(['1', '2', '3', '4', '5', '6', '7', '8', '9'])
        self.comboBox_3.setCurrentIndex(self.traj_manager.current_lut() - 1)
        self.push_build_trajectory.clicked.connect(self.build_trajectory)
        self.push_save_trajectory.clicked.connect(self.save_trajectory)
        self.push_update_offset.clicked.connect(self.update_offset)
        self.push_select_traj_file.clicked.connect(self.get_traj_names)
        self.push_load_trajectory.clicked.connect(self.load_trajectory)
        self.push_init_trajectory.clicked.connect(self.init_trajectory)
        self.push_read_traj_info.clicked.connect(self.read_trajectory_info)
        self.push_prepare_trajectory.clicked.connect(self.run_prep_traj)
        self.push_plot_traj.clicked.connect(self.plot_traj_file)
        self.push_plot_traj.setDisabled(True)
        self.push_save_trajectory.setDisabled(True)

        # Initialize XIA tab
        self.xia_parser = xiaparser.xiaparser()
        self.push_gain_matching.clicked.connect(self.run_gain_matching)

        # Initialize detectors
        self.xia = detectors['xia']
        self.pba1 = detectors['pba1']
        self.pba2 = detectors['pba2']

        # Initialize 'tune' tab
        self.push_tune.clicked.connect(self.run_tune)
        self.tune_funcs = tune_funcs
        self.tune_funcs_names = [tune.__name__ for tune in tune_funcs]
        self.comboBox_4.addItems(self.tune_funcs_names)

        # Initialize 'run' tab
        self.plan_funcs = plan_funcs
        self.plan_funcs_names = [plan.__name__ for plan in plan_funcs]
        self.run_type.addItems(self.plan_funcs_names)
        self.push_re_abort.clicked.connect(self.re_abort)
        self.pushButton_scantype_help.clicked.connect(self.show_scan_help)
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.update_re_state)
        self.timer.start(1000)

        self.run_type.currentIndexChanged.connect(self.populateParams)
        self.params1 = []
        self.params2 = []
        self.params3 = []
        self.populateParams(0)

        # Initialize Ophyd elements
        self.shutter_a = elements.shutter('XF:08ID-PPS{Sh:FE}', name = 'shutter_a')
        self.shutter_b = elements.shutter('XF:08IDA-PPS{PSh}', name = 'shutter_b')
        self.shutter_a.state.subscribe(self.update_shutter)
        self.shutter_b.state.subscribe(self.update_shutter)
        self.push_fe_shutter.clicked.connect(self.toggle_fe_button)
        self.push_ph_shutter.clicked.connect(self.toggle_ph_button)
        self.push_es_shutter.clicked.connect(self.toggle_es_button)

        if self.shutter_a.state.value == 0:
            self.push_fe_shutter.setStyleSheet("background-color: lime")
        else:
            self.push_fe_shutter.setStyleSheet("background-color: red")
        if self.shutter_b.state.value == 0:
            self.push_ph_shutter.setStyleSheet("background-color: lime")
        else:
            self.push_ph_shutter.setStyleSheet("background-color: red")
        self.shutters_sig.connect(self.change_shutter_color)

        self.es_shutter_sig.connect(self.change_es_shutter_color)
        self.es_shutter.subscribe(self.update_es_shutter)
        self.change_es_shutter_color()

        # Initialize 'processing' tab
        self.push_select_file.clicked.connect(self.selectFile)
        self.push_bin.clicked.connect(self.process_bin)
        self.push_save_bin.clicked.connect(self.save_bin)
        self.push_calibrate.clicked.connect(self.calibrate_offset)
        self.push_replot_exafs.clicked.connect(self.update_k_view)
        self.push_replot_file.clicked.connect(self.replot_bin_equal)
        # Disable buttons
        self.push_bin.setDisabled(True)
        self.push_save_bin.setDisabled(True)
        self.push_replot_exafs.setDisabled(True)
        self.push_replot_file.setDisabled(True)

        # Redirect terminal output to GUI
        sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
        sys.stderr = EmittingStream(textWritten=self.normalOutputWritten)

    def update_user(self):
        dlg = UpdateUserDialog.UpdateUserDialog(self.label_6.text(), self.label_7.text(), self.label_8.text(), self.label_9.text(), self.label_10.text())
        if dlg.exec_():
            self.RE.md['year'], self.RE.md['cycle'], self.RE.md['PROPOSAL'], self.RE.md['SAF'], self.RE.md['PI'] = dlg.getValues()
            self.label_6.setText('{}'.format(self.RE.md['year']))
            self.label_7.setText('{}'.format(self.RE.md['cycle']))
            self.label_8.setText('{}'.format(self.RE.md['PROPOSAL']))
            self.label_9.setText('{}'.format(self.RE.md['SAF']))
            self.label_10.setText('{}'.format(self.RE.md['PI']))

    def update_offset(self):
        dlg = UpdateAngleOffset.UpdateAngleOffset(self.label_angle_offset.text())
        if dlg.exec_():
            self.RE.md['angle_offset'] = dlg.getValues()
            self.label_angle_offset.setText('{}'.format(self.RE.md['angle_offset']))

    def update_es_shutter(self, pvname=None, value=None, char_value=None, **kwargs):
        self.es_shutter_sig.emit()

    def change_es_shutter_color(self):
        if self.es_shutter.state == 'closed':
            self.push_es_shutter.setStyleSheet("background-color: red")
        elif self.es_shutter.state == 'open':
            self.push_es_shutter.setStyleSheet("background-color: lime")

    def update_shutter(self, pvname=None, value=None, char_value=None, **kwargs):
        if(kwargs['obj'] == self.shutter_a.state):
            current_button = self.push_fe_shutter
        elif(kwargs['obj'] == self.shutter_b.state):
            current_button = self.push_ph_shutter

        self.current_button = current_button
        if int(value) == 0:
            self.current_button_color = 'lime'
        if int(value) == 1:
            self.current_button_color = 'red'
        self.shutters_sig.emit()

    def change_shutter_color(self):
        self.current_button.setStyleSheet("background-color: " + self.current_button_color)

    def toggle_fe_button(self):
        if(int(self.shutter_a.state.value)):
            self.shutter_a.open()
        else:
            self.shutter_a.close()

    def toggle_ph_button(self):
        if(int(self.shutter_b.state.value)):
            self.shutter_b.open()
        else:
            self.shutter_b.close()

    def toggle_es_button(self):
        if(self.es_shutter.state == 'closed'):
            self.es_shutter.open()
        else:
            self.es_shutter.close()

    def update_progress(self, pvname = None, value=None, char_value=None, **kwargs):
        self.progress_sig.emit()
        self.progressValue = value

    def update_progressbar(self):
        self.progressBar.setValue(int(np.round(self.progressValue)))

    def getX(self, event):
        self.edit_E0_2.setText(str(int(np.round(event.xdata))))

    def selectFile(self):
        if self.checkBox_process_bin.checkState() > 0:
            self.selected_filename_bin = QtGui.QFileDialog.getOpenFileNames(directory = '/GPFS/xf08id/User Data/', filter = '*.txt')
        else:
            self.selected_filename_bin = [QtGui.QFileDialog.getOpenFileName(directory = '/GPFS/xf08id/User Data/', filter = '*.txt')]
        if self.selected_filename_bin:
            if len(self.selected_filename_bin) > 1:
                filenames = []
                for name in self.selected_filename_bin:
                    filenames.append(name.rsplit('/', 1)[1])
                filenames = ', '.join(filenames)
            elif len(self.selected_filename_bin) == 1:
                filenames = self.selected_filename_bin[0]

            self.label_24.setText(filenames)
            self.process_bin_equal()

    def save_bin(self):
        filename = self.curr_filename_save
        self.abs_parser.data_manager.export_dat(filename, self.abs_parser.header_read.replace('Timestamp (s)   ','', 1)[:-1])
        print('File Saved! [{}]'.format(filename[:-3] + 'dat'))

    def calibrate_offset(self):
        ret = self.questionMessage('Confirmation', 'Are you sure you would like to calibrate it?')
        if not ret:
            print ('[E0 Calibration] Aborted!')
            return False
        self.RE.md['angle_offset'] = str(float(self.RE.md['angle_offset']) + (xray.energy2encoder(float(self.edit_E0_2.text())) - xray.energy2encoder(float(self.edit_ECal.text())))/360000)
        self.label_angle_offset.setText('{0:.4f}'.format(float(self.RE.md['angle_offset'])))
        print ('[E0 Calibration] New value: {}\n[E0 Calibration] Completed!'.format(self.RE.md['angle_offset']))

    def get_dic(self, module):
        dic = dict()
        if self.checkBox_num_i0.checkState() > 0:
            dic['numerator'] = module.i0_interp
            if(hasattr(module, 'data_i0')):
                dic['original_numerator'] = module.data_i0
        elif self.checkBox_num_it.checkState() > 0:
            dic['numerator'] = module.it_interp
            if(hasattr(module, 'data_it')):
                dic['original_numerator'] = module.data_it
        elif self.checkBox_num_ir.checkState() > 0:
            dic['numerator'] = module.ir_interp
            if(hasattr(module, 'data_ir')):
                dic['original_numerator'] = module.data_ir
        elif self.checkBox_num_1.checkState() > 0:
            if len(module.i0_interp.shape) > 1:
                array_ones = np.copy(module.i0_interp)
                array_ones[:,1] = np.ones(len(module.i0_interp[:,0]))
            else:
                array_ones = np.ones(len(module.i0_interp))
            dic['numerator'] = array_ones

            if(hasattr(module, 'data_i0')):
                if len(module.data_i0.shape) > 1:
                    array_ones = np.copy(module.data_i0)
                    array_ones[:,1] = np.ones(len(module.data_i0[:,0]))
                else:
                    array_ones = np.ones(len(module.data_i0))
                    dic['original_numerator'] = module.data_i0
            dic['original_numerator'] = array_ones

        if self.checkBox_den_i0.checkState() > 0:
            dic['denominator'] = module.i0_interp
            if(hasattr(module, 'data_i0')):
                dic['original_denominator'] = module.data_i0
        elif self.checkBox_den_it.checkState() > 0:
            dic['denominator'] = module.it_interp
            if(hasattr(module, 'data_it')):
                dic['original_denominator'] = module.data_it
        elif self.checkBox_den_ir.checkState() > 0:
            dic['denominator'] = module.ir_interp
            if(hasattr(module, 'data_ir')):
                dic['original_denominator'] = module.data_ir
        elif self.checkBox_den_1.checkState() > 0:
            if len(module.i0_interp.shape) > 1:
                array_ones = np.copy(module.i0_interp)
                array_ones[:,1] = np.ones(len(module.i0_interp[:,0]))
            else:
                array_ones = np.ones(len(module.i0_interp))
            dic['denominator'] = array_ones

            if(hasattr(module, 'data_i0')):
                if len(module.data_i0.shape) > 1:
                    array_ones = np.copy(module.data_i0)
                    array_ones[:,1] = np.ones(len(module.data_i0[:,0]))
                else:
                    array_ones = np.ones(len(module.data_i0))
                    dic['original_denominator'] = module.data_i0
            dic['original_denominator'] = array_ones

        if self.checkBox_log.checkState() > 0:
            dic['log'] = True
        else:
            dic['log'] = False

        return dic

    def update_k_view(self):
        e0 = int(self.edit_E0_2.text())
        edge_start = int(self.edit_edge_start.text())
        edge_end = int(self.edit_edge_end.text())
        preedge_spacing = float(self.edit_preedge_spacing.text())
        xanes_spacing = float(self.edit_xanes_spacing.text())
        exafs_spacing = float(self.edit_exafs_spacing.text())
        k_power = float(self.edit_y_power.text())

        k_data = self.abs_parser.data_manager.get_k_data(e0,
                                                         edge_end,
                                                         exafs_spacing,
                                                         self.abs_parser.data_manager.abs,
                                                         self.abs_parser.data_manager.sorted_matrix[:, 1],
                                                         self.abs_parser.data_manager.data_en,
                                                         self.abs_parser.data_manager.abs_orig,
                                                         k_power)
        self.figure_old_scans.ax.cla()
        self.figure_old_scans.ax.plot(k_data[0], k_data[1])
        self.figure_old_scans.ax.set_xlabel('k')
        self.figure_old_scans.ax.set_ylabel(r'$\kappa$ * k ^ {}'.format(k_power)) #'ϰ * k ^ {}'.format(k_power))
        self.figure_old_scans.ax.grid(True)
        self.canvas_old_scans.draw_idle()

    def process_bin(self):

        # Plot equal spacing bin
        #self.figure_old_scans_3.ax.cla()
        e0 = int(self.edit_E0_2.text())
        edge_start = int(self.edit_edge_start.text())
        edge_end = int(self.edit_edge_end.text())
        preedge_spacing = float(self.edit_preedge_spacing.text())
        xanes_spacing = float(self.edit_xanes_spacing.text())
        exafs_spacing = float(self.edit_exafs_spacing.text())
        k_power = float(self.edit_y_power.text())

        if e0 < self.figure_old_scans_2.axes[0].xaxis.get_data_interval()[0] or e0 > self.figure_old_scans_2.axes[0].xaxis.get_data_interval()[1]:
            ret = self.questionMessage('E0 Confirmation', 'E0 seems to be out of the scan range. Would you like to proceed?')
            if not ret:
                print ('Binning aborted!')
                return False

        self.abs_parser.bin(e0,
                            e0 + edge_start,
                            e0 + edge_end,
                            preedge_spacing,
                            xanes_spacing,
                            exafs_spacing)

        dic = self.get_dic(self.abs_parser.data_manager)

        self.abs_parser.data_manager.plot(plotting_dic = dic, ax = self.figure_old_scans_3.ax, color = 'r')
        self.canvas_old_scans_3.draw_idle()

        k_data = self.abs_parser.data_manager.get_k_data(e0,
                                                         edge_end,
                                                         exafs_spacing,
                                                         self.abs_parser.data_manager.abs,
                                                         self.abs_parser.data_manager.sorted_matrix[:, 1],
                                                         self.abs_parser.data_manager.data_en,
                                                         self.abs_parser.data_manager.abs_orig,
                                                         k_power)
        self.figure_old_scans.ax.cla()
        self.figure_old_scans.ax.plot(k_data[0], k_data[1])
        self.figure_old_scans.ax.grid(True)
        self.figure_old_scans.ax.set_xlabel('k')
        self.figure_old_scans.ax.set_ylabel(r'$\kappa$ * k ^ {}'.format(k_power)) #'ϰ * k ^ {}'.format(k_power))
        self.canvas_old_scans.draw_idle()
        self.push_replot_exafs.setEnabled(True)
        self.push_save_bin.setEnabled(True)

    def replot_bin_equal(self):
        # Erase final plot (in case there is old data there)
        self.figure_old_scans_3.ax.cla()
        self.canvas_old_scans_3.draw_idle()

        self.figure_old_scans.ax.cla()
        self.canvas_old_scans.draw_idle()

        self.figure_old_scans_3.ax.cla()
        dic = self.get_dic(self.abs_parser)
        self.abs_parser.plot(plotting_dic = dic,
                             ax = self.figure_old_scans_3.ax,
                             color = 'b')

        self.figure_old_scans_2.ax.cla()
        self.figure_old_scans_2.ax2.cla()
        self.canvas_old_scans_2.draw_idle()
        self.toolbar_old_scans_2._views.clear()
        self.toolbar_old_scans_2._positions.clear()
        #self.abs_parser.bin_equal()
        dic = self.get_dic(self.abs_parser.data_manager)
        self.abs_parser.data_manager.plot(plotting_dic = dic,
                                          ax = self.figure_old_scans_2.ax,
                                          color = 'b')
        self.figure_old_scans_2.ax.set_ylabel('Log(i0/it)', color='b')
        self.edge_index = self.abs_parser.data_manager.get_edge_index(self.abs_parser.data_manager.abs)
        if self.edge_index > 0:
            x_edge = self.abs_parser.data_manager.en_grid[self.edge_index]
            y_edge = self.abs_parser.data_manager.abs[self.edge_index]

            self.figure_old_scans_2.ax.plot(x_edge, y_edge, 'ys')
            edge_path = mpatches.Patch(facecolor='y', edgecolor = 'black', label='Edge')
            self.figure_old_scans_2.ax.legend(handles = [edge_path])
            self.figure_old_scans_2.ax.annotate('({0:.2f}, {1:.2f})'.format(x_edge, y_edge), xy=(x_edge, y_edge), textcoords='data')
            print('Edge: ' + str(int(np.round(self.abs_parser.data_manager.en_grid[self.edge_index]))))
            self.edit_E0_2.setText(str(int(np.round(self.abs_parser.data_manager.en_grid[self.edge_index]))))

        self.abs_parser.data_manager.plot_der(plotting_dic = dic, ax = self.figure_old_scans_2.ax2, color = 'r')
        self.figure_old_scans_2.ax2.set_ylabel('Derivative', color='r')

        self.canvas_old_scans_3.draw_idle()
        self.canvas_old_scans_2.draw_idle()

        self.push_replot_exafs.setDisabled(True)
        self.push_save_bin.setDisabled(True)

    def process_bin_equal(self):
        for filename in self.selected_filename_bin:
            self.abs_parser.loadInterpFile(filename) #self.label_24.text())

            # Erase final plot (in case there is old data there)
            self.figure_old_scans_3.ax.cla()
            self.canvas_old_scans_3.draw_idle()

            self.figure_old_scans.ax.cla()
            self.canvas_old_scans.draw_idle()

            self.figure_old_scans_3.ax.cla()
            dic = self.get_dic(self.abs_parser)
            self.abs_parser.plot(plotting_dic = dic, ax = self.figure_old_scans_3.ax, color = 'b')

            self.figure_old_scans_2.ax.cla()
            self.figure_old_scans_2.ax2.cla()
            self.canvas_old_scans_2.draw_idle()
            self.toolbar_old_scans_2._views.clear()
            self.toolbar_old_scans_2._positions.clear()
            self.abs_parser.bin_equal()
            dic = self.get_dic(self.abs_parser.data_manager)
            self.abs_parser.data_manager.plot(plotting_dic = dic, ax = self.figure_old_scans_2.ax, color = 'b')
            self.figure_old_scans_2.ax.set_ylabel('Log(i0/it)', color='b')

            if self.checkBox_find_edge.checkState() > 0:
                self.edge_index = self.abs_parser.data_manager.get_edge_index(self.abs_parser.data_manager.abs)
                if self.edge_index > 0:
                    x_edge = self.abs_parser.data_manager.en_grid[self.edge_index]
                    y_edge = self.abs_parser.data_manager.abs[self.edge_index]

                    self.figure_old_scans_2.ax.plot(x_edge, y_edge, 'ys')
                    edge_path = mpatches.Patch(facecolor='y', edgecolor = 'black', label='Edge')
                    self.figure_old_scans_2.ax.legend(handles = [edge_path])
                    self.figure_old_scans_2.ax.annotate('({0:.2f}, {1:.2f})'.format(x_edge, y_edge), xy=(x_edge, y_edge), textcoords='data')
                    print('Edge: ' + str(int(np.round(self.abs_parser.data_manager.en_grid[self.edge_index]))))
                    self.edit_E0_2.setText(str(int(np.round(self.abs_parser.data_manager.en_grid[self.edge_index]))))

            self.abs_parser.data_manager.plot_der(plotting_dic = dic, ax = self.figure_old_scans_2.ax2, color = 'r')
            self.figure_old_scans_2.ax2.set_ylabel('Derivative', color='r')

            self.canvas_old_scans_3.draw_idle()
            self.canvas_old_scans_2.draw_idle()

            cid = self.canvas_old_scans_2.mpl_connect('button_press_event', self.getX)

            self.curr_filename_save = filename
            if self.checkBox_process_bin.checkState() > 0:
                self.process_bin()
                self.save_bin()

            self.push_bin.setEnabled(True)
            self.push_replot_exafs.setDisabled(True)
            self.push_save_bin.setDisabled(True)
            self.push_replot_file.setEnabled(True)

    def __del__(self):
        # Restore sys.stdout
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__

    def normalOutputWritten(self, text):
        """Append text to the QtextEdit_terminal."""
        # Maybe QtextEdit_terminal.append() works as well, but this is how I do it:
        cursor = self.textEdit_terminal.textCursor()
        cursor.movePosition(QtGui.QTextCursor.End)
        cursor.insertText(text)
        self.textEdit_terminal.setTextCursor(cursor)
        self.textEdit_terminal.ensureCursorVisible()
        #sys.__stdout__.writelines(text)

    def populateParams(self, index):
        for i in range(len(self.params1)):
            self.gridLayout_13.removeWidget(self.params1[i])
            self.gridLayout_13.removeWidget(self.params2[i])
            self.gridLayout_13.removeWidget(self.params3[i])
            self.params1[i].deleteLater()
            self.params2[i].deleteLater()
            self.params3[i].deleteLater()
        self.params1 = []
        self.params2 = []
        self.params3 = []
        self.param_types = []
        plan_func = self.plan_funcs[index]
        signature = inspect.signature(plan_func)
        for i in range(0, len(signature.parameters)):
            default = re.sub(r':.*?=', '=', str(signature.parameters[list(signature.parameters)[i]]))
            if default == str(signature.parameters[list(signature.parameters)[i]]):
                default = re.sub(r':.*', '', str(signature.parameters[list(signature.parameters)[i]]))
            self.addParamControl(list(signature.parameters)[i], default, signature.parameters[list(signature.parameters)[i]].annotation)
            self.param_types.append(signature.parameters[list(signature.parameters)[i]].annotation)

    def addParamControl(self, name, default, annotation):
        rows = int(self.gridLayout_13.count()/3)
        param1 = QtGui.QLabel('Par ' + str(rows + 1))

        def_val = ''
        if default.find('=') != -1:
            def_val = re.sub(r'.*=', '', default)
        if annotation == int:
            param2 = QtGui.QSpinBox()
            def_val = int(def_val)
            param2.setValue(def_val)
        elif annotation == float:
            param2 = QtGui.QDoubleSpinBox()
            def_val = float(def_val)
            param2.setValue(def_val)
        elif annotation == bool:
            param2 = QtGui.QCheckBox()
            if def_val == 'True':
                def_val = True
            else:
                def_val = False
            param2.setCheckState(def_val)
            param2.setTristate(False)
        else:
            param2 = QtGui.QLineEdit()
            def_val = str(def_val)
            param2.setText(def_val)

        param3 = QtGui.QLabel(default)
        self.gridLayout_13.addWidget(param1, rows, 0, QtCore.Qt.AlignTop)
        self.gridLayout_13.addWidget(param2, rows, 1, QtCore.Qt.AlignTop)
        self.gridLayout_13.addWidget(param3, rows, 2, QtCore.Qt.AlignTop)
        self.params1.append(param1)
        self.params2.append(param2)
        self.params3.append(param3)

    def get_traj_names(self):
        #self.comboBox.clear()
        #self.comboBox.addItems([f for f in sorted(listdir(self.trajectory_path)) if isfile(join(self.trajectory_path, f))])
        self.label_56.setText(QtGui.QFileDialog.getOpenFileName(directory = self.trajectory_path, filter = '*.txt').rsplit('/',1)[1])
        self.push_plot_traj.setEnabled(True)

    def addCanvas(self):
        self.figure = Figure()
        self.figure.set_facecolor(color='0.89')
        self.canvas = FigureCanvas(self.figure)
        self.figure.ax = self.figure.add_subplot(111)
        self.toolbar = NavigationToolbar(self.canvas, self.tab_2, coordinates=True)
        self.toolbar.setMaximumHeight(25)
        self.plots.addWidget(self.toolbar)
        self.plots.addWidget(self.canvas)
        self.canvas.draw_idle()

        self.figure_single_trajectory = Figure()
        self.figure_single_trajectory.set_facecolor(color='0.89')
        self.canvas_single_trajectory = FigureCanvas(self.figure_single_trajectory)
        self.figure_single_trajectory.ax = self.figure_single_trajectory.add_subplot(111)
        self.toolbar = NavigationToolbar(self.canvas_single_trajectory, self.tab_2, coordinates=True)
        self.toolbar.setMaximumHeight(25)
        self.plot_single_trajectory.addWidget(self.toolbar)
        self.plot_single_trajectory.addWidget(self.canvas_single_trajectory)
        self.canvas_single_trajectory.draw_idle()

        self.figure_full_trajectory = Figure()
        self.figure_full_trajectory.set_facecolor(color='0.89')
        self.canvas_full_trajectory = FigureCanvas(self.figure_full_trajectory)
        self.figure_full_trajectory.add_subplot(111)
        self.figure_full_trajectory.ax = self.figure_full_trajectory.add_subplot(111)
        self.toolbar = NavigationToolbar(self.canvas_full_trajectory, self.tab_2, coordinates=True)
        self.toolbar.setMaximumHeight(25)
        self.plot_full_trajectory.addWidget(self.toolbar)
        self.plot_full_trajectory.addWidget(self.canvas_full_trajectory)
        self.canvas_full_trajectory.draw_idle()

        self.figure_tune = Figure()
        self.figure_tune.set_facecolor(color='0.89')
        self.canvas_tune = FigureCanvas(self.figure_tune)
        self.figure_tune.add_subplot(111)
        self.plot_tune.addWidget(self.canvas_tune)
        self.canvas_tune.draw_idle()

        self.figure_gain_matching = Figure()
        self.figure_gain_matching.set_facecolor(color='0.89')
        self.canvas_gain_matching = FigureCanvas(self.figure_gain_matching)
        self.figure_gain_matching.add_subplot(111)
        self.plot_gain_matching.addWidget(self.canvas_gain_matching)
        self.canvas_gain_matching.draw_idle()

        self.figure_old_scans = Figure()
        self.figure_old_scans.set_facecolor(color='0.89')
        self.canvas_old_scans = FigureCanvas(self.figure_old_scans)
        self.figure_old_scans.ax = self.figure_old_scans.add_subplot(111)
        self.toolbar_old_scans = NavigationToolbar(self.canvas_old_scans, self.tab_2, coordinates=True)
        self.plot_old_scans.addWidget(self.toolbar_old_scans)
        self.plot_old_scans.addWidget(self.canvas_old_scans)
        self.canvas_old_scans.draw_idle()

        self.figure_old_scans_2 = Figure()
        self.figure_old_scans_2.set_facecolor(color='0.89')
        self.canvas_old_scans_2 = FigureCanvas(self.figure_old_scans_2)
        self.figure_old_scans_2.ax = self.figure_old_scans_2.add_subplot(111)
        self.figure_old_scans_2.ax2 = self.figure_old_scans_2.ax.twinx()
        self.toolbar_old_scans_2 = NavigationToolbar(self.canvas_old_scans_2, self.tab_2, coordinates=True)
        self.plot_old_scans_2.addWidget(self.toolbar_old_scans_2)
        self.plot_old_scans_2.addWidget(self.canvas_old_scans_2)
        self.canvas_old_scans_2.draw_idle()

        self.figure_old_scans_3 = Figure()
        self.figure_old_scans_3.set_facecolor(color='0.89')
        self.canvas_old_scans_3 = FigureCanvas(self.figure_old_scans_3)
        self.figure_old_scans_3.ax = self.figure_old_scans_3.add_subplot(111)
        self.toolbar_old_scans_3 = NavigationToolbar(self.canvas_old_scans_3, self.tab_3, coordinates=True)
        self.plot_old_scans_3.addWidget(self.toolbar_old_scans_3)
        self.plot_old_scans_3.addWidget(self.canvas_old_scans_3)
        self.canvas_old_scans_3.draw_idle()

    @property
    def plot_x(self):
        return self.plot_selection_dropdown.value()

    def figure_content(self):
        fig1 = Figure()
        fig1.set_facecolor(color='0.89')
        fig1.stale_callback = auto_redraw_factory(fig1.stale_callback)
        ax1f1 = fig1.add_subplot(111)
        ax1f1.plot(np.random.rand(5))
        self.ax = ax1f1
        return fig1

    def run_tune(self):
        if self.shutter_a.state.value == 1 or self.shutter_b.state.value == 1:
            ret = self.questionMessage('Shutter closed', 'Would you like to run the tuning with the shutter closed?')
            if not ret:
                print ('Aborted!')
                return False

        self.figure_tune.clf()
        self.tune_funcs[self.comboBox_4.currentIndex()](float(self.edit_tune_range.text()), float(self.edit_tune_step.text()), self.spinBox_tune_retries.value(), self.figure_tune)

    def run_prep_traj(self):
        self.RE(self.prep_traj_plan())

    def build_trajectory(self):
        E0 = int(self.edit_E0.text())
        preedge_lo = int(self.edit_preedge_lo.text())
        preedge_hi = int(self.edit_preedge_hi.text())
        edge_hi = int(self.edit_edge_hi.text())

        postedge_k = float(self.edit_postedge_hi.text())
        postedge_hi = xray.k2e(postedge_k, E0) - E0 #(1000 * ((postedge_k ** 2) + (16.2009 ** 2) * E0/1000) / (16.2009 ** 2)) - E0

        velocity_preedge = int (self.edit_velocity_preedge.text())
        velocity_edge = int(self.edit_velocity_edge.text())
        velocity_postedge = int(self.edit_velocity_postedge.text())

        preedge_stitch_lo = int(self.edit_preedge_stitch_lo.text())
        preedge_stitch_hi = int(self.edit_preedge_stitch_hi.text())
        edge_stitch_lo =  int(self.edit_edge_stitch_lo.text())
        edge_stitch_hi = int(self.edit_edge_stitch_hi.text())
        postedge_stitch_lo = int(self.edit_postedge_stitch_lo.text())
        postedge_stitch_hi = int(self.edit_postedge_stitch_hi.text())

        padding_preedge = float(self.edit_padding_preedge.text())
        padding_postedge = float(self.edit_padding_postedge.text())

        sine_duration = float(self.edit_sine_total_duration.text())

        dsine_preedge_duration = float(self.edit_ds_pree_duration.text())
        dsine_postedge_duration = float(self.edit_ds_poste_duration.text())

        traj_type = self.tabWidget_2.tabText(self.tabWidget_2.currentIndex())

        #Create and interpolate trajectory
        self.traj.define(edge_energy = E0, offsets = ([preedge_lo,preedge_hi,edge_hi,postedge_hi]),velocities = ([velocity_preedge, velocity_edge, velocity_postedge]),\
                        stitching = ([preedge_stitch_lo, preedge_stitch_hi, edge_stitch_lo, edge_stitch_hi, postedge_stitch_lo, postedge_stitch_hi]),\
                        servocycle = 16000, padding_lo = padding_preedge ,padding_hi=padding_postedge, sine_duration = sine_duration,
                        dsine_preedge_duration = dsine_preedge_duration, dsine_postedge_duration = dsine_postedge_duration, trajectory_type = traj_type)
        self.traj.interpolate()

        #Plot single trajectory motion
        self.figure_single_trajectory.clf()
        ax = self.figure_single_trajectory.add_subplot(111)
        ax.hold(False)
        ax.plot(self.traj.time, self.traj.energy, 'ro')
        ax.hold(True)
        ax.plot(self.traj.time_grid, self.traj.energy_grid, 'b')
        ax.set_xlabel('Time /s')
        ax.set_ylabel('Energy /eV')
        ax2 = ax.twinx()
        ax2.hold(False)
        ax2.plot(self.traj.time_grid[0:-1], self.traj.energy_grid_der, 'r')
        self.canvas_single_trajectory.draw_idle()

        # Tile trajectory
        self.figure_full_trajectory.clf()
        self.traj.tile(reps=self.spinBox_tiling_repetitions.value())

        # Convert to encoder counts
        self.traj.e2encoder(float(self.label_angle_offset.text()))

        # Draw
        ax = self.figure_full_trajectory.add_subplot(111)
        ax.hold(False)
        ax.plot(self.traj.encoder_grid, 'b')
        ax.set_xlabel('Servo event / 1/16000 s')
        ax.set_ylabel('Encoder count')
        self.canvas_full_trajectory.draw_idle()

        self.push_save_trajectory.setEnabled(True)

    def save_trajectory(self):
        filename = QtGui.QFileDialog.getSaveFileName(self, 'Save trajectory...', self.trajectory_path, '*.txt')
        if filename[-4:] != '.txt' and len(filename):
            filename += '.txt'
            if (os.path.isfile(filename)):
                ret = self.questionMessage('Save trajectory...', '{} already exists. Do you want to replace it?'.format(filename.rsplit('/',1)[1]))
                if not ret:
                    print ('Aborted!')
                    return
        elif not len(filename):
            print('\nInvalid name! Select a valid name...')
            return
        print('Filename = {}'.format(filename))

        if(len(self.traj.energy_grid)):
            np.savetxt(filename,
	               self.traj.encoder_grid, fmt='%d')
            call(['chmod', '666', filename])
            #self.get_traj_names()
            self.label_56.setText(filename.rsplit('/',1)[1])
            self.push_plot_traj.setEnabled(True)
            print('Trajectory saved! [{}]'.format(filename))
        else:
            print('\nCreate the trajectory first...')

    def plot_traj_file(self):
        self.traj.load_trajectory_file('/GPFS/xf08id/trajectory/' + self.label_56.text())#self.comboBox.currentText())

        self.figure_single_trajectory.clf()
        self.figure_single_trajectory.add_subplot(111)
        self.canvas_single_trajectory.draw_idle()

        ax = self.figure_full_trajectory.add_subplot(111)
        ax.hold(False)
        ax.plot(np.arange(0, len(self.traj.energy_grid_loaded)/16000, 1/16000), self.traj.energy_grid_loaded, 'b')
        ax.set_xlabel('Time /s')
        ax.set_ylabel('Energy /eV')
        ax.set_title(self.label_56.text())#self.comboBox.currentText())
        self.canvas_full_trajectory.draw_idle()
        print('Trajectory Load: Done')

        self.push_save_trajectory.setDisabled(True)

    def load_trajectory(self):
        self.traj_manager.load(orig_file_name = self.label_56.text(), new_file_path = self.comboBox_2.currentText())

    def init_trajectory(self):
        self.run_start.setDisabled(True)
        self.traj_manager.init(int(self.comboBox_3.currentText()))
        self.run_start.setEnabled(True)

    def read_trajectory_info(self):
        self.traj_manager.read_info()

    def run_scan(self):
        if self.run_type.currentText() == 'get_offsets':
            if self.shutter_b.state.read()['shutter_b_state']['value'] != 1:
                self.shutter_b.close()
                while self.shutter_b.state.read()['shutter_b_state']['value'] != 1:
                    QtGui.QApplication.processEvents()
                    ttime.sleep(0.1)

        elif self.shutter_a.state.value == 1 or self.shutter_b.state.value == 1:
            ret = self.questionMessage('Shutter closed', 'Would you like to run the scan with the shutter closed?')
            if not ret:
                print ('Aborted!')
                return False

        self.comment = self.params2[0].text()
        if(self.comment):
            print('\nStarting scan...')

            # Get parameters from the widgets and organize them in a tuple (run_params)
            run_params = ()
            for i in range(len(self.params1)):
                if (self.param_types[i] == int):
                    run_params += (self.params2[i].value(),)
                elif (self.param_types[i] == float):
                    run_params += (self.params2[i].value(),)
                elif (self.param_types[i] == bool):
                    run_params += (bool(self.params2[i].checkState()),)
                else:
                    run_params += (self.params2[i].text(),)

            # Erase last graph
            self.figure.ax.cla()
            self.canvas.draw_idle()

            # Run the scan using the tuple created before
            self.current_uid, self.current_filepath, absorp = self.plan_funcs[self.run_type.currentIndex()](*run_params)

            if absorp == True:
                self.parser = xasdata.XASdataAbs()
                self.parser.loadInterpFile(self.current_filepath)
                self.parser.plot(ax = self.figure.ax)
            elif absorp == False:
                self.parser = xasdata.XASdataFlu()
                self.parser.loadInterpFile(self.current_filepath)
                xia_filename = self.db[self.current_uid]['start']['xia_filename']
                xia_filepath = 'smb://elistavitski-ni/epics/{}'.format(xia_filename)
                xia_destfilepath = '/GPFS/xf08id/xia_files/{}'.format(xia_filename)
                smbclient = xiaparser.smbclient(xia_filepath, xia_destfilepath)
                smbclient.copy()
                xia_parser = self.xia_parser
                xia_parser.parse(xia_filename, '/GPFS/xf08id/xia_files/')
                xia_parsed_filepath = self.current_filepath[0 : self.current_filepath.rfind('/') + 1]
                xia_parser.export_files(dest_filepath = xia_parsed_filepath, all_in_one = True)
            # Fix that later
                length = min(len(xia_parser.exporting_array1), len(parser.energy_interp))
                xia_parser.plot_roi(xia_filename, '/GPFS/xf08id/xia_files/', range(0, length), 1, 8, 10, self.figure.ax, parser.energy_interp)
                xia_parser.plot_roi(xia_filename, '/GPFS/xf08id/xia_files/', range(0, length), 2, 8, 10, self.figure.ax, parser.energy_interp)
                xia_parser.plot_roi(xia_filename, '/GPFS/xf08id/xia_files/', range(0, length), 3, 8, 10, self.figure.ax, parser.energy_interp)
                xia_parser.plot_roi(xia_filename, '/GPFS/xf08id/xia_files/', range(0, length), 4, 8, 10, self.figure.ax, parser.energy_interp)

            if absorp != '':
                self.figure.ax.set_title(self.comment)

                self.log_path = self.current_filepath[0 : self.current_filepath.rfind('/') + 1] + 'log/'
                if(not os.path.exists(self.log_path)):
                    os.makedirs(self.log_path)

                self.snapshots_path = self.log_path + 'snapshots/'
                if(not os.path.exists(self.snapshots_path)):
                    os.makedirs(self.snapshots_path)

                self.file_path = 'snapshots/' + self.comment + '.png'
                fn = self.log_path + self.file_path
                repeat = 1
                while(os.path.isfile(fn)):
                    repeat += 1
                    self.file_path = 'snapshots/' + self.comment + '-' + str(repeat) + '.png'
                    fn = self.log_path + self.file_path
                self.figure.savefig(fn)

                self.canvas.draw_idle()

                if self.checkBox_auto_process.checkState() > 0: # Change to a control
                    self.tabWidget.setCurrentIndex(4)
                    self.selected_filename_bin = [self.current_filepath]
                    self.label_24.setText(self.current_filepath)
                    self.process_bin_equal()

                # Check saturation:
                # TODO: add way to check all three before raising error exception
                try:
                    warnings = ()
                    if np.max(np.abs(self.parser.i0_interp[:,1])) > 3.9:
                        warnings += ('"i0" seems to be saturated',) #(values > 3.9 V), please change the ion chamber gain',)
                    if np.max(np.abs(self.parser.it_interp[:,1])) > 3.9:
                        warnings += ('"it" seems to be saturated',) #(values > 3.9 V), please change the ion chamber gain',)
                    if np.max(np.abs(self.parser.ir_interp[:,1])) > 9.9:
                        warnings += ('"ir" seems to be saturated',) #(values > 9.9 V), please change the ion chamber gain',)
                    if len(warnings):
                        raise Warning(warnings)

                except Warning as warnings:
                    warningtxt = ''
                    for warning in warnings.args[0]:
                        print('Warning: {}'.format(warning))
                        warningtxt += '{}\n'.format(warning)
                    warningtxt += 'Check the gains of the ion chambers'
                    QtGui.QMessageBox.warning(self, 'Warning!', warningtxt)
                    #raise

        else:
            print('\nPlease, type a comment about the scan in the field "comment"\nTry again')

    def re_abort(self):
        self.RE.abort()

    def update_re_state(self):
        palette = self.label_11.palette()
        if(self.RE.state == 'idle'):
            palette.setColor(self.label_11.foregroundRole(), QtGui.QColor(193, 140, 15))
        elif(self.RE.state == 'running'):
            palette.setColor(self.label_11.foregroundRole(), QtGui.QColor(0, 165, 0))
        elif(self.RE.state == 'paused'):
            palette.setColor(self.label_11.foregroundRole(), QtGui.QColor(255, 0, 0))
        self.label_11.setPalette(palette)
        self.label_11.setText(self.RE.state)
        if self.RE.state != self.RE.last_state:
            self.RE.last_state = self.RE.state

    def run_gain_matching(self):
        channels = range(4)

        ax = self.figure_gain_matching.add_subplot(111)
        gain_adjust = [0.001, 0.001, 0.001, 0.001]
        diff = [0, 0, 0, 0]
        diff_old = [0, 0, 0, 0]

        # Run number of iterations defined in the text edit edit_gain_matching_iterations:
        for i in range(int(self.edit_gain_matching_iterations.text())):
            self.xia.collect_mode.put('MCA spectra')
            ttime.sleep(0.25)
            self.xia.mode.put('Real time')
            ttime.sleep(0.25)
            self.xia.real_time.put('1')
            self.xia.capt_start_stop.put(1)
            ttime.sleep(0.05)
            self.xia.erase_start.put(1)
            ttime.sleep(2)
            ax.cla()

            # For each channel:
            for j in channels:
                # If checkbox of current channel is checked:
                if getattr(self, "checkBox_gm_ch{}".format(j + 1)).checkState() > 0:

                    # Get current channel pre-amp gain:
                    curr_ch_gain = getattr(self.xia, "pre_amp_gain{}".format(j + 1))

                    coeff = self.xia_parser.gain_matching(self.xia, self.edit_center_gain_matching.text(),
                                              self.edit_range_gain_matching.text(), channels[j] + 1, ax)
                    # coeff[0] = Intensity
                    # coeff[1] = Fitted mean
                    # coeff[2] = Sigma

                    diff[j] = float(self.edit_gain_matching_target.text()) - float(coeff[1]*1000)

                    if i != 0:
                        sign = (diff[j] * diff_old[j]) /  math.fabs(diff[j] * diff_old[j])
                        if int(sign) == -1:
                            gain_adjust[j] /= 2
                    print('Chan ' + str(j + 1) + ': ' + str(diff[j]) + '\n')

                    # Update current channel pre-amp gain:
                    curr_ch_gain.put(curr_ch_gain.value - diff[j] * gain_adjust[j])
                    diff_old[j] = diff[j]

                    self.canvas_gain_matching.draw_idle()

    def questionMessage(self, title, question):
        reply = QtGui.QMessageBox.question(self, title,
                question,
                QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            return True
        elif reply == QtGui.QMessageBox.No:
            return False
        else:
            return False

    def show_scan_help(self):
        title = self.run_type.currentText()
        message = self.plan_funcs[self.run_type.currentIndex()].__doc__
        QtGui.QMessageBox.question(self,
                                   'Help! - {}'.format(title),
                                   message,
                                   QtGui.QMessageBox.Ok)

# Class to write terminal output to screen
class EmittingStream(QtCore.QObject):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.buffer = sys.__stdout__.buffer
        self.close = sys.__stdout__.close
        self.closed = sys.__stdout__.closed
        self.detach = sys.__stdout__.detach
        self.encoding = sys.__stdout__.encoding
        self.errors = sys.__stdout__.errors
        self.fileno = sys.__stdout__.fileno
        self.flush = sys.__stdout__.flush
        self.isatty = sys.__stdout__.isatty
        self.line_buffering = sys.__stdout__.line_buffering
        self.mode = sys.__stdout__.mode
        self.name = sys.__stdout__.name
        self.newlines = sys.__stdout__.newlines
        self.read = sys.__stdout__.read
        self.readable = sys.__stdout__.readable
        self.readlines = sys.__stdout__.readlines
        self.seek = sys.__stdout__.seek
        self.seekable = sys.__stdout__.seekable
        #self.softspace = sys.__stdout__.softspace
        self.tell = sys.__stdout__.tell
        self.truncate = sys.__stdout__.truncate
        self.writable = sys.__stdout__.writable
        self.writelines = sys.__stdout__.writelines

    textWritten = QtCore.pyqtSignal(str)

    def write(self, text):
        self.textWritten.emit(str(text))
        # Comment next line if the output should be printed only in the GUI
        sys.__stdout__.write(text)

#    @property
#    def plan(self):
#        lp = LivePlot(self.plot_x,
#                      self.plot_y,
#                      fig=self.fig)

#        @subs_decorator([lp])
#        def scan_gui_plan():
#            return (yield from self.plan_func(self.dets, *self.get_args()))

#def tune_factory(motor):
#    from bluesky.plans import scan
#    from collections import ChainMap

#    def tune(md=None):
#        if md is None:
#            md = {}
#        md = ChainMap(md, {'plan_name': 'tuning {}'.format(motor)})
#        yield from scan(motor, -1, 1, 100, md=md)

#    return tune


import matplotlib.pyplot as plt
import numpy as np
import random
import math

#-------------------------- FONCTIONS ------------------------------------------------

#--------------------- MATRICE DE CORRELATION ----------------------------------------

def se_corr(x, y, lxy):
    """Square exponential correlation kernel or Gaussian kernel in 2D
    x and y   -- vector of coordinates
    lx and ly -- decorrelation lenght scale
    In this example, the decorrelation lenght scale is constant.
    However, in the general case, the decorrelation lenght scale is not constant. It changes from one point to another.
    """
    n = len(x)
    m = len(y)
    C = np.zeros((n,m,n,m,)) #matrice de dimension 4
    two_lxy2 = 2.0*lxy*lxy

    for i in range(n):
        for j in range(m):
            for k in range(n):
                for l in range(m):
                    distx = x[i] - x[k]
                    disty = y[j] - y[l]
                    C[i,j,k,l] = np.exp( -(distx**2 + disty**2)/two_lxy2 )

    return C

#-------------------- NORMALISATION DE LA MATRICE -----------------------------------

#encore a modifier
def make_simple_covariance(C):
    """Make a simple covariance by normalizing a correlation matrix.
    this builds a pseudo covariance that can be used for Gaussian denoising
    """
    n = C.shape[0]

    W = np.zeros_like(C) #matrix similaire a C remplie de 0
    for k in range(n):
        W[k,k] = 1./np.sqrt(sum(C[k,:])) #1/racine carree de la sum de chaque ligne
        pass
    B = np.dot( W, np.dot( C, W ) )
    return B

#------------- INITIALISATION DES VARIABLES --------------------------

#construction des Matrices X et Y
x, y = np.linspace(0,1,10), np.linspace(0,1,10)
dCoordx = x[1]-x[0]
dCoordy = y[1]-y[0]
lxy = 10.0*dCoordx*dCoordy

C = se_corr(x, y, lxy) #obtention de la matrix de correlation
print(C)


from pplugins import blocks
from pplugins.utils import exc_chat, playerPos, mark_pos, marked_pos
from pplugins.placemarks import Placemarks

from mcpi.minecraft import Minecraft

import time
import random

def attempt():
    "_mcp: attempt"
    mc = Minecraft.create()
    while True:
        pos = mc.player.getTilePos()
        mc.postToChat('x={} z={}'.format(pos.x, pos.z))
        time.sleep(1)

def tprand(far=200):
    "_mcp: random teleport"
    mc = Minecraft.create()
    far = int(far)
    x = random.randint(0-far,far)
    z = random.randint(0-far,far)
    y = mc.getHeight(x,z)
    mc.player.setTilePos(x,y,z)

def high():
    """_mcp: set y to world height"""
    mc = Minecraft.create()
    with exc_chat(mc):
        x, y, z = playerPos(mc.player)
        y = mc.getHeight(x, z) + 1
        mc.player.setTilePos(x, y, z)

def up(up=10):
    "_mcp: zoom up"
    mc = Minecraft.create()
    with exc_chat(mc):
        up = int(up)
        x, y, z = playerPos(mc.player)
        y = mc.getHeight(x, z) + 1
        for i in range(up):
            mc.player.setTilePos(x, y+i, z)
            time.sleep(0.005) # zip!

def compass():
    """_mcp: get direction"""
    mc = Minecraft.create()
    with exc_chat(mc):
        d = mc.player.getDirection()
        mc.postToChat('{}'.format(d))

def zoom(steps=100):
    "_mcp: zoom"
    mc = Minecraft.create()
    with exc_chat(mc):
        steps = int(steps)
        d = mc.player.getDirection()
        dx = float(getattr(d,'x'))
        dy = float(getattr(d,'y'))
        dz = float(getattr(d,'z'))
        x, y, z = playerPos(mc.player)
        for n in range(steps):
            i = int(x) + int(dx * n)
            j = int(y) + int(dy * n)
            k = int(z) + int(dz * n)
            mc.player.setTilePos(i, j, k)
            time.sleep(0.005)

def ray(steps=100):
    "_mcp: zoom"
    mc = Minecraft.create()
    with exc_chat(mc):
        steps = int(steps)
        d = mc.player.getDirection()
        dx = float(getattr(d,'x'))
        dy = float(getattr(d,'y'))
        dz = float(getattr(d,'z'))
        x, y, z = playerPos(mc.player)
        for n in range(steps):
            i = int(x) + int(dx * n)
            j = int(y) + int(dy * n) + 1
            k = int(z) + int(dz * n)
            if mc.getBlock(i, j, k) != blocks.AIR:
                mc.setBlock(i, j, k, blocks.TNT)
                mc.setBlock(i, j+1, k, blocks.FIRE)
                return
            mc.setBlock(i, j, k, blocks.TNT)
            mc.getBlock(i, j, k)
            mc.setBlock(i, j, k, blocks.AIR)

def mark(name='mark'):
    "_mcp: mark position for later teleport"
    with exc_chat() as mc:
        x, y, z = playerPos(mc.player)
        Placemarks().add(name, [x,y,z]).save()
        mc.postToChat('marked {},{},{} as {}'.format(x, y, z, name))

def tp(name='mark'):
    "_mcp: teleport to marked location"
    with exc_chat() as mc:
        try:
            x, y, z = Placemarks().location[name]
            mc.player.setTilePos(x, y, z)
            mc.postToChat('teleported to {}'.format(name))
        except KeyError:
            mc.postToChat('no marked location called {}'.format(name))

def tplist():
    "_mcp: list teleport locations by name"
    with exc_chat() as mc:
        msg = ' '.join(sorted(Placemarks().location))
        mc.postToChat('locations: {}'.format(msg))

def tpxyz(x=0,y=0,z=0):
    "_mcp: teleport to x,y,z"
    with exc_chat() as mc:
        x, y, z = int(x), int(y), int(z)
        mc.player.setTilePos(x, y, z)

def tporigin():
    "_mcp: teleport to 0,0,0"
    with exc_chat() as mc:
        mc.player.setTilePos(0, 0, 0)
        mc.postToChat('teleported to origin')


# Lambda Function Code - SPTool_OD_pricing_Download
# Function to download OnDemand pricing, get out the required lines & upload it to S3 as a zipped file
# It will find 'OnDemand' and 'Compute Instance', and write to a file
# Written by natbesh@amazon.com
# Please reachout to costoptimization@amazon.com if there's any comments or suggestions
import boto3
import gzip
import urllib3
import os

def lambda_handler(event, context):

    # Create the connection
    http = urllib3.PoolManager()

    try:
        # Get the EC2 OnDemand pricing file, its huge >1GB
        r = http.request('GET', 'https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/index.csv')

        # Put the response data into a variable & split it into an array line by line
        plaintext_content = r.data
        plaintext_lines = plaintext_content.splitlines()

        # Varaible to hold the OnDemand pricing data
        pricing_output = ""

        # Go through each of the pricing lines to find the ones we need
        for line in plaintext_lines:

            # If the line contains 'OnDemand' or 'Compute Instance' then add it to the output string
            if ((str(line).find('OnDemand') != -1) and (str(line).find('RunInstances') != -1)):
                pricing_output += str(line.decode("utf-8"))
                pricing_output += "\n"

        # Add the output to a local temporary file & zip it
        with gzip.open('/tmp/od_pricedata.txt.gz', 'wb') as f:
            f.write(pricing_output.encode())

        # Upload the zipped file to S3
        s3 = boto3.resource('s3')

        # Specify the local file, the bucket, and the folder and object name - you MUST have a folder and object name
        s3.meta.client.upload_file('/tmp/od_pricedata.txt.gz', os.environ["BUCKET_NAME"], 'Pricing/od_pricedata/od_pricedata.txt.gz')

    # Die if you cant get the pricing file
    except Exception as e:
        print(e)
        raise e

    # Return happy
    return {
        'statusCode': 200
    }


from django.conf.urls import url, include
from django.contrib import admin
from django.core.urlresolvers import reverse_lazy
from django.views.generic.base import RedirectView

from . import views

app_name = 'hori'
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^detail/(?P<client_id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^tasks/(?P<client_id>[0-9]+)/$', views.tasks, name='tasks'),
    url(r'^contacts/(?P<client_id>[0-9]+)/$', views.contacts, name='contacts'),
    url(r'^emergency/(?P<client_id>[0-9]+)/$', views.emergency,
        name='emergency'),
    url(r'^medication/(?P<client_id>[0-9]+)/$', views.medication,
        name='medication'),
    url(r'^manager/$', views.manager, name='manager'),
    url(r'^report/(?P<client_id>[0-9]+)/$', views.test_html_report,
        name='report'),
    url(r'^task_submit/$', views.task_submit, name='task_submit'),
    url(r'^test_pdf/$', views.some_view, name='some_view'),
]


from banal import is_mapping
from urlnormalizer import normalize_url

def make_url(url, params):
    if is_mapping(params):
        params = params.items()
    if params is not None:
        url = normalize_url(url, extra_query_args=params)
    return url


#!/user/bin/env python3
from emailBot import *
from Models  import *
import Models
import time
import os
import atexit
# This script uses the emailBot and Models Model to
# implement a simple imapclient that filters unwanted emals
# added to the folder Created by the script

# Yahoo Mail: No problem as of yet
# Gmail: Needs to turn on less secure app. Working on using Oauth
# Other mails: Not tested yet
def save_state(obj,filename):
	with open(filename,'wb') as file:
		Models.save_instance(obj,file)
		file.close()

if __name__ == '__main__':

	if(os.path.exists("./EmailBot")):
		os.chdir("./EmailBot")
	else:
		os.mkdir("EmailBot")
		os.chdir("EmailBot")

	spam_file = "./Spammers.pickle"
	email_file = "./email.pickle"

	email_list = [] # add email addresses

	# add spam emails or email address or you can just run this script it
	# will add a MailBot folder then move all email you want to filter out into
	# the folder.
	spam_list = []

	Bot = MailBot()
	try:
		if(os.path.exists(email_file)):
			with open(spam_file,'rb') as s_file:
				with open(email_file,'rb') as e_file:
					Bot.spam_list = Models.load_instance(s_file)
					e_List = Models.load_instance(e_file)

					Bot.set_email_list(e_List)
					s_file.close()
					e_file.close()
					# Models.print_list(Bot.spam_list)
					# print((Bot.get_email_list())[0].get_email_addr())
					while(True):
						Bot.login()
						if(Bot.login_flag):
							Bot.add_folder()
							obj = Bot.parse_mailbot() #err
							obj = Bot.parse_mailbot_helper(obj)

							Bot.add_parse_spam(obj)
							Bot.work()
							Bot.work(label='INBOX')

							if(Bot.done):
								Bot.empty_spam()
								Bot.clean_up()
								save_state(Bot.get_email_list(),email_file)
								save_state(Bot.spam_list,spam_file)
								time.sleep(10)
						else:
							print("No work to do!")
							break
			# save_state(Bot)
		else:

			for email in email_list:
				email = Email(email)
				print("Enter password for %s" % email.get_email_addr())
				email.set_pass()

				Bot.add_email(email)

			for spam in spam_list:

				spam = Spam(spam)
				Bot.add_spam(spam)

			while(True):
				Bot.login()
				if(Bot.login_flag):

					Bot.add_folder()
					obj = Bot.parse_mailbot() #err
					obj = Bot.parse_mailbot_helper(obj)

					Bot.add_parse_spam(obj)
					Bot.work()
					Bot.work(label='INBOX')

					if(Bot.done):
						# Bot.empty_spam()
						Bot.clean_up()
						save_state(Bot.get_email_list(),email_file)
						save_state(Bot.spam_list,spam_file)
						time.sleep(10)
				else:
					print("No work to do!")
					break
	except KeyboardInterrupt:
		 atexit.register(save_state,obj = Bot.spam_list, filename = spam_file)
		 atexit.register(save_state,obj = Bot.get_email_list(), filename = email_file)
		 exit(0)