import paho.mqtt.client as mqtt
import stmpy
import logging
from threading import Thread
import json

MQTT_BROKER = 'mqtt20.iik.ntnu.no'
MQTT_PORT = 1883
MQTT_TOPIC_INPUT = 'ttm4115/team_5/#'
MQTT_TOPIC_OUTPUT = 'ttm4115/team_5/command'
MQTT_TOPIC_STUDENT = 'ttm4115/team_5/student'
MQTT_TOPIC_TEACHER = 'ttm4115/team_5/teacher'

TOTAL_TEAMS = 3
TEAM_ONE = 3
TEAM_TWO = 3
TEAM_THREE = 4

TEAM_ONE_C = 3
TEAM_TWO_C = 3
TEAM_THREE_C= 4

teams_done_with_irat = []
TEAM_IDS = [1,2,3]
irat_finished_counter = 0
trat_finished_counter = 0

class TimerLogic:
    def __init__(self, name, duration, component):
        self._logger = logging.getLogger(__name__)
        self.name = name
        self.duration = duration
        self.component = component
        self.mqtt_client = mqtt.Client()
        self.mqtt_client.connect(MQTT_BROKER, MQTT_PORT)
        self.mqtt_client.subscribe(MQTT_TOPIC_INPUT)
        self.mqtt_client.loop_start()

    def started(self):
        self.stm.start_timer('t', self.duration)
        self._logger.debug('New timer {} with duration {} started.'
                           .format(self.name, self.duration))

    def timer_completed(self):
        self._logger.debug('Timer {} expired.'.format(self.name))
        t_name = self.name
        string = str('{{"command":"{}_expired"}}'.format(t_name))
        self.mqtt_client.publish(MQTT_TOPIC_STUDENT, string)
        self.stm.terminate()

    def create_machine(timer_name, duration, component):
        timer_logic = TimerLogic(name=timer_name, duration=duration, component=component)
        t0 = {'source': 'initial',
              'target': 'active',
              'effect': 'started'}
        t1 = {
            'source': 'active',
            'target': 'completed',
            'trigger': 't',
            'effect': 'timer_completed'}
        t2 = {
            'source': 'active',
            'target': 'completed',
            'trigger': 'rat_completed',
            'effect': 'timer_completed'}
        timer_stm = stmpy.Machine(name=timer_name, transitions=[t0, t1, t2],
                                  obj=timer_logic)
        timer_logic.stm = timer_stm
        return timer_stm

class TeamManagerComponent:
    def on_connect(self, client, userdata, flags, rc):
        self._logger.debug('MQTT connected to {}'.format(client))

    def on_message(self, client, userdata, msg):
        global TEAM_ONE, TEAM_TWO, TEAM_THREE, TEAM_ONE_C, TEAM_TWO_C, TEAM_THREE_C, teams_done_with_irat, TEAM_IDS

        self._logger.debug('Incoming message to topic {}'.format(msg.topic))

        try:
            payload = json.loads(msg.payload.decode("utf-8"))
        except Exception as err:
            self._logger.error('Message sent to topic {} had no valid JSON. Message ignored. {}'.format(msg.topic, err))
            return
        command = payload.get('command')
        self._logger.debug('Command in message is {}'.format(command))

        if command == 'student_iRAT_done':

            try:
                print(type(self))
                student_id = payload.get('student_id')
                team_id = payload.get('team_id')
                RAT_id = payload.get("RAT_id")
                answers = payload.get('answers')
                print(f"answers from student {student_id}: {answers}")

                score = answers.count("correct")
                print(f"The score for student id {student_id}, team {team_id}, RAT {RAT_id}: {score}")

                """
                #Åpner studentscores og legger til iRAT scoren på riktig sted
                with open('Server/resources/ratDB2.json', 'r') as f:
                    data = json.load(f)
                data[student_id][RAT_id]['iRAT_score'] = str(score)

                with open('Server/resources/ratDB2.json', 'w') as f:
                    json.dump(data, f)
                """

                message_non_parsed = {"command":"iRAT_done"}
                message_start_tRAT_non_parsed = {"command":"start_tRAT", "team_id":team_id, "leader_id": "1"}
                message = json.dumps(message_non_parsed)
                message_start_tRAT = json.dumps(message_start_tRAT_non_parsed)
                if team_id == "1":
                    TEAM_ONE +=1
                    if TEAM_ONE == TEAM_ONE_C:
                        self._logger.debug('everyone in TEAM_ONE is done with iRAT, starting tRAT')
                        self.mqtt_client.publish(MQTT_TOPIC_STUDENT, message)
                        self.mqtt_client.publish(MQTT_TOPIC_STUDENT, message_start_tRAT)
                        timer_stm2 = TimerLogic.create_machine("timer_team1", 200000, self)
                        teams_done_with_irat.append(team_id)
                        self.stm_driver.add_machine(timer_stm2)

                if team_id == "2":
                    TEAM_TWO +=1
                    if TEAM_TWO == TEAM_TWO_C:
                        self._logger.debug('everyone in TEAM_TWO is done with iRAT, starting tRAT')
                        self.mqtt_client.publish(MQTT_TOPIC_STUDENT, message)
                        self.mqtt_client.publish(MQTT_TOPIC_STUDENT, message_start_tRAT)
                        timer_stm3 = TimerLogic.create_machine("timer_team2", 200000, self)
                        teams_done_with_irat.append(team_id)
                        self.stm_driver.add_machine(timer_stm3)

                if team_id == "3":
                    TEAM_THREE +=1
                    if TEAM_THREE == TEAM_THREE_C:
                        self._logger.debug('everyone in TEAM_THREE is done with iRAT, starting tRAT')
                        self.mqtt_client.publish(MQTT_TOPIC_STUDENT, message)
                        self.mqtt_client.publish(MQTT_TOPIC_STUDENT, message_start_tRAT)
                        timer_stm4 = TimerLogic.create_machine("timer_team3", 200000, self)
                        teams_done_with_irat.append(team_id)
                        self.stm_driver.add_machine(timer_stm4)

            except Exception as err:
                print("An error occured in TeamManagerComponent")

        if command == "tRAT_answers":
            try:
                print(type(self))
                team_id = payload.get('team_id')
                RAT_id = payload.get("RAT_id")
                answers = payload.get('answers')
                print(f"answers from team {team_id}: {answers}")

                score = answers.count("correct")
                print(f"Score for team {team_id}: {score}")

                """
              #Updates scores
                with open('Server/resources/ratDB2.json') as f:
                    data = json.load(f)
                    print("opened file")
                for student in data.values():
                    print("Parsing file")
                    if student["team"] == team_id:
                        print(f"team id: {team_id}")
                        student[RAT_id]["tRAT_score"] = str(score)
                with open('Server/resources/ratDB2.json', 'w') as f:
                    print("Writing to file")
                    json.dump(data, f)

                """

                if team_id == "1":
                    self.stm_driver.send('rat_completed','timer_team1')
                elif team_id == "2":
                    self.stm_driver.send('rat_completed','timer_team2')
                elif team_id == "3":
                    self.stm_driver.send('rat_completed','timer_team3')

                trat_message_non_parsed = {"command":"tRAT_done"}
                tRAT_message = json.dumps(trat_message_non_parsed)
                self.mqtt_client.publish(MQTT_TOPIC_INPUT, tRAT_message)

            except Exception as err:
                print("Error occured in Team manager component adter tRAT_done")

    def __init__(self):
        self._logger = logging.getLogger(__name__)
        print('logging under name {}.'.format(__name__))
        self._logger.info('Starting Component')
        self._logger.debug('Connecting to MQTT broker {} at port {}'.format(MQTT_BROKER, MQTT_PORT))
        self.mqtt_client = mqtt.Client()
        self.mqtt_client.on_connect = self.on_connect
        self.mqtt_client.on_message = self.on_message
        self.mqtt_client.connect(MQTT_BROKER, MQTT_PORT)
        self.mqtt_client.subscribe(MQTT_TOPIC_INPUT)
        self.mqtt_client.loop_start()
        self.stm_driver = stmpy.Driver()
        self.stm_driver.start(keep_active=True)
        self._logger.debug('Component initialization finished')

    def stop(self):
        self.mqtt_client.loop_stop()
        self.stm_driver.stop()

class ManagerComponent:
    def on_connect(self, client, userdata, flags, rc):
        self._logger.debug('MQTT connected to {}'.format(client))

    def on_message(self, client, userdata, msg):
        global irat_finished_counter, TEAM_ONE, TEAM_TWO, TEAM_THREE, trat_finished_counter
        self._logger.debug('Incoming message to topic {}'.format(msg.topic))

        try:
            payload = json.loads(msg.payload.decode("utf-8"))
        except Exception as err:
            self._logger.error('Message sent to topic {} had no valid JSON. Message ignored. {}'.format(msg.topic, err))
            return
        command = payload.get('command')
        self._logger.debug('Command in message is {}'.format(command))

        if command == 'start_RAT':
            irat_finished_counter = 0
            trat_finished_counter = 0
            TEAM_ONE = 0
            TEAM_TWO = 0
            TEAM_THREE = 0
            try:
                print(type(self))
                rat_id = payload.get('RAT_ID')
                print(f"RAT_ID is {rat_id}")
                timer_stm1 = TimerLogic.create_machine("t1", 100000, self)

                try:
                    with open('Server/resources/ratDB2.json', 'r') as f:
                        print(f"Opened ratDB2.json")
                        data = json.load(f)
                    question = data[str(rat_id)]
                    print(type(question))
                    print(question)

                    new_data = {
                        "command": "start_iRAT",
                        **question
                        }

                    self.mqtt_client.publish(MQTT_TOPIC_STUDENT, json.dumps({"command" : "start_iRAT", "RAT" : question}))
                except Exception as err:
                    print("Error occured in Manager component after start_RAT error: {}".format(err))

                self.stm_driver.add_machine(timer_stm1)
            except Exception as err:
                self._logger.error('Invalid arguments to command. {}'.format(err))

        elif command == 'iRAT_done':
            irat_finished_counter += 1
            self._logger.debug('{} teams are done with iRAT'.format(irat_finished_counter))
            if irat_finished_counter == TOTAL_TEAMS:
                self.stm_driver.send('rat_completed','t1')

        elif command == 'tRAT_done':
            print(f"trad_finished_counter before: {trat_finished_counter}")
            trat_finished_counter += 1
            self._logger.debug('{} teams are done with tRAT'.format(trat_finished_counter))
            if trat_finished_counter == TOTAL_TEAMS:
                self.mqtt_client.publish(MQTT_TOPIC_TEACHER, "RAT SESSION DONE")

        elif command == 'fetch_RATs':
            print("Fetching RATs")
            def read_rat_names(file_path):
                with open(file_path, 'r') as f:
                    data = json.load(f)
                rat_names = {}
                for rat_id, rat_data in data.items():
                    rat_names[rat_id] = rat_data['name']
                return rat_names

            file_path = 'Server/resources/ratDB2.json'
            rat_names = read_rat_names(file_path)
            string = json.dumps({"command": "available_RATs", "rat_info": rat_names})
            self.mqtt_client.publish(MQTT_TOPIC_TEACHER, string)

        elif command == 'create_RAT':
            print("Creating RAT")
            message = payload
            message.pop('command', None)
            with open("Server/resources/ratDB2.json", "r") as f:
                data = json.load(f)
                data.update(message)
            with open("Server/resources/ratDB2.json", "w") as f:
                json.dump(data, f, indent=4)

    def __init__(self):
        self._logger = logging.getLogger(__name__)
        print('logging under name {}.'.format(__name__))
        self._logger.info('Starting Component')
        self._logger.debug('Connecting to MQTT broker {} at port {}'.format(MQTT_BROKER, MQTT_PORT))
        self.mqtt_client = mqtt.Client()
        self.mqtt_client.on_connect = self.on_connect
        self.mqtt_client.on_message = self.on_message
        self.mqtt_client.connect(MQTT_BROKER, MQTT_PORT)
        self.mqtt_client.subscribe(MQTT_TOPIC_INPUT)
        self.mqtt_client.loop_start()
        self.stm_driver = stmpy.Driver()
        self.stm_driver.start(keep_active=True)
        self._logger.debug('Component initialization finished')

    def stop(self):
        self.mqtt_client.loop_stop()
        self.stm_driver.stop()

debug_level = logging.DEBUG
logger = logging.getLogger(__name__)
logger.setLevel(debug_level)
ch = logging.StreamHandler()
ch.setLevel(debug_level)
formatter = logging.Formatter('%(asctime)s - %(name)-12s - %(levelname)-8s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

t = ManagerComponent()
t2= TeamManagerComponent()


from typing import List

class Solution:
    def combinationSum3(self, k: int, n: int) -> List[List[int]]:
        candidates = list(range(1, 10))
        res = []

        def helper(index, acc, path):
            if len(path) == k:
                if acc == n:
                    res.append(path[:])
                return
            for i in range(index, len(candidates)):
                if acc + candidates[i] > n:
                    break
                path.append(candidates[i])
                helper(i + 1, acc + candidates[i], path)
                path.pop()

        helper(0, 0, [])
        return res

print(Solution().combinationSum3(3, 9))


##########################################################################
#                                                                        #
#      May you do good and not evil                                      #
#      May you find forgiveness for yourself and forgive others          #
#      May you share freely, never taking more than you give.            #
#                                                                        #
##########################################################################

# My libraries
from my_plots import SimpleScatter
from my_analysis import RandomDataAnalysis

# Other general python libraries
import sys

# Libraries for the PyQt5 interface
#from PyQt5 import QtGui as qtg
#from PyQt5.QtCore import Qt as qt
from PyQt5 import QtWidgets as qtw
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg

class MyInterface_Qt:
    def __init__(self,
                 geometry = None # Unused
                 ):

        # The engine is for the math/analysis
        self.engine = RandomDataAnalysis()

        # The display is for the plot
        self.display = SimpleScatter()

        # A separate method to initialize the plot
        # This was removed from the SimpleScatter contstructor
        # due to an issue with tk
        self.display.init_scatter()

        # Layout the figure and the buttons
        self.configure_main_window(geometry)

        # Show the window and run the app
        self.main_window.show()
        sys.exit(self.app.exec_())
        return

    def configure_main_window(self,
                              geometry
                              ):

        # Application
        self.app = qtw.QApplication([])

        # Main window
        self.main_window = qtw.QWidget()
        self.main_window.setWindowTitle('Example interface using PyQt5')

        # The main layout is a vertical container
        #   The top part will be the figure, the bottom will be the buttons
        self.main_layout = qtw.QVBoxLayout()
        self.main_window.setLayout(self.main_layout)

        # For the figure, we first create a canvas
        self.canvas = FigureCanvasQTAgg(self.display.fig)
        # Then we add it to the main_layout
        self.main_layout.addWidget(self.canvas)

        # Two rows of buttons, each is a horizontal container
        self.button_row1 = qtw.QHBoxLayout()
        self.button_row2 = qtw.QHBoxLayout()

        # Put those two button rows in their own vertical container
        self.button_rows = qtw.QVBoxLayout()
        self.button_rows.addLayout(self.button_row1)
        self.button_rows.addLayout(self.button_row2)
        # Put the button row container in the main_layout
        self.main_layout.addLayout(self.button_rows)

        ### Creating the actual buttons
        # Create a button
        self.button_generate_random_data = qtw.QPushButton("Generate Random Data")
        # Tell the button what function to call when clicked
        self.button_generate_random_data.clicked.connect(self.make_random_data)
        # Add it to its row
        self.button_row1.addWidget(self.button_generate_random_data)

        # Repeat for the rest of the buttons
        self.button_load_data = qtw.QPushButton("Load a File")
        self.button_load_data.clicked.connect(self.load_data)
        self.button_row1.addWidget(self.button_load_data)

        self.button_save_data = qtw.QPushButton("Save data to file")
        self.button_save_data.clicked.connect(self.engine.output_data)
        self.button_row2.addWidget(self.button_save_data)

        # Save Figure
        self.button_save_figure = qtw.QPushButton("Save Figure")
        self.button_save_figure.clicked.connect(lambda: self.display.save_plot())
        self.button_row2.addWidget(self.button_save_figure)
        return

    def make_random_data(self):
        self.engine.make_random_data()
        self.update_plot()
        return

    def update_plot(self):
        self.display.update_scatter(self.engine.data)
        self.canvas.draw()
        return

    def load_data(self):
        filename,_ = qtw.QFileDialog.getOpenFileName(self.main_window,
                                                     "Select file",
                                                     "./data/")
        try:
            self.engine.load_data(filename)
            self.update_plot()
        except Exception as e:
            print('Problem loading data. File name was: ' + str(filename))
        return

if __name__ == '__main__':
    MyInterface_Qt()


''' A- Imprima un menú de opciones desde una lista
Opciones: 1-Multiplicación de 2 números, 2-Suma y multiplicación de 3 números,
3-Número positivo o negativo, 4-Par o impar, 99-Volver al menú
B-Solicite el ingreso de una opciónam
C- Dada la opción ejecutar la porción de programa que corresponda:
1. Hacer un programa que permita ingresar dos números, y muestre el producto de ambos.
2. Hacer un programa que permita ingresar tres números, y muestre en pantalla tanto la suma
como el producto de ellos.
3. Confeccione un programa que lea un número e indique si este es positivo o negativo
4. Confeccione un programa que lea un número e indique si este es par o impar '''

opciones=['1-Producto de 2 números', '2-Suma y producto de 3 números', '3-Número positivo o negativo', '4-Par o impar', "99-Finalizar programa"]
while True:
    print("--MENÚ DE OPERACIONES--")
    for e in opciones:
        print("  ",e)
    dato=input("Ingrese una opción a ejecutar:")
    try:
        opcion=int(dato)
        if opcion==1:
            uno=input("Ingrese el primer número:")
            dos=input("Ingrese el segundo número:")
            try:
                u=float(uno)
                d=float(dos)
                print("La suma entre ambos da: ", u+d,"\n")
            except:
                print("Debe ser solo número. Intente de vuelta\n\n")
            continue
        elif opcion==2:
            uno=input("Ingrese el primer número:")
            dos=input("Ingrese el segundo número:")
            tres=input("Ingrese el tercer número:")
            try:
                u=float(uno)
                d=float(dos)
                t=float(tres)
                print("La suma entre ellos es ", u+d+t," y el producto resulta ",u*d*t,"\n")
            except:
                print("Debe ser solo número. Intente de vuelta\n\n")
            continue
        elif opcion==3:
            uno=input("Ingrese el primer número:")
            try:
                u=float(uno)
                if u<0:
                    print(u," es negativo\n")
                elif u>0:
                    print(u," es positivo\n")
                else:
                    print("Divide a los extremos infinitos\n")
            except:
                print("Debe ser solo número. Intente de vuelta\n\n")
            continue
        elif opcion==4:
            a= input("Ingrese un número :")
            try:
                number=int(a)
                if number%2==0:
                    print(number, " es par")
                else:
                    print(number, " es impar")
            except:
                print("Debe ser solo número. Intente de vuelta\n\n")
            continue
        elif opcion==99:
            print("--Programa finalizado--")
            break
        else:
            print("No corresponde.Intente de vuelta\n")
            continue
    except:
        print("Debe ser solo número. Intente de vuelta\n\n")
        continue


import json
import numpy as np

def create_minutely(time, hours):
	minutely = [{"key": time + 60*x, "value": np.random.normal(25, 1) + (x/60)%300} for x in range(60*hours)]
	return minutely

def create_hourly(time):
	hourly = [{"key": time + 3600*x, "value": np.random.normal(20, 1) + x%5} for x in range(24)]
	return hourly

##Dump to text file
hour = create_minutely(1436920200 - 1770, 1)
hour_json = json.dumps(hour,separators=(',', ': '), sort_keys=True, indent=4)
print (hour[1]['value'])

text_file = open("hour.txt", 'w')
text_file.write(hour_json)
text_file.flush()
text_file.close()

min_day = create_minutely(1436920200 - 1770, 24)
min_day_json = json.dumps(min_day,separators=(',', ': '), sort_keys=True, indent=4)

text_file = open("min_day.txt", 'w')
text_file.write(min_day_json)
text_file.flush()
text_file.close()

day = create_hourly(1436920200)
day_json = json.dumps(day,separators=(',', ': '), sort_keys=True, indent=4)

text_file = open("day.txt", 'w')
text_file.write(day_json)
text_file.flush()
text_file.close()

##Test importing
text_file = open("hour.txt", 'r')
hour2 = json.loads(text_file.read())
if hour2 == hour:
	print ("Worked!")
	print (hour2[1]['value'])


# # # -*- coding: utf-8 -*-
# # # @Author  : 翟会德
# # # @Time    : 2019-06-09 11:26
# # # @File    : requests_study.py
# # # @Software: PyCharm
# # import random
# # import requests
# # import json
# # from requests import Request, Session
# # from contextlib import closing
# #
# # import config
# #
# #
# # def is_json(str_msg):
# #     try:
# #         json.loads(str_msg)
# #         return True
# #     except ValueError:
# #         return False
# #
# #
# # def better_json(msg):
# #     if type(msg) is str:
# #         if is_json(msg):
# #             return json.dumps(json.loads(msg), indent=4)
# #     elif type(msg) is dict:
# #         return json.dumps(msg, indent=4)
# #
# #
# # # result = better_json(demo1)
# # # print(result)
# #
# # # base_url = "http://httpbin.org/get"
# # # test_url = "http://example.org"
# # # photo_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1560074037993&di=7a5da8929f5e9acedba291248aa3a9d2&imgtype=0&src=http%3A%2F%2Fcdn4.freepik.com%2Fimage%2Fth%2F318-41747.jpg"
# # # headers = {
# # #     "User-Agent": random.choice(config.agents)
# # # }
# # # proxies = {
# # #     "http": "http://10.10.1.10:3128",
# # #     "https": "http://10.10.1.10:1080",
# # # }
# # # s = Session()
# # # response = s.get(test_url, headers=headers, proxies=proxies)
# # # print(response.text)
# #
# # # print(response.reason)
# # # print(response.headers)
# # # with open("./demo.jpg", "wb") as f:
# # #     response = s.get(url=photo_url, headers=headers, stream=True)
# # #     for i in response.iter_content(128):
# # #         f.write(i)
# #
# #
# # # def get_key_info(response, *args, **kwargs):
# # #     print(response.headers['Content-Type'])
# # #
# # #
# # # def main():
# # #     requests.get("https://api.github.com", hooks=dict(response=get_key_info))
# # #
# # #
# # # if __name__ == '__main__':
# # #     main()
# #
# #
# # # BASE_URL = "https://api.github.com"
# # #
# # #
# # # def construct_url(end_point):
# # #     return "/".join([BASE_URL, end_point])
# # #
# # #
# # # def basic_auth():
# # #     # 基本认证
# # #     response = requests.get(construct_url('user'), auth=("imoocdemo", "imoocdemo123"))
# # #     print(response.text)
# # #     # print(response.request.headers)
# #
# # import time
# # from functools import reduce
# #
# #
# # def performance(f):
# #     def fn(*args, **kw):
# #         t1 = time.time()
# #         r = f(*args, **kw)
# #         t2 = time.time()
# #         print('调用 %s()方法共用时：%fs' % (f.__name__, (t2 - t1)))
# #         return r
# #
# #     return fn
# #
# #
# # @performance
# # def factorial(n):
# #     return reduce(lambda x, y: x * y, range(1, n + 1))
# #
# #
# # print(factorial(10))
# import random
#
# # demo = [random.randint(1, 11) for _ in range(10)]
# # for i in demo:
# #     if i == 6:
# #         print("存在")
# #         break
# # else:
# #     print("不存在")
#
#
# from queue import Queue
# from threading import Thread
#
# # 用来表示终止的特殊对象
# _sentinel = object()
#
#
# # A thread that produces data
# def producer(out_q):
#     for i in range(10):
#         print("生产")
#         out_q.put(i)
#     out_q.put(_sentinel)
#
#
# # A thread that consumes data
# def consumer(in_q):
#     while True:
#         data = in_q.get()
#         if data is _sentinel:
#             in_q.put(_sentinel)
#             break
#         else:
#             print("消费", data)
#
#
# # Create the shared queue and launch both threads
# q = Queue()
# t1 = Thread(target=consumer, args=(q,))
# t2 = Thread(target=producer, args=(q,))
# t1.start()
# t2.start()

# from queue import Queue
#
# q = Queue()
# for i in range(1, 11):
#     q.put(i)
#
# for _ in range(5):
#     print(q.get())

from threading import Thread
from queue import Queue
import time
from selenium import webdriver

q = Queue()

def tools():
    driver = webdriver.Chrome()
    driver.get("https://www.baidu.com")
    driver.maximize_window()
    time.sleep(3)
    driver.quit()

for _ in range(10):
    t = Thread(target=tools, args=())
    q.put(t)

while not q.empty():
    if q.qsize() == 5:
        break
    t = q.get()
    t.start()


# Introdução às Generators functions em Python
# generator = (n for n in range(1000000))

# def generator(n=0, maximum=10):
#     yield 1    # Pausar
#     print('Continuando..')
#     yield 2    # Pausar
#     print('Mais uma vez..')
#     yield 3    # Pausar
#     print('Terminou')
#     return 'Acabou'

def generator(n=0, maximum=10):
    while True:
        yield n
        n += 1
        if n >= maximum:
            return

gen = generator(n=5, maximum=8)

for n in gen:
    print(n)


from django import forms
from .models import Receipt

class ReceiptForm(forms.ModelForm):
    is_favorite = forms.BooleanField(required=False)

    class Meta:
        model = Receipt
        fields = ('name', 'type', 'cooking_time', 'img', 'instruction', 'user', 'is_favorite')

    img = forms.ImageField(required=False, widget=forms.FileInput)


from typing import Any, Dict, List, Union

from langchain_core.messages import BaseMessage, get_buffer_string

from langchain.memory.chat_memory import BaseChatMemory

class ConversationBufferWindowMemory(BaseChatMemory):
    """Buffer for storing conversation memory inside a limited size window."""

    human_prefix: str = "Human"
    ai_prefix: str = "AI"
    memory_key: str = "history"  #: :meta private:
    k: int = 5
    """Number of messages to store in buffer."""

    @property
    def buffer(self) -> Union[str, List[BaseMessage]]:
        """String buffer of memory."""
        return self.buffer_as_messages if self.return_messages else self.buffer_as_str

    @property
    def buffer_as_str(self) -> str:
        """Exposes the buffer as a string in case return_messages is True."""
        messages = self.chat_memory.messages[-self.k * 2 :] if self.k > 0 else []
        return get_buffer_string(
            messages,
            human_prefix=self.human_prefix,
            ai_prefix=self.ai_prefix,
        )

    @property
    def buffer_as_messages(self) -> List[BaseMessage]:
        """Exposes the buffer as a list of messages in case return_messages is False."""
        return self.chat_memory.messages[-self.k * 2 :] if self.k > 0 else []

    @property
    def memory_variables(self) -> List[str]:
        """Will always return list of memory variables.

        :meta private:
        """
        return [self.memory_key]

    def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
        """Return history buffer."""
        return {self.memory_key: self.buffer}


from google.cloud import bigquery
client = bigquery.Client()

query = (
    "SELECT timestamp FROM `bigquery-public-data.crypto_bitcoin.blocks` ORDER BY timestamp ASC LIMIT 10"
)
query_job = client.query(
    query,
    # Location must match that of the dataset(s) referenced in the query.
    location="US",
)

# SELECT
#   `hash`,
#   input_count,
#   output_count,
#   input_value,
#   output_value,
#   fee,
#   i.addresses as ia,
#   o.addresses as oa,
#   i.value as iv,
#   o.value as ov
# FROM
#   `bigquery-public-data.crypto_bitcoin.transactions`, UNNEST(inputs) as i, UNNEST(outputs) as o
# WHERE
#   block_hash = '000000000000000000d6cdda66edb65e8969566ca65fe3c2a7470e553439c0b0'
# ORDER BY `hash` ASC

for row in query_job:
    print(row)


#!/home/francisco/Projects/Pycharm/py-binary-trees-draw/venv/bin/python

from node import Node

class BinaryTree:
    def __init__(self):
        self.root = None
        self.leaf = Node(None)
        self.nodes_dict = {}

    def insert(self, key):
        """
        Insert key values in tree.
        :param key:
        :return: self.nodes_dict a dict where keys are tuple (parent, height) and values are the children.
        """
        node = Node(key)
        node.left = self.leaf
        node.right = self.leaf

        if not self.root:
            self.root = node
        else:
            current = self.root
            parent = current
            while current != self.leaf:
                parent = current
                node.height += 1
                if node.key < current.key:
                    current = current.left
                elif node.key > current.key:
                    current = current.right
                else:
                    return False

            node.parent = parent

            if not ((node.parent.key, node.height) in self.nodes_dict):
                self.nodes_dict[(node.parent.key, node.height)] = [None, None]

            if node.key < parent.key:
                parent.left = node
                self.nodes_dict[(node.parent.key, node.height)][0] = node.key
            else:
                parent.right = node
                self.nodes_dict[(node.parent.key, node.height)][1] = node.key

    def walk_in_order(self, node=None):
        """
        Walking tree in pre-order.
        :param node: node object.
        """
        if not node:
            node = self.root

        if node != self.leaf:

            self.walk_in_order(node.left)
            if node.parent:
                print('{0}\t{1}\t{2}\t{3}\t{4}'.format(node.key, node.parent.key, node.left.key, node.right.key,
                                                       node.height))
            else:
                print('{0}\t{1}\t{2}\t{3}\t{4}'.format(node.key, None, node.left.key, node.right.key, node.height))
            self.walk_in_order(node.right)

    def walk_pos_order(self, node=None):
        """
        Walking tree in pos-order.
        :param node: node object.
        """
        if not node:
            node = self.root

        if node != self.leaf:

            self.walk_pos_order(node.right)
            if node.parent:
                print('{0}\t{1}\t{2}\t{3}'.format(node.key, node.parent.key, node.left.key, node.right.key))
            else:
                print('{0}\t{1}\t{2}\t{3}'.format(node.key, None, node.left.key, node.right.key))
            self.walk_pos_order(node.left)

    def search_node(self, value):
        """
        Search the node object that key is equal for given value.
        :param value: numeric.
        :return: node object.
                """
        current = self.root
        if value == current.key:
            return self.root

        while value != current.key and current != self.leaf:
            if current.key > value:
                current = current.left
            else:
                current = current.right

        if current == self.leaf:
            return False

        return current

    def search_children(self, node, children_list, flag=False):

        if node != self.leaf:
            self.search_children(node.right, children_list, True)
            if flag:
                children_list.append(node)
            self.search_children(node.left, children_list, True)

        return children_list

    def minimum(self, node=None):
        """
        Search the minimum key in subtree that start from given node.
        :param node:
        :return: node object.
        """
        if not node:
            node = self.root

        while node.left != self.leaf:
            node = node.left

        return node

    def maximum(self, node=None):
        """
        Search the maximum key in subtree that start from given node.
        :param node:
        :return: node object.
        """
        if not node:
            node = self.root

        while node.right != self.leaf:
            node = node.right

        return node

    def successor(self, value):
        """
        Find the largest value in the tree directly above the given value.
        :param value: numeric.
        :return: object node.
        """
        current = self.search_node(value)
        if not current:
            return False
        elif current.right != self.leaf:
            node = self.minimum(current.right)
            return node

        node = current.parent
        while node and current == node.right:
            current = node
            node = current.parent

        if not node:
            return self.maximum()

        return node

    def predecessor(self, value):
        """
        It finds in the tree the lowest value directly below the given number.
        :param value: numeric.
        :return: node object.
        """
        current = self.search_node(value)
        if not current:
            return False
        elif current.left != self.leaf:
            node = self.maximum(current.left)
            return node

        node = current.parent
        while node and current == node.left:
            current = node
            node = current.parent

        if not node:
            return self.minimum()

        return node

    def remove(self, value):
        """
        Remove node where key is equal of given value.
        :param value: numeric
        """
        node = self.search_node(value)
        if not node:
            return False
        if node == self.root:
            return self._remove_root()
        elif node.left == self.leaf and node.right == self.leaf:
            return self._remove_if_leaf(node)
        elif (node.left == self.leaf) ^ (node.right == self.leaf):
            return self._remove_if_one_child(node)
        else:
            return self._remove_if_two_children(node)

    def _remove_if_leaf(self, node):
        remove_key = node.key
        if node.parent.left == node:
            node.parent.left = self.leaf
        else:
            node.parent.right = self.leaf

        if node.parent.left.key:
            self.nodes_dict[node.parent.key, node.height] = [node.parent.left.key, None]
        elif node.parent.right.key:
            self.nodes_dict[node.parent.key, node.height] = [None, node.parent.right.key]
        else:
            del self.nodes_dict[node.parent.key, node.height]

        del node

        return remove_key, None

    def _remove_if_one_child(self, node):
        remove_key = node.key
        self._recovery_children_height(node)

        left, right = self.nodes_dict[(node.parent.key, node.height)]

        # replace the node with your child
        # reset node key by new key in nodes dict
        if node.parent.left == node:
            # tree remove
            if node.right == self.leaf:
                node.parent.left = node.left
                self.nodes_dict[(node.parent.key, node.height)] = [node.left.key, right]
            else:
                node.parent.left = node.right
                self.nodes_dict[(node.parent.key, node.height)] = [node.right.key, right]
        else:
            # tree remove
            if node.right == self.leaf:
                node.parent.right = node.left
                self.nodes_dict[(node.parent.key, node.height)] = [left, node.left.key]
            else:
                node.parent.right = node.right
                self.nodes_dict[(node.parent.key, node.height)] = [left, node.right.key]

        node.left.parent = node.parent
        node.right.parent = node.parent

        del node

        self.arrange_nodes_dict()

        return remove_key, None

    def _remove_if_two_children(self, node):
        remove_key = node.key
        successor = self.successor(node.key)

        if successor == node.right:  # case 1
            self._recover_nd_rm_two_children_case1(node, successor)
            if node == node.parent.left:
                node.parent.left = successor
            else:
                node.parent.right = successor

            successor.parent = node.parent
            successor.left = node.left
            successor.left.parent = successor
        else:  # case 2
            self._recover_nd_rm_two_children_case2(node, successor)
            if node == node.parent.left:
                node.parent.left = successor
            else:
                node.parent.right = successor

            successor.parent.left = successor.right
            successor.left = node.left
            successor.right = node.right

            node.right.parent = successor
            node.left.parent = successor
            successor.parent = node.parent

        del node

        return remove_key, successor.key

    def _remove_root(self):
        remove_key = self.root.key
        if self.root.left == self.leaf and self.root.right == self.leaf:
            self.root = None
            self.nodes_dict = {}

            return remove_key, None
        elif (self.root.left == self.leaf) ^ (self.root.right == self.leaf):
            self._recovery_children_height(self.root)
            self.arrange_nodes_dict()
            if self.root.left != self.leaf:
                self.root = self.root.left
            else:
                self.root = self.root.right

            self.root.parent = None

            return remove_key, None
        else:
            successor = self.successor(self.root.key)
            if successor == self.root.right:
                self._recover_nd_rm_two_ch_case1_root(successor)
                successor.parent = None
                successor.left = self.root.left
                self.root.left.parent = successor
                self.root = successor
            else:
                self._recover_nd_rm_two_ch_case2_root(successor)
                if successor.right:
                    successor.right.parent = successor.parent

                successor.parent.left = successor.right
                successor.left = self.root.left
                successor.right = self.root.right

                self.root.left.parent = successor
                self.root.right.parent = successor
                successor.parent = None
                self.root = successor

            return remove_key, successor.key

    def _recover_nd_rm_two_children_case1(self, node, successor):
        left, right = self.nodes_dict[(node.parent.key, node.height)]
        if node == node.parent.left:
            self.nodes_dict[(node.parent.key, node.height)] = [successor.key, right]
        else:
            self.nodes_dict[(node.parent.key, node.height)] = [left, successor.key]

        # in nodes we have to del where remove node is key
        del self.nodes_dict[(node.key, node.height + 1)]

        # in nodes dict we have to recreate where successor is key and delete the old key
        # but we have to test if successor is a leaf when that key don't exist
        if (successor.key, successor.height + 1) in self.nodes_dict:
            del self.nodes_dict[(successor.key, successor.height + 1)]
            self.nodes_dict[(successor.key, node.height + 1)] = [node.left.key, successor.right.key]
        elif node.left != self.leaf:
            self.nodes_dict[(successor.key, node.height + 1)] = [node.left.key, None]

        # reset node class heights
        successor.height -= 1

        self._recovery_children_height(successor)

        self.arrange_nodes_dict()

    def _recover_nd_rm_two_children_case2(self, node, successor):
        left, right = self.nodes_dict[(node.parent.key, node.height)]
        if node == node.parent.left:
            self.nodes_dict[(node.parent.key, node.height)] = [successor.key, right]
        else:
            self.nodes_dict[(node.parent.key, node.height)] = [left, successor.key]

        self.nodes_dict[(successor.key, node.height + 1)] = [node.left.key, node.right.key]
        del self.nodes_dict[(node.key, node.height + 1)]

        if successor.right != self.leaf and successor.parent.right != self.leaf:
            self.nodes_dict[(successor.parent.key, successor.height)] = [successor.right.key,
                                                                         successor.parent.right.key]
        elif successor.right != self.leaf and successor.parent.right == self.leaf:
            self.nodes_dict[(successor.parent.key, successor.height)] = [successor.right.key, None]
        elif successor.right == self.leaf and successor.parent.right != self.leaf:
            self.nodes_dict[(successor.parent.key, successor.height)] = [None, successor.parent.right.key]
        else:
            del self.nodes_dict[(successor.parent.key, successor.height)]

        # the successor replaces the node so it receives the height of the node
        successor.height = node.height

        self._recovery_children_height(successor)

        self.arrange_nodes_dict()

    def _recover_nd_rm_two_ch_case1_root(self, successor):
        right = successor.right
        left = self.root.left

        if left != self.leaf and right != self.leaf:
            self.nodes_dict[(successor.key, 1)] = [left.key, right.key]
        elif left != self.leaf and right == self.leaf:
            self.nodes_dict[(successor.key, 1)] = [left.key, None]
        elif left == self.leaf and right != self.leaf:
            self.nodes_dict[(successor.parent.key, successor.height)] = [None, successor.right.key]
        else:
            self.nodes_dict[(successor.key, 1)] = [None, None]

        del self.nodes_dict[self.root.key, 1]

        successor.height -= 1

        self._recovery_children_height(successor)

        self.arrange_nodes_dict()

    def _recover_nd_rm_two_ch_case2_root(self, successor):
        left, right = self.nodes_dict[self.root.key, 1]
        if self.root.left != self.leaf:
            self.nodes_dict[(successor.key, 1)] = [left, self.root.right.key]
        else:
            self.nodes_dict[(successor.key, 1)] = [None, self.root.right.key]

        if successor.right != self.leaf and successor.parent.right != self.leaf:
            self.nodes_dict[(successor.parent.key, successor.height)] = [successor.right.key,
                                                                         successor.parent.right.key]
        elif successor.right != self.leaf and successor.parent.right == self.leaf:
            self.nodes_dict[(successor.parent.key, successor.height)] = [successor.right.key, None]
        elif successor.right == self.leaf and successor.parent.right != self.leaf:
            self.nodes_dict[(successor.parent.key, successor.height)] = [None, successor.parent.right.key]
        else:
            del self.nodes_dict[(successor.parent.key, successor.height)]

        del self.nodes_dict[self.root.key, 1]

        successor.height -= 1

        self._recovery_children_height(successor)

        self.arrange_nodes_dict()

    def _recovery_children_height(self, node):
        children = self.search_children(node, list())
        keys = set()

        for child in children:
            # create a key set with children of node
            # we have do to this before we reset node key by new key in nodes dict
            if (child.parent.key, child.height) in self.nodes_dict:
                keys.add((child.parent.key, child.height))
            # reset the height in node class
            child.height -= 1

        # reset keys for children of node by new keys with new heights
        for key in keys:
            node_key, height = key
            if node_key == node.key:
                del self.nodes_dict[key]
            else:
                left, right = self.nodes_dict[key]
                del self.nodes_dict[key]
                self.nodes_dict[(node_key, height - 1)] = [left, right]

    def arrange_nodes_dict(self):
        aux1 = sorted(self.nodes_dict.keys(), key=lambda key: key[1])
        aux2 = {}
        for key in aux1:
            aux2[key] = self.nodes_dict[key]
        self.nodes_dict = aux2

if __name__ == '__main__':
    bt = BinaryTree()
    print('node\tparent\tleft\tright\theight\tfb')
    print('***********************************************')
    bt.insert(11)
    bt.insert(2)
    bt.insert(14)
    bt.insert(1)
    bt.insert(7)
    bt.insert(15)
    bt.insert(5)
    bt.insert(8)
    bt.insert(4)
    bt.walk_in_order()
    # print('***********************************************')
    # print(bt.nodes_dict)
    # print('***********************************************')
    # bt.remove(50)
    # print('***********************************************')
    # print('node\tparent\tleft\tright\theight\tfb')
    # print('***********************************************')
    # bt.walk_in_order()
    # print('***********************************************')
    # print(bt.nodes_dict)
    # print('***********************************************')


input("Learn_python" + " " + "Press Enter to continue...")

#   этот код полностью написан с помощью github copilot!!!
# write a program that asks for two numbers and prints the bigger one (without using if)
def bigger_number(a, b):
    if a > b:
        return a
    else:
        return b

# write a function that takes a list of numbers and returns the sum of the biggest and the smallest number
def sum_biggest_and_smallest(numbers):
    for number in numbers:
        if number > numbers[0]:
            numbers[0] = number
    print(numbers[0])

    for number in numbers:
        if number < numbers[1]:
            numbers[1] = number
    print(numbers[1])

    return numbers[0] + numbers[1]

print(sum_biggest_and_smallest(numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))


from open_mysql_project import openproject
import sys

if __name__ == "__main__":
    conn, cur = openproject()

    cur.execute("SELECT x.pipelineID from datareduction as x join object as y using(objectID) where y.type='OBJECT';")
    rows = cur.fetchall()
    ppid = [i[0] for i in rows]

    notreduced = []
    for i in range(len(ppid)):
        cur.execute("SELECT * from telluriccorrection where pipelineIDobj='%s';" % ppid[i])
        rows = cur.fetchall()
        if rows == []:
            notreduced.append(ppid[i])
            print(ppid[i])

    conn.close()


from flask import Flask, render_template
import pandas as pd
import matplotlib.pyplot as plt
from io import BytesIO
import base64
import matplotlib
from helper import *

matplotlib.use('Agg')

app = Flask(__name__)

data = load_data()

@app.route("/")
def index():
	# generate value for cards

	percent_fraud = float(pd.crosstab(index=data['fraud_reported'],columns='count',normalize=True).loc['Y']*100)
	fraud_loss = data.groupby('fraud_reported').sum()['total_claim_amount'].loc['Y']
	average_claim = data.groupby('fraud_reported').median()['total_claim_amount'].loc['Y']

	# compile card values as card_data

	card_data = dict(
		percent_fraud = f'{percent_fraud}%',
		fraud_loss = f'US$ {fraud_loss:,}',
		average_claim = f'US$ {average_claim:,}'
	)

	# generate plot
	plot_age_res = plot_age(data)
	plot_premium_res = plot_premium(data)
	plot_incident_res = plot_incident(data)
	plot_report_res = plot_report(data)
	plot_hourgroup_res = plot_hourgroup(data)
	plot_incidenthour_res = plot_incidenthour(data)

	# render to html
	return render_template('index.html',
		card_data = card_data,
		plot_age_res=plot_age_res,
		plot_premium_res=plot_premium_res,
		plot_incident_res=plot_incident_res,
		plot_report_res=plot_report_res,
		plot_hourgroup_res=plot_hourgroup_res,
		plot_incidenthour_res=plot_incidenthour_res
		)

if __name__ == "__main__":
    app.run(debug=True)


from Tools.scripts.treesync import raw_input

# 输入用户名的例子
# 要注意 raw_input()的时候，空格也是判定非空的，要strip

name = ''
while not name:
    name = input('please enter your name:')
print('name is : ' + name.strip())

strings = "abcdefghi"
for string in strings:
    # if 'abc' in string:
    print(string)

for index, string in enumerate(strings):
    if "abc" in string:
        print(index)

for i in range(1, 100, 1):
    print('not')
    if i == 50:
        print(i)
        # 此处break会导致else执行不到
        break
else:
    print("got?")

# 列表推导式
a = [x ** 2 for x in range(10) if x % 2 == 0]
print('列表推导：' + str(a))

# pass: 空代码块是非法的，用pass占位
# del：删除不再使用的对象，删除名称，解释器会负责内存的回收


from docx import Document
import os

dict = {}
for filename in os.listdir("documents"):
    if filename.endswith(".docx") :
        print("reading :" + filename)
        document = Document("documents/" + filename)

        docText = '\n\n'.join([
            paragraph.text for paragraph in document.paragraphs
        ])
        #print(docText)

        words = docText.split()
        for word in words:
            if (word in dict.keys()):
                dict[word] = dict[word] + 1
            else:
                dict[word] = 1

        #for word in dict.keys():
           # print(word + ":" + "\n")
           #print(dict[word])

        k = 0
    else:
        continue

s = [(k, dict[k]) for k in sorted(dict, key=dict.get, reverse=True)]
for k, v in s:
    print(k, v)


import meta_ultra.config as config
from meta_ultra.utils import *
from meta_ultra.modules import *

class BWAModule( Module):
	def __init__(self, **kwargs):
		super(BWAModule, self).__init__(**kwargs)

	def buildConf(self, confBuilder):
		bwa = confBuilder.addModule(self.moduleName().upper())
		if not bwa:
			return

		bwa.add_field('EXC',
				  UserChoice('BWA',
					     self.tools,
					     new=lambda :self.askUserForTool('bwa')
				  ))
	@staticmethod
	def moduleName():
		return 'bwa'

modules.append(BWAModule)


import tensorflow as tf
import pickle_utils as pu
import itertools as it
import numpy as np
import collections
import os
import sys
from tqdm import tqdm
import common

sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                '..', 'GRUD-baseline'))
from read_tfrecords import build_input_machinery

flags = tf.app.flags
flags.DEFINE_string('dataset', '', 'The file to use as input dataset')
flags.DEFINE_string('command', 'GatherInterpolationInfo',
                    'The command to perform')
flags.DEFINE_integer('batch_size', 64, 'batch size')
del flags
FLAGS = tf.app.flags.FLAGS

class GatherInterpolationInfo:
    keys = ['numerical_ts', 'categorical_ts', 'length']

    def __init__(self, dataset):
        self.dataset = dataset
        self.num_shape = [FLAGS.batch_size, int(self.dataset['numerical_ts']
                                          .get_shape()[2])]
        self.last_num_ts = np.empty(self.num_shape, dtype=np.float32)
        self.num_ts_dt = np.zeros(self.num_shape, dtype=np.int)

        self.cat_shape = [FLAGS.batch_size, int(self.dataset['categorical_ts']
                                          .get_shape()[2])]
        self.last_cat_ts = -np.ones(self.cat_shape, dtype=np.int32)  # all -1
        self.cat_ts_dt = np.zeros(self.cat_shape, dtype=np.int)

        self.num_interpolate_X = list([] for _ in range(self.num_shape[1]))
        self.num_interpolate_y = list([] for _ in range(self.num_shape[1]))
        self.cat_interpolate_X = list([] for _ in range(self.cat_shape[1]))
        self.cat_interpolate_y = list([] for _ in range(self.cat_shape[1]))

        self.category_counts = list(collections.Counter()
                                    for _ in range(self.cat_shape[1]))
        self.numerical_averages = np.zeros(self.num_shape[1], dtype=np.float32)
        self.numerical_averages_counts = np.zeros(self.num_shape[1],
                                                  dtype=np.int)
        self.numerical_averages_counts_2 = np.zeros(self.num_shape[1],
                                                    dtype=np.int)

    def process(self, data):
        numerical_ts, categorical_ts, length = map(data.__getitem__,
                                                   self.keys)
        for feature_i in range(self.cat_shape[1]):
            for j, l in enumerate(length):
                self.category_counts[feature_i].update(
                    categorical_ts[j,:l,feature_i].flatten())

        if numerical_ts.shape[0] < self.num_shape[0]:
            # Pad so that the first dimension is always FLAGS.batch_size
            numerical_ts = np.pad(
                numerical_ts,
                [(0, self.num_shape[0]-numerical_ts.shape[0]), (0, 0), (0, 0)],
                'constant', constant_values=np.nan)
            categorical_ts = np.pad(
                categorical_ts,
                [(0, self.cat_shape[0]-categorical_ts.shape[0]), (0, 0),
                 (0, 0)], 'constant', constant_values=-1)
            length = np.pad(
                length,
                [(0, self.num_shape[0]-length.shape[0])],
                'constant', constant_values=0)

        self.last_num_ts[:] = np.nan
        self.num_ts_dt[:] = 0
        self.last_cat_ts[:] = -1
        self.cat_ts_dt[:] = 0
        for t in range(numerical_ts.shape[1]):
            num_ts = numerical_ts[:,t,:]
            cat_ts = categorical_ts[:,t,:]
            assert num_ts.shape[1] == self.num_shape[1]
            assert cat_ts.shape[1] == self.cat_shape[1]

            is_in_range = (t < length)[:,None]
            num_is_valid = is_in_range * (~np.isnan(num_ts))

            for ex, f in zip(*num_is_valid.nonzero()):
                n = self.last_num_ts[ex,f]
                if not np.isnan(n):
                    self.num_interpolate_X[f].append((n, self.num_ts_dt[ex,f]))
                    self.num_interpolate_y[f].append((num_ts[ex,f]))
                self.numerical_averages[f] += num_ts[ex,f]
                self.numerical_averages_counts_2[f] += 1
            self.numerical_averages_counts += np.sum(num_is_valid, axis=0)

            cat_is_valid = is_in_range * (cat_ts >= 0)
            for ex, f in zip(*cat_is_valid.nonzero()):
                n = self.last_cat_ts[ex,f]
                if n >= 0:
                    self.cat_interpolate_X[f].append((n, self.cat_ts_dt[ex,f]))
                    self.cat_interpolate_y[f].append((cat_ts[ex,f]))

            self.num_ts_dt += 1
            self.num_ts_dt[num_is_valid] = 0
            self.last_num_ts[num_is_valid] = num_ts[num_is_valid]
            self.cat_ts_dt += 1
            self.cat_ts_dt[cat_is_valid] = 0
            self.last_cat_ts[cat_is_valid] = cat_ts[cat_is_valid]

    def finish(self):
        for i, niX, niY in zip(it.count(0), self.num_interpolate_X, self.num_interpolate_y):
            pu.dump((niX, niY), 'interpolation/num_{:d}.pkl.gz'.format(i))
        for i, ciX, ciY in zip(it.count(0), self.cat_interpolate_X, self.cat_interpolate_y):
            pu.dump((ciX, ciY), 'interpolation/cat_{:d}.pkl.gz'.format(i))
        nums_cats = pu.load('dataset/number_of_categories_200.pkl.gz')['categorical_ts']
        for i, (count, n_cats) in enumerate(zip(self.category_counts, nums_cats)):
            a = np.zeros(n_cats, dtype=np.int)
            print("Category", i, "counts", count)
            for j in range(n_cats):
                if j in count:
                    a[j] = count[j]
            pu.dump(a, 'interpolation/counts_cat_{:d}.pkl.gz'.format(i))
        pu.dump((self.numerical_averages, self.numerical_averages_counts_2), 'dataset/means.pkl.gz')
        assert np.all(self.numerical_averages_counts_2 == self.numerical_averages_counts)

class AddInterpolationInputs:
    keys = ['icustay_id', 'numerical_static', 'categorical_static',
            'numerical_ts_dt', 'categorical_ts_dt', 'time_until_label',
            'label', 'numerical_ts', 'categorical_ts', 'treatments_ts',
            'ventilation_ends', 'length']

    def __init__(self, dataset):
        self.dataset = dataset
        self.writer = tf.python_io.TFRecordWriter(FLAGS.dataset+'-new')
        self.writer.__enter__()
        self.imputation_writer = tf.python_io.TFRecordWriter(
            FLAGS.dataset+'-imputation')
        self.imputation_writer.__enter__()
        dataset_dir = os.path.dirname(FLAGS.dataset)
        values, counts = pu.load(os.path.join(dataset_dir, 'means.pkl.gz'))
        self.means = values/counts
        self.most_common_cats = pu.load(os.path.join(dataset_dir, 'most_common_cats.pkl.gz'))

    def process(self, data):
        num_valid = common.make_within_length(data['numerical_ts'], data['length']) * (
            ~np.isnan(data['numerical_ts']))
        data['numerical_ts_dt_all'], numerical_ts_dt_all_delayed = common.build_dt(
            data['numerical_ts'], num_valid, data['numerical_ts_dt'], also_delayed=True)
        data['numerical_ts_forward'], numerical_ts_forward_delayed = (
            common.build_impute_forward(data['numerical_ts'], num_valid,
                self.means, also_delayed=True))

        cat_valid = common.make_within_length(data['categorical_ts'], data['length']) * (
            data['categorical_ts'] >= 0)
        data['categorical_ts_dt_all'] = (
            common.build_dt(data['categorical_ts'], cat_valid,
                            data['categorical_ts_dt'], also_delayed=False))
        data['categorical_ts_forward'] = (
            common.build_impute_forward(data['categorical_ts'], cat_valid,
                                        self.most_common_cats, also_delayed=False))

        # SequenceExample for normal training
        batch_size = len(data['numerical_ts'])
        for i in range(batch_size):
            ex = common.example_from_data(dict(map(
                lambda t: (t[0], t[1][i,...] if len(t[1].shape) > 1 else t[1][i:i+1]),
                data.items())))
            self.writer.write(ex.SerializeToString())

        # Example for imputation training
        for i in range(batch_size):
            for t in range(data['length'][i]):
                if num_valid[i,t,:].any():
                    ex = tf.train.Example()
                    ntad = numerical_ts_dt_all_delayed[i,t,:].astype(np.float32)
                    assert not np.any(np.isnan(ntad))
                    common.feature(ex, 'num_ts', 'float', ntad)
                    ntfd = numerical_ts_forward_delayed[i,t,:].astype(np.float32)
                    assert not np.any(np.isnan(ntfd))
                    common.feature(ex, 'num_forward', 'float', ntfd)
                    common.feature(ex, 'num_labels', 'float',
                                   data['numerical_ts'][i,t,:])
                    self.imputation_writer.write(ex.SerializeToString())

    def finish(self):
        self.writer.__exit__(None, None, None)
        self.imputation_writer.__exit__(None, None, None)

class MostFrequentCategories(AddInterpolationInputs):
    def __init__(self, dataset):
        self.category_counters = list(collections.Counter() for _ in range(
            int(dataset['categorical_ts'].get_shape()[2])))

    def process(self, data):
        cat_ts = data['categorical_ts']
        cat_is_valid = common.make_within_length(cat_ts, data['length']) * (cat_ts >= 0)
        for i, counter in enumerate(self.category_counters):
            counter.update(cat_ts[cat_is_valid[:,:,i],i])

    def finish(self):
        most_common_cats = np.zeros([len(self.category_counters)], dtype=np.int)
        for i, counter in enumerate(self.category_counters):
            most_common_cats[i] = counter.most_common(1)[0][0]
        pu.dump(most_common_cats, os.path.join(
            os.path.dirname(FLAGS.dataset), 'most_common_cats.pkl.gz'))

class MostFrequentCategories(AddInterpolationInputs):
    def __init__(self, dataset):
        self.category_counters = list(collections.Counter() for _ in range(
            int(dataset['categorical_ts'].get_shape()[2])))

    def process(self, data):
        cat_ts = data['categorical_ts']
        cat_is_valid = self.make_within_length(cat_ts, data['length']) * (cat_ts >= 0)
        for i, counter in enumerate(self.category_counters):
            counter.update(cat_ts[cat_is_valid[:,:,i],i])

    def finish(self):
        most_common_cats = np.zeros([len(self.category_counters)], dtype=np.int)
        for i, counter in enumerate(self.category_counters):
            most_common_cats[i] = counter.most_common(1)[0][0]
        pu.dump(most_common_cats, os.path.join(
            os.path.dirname(FLAGS.dataset), 'most_common_cats.pkl.gz'))

def main(_):
    assert os.path.isfile(FLAGS.dataset)

    if not os.path.exists('interpolation'):
        os.mkdir('interpolation')
    print("Will write files to ./interpolation")

    feature_numbers = pu.load(os.path.join(
        os.path.dirname(FLAGS.dataset), 'feature_numbers.pkl.gz'))
    Klass = globals()[FLAGS.command]
    dataset = build_input_machinery([FLAGS.dataset], feature_numbers, False, 1,
                                    FLAGS.batch_size, None, 1, keys=Klass.keys)
    klass = Klass(dataset)

    config = tf.ConfigProto(device_count = {'GPU': 0})
    with tf.Session(config=config) as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        queue_threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        progress_bar = tqdm()
        try:
            while not coord.should_stop():
                data = dict(zip(klass.keys,
                                sess.run(list(map(dataset.__getitem__, klass.keys)))))
                n_processed = data['numerical_ts'].shape[0]
                klass.process(data)
                progress_bar.update(n=n_processed)
        except tf.errors.OutOfRangeError:
            pass

        klass.finish()

        coord.request_stop()
        coord.join(queue_threads)

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

import unittest
class TestAddInterpolation(unittest.TestCase):
    def test_build_dt(self):
        a = np.array([[[0, 1],
                       [0, 0],
                       [1, 0]],
                      [[1, 1],
                       [1, 0],
                       [0, 0]]])
        self.assertTrue(np.all(AddInterpolationInputs.build_dt(a, a==0) ==
                         np.array([[[0, 1000],
                                    [0, 0],
                                    [1, 0]],
                                   [[1000, 1000],
                                    [1001, 0],
                                    [0, 0]]])))


''' to get the largest number from a list.
to sum all the items in a list.
to multiply all the items in a list.'''
marks=[10,20,30,70,5]
Mul=1
marks.sort()#sort garera ascending order ma data lai rakhxa jasle garda highest vale vako data last ma hunxa
print(marks[len(marks)-1])#its because list ko data ko numbering 0 dekhi suru hunxa
i=0
Sum=0
for item in marks:
    Mul=Mul*item
    Sum=Sum+item
print(Mul)
print(Sum)


import numpy as np
import matplotlib.pyplot as plt

from mpl_toolkits import mplot3d

class Model:

    def __init__(self, converter, policies, mutator, crossover, scaler=None):
        self._converter = converter
        self._policies = policies
        self._scaler = scaler
        self._mutator = mutator
        self._crossover = crossover

        self._func = None
        self._population = None
        self._arg_num = None
        self._axes = None
        self._digits = None

        self._cache = {}

    @property
    def converter(self):
        return self._converter.__name__

    @property
    def policies(self):
        include = self._policies['include'].__name__
        exclude = self._policies['exclude'].__name__
        parents = self._policies['parents'].__name__

        return 'include: {}\nexclude{}\nparents:{}'.format(include, exclude, parents)

    @property
    def scaler(self):
        return self._scaler.__name__

    @property
    def mutator(self):
        return self._mutator.__name__

    @property
    def crossover(self):
        return self._crossover.__name__

    @property
    def population(self):
        return self._population

    def run(self, func, *intervals, n=20, eps=0.01):
        """
            Run algorithm.

            func:function - function to optimize.
            intervals:[(float)] - intervals for each variable where we optimize our function.
            n:int - population volume.
            eps:float - accuracy of the algorithm.
        """

        assert self._converter is not None, 'Converter is not found.'
        assert self._policies is not None, 'Policies is not found.'
        assert self._mutator is not None, 'Mutator operator is not found.'
        assert self._crossover is not None, 'Crossover operator is not found.'

        self._func = func
        self._arg_num = func.__code__.co_argcount

        assert self._arg_num == len(intervals)

        self._axes = [list(np.arange(a, b, eps)) for a, b in intervals]

        max_len = max([len(ax) for ax in self._axes])

        self._digits = int(np.ceil(np.log2(max_len)))

        self._population = [list(np.random.choice(ax, size=n)) for ax in self._axes]

        self._population = list(zip(*self._population))

    def _to_genes(self, x, scope):
        """
        Converts x value to gene representation.

        :param x:float - value.
        :param scope:[float] - scope where x could be.
        :return:[bool] - gene representation/
        """

        x = scope.index(x)
        x = self._converter.convert(x, self._digits)

        return x

    def _to_float(self, x, scope):
        """
        Converts x gene representation to its value.

        :param x:[bool] - gene representation.
        :param scope:[float] - scope where x could be
        :return:float - value.
        """

        x = self._converter.to_int(x)
        x = min(x, len(scope) - 1)
        x = scope[x]

        return x

    def _caching(self, t):
        """
        Cache data for future ploting and other activities.

        :param t:int - time-step when we use caching.
        :return:None
        """

        self._cache[t] = self._population[:]

    def plot(self):
        """
        Plot evolution.

        :return: None
        """

        for key, value in self._cache.items():
            eps = 0.0001
            x = [key] * len(value)
            y = [self._func(*i) for i in value]
            #size = [np.sqrt(abs(i)) + eps for i in y]

            plt.scatter(x, y, label='Epoch: {}'.format(key))

        plt.xlabel('epoch')
        plt.ylabel('function value')
        plt.legend()
        plt.savefig('result.png')
        plt.show()

    def plot_graph(self):
        """
        Plot function graph depending on the number of values.

        :return: None
        """

        assert self._arg_num in (1, 2)

        if self._arg_num == 1:
            self.plot1D()
        elif self._arg_num == 2:
            self.plot2D()

    def plot1D(self):
        """
        Plot function graph.

        :return: None
        """

        assert self._arg_num == 1, 'Function must be 1-dimensional'

        x = self._axes[0]
        y = [self._func(i) for i in x]
        plt.plot(x, y)

        max_key = max(self._cache.keys())
        min_key = min(self._cache.keys())
        for key, value in self._cache.items():
            color = 1 - (key - min_key) / (max_key - min_key)
            x = [i[0] for i in value]
            y = [self._func(i) for i in x]
            plt.scatter(x, y, c=str(color), label='Epoch: {}'.format(key))

        plt.xlabel('x')
        plt.ylabel('f(x)')
        plt.legend()
        plt.show()

    def plot2D(self):
        """
        Plot function graph.

        :return: None
        """

        assert self._arg_num == 2, 'Function must 2-dimensional.'

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')

        x, y = self._axes
        X, Y = np.meshgrid(x, y)
        Z = self._func(X, Y)
        ax.plot_surface(X, Y, Z)

        max_key = max(self._cache.keys())
        min_key = min(self._cache.keys())
        for key, value in self._cache.items():
            X, Y, Z = [], [], []
            for x, y in value:
                X.append(x)
                Y.append(y)
                Z.append(self._func(x, y))

            color = 1 - (key - min_key) / (max_key - min_key)

            ax.scatter(X, Y, Z, c=str(color), label='Epoch: {}'.format(key))

        ax.set_xlabel('x')
        ax.set_ylabel('y')
        ax.set_zlabel('f(x, y)')
        ax.legend()

        plt.show()


'''
This could be improved by using networkx during part 1 instead of walking back and forth repeatedly
- may be a task for a later date.
'''
class Node():
    def __init__(self, x,y, path, closed, result, goal=False):
        self.x = x
        self.y = y
        self.path = path
        self.closed = closed
        self.result = result
        self.distance = len(path)
        self.goal = goal
        self.oxygenated = False

    def __repr__(self):
        return "({},{})N".format(self.x,self.y)

def update_location(x,y,dr):
    '''Converts intcomputer-readable movement to x,y coordinates'''
    if dr==1:
        return x,y+1
    elif dr==2:
        return x,y-1
    elif dr==3:
        return x-1,y
    elif dr==4:
        return x+1,y

def wiggle(x,y,nodes,movement_options,reverse_movement,path,brain):
    '''Uses the intcode computer brain to visit nearby locations (adjacent to x,y), and check if they are open.
    Logs their information.'''
    # wiggle (based on x,y)
    for i in range(4):
        movement = movement_options[i]
        reverse = reverse_movement[i]

        new_x,new_y = update_location(x,y,movement)

        # skip node if it's been calculated before
        # might need to change to check for shorter path later
        if (new_x,new_y) in [(node.x,node.y) for node in nodes]:
            #print('skipping {},{}'.format(new_x,new_y))
            continue

        if new_x==0 and new_y==0:
            print('wow ok')
        current_path = path+[movement]
        # try move and find out what's there
        brain.add_input(movement)
        brain.process_code()
        result = brain.outputs[-1]

        # store result (if wall, empty, or goal)
        nodes.append(Node(new_x,new_y, path=current_path, closed=(result==0),
                                                    result=result, goal=(result==2)))

        if result>0:
            # move back to starting location
            brain.add_input(reverse)
            brain.process_code()
            assert brain.outputs[-1]!=0

    return nodes

def part_1(brain):
    '''Fills out the list nodes by traveling to each node individually, then "wiggling" to visit each neighbor,
    while keeping track of position. Will continue until all locations have been tested.'''

    nodes = []
    movement_options = np.arange(1,5)
    reverse_movement = movement_options[[1,0,3,2]]

    # start at 0,0
    x,y = 0,0

    # store location
    nodes.append(Node(x,y,path=[], result=1, closed=False))

    max_distance = -1
    while max_distance!=max([node.distance for node in nodes]):
        max_distance = max([node.distance for node in nodes])

        # pick next x,y to wiggle around
        for node in nodes:
            if node.distance==max_distance and ~node.closed:
                # move to node
                for dr in node.path:
                    brain.add_input(dr)
                    result = brain.process_code()
                    assert result!=0

                # wiggle
                nodes = wiggle(node.x,node.y,nodes,movement_options,reverse_movement,node.path,brain)

                # return to start
                for dr in node.path[::-1]:
                    brain.add_input(reverse_movement[dr-1])
                    assert brain.process_code()!=0

    return nodes

def part_2(nodes, goal_node):
    '''Uses input list of nodes to create a graph in using networkx, then finds the furthest distance
    from any node in the graph to the goal node'''
    import networkx as nx

    # convert nodes to dictionary
    node_guide = {(node.x,node.y):node.result for node in nodes}

    g = nx.Graph()

    # add all edges to graph
    for x,y in node_guide.keys():
        result = node_guide[(x,y)]

        if result==0:
            continue

        if node_guide[(x-1,y)] == 1:
            g.add_edge((x,y), (x-1,y))
        if node_guide[(x+1,y)] == 1:
            g.add_edge((x,y), (x+1,y))
        if node_guide[(x,y-1)] == 1:
            g.add_edge((x,y), (x,y-1))
        if node_guide[(x,y+1)] == 1:
            g.add_edge((x,y), (x,y+1))

    # iterate through every location, finding the maximum number of steps from the oxygenated node
    # this is the number of minutes it will take for oxygen to fill the entire map
    num_minutes = 0
    for x, y in node_guide.keys():
        if node_guide[(x,y)]==0:
            continue
        path_length =  len(nx.dijkstra_path(g,(goal_node.x,goal_node.y),(x,y)))
        if path_length > num_minutes:
            num_minutes = path_length

    return num_minutes-1

def main():
    with open('day15.txt', 'r') as f:
        data = np.array(f.read().strip().split(","), dtype=np.int64)
    f.close()

    # initialize computer
    from intComputer import IntcodeComputer

    brain = IntcodeComputer(dt=data)

    # part 1
    nodes = part_1(brain)
    goal_node = [node for node in nodes if node.goal][0]
    print(goal_node.distance)

    # part 2
    print(part_2(nodes, goal_node))

if __name__=='__main__':
    import numpy as np
    main()


import cv2
from matplotlib import pyplot as plt
imgL = cv2.imread('left_img.jpg',0)
imgR = cv2.imread('right_img.jpg',0)

cv2.imshow('Image', imgL)
#cv2.waitKey(0)
#v2.destroyAllWindows()
stereo = cv2.StereoBM_create(numDisparities=48, blockSize=5)
disparity = stereo.compute(imgL,imgR)
plt.imshow(disparity,'gray')
plt.show()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: qiujiarong
# Date: 08/03/2018

from __future__ import print_function
from __future__ import absolute_import
from __future__ import division

import tensorflow as tf
import numpy as np
from PIL import Image

from video.inception_resnet_v2 import inception_resnet_v2_arg_scope, inception_resnet_v2
from audio import vggish_postprocess, vggish_slim, vggish_params, vggish_input
from video.flow_test import dense_optical_flow

slim = tf.contrib.slim

DIR_NAME = "../data/"

class RGBFeature(object):
    """"""

    def __init__(self):
        """Constructor for RGBFeature"""
        checkpoint_file = DIR_NAME + 'checkpoints/inception_resnet_v2_2016_08_30.ckpt'
        self.input_tensor = tf.placeholder(tf.float32, [None, 299, 299, 3])
        sess = tf.Session()
        arg_scope = inception_resnet_v2_arg_scope()
        with slim.arg_scope(arg_scope):
            _, end_points = inception_resnet_v2(self.input_tensor, is_training=False)
        saver = tf.train.Saver()
        saver.restore(sess, checkpoint_file)
        self.end_points = end_points
        self.sess = sess
        print("RGB Model Load In Finished")

    def images2feature(self, images):
        ret = []
        for image in images:
            im = Image.open(image).resize((299, 299))
            im = np.array(im)
            im = im.reshape(-1, 299, 299, 3)
            im = 2 * (im / 255.0) - 1.0
            drop_out_values = self.sess.run(self.end_points['PreLogitsFlatten'], feed_dict={self.input_tensor: im})
            # print(drop_out_values.shape)
            ret.append(drop_out_values.squeeze())
        return np.array(ret)

    def test(self):
        sample_images = [DIR_NAME + 'dog.jpg']
        print(self.images2feature(sample_images).shape)

class FlowFeature(object):
    """"""

    def __init__(self ):
        """Constructor for FlowFeature"""

    def images2feature(self,images):
        return dense_optical_flow(images)

    def test(self):
        sample_images = []
        for i in range(1,6):
            sample_images.append(DIR_NAME+"flow_test/"+str(i)+".jpg")
        print(sample_images)
        print(self.images2feature(images=sample_images).shape)

class AudioFeature(object):
    """"""

    def __init__(self):
        """Constructor for AudioFeature"""
        checkpoint_path = DIR_NAME + 'checkpoints/vggish_model.ckpt'
        pca_params_path = DIR_NAME + 'checkpoints/vggish_pca_params.npz'
        pproc = vggish_postprocess.Postprocessor(pca_params_path)
        sess = tf.Session()
        vggish_slim.define_vggish_slim(training=False)
        vggish_slim.load_vggish_slim_checkpoint(sess, checkpoint_path)
        features_tensor = sess.graph.get_tensor_by_name(
            vggish_params.INPUT_TENSOR_NAME)
        embedding_tensor = sess.graph.get_tensor_by_name(
            vggish_params.OUTPUT_TENSOR_NAME)
        self.embedding_tensor = embedding_tensor
        self.features_tensor = features_tensor
        self.sess = sess
        self.pproc = pproc
        print("Audio Model Load In Finished")

    def wav2feature(self, wav_file):
        examples_batch = vggish_input.wavfile_to_examples(wav_file)
        [embedding_batch] = self.sess.run([self.embedding_tensor], feed_dict={self.features_tensor: examples_batch})
        postprocessed_batch = self.pproc.postprocess(embedding_batch)
        return postprocessed_batch

    def test(self):
        wav_file = DIR_NAME + "audio_test.wav"
        print(self.wav2feature(wav_file).shape)

if __name__ == '__main__':
    # rgb=RGBFeature()
    # rgb.test()
    audio = AudioFeature()
    audio.test()
    # flow=FlowFeature()
    # flow.test()


import json

# ASCII symbol to sokoban entity
# I've assumed that the sokoban robot starts from his docking station
conversion_dict = {
	'#': 'U',
	'@': 'RD',
	'$': 'B',
	'.': 'G',
	' ': ' '
}

def convert_line(line, line_counter):
	"""Converts an ASCIIart sokoban instance to a JSON request that can be passed to the server thru curl"""
	cells = []
	# cell = collections.OrderedDict({})
	cell = {}

	for x in range(len(line)):
		cell = {'y': line_counter, 'x': x, 'entities': conversion_dict[line[x]]}
		cells.append(cell)
	return cells

def parse_ASCII(file_in):
	"""Parses a file to extract the ASCII art"""
	sokoban_instance = {'version': 1.0}
	cell_list = []

	with open(file_in, 'r') as f:
		lines = [line.rstrip('\n') for line in f]

	rows = len(lines)
	columns = len(lines[0])

	line_counter = 0

	# check walls presence at the beginning & at the end of the file
	if ''.join(lines[0]) != '#' * columns or ''.join(lines[-1]) != '#' * columns:
		raise ValueError("Unexpected file format @ line {}".format(line_counter))

	for l in lines:
		if line_counter == 0 or line_counter == len(lines) - 1:
			pass
		else:
			if len(l) > 0:
				if l[0] != '#' or l[-1] != '#':  # expecting a starting (and ending) wall for each line
					raise ValueError("Unexpected file format @ line {}".format(line_counter))
				else:
					formatted_line = l[1:len(l) - 1]  # remove walls in the line
					line2cells = convert_line(line=formatted_line, line_counter=line_counter)
					for cell in line2cells:
						cell_list.append(cell)
		line_counter += 1

	world_instance = {'rows': rows, 'columns': columns, 'cells': cell_list}
	sokoban_instance['world'] = world_instance

	return sokoban_instance

def dict_2_json(d, file_out):
	"""Write a dictionary to a file"""
	with open(file_out, 'w') as f:
		json.dump(d, f, indent=4)

# main for quick testing
# if __name__ == '__main__':
# 	instance = parse_ASCII(file_in=r'C:\Users\Nicola\Desktop\ZUMOrobot\Robotieee\Server\planner_wrapper\tests\sokoban_example2.txt')
# 	dict_2_json(d=instance, file_out=r'C:\Users\Nicola\Desktop\ZUMOrobot\Robotieee\Server\planner_wrapper\tests\sokoban_out.json')


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

import chainer
from chainer import cuda

import csv
import sys
import time
import random
import copy
import math
import os
import numpy as np
import argparse
import configparser

sys.path.append(os.getcwd())
from src.lib.trainer import NSNTrainer, NDNTrainer
from src.lib.utils import createOpbase
from src.lib.utils import create_dataset_parser, create_model_parser, create_runtime_parser
from src.lib.utils import print_args
from src.lib.utils import get_dataset, get_model

def main():

    """ Implementation of Quantitative Criteria Acquisition Network based on Chainer """
    start_time = time.time()

    ''' ConfigParser '''
    conf_parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
        add_help=False
    )
    conf_parser.add_argument("-c", "--conf_file",
                             help="Specify config file", metavar="FILE_PATH")
    args, remaining_argv = conf_parser.parse_known_args()

    dataset_conf_dict, model_conf_dict, runtime_conf_dict = {}, {}, {}

    if args.conf_file is not None:
        config = configparser.ConfigParser()
        config.read([args.conf_file])
        dataset_conf_dict = dict(config.items("Dataset"))
        model_conf_dict = dict(config.items("Model"))
        runtime_conf_dict = dict(config.items("Runtime"))

    ''' Parameters '''
    dataset_parser, dataset_args, remaining_argv = \
        create_dataset_parser(remaining_argv, **dataset_conf_dict)
    model_parser, model_args, remaining_argv = \
        create_model_parser(remaining_argv, **model_conf_dict)
    runtime_parser, runtime_args, remaining_argv = \
        create_runtime_parser(remaining_argv, **runtime_conf_dict)

    ap = argparse.ArgumentParser(
        description='Learning Nuclear Segmentation Network',
        parents=[conf_parser, dataset_parser, model_parser, runtime_parser])
    ap.add_argument('--gpu_id', type=int, help='GPU option')
    args = ap.parse_args()

    patchsize = args.patch_size
    opbase = createOpbase(args.save_dir)
    psep = '/'
    print_args(dataset_args, model_args, runtime_args)
    #with open(opbase + psep + 'result.txt', 'w') as f:
    #    f.write(print_args(dataset_args, model_args, runtime_args))

    ''' Dataset '''
    print('Loading datasets...')
    train_dataset, validation_dataset = get_dataset(args)
    print('-- train_dataset.size = {}\n-- validation_dataset.size = {}'.format(
        len(train_dataset), len(validation_dataset)))

    ''' Iterator '''
    train_iterator = chainer.iterators.SerialIterator(
        train_dataset, int(args.batchsize), repeat=True, shuffle=True
    )
    validation_iterator = chainer.iterators.SerialIterator(
        validation_dataset, int(args.batchsize), repeat=True, shuffle=False
    )

    ''' Model '''
    print('Initializing models...')
    model = get_model(args)
    if args.init_model is not None:
        print('Load model from', args.init_model)
        try:
            chainer.serializers.load_npz(args.init_model, model)
        except:
            chainer.serializers.load_hdf5(args.init_model, model)
    if args.gpu >= 0:
        cuda.get_device(args.gpu).use()  # Make a specified GPU current
        model.to_gpu()  # Copy the SegmentNucleus model to the GPU

    ''' Training Phase '''
    with open(opbase + psep + 'TestResult.csv', 'w') as f:
        c = csv.writer(f)
        c.writerow(['Epoch', 'Accuracy', 'Recall', 'Precision', 'Specificity', 'F-measure', 'IoU'])
    with open(opbase + psep + 'TrainResult.csv', 'w') as f:
        c = csv.writer(f)
        c.writerow(['Epoch', 'Accuracy', 'Recall', 'Precision', 'Specificity', 'F-measure', 'IoU'])

    mean_image = None
    if args.model == 'NSN' or args.model == '3DUNet':
        trainer = NSNTrainer(
            model=model,
            epoch=args.epoch,
            patchsize=eval(args.patch_size),
            batchsize=args.batchsize,
            gpu=args.gpu,
            opbase=opbase,
            mean_image=mean_image,
            opt_method=args.optimizer,
            ndim=args.ndim
        )
    elif args.model == 'NDN':
        trainer = NDNTrainer(
            model=model,
            epoch=args.epoch,
            patchsize=eval(args.patch_size),
            batchsize=args.batchsize,
            gpu=args.gpu,
            opbase=opbase,
            mean_image=mean_image,
            opt_method=args.optimizer,
            delv=3,
            r_thr=10,
            ndim=args.ndim
        )
    train_eval, test_eval, best_score = trainer.training((train_iterator, validation_iterator))

    end_time = time.time()
    process_time = end_time - start_time
    print('Elapsed time is (sec) {}'.format(process_time))
    with open(opbase + psep + 'result.txt', 'a') as f:
        f.write('======================================\n')
        f.write('Elapsed time is (sec) {} \n'.format(process_time))

if __name__ == '__main__':
    main()


#!/usr/bin/env python
# coding: utf-8

# In[1]:

# Importing all libraries required in this notebook
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')

# In[2]:

link= "http://bit.ly/w-data"
data=pd.read_csv(link)

# In[3]:

print("Data imported successfully")
data.head(10)

# In[5]:

# Plotting the distribution of scores
data.plot(x='Hours', y='Scores', style='o')
plt.title('Hours vs Percentage')
plt.xlabel('Hours Studied')
plt.ylabel('Percentage Score')
plt.show()

# In[6]:

x=data.iloc[:,:-1].values
y=data.iloc[:,1].values

# In[7]:

x

# In[8]:

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)

# In[9]:

x_test

# In[10]:

from sklearn.linear_model import LinearRegression

# In[11]:

LR=LinearRegression()
LR.fit(x_train,y_train)
print("We have successfully train the algo")

# In[12]:

# Plotting the regression line
line = LR.coef_*x+LR.intercept_

# Plotting for the test data
plt.scatter(x, y)
plt.plot(x, line);
plt.show()

# In[13]:

# Predicting the scores
y_pred = LR.predict(x_test)

# In[14]:

y_pred

# In[15]:

# Comparing Actual vs Predicted
df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
df

# In[17]:

from sklearn import metrics
print('Mean Absolute Error:',
      metrics.mean_absolute_error(y_test, y_pred))

# In[ ]:


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import torch.nn as nn

# Do not change these imports; your module names should be
#   `CNN` in the file `cnn.py`
#   `Highway` in the file `highway.py`
# Uncomment the following two imports once you're ready to run part 1(f)

from cnn import CNN
from highway import Highway

# End "do not change"

class ModelEmbeddings(nn.Module):
    """
    Class that converts input words to their CNN-based embeddings.
    """

    def __init__(self, embed_size, vocab, char_embed_size=50):
        """
        Init the Embedding layer for one language
        @param embed_size (int): Embedding size (dimensionality) for the output
        @param vocab (VocabEntry): VocabEntry object. See vocab.py for documentation.
        """
        super(ModelEmbeddings, self).__init__()

        ## A4 code
        # pad_token_idx = vocab.src['<pad>']
        # self.embeddings = nn.Embedding(len(vocab.src), embed_size, padding_idx=pad_token_idx)
        ## End A4 code

        ### YOUR CODE HERE for part 1f
        self.embed_size = embed_size
        self.char_embed_size = char_embed_size
        self.pad_token_idx = vocab.char2id['<pad>']
        self.char_embeddings = nn.Embedding(len(vocab.char2id), self.char_embed_size, padding_idx = self.pad_token_idx)
        self.cnn = CNN(self.char_embed_size, self.embed_size)
        self.highway = Highway(self.embed_size)
        self.dropout = nn.Dropout(p=0.3)
        ### END YOUR CODE

    def forward(self, input_tensor):
        """
        Looks up character-based CNN embeddings for the words in a batch of sentences.
        @param input_tensor: Tensor of integers of shape (sentence_length, batch_size, max_word_length) where
            each integer is an index into the character vocabulary

        @param output: Tensor of shape (sentence_length, batch_size, embed_size), containing the
            CNN-based embeddings for each word of the sentences in the batch
        """
        ## A4 code
        # output = self.embeddings(input)
        # return output
        ## End A4 code
        ### YOUR CODE HERE for part 1f
        sentence_length, batch_size, max_word_length = input_tensor.shape[0], input_tensor.shape[1], input_tensor.shape[2]
        x = self.char_embeddings(input_tensor) # (sentence_length, batch_size, max_word_len, char_embed_size)
        x = x.permute(0,1,3,2) # change it to sentence_length, batch_size, char_embed_size, max_word_length

        x = x.reshape(-1, self.char_embed_size, max_word_length) # sentence_len* batch_size, char_embed_size, max_word_length
        # Test x.shape == (char_embed_size, m_word)
        x_conv = self.cnn(x)
        x_highway = self.highway(x_conv)
        x_highway = x_highway.reshape(sentence_length, batch_size, -1) # undo the shape unwrapping the same way you did. x_highway is supposed to be sentence first, batch second.
        x_word_embed = self.dropout(x_highway)
        return x_word_embed
        ### END YOUR CODE


import pandas as pd
from os import listdir
from os.path import isfile, join

trials_plans_path = r'C:\Users\97254\Desktop\Projects\MathThinkingLabDA\TrialsPlans'
trials_results_path = r'C:\Users\97254\Desktop\Projects\MathThinkingLabDA\Results'

def get_plan_trials(trials_plans_path: str):
    """ This function imports the uids of every trial plan,
    in order to allow uids comparisons with actual results files.

    parameters:
        trials_plans_path: a path to a directory of csv files containing the original trails plans.

    return: a list in the following format: [(file_name, uids)]
    """

    # importing the file names from directory
    trials_plans = [f for f in listdir(trials_plans_path) if isfile(join(trials_plans_path, f))]

    # creating a dict of {plan_file_name: uids_list}
    plans_uids = []
    for plan_file_name in trials_plans:
        plan_path = trials_plans_path + f'\{plan_file_name}'
        plan = pd.read_csv(plan_path, usecols=['uid'])
        plan['uid'] = plan['uid'].astype(int) # making sure all uids are ints
        plan.dropna(inplace=True)
        plan.drop_duplicates(inplace=True)
        plans_uids.append((plan_file_name, plan['uid'].to_list()))

    return plans_uids

def find_actual_trials(trials_plans_path: str, trials_results_path: str, trials_results: list=[]):
    """ This function helps to determine which trail plan has been used for each trail.

    parameters:
        trials_plans_path: a path to a directory of csv files containing the original trails plans.
        trials_results_path: a path to a directory of csv files containing the actual trails results.
        trials_results: a subset of the csv file names, to check.

    return: a dictionary in the following format: {trail_results_file_name: trail_plans_file_name}
    """

    # importing the file names from directory
    print(f"searching for: {trials_results if trials_results else 'all'}")
    if not trials_results: # if no specific results files are chosen
        trials_results = [f for f in listdir(trials_results_path) if isfile(join(trials_results_path, f))]

    # searching for the correct original plan for each results file
    actual_trials = {}
    all_res_uids = {} # just keeping them for later printing
    all_plans_uids = get_plan_trials(trials_plans_path) # importing original plans' uids
    for res_file_name in trials_results:
        res_path = trials_results_path + f'\{res_file_name}'
        res = pd.read_csv(res_path, usecols=['uid'])
        res.dropna(inplace=True)
        res.drop_duplicates(inplace=True)
        res['uid'] = res['uid'].astype(int) # making sure all uids are ints
        res_uids = res['uid'].to_list()
        all_res_uids.update({res_file_name: res_uids})

        # iterating over all uids in res to find a match,
        # and if so - add it to the actual_trials dict
        curr_plan_uids = all_plans_uids.copy()
        for r_uid in res_uids:
            curr_plan_uids = [(p_name, p_uids) for p_name, p_uids in curr_plan_uids if r_uid in p_uids]
            curr_plan_uids = [(p_name, p_uids[p_uids.index(r_uid):]) for p_name, p_uids in curr_plan_uids]
        matched_plan_name = curr_plan_uids[0][0] if curr_plan_uids != [] else 'not found'
        actual_trials.update({res_file_name: matched_plan_name})

    # generating an explanatory message for the user, with actual files and uids
    final_msg = """found as following:"""
    for res_name, plan_name in actual_trials.items():
        final_msg += f"\n--- results file: '{res_name}' \n---original file: '{plan_name}'"
        plan_uids = [uids for p_name, uids in all_plans_uids if p_name == plan_name][0]
        res_uids = all_res_uids[res_name]
        final_msg += f"\n result uids: \n {res_uids}, \n plan uids: \n {plan_uids}"

    print(final_msg)
    return actual_trials


from rest_framework import serializers

from app.models import Book

class BookModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ("book_name", "price", "publish", "authors", "pic")
        extra_kwargs = {
            'book_name': {
                'required': True,
                'min_length': 2,
                'error_message': {
                    'required': '图书名必须提供',
                    'min_length': '图书名不能少于两个字',
                }
            },
            'pic': {
                'read_only': True
            },
            'publish': {
                'write_only': True
            },
            'authors': {
                'write_only': True
            },
        }

    def validate(self, attrs):
        return attrs

    def validate_book_name(self, value):
        return value


def next_valid_line(f):
    # skip comments and empty lines, return None on EOF
    while True:
        line = f.readline()
        if len(line) == 0:
            return None
        if len(line) > 1 and line[0] != '#':
            return line.strip()

def read_data(input_file):

    file = open(input_file, 'r')

    scheduleLength = int(next_valid_line(file))
    numEmployees = int(next_valid_line(file))
    numShifts = int(next_valid_line(file))

    if numShifts == 3:
        matrixline1 = []
        matrixline2 = []
        matrixline3 = []
        for matrix in [matrixline1, matrixline2, matrixline3]:
            matrixTemp = next_valid_line(file).split()
            for num in matrixTemp:
                matrix.append(int(num))
        matrix = [matrixline1, matrixline2, matrixline3]

        morning = []
        afternoon = []
        night = []
        for day in [morning, afternoon, night]:
            dayTemp = next_valid_line(file).split()
            day.append(dayTemp[0])
            day.append(int(dayTemp[3]))
            day.append(int(dayTemp[4]))
    else:
        matrixline1 = []
        matrixline2 = []
        for matrix in [matrixline1, matrixline2]:
            matrixTemp = next_valid_line(file).split()
            for num in matrixTemp:
                matrix.append(int(num))
        matrix = [matrixline1, matrixline2]
        morning = []
        afternoon = []
        night = []
        for i in range(2):
            dayTemp = next_valid_line(file).split()
            if (dayTemp[0] == 'D'):
                morning.append(dayTemp[0])
                morning.append(int(dayTemp[3]))
                morning.append(int(dayTemp[4]))
            if (dayTemp[0] == 'A'):
                afternoon.append(dayTemp[0])
                afternoon.append(int(dayTemp[3]))
                afternoon.append(int(dayTemp[4]))
            if (dayTemp[0] == 'N'):
                night.append(dayTemp[0])
                night.append(int(dayTemp[3]))
                night.append(int(dayTemp[4]))

    lengthTemp = next_valid_line(file).split()
    minDaysOffLength = int(lengthTemp[0])
    maxDaysOffLength = int(lengthTemp[1])

    lengthTemp = next_valid_line(file).split()
    minWorkBlockLength = int(lengthTemp[0])
    maxWorkBlockLength = int(lengthTemp[1])

    lengthTemp = next_valid_line(file).split()
    NrSequencesOfLength2 = int(lengthTemp[0])
    NrSequencesOfLength3 = int(lengthTemp[1])

    notAllowedShiftSequences2 = []
    notAllowedShiftSequences3 = []
    while (True):
        temp = next_valid_line(file)
        if temp == None:
            break
        temp = temp.split()
        if (len(temp) == 2):
            notAllowedShiftSequences2.append(temp)
        else:
            notAllowedShiftSequences3.append([temp[0], temp[2]])
    file.close()

    shitToMap = [scheduleLength, numEmployees, numShifts, matrix,
                morning, afternoon, night,
                minDaysOffLength, maxDaysOffLength, minWorkBlockLength,
                maxWorkBlockLength, NrSequencesOfLength2, NrSequencesOfLength3,
                notAllowedShiftSequences2, notAllowedShiftSequences3]
    dataMap = {}
    count = 0
    for key in ['scheduleLength', 'numEmployees', 'numShifts', 'matrix',
                'morning', 'afternoon', 'night',
                'minDaysOffLength', 'maxDaysOffLength', 'minWorkBlockLength',
                'maxWorkBlockLength', 'NrSequencesOfLength2', 'NrSequencesOfLength3',
                'notAllowedShiftSequences2', 'notAllowedShiftSequences3']:
        dataMap[key] = shitToMap[count]
        count+=1
    return dataMap