from socket import socket, AF_INET, SOCK_STREAM
from jim_protocol import JIM, JIMPresence, JIMMessage, JIMResponse, OK, JIMAddContact, JIMDelContact, WRONG_REQUEST, FORBIDDEN, GONE, NOT_FOUND, CONFLICT, JIMCreateChat, JIMLeaveChat, JIMJoinChat, CREATED, JIMGetContacts, ACCEPTED, JIMContactList, JIMRespect, ADDED, MESSAGE_SIZE, SENDED, NOT_ADDED, NOT_SENDED, DELETED, JIMInviteChat, INVITED, JIMGetUsers, ACCEPT
from threading import Thread
from queue import Queue
from client_db import ClientDB
from GUI import MainWindow, NotMainWindow
from PyQt5 import QtWidgets, QtCore, QtGui
import sys
import time

class Client:
    def __init__(self, addr, port):
        self.addr = addr
        self.port = port
        self.sckt = socket(AF_INET, SOCK_STREAM)
        self.queue = Queue()
        self.added_queue = Queue()
        self.sended_queue = Queue()
        self.deleted_queue = Queue()
        self.created_queue = Queue()
        self.sended = False
        self.main_window = MainWindow()
        self.receive = Receive(self)
        self.processing = Processing(self)
        self.receive.mysignal_recv.connect(self.show, QtCore.Qt.QueuedConnection)
        self.processing.mysignal_invite.connect(self.send_agreement, QtCore.Qt.QueuedConnection)
        self.contact_model = QtGui.QStandardItemModel()
        self.message_model = QtGui.QStandardItemModel()
        # self.processing.mysignal_contact.connect(self.model_add, QtCore.Qt.QueuedConnection)
        self.processing.mysignal_message.connect(self.message, QtCore.Qt.QueuedConnection)
        self.main_window.ui.pushButton_2.clicked.connect(self.add_contact)
        self.main_window.ui.pushButton.clicked.connect(self.send_message)
        self.contact = None
        # self.processing.mysignal_history.connect(self.add_history, QtCore.Qt.QueuedConnection)
        # self.message_list = []
        # self.message_model.setStringList(self.message_list)
        # self.send_mute = QtCore.QMutex
        self.chat_model = QtGui.QStandardItemModel()
        self.main_window.ui.listView_2.clicked.connect(self.select_contact)
        self.main_window.ui.listView_2.customContextMenuRequested.connect(self.context_menu)
        self.delete_action = QtWidgets.QAction(self.main_window.tr('Удалить'), self.main_window)
        self.delete_action.triggered.connect(self.del_action)
        self.create_chat_action = QtWidgets.QAction(self.main_window.tr('Создать групповую беседу'), self.main_window)
        self.create_chat_action.triggered.connect(self.create_chat)
        self.main_window.ui.listView_3.setModel(self.chat_model)
        self.main_window.ui.listView_3.setWordWrap(True)
        self.main_window.ui.listView_3.customContextMenuRequested.connect(self.second_context_menu)
        self.invite_action = QtWidgets.QAction(self.main_window.tr('Пригласить в чат'), self.main_window)
        self.invite_action.triggered.connect(self.invite_chat)
        self.leave_action = QtWidgets.QAction(self.main_window.tr('Покинуть чат'), self.main_window)
        self.leave_action.triggered.connect(self.leave_chat)
        self.listik = NotMainWindow()
        self.listik.ui.listView_2.setModel(self.contact_model)
        # self.listik.setWindowModality(QtCore.Qt.ApplicationModal)
        self.contact_for_invite = None
        self.listik.ui.pushButton.clicked.connect(self.button_invite)
        self.listik.hide()
        self.chat_name = None
        self.processing.mysignal_users.connect(self.add_chat)
        self.main_window.ui.listView_3.clicked.connect(self.select_chat)

    def button_invite(self):
        self.listik.hide()
        contact = self.listik.ui.listView_2.selectedIndexes()[0]
        self.contact_for_invite = self.contact_model.data(contact)
        self.sckt.send(JIMInviteChat(self.user_name, self.contact_for_invite,
                                     self.chat_name).packing())

    def context_menu(self, pos):
        menu = QtWidgets.QMenu(self.main_window)
        menu.addAction(self.delete_action)
        menu.addAction(self.create_chat_action)
        menu.exec_(self.main_window.ui.listView_2.mapToGlobal(pos))

    def second_context_menu(self, pos):
        menu = QtWidgets.QMenu(self.main_window)
        menu.addAction(self.invite_action)
        menu.addAction(self.leave_action)
        menu.exec_(self.main_window.ui.listView_3.mapToGlobal(pos))

    def leave_chat(self):
        pass

    def invite_chat(self):
        chat = self.main_window.ui.listView_3.selectedIndexes()[0]
        row = chat.row()
        self.chat_name = self.chat_model.data(chat, role=33)
        self.listik.show()

    def del_action(self):
        contact = self.main_window.ui.listView_2.selectedIndexes()[0]
        row = contact.row()
        self.contact = self.contact_model.data(contact)
        print(self.contact)
        msg = JIMDelContact(self.user_name, self.contact)
        self.sckt.send(msg.packing())
        resp = self.deleted_queue.get()
        if resp == "DELETED":
            self.db.del_messages(self.contact)
            self.db.del_contact(msg.contact)
            self.contact_model.removeRow(row)

        else:
            print('Как это!?!')

    def name(self):
        name, ok = QtWidgets.QInputDialog.getText(self.main_window,
                                                  'СКУПЕ', 'Введите имя')
        if ok:
            self.user_name = name
            self.db = ClientDB(self.user_name)

    def send_agreement(self, contact, room):
        print('invited!!!')
        dialog = QtWidgets.QMessageBox.question(self.main_window, 'СКУРЕ',
                                                '{} хочет добавить вас в групповую беседу'.format(contact),
                                                )
        # print(dialog)
        # print(type(dialog))
        if dialog == 16384:
            print('Yes')
            msg = JIMResponse(INVITED, room)
            self.sckt.send(msg.packing())
            icon = 'chat.png'
            chat_item = QtGui.QStandardItem(QtGui.QIcon(icon), '*')
            chat_item.setData(room, role=33)
            self.chat_model.appendRow(chat_item)
            self.db.add_contact(0, room, room)
            self.sckt.send(JIMGetUsers(self.user_name, room).packing())
        elif dialog == 65536:
            print('No')

    def add_contact(self):
        contact = self.main_window.ui.lineEdit_2.text()
        msg = JIMAddContact(self.user_name, contact).packing()
        self.sckt.send(msg)
        print('before resp')
        resp = self.added_queue.get()
        print('after resp')
        if resp == 'ADDED':
            id = self.added_queue.get()
            self.db.add_contact(id, contact, contact)
            self.model_add(contact)
        elif resp == 'NOPE!':
            pass

    def vk(self, contact):
        self.contact = contact

    def select_chat(self, contact):
        self.message_model.clear()
        print(contact)
        name = self.chat_model.data(contact, role=33)
        print(name)
        self.contact = name
        self.get_good_messages()
        self.main_window.ui.listView.setModel(self.message_model)

    def select_contact(self, contact):
        self.message_model.clear()
        print(contact)
        print('123')
        name = self.contact_model.data(contact)
        self.contact = name
        print(self.contact)
        # id = self.db.select_user_id('timmy')
        # print(id)
        # id = self.db.select_user_id(self.receiver)
        self.get_good_messages()
        self.main_window.ui.listView.setModel(self.message_model)
        # lst = []
        # lst.extend(messages)
        # print(lst)
        # self.message_model.setStringList(lst)

        # pass

    def combo_vombo(self, contact):
        pass
        # if self.Right:
        #     print('Right click')
        # else:
        #     print('Left click')

    def message(self, sender, message, receiver, time, receiv):
        if receiver.startswith('<server_chat.Chat object at'):
            new_message = sender + ': ' + message
            self.db.add_mesage(receiver, message, time, receiv)
        else:
            self.db.add_mesage(sender, message, time, receiv)
        # pass

    def get_contacts(self):
        msg = JIMGetContacts(self.user_name).packing()
        self.sckt.send(msg)
        msg = self.sckt.recv(MESSAGE_SIZE)
        msg = JIM.unpacking(msg)
        if msg.code == ACCEPTED:
            for _ in range(msg.quantity):
                msg = self.sckt.recv(MESSAGE_SIZE)
                msg = JIM.unpacking(msg)
                self.db.add_contact(msg.user_id, msg.user_name, msg.user_nickname)
        else:
            self.show(msg)

    # def get_cont_db(self):
    #     self.db.get_contacts()

    def create_chat(self):
        contact = self.main_window.ui.listView_2.selectedIndexes()[0]
        row = contact.row()
        self.contact = self.contact_model.data(contact)
        msg = JIMCreateChat(self.user_name)
        self.sckt.send(msg.packing())
        resp = self.created_queue.get()
        if resp == 'CREATED':
            chat_name = self.created_queue.get()
            icon = 'chat.png'
            chat_item = QtGui.QStandardItem(QtGui.QIcon(icon), '*')
            chat_item.setData(chat_name, role=33)
            self.db.add_contact(None, chat_name, chat_name)
            self.chat_model.appendRow(chat_item)
            self.sckt.send(JIMInviteChat(self.user_name, self.contact, chat_name).packing())

    def add_chat(self, room, user):
        print('add_chat user: ' + str(user))
        print('add_chat room: ' + room)
        if isinstance(user, int):
            new_user = self.db.select_user_name(user)
        else:
            new_user = user
        print(new_user)
        chats = self.get_all_items(self.chat_model)
        my_chat = None
        for chat in chats:
            if chat.data(33) == room:
                my_chat = chat
                break
        # item = QtGui.QStandardItem(user)
        # my_chat.appendRow(item)
        users = my_chat.text()
        print('users of my_chat: ' + users)
        print('new_user for my_chat' + new_user)
        if users == '*':
            my_chat.setText(new_user)
        else:
            new_users = users + '\n' + new_user
            my_chat.setText(new_users)
        # for ch in self.chat_model:
        #     print(ch)
        # item = QtGui.QStandardItem(user)
        # chat.appendRow(item)

    def join_chat(self, room):
        msg = JIMJoinChat(self.user_name, room).packing()
        self.sckt.send(msg)

    def get_all_items(self, model):
        count = 0
        lst = []
        while True:
            print(model.item(count))
            if model.item(count):
                lst.append(model.item(count))
                count += 1
            else:
                break
        return lst

    # def leave_chat(self, room):
    #     msg = JIMLeaveChat(self.user_name, room).packing()
    #     self.sckt.send(msg)

    def send_message(self):
        message = self.main_window.ui.lineEdit.text()
        msg = JIMMessage(self.user_name, self.contact, message)
        self.sckt.send(msg.packing())
        resp = self.sended_queue.get()
        if resp == 'SENDED':
            self.add_message(msg.receiver, msg.message, msg.time, False)
            # print('SENDED')
            # self.db.add_mesage(msg.receiver, msg.message, msg.time, False)
            # print('db.add_message()')
            # good_time = time.localtime(msg.time)
            # print('good_time()')
            # result_time = time.strftime('%d%b%H%M%S', good_time)
            # print('result_time')
            # lst = self.message_model.stringList()
            # print('model list')
            # lst.append(result_time)
            # print('append time')
            # lst.append(msg.message)
            # print('apend msg')
        elif resp == 'NOPE!':
            pass

    def add_message(self, receiver, message, msgtime, not_sender):
        print('SENDED')
        self.db.add_mesage(receiver, message, msgtime, not_sender)
        print('db.add_message()')
        good_time = time.localtime(msgtime)
        print('good_time()')
        result_time = time.strftime('%d %b %H:%M:%S', good_time)
        # result_time = '{} {} {} {} {}'.format(good_time[2], good_time[1], good_time[3], good_time[4], good_time[5])
        item = QtGui.QStandardItem()
        item.setText(result_time)
        item.setSelectable(False)
        item.setEditable(False)
        if not_sender:
            item.setTextAlignment(QtCore.Qt.AlignLeft)
            item.setBackground(QtGui.QColor('#E4FDF2'))
        else:
            item.setTextAlignment(QtCore.Qt.AlignRight)
            item.setBackground(QtGui.QColor('#C5F3FF'))
        self.message_model.appendRow(item)
        item = QtGui.QStandardItem()
        item.setText(message)
        item.setSelectable(False)
        item.setEditable(False)
        if not_sender:
            item.setTextAlignment(QtCore.Qt.AlignLeft)
            item.setBackground(QtGui.QColor('#E4FDF2'))
        else:
            item.setTextAlignment(QtCore.Qt.AlignRight)
            item.setBackground(QtGui.QColor('#C5F3FF'))
        self.message_model.appendRow(item)
        # print('result_time')
        # lst = self.message_model.stringList()
        # print('model list')
        # lst.append(result_time)
        # print('append time')
        # lst.append(message)
        # print('apend msg')
        # self.message_model.setStringList(lst)

    # def processing(self):
    #     pass
        # self.get_contacts()
        # resp = self.queue.get()
        # if resp == 'OK':
        #     self.get_contacts()

            # elif isinstance(response, JIMContactList)

    def send_presence(self, user_name):
        msg = JIMPresence(user_name).packing()
        self.sckt.send(msg)

    def dialog_ok(self):
        self.user_name = self.dialog.ui.lineEdit.text()

    def model_add(self, contact):
        print('Model_add')
        # self.db.add_contact(id, contact, contact)
        icon = 'Icon.png'
        item = QtGui.QStandardItem(QtGui.QIcon(icon), contact)
        self.contact_model.appendRow(item)
        self.contact_model.sort(QtCore.Qt.AscendingOrder)

        # index = self.model.indexFromItem(item)
        # print(index)

    def gui_contacts(self):
        lst = self.db.get_contacts()
        for contact in lst:
            icon = 'Icon.png'
            item = QtGui.QStandardItem(QtGui.QIcon(icon), contact.name)
            self.contact_model.appendRow(item)
        self.main_window.ui.listView_2.setModel(self.contact_model)
        self.contact_model.sort(QtCore.Qt.AscendingOrder)

    def get_good_messages(self):
        messages = self.db.get_messages(self.contact)
        messages.sort(key=self.sort_by_time)
        # all_messages = []
        # all_messages.extend(messages)
        # all_messages.extend(my_messages)
        # all_messages.sort(key=self.sort_by_time)
        result = []
        # print(all_messages)
        for message in messages:
            print(message)
            good_time = time.localtime(message.time)
            result_time = time.strftime('%d %b %H:%M:%S', good_time)
            # result_time = '{} {} {} {} {}'.format(good_time[2], good_time[1], good_time[3], good_time[4], good_time[5])
            # result.append(result_time)
            # result.append(message.message)
            item = QtGui.QStandardItem()
            item.setText(result_time)
            item.setSelectable(False)
            item.setEditable(False)
            if not message.receiver:
                item.setTextAlignment(QtCore.Qt.AlignRight)
                item.setBackground(QtGui.QColor('#C5F3FF'))
                self.message_model.appendRow(item)
                item = QtGui.QStandardItem()
                item.setText(message.message)
                item.setTextAlignment(QtCore.Qt.AlignRight)
                item.setSelectable(False)
                item.setEditable(False)
                item.setBackground(QtGui.QColor('#C5F3FF'))
                self.message_model.appendRow(item)
            else:
            # print(message)
            # good_time = time.localtime(message.time)
            # result_time = time.strftime('%d %b %H:%M:%S', good_time)
            # result_time = '{} {} {} {} {}'.format(good_time[2], good_time[1], good_time[3], good_time[4], good_time[5])
            # result.append(result_time)
            # result.append(message.message)
                item = QtGui.QStandardItem()
                item.setText(result_time)
                item.setTextAlignment(QtCore.Qt.AlignLeft)
                item.setSelectable(False)
                item.setEditable(False)
                item.setBackground(QtGui.QColor('#E4FDF2'))
                self.message_model.appendRow(item)
                item = QtGui.QStandardItem()
                item.setText(message.message)
                item.setTextAlignment(QtCore.Qt.AlignLeft)
                item.setSelectable(False)
                item.setEditable(False)
                item.setBackground(QtGui.QColor('#E4FDF2'))
                self.message_model.appendRow(item)
        # return result
        # pass

    def sort_by_time(self, message):
        return message.time

    def go(self):
        self.name()
        self.main_window.setWindowTitle(self.user_name)
        self.main_window.show()
        self.sckt.connect((self.addr, self.port))
        self.send_presence(self.user_name)
        msg = self.sckt.recv(MESSAGE_SIZE)
        msg = JIM.unpacking(msg)
        self.show(msg)
        # resp = self.queue.get()
        # if resp == 'OK':
        self.db.start()
        #     self.get_contacts()
        self.get_contacts()
        # self.queue.put(JIMResponse(OK))
        self.gui_contacts()
        self.receive.start()
        self.processing.start()
        # t1 = Thread(target=self.processing)
        # t1.start()
        # t2 = Thread(target=self.receive)
        # t2.start()

    def stop(self):
        self.sckt.close()

    def show(self, some_data):
        if isinstance(some_data, JIMResponse):
            if some_data.message and some_data.code != CREATED and some_data.code != INVITED\
                    and some_data.code != ACCEPT:
                # print(some_data.message)
                QtWidgets.QMessageBox.information(self.main_window,
                                                  'СКУРЕ', '{}'.format(some_data.message))
            elif some_data.code == INVITED:
                print(some_data)
                room = some_data.message
                print(some_data.quantity)
                name = self.db.select_user_name(some_data.quantity)
                print('Name = ' + name)
                QtWidgets.QMessageBox.information(self.main_window, 'СКУРЕ',
                                                  '{} присоединился к чату'.format(name))
                self.add_chat(room, name)

class Receive(QtCore.QThread):
    mysignal_recv = QtCore.pyqtSignal(JIM)
    def __init__(self, client, parent=None):
        QtCore.QThread.__init__(self, parent)
        self.client = client
    def run(self):
        while True:
            msg = self.client.sckt.recv(MESSAGE_SIZE)
            msg = JIM.unpacking(msg)
            self.mysignal_recv.emit(msg)
            # self.client.show(msg)
            self.client.queue.put(msg)

class Processing(QtCore.QThread):
    mysignal_invite = QtCore.pyqtSignal(str, str)
    mysignal_message = QtCore.pyqtSignal(str, str,str, int, bool)
    mysignal_users = QtCore.pyqtSignal(str, int)
    def __init__(self, client, parent=None):
        QtCore.QThread.__init__(self, parent)
        self.client = client
    def run(self):
        while True:
            # print('до залипания')

            # НЕ ЗАБЫТЬ ПРО ТАЙМАУТ!!!
            print('before queue')

            response = self.client.queue.get()

            # self.queue.task_done()
            # print('после разлипания')
            if isinstance(response, JIMResponse):
                if response.code == OK:
                    print('resp was OK')
                    # msg = input()
                    # if msg:
                    #     msg = msg.split()
                    #     command = msg[0]
                    #     msg = msg[1:]
                    #     print(str(command))
                    #     print(len(str(command)))
                    #     print(str(msg))
                    #     print(len(str(msg)))
                    #     if len(str(command)) > 15:
                    #         print('Такого пользователя не существует')
                    #         self.client.queue.put(JIMResponse(OK))
                    #         continue
                    #     # print(msg)
                    #     # print(str(msg))
                    #     # print(len(str(msg)))
                    #     if len(str(msg)) > MESSAGE_SIZE - 101:
                    #         print('Слишком длинное сообщение')
                    #         self.client.queue.put(JIMResponse(OK))
                    #         continue
                    #     message = ''
                    #     for i in msg:
                    #         message = message + i + ' '
                    #     message = message[:-1]
                    #     # print(len(message))
                    #     if command == 'add':
                    #         self.client.add_contact(message)
                    #     elif command == 'del':
                    #         print('удаляем')
                    #         self.client.queue.put(JIMResponse(OK))
                    #     elif command == 'get':
                    #         # self.get_contacts()
                    #         contacts = self.client.db.get_contacts()
                    #         for contact in contacts:
                    #             print(contact.nick)
                    #         self.client.queue.put(JIMResponse(OK))
                    #     elif command == 'create':
                    #         self.client.create_chat(message)
                    #     elif command == 'join':
                    #         self.client.join_chat(message)
                    #     elif command == 'leave':
                    #         self.client.leave_chat(message)
                    #     else:
                    #         self.client.send_message(command, message)
                    # else:
                    #     self.client.queue.put(JIMResponse(OK))

                elif response.code == ADDED:
                    # self.client.db.add_contact(response.quantity, response.message, response.message)
                    # self.mysignal_contact.emit(response.quantity, response.message)
                    print("ADDED")
                    self.client.added_queue.put('ADDED')
                    self.client.added_queue.put(response.quantity)
                elif response.code == NOT_ADDED:
                    print('NOT_ADDED')
                    self.client.added_queue.put('NOPE!')
                elif response.code == SENDED:
                    # self.client.db.add_contact(response.quantity, response.message, response.message)
                    # self.mysignal_contact.emit(response.quantity, response.message)
                    print("SENDED")
                    self.client.sended_queue.put('SENDED')
                elif response.code == NOT_SENDED:
                    self.client.sended_queue.put('NOPE!')
                elif response.code == ACCEPTED:
                    for _ in range(response.quantity):
                        contact = self.client.queue.get()
                        self.client.db.add_contact(contact.user_id, contact.user_name, contact.user_nickname)
                    self.client.queue.put(JIMResponse(OK))
                elif response.code == DELETED:
                    self.client.deleted_queue.put('DELETED')
                elif response.code == NOT_FOUND:
                    self.client.queue.put(JIMResponse(OK))
                elif response.code == GONE:
                    self.client.queue.put(JIMResponse(OK))
                elif response.code == CONFLICT:
                    self.client.queue.put(JIMResponse(OK))
                elif response.code == CREATED:
                    self.client.created_queue.put('CREATED')
                    self.client.created_queue.put(response.message)
                elif response.code == ACCEPT:
                    for _ in range(response.quantity):
                        print('первый пошёл')
                        msg = self.client.queue.get()
                        print(msg.message, msg.quantity)
                        self.mysignal_users.emit(msg.message, msg.quantity)

            elif isinstance(response, JIMMessage):
                self.mysignal_message.emit(response.user_name, response.receiver,
                                           response.message, response.time, True)

            elif isinstance(response, JIMInviteChat):
                self.mysignal_invite.emit(response.user_name, response.room)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    client = Client('localhost', 7777)
    client.go()
    sys.exit(app.exec_())


# -*- coding: utf-8 -*-
"""
Created on Mon Jul 24 15:06:26 2017

@author: dori
"""

#from scattDB import shape
#from glob import glob
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#from scattDB import scattering
from scattDB import psd
from scipy import integrate

Br07 = lambda x:0.08900e-3*x**2.1
BF95 = lambda x:0.02414e-3*x**1.9

RHfile = '/home/dori/develop/scatdb/share/scatdb.csv'
dataRH = pd.read_csv(RHfile)
dataRH = dataRH[dataRH.flaketype == 20]
dataRH.set_index('max_dimension_mm',inplace=True)
dataRH.sort_index(inplace=True)

tol = 0.3
dataRHx = dataRH[abs(dataRH.frequencyghz-10.6) < tol]
dataRHu = dataRH[abs(dataRH.frequencyghz-13.6) < tol]
dataRHk = dataRH[abs(dataRH.frequencyghz-23.8) < tol]
dataRHa = dataRH[abs(dataRH.frequencyghz-35.6) < tol]
dataRHw = dataRH[abs(dataRH.frequencyghz-94)   < tol]

#dataRHx = dataRHx.loc[dataRHa.index]
#dataRHu = dataRHu.loc[dataRHa.index]
#dataRHk = dataRHk.loc[dataRHa.index]
#dataRHw = dataRHw.loc[dataRHa.index]

plt.close('all')

c = 299792458000. # mm/s
lamx = c/(9.6*1e9)
lamu = c/(13.4*1e9)
lamk = c/(24.*1e9)
lama = c/(35.6*1e9)
lamw = c/(94.*1e9)
coeffx = lamx**4./(0.93*np.pi**5.)
coeffu = lamu**4./(0.95*np.pi**5.)
coeffk = lamk**4./(0.95*np.pi**5.)
coeffa = lama**4./(0.95*np.pi**5.)
coeffw = lamw**4./(0.75*np.pi**5.)

tablesfolder = 'tables/'
authors = ['DO','BJ','JL']
author = 'DO'
melt = '00'

dataDO = pd.read_csv(tablesfolder+author+'_'+melt+'.csv')
#dataDO.drop('Unnamed: 0',axis=1,inplace=True)
meltDO = pd.read_csv(tablesfolder+author+'_'+'10'+'.csv')
#meltDO.drop('Unnamed: 0',axis=1,inplace=True)

dataDOssrg = pd.read_csv(tablesfolder+author+'_'+'SSRG.csv')
#dataDOssrg.drop('Unnamed: 0',axis=1,inplace=True)

dataJT = pd.read_csv(tablesfolder+'jani_fractals/JT_fractals.csv')
dataJT.drop('Unnamed: 0',axis=1,inplace=True)
dataJT.mkg = Br07(dataJT.Dmax)

author = 'BJ'
dataBJ2 = pd.read_csv(tablesfolder+author+'_agg2_0'+'.csv')
dataBJ2.drop('Unnamed: 0',axis=1,inplace=True)

dataBJ2_10 = pd.read_csv(tablesfolder+author+'_agg2_10'+'.csv')
dataBJ2_10.drop('Unnamed: 0',axis=1,inplace=True)
dataBJ2_20 = pd.read_csv(tablesfolder+author+'_agg2_20'+'.csv')
dataBJ2_20.drop('Unnamed: 0',axis=1,inplace=True)
dataBJ2_30 = pd.read_csv(tablesfolder+author+'_agg2_30'+'.csv')
dataBJ2_30.drop('Unnamed: 0',axis=1,inplace=True)
dataBJ2_40 = pd.read_csv(tablesfolder+author+'_agg2_40'+'.csv')
dataBJ2_40.drop('Unnamed: 0',axis=1,inplace=True)
dataBJ2_49 = pd.read_csv(tablesfolder+author+'_agg2_49'+'.csv')
dataBJ2_49.drop('Unnamed: 0',axis=1,inplace=True)
dataBJ2_70 = pd.read_csv(tablesfolder+author+'_agg2_70'+'.csv')
dataBJ2_70.drop('Unnamed: 0',axis=1,inplace=True)

dataBJ3 = pd.read_csv(tablesfolder+author+'_agg3_0'+'.csv')
dataBJ3.drop('Unnamed: 0',axis=1,inplace=True)

author = 'dataJL_B1.0'
author = 'dataJL_A0.5'
author = 'dataJL_A0.0'
dataJL = pd.read_csv(tablesfolder+author+'.csv')
dataJL['xpolKa'] = 0.0

plt.figure()
ax = plt.gca()
#ax.scatter(dataBJ2.Dmax,dataBJ2.mkg,label='BJ2')
#ax.scatter(dataBJ3.Dmax,dataBJ3.mkg,label='BJ3')
ax.scatter(dataJL.Dmax, dataJL.mkg, label='JL')
ax.scatter(dataDO.Dmax, dataDO.mkg, label='DO')
ax.plot(dataDO.Dmax,Br07(dataDO.Dmax),label='brandes')
ax.plot(dataDO.Dmax,BF95(dataDO.Dmax),label='BF95')
ax.legend()
ax.grid()
ax.set_yscale('log')
ax.set_xscale('log')

dataRH=pd.DataFrame(index=dataRHa.index,columns=dataDO.columns)
dataRH.Dmax = dataRH.index
dataRH.mkg = Br07(dataRH.Dmax)
dataRH.X  = dataRHx.cbk
dataRH.Ku = dataRHu.cbk
dataRH['K']  = dataRHk.cbk
dataRH.Ka = dataRHa.cbk
dataRH.W  = dataRHw.cbk
#%%

def IntPsd(D,s,psd):
    conc = psd(D)
    intZ = np.multiply(conc,s)
    return integrate.trapz(intZ,D)

def myGammaPSD(D0,mu,D_max=100.0):
    return psd.GammaPSD(D0=D0,Nw=1.,mu=mu,D_max=D_max)

def Gauss(mu,sig):
    def F(D):
        x = D-mu
        return np.exp(x*x/(2.0*sig*sig))/(sig*np.sqrt(2.0*np.pi))
    return F

D0s = np.linspace(0.1,20.,30)
mus = [-1.,0.,1.,2.,3.]
#mus = [0.1,0.3,0.5,0.8,1]
marks =[',','+','h','v','.']
colors=['C0','C1','C2','C3','C4']
def f3profile(data,title='title',what=1.0,color=None,ax=None):
    set_newplot = False
    if ax is None:
        set_newplot = True
        plt.figure()
        ax = plt.gca()
    for mu,color in zip(mus,colors):
        Zx, Za, Zw, XKa, KaW, LDR, IWC, MRR = ([] for i in range(8))
        for D0 in D0s:
            conc  = myGammaPSD(D0,mu)
            #conc = Gauss(D0,mu)
            iwc = IntPsd(data.Dmax,data.mkg,conc)
            if type(what) is float:
                iwc = iwc/what
            IWC.append(iwc)
            zx = 10.*np.log10(coeffx*IntPsd(data.Dmax,data.X,conc)/iwc)
            zu = 10.*np.log10(coeffu*IntPsd(data.Dmax,data.Ku,conc)/iwc)
            if 'K' in data.columns:
                mrr = 10.*np.log10(coeffk*IntPsd(data.Dmax,data.K,conc)/iwc)
            else:
                mrr = np.nan
            if np.isnan(zx):
                zx = zu
            Zx.append(zx)
            MRR.append(mrr)
            zalin = IntPsd(data.Dmax,data.Ka,conc)
            ldrka = IntPsd(data.Dmax,data.xpolKa,conc)/zalin
            Za.append(10.*np.log10(coeffa*zalin/iwc))
            Zw.append(10.*np.log10(coeffw*IntPsd(data.Dmax,data.W,conc)/iwc))
            LDR.append(10.*np.log10(ldrka))
        XKa = np.array(Zx)-np.array(Za)
        KaW = np.array(Za)-np.array(Zw)
        if type(what) is float:
            ax.plot(Zx,D0s,c=color,linestyle='-.')
            ax.plot(Za,D0s,c=color,linestyle='-',label='$\mu$='+str(mu))
            ax.plot(Zw,D0s,c=color,linestyle=':')
            ax.plot(MRR,D0s,c=color,linestyle='--')
            ax.set_xlabel('Z    [dBZ]')
        elif what=='DWR':
            ax.plot(XKa,D0s,c=color,linestyle='-.')
            ax.plot(KaW,D0s,c=color,linestyle='-',label='$\mu$='+str(mu))
            ax.set_xlabel('DWR     [dBZ]')
        elif what=='LDR':
            ax.plot(LDR,D0s,c=color,linestyle='-.',label='$\mu$='+str(mu))
            ax.set_xlim([-40,-15])
            ax.set_xlabel('LDR     [dB]')
        ax.invert_yaxis()
        ax.set_ylabel('D$_0$')
        ax.grid()
        ax.legend()
        ax.set_title(title)
    return ax

def f3plot(data,title='title',color=None,ax=None):
    set_colorbar = False
    if ax is None:
        set_colorbar = True
        plt.figure()
        ax = plt.gca()
    for mu,m in zip(mus,marks):
        Zx, Za, Zw, XKa, KaW, LDR, IWC = ([] for i in range(7))
        for D0 in D0s:
            conc  = myGammaPSD(D0,mu)
            #conc = Gauss(D0,mu)
            iwc = IntPsd(data.Dmax,data.mkg,conc)
            IWC.append(iwc)
            zx = 10.*np.log10(coeffx*IntPsd(data.Dmax,data.X,conc)/iwc)
            zu = 10.*np.log10(coeffu*IntPsd(data.Dmax,data.Ku,conc)/iwc)

            if np.isnan(zx):
                zx = zu
            Zx.append(zx)
            zalin = IntPsd(data.Dmax,data.Ka,conc)
            ldrka = IntPsd(data.Dmax,data.xpolKa,conc)/zalin
            Za.append(10.*np.log10(coeffa*zalin/iwc))
            Zw.append(10.*np.log10(coeffw*IntPsd(data.Dmax,data.W,conc)/iwc))
            LDR.append(10.*np.log10(ldrka))
        XKa = np.array(Zx)-np.array(Za)
        KaW = np.array(Za)-np.array(Zw)

        if color is None:
            colorv = D0s
            vmin=0
            vmax=20
            cbarlabel = '$D_0$'
        elif color == 'ldr':
            colorv = LDR
            vmin = -35
            vmax = -15
            cbarlabel = 'LDR$_Ka$'
        else:
            cbarlabel = color + '/iwc    [dB/(g/m$^3$)]'
            if color == 'Zx':
                colorv = Zx
                print(max(Zx))
                vmin = 20
                vmax = 40
            elif color == 'Zka':
                colorv = Za
                print(max(Za))
                vmin = 10
                vmax = 30
            elif color == 'Zw':
                colorv = Zw
                print(max(Zw))
                vmin = 0
                vmax = 20

        s = ax.scatter(KaW,XKa,c=colorv,marker=m,label='$\mu=$ '+str(mu),vmin=vmin,vmax=vmax,cmap='jet')
    ax.grid()
    ax.set_xlabel('DWR$_{Ka,W}$')
    ax.set_ylabel('DWR$_{X,Ka}$')
    ax.set_xlim([0,15])
    ax.set_ylim([0,22])
    if set_colorbar:
        colorbar = plt.colorbar(mappable=s,ax=ax)
        colorbar.set_label(cbarlabel)
        ax.legend()
    ax.set_title(title)
    return ax

#f3plot(dataDOssrg,'Dave SSRG')
#f3plot(dataDO,'Davide dry')
#f3plot(dataDO,'Davide dry',color='Zx')
#f3plot(dataBJ2,'BJ2 dry',color='Zx')
#
#dataJL.columns = ['Dmax','model','ELWP','mkg','Dmax.1','Rgyr','ldr','riming', 'Xa','Xs', 'X', 'Xe', 'Ua', 'Us', 'Ku', 'Ue', 'Aa', 'As', 'Ka', 'Ae', 'Wa','Ws', 'W', 'We','xpolKa']
#dataJL.X  = dataJL.X*1.0e6
#dataJL.Ku = dataJL.Ku*1.0e6
#dataJL.Ka = dataJL.Ka*1.0e6
#dataJL.W  = dataJL.W*1.0e6
#f3plot(dataJL,'Jussi rimed A0.5',color='Zx')
#f3plot(dataJL,'Jussi rimed A0.5')
#
#f3plot(dataDO,'Davide dry',color='Zka')
#f3plot(dataJL,'Jussi rimed A0.5',color='Zka')
#
#f3plot(dataDO,'Davide dry',color='Zw')
#f3plot(dataJL,'Jussi rimed A0.5',color='Zw')
#
#ax = f3plot(dataDO,'Davide dry',color='ldr')
#f3plot(meltDO,'Davide 0 - 10 %',color='ldr',ax=ax)
#ax.grid()
#
#f3plot(dataJT,'JT fractals')
#f3plot(dataJT,'JT fractals',color='ldr')
#f3profile(dataJT,title='JT fractals',what='LDR')
#f3profile(dataJT,title='JT fractals')
#f3profile(dataJT,title='JT fractals',what='DWR')
#
#ax = f3plot(dataBJ2,'BJ2 dry',color='ldr')
#f3plot(dataBJ2_10,'BJ2 10%',color='ldr',ax=ax)
#f3plot(dataBJ2_20,'BJ2 20%',color='ldr',ax=ax)
#f3plot(dataBJ2_30,'BJ2 30%',color='ldr',ax=ax)
#f3plot(dataBJ2_40,'BJ2 40%',color='ldr',ax=ax)
#f3plot(dataBJ2_49,'BJ2 49%',color='ldr',ax=ax)
#f3plot(dataBJ2_70,'BJ2 0 - 70%',color='ldr',ax=ax)
#ax.grid()
#
#
#f3profile(dataJL,title='Jussi unrimed')
#f3profile(dataJL,title='Jussi unrimed 0.01 kg/m$^2$',what=0.01)
#f3profile(dataJL,title='Jussi unrimed',what='DWR')
#
#f3profile(dataDO,title='Davide dry')
#f3profile(dataDO,title='Davide dry 0.01 kg/m$^2$',what=0.01)
#f3profile(dataDO,title='Davide dry',what='DWR')
#f3profile(dataDO,title='Davide dry',what='LDR')
#f3profile(dataDOssrg,title='Dave SSRG 0.01 kg/m$^2$',what=0.01)
#f3profile(dataDOssrg,title='Dave SSRG',what='DWR')
#
#f3profile(dataRH,title='RH spherical')
#f3profile(dataRH,title='RH spherical',what='DWR')
#f3plot(dataRH,'RH spherical')

D0s = [10.,15.,20.]
marks=['--','-','.']
mus = [0.0,4.0]
Dmax = np.linspace(10,50,100)
data = 1.0*dataDOssrg
f,((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2,sharex=True)

for D0,m in zip(D0s,marks):
    for mu in mus:
        Zx, Za, Zw, XKa, KaW, LDR, IWC, MRR = ([] for i in range(8))
        for Dx in Dmax:
            conc  = myGammaPSD(D0,mu,D_max=Dx)
            iwc = IntPsd(data.Dmax,data.mkg,conc)
            IWC.append(iwc)
            zx = 10.*np.log10(coeffx*IntPsd(data.Dmax,data.X,conc)/iwc)
            zu = 10.*np.log10(coeffu*IntPsd(data.Dmax,data.Ku,conc)/iwc)

            if np.isnan(zx):
                zx = zu
            Zx.append(zx)
            zalin = IntPsd(data.Dmax,data.Ka,conc)
            ldrka = IntPsd(data.Dmax,data.xpolKa,conc)/zalin
            Za.append(10.*np.log10(coeffa*zalin/iwc))
            Zw.append(10.*np.log10(coeffw*IntPsd(data.Dmax,data.W,conc)/iwc))
            LDR.append(10.*np.log10(ldrka))
        XKa = np.array(Zx)-np.array(Za)
        KaW = np.array(Za)-np.array(Zw)
        ax1.plot(Dmax,Zx,m,label='D$_0$='+str(D0)+' $\mu$='+str(mu))
        ax2.plot(Dmax,Za,m,label='D$_0$='+str(D0)+' $\mu$='+str(mu))
        ax3.plot(Dmax,Zw,m,label='D$_0$='+str(D0)+' $\mu$='+str(mu))
        ax4.plot(Dmax,KaW,m,label='D$_0$='+str(D0)+' $\mu$='+str(mu))
ax1.legend()
ax1.grid()
ax1.set_ylabel('Zx/iwc')
ax2.grid()
ax2.set_ylabel('Za/iwc')
ax3.grid()
ax3.set_ylabel('Zw/iwc')
ax3.set_xlabel('Truncation size')
ax4.grid()
ax4.set_ylabel('DWR Ka-W')
ax4.set_xlabel('Truncation size')


import argparse
import os

from ibis_benchmark.chart import gen_chart
from ibis_benchmark.nyc_taxi import config
from ibis_benchmark.utils import register_log

def main():
    parser = argparse.ArgumentParser()

    group_backend = parser.add_mutually_exclusive_group(required=True)

    group_backend.add_argument(
        '--omniscidb-cpu',
        dest='is_omniscidb_cpu',
        action='store_true',
        help='Set OmniSciDB CPU as the default backend.',
    )

    group_backend.add_argument(
        '--omniscidb-cuda',
        dest='is_omniscidb_cuda',
        action='store_true',
        help='Set OmniSciDB CUDA as the default backend.',
    )

    group_backend.add_argument(
        '--pandas',
        dest='is_pandas',
        action='store_true',
        help='Set pandas as the default backend.',
    )

    group_backend.add_argument(
        '--reset-results',
        dest='reset_results',
        action='store_true',
        help='Reset previous result.',
    )

    ns = parser.parse_args()
    runpy_file = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), 'run.py'
    )

    if ns.reset_results:
        # start a new benchmark log
        register_log(config.log_path, new_log=True)
        return
    elif ns.is_omniscidb_cpu:
        os.system('python {} --omniscidb --cpu --cursor'.format(runpy_file))
        os.system('python {} --omniscidb --cpu --ipc'.format(runpy_file))
    elif ns.is_omniscidb_cuda:
        os.system('python {} --omniscidb --cuda --ipc'.format(runpy_file))
    elif ns.is_pandas:
        os.system('python {} --pandas'.format(runpy_file))

    # generate a comparison chart
    gen_chart('nyc-taxi')

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-

import logging

from pyramid.events import BeforeRender
from pyramid.events import subscriber
from pyramid.renderers import get_renderer

from amnesia import helpers

log = logging.getLogger(__name__)  # pylint: disable=invalid-name

def includeme(config):
    config.scan(__name__)

@subscriber(BeforeRender)
def add_renderers_global(event):
    registry = event['request'].registry
    layout = get_renderer('templates/layout.pt').implementation()

    event.update({
        'h': helpers,
        'widgets': registry['widgets'],
        'layout': layout
    })


import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F

class AttantionLSTMClassifier(nn.Module):
	def __init__(self, batch_size, output_size, hidden_size, vocab_size, embedding_length, num_layers, weights):
		super(AttantionLSTMClassifier, self).__init__()

		"""
		Arguments
		---------
		batch_size : Size of the batch which is same as the batch_size of the data returned by the TorchText BucketIterator
		output_size : 2 = (pos, neg)
		hidden_sie : Size of the hidden_state of the LSTM
		vocab_size : Size of the vocabulary containing unique words
		embedding_length : Embeddding dimension of GloVe word embeddings
		weights : Pre-trained GloVe word_embeddings which we will use to create our word_embedding look-up table

		"""
		self.hidden_size = hidden_size
		self.batch_size = batch_size
		self.num_layers = num_layers

		self.word_embeddings = nn.Embedding(vocab_size, embedding_length)# Initializing the look-up table.
		self.word_embeddings.weight = nn.Parameter(weights, requires_grad=False) # Assigning the look-up table to the pre-trained GloVe word embedding.
		self.lstm = nn.LSTM(embedding_length, hidden_size, num_layers, batch_first = True)

		self.label = nn.Linear(hidden_size, output_size) #hidden_size*2 for bidirect ### //2 for maxpool

	def attention_net(self, lstm_output, final_state):

		"""
		Now we will incorporate Attention mechanism in our LSTM model. In this new model, we will use attention to compute soft alignment score corresponding
		between each of the hidden_state and the last hidden_state of the LSTM. We will be using torch.bmm for the batch matrix multiplication.

		Arguments
		---------

		lstm_output : Final output of the LSTM which contains hidden layer outputs for each sequence.
		final_state : Final time-step hidden state (h_n) of the LSTM

		---------

		Returns : It performs attention mechanism by first computing weights for each of the sequence present in lstm_output and and then finally computing the
				  new hidden state.

		Tensor Size :
					hidden.size() = (batch_size, hidden_size)
					attn_weights.size() = (batch_size, num_seq)
					soft_attn_weights.size() = (batch_size, num_seq)
					new_hidden_state.size() = (batch_size, hidden_size)

		"""

		hidden = final_state.squeeze(0)
		attn_weights = torch.bmm(lstm_output, hidden.unsqueeze(2)).squeeze(2)
		soft_attn_weights = F.softmax(attn_weights, 1)
		new_hidden_state = torch.bmm(lstm_output.transpose(1, 2), soft_attn_weights.unsqueeze(2)).squeeze(2)

		return new_hidden_state

	def forward(self, input_sentence, batch_size=None):

		"""
		Parameters
		----------
		input_sentence: input_sentence of shape = (batch_size, num_sequences)
		batch_size : default = None. Used only for prediction on a single sentence after training (batch_size = 1)

		Returns
		-------
		Output of the linear layer containing logits for positive & negative class which receives its input as the final_hidden_state of the LSTM
		final_output.shape = (batch_size, output_size)

		"""

		''' Here we will map all the indexes present in the input sequence to the corresponding word vector using our pre-trained word_embedddins.'''
		input = self.word_embeddings(input_sentence) # embedded input of shape = (batch_size, num_sequences,  embedding_length)
		input = input.permute(1, 0, 2) # input.size() = (num_sequences, batch_size, embedding_length)
		#the trick with batch_size is for making ability to use another batch_size (for test (evalueting) model)
		if batch_size is None:
		  h_0 = Variable(torch.zeros(self.num_layers, self.batch_size, self.hidden_size).cuda()) # Initial hidden state of the LSTM, num_layers*2 for biderection
		  c_0 = Variable(torch.zeros(self.num_layers, self.batch_size, self.hidden_size).cuda()) # Initial cell state of the LSTM, num_layers*2 for biderection
		else:
			h_0 = Variable(torch.zeros(self.num_layers, batch_size, self.hidden_size).cuda())
			c_0 = Variable(torch.zeros(self.num_layers, batch_size, self.hidden_size).cuda())
		output, (final_hidden_state, final_cell_state) = self.lstm(input, (h_0, c_0))

		attn_output = self.attention_net(output, final_hidden_state)
		final_output = self.label(attn_output)

		return final_output


import numpy as np
import argparse
import matplotlib.pyplot as plt
import os
from tqdm import tqdm_notebook as tqdm
from matplotlib.font_manager import FontProperties

def get_color(counter):
    counter += 1
    return 'C'+str(counter), counter

def smooth(x, N=30):
    cumsum = np.cumsum(np.insert(x, 0, 0))
    return (cumsum[N:] - cumsum[:-N]) / float(N)

def save_file(data_mean, file_name):
    with open(file_name,'w') as f:
        for i in range(len(data_mean)):
            f.write("%d\n" % data_mean[i])

# def plot_data(env_type, data_mean, data_std,title, color,label='',y_label=''):
#     data_x = range(len(smooth(data_mean)))
#     data_y_plus = smooth(data_mean+data_std)
#     data_y_minus = smooth(data_mean-data_std)
#
#     # plt.rcParams["figure.figsize"] = (30,20)
#     plt.plot(smooth(data_mean),color=color, label=label)
#     plt.fill_between(data_x,data_y_plus,data_y_minus,color=color,alpha=0.2)
#     plt.xlabel('Episodes ['+ env_type + ']')
#     plt.ylabel(y_label)
#     plt.title(title)
#     plt.grid()
#     # from matplotlib.font_manager import FontProperties
#     #
#     fontP = FontProperties()
#     fontP.set_size('x-small')
#     # plt.legend(bbox_to_anchor=(1.04, 0.5), loc="center left", borderaxespad=0, prop=fontP)
#     # plt.tight_layout(rect=[0, 0, 0.75, 1])
#
#     plt.legend(shadow=True)

def plot_data(env_type, data_mean, data_std, title, color, label='', y_label=''):
    data_x = range(len(smooth(data_mean)))
    data_y_plus = smooth(data_mean + data_std)
    data_y_minus = smooth(data_mean - data_std)

    # plt.rcParams["figure.figsize"] = (10,10)
    plt.plot(smooth(data_mean), color=color, label=label)
    plt.fill_between(data_x, data_y_plus, data_y_minus, color=color, alpha=0.2)
    plt.xlabel('Episodes [' + env_type + ']')
    plt.ylabel(y_label)
    plt.title(title)
    plt.grid()

    fontP = FontProperties()
    fontP.set_size('x-small')
    # plt.legend(bbox_to_anchor=(1.04, 0.5), loc="center left", borderaxespad=0, prop=fontP)
    # plt.tight_layout(rect=[0, 0, 0.75, 1])

    plt.legend(shadow=True, prop=fontP)
    # plt.tight_layout()
    # plt.subplots_adjust(right=0.75)

def read_files(path, env, replay):
    print("loading files")
    files = os.listdir(path)

    data = []

    for fd_name in tqdm(files):
        if ('.npy' in fd_name) and (env in fd_name) and (replay in fd_name):
            data.append(np.load(path + "/" + fd_name))

    data_m = np.mean(data, axis=0)
    data_std = np.std(data, axis=0)

    return data_m, data_std

envs = ["MountainCar", "CartPole", "LunarLander"]
replays = ["rank", "Combined", "Naive"]
label_dict = {"rank":"APER-rank", "Combined":"ACER", "Naive":"ANER"}
parent_path = "../adaptive_buffer/results"

counter = 0
for env in envs:

    for r, replay in enumerate(replays):
        mean, std = read_files(parent_path, env, replay)

        # get color
        color, counter = get_color(counter)
        plot_data(env, mean, std, 'Buffer per episode', color, label=label_dict[replay], y_label='Buffer Size')

    counter = 0

    plt.savefig(parent_path + f'/{env}_buffer.png')
    plt.show()


from flask import render_template, flash, redirect, url_for
from app import app
from app.forms import LoginForm
from flask_login import current_user, login_user
from app.models import User, Item
from flask_login import logout_user
from flask_login import login_required
from flask import request, jsonify, Blueprint
from werkzeug.urls import url_parse
from app import db
from app.forms import RegistrationForm, AddItemForm, EditItemForm
from app.mod_tables.table_models import TableBuilder

@app.route('/')
@app.route('/index')
@login_required
def index():
    return render_template('index.html', title='Home')

@app.route('/table')
@login_required
def table():
    #items=Item.query.all()

    page = request.args.get('page', 1, type=int)

    items = Item.query.paginate(page, app.config['ITEMS_PER_PAGE'], False)
    next_url = url_for('table', page=items.next_num) \
        if items.has_next else None
    prev_url = url_for('table', page=items.prev_num) \
        if items.has_prev else None

    return render_template('table.html', title='db table', items=items.items,
                            next_url=next_url, prev_url=prev_url)

@app.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash('Invalid username or password')
            return redirect(url_for('login'))
        login_user(user, remember=form.remember_me.data)
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('index')
        return redirect(next_page)
    return render_template('login.html', title='Sign In', form=form)

@app.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('index'))

@app.route('/register', methods=['GET', 'POST'])
def register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(username=form.username.data, email=form.email.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash('Congratulations, you are now a registered user!')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)

@app.route('/additem', methods=['GET', 'POST'])
@login_required
def additem():
    form = AddItemForm()
    if form.validate_on_submit():
        item = Item(producer=form.producer.data,
                    model_name=form.model_name.data,
                    gender=form.gender.data,
                    size=form.size.data,
                    amount=form.amount.data,
                    price=form.price.data)
        db.session.add(item)
        db.session.commit()
        flash('A new item has been added to the DB')
        return redirect(url_for('table'))
    return render_template('additem.html', title='Add Item', form = form)

@app.route('/<int:id>/delete', methods=['POST'])
@login_required
def delete_item(id):
    qry = db.session.query(Item).filter(Item.id==id)
    del_item = qry.first()
    flash(f'The item {del_item.producer} {del_item.model_name} has been deleted from the DB')
    db.session.delete(del_item)
    db.session.commit()
    return redirect(url_for('table'))

@app.route('/<int:id>/edit', methods=['GET', 'POST'])
@login_required
def edit(id):
    qry = db.session.query(Item).filter(Item.id==id)
    edited_item = qry.first()
    return redirect(url_for('editing_item', id=id))

@app.route('/edit_item/<int:id>', methods=['GET', 'POST'])
@login_required
def editing_item(id):
    form = EditItemForm()
    qry = db.session.query(Item).filter(Item.id==id)
    item = qry.first()
    if form.validate_on_submit():
        item.producer = form.producer.data
        item.model_name = form.model_name.data
        item.gender = form.gender.data
        item.size = form.size.data
        item.amount = form.amount.data
        item.price = form.price.data
        db.session.commit()
        flash('Item details have been updated')

        return redirect(url_for('table'))
    return render_template('edit_item.html', title='Edit Item', form = form,
                            edited_item = item)

tables = Blueprint('tables', __name__, url_prefix='/tables')

table_builder = TableBuilder()

@tables.route("/test_table", methods=['GET'])
def serverside_table_content():
    data = table_builder.collect_data_serverside(request)
    return jsonify(data)

@app.route("/test_table")
def test_table():
	return render_template('test_table.html')


from imutils.video import VideoStream
from imutils import face_utils
from scipy.spatial import distance
from threading import Thread
import pyglet
import numpy as np
import imutils
import time
import dlib
import cv2
import pygame
import argparse
import paho.mqtt.client as mqtt
import pymysql
import sys

model = '/home/pi/opencv/DNN/res10_300x300_ssd_iter_140000_fp16.caffemodel'
config = '/home/pi/opencv/DNN/deploy.prototxt'

def light_removing(frame):
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
    L = lab[:,:,0]
    med_L = cv2.medianBlur(L,99)
    invert_L = cv2.bitwise_not(med_L)
    composed = cv2.addWeighted(gray, 0.75, invert_L, 0.25, 0)
    return L, composed

def select_alarm(result):
    if result == 1:
        sound_alarm("ALARM.wav")

def sound_alarm(path):
    pygame.mixer.init()
    pygame.mixer.music.load(path)
    pygame.mixer.music.play()

def eye_aspect_ratio(eye):
    A = distance.euclidean(eye[1], eye[5])
    B = distance.euclidean(eye[2], eye[4])
    C = distance.euclidean(eye[0], eye[3])
    ear = (A + B) / (2.0 * C)
    return ear

thresh = 0.3
frame_check = 10

ALARM_ON = False

print("[INFO] loading facial landmark predictor...")
detector = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

print("[INFO] starting video stream thread...")

cap = cv2.VideoCapture(-1)
flag=0
time.sleep(1.0)

if not cap.isOpened():
    print('Camera open failed!')
    sys.exit()

net = cv2.dnn.readNet(model, config)

if net.empty():
    print('Net open failed!')
    sys.exit()

while True:
    ret, frame = cap.read()

    if not ret:
        break

    frame = imutils.resize(frame, width=450)
    L, gray = light_removing(frame)

    rects = detector.detectMultiScale(gray, scaleFactor=1.1,minNeighbors=5, minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)

    if len(rects) <= 0:
        blob = cv2.dnn.blobFromImage(frame,1,(300,300),(104,177,123))
        net.setInput(blob)
        out = net.forward()
        detect = out[0,0, :, :]
        (hei, wei) = frame.shape[:2]

        for i in range(detect.shape[0]):
            confidence = detect[i, 2]
            if confidence < 0.5:
                break
            # face detect fail -> timer stop -> sleep
            # timer stop code

            # face detect success -> timer restart -> studying
            # timer start code
            x1 = int(detect[i, 3] * wei)
            y1 = int(detect[i, 4] * hei)
            x2 = int(detect[i, 5] * wei)
            y2 = int(detect[i, 6] * hei)
            cv2.rectangle(frame, (x1,y1),(x2,y2),(0,0,255))

            label = f'Face: {confidence:4.2f}'
            cv2.putText(frame, label, (x1, y1 - 1), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,0,255),1,cv2.LINE_AA)

    # face detect success -> timer restart -> studying
    # timer start code
    for (x, y, w, h) in rects:

        rect = dlib.rectangle(int(x), int(y), int(x+w), int(y+h))
        cv2.rectangle(frame, (x,y),(x+w,y+h),(0,255,0))
        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)

        leftEye = shape[lStart:lEnd]
        rightEye = shape[rStart:rEnd]
        leftEAR = eye_aspect_ratio(leftEye)
        rightEAR = eye_aspect_ratio(rightEye)

        ear = (leftEAR + rightEAR) / 2.0

        leftEyeHull = cv2.convexHull(leftEye)
        rightEyeHull = cv2.convexHull(rightEye)
        cv2.drawContours(frame, [leftEyeHull], -1, (0,255,0), 1)
        cv2.drawContours(
            frame, [rightEyeHull], -1, (0,255,0), 1)
        if ear < thresh:
            flag += 1
            print(flag)

            if flag >= frame_check:

                t = Thread(target = select_alarm, args = (1, ))
                t.deamon =True
                t.start()

                cv2.putText(frame, "**ALERT!**", (10,30), cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255), 2)

        else:
            flag = 0
            ALARM_ON = False

    cv2.imshow('frame',frame)

    if cv2.waitKey(1) == 27:
        break
cv2.destroyAllWindows()


#!/usr/bin/env python

#**************************************************************************************
#**************************************************************************************
#  nidm_query.py
#  License: GPL
#**************************************************************************************
#**************************************************************************************
# Date: 8-1-18                 Coded by: David Keator (dbkeator@gmail.com)
# Filename: nidm_query.py
#
# Program description:  This program provides query functionalty for NIDM-Experiment files
#
#
#**************************************************************************************
# Development environment: Python - PyCharm IDE
#
#**************************************************************************************
# System requirements:  Python 3.X
# Libraries: os, sys, rdflib, pandas, argparse, logging
#**************************************************************************************
# Start date: 8-1-18
# Update history:
# DATE            MODIFICATION				Who
#
#
#**************************************************************************************
# Programmer comments:
#
#
#**************************************************************************************
#**************************************************************************************

import os, sys
from rdflib import Graph, util
import pandas as pd
from argparse import ArgumentParser
import logging
from nidm.experiment.Query import query_nidm

def query(nidm_file_list,query_file, output_file):

    #query result list
    results = []

    #read query from text fiile
    with open(query_file, 'r') as fp:
        query = fp.read()

    df = query_nidm(nidm_file_list,query,output_file)

    return df

def main(argv):
    parser = ArgumentParser(description='This program provides query support for NIDM-Experiment files')

    parser.add_argument('-query', '--nidm', dest='query_file', required=True, help="Text file containing a SPARQL query to execute")
    parser.add_argument('-nidm-list', '--nidm-list', dest='nidm_list', type=str,  required=True, help='A comma separated list of NIDM files with full path ')
    parser.add_argument('-o', '--o', dest='output_file', default=None, required=False, help="Optional output file (CSV) to store results of query")

    args = parser.parse_args()

    nidm_file_list= [str(item) for item in args.nidm_list.split(',')]

    #execute query
    query_result = query(nidm_file_list,args.query_file, args.output_file)

if __name__ == "__main__":
    main(sys.argv[1:])


import torch
import torchvision
import PIL
import matplotlib.pyplot as plt
import pandas as pd
from tqdm import tqdm
import os
import sys
import shutil
import tempfile
import numpy as np

from torch.utils.data import Dataset
from PIL import Image
from typing import Dict, Optional, Tuple, List

import torchvision.transforms.functional as TF

from dataclasses import dataclass
from typing import Tuple
import re

# Each sample we downloaded can be identified by the name of the city as well as a frame and sequence id
@dataclass
class CityscapesSample:
    city: str
    seq_id: str
    frame_id: str

    @property
    def id(self):
        return os.path.join(self.city, "_".join([self.city, self.seq_id, self.frame_id]))

    @staticmethod
    def from_filename(filename: str):
        # Create a CityscapesSample from a filename, which has a fixed structure {city}_{sequence}_{frame}
        match = re.match(r"^(\w+)_(\d+)_(\d+).*.png$", filename, re.I)
        return CityscapesSample(match.group(1), match.group(2), match.group(3))

# Each class that we aim to detect is assigned a name, id and color.
@dataclass
class CityscapesClass:
    name: str
    id: int
    color: Tuple[int, int, int]

# List of classes that we want to detect in the input
classes = [
    CityscapesClass('void', 0, (0, 0, 0)),
    CityscapesClass('dynamic', 1, (111, 74, 0)),
    CityscapesClass('ground', 2, (81, 0, 81)),
    CityscapesClass('road', 3, (128, 64, 128)),
    CityscapesClass('sidewalk', 4, (244, 35, 232)),
    CityscapesClass('parking', 5, (250, 170, 160)),
    CityscapesClass('rail track', 6, (230, 150, 140)),
    CityscapesClass('building', 7, (70, 70, 70)),
    CityscapesClass('wall', 8, (102, 102, 156)),
    CityscapesClass('fence', 9, (190, 153, 153)),
    CityscapesClass('guard rail', 10, (180, 165, 180)),
    CityscapesClass('bridge', 11, (150, 100, 100)),
    CityscapesClass('tunnel', 12, (150, 120, 90)),
    CityscapesClass('pole', 13, (153, 153, 153)),
    CityscapesClass('polegroup', 14, (153, 153, 153)),
    CityscapesClass('traffic light', 15, (250, 170, 30)),
    CityscapesClass('traffic sign', 16, (220, 220, 0)),
    CityscapesClass('vegetation', 17, (107, 142, 35)),
    CityscapesClass('terrain', 18, (152, 251, 152)),
    CityscapesClass('sky', 19, (70, 130, 180)),
    CityscapesClass('person', 20, (220, 20, 60)),
    CityscapesClass('rider', 21, (255, 0, 0)),
    CityscapesClass('car', 22, (0, 0, 142)),
    CityscapesClass('truck', 23, (0, 0, 70)),
    CityscapesClass('bus', 24, (0, 60, 100)),
    CityscapesClass('caravan', 25, (0, 0, 90)),
    CityscapesClass('trailer', 26, (0, 0, 110)),
    CityscapesClass('train', 27, (0, 80, 100)),
    CityscapesClass('motorcycle', 28, (0, 0, 230)),
    CityscapesClass('bicycle', 29, (119, 11, 32)),
]

class CityscapesDataset(Dataset):
    # Regular expression matching each PNG file in the dataset
    __read_reg = r"^(\w+)_(\d+)_(\d+).*.png$"

    def __init__(self, dir_input: str, dir_truth: str, sample_size: Tuple[int, int], classes: List[CityscapesSample]):
        super().__init__()

        # These variables are also available as globals, but it is good practice to make classes
        # not depend on global variables.
        self.dir_input = dir_input
        self.dir_truth = dir_truth
        self.sample_size = sample_size
        self.classes = classes

        # Walk through the inputs directory and add each file to our items list
        self.items = []
        for (_, _, filenames) in os.walk(self.dir_input):
            self.items.extend([CityscapesSample.from_filename(f) for f in filenames])

        # Sanity check: do the provided directories contain any samples?
        assert len(self.items) > 0, f"No items found in {self.dir_input}"

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

    def __getitem__(self, i: int) -> (torch.Tensor, torch.Tensor):
        sample = self.items[i]

        input = self.load_input(sample)
        truth = self.load_truth(sample)

        return self.transform(input, truth)

    def load_input(self, sample: CityscapesSample) -> Image:
        path = os.path.join(self.dir_input, f'{sample.id}_leftImg8bit.png')
        return Image.open(path).convert("RGB").resize(self.sample_size, Image.NEAREST)

    def load_truth(self, sample: CityscapesSample) -> Image:
        path = os.path.join(self.dir_truth, f'{sample.id}_gtFine_color.png')
        return Image.open(path).convert("RGB").resize(self.sample_size, Image.NEAREST)

    def transform(self, img: Image.Image, mask: Optional[Image.Image]) -> (torch.Tensor, torch.Tensor):
        ## EXERCISE #####################################################################
        #
        # Data augmentation is a way to improve the accuracy of a model.
        #
        # Once you have a model that works, you can implement some data augmentation
        # techniques here to further improve performance.
        #
        ##################################################################################

        pass

        #################################################################################

        # Convert the image to a tensor
        img = TF.to_tensor(img)

        # If no mask is provided, then return only the image
        if mask is None:
            return img, None

        # Transform the mask from an image with RGB-colors to an 1-channel image with the index of the class as value
        mask_size = [s for s in self.sample_size]
        mask = torch.from_numpy(np.array(mask)).permute((2, 0, 1))
        target = torch.zeros((mask_size[1], mask_size[0]), dtype=torch.uint8)
        for i, c in enumerate(classes):
            eq = mask[0].eq(c.color[0]) & mask[1].eq(c.color[1]) & mask[2].eq(c.color[2])
            target += eq * i

        return img, target

    def masks_to_indices(self, masks: torch.Tensor) -> torch.Tensor:
        _, indices = masks.softmax(dim=1).max(dim=1)
        return indices

    def to_image(self, indices: torch.Tensor) -> Image.Image:
        target = torch.zeros((3, indices.shape[0], indices.shape[1]),
                             dtype=torch.uint8, device=indices.device, requires_grad=False)

        for i, lbl in enumerate(self.classes):
            eq = indices.eq(i)

            target[0] += eq * lbl.color[0]
            target[1] += eq * lbl.color[1]
            target[2] += eq * lbl.color[2]

        return TF.to_pil_image(target.cpu(), 'RGB')


import argparse
import pathlib
import pysuerga

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Static site generator for open source projects')
    parser.add_argument('source_path', help='path to the source directory containing config.yml')
    parser.add_argument('--target-path', default='build', help='directory to write the output')
    parser.add_argument('--base-url', default='', help='base url where the website is served from')
    parser.add_argument('--remove-target', action='store_true',
                        help='remove the content of the target directory')
    args = parser.parse_args()
    pysuerga.main(pathlib.Path(args.source_path),
                  pathlib.Path(args.target_path),
                  args.base_url,
                  args.remove_target)


#! /usr/bin/python
import os
import sys
import itertools
import filecmp

path = sys.argv[1]
names = os.listdir(path)

filenames = [
    name for name in names
    if os.path.isfile(os.path.join(path, name))
]

for filename1, filename2 in itertools.combinations(filenames, 2):
    filepath1 = os.path.join(path, filename1)
    filepath2 = os.path.join(path, filename2)
    if filecmp.cmp(filepath1, filepath2):
        print("{} = {}".format(filepath1, filepath2))


# file: persistence.py

import sqlite3
import atexit

# Data Transfer Objects
class Employee(object):
    def __init__(self, employee_id, name, salary, coffee_stand):
        self.employee_id = employee_id
        self.name = name
        self.salary = salary
        self.coffee_stand = coffee_stand

class Supplier(object):
    def __init__(self, supplier_id, name, contact_information):
        self.supplier_id = supplier_id
        self.name = name
        self.contact_information = contact_information

class Product(object):
    def __init__(self, product_id, description, price, quantity):
        self.product_id = product_id
        self.description = description
        self.price = price
        self.quantity = quantity

class CoffeeStand(object):
    def __init__(self, stand_id, location, number_of_employee):
        self.stand_id = stand_id
        self.location = location
        self.number_of_employees = number_of_employee

class Activity(object):
    def __init__(self, product_id, quantity, activator_id, date):
        self.product_id = product_id
        self.quantity = quantity
        self.activator_id = activator_id
        self.date = date

class ActivityReport(object):
    def __init__(self, date, description, quantity, seller_name, supplier_name):
        self.date = date
        self.description = description
        self.quantity = quantity
        self.supplier_name = supplier_name
        self.seller_name = seller_name

class Report(object):
    def __init__(self, name, salary, location, total):
        self.name = name
        self.salary = salary
        self.location = location
        self.total = total

class _Employees:
    def __init__(self, conn):
        self._conn = conn

    def insert(self, employee):
        cursor = self._conn.cursor()
        cursor.execute("INSERT INTO Employees (id,name,salary,coffee_stand) VALUES (?,?,?,?)",
                       (employee.employee_id, employee.name, employee.salary, employee.coffee_stand))

    def get_all_records(self):
        cur = self._conn.cursor()
        cur.execute("""
                    SELECT * FROM Employees ORDER BY id
                """)
        result = cur.fetchall()
        employees = []
        for i in range(0, len(result)):
            employees.append(Employee(*result[i]))
        return employees

class _Suppliers:
    def __init__(self, conn):
        self._conn = conn

    def insert(self, supplier):
        cur = self._conn.cursor()
        cur.execute("""
                        INSERT INTO Suppliers (id, name, contact_information) VALUES (?,?,?)
                        """, [supplier.supplier_id, supplier.name, supplier.contact_information])

    def get_all_records(self):
        cur = self._conn.cursor()
        cur.execute("""
                            SELECT * FROM Suppliers ORDER BY id
                        """)
        result = cur.fetchall()
        suppliers = []
        for i in range(0, len(result)):
            suppliers.append(Supplier(*result[i]))
        return suppliers

class _Products:
    def __init__(self, conn):
        self._conn = conn

    def insert(self, product):
        cur = self._conn.cursor()
        cur.execute("""
                        INSERT INTO Products (id, description, price, quantity) VALUES (?,?,?,?)
                        """, [product.product_id, product.description, product.price, product.quantity])

    def get_all_records(self):
        cur = self._conn.cursor()
        cur.execute("""
                        SELECT * FROM Products ORDER BY id
                        """)
        result = cur.fetchall()
        products = []
        for i in range(0, len(result)):
            products.append(Product(*result[i]))
        return products

    def find(self, product_id):
        cur = self._conn.cursor()
        cur.execute("""
            SELECT * FROM  Products WHERE id = ?
        """, [product_id])
        result = cur.fetchone()
        return Product(*result)

    def update(self, product):
        cur = self._conn.cursor()
        cur.execute("""
            UPDATE Products SET description = ?, price = ?, quantity = ? WHERE id = ?
        """, [product.description, product.price, product.quantity, product.product_id])

class _CoffeeStands:
    def __init__(self, conn):
        self._conn = conn

    def insert(self, coffee_stand):
        cur = self._conn.cursor()
        cur.execute("""
                        INSERT INTO Coffee_stands (id, location, number_of_employees) VALUES (?,?,?)
                        """, [coffee_stand.stand_id, coffee_stand.location, coffee_stand.number_of_employees])

    def get_all_records(self):
        cur = self._conn.cursor()
        cur.execute("""
                        SELECT * FROM Coffee_stands ORDER BY id
                        """)
        result = cur.fetchall()
        stands = []
        for i in range(0, len(result)):
            stands.append(CoffeeStand(*result[i]))
        return stands

class _Activities:
    def __init__(self, conn):
        self._conn = conn

    def insert(self, activity):
        cur = self._conn.cursor()
        cur.execute("""
                    INSERT INTO Activities (product_id, quantity, activator_id, date) VALUES (?,?,?,?)
                """, [activity.product_id, activity.quantity, activity.activator_id, activity.date])

    def get_all_records(self):
        cur = self._conn.cursor()
        cur.execute("""
                        SELECT * FROM Activities ORDER BY Activities.date
                        """)
        result = cur.fetchall()
        acts = []
        for i in range(0, len(result)):
            acts.append(Activity(*result[i]))
        return acts

class _Repository(object):
    def __init__(self):
        self._conn = sqlite3.connect('moncafe.db')
        self.employees = _Employees(self._conn)
        self.suppliers = _Suppliers(self._conn)
        self.products = _Products(self._conn)
        self.coffee_stands = _CoffeeStands(self._conn)
        self.activities = _Activities(self._conn)

    def create_db(self):
        cursor = self._conn.cursor()
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS Employees(
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            salary REAL NOT NULL,
            coffee_stand INTEGER REFERENCES Coffee_stand(id))
                    """)
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS Suppliers(
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            contact_information TEXT)""")
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS Products(
            id INTEGER PRIMARY KEY,
            description TEXT NOT NULL,
            price REAL NOT NULL,
            quantity INTEGER NOT NULL)
            """)
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS Coffee_stands(
            id INTEGER PRIMARY KEY,
            location TEXT NOT NULL,
            number_of_employees INTEGER)
            """)
        cursor.execute("""
           CREATE TABLE IF NOT EXISTS Activities(
           product_id INTEGER INTEGER REFERENCES Product(id),
           quantity INTEGER NOT NULL,activator_id INTEGER NOT NULL,
           date DATE NOT NULL )
           """)

    def _close(self):
        self._conn.commit()
        self._conn.close()

    def employees_report(self):
        cursor = self._conn.cursor()
        cursor.execute("""
                        SELECT e.name, e.salary, c.location, SUM(p.price * (-a.quantity)) as total_sales_income
                        FROM Employees as e
                        JOIN Coffee_stands as c ON e.coffee_stand = c.id
                        LEFT JOIN Activities as a ON e.id = a.activator_id
                        LEFT JOIN Products as p ON p.id = a.product_id
                        GROUP BY e.id ORDER BY e.name
                        """)
        result = cursor.fetchall()
        reports = []
        for i in range(0, len(result)):
            x = result[i][3]
            if x is None:
                x = 0
            reports.append(Report(result[i][0], result[i][1], result[i][2], x))
        return reports

    def report_activities(self):
        cur = self._conn.cursor()
        cur.execute("""
            SELECT a.date, p.description, a.quantity, e.name, s.name
            FROM Activities a JOIN Products p on a.product_id = p.id
            LEFT JOIN Employees e on a.activator_id = e.id
            LEFT JOIN Suppliers s on s.id = a.activator_id
            ORDER BY a.date
            """)
        result = cur.fetchall()
        reports = []
        for i in range(0, len(result)):
            reports.append(ActivityReport(*result[i]))
        return reports

# Repository is a singleton
repo = _Repository()
atexit.register(repo._close)


from __future__ import absolute_import, unicode_literals

import unittest

from fluent.runtime import FluentBundle
from fluent.runtime.errors import FluentReferenceError

from ..utils import dedent_ftl

class TestVariants(unittest.TestCase):

    def setUp(self):
        self.ctx = FluentBundle(['en-US'], use_isolating=False)
        self.ctx.add_messages(dedent_ftl("""
            -variant = {
                [a] A
               *[b] B
             }
            foo = { -variant }
            bar = { -variant[a] }
            baz = { -variant[b] }
            qux = { -variant[c] }
        """))

    def test_returns_the_default_variant(self):
        val, errs = self.ctx.format('foo', {})
        self.assertEqual(val, 'B')
        self.assertEqual(len(errs), 0)

    def test_choose_other_variant(self):
        val, errs = self.ctx.format('bar', {})
        self.assertEqual(val, 'A')
        self.assertEqual(len(errs), 0)

    def test_choose_default_variant(self):
        val, errs = self.ctx.format('baz', {})
        self.assertEqual(val, 'B')
        self.assertEqual(len(errs), 0)

    def test_choose_missing_variant(self):
        val, errs = self.ctx.format('qux', {})
        self.assertEqual(val, 'B')
        self.assertEqual(len(errs), 1)
        self.assertEqual(
            errs,
            [FluentReferenceError("Unknown variant: c")])


from flask import Flask, request, abort
from flask_sqlalchemy import SQLAlchemy
import logging
import os
import sys

from hero.adapters.web.config import Config
from hero.adapters.slack.verify_request import verify_request

LOGGER = logging.getLogger(__name__)
app = Flask(__name__)
app.config.from_object(Config)

@app.route("/", methods=["POST"])
def index():
    return str(main(request))

@app.route("/claim", methods=["POST"])
def claim():
    from hero.core import claim

    return str(claim(request))

def main(request):
    try:
        request.data = request.get_data()
        if not verify_request(request):
            abort(403)

        from hero.core import start, stop, claim, high_score

        method = request.form["text"]
        if method == "start":
            return start(request.form)
        elif method == "stop":
            return stop(request.form)
        elif method == "high_score":
            return high_score(request.form)
        else:
            LOGGER.error("Invalid method: %s", str(request))
            raise ValueError(f"You tried to use an invalid method: {method}")
    except Exception as e:
        LOGGER.error(str(e))
        raise e


# Copyright 1997 - 2018 by IXIA Keysight
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from ixnetwork_restpy.base import Base
from ixnetwork_restpy.errors import IxNetworkError

class Sessions(Base):
    """Sessions class

    Manage IxNetwork sessions based on the connection information provided to the TestPlatform class.

    Child classes:
        Sessions.Ixnetwork
    """
    _SDM_NAME = 'sessions'

    def __init__(self, parent):
        super(Sessions, self).__init__(parent)

    @property
    def Ixnetwork(self):
        """An instance of the Ixnetwork class

        Returns:
            obj(ixnetwork_restpy.testplatform.sessions.ixnetwork.ixnetwork.Ixnetwork):

        Raises:
            ValueError: If the version of IxNetwork server is not suporrted. The minimum version supported is 8.42.
        """
        from ixnetwork_restpy.testplatform.sessions.ixnetwork.ixnetwork import Ixnetwork
        ixnetwork = Ixnetwork(self)
        build_number = ixnetwork._connection._read('%s/ixnetwork/globals' % self.href)['buildNumber']
        from distutils.version import LooseVersion
        if LooseVersion(build_number) < LooseVersion('8.42'):
            raise ValueError('IxNetwork server version %s is not supported. The minimum version supported is 8.42' % build_number)
        ixnetwork._set_properties(ixnetwork._connection._read('%s/%s' % (self.href, Ixnetwork._SDM_NAME)))
        return ixnetwork

    @property
    def State(self):
        """The state of the session

        Returns:
            str
        """
        return self._properties['state'].upper()

    @property
    def ApplicationType(self):
        """The application type of the session

        Returns:
            str
        """
        return self._properties['applicationType']

    @property
    def Id(self):
        """The id of the session

        Returns:
            number
        """
        return self._properties['id']

    @property
    def UserId(self):
        """The user id of the session

        Returns:
            str
        """
        return self._properties['userId']

    @property
    def UserName(self):
        """The user name of the session

        Returns:
            str
        """
        return self._properties['userName']

    def Start(self):
        """Starts the session

        Returns:
            None
        """
        if self._connection._platform != 'windows':
            response = self._execute('start', payload={'applicationType': self.ApplicationType})
            if response is not None:
                self._set_properties(response, clear=True)

    def find(self, Id=None):
        """Finds all child instances of Sessions on the server.

        Raises:
            ServerError: The server has encountered an uncategorized error condition
        """
        responses = self._connection._read('%s/%s' % (self._parent.href, self._SDM_NAME))
        self._clear()
        for response in responses:
            if Id is not None:
                if str(response['id']) == str(Id):
                    self._set_properties(response)
            else:
                self._set_properties(response)
        return self

    def add(self):
        """Adds a new sessions node on the server and retrieves it in this instance.

        Raises:
            ServerError: The server has encountered an uncategorized error condition
        """
        applicationType = 'ixnrest'
        self._create(locals())
        if self._connection._platform == 'linux':
            self.Start()
        return self

    def remove(self):
        """Deletes all the sessions data in this instance from server.

        Raises:
            NotFoundError: The requested resource does not exist on the server
            ServerError: The server has encountered an uncategorized error condition
        """
        try:
            self._execute('stop')
            self._delete()
        except IxNetworkError as e:
            if e._status_code not in [404, 405]:
                raise e

    def GetObjectFromHref(self, href):
        """Given an href get the corresponding object

        References are used to establish relationships between disparate nodes.
        This allows a user to get an object using a reference from a node.
        For example:
            A /vport node has a ConnectedTo property that is a reference to an /availableHardware/chassis/card/port node.
            A user can get a vport and use this method to get the port node in order to clear ownership

        Args:
            href (str): A valid href reference

        Returns:
            obj | None: If the reference is valid an ixnetwork_restpy object or None if the reference cannot be resolved
        """
        try:
            node = self
            for piece in href[href.find('ixnetwork'):].split('/'):
                if piece.isdigit():
                    for item in node:
                        if item.href.split('/').pop() == piece:
                            node = node._read(item.href)
                            break
                else:
                    class_name = '%s%s' % (piece[0].upper(), piece[1:])
                    node = getattr(node, class_name)
                    if hasattr(node, 'find') is True:
                        node.find()
            return node
        except:
            return None

    def GetFileList(self, remote_directory=None):
        """Get a list of files from the IxNetwork session instance

        Args:
            remote_directory (str): A remote directory path

        Returns:
            dict(root_path, list(dict(filename, filesize)): A list of all the files for a given directory
        """
        href = '%s/ixnetwork/files' % self.href
        if remote_directory is not None:
            href = '%s?absolute=remote_directory'
        return self._connection._read(href)

    def DownloadFile(self, remote_filename, local_filename = None):
        """Download a file from the IxNetwork session instance

        Args:
            remote_fileanme (str): the name of the remote file
            local_filename (str): the name that the remote contents will be saved to

        Returns:
            str: the local file name
        """
        return self._connection._get_file('%s/ixnetwork' % self.href, remote_filename=remote_filename, local_filename=local_filename)

    def UploadFile(self, local_filename, remote_filename=None):
        """Upload a file to the IxNetwork session instance

        Args:
            local_filename (str): the name of the local file
            remote_filename (str): the name that will be used when saving the file on the IxNetwork sesson instance

        Returns:
            dict(filename, filesize): Details of the file that was uploaded
        """
        return self._connection._put_file('%s/ixnetwork' % self.href, local_filename=local_filename, remote_filename=remote_filename)


import numpy as np
import h5py
import seaborn as sns
import matplotlib.pyplot as plt

out = h5py.File('../output/BetaPlane/BetaPlane.mat');
out = out['out']
freqUnstb = out['freq'].value.view(np.double)*24*3600/2/3.14159
growthUnstb = out['growth'].value.view(np.double)*24*3600
wnUnstb = out['wn'].value.view(np.double)

freq = freqUnstb[np.where(growthUnstb>0)]
growth = growthUnstb[np.where(growthUnstb>0)]
wn = wnUnstb[np.where(growthUnstb>0)]

growth = growth[np.where(np.logical_and(freq<1, abs(freq)>0.05))]
wn = wn[np.where(np.logical_and(freq<1, abs(freq)>0.05))]
freq = freq[np.where(np.logical_and(freq<1, abs(freq)>0.05))]

msize = growth.flatten()*40
malpha = msize/max(msize)*0.85

plt.scatter(wn*freq/abs(freq),abs(freq),c='0.5',edgecolor='face',s=msize**2,alpha=0.8)
plt.legend(markerscale=0.6)
#plt.xlim(-0.002,0.014)
plt.ylim(0,1)
plt.ylabel('Freqency (cycles/day)',fontsize=18)
plt.xlabel('Wavenumber',fontsize=18)
plt.title(r'Unstable dry modes, $\mu_{max}=$%.4f/day'%growth.max(),fontsize=16)
#plt.savefig('../figure/gravVel/dry_dr500_c'+str(c)+'.pdf')
plt.show()


# -*- coding: utf-8 -*-
import utils, os, MasterData, excel_template
import spold2_reader as spold2
from copy import deepcopy, copy
MD = MasterData.get_current_MD()
folder = r'C:\releases\other_versions\UVEK\excel'
filename = 'ecoinvent 3 wood DS for update.xlsx'
tab = 'activity DS'
filelist = utils.read_excel(folder, filename, tab)
filelist = utils.df_value_set(filelist, ['Name', 'Geography'])
version = '20181123'
system_model = 'Undefined'
folder = utils.version_system_model_path(version, system_model)
ao = utils.pkl_load(os.path.join(folder, 'pkl'), 'ao')
ao_for_AL = utils.ao_for_AL(ao)
ao = ao[ao[['activityName', 'geography']].apply(lambda x: tuple(x) in filelist, axis = 1)]
filelist = set(ao['filename'])
dataset_folder = os.path.join(folder, 'datasets')
classif = dict(zip(MD['IntermediateExchanges']['name'], MD['IntermediateExchanges']['By-product classification']))

ps = []
p = {'name': 'SAM_activityLink_1',
     'unitName': 'dimensionless',
     'amount': 0,
     'propertyId': '5fbe72b3-a70b-42fe-932b-1c17327e3fa6',
     }
ps.append(spold2.TProperty(p, constructor = 'dict'))
p['name'] = 'SAM_activityLink_1_scaling_factor'
p['propertyId'] = 'afd66188-6460-4b90-a5ee-9ecb56c99574'
ps.append(spold2.TProperty(p, constructor = 'dict'))
result_folder = os.path.join(folder, 'datasets')
fs = []
for filename in filelist:
    f = spold2.Dataset(dataset_folder, filename, ao_for_AL = ao_for_AL)
    for group in ['FromTechnosphere', 'ByProduct']:
        g = f.get(group, [])
        setattr(f, group, [])
        for exc in g:
            if group == 'FromTechnosphere' or (group == 'ByProduct' and classif[exc.name] == 'Waste'):
                for i in range(len(ps)):
                    ps[i].exchange_baseline.update(exc.baseline(['activityLink_activityName', 'activityLink_geography']))
                ps = deepcopy(ps)
                exc.properties.extend(ps)
                exc.properties_dict.update({p.name: p for p in ps})
                getattr(f, group).append(deepcopy(exc))
    fs.append(copy(f))
template = excel_template.assemble_for_templates(fs)
version = 'UVEK'
system_model = 'Allocation, cut-off'
folder = utils.version_system_model_path(version, system_model)
indexes = utils.pkl_load(os.path.join(folder, 'pkl'), 'indexes')
df = indexes.dfs['ie'].copy()
df['pool name'] = df[['activityName', 'geography']].apply(lambda x: ' - '.join(tuple(x)), axis = 1)
dfs = excel_template.template_to_dfs(template)
d = template['exchanges'].copy()
keep = ['activityName', 'geography', 'group', 'name', 'compartment', 'subcompartment',
    'activityLink_activityName', 'activityLink_geography', 'amount', 'unitName']
d = d[keep]
d.rename(columns = {'amount': 'exchange amount', 'unitName': 'exchange unitName'}, inplace = True)
tab = 'property'
d = utils.replace_empty_in_df(d)
template[tab] = utils.replace_empty_in_df(template[tab])
template[tab] = utils.join_df(template[tab], d, keep[:-2])
template[tab].insert(0, 'x', range(len(template[tab])))
template[tab]['x'] += 2
template[tab].insert(0, 'pool name',
    template[tab]['x'].apply(lambda x: '=IF(AND(LEFT(N{}, 4)="SAM_",NOT(RIGHT(N{}, 6)="factor"),NOT(ISBLANK(P{}))),VLOOKUP(P{},pool!A:B,2,0),"")'.format(x, x, x, x)))
template[tab].insert(0, 'pool unitName',
    template[tab]['x'].apply(lambda x: '=IF(AND(LEFT(N{}, 4)="SAM_",NOT(RIGHT(N{}, 6)="factor"),NOT(ISBLANK(P{}))),VLOOKUP(P{},pool!A:C,3,0),"")'.format(x, x, x, x)))
condition = template[tab]['propertyName'].apply(lambda x: 'SAM_' in x)
template[tab].loc[condition, 'amount'] = ''
#template[tab].insert(0, 'pool name', '')
columns = dfs[2][2].copy()
columns.insert(9, 'exchange amount')
columns.insert(10, 'exchange unitName')
columns.insert(11, 'pool name')
columns.insert(12, 'pool unitName')
dfs[2] = (template[tab], tab, columns)
df.rename(columns = {'index': 'pool id', 'unitName': 'pool unitName'}, inplace = True)
dfs.append((df, 'pool', ['pool id', 'pool name', 'pool unitName']))
result_folder = r'C:\releases\other_versions\UVEK\Undefined\excel'
result_filename = 'wood_datasets_20181128.xlsx'
utils.dataframe_to_excel(result_folder, result_filename, dfs, feedback = True)


# Import of troch library
import torch
from torch import optim
from torch import nn

# Import other functions
from Functions.compute_nb_errors import compute_nb_errors

# Function that train and test the model
def train_and_test_model(Models, train_input, train_target, train_classes, Tests, nb_epochs, mini_batch_size):

    # Initialization for the optimization
    criterion = nn.CrossEntropyLoss()
    eta = 1e-3
    loss_coeff = 10

    # Initialization of the train and test error list
    Train_error = []
    Test_error = []
    std_deviation = 0.0 # We only need the standard deviation for the last step so no need of list

    for e in range(nb_epochs):

        train_error = 0.0
        avg_nb_test_error = torch.tensor(())

        # Print of the actual epoch
        # print('epoch =', e)

        for k in range(len(Models)):

            model = Models[k]
            optimizer = optim.Adam(model.parameters(), lr = eta)

            # Batch
            for b in range(0, train_input.size(0), mini_batch_size):
                digit1, digit2, result = model(train_input.narrow(0, b, mini_batch_size))

                loss_result = criterion(result, train_target.narrow(0, b, mini_batch_size))
                loss_digit1 = criterion(digit1, train_classes[:,0].narrow(0, b, mini_batch_size))
                loss_digit2 = criterion(digit2, train_classes[:,1].narrow(0, b, mini_batch_size))

                if model.AL:
                    loss = loss_result + loss_coeff*loss_digit1 + loss_coeff*loss_digit2
                else:
                    loss = loss_result

                model.zero_grad()
                loss.backward()
                optimizer.step()

            # Compute train and test error for e epochs
            nb_train_errors = compute_nb_errors(model, train_input, train_target, mini_batch_size)
            train_error += nb_train_errors / 10

            nb_test_errors = compute_nb_errors(model, Tests[k][0], Tests[k][1], mini_batch_size)
            nb_test_errors = torch.tensor([nb_test_errors/10]).float()
            avg_nb_test_error = torch.cat((avg_nb_test_error, nb_test_errors), 0)

        Train_error.append(train_error / len(Models))
        Test_error.append(avg_nb_test_error.mean().tolist())
        std_deviation = avg_nb_test_error.std().tolist()

    return Train_error, Test_error, std_deviation


from __future__ import division, print_function
import glob

from matplotlib.pyplot import *
import matplotlib.mlab as mlab

from qubicpack import qubicpack as qp
import qubic.fibtools as ft

import qubic.demodulation_lib as dl
from qubic.utils import progress_bar

# ========== Get data ==================
day = '2019-04-04'
data_dir = '/home/louisemousset/QUBIC/Qubic_work/Calibration/datas/' + day + '/'
dirs = np.sort(glob.glob(data_dir + '*RFswitch*'))
print (dirs)

# Take the first file
thedir = dirs[0]
print(thedir)

# Look at the file labels
labels = []
for d in dirs:
    bla = str.split(d, '__')
    labels.append(bla[1])
print (labels)

# =============================================

def get_amp_first_harmonic(a, tes, freq_mod):
    """
    This function takes the signal of a given TES, makes its spectrum
    and get the amplitude of the first harmonic that should be around
    the modulation frequency of the source.

    Parameters
    ----------
    a : qubicpack object
    tes : int
        tes number
    freq_mod : float
        modulation frequency of the external source

    Returns
    -------

    """
    data = a.timeline(TES=tes)
    t_data = a.timeline_timeaxis(axistype='pps')
    FREQ_SAMPLING = 1. / (t_data[1] - t_data[0])
    spectrum_f, freq_f = mlab.psd(data, Fs=FREQ_SAMPLING, NFFT=len(data), window=mlab.window_hanning)
    amp_peak = np.interp(freq_mod, freq_f, spectrum_f)
    okfit = np.abs(freq_f - freq_mod) < 0.1

    guess = np.array([freq_mod, 0.01, np.max(spectrum_f[okfit]), np.median(spectrum_f[okfit])])

    res = ft.do_minuit(freq_f[okfit], spectrum_f[okfit], np.ones(np.sum(okfit)), guess, functname=dl.gauss,
                       fixpars=[1, 0, 0, 0, 0], nohesse=True, force_chi2_ndf=True)

    return t_data, data, FREQ_SAMPLING, freq_f, spectrum_f, amp_peak, okfit, res

# =========== Test with one TES ==============
# Read data from a given ASIC

AsicNum = 1
a = qp()
a.read_qubicstudio_dataset(thedir, asic=AsicNum)

freq_mod = 1.
tes_num = 44
t_data, data, FREQ_SAMPLING, freq_f, spectrum_f, amp_peak, okfit, res = get_amp_first_harmonic(a, tes_num, freq_mod)
print('FREQ_SAMPLING = {}'.format(FREQ_SAMPLING))

# Signal as a function of time
plot(t_data, (data - np.mean(data)) / np.std(data), label='Data')

# Look at the amplitude of the peak
print('Amplitude = {}'.format(res[1][2]))

figure('TES spectrum')
plot(freq_f, spectrum_f, label='Data')
plot(freq_f[okfit], dl.gauss(freq_f[okfit], res[1]), label='Gaussian: amplitude = {0:5.3g}'.format(res[1][2]))
plot([freq_mod, freq_mod], [1e4, 1e13], label='Modulation Frequency: {}Hz'.format(freq_mod))
yscale('log')
xscale('log')
xlim(0.01, 3.)
ylim(1e4, 1e13)
legend(loc='best')

# =============== Loop on all TES an both ASICS ================
allres = np.zeros((256, 4))
allerr = np.zeros((256, 4))
allamp_peak = np.zeros(256)

for asic in [1, 2]:
    a.read_qubicstudio_dataset(thedir, asic=asic)
    bar = progress_bar(128, 'ASIC #{}'.format(AsicNum))
    for tes in np.arange(128) + 1:
        t_data, data, FREQ_SAMPLING, freq_f, spectrum_f, amp_peak, okfit, res = get_amp_first_harmonic(a, tes, freq_mod)
        bar.update()
        tes_index = (tes - 1) + 128 * (asic - 1)
        allres[tes_index, :] = res[1]
        allerr[tes_index, :] = res[2]
        allamp_peak[tes_index] = amp_peak

amps = allres[:, 2]
# amps = allamp_peak
img_one = ft.image_asics(all1=amps)
mm, ss = ft.meancut(amps, 3)
imshow(img_one, vmin=0, vmax=mm + 3 * ss, interpolation='nearest')
colorbar()

# ============== Loop on all files =================
allres_tot = np.zeros((len(dirs), 256, 4))
allerr_tot = np.zeros((len(dirs), 256, 4))
allamp_peak_tot = np.zeros((len(dirs), 256))

for idir in range(len(dirs)):
    thedir = dirs[idir]
    for asic in [1, 2]:
        a.read_qubicstudio_dataset(thedir, asic=asic)
        bar = progress_bar(128, 'file #{0} ASIC #{1}'.format(idir, AsicNum))
        for tes in np.arange(128) + 1:
            t_data, data, FREQ_SAMPLING, freq_f, spectrum_f, amp_peak, okfit, res = get_amp_first_harmonic(a, tes, freq_mod)
            bar.update()
            tes_index = (tes - 1) + 128 * (asic - 1)
            allres_tot[idir, tes_index, :] = res[1]
            allerr_tot[idir, tes_index, :] = res[2]
            allamp_peak_tot[idir, tes_index] = amp_peak

# ============= Look at signal for each file ===========
amplitudes = allres_tot[:, :, 2]
# amplitudes = allamp_peak_tot

mm, ss = ft.meancut(amplitudes, 3)

allimg = np.empty((len(dirs), 17, 17))

figure('Each measurement')
for i in range(len(dirs)):
    subplot(3, 3, i + 1)
    amps = amplitudes[i, :]
    img = ft.image_asics(all1=amps)
    allimg[i, :, :] = img
    imshow(img, vmin=0, vmax=50*ss, interpolation='nearest')
    colorbar()
    title(labels[i])

# ============== Try to make the fringes ================

# This is Stot
index_tot = 1
Stot = amplitudes[index_tot, :]

# These are C-i, C-j and S-ij
index_21_35 = ['21_35', 3, 4, 0]
index_21_39 = ['21_39', 3, 5, 2]
index_39_54 = ['39_54', 5, 8, 6]
index_21_54 = ['21_54', 3, 8, 7]

allsets = [index_21_35, index_21_39, index_39_54, index_21_54]

allfringe = np.zeros((len(allsets), 17, 17))

for iset in range(len(allsets)):
    theset = allsets[iset]
    C_i = amplitudes[theset[1], :]
    C_j = amplitudes[theset[2], :]
    S_ij = amplitudes[theset[3], :]
    fringe = Stot + S_ij - C_i - C_j
    allfringe[iset, :, :] = ft.image_asics(all1=fringe)

mm_fringe, ss_fringe = ft.meancut(np.isfinite(allfringe), 3)
rng = ss_fringe

figure('fringes')
for i in range(len(allsets)):
    subplot(2, 2, i + 1)
    imshow(allfringe[i, :, :], vmin=0., vmax=1e13, interpolation='nearest')
    title(allsets[i][0])
    colorbar()