import telebot
from config.config import TOKEN
from flask import Blueprint, request, abort
from app import db
from app.models import User, NewWords
from app.telegram_bot.process_user_welcome import ProcessWelcome
from app.telegram_bot.synonyms import find_synonym
import random

bot = telebot.TeleBot(TOKEN)
web = Blueprint("Web", __name__)

commands = (
"""
/addwords - add words to your vocabulary
/getsynonyms - Get synonyms for words
/mywords - get your words
/myscore = get your points for correct answers
/stop - stop studying
/study - bot gives you a word and you must write the synonym for this word.
If the synonym you wrote is in the list of synonyms, you get one point.
Type in 'stop' to see the results.
The progress can be seen on your profile page on the website.
Enjoy!
""")

greeting = f"Welcome to 'StudyEnglish with Bot'! {commands}"

#  set webhook for telegram bot
@web.route("/setweb", methods=["POST"])
def webhook():
    """Sets a webhook."""

    if request.headers.get("content-type") == "application/json":
        json_string = request.get_data().decode("utf-8")
        update = telebot.types.Update.de_json(json_string)
        bot.process_new_updates([update])
        return ''
    else:
        abort(403)

@bot.message_handler(commands=["start"])
def send_welcome(message):
    """Sends welcome message to the registered user."""

    send_welcome.unique_code = ProcessWelcome.extract_unique_code(message.text)
    chat_id = message.from_user.id
    if send_welcome.unique_code:
        send_welcome.get_username = ProcessWelcome.get_username_from_db(send_welcome.unique_code)
        if send_welcome.get_username:
            ProcessWelcome.save_chat_id(chat_id, send_welcome.unique_code)
            reply = "Hello {0}! {1} Let's add some new words into your vocabulary! Just type in any word. Press finish to stop.".format(
                send_welcome.get_username,
                greeting
            )
            mes = bot.send_message(chat_id, reply)
            bot.register_next_step_handler(mes, gather_words, send_welcome.unique_code)
        else:
            no_id = "I have no clue who you are"
            bot.send_message(chat_id, no_id)
    else:
        mistake = "Sorry, but i accept messages from registered users only.."
        bot.send_message(chat_id, mistake)

@bot.message_handler(commands=["addwords"])
def add_words(message):
    """Adds user words to the db."""

    chat_id = message.from_user.id
    unique_code = send_welcome.unique_code
    msg = bot.send_message(chat_id,
    "Type in the word you want to add. Type in 'finish' to stop adding words and see the synonyms.")
    bot.register_next_step_handler(msg, gather_words, unique_code)

@bot.message_handler(commands=["study"])
def study(message):
    """Activates studying mode."""

    chat_id = message.from_user.id
    username = send_welcome.get_username
    if username:
        user = User.query.filter_by(name=username).first()
        words = NewWords.query.filter_by(person_id=user.id).all()
        choice = random.choice(words)
        msg = bot.send_message(chat_id, choice)
        bot.register_next_step_handler(msg, guess, choice)
    else:
        bot.send_message(chat_id, "Sorry, i have no clue who you are")

@bot.message_handler(commands=["mywords"])
def mywords(message):
    """Get a list of added words."""

    try:
        chat_id = message.from_user.id
        username = send_welcome.get_username
        if username:
            user = User.query.filter_by(name=username).first()
            words = NewWords.query.filter_by(person_id=user.id).all()
            get_words = '\n'.join(str(word) for word in words)
            bot.send_message(chat_id, f"Your list of words:\n{get_words}")
        else:
            bot.send_message(chat_id, "Sorry, i have no clue who you are")
    except Exception as e:
        print(str(e))
        bot.send_message(chat_id, "Ooops")

@bot.message_handler(commands=["myscore"])
def myscore(message):
    """Counts user points and shows the total."""

    try:
        chat_id = message.from_user.id
        username = send_welcome.get_username
        if username:
            user = User.query.filter_by(name=username).first()
            total = user.user_points
            if total > 0:
                bot.send_message(chat_id, f"Your score is {total}")
            else:
                bot.send_message(chat_id, "Do studying to get the score! /study")
        else:
            bot.send_message(chat_id, "I have no clue who you are..")
    except Exception as e:
        print(str(e))
        bot.send_message(chat_id, "Ops")

def add_words_to_vocab(word: str, unique_code: str):
    """Adds words to specific user."""

    get_username = ProcessWelcome.get_username_from_db(unique_code)
    if get_username:
        user = User.query.filter_by(name=get_username).first()
        word_to_db = NewWords(user_word=word)
        db.session.add(word_to_db)
        user.new_user_words.append(word_to_db)
        db.session.commit()

answers = iter(["Come ooon", "Moooore words!", "One more!", "Don't be weak!", "One moore!!"])

def gather_words(message, unique_code: str):
    """Sends a list of words to the user."""

    try:
        chat_id = message.from_user.id
        text = message.text
        if text == "finish":
            get_username = ProcessWelcome.get_username_from_db(unique_code)
            if get_username:
                user = User.query.filter_by(name=get_username).first()
                words = NewWords.query.filter_by(person_id=user.id).all()
                get_words = '\n'.join(str(word) for word in words)
                bot.send_message(chat_id, f"Your list of words:\n{get_words}")
            else:
                bot.send_message(chat_id, "Sorry, i don't know who you are..")
        else:
            add_words_to_vocab(text, unique_code)
            msg = bot.send_message(chat_id, next(answers, "One more!"))
            bot.register_next_step_handler(msg, gather_words, unique_code)
    except Exception as e:
        print(str(e))
        bot.send_message(chat_id, "Ops, words havent been added..")

@bot.message_handler(func=lambda message: True, content_types=['text'])
def other_text(message):
    """Replies with a message to the unknown command."""

    try:
        chat_id = message.from_user.id
        username = send_welcome.get_username
        if username:
            bot.send_message(chat_id, "I don't understand:( Maybe try /help command?")
        else:
            bot.send_message(chat_id, "Sorry, i don't know who you are..")
    except Exception as e:
        print(str(e))
        bot.send_message(chat_id, "Ooops")

def guess(message, word):
    """Activates synonyms."""

    try:
        chat_id = message.from_user.id
        text = message.text
        synonyms = find_synonym(word)
        username = send_welcome.get_username
        if username:
            if text == "/stop":
                bot.send_message(chat_id, "Press /myscore to see your score!")
            else:
                user = User.query.filter_by(name=username).first()
                words = NewWords.query.filter_by(person_id=user.id).all()
                if text.lower() in synonyms:
                    setattr(user, "user_points", user.user_points + 1)
                    db.session.commit()
                    choice = random.choice(words)
                    msg = bot.send_message(chat_id, f"Correct! The next word is:\n {choice}",
                                           choice)
                    bot.register_next_step_handler(msg, guess, choice)
                if text.lower() not in synonyms:
                    choice = random.choice(words)
                    msg = bot.send_message(chat_id, f"Incorrect! The next word is:\n{choice}",
                                           choice)
                    bot.register_next_step_handler(msg, guess, choice)
        else:
            bot.send_message(chat_id, "Sorry, i have no clue who you are..")
    except Exception as e:
        print(str(e))
        bot.send_message(chat_id, "Ooooppss")


# -*- coding: utf-8 -*-
"""
Created on Fri Oct  9 09:25:22 2020

@author: hrmha
"""
if __name__ == '__main__':
    print('Starting')
from userClass import *
from userClass import *
from hash import *
from findHash import *
from getUsersData import *
import matplotlib.pyplot as plt

def getData():
    data = usersListFromCsv()
    return data
#, lastName, age, gender, phoneNumber, creditCard, cCType
def transformToSorteable(data, args):
    transformedData = []
    for i in range(len(data)):
        if args == 1:
            transformedData.append(data[i].userId)
        elif args == 2:
            transformedData.append(int(((data[i]).name), 36))
        elif args == 3:
            transformedData.append(int((data[i]).lastName), 36)
        elif args == 4:
            transformedData.append((data[i]).age)
        elif args == 5:
            transformedData.append((data[i]).gender)
        elif args == 6:
            transformedData.append(int((data[i]).phoneNumber))
        elif args == 7:
            transformedData.append(int((data[i]).creditCard))
        elif args == 8:
            transformedData.append(int(((data[i]).cCType), 36))
    return transformedData

def main():
    arg = 7
    c = 100
    x = range(1, c)
    yBa = []
    yBs = []
    yWs = []
    userList = getData()
    data = transformData(userList)
    usersStack = addUserFromData(data, c)
    sortableData = transformToSorteable(usersStack, arg)
    for i in x:
        if arg == 1 or arg == 4 or arg == 5:
            hashData = makeHashList(sortableData[:i], 10)
            worseData = makeWorseHashList(sortableData[:i], 10)
        else:
            worseData = makeWorseHashList(sortableData[:i], 10)
            hashData = makeHashList(sortableData[:i], 36)
        BaHf, BsHf, WsHf = toPlotHf(hashData, worseData)
        yBa.append(BaHf)
        yBs.append(BsHf)
        yWs.append(WsHf)
    plt.figure(1)
    plt.suptitle('Busqueda Por transformación de llaves')
    plt.subplot(334)
    plt.plot(x, yBa, 'y-')
    plt.xlabel('Caso base')
    plt.subplot(335)
    plt.plot(x, yBs, 'g-')
    plt.xlabel('Mejor caso')
    plt.subplot(336)
    plt.plot(x, yWs, 'r-')
    plt.xlabel('Peor caso')
    plt.savefig('hash.png')
    plt.show()

if __name__ == '__main__':
    main()


import asyncio
import aiocoap

import logging
import time
import os
import re
import hashlib
import aiocoap.resource as resource
logging.basicConfig(level=logging.DEBUG,format='%(levelname)s %(message)s')
logger = logging.getLogger('lwm2mserver')

class Hasher(object):
    def __init__(self, seed):
        self.seed = seed

    def hash_for(self, string):
        assert string is not None
        h = hashlib.new("sha1")
        h.update(string.encode())
        h.update(self.seed)
        return h.hexdigest()

class Registry_device():
    def getByName(self,registrys,devicename):
        for k in registrys:
            if registrys[k]["endpoint"]==devicename:
                return registrys[k]

    def unregister(self,registrys,id):
        if registrys.get(id):
            del registrys[id]
        else:
            logger.error("not found the id ")
    def register(self,registrys,idnum,device):
        registrys[idCounter]=device
        registrys[idCounter]['id']=idCounter

    def clean(self,registrys):
        registrys={}

    def getObject(self,registrys,id):
        if registrys.get(id):
            return registrys[id]
        else:
            return None

    def update(self,registrys,id,obj):
        if registrys.get(id):
            registrys[id]=obj
        else:
            logger.error("not found the id")
    def list(self,registrys):
        print(registrys)
    def stopLifetimeCheck(self):
        pass
    def checkLifetime(self):
        pass

class WithAsyncloop():
    def __init__(self):
        self.loop=asyncio.get_event_loop()

class WithClient(WithAsyncloop):
    def __init__(self):
        super(WithClient,self).__init__()
        self.client=self.loop.run_until_complete(aiocoap.Context.create_client_context())

    def close(self):
        self.loop.run_until_complete(self.client.shutdown())

    def fetch_response(self,request,exchange_monitor_factory=lambda x:None):
        return self.loop.run_until_complete(self.client.request(request, exchange_monitor_factory=exchange_monitor_factory).response)

class DeviceManagement(WithClient,Registry_device):
    def __init__(self):
        super(DeviceManagement,self).__init__()
    def read(self,registrys,deviceId, objectType, objectId, resourceId):
        obj=self.getByName(registrys,deviceId)
        print(obj)
        request=aiocoap.Message(code=aiocoap.GET)
        request.remote=obj["address"]
        request.opt.uri_host=obj["host"]
        request.opt.uri_port=obj["port"]
        request.opt.uri_path=(objectType, objectId, resourceId)
        return request
    def write(self,registrys,deviceId, objectType, objectId, resourceId, value):
        obj=self.getByName(registrys,deviceId)
        request=aiocoap.Message(code=aiocoap.PUT)
        request.payload=value
        request.remote=obj["address"]
        request.opt.uri_host=obj["host"]
        request.opt.uri_port=obj["port"]
        request.opt.uri_path=(objectType, objectId, resourceId)
        return request

    def execute(self,registrys,deviceId, objectType, objectId, resourceId, value):
        obj=self.getByName(registrys,deviceId)
        request=aiocoap.Message(code=aiocoap.POST)
        request.payload=value
        request.remote=obj["address"]
        request.opt.uri_host=obj["host"]
        request.opt.uri_port=obj["port"]
        request.opt.uri_path=(objectType, objectId, resourceId)
        return request
    def writeAttributes(deviceId, objectType, objectId, resourceId, attributes):
            pass
    def discover(self,registrys,deviceId, objectType, objectId, resourceId):
        obj=self.getByName(registrys,deviceId)
        logger.debug('Executing a resource discover operation on resource')

        request=aiocoap.Message(code=aiocoap.GET)
        request.remote=obj["address"]
        request.opt.uri_host=obj["host"]
        request.opt.uri_port=obj["port"]
        print(obj["objects"])
        request.opt.accept=aiocoap.numbers.media_types_rev['application/link-format']
        if obj["objects"].get(objectType):
            if obj["objects"][objectType].get(objectId):
                if obj["objects"][objectType][objectId]==resourceId:
                    request.opt.uri_path=(objectType,objectId,resourceId)
                else:
                    request.opt.uri_path=(objectType,objectId)
            else:
                request.opt.uri_path=(objectType,)
        return request
    def create(self,registrys,deviceId, objectType, objectId):
        obj=self.getByName(registrys,deviceId)
        logger.debug('Creating a new instance of object type [%s] in the device [%d] with instance id [%s]',objectType, deviceId, objectId)
        request=aiocoap.Message(code=aiocoap.POST)
        request.remote=obj["address"]
        request.opt.uri_host=obj["host"]
        request.opt.uri_port=obj["port"]
        request.opt.uri_path=(deviceId, objectType)
        return request
    def remove(self,deviceId, objectType, objectId):
        pass
    def observe_parseId():
        pass
    def observe(self,registrys,deviceId, objectType, objectId,resourceId):
        obj=self.getByName(registrys,deviceId)
        serveraddress = "::1"
        servernetloc = "[%s]"%serveraddress
        logger.debug('Observing value from resource /%s/%s/%s in device [%d]',objectType, objectId, resourceId, deviceId)
        request = aiocoap.Message(code=aiocoap.GET)
        request.unresolved_remote = servernetloc
        request.remote=obj["address"]
        request.opt.uri_host=obj["host"]
        request.opt.uri_port=obj["port"]
        request.opt.uri_path = (objectType,objectId,resourceId)
        request.opt.observe = 1
        return request

    def list_observe(self):
    	print(subscriptions)
    def clean_observe(self):
    	subscriptions={}
    def cancel_observe(self,deviceId, objectType, objectId, resourceId):
    	pass

class Server(DeviceManagement,resource.ObservableResource):
    host='localhost'
    port=5683
    lifetime=86400
    version='1.0.0'
    log_level="DEBUG"
    ENDPOINT = "ep"
    LIFETIME = "lt"
    BINDING_MODE = "b"
    SMS = "sms"
    LWM2M_VERSION = "lwm2m"
    DEFAULT_VERSION = "1.0"
    DEFAULT_LIFETIME = 86400
    CLEANER_PERIOD_SECONDS = 10
    site=aiocoap.resource.Site()
    registry={}
    idCounter=1
    subscriptions = {}

    def __init__(self):
        super(Server,self).__init__()
        logger.debug("creating server instance")
        self.hasher = Hasher(os.urandom(30))
        self.site = resource.Site()
        self.site.add_resource((self.get_path(),), self)
        self.handle=None

    def notify(self):
        self.updated_state()
        self.reschedule()

    def reschedule(self):
        self.handle = asyncio.get_event_loop().call_later(5, self.notify)

    def update_observation_count(self, count):
        if count and self.handle is None:
            print("Starting the clock")
            self.handle = self.reschedule()
        if count == 0 and self.handle:
            print("Stopping the clock")
            self.handle.cancel()
            self.handle = None

    async def render_get(self, request):
        payload = datetime.datetime.now().\
                strftime("%Y-%m-%d %H:%M").encode('ascii')
        return aiocoap.Message(payload=payload)

    @staticmethod
    def get_path():
        return "rd"

    def get_device_location(self,location):
        for k in self.registry:
            if self.registry[k]['location']==location:
                return self.registry[k]
    async def render_post(self, request):
        print(request.opt.uri_host)
        print(request.opt.uri_port)
        address=request.remote
        opts = dict(x.split("=") for x in request.opt.uri_query)
        logger.debug("POST called with options %s" % opts)

        if self.ENDPOINT not in opts:
            return self.bad_request(b"endpoint name is required")

        client = dict()
        client["address"]=address
        client["host"]=self.host
        client["port"]=self.port
        client["endpoint"] = opts[self.ENDPOINT]
        client["lifetime"] = self._get_lifetime(opts, self.DEFAULT_LIFETIME)

        client["lwm2m_version"] = opts[self.LWM2M_VERSION] if self.LWM2M_VERSION in opts else self.DEFAULT_VERSION
        client["sms"] = opts[self.SMS] if self.SMS in opts else None
        client["binding_mode"] = self.get_binding_mode(
            opts[self.BINDING_MODE]) if self.BINDING_MODE in opts else "U"
        payload = request.payload.decode()
        if payload == "":
            return self.bad_request(b"payload must contain object links")

        client["objects"] = self.get_object_links(request.payload.decode())
        client["time"] = asyncio.get_event_loop().time()
        location=self.hasher.hash_for(opts[Server.ENDPOINT])

        client['location']=location
        self.registry[self.idCounter]=client
        registrys=self.registry

        self.idCounter +=1

        self.site.add_resource((self.get_path(), location), self)
        response = aiocoap.Message(code=aiocoap.CREATED)
        return response

    def _get_lifetime(self, opts, default_lifetime):
        if self.LIFETIME in opts:
            try:
                lifetime = int(opts[Server.LIFETIME])
            except ValueError:
                return Server.bad_request(b"non-numeric lifetime value")

            if lifetime <= 0:
                return Server.bad_request(b"lifetime value out of range")
            else:
                return lifetime
        else:
            return default_lifetime

    def _remove_client(self, location):
        self.site.remove_resource((self.get_path(), location))

    @staticmethod
    def get_binding_mode(bmode):
        modes = ["U", "UQ", "S", "SQ", "US", "UQS"]
        return bmode if bmode in modes else "U"

    @staticmethod
    def get_object_links(payload):
        objs = dict()
        for link in payload.split(","):
            m = re.match("^<(/(\d+))(/(\d+))*>.*$", link)
            if m.group(4) is not None:
                obj_id = str(m.group(2))
                instance_id = str(m.group(4))
                if obj_id in objs:
                    objs[obj_id][instance_id] = instance_id
                else:
                    objs[obj_id] = {instance_id: instance_id}
            else:
                obj_id = str(m.group(2))
                if obj_id in objs:
                    objs[obj_id]["0"] = "0"
                else:
                    objs[obj_id] = {"0": "0"}

        return objs

    @staticmethod
    def bad_request(payload=None):
        response = aiocoap.Message(code=aiocoap.BAD_REQUEST)
        if payload is not None:
            response.payload = payload
        return response

    @asyncio.coroutine
    def render_put(self, request):
        location = request.opt.uri_path[-1]
        logger.debug("PUT called for location %s" % location)
        opts = dict(x.split("=") for x in request.opt.uri_query)
        client = self.get_device_location(location)# self.client_persistence.get_client_for_location(location)
        client["time"] = asyncio.get_event_loop().time()
        client["lifetime"] = self._get_lifetime(opts, client["lifetime"])
        client["binding_mode"] = self.get_binding_mode(
            opts[self.BINDING_MODE]) if self.BINDING_MODE in opts else client["binding_mode"]
        client["sms"] = opts[self.SMS] if self.SMS in opts else client["sms"]
        payload = request.payload.decode()
        if payload != "":
            client["objects"] = self.get_object_links(payload)
        registrys=self.registry

        response = aiocoap.Message(code=aiocoap.CHANGED)
        return response

    @asyncio.coroutine
    def render_delete(self, request):
        location = request.opt.uri_path[-1]
        logger.debug("DELETE called for location %s" % location)
        self._remove_client(location)
        response = aiocoap.Message(code=aiocoap.DELETED)
        return response

    def notify(self):
        self.updated_state()
        self.reschedule()

    def cleaner(self):
        logger.debug("running periodic cleaner")
        to_delete = list()
        for k in self.registry:
            time = asyncio.get_event_loop().time()
            if time - self.registry[k]["time"] > self.registry[k]["lifetime"]:
                to_delete.append(self.registry[k]["location"])
        for location in to_delete:
            logger.debug("removing dead client (location %s)" % location)
            self._remove_client(location)
        # reschedule the cleaner
        asyncio.get_event_loop().call_later(self.CLEANER_PERIOD_SECONDS, self.cleaner)
    async def start(self):
        self.protocol= await aiocoap.Context.create_server_context(self.site,bind=(self.host, self.port))
        self.protocol1=await aiocoap.Context.create_client_context()
        # asyncio.ensure_future(self.protocol)
        # self.server = self.loop.run_until_complete(self.protocol)
    def stop(self):
        self.loop.run_until_complete(asyncio.sleep(CLEANUPTIME))
        self.loop.run_until_complete(self.server.shutdown())


import tensorflow as tf
from tensorflow.keras.applications.resnet50 import preprocess_input
import keras
from keras import layers, models
import os
import numpy as np
from pathlib import Path
from keras.regularizers import l2

import matplotlib.pyplot as plt

#from sklearn.model_selection import train_test_split
#uncomment for MACOSx
'''os.environ['KMP_DUPLICATE_LIB_OK']='True'
os.environ['MKL_NUM_THREADS'] = '16'
os.environ['GOTO_NUM_THREADS'] = '16'
os.environ['OMP_NUM_THREADS'] = '16'
os.environ['openmp'] = 'True'
'''

def affiche_evolution_apprentissage(history):
    # résumé de l'historique pour loss
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('Erreur du modèle')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['apprentissage', 'test'], loc='upper right')
    #Path to save model learning curve
    plt.savefig("_3dNet_Save_3/Model_Error.png")

#Path to saved numpy RGB patches and 3D coordinates
Group_scenes = ["data10","data11","data12","data13","data14","data15"]

a10 = np.load(f"{Group_scenes[0]}_Patches_RGB_list.npy")
b10 = np.load(f"{Group_scenes[0]}_Patches_3D_list.npy")
#c10 = np.load(f"{Group_scenes[0]}_Patches_2D_list.npy")

a11 = np.load(f"{Group_scenes[1]}_Patches_RGB_list.npy")
b11 = np.load(f"{Group_scenes[1]}_Patches_3D_list.npy")
#c11 = np.load(f"{Group_scenes[1]}_Patches_2D_list.npy")

a12 = np.load(f"{Group_scenes[2]}_Patches_RGB_list.npy")
b12 = np.load(f"{Group_scenes[2]}_Patches_3D_list.npy")
#c12 = np.load(f"{Group_scenes[2]}_Patches_2D_list.npy")

a13 = np.load(f"{Group_scenes[3]}_Patches_RGB_list.npy")
b13 = np.load(f"{Group_scenes[3]}_Patches_3D_list.npy")
#c13 = np.load(f"{Group_scenes[3]}_Patches_2D_list.npy")

a14 = np.load(f"{Group_scenes[4]}_Patches_RGB_list.npy")
b14 = np.load(f"{Group_scenes[4]}_Patches_3D_list.npy")
#c14 = np.load(f"{Group_scenes[4]}_Patches_2D_list.npy")

#training data
x_train =  np.concatenate((a10,a11,a12,a13),0)
y_train =  np.concatenate((b10,b11,b12,b13),0)

#testing data
x_test =  a14 #np.concatenate((a14,a15),0)
y_test =  b14 #np.concatenate((b14,b15),0)

print("max - min Train data: ",y_train.max()," , ", y_train.min())
print("max - min test data: ",y_test.max()," , ", y_test.min())

# normalize rgb patches data (data preprocessing)
X_train = preprocess_input(x_train)
X_test = preprocess_input(x_test)

# noramlize 3D labels data
Y_train = y_train.reshape(-1,3)
Y_test = y_test.reshape(-1,3)

X_train.shape,Y_train.shape,X_test.shape,Y_test.shape

# CNN parameters
nb_kernels = 16
val_batch_size = 2048
val_epcohs = 500
val_dropout = 0.5
val_weight_decay = 1e-5

#Sequential Model
_3dNet_ = models.Sequential()

#Convolutional layers
_3dNet_.add(layers.convolutional.Conv2D(nb_kernels,(3,3),padding='valid',kernel_regularizer=l2(val_weight_decay), bias_regularizer=l2(val_weight_decay),activation='relu',input_shape=(50,50,3)))
_3dNet_.add(layers.BatchNormalization())
_3dNet_.add(layers.MaxPooling2D((3,3),strides=(2,2)))

_3dNet_.add(layers.convolutional.Conv2D(nb_kernels*2,(3,3),padding='same',kernel_regularizer=l2(val_weight_decay), bias_regularizer=l2(val_weight_decay),activation='relu'))
_3dNet_.add(layers.BatchNormalization())
_3dNet_.add(layers.MaxPooling2D((3,3),strides=(2,2),padding='same'))

_3dNet_.add(layers.convolutional.Conv2D(nb_kernels*4,(3,3),kernel_regularizer=l2(val_weight_decay), bias_regularizer=l2(val_weight_decay),activation='relu'))
_3dNet_.add(layers.BatchNormalization())
_3dNet_.add(layers.MaxPooling2D((3,3),strides=(2,2),padding='same'))

_3dNet_.add(layers.convolutional.Conv2D(nb_kernels*8,(3,3),kernel_regularizer=l2(val_weight_decay), bias_regularizer=l2(val_weight_decay),activation='relu',padding='same'))
_3dNet_.add(layers.BatchNormalization())
_3dNet_.add(layers.MaxPooling2D((3,3),strides=(2,2),padding='same'))

_3dNet_.add(layers.convolutional.Conv2D(nb_kernels*16,(3,3),kernel_regularizer=l2(val_weight_decay), bias_regularizer=l2(val_weight_decay),activation='relu',padding='same'))
_3dNet_.add(layers.BatchNormalization())
_3dNet_.add(layers.MaxPooling2D((3,3),strides=(2,2),padding='same'))

#MLP Layers
_3dNet_.add(layers.Flatten())
_3dNet_.add(layers.Dense(512,kernel_regularizer=l2(val_weight_decay), bias_regularizer=l2(val_weight_decay),activation='relu'))
_3dNet_.add(layers.Dropout(val_dropout))
_3dNet_.add(layers.Dense(256,kernel_regularizer=l2(val_weight_decay), bias_regularizer=l2(val_weight_decay),activation='relu'))
_3dNet_.add(layers.Dropout(val_dropout))

#Regression Layers
_3dNet_.add(layers.Dense(3,activation='linear'))

#Compiling Model
_3dNet_.compile(optimizer=keras.optimizers.Adam(),loss='mse')

_3dNet_.summary()

#Fitting Model
history = _3dNet_.fit(X_train, Y_train,
   validation_data=(X_test, Y_test),
   epochs=val_epcohs,
   batch_size=val_batch_size)

# Enregistrer la structure du réseau
model_structure = _3dNet_.to_json()
f = Path("_3dNet_Save_3/_3dNet__structure.json")
f.write_text(model_structure)
# Sauvegarde des poids appris par le réseau
#Path to save
_3dNet_.save_weights("_3dNet_Save_3/_3dNet__weights.h5")
#Save History
#Path to save
np.save("_3dNet_Save_3/_3DNet_History",history.history)
#Save Plot
affiche_evolution_apprentissage(history)


# logicPlan.py
# ------------
# Licensing Information:  You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).

"""
In logicPlan.py, you will implement logic planning methods which are called by
Pacman agents (in logicAgents.py).
"""

import util
import sys
import logic
import game

from logic import conjoin, disjoin
from logic import PropSymbolExpr, Expr, to_cnf, pycoSAT, parseExpr

import itertools
import copy

pacman_str = 'P'
food_str = 'FOOD'
wall_str = 'WALL'
pacman_wall_str = pacman_str + wall_str
ghost_pos_str = 'G'
ghost_east_str = 'GE'
pacman_alive_str = 'PA'
DIRECTIONS = ['North', 'South', 'East', 'West']
blocked_str_map = dict([(direction, (direction + "_blocked").upper()) for direction in DIRECTIONS])
geq_num_adj_wall_str_map = dict([(num, "GEQ_{}_adj_walls".format(num)) for num in range(1, 4)])
DIR_TO_DXDY_MAP = {'North':(0, 1), 'South':(0, -1), 'East':(1, 0), 'West':(-1, 0)}

class PlanningProblem:
    """
    This class outlines the structure of a planning problem, but doesn't implement
    any of the methods (in object-oriented terminology: an abstract class).

    You do not need to change anything in this class, ever.
    """

    def getStartState(self):
        """
        Returns the start state for the planning problem.
        """
        util.raiseNotDefined()

    def getGhostStartStates(self):
        """
        Returns a list containing the start state for each ghost.
        Only used in problems that use ghosts (FoodGhostPlanningProblem)
        """
        util.raiseNotDefined()

    def getGoalState(self):
        """
        Returns goal state for problem. Note only defined for problems that have
        a unique goal state such as PositionPlanningProblem
        """
        util.raiseNotDefined()

def tinyMazePlan(problem):
    """
    Returns a sequence of moves that solves tinyMaze.  For any other maze, the
    sequence of moves will be incorrect, so only use this for tinyMaze.
    """
    from game import Directions
    s = Directions.SOUTH
    w = Directions.WEST
    return  [s, s, w, s, w, w, s, w]

# **************
#  Question 1
# **************
def sentence1():
    """Returns a Expr instance that encodes that the following expressions are all true.

    A or B
    (not A) if and only if ((not B) or C)
    (not A) or (not B) or C
    """
    "*** BEGIN YOUR CODE HERE ***"
    A = logic.Expr("A")
    B = logic.Expr("B")
    C = logic.Expr("C")
    L = [logic.disjoin(A, B), ~A % (~B | C), logic.disjoin(~A, ~B, C)]
    return logic.conjoin(L[0], L[1], L[2])
    "*** END YOUR CODE HERE ***"

# **************
#  Question 1
# **************
def sentence2():
    """Returns a Expr instance that encodes that the following expressions are all true.

    C if and only if (B or D)
    A implies ((not B) and (not D))
    (not (B and (not C))) implies A
    (not D) implies C
    """
    "*** BEGIN YOUR CODE HERE ***"
    A = logic.Expr("A")
    B = logic.Expr("B")
    C = logic.Expr("C")
    D = logic.Expr("D")
    L = [C % (B | D), A >> (~B & ~D), ~(B & ~C) >> A, ~D >> C]
    return logic.conjoin(L[0], L[1], L[2], L[3])
    "*** END YOUR CODE HERE ***"

# **************
#  Question 1
# **************
def sentence3():
    """Using the symbols PacmanAlive[1], PacmanAlive[0], PacmanBorn[0], and PacmanKilled[0],
    created using the PropSymbolExpr constructor, return a PropSymbolExpr
    instance that encodes the following English sentences (in this order):

    Pacman is alive at time 1 if and only if Pacman was alive at time 0 and it was
    not killed at time 0 or it was not alive at time 0 and it was born at time 0.

    Pacman cannot both be alive at time 0 and be born at time 0.

    Pacman is born at time 0.
    """
    "*** BEGIN YOUR CODE HERE ***"
    A = logic.PropSymbolExpr("PacmanAlive[0]")
    B = logic.PropSymbolExpr("PacmanAlive[1]")
    C = logic.PropSymbolExpr("PacmanBorn[0]")
    D = logic.PropSymbolExpr("PacmanKilled[0]")
    one = B % ((A & ~D) | (~A & C))
    two = ~(A & C)
    three = C
    return logic.conjoin(one, two, three)
    "*** END YOUR CODE HERE ***"

# **************
#  Question 1
# **************
def findModel(sentence):
    """Given a propositional logic sentence (i.e. a Expr instance), returns a satisfying
    model if one exists. Otherwise, returns False.
    """
    "*** BEGIN YOUR CODE HERE ***"
    sentence_cnf = logic.to_cnf(sentence)
    sat_model = logic.pycoSAT(sentence_cnf)
    return sat_model
    "*** END YOUR CODE HERE ***"

def modelToString(model):
    """Converts the model to a string for printing purposes. The keys of a model are
    sorted before converting the model to a string.

    model: Either a boolean False or a dictionary of Expr symbols (keys)
    and a corresponding assignment of True or False (values). This model is the output of
    a call to pycoSAT.
    """
    if model == False:
        return "False"
    else:
        # Dictionary
        modelList = sorted(model.items(), key=lambda item: str(item[0]))
        return str(modelList)

# **************
#  Question 2
# **************
def atLeastOne(literals):
    """
    Given a list of Expr literals (i.e. in the form A or ~A), return a single
    Expr instance in CNF (conjunctive normal form) that represents the logic
    that at least one of the literals in the list is true.
    >>> A = PropSymbolExpr('A');
    >>> B = PropSymbolExpr('B');
    >>> symbols = [A, B]
    >>> atleast1 = atLeastOne(symbols)
    >>> model1 = {A:False, B:False}
    >>> print(pl_true(atleast1,model1))
    False
    >>> model2 = {A:False, B:True}
    >>> print(pl_true(atleast1,model2))
    True
    >>> model3 = {A:True, B:True}
    >>> print(pl_true(atleast1,model2))
    True
    """
    "*** BEGIN YOUR CODE HERE ***"
    return logic.disjoin(literals)
    "*** END YOUR CODE HERE ***"

# **************
#  Question 2
# **************
def atMostOne(literals):
    """
    Given a list of Expr literals, return a single Expr instance in
    CNF (conjunctive normal form) that represents the logic that at most one of
    the expressions in the list is true.
    """
    "*** BEGIN YOUR CODE HERE ***"
    literals_negations = [~literal for literal in literals]
    pairs_disj = [disjoin(list(pair)) for pair in itertools.combinations(literals_negations, 2)]
    return conjoin(pairs_disj)
    "*** END YOUR CODE HERE ***"

# **************
#  Question 2
# **************
def exactlyOne(literals):
    """
    Given a list of Expr literals, return a single Expr instance in
    CNF (conjunctive normal form)that represents the logic that exactly one of
    the expressions in the list is true.
    """
    "*** BEGIN YOUR CODE HERE ***"
    return conjoin(atLeastOne(literals), atMostOne(literals))
    "*** END YOUR CODE HERE ***"

def extractActionSequence(model, actions):
    """
    Convert a model in to an ordered list of actions.
    model: Propositional logic model stored as a dictionary with keys being
    the symbol strings and values being Boolean: True or False
    Example:
    >>> model = {"North[2]":True, "P[3,4,0]":True, "P[3,3,0]":False, "West[0]":True, "GhostScary":True, "West[2]":False, "South[1]":True, "East[0]":False}
    >>> actions = ['North', 'South', 'East', 'West']
    >>> plan = extractActionSequence(model, actions)
    >>> print(plan)
    ['West', 'South', 'North']
    """
    plan = [None for _ in range(len(model))]
    for sym, val in model.items():
        parsed = parseExpr(sym)
        if type(parsed) == tuple and parsed[0] in actions and val:
            action, time = parsed
            plan[int(time)] = action
    #return list(filter(lambda x: x is not None, plan))
    return [x for x in plan if x is not None]

def pacmanSuccessorStateAxioms(x, y, t, walls_grid, var_str=pacman_str):
    """
    Successor state axiom for state (x,y,t) (from t-1), given the board (as a
    grid representing the wall locations).
    Current <==> (previous position at time t-1) & (took action to move to x, y)
    Available actions are ['North', 'East', 'South', 'West']
    Note that STOP is not an available action.
    """
    possibilities = []
    if not walls_grid[x][y+1]:
        possibilities.append( PropSymbolExpr(var_str, x, y+1, t-1)
                            & PropSymbolExpr('South', t-1))
    if not walls_grid[x][y-1]:
        possibilities.append( PropSymbolExpr(var_str, x, y-1, t-1)
                            & PropSymbolExpr('North', t-1))
    if not walls_grid[x+1][y]:
        possibilities.append( PropSymbolExpr(var_str, x+1, y, t-1)
                            & PropSymbolExpr('West', t-1))
    if not walls_grid[x-1][y]:
        possibilities.append( PropSymbolExpr(var_str, x-1, y, t-1)
                            & PropSymbolExpr('East', t-1))

    if not possibilities:
        return None

    return PropSymbolExpr(var_str, x, y, t) % disjoin(possibilities)

def pacmanSLAMSuccessorStateAxioms(x, y, t, walls_grid, var_str=pacman_str):
    """
    Similar to `pacmanSuccessorStateAxioms` but accounts for illegal actions
    where the pacman might not move timestep to timestep.
    Available actions are ['North', 'East', 'South', 'West']
    """
    moved_tm1_possibilities = []
    if not walls_grid[x][y+1]:
        moved_tm1_possibilities.append( PropSymbolExpr(var_str, x, y+1, t-1)
                            & PropSymbolExpr('South', t-1))
    if not walls_grid[x][y-1]:
        moved_tm1_possibilities.append( PropSymbolExpr(var_str, x, y-1, t-1)
                            & PropSymbolExpr('North', t-1))
    if not walls_grid[x+1][y]:
        moved_tm1_possibilities.append( PropSymbolExpr(var_str, x+1, y, t-1)
                            & PropSymbolExpr('West', t-1))
    if not walls_grid[x-1][y]:
        moved_tm1_possibilities.append( PropSymbolExpr(var_str, x-1, y, t-1)
                            & PropSymbolExpr('East', t-1))

    if not moved_tm1_possibilities:
        return None

    moved_tm1_sent = conjoin([~PropSymbolExpr(var_str, x, y, t-1) , ~PropSymbolExpr(wall_str, x, y), disjoin(moved_tm1_possibilities)])

    unmoved_tm1_possibilities_aux_exprs = [] # merged variables
    aux_expr_defs = []
    for direction in DIRECTIONS:
        dx, dy = DIR_TO_DXDY_MAP[direction]
        wall_dir_clause = PropSymbolExpr(wall_str, x + dx, y + dy) & PropSymbolExpr(direction, t - 1)
        wall_dir_combined_literal = PropSymbolExpr(wall_str + direction, x + dx, y + dy, t - 1)
        unmoved_tm1_possibilities_aux_exprs.append(wall_dir_combined_literal)
        aux_expr_defs.append(wall_dir_combined_literal % wall_dir_clause)

    unmoved_tm1_sent = conjoin([
        PropSymbolExpr(var_str, x, y, t-1),
        disjoin(unmoved_tm1_possibilities_aux_exprs)])

    return conjoin([PropSymbolExpr(var_str, x, y, t) % disjoin([moved_tm1_sent, unmoved_tm1_sent])] + aux_expr_defs)

# **************
#  Question 3
# **************
def pacphysics_axioms(t, all_coords, non_outer_wall_coords):
    """
    Given:
        t: timestep
        all_coords: list of (x, y) coordinates of the entire problem
        non_outer_wall_coords: list of (x, y) coordinates of the entire problem,
            excluding the outer border (these are the actual squares pacman can
            possibly be in)
    Return a logic sentence containing all of the following:
        - for all (x, y) in all_coords:
            If a wall is at (x, y) --> Pacman is not at (x, y)
        - Pacman is at exactly one of the squares at timestep t.
        - Pacman takes exactly one action at timestep t.
    """
    pacphysics_sentences = []

    "*** BEGIN YOUR CODE HERE ***"
    pac_pos_list = []
    pac_act_list = []
    for x, y in all_coords:
        A = PropSymbolExpr(pacman_str, x, y, t) # whether or not Pacman is at (x, y) at time t
        B = PropSymbolExpr(wall_str, x, y) # whether or not a wall is at (x, y)
        pacphysics_sentences.append(B >> ~A)
    for x, y in non_outer_wall_coords:
        A = PropSymbolExpr(pacman_str, x, y, t) # whether or not Pacman is at (x, y) at time t
        pac_pos_list.append(A)
    pacphysics_sentences.append(exactlyOne(pac_pos_list))
    for action in DIRECTIONS:
        C = PropSymbolExpr(action, t) # whether or not pacman takes action action at time t, where action is an element of DIRECTIONS
        pac_act_list.append(C)
    pacphysics_sentences.append(exactlyOne(pac_act_list))
    "*** END YOUR CODE HERE ***"

    return conjoin(pacphysics_sentences)

# **************
#  Question 3
# **************
def check_location_satisfiability(x1_y1, x0_y0, action0, action1, problem):
    """
    Given:
        - x1_y1 = (x1, y1), a potential location at time t = 1
        - x0_y0 = (x0, y0), Pacman's location at time t = 0
        - action0 = one of the four items in DIRECTIONS, Pacman's action at time t = 0
        - problem = An instance of logicAgents.LocMapProblem
    Return:
        - a model proving whether Pacman is at (x1, y1) at time t = 1
        - a model proving whether Pacman is not at (x1, y1) at time t = 1
    """
    walls_grid = problem.walls
    walls_list = walls_grid.asList()
    all_coords = list(itertools.product(range(problem.getWidth()+2), range(problem.getHeight()+2)))
    non_outer_wall_coords = list(itertools.product(range(1, problem.getWidth()+1), range(1, problem.getHeight()+1)))
    KB = []
    x0, y0 = x0_y0
    x1, y1 = x1_y1

    # We know which coords are walls:
    map_sent = [PropSymbolExpr(wall_str, x, y) for x, y in walls_list]
    KB.append(conjoin(map_sent))

    "*** BEGIN YOUR CODE HERE ***"
    t = 0
    KB.append(PropSymbolExpr(pacman_str, x0, y0, t))
    KB.append(pacphysics_axioms(t, all_coords, non_outer_wall_coords))
    KB.append(PropSymbolExpr(action0, t))
    KB.append(allLegalSuccessorAxioms(t+1, walls_grid, non_outer_wall_coords))
    t = 1
    KB.append(pacphysics_axioms(t, all_coords, non_outer_wall_coords))
    KB.append(PropSymbolExpr(action1, t))
    A = PropSymbolExpr(pacman_str, x1, y1, t)  # whether or not Pacman is at (x, y) at time t
    model1 = KB.copy()
    model2 = KB.copy()
    model1.append(~A)
    model2.append(A)
    return (findModel(conjoin(model1)), findModel(conjoin(model2)))
    "*** END YOUR CODE HERE ***"

# **************
#  Question 4
# **************
def positionLogicPlan(problem):
    """
    Given an instance of a PositionPlanningProblem, return a list of actions that lead to the goal.
    Available actions are ['North', 'East', 'South', 'West']
    Note that STOP is not an available action.
    """
    walls = problem.walls
    width, height = problem.getWidth(), problem.getHeight()
    walls_list = walls.asList()
    x0, y0 = problem.startState
    xg, yg = problem.goal

    # Get lists of possible locations (i.e. without walls) and possible actions
    all_coords = list(itertools.product(range(width + 2),
            range(height + 2)))

    non_wall_coords = [loc for loc in all_coords if loc not in walls_list]
    actions = [ 'North', 'South', 'East', 'West' ]
    KB = []

    "*** BEGIN YOUR CODE HERE ***"
    t = 0
    KB.append(PropSymbolExpr(pacman_str, x0, y0, t))
    for t in range(51):
        pac_pos_list = []
        for x, y in non_wall_coords:
            A = PropSymbolExpr(pacman_str, x, y, t)  # whether or not Pacman is at (x, y) at time t
            pac_pos_list.append(A)
        KB.append(exactlyOne(pac_pos_list))
        if findModel(conjoin(KB + [PropSymbolExpr(pacman_str, xg, yg, t)])):
            return extractActionSequence(findModel(conjoin(KB + [PropSymbolExpr(pacman_str, xg, yg, t)])), actions)
        pac_act_list = []
        for action in actions:
            C = PropSymbolExpr(action, t)  # whether or not pacman takes action action at time t, where action is an element of DIRECTIONS
            pac_act_list.append(C)
        KB.append(exactlyOne(pac_act_list))
        for x, y in non_wall_coords:
            KB.append(pacmanSuccessorStateAxioms(x, y, t+1, walls, var_str=pacman_str))
    "*** END YOUR CODE HERE ***"

# Helpful Debug Method
def visualize_coords(coords_list, problem):
    wallGrid = game.Grid(problem.walls.width, problem.walls.height, initialValue=False)
    for (x, y) in itertools.product(range(problem.getWidth()+2), range(problem.getHeight()+2)):
        if (x, y) in coords_list:
            wallGrid.data[x][y] = True
    print(wallGrid)

# Helpful Debug Method
def visualize_bool_array(bool_arr, problem):
    wallGrid = game.Grid(problem.walls.width, problem.walls.height, initialValue=False)
    wallGrid.data = copy.deepcopy(bool_arr)
    print(wallGrid)

def sensorAxioms(t, non_outer_wall_coords):
    all_percept_exprs = []
    combo_var_def_exprs = []
    for direction in DIRECTIONS:
        percept_exprs = []
        dx, dy = DIR_TO_DXDY_MAP[direction]
        for x, y in non_outer_wall_coords:
            combo_var = PropSymbolExpr(pacman_wall_str, x, y, t, x + dx, y + dy)
            percept_exprs.append(combo_var)
            combo_var_def_exprs.append(combo_var % (
                PropSymbolExpr(pacman_str, x, y, t) & PropSymbolExpr(wall_str, x + dx, y + dy)))

        percept_unit_clause = PropSymbolExpr(blocked_str_map[direction], t)
        all_percept_exprs.append(percept_unit_clause % disjoin(percept_exprs))

    return conjoin(all_percept_exprs + combo_var_def_exprs)

def four_bit_percept_rules(t, percepts):
    """
    Localization and Mapping both use the 4 bit sensor, which tells us True/False whether
    a wall is to pacman's north, south, east, and west.
    """
    assert isinstance(percepts, list), "Percepts must be a list."
    assert len(percepts) == 4, "Percepts must be a length 4 list."

    percept_unit_clauses = []
    for wall_present, direction in zip(percepts, DIRECTIONS):
        percept_unit_clause = PropSymbolExpr(blocked_str_map[direction], t)
        if not wall_present:
            percept_unit_clause = ~PropSymbolExpr(blocked_str_map[direction], t)
        percept_unit_clauses.append(percept_unit_clause) # The actual sensor readings
    return conjoin(percept_unit_clauses)

def num_adj_walls_percept_rules(t, percepts):
    """
    SLAM uses a weaker num_adj_walls sensor, which tells us how many walls pacman is adjacent to
    in its four directions.
        000 = 0 adj walls.
        100 = 1 adj wall.
        110 = 2 adj walls.
        111 = 3 adj walls.
    """
    assert isinstance(percepts, list), "Percepts must be a list."
    assert len(percepts) == 3, "Percepts must be a length 3 list."

    percept_unit_clauses = []
    num_adj_walls = sum(percepts)
    for i, percept in enumerate(percepts):
        n = i + 1
        percept_literal_n = PropSymbolExpr(geq_num_adj_wall_str_map[n], t)
        if not percept:
            percept_literal_n = ~percept_literal_n
        percept_unit_clauses.append(percept_literal_n)
    return conjoin(percept_unit_clauses)

def allLegalSuccessorAxioms(t, walls_grid, non_outer_wall_coords):
    all_xy_succ_axioms = []
    for x, y in non_outer_wall_coords:
        xy_succ_axiom = pacmanSuccessorStateAxioms(
            x, y, t, walls_grid, var_str=pacman_str)
        if xy_succ_axiom:
            all_xy_succ_axioms.append(xy_succ_axiom)
    return conjoin(all_xy_succ_axioms)

# Abbreviations
plp = positionLogicPlan
# Sometimes the logic module uses pretty deep recursion on long expressions
sys.setrecursionlimit(100000)

Pacman intelligent agents AI/Project 3: Propositional logic-based agents/logicPlan.py


import cv2 as cv
import numpy as np
from math import atan

# deskews picture with i pixels
# returns deskewed picture
def deskew(img, i):

    rows = img.shape[0]
    cols = img.shape[1]
    pts1 = np.float32([[0, 0], [cols - 1,  0], [cols - 1 + i, rows - 1]])
    pts2 = np.float32([[0, 0], [cols - 1, 0], [cols - 1, rows - 1]])
    M = cv.getAffineTransform(pts1,pts2)
    dst = cv.warpAffine(img,M,(cols,rows))

    return dst

# crops whitespace from image
def cropWhiteSpace(image):
    coords = cv.findNonZero(image)
    x, y, w, h = cv.boundingRect(coords)
    newImg = image[y:y+h, x:x+w]

    return newImg

def cropWhiteSpace3D(image):
    coords = cv.findNonZero(image[:,:,0])
    coords2 = (cv.findNonZero(image[:,:,1]))
    coords3 = (cv.findNonZero(image[:,:,2]))
    x, y, w, h = cv.boundingRect(coords + coords2 + coords3)
    newImg = image[y:y+h, x:x+w, :]

    return newImg
# deskews over different angles and calculates best based on
# summed squared vertical density
# returns slant angle + deslanted image
def slantNormalize(img):

    imgs = []
    ss = []
    for i in range(50):
        # pad whitespace to left border
        dst = cv.copyMakeBorder(img, 0, 0, 50, 0, cv.BORDER_CONSTANT)
        newImg = deskew(dst, i)

        # crop image
        newImg = cropWhiteSpace(newImg)
        y, x = newImg.shape

        s = 0
        for i in range(x):
            s += sum(newImg[:,i]) ** 2

        imgs.append(newImg)
        ss.append(s)

    finalS = np.argmax(ss)
    finalImg = imgs[finalS]

    y, x = finalImg.shape
    angle = atan(finalS / y)

    return angle, finalImg


import sys
from random import randint
import pygame
from utils.models import Game, GameElement

pygame.init()

game = Game(width=900, height=700, color=(32, 52, 81))
game.display.set_caption("Awesome Shooter Game")
dimentions = (game.screen.get_width(), game.screen.get_height())

fighter = GameElement('Fighter', './images/fighter.png', dimentions)
fighter.move_by_x(game.screen.get_width() / 2 - fighter.width / 2)
fighter.move_by_y(game.screen.get_height() - fighter.height)
fighter.set_step(0.5)

ball = GameElement('Ball', './images/ball.png', dimentions)
ball.set_step(0.3)
ball.hide()

alien = GameElement('Alien', './images/alien.png', dimentions)
alien.set_step(0.1)
alien.set_position(randint(0, game.screen.get_width() - alien.width), 0)

game_is_running = True

while game_is_running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT and fighter.can_move_left():
                fighter.move_left()
            if event.key == pygame.K_RIGHT and fighter.can_move_right():
                fighter.move_right()
            if event.key == pygame.K_SPACE:
                ball.show()
                ball.move_by_x(fighter.x + fighter.width / 2 - ball.width / 2)
                ball.move_by_y(fighter.y - ball.height)
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                fighter.stop_moving_left()
            if event.key == pygame.K_RIGHT:
                fighter.stop_moving_right()

    fighter.keep_moving_left()
    fighter.keep_moving_right()

    alien.move_down()

    if ball.is_visible and ball.is_above_up():
        ball.hide()

    if ball.is_visible:
        ball.move_up()

    game.fill()
    game.blit_element(fighter)
    game.blit_element(alien)

    if ball.is_visible:
        game.blit_element(ball)

    game.display.update()

    if alien.is_under_down():
        game_is_running = False

game_over_text = game.font.render("Game Over", True, "white")
game_over_rectangle = game_over_text.get_rect()
game_over_rectangle.center = (game.screen.get_width() / 2, game.screen.get_height() / 2)
game.screen.blit(game_over_text, game_over_rectangle)
pygame.display.update()
pygame.time.wait(5000)

pygame.quit()


from DataManager import dataBase
from telethon import TelegramClient
import asyncio
import requests
from LogData import logger, getDateTime
from utilities import *

logger.info('default', 'Basic Startup complete!!')
userData = dataBase.getUserData()
#function to send the telegram messages
async def sendMessage(data,message):
    data['session'] = message
    print(message)
    for user in data['user']:
        try:
            await client.send_message(user, message)
            logger.info('SlotSuccess', f"to {user} {msg}")
        except Exception as e:
            logger.error('SendFail', f"{msg}\n\nUSER: {user} \nEXCEPTION: {' , '.join(e.args)}")

async def filterData(session, age, data):
    # process the data and get relevant data
    message = processData(session, age)
    #check if the message is not empty. else do not send any message on group
    if(len(message)):
        #check if the message is same as that sent before to avoid repetition of multiple same messages
        if data['session'] != message:
            await sendMessage(data, message)
        else:
            logger.warning('SlotSame', msg)
    else:
        logger.info('SlotFail', msg)

#Telegram Authorization and login function
async def login():
    account = dataBase.getTelegramCredentials()
    global client
    client = TelegramClient( account['Name'], account['Id'], account['Hash'] )
    logger.info('CliStrt')
    try:
        await client.start()
        logger.info('StartSuccess')
        return True
    except Exception as e:
        logger.error('StartFailed')
        return False

async def main():

    #try to login until successful
    while(not await login()):
        pass

    while(1):
        counter = 0
        userData = dataBase.getUserData()
        sleepTime = dataBase.getSleepTime()
        t,date =getDateTime()
        for district,data in userData.items():
            url = f"{baseURL}{district}&date={date}"
            response = requests.get(url, headers=headers)
            global msg
            msg = f"at PINCODE: {district}"

            #check if the request was processed successfully.
            if response.ok :
                session= response.json()
                for age,value in data.items():
                    if len(value['user']):
                        await filterData(session,int(age),value)

            else:
                logger.error('ApiFail', f"{msg} status code: {str(response.status_code)} REASON: {response.reason}")
            counter  += 1
            await asyncio.sleep(sleepTime)
            if(counter > 10):
                dataBase.loadData()

asyncio.run(main())


import pygame

import time

import math

import collections

import random

ifsetup = True

thestat = 0

# setting up pygame stuff

pygame.init()

infoObject = pygame.display.Info()

height = infoObject.current_h

width = infoObject.current_w

print("Screen Resolution: %s, %s" % (infoObject.current_w, infoObject.current_h))

gameDisplay = pygame.display.set_mode((infoObject.current_w, infoObject.current_h), pygame.FULLSCREEN)

clock = pygame.time.Clock()

pygame.display.set_caption('UPlan')  # set window title

vertices = []  # where all the vertices are stored

warningMessages = []  # where all the warningMessages are stored

roads = []  # where all the roads are stored

cars = []  # where all the cars are stored

staticTexts = []

connections = {}

ncars=30

#2 vertice input, outputs array of vertices

def myBFScost(state,end):

    queue = PriorityQueue()

    visited = []

    queue.push((state, [],0),0)

    #print(queue.heap)

    while not queue.isEmpty():

        current = queue.pop()

        if current[0]==end:

            return current[1]

        successors = [(i, i,((i.x-current[0].x)**2+(i.x-current[0].x)**2)**0.5) for i in connections[current[0]]]

        successors.reverse()

        for i in successors:

            if i[0] not in visited:

                tempPush = (i[0], [j for j in current[1]],current[2]+i[2])

                tempPush[1].append(i[1])

                visited.append(i[0])

                queue.push(tempPush,tempPush[2])

    return [state,end]

def text_objects(text, font, color):

    textSurface = font.render(text, True, color)

    return textSurface, textSurface.get_rect()

class PriorityQueue:

    """

      Implements a priority queue data structure. Each inserted item

      has a priority associated with it and the client is usually interested

      in quick retrieval of the lowest-priority item in the queue. This

      data structure allows O(1) access to the lowest-priority item.

    """

    def  __init__(self):

        self.heap = []

    def push(self, item, priority):

        self.heap.append((item, priority))

        self.heap.sort(key=lambda x: x[1])

    def pop(self):

        temp=self.heap[0][0]

        self.heap=self.heap[1:]

        return temp

    def isEmpty(self):

        return len(self.heap) == 0

class Car():

    def __init__(self, v1, v2, path=[], v=0, a=0.075):

        if path==[]:

            self.path=myBFScost(v1,v2)

            self.path.insert(0,v1)

            self.v1 = self.path[0]

            self.v2 = self.path[1]

        else:

            self.path=path

            self.v1 = v1

            self.v2 = v2

        self.pos = (v1.x,v1.y)

        self.endpos = (self.path[1].x,self.path[1].y)

        #self.path=myBFScost(v1,v2)

        self.v = v

        self.a = a

        self.t=0

        self.boost = False

        self.boostval = 0

        for r in roads:

            if (self.pos==(int(r.x),int(r.y)) and self.endpos==(int(r.x1),int(r.y1))) or (self.endpos==(int(r.x),int(r.y)) and self.pos==(int(r.x1),int(r.y1))):

                r.onRoad.append(self)

                self.road = r

                break

        #print(self.road)

        self.order=connections[self.v2].index(self.v1)

        self.maxorder=len(connections[self.v2])

        cars.append(self)

    def __str__(self):

        print(self.pos, self.endpos)

    def isTouching(self, x1, y1):

        tempD = ((x1 - self.pos[0]) ** 2 + (y1 - self.pos[1]) ** 2) ** 0.5  # Find distance between them

        if tempD <= 20:

            return True

        else:

            return False

class WarningMessage():

    def __init__(self, message):

        self.message = message

        self.timer = 100  # initialize timer for warning

        warningMessages.append(self)  # add it to the vertices list

    def __str__(self):

        return "[Warning Message: " + self.message  # Prints this when printing warning obj

class StaticText():

    def __init__(self, message):

        self.message = message

        staticTexts.append(self)  # add it to the vertices list

    def __str__(self):

        return "[Static Text: " + self.message  # Prints this when printing warning obj

class Road():

    def __init__(self, x, y, x1, y1):

        self.x, self.y = x, y

        self.x1, self.y1 = x1, y1

        self.width = int(infoObject.current_w / 373.6) * 4

        self.right=[i for i in range(0,self.length()+1)]

        self.left = [i for i in range(0, self.length() + 1)]

        self.onRoad = []

        roads.append(self)

    def __str__(self):

        return "[Road: %s, %s]" % ((self.x, self.y), (self.x1, self.y1))  # Prints this when printing vertex obj

    def length(self):

        return int(((self.x+self.x1)^2+(self.y+self.y1)^2)**0.5)

    def isTouching(self, x3, y3):

        def isTouching(self, x3, y3):

            x1 = self.x1

            x2 = self.x2

            y1 = self.y1

            y2 = self.y2

            slope1 = (y2 - y1) / (x2 - x1)

            slope2 = (1 / slope1) * -1

            c1 = y1 - (slope1 * x1)

            c2 = y3 - (slope2 * x3)

            x4 = (c2 - c1) / (slope1 - slope2)

            y4 = slope1 * x4 + c1

            distance = ((x4 - x3) ** 2 + (y4 - y3) ** 2) ** 1 / 2

            if distance > self.width:

                return False

            c1 = y1 - slope2 * x1

            c2 = y2 - slope2 * x2

            liney1 = slope2 * x1 + c1

            liney2 = slope2 * x2 + c2

            if (liney1 > y3 and liney2 < y3) or (liney1 < y3 and liney2 > y3):

                return True

            else:

                return False

class Vertex():

    def __init__(self, x, y):

        self.x = x

        self.y = y

        self.radius = int(infoObject.current_w / 373.6)  # keeps a constant pixel radius

        self.timer = 100  # time left before disappear

        self.traffic=0

        self.roads = 0

        vertices.append(self)  # add it to the vertices list

        connections[self] = []

    def __str__(self):

        return "[Vertex: %s, %s]" % (self.x, self.y)  # Prints this when printing vertex obj

    def isTouching(self, x1, y1):

        tempD = ((x1 - v.x) ** 2 + (y1 - v.y) ** 2) ** 0.5  # Find distance between them

        if tempD <= v.radius:

            return True

        else:

            return False

def RenderVertices():

    for v in vertices:

        if v.timer%300==0 and v in connections.keys():

            v.traffic = (v.traffic + 1) % (len(connections[v]) + 1)

        v.timer+=1

        if len(connections[v]) > 0:

            pygame.draw.circle(gameDisplay, (0, 0, 0), (v.x, v.y), v.radius * 2)

        if v == vertexSelected:

            pygame.draw.circle(gameDisplay, (0, 255, 0), (v.x, v.y), v.radius)

        else:

            pygame.draw.circle(gameDisplay, (0, 0, 255), (v.x, v.y), v.radius)

def RenderWarningMessages():

    for w in warningMessages:

        font1 = pygame.font.Font('freesansbold.ttf', 40)  # creates a font

        TextSurf, TextRect = text_objects(w.message, font1, (255, 0, 0))  #

        TextRect.center = ((infoObject.current_w / 2), (infoObject.current_h / 2))  # set the center

        gameDisplay.blit(TextSurf, TextRect)  # put it on the screen

        w.timer -= 1

        # print(w.timer)

        if w.timer == 0:

            warningMessages.remove(w)

def RenderStaticText():

    for w in staticTexts:

        font1 = pygame.font.Font('freesansbold.ttf', 40)  # creates a font

        TextSurf, TextRect = text_objects(w.message, font1, (0, 0, 0))  #

        TextRect.center = ((infoObject.current_w / 2), (infoObject.current_h / 2))  # set the center

        gameDisplay.blit(TextSurf, TextRect)  # put it on the screen

def RenderMisc():

    return

def RenderRoads():

    for r in roads:

        pygame.draw.line(gameDisplay, (0, 0, 0), (r.x, r.y), (r.x1, r.y1), r.width*2)

def RenderCars():

    global thestat

    for c in cars:

        #print(c.pos)

        if (abs(round(c.pos[0], 0)) - c.endpos[0])**2 + (abs(round(c.pos[1], 0)) - c.endpos[1])**2<=100:

      # if ((c.pos[0]-c.endpos[0])**2 + (c.pos[1]-c.endpos[1])**2)**0.5<=30:

            if len(c.path)>=3:

                Car(c.path[1],c.path[2],c.path[1:])

            else:

                makeRandomCar()

            cars.remove(c)

            c.road.onRoad.remove(c)

        dx = c.endpos[0] - c.pos[0]

        dy = c.endpos[1] - c.pos[1]

        angle = abs(math.atan(dy / dx))

        vy = math.sin(angle) *c.v

        vx = math.cos(angle) *c.v

        ay = math.sin(angle) *c.a

        ax = math.cos(angle) * c.a

        if dy <= 0 and dx <= 0:

            vy *= -1

            ay *= -1

            ax *= -1

            vx *= -1

        elif dy <= 0 and dx >= 0:

            ay *= -1

            vy *= -1

        elif dy >= 0 and dx <= 0:

            ax *= -1

            vx *= -1

        elif dy >= 0 and dx >= 0:

            ax *= 1

            vx *= 1

        update=True

        for c1 in c.road.onRoad:

                if c!=c1:

                    if ((c1.pos[0]-c.pos[0])**2+(c1.pos[1]-c.pos[1])**2)**0.5<=30 and c.endpos==c1.endpos and c!=c1:

                        if (vx > 0 and c1.pos[0] > c.pos[0]) or (vx < 0 and c1.pos[0] < c.pos[0]):

                            update=False

                            c.t=0

                            c.v=0

                            break

        if update:

            if ((c.endpos[0] - c.pos[0]) ** 2 + (c.endpos[1] - c.pos[1]) ** 2) ** 0.5 <= 60 and c.maxorder>2 and c.v2.traffic!=c.order:

                update=False

                c.t = 0

                c.v = 0

                #print('nope')

        if update:

            if c.v < 2:

                if c.t < 50:

                    c.t += 1

                else:

                    c.v = ((vy + ay) ** 2 + (vx + ax) ** 2) ** 0.5

            c.pos = (c.pos[0] + vx, c.pos[1] + vy)

        if not update:

            v=0

            c.t=0

            #print('nope')

        if abs(dy)>abs(dx):

            if dy<0:

                pygame.draw.circle(gameDisplay, (255, 0, 0), (int(c.pos[0]+10), int(c.pos[1])), 10)

            else:

                pygame.draw.circle(gameDisplay, (255, 0, 0), (int(c.pos[0]-10), int(c.pos[1])), 10)

        else:

            if dx<0:

                pygame.draw.circle(gameDisplay, (255, 0, 0), (int(c.pos[0]), int(c.pos[1]+10)), 10)

            else:

                pygame.draw.circle(gameDisplay, (255, 0, 0), (int(c.pos[0]), int(c.pos[1]-10)), 10)

        thestat += c.v

        #if c.boost == False:

        #    if vx > 0:

        #       c.pos = (c.pos[0], c.pos[1]+20)

        #    else:

        #       c.pos = (c.pos[0],c.pos[1]-20)

        #    c.boost = True

def makeRandomCar():

    keys = [i for i in connections.keys()]

    if len(keys) > 1:

        v1 = keys[random.randint(0, len(keys) - 1)]

        v2 = keys[random.randint(0, len(keys) - 1)]

        while not [i.isTouching(v2.y, v2.x) for i in cars] == [False for i in cars]:

            v2 = keys[random.randint(0, len(keys) - 1)]

        while True:

            if not [i.isTouching(v1.y, v1.x) for i in cars] == [False for i in cars]:

                v1 = keys[random.randint(0, len(keys) - 1)]

            elif len(connections[v1])>1:

                v1 = keys[random.randint(0, len(keys) - 1)]

            elif v1 == v2:

                v1 = keys[random.randint(0, len(keys) - 1)]

            else:

                break

        return Car(v1, v2)

v1 = Vertex(int(infoObject.current_w / 2), int(infoObject.current_h / 2))  # creates new vertex at (0,0)

# w1 = WarningMessage("ERROR: ewyurhbfm,sdfsd")

#r1 = Road(int(infoObject.current_w/2), int(infoObject.current_h/2),1000,1000)

#c1 = Car((1000,700),(700,1000),0,0.075,r1)

# t1 = StaticText("hewo worwd")

windowExit = False

vertexSelected = None

keys = pygame.key.get_pressed()

#print(connections[v1])

ihadtodoittoem=True

shiftheld = False

statstimer=0

while not windowExit:

    #print(len(cars))

    if ifsetup == True:

        # print(keys)

        for event in pygame.event.get():

            keys = pygame.key.get_pressed()

            if event.type == pygame.KEYDOWN and event.key == pygame.K_1:

                ifsetup = False

            if event.type == pygame.KEYDOWN and event.key == pygame.K_LSHIFT:

                shiftheld = True

            if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:

                shiftheld = False

            if event.type == pygame.QUIT:

                windowExit = True

            if event.type == pygame.MOUSEBUTTONDOWN:

                if event.button == 1:  # left click

                    if shiftheld == True:

                        if vertexSelected is not None:

                            for v in vertices:

                                if v.isTouching(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):

                                    connections[vertexSelected].append(v)

                                    connections[v].append(vertexSelected)

                                    tempR = Road(v.x, v.y, vertexSelected.x, vertexSelected.y)

                                    vertexSelected = None

                                    break

                    else:

                        place = True

                        for v in vertices:

                            if v.isTouching(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):

                                place = False

                                vertexSelected = v

                                break

                        if place:

                            tempV = Vertex(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])

                            vertexSelected = None

                if event.button == 3:  # right click

                    for v in vertices:

                        if v.isTouching(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):

                            vertices.remove(v)

                            connections[v] = []

                            connections.pop(v,None)

                            for key in connections.keys():

                                if v in connections[key]:

                                    connections[key].remove(v)

                    for r in roads:

                        if r.isTouching(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):

                            roads.remove(r)

                            v1=None

                            v2=None

                            for v in vertices:

                                if (r.x1,r.y1)==(v.x,v.y) or (r.x1,r.y1)==(v.x,v.y):

                                    if v1==None:

                                        v1=v

                                    else:

                                        v2=v

                            connections[v1].remove(v2)

                            connections[v2].remove(v1)

    if ifsetup == False:

        statstimer += 1

    if statstimer%10000 == 9999:

        print(thestat/statstimer)

    if ifsetup == False and ihadtodoittoem:

        ihadtodoittoem=False

        while len(cars)<ncars:

            makeRandomCar()

                                    # print(event)

    # print(cars)

    # Layers

    # print(t1,staticTexts)

    gameDisplay.fill((255, 255, 255))  # make background white

    RenderRoads()  # Draw Roads

    RenderVertices()  # Draw Vertices

    RenderCars()  # Draw Cars

    RenderMisc()

    RenderStaticText()

    RenderWarningMessages()  # DrawWarningMessages

    pygame.display.update()


'''
breadth first values

Write a function, breadth_first_values, that takes in the root of a binary tree. The function should return a list containing all values of the tree in breadth-first order.
test_00:

a = Node('a')
b = Node('b')
c = Node('c')
d = Node('d')
e = Node('e')
f = Node('f')

a.left = b
a.right = c
b.left = d
b.right = e
c.right = f

#      a
#    /   \
#   b     c
#  / \     \
# d   e     f

breadth_first_values(a)
#    -> ['a', 'b', 'c', 'd', 'e', 'f']

test_01:

a = Node('a')
b = Node('b')
c = Node('c')
d = Node('d')
e = Node('e')
f = Node('f')
g = Node('g')
h = Node('h')

a.left = b
a.right = c
b.left = d
b.right = e
c.right = f
e.left = g
f.right = h

#      a
#    /   \
#   b     c
#  / \     \
# d   e     f
#    /       \
#   g         h

breadth_first_values(a)
#   -> ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
'''

# class Node:
#   def __init__(self, val):
#     self.val = val
#     self.left = None
#     self.right = None

def breadth_first_values(root):
  if root is None:
    return []
  result = []
  queue = [ root ]

  while queue:
    current = queue.pop()
    result.append(current.val)

    if current.left:
      queue.insert(0, current.left)
    if current.right:
      queue.insert(0, current.right)

  return result

'''
APPROACH
- it is much easier to "process" the value as it leaves the queue
'''

# use a deque instead
from collections import deque

def breadth_first_values(root):
  if root is None:
    return []
  values = []
  queue = deque([root])

  while queue:
    current = queue.popleft()
    values.append(current.val)

    if current.left:
      queue.append(current.left)

    if current.right:
      queue.append(current.right)

  return values


import torch
import torchvision

from PIL import Image

# img = Image.open('PennFudanPed/PNGImages/FudanPed00001.png')
#
# mask = Image.open('PennFudanPed/PedMasks/FudanPed00001_mask.png')
#
# mask.putpalette([
#     0, 0, 0,  # black background
#     255, 0, 0,  # index 1 is red
#     255, 255, 0,  # index 2 is yellow
#     255, 153, 0,  # index 3 is orange
# ])
#
#
# Image._show(img)
# Image._show(mask)
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from torchvision.models.detection.mask_rcnn import MaskRCNNPredictor

import utils
import transforms as T
from ObjDetectionFinetuning import PennFudanDataset, get_transform

def get_model_instance_segmentation(num_classes):
    # load an instance segmentation model pre-trained pre-trained on COCO
    model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)

    # get number of input features for the classifier
    in_features = model.roi_heads.box_predictor.cls_score.in_features
    # replace the pre-trained head with a new one
    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)

    # now get the number of input features for the mask classifier
    in_features_mask = model.roi_heads.mask_predictor.conv5_mask.in_channels
    hidden_layer = 256
    # and replace the mask predictor with a new one
    model.roi_heads.mask_predictor = MaskRCNNPredictor(in_features_mask,
                                                       hidden_layer,
                                                       num_classes)

    return model

def get_transform(train):
    transforms = []
    transforms.append(T.ToTensor())
    if train:
        transforms.append(T.RandomHorizontalFlip(0.5))
    return T.Compose(transforms)

device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

# our dataset has two classes only - background and person
num_classes = 2
# use our dataset and defined transformations
dataset = PennFudanDataset('PennFudanPed', get_transform(train=True))
dataset_test = PennFudanDataset('PennFudanPed', get_transform(train=False))

# split the dataset in train and test set
indices = torch.randperm(len(dataset)).tolist()
dataset_test = torch.utils.data.Subset(dataset_test, indices[-50:])

data_loader_test = torch.utils.data.DataLoader(
    dataset_test, batch_size=1, shuffle=False, num_workers=4,
    collate_fn=utils.collate_fn)
model = get_model_instance_segmentation(2)
# model = torch.load(r'mask_R_CNN.pth')
model.load_state_dict(torch.load(r'mask_R_CNN.pth'))
# pick one image from the test set
# img, _ = dataset_test[0]
img_raw = Image.open('./TestImages/FudanPed00005.png').convert("RGB")
img = torchvision.transforms.functional.to_tensor(img_raw)
# put the model in evaluation mode
model.eval()
with torch.no_grad():
    prediction = model([img.to(device)])
print(prediction)
im = Image.fromarray(img.mul(255).permute(1, 2, 0).byte().numpy())
Image._show(im)
im2 = Image.fromarray(prediction[0]['masks'][0, 0].mul(255).byte().cpu().numpy())
Image._show(im2)
print("That's it!")


from typing import List

class Solution:

    def reorderLogFiles(self, logs: List[str]) -> List[str]:
        digit_logs = []
        letter_logs = []

        for log in logs:
            if log.split()[1].isdigit():
                digit_logs.append(log)
            else:
                letter_logs.append(log)

        letter_logs.sort(key=lambda x: (x.split()[1:], x.split()[0]))

        return letter_logs + digit_logs

Leetcode_937_Reorder Data in Log Files.py


# https://nasa-openscapes.github.io/2021-Cloud-Hackathon/tutorials/05_Data_Access_Direct_S3.html
# Instructions for getting access to the NASA data: https://urs.earthdata.nasa.gov/documentation/for_users/data_access/curl_and_wget

# imports
from datetime import datetime, timedelta
import os
import requests
import boto3
import numpy as np
import xarray as xr
import rasterio as rio
from rasterio.session import AWSSession

import rioxarray
import pandas as pd
import json
import rasterio
import rasterio.mask
from loguru import logger

START_IDX = 0 # index starts from 0
END_IDX = 2  # None means to finish until end (index also starts at 0)
FAIL_LOG_FP = "failure_indices.txt"
DATA_DIR = "../data"                            # where data is stored (csv, json reference files)
DATE_STR_FMT = '%Y-%m-%d'
OUT_DIR = "/Volumes/R Sandisk SSD/hls_tmp"    # folder where data will be downloaded to

ssc_json_path = f'{DATA_DIR}/ssc_sample_2573.json'
aqsat_path = f'{DATA_DIR}/Aqusat_TSS_v1.csv'
aqsat_path = f'{DATA_DIR}/L08_data_exactloc.csv'
s3_cred_endpoint = 'https://data.lpdaac.earthdatacloud.nasa.gov/s3credentials'

if not os.path.exists(OUT_DIR):
    os.makedirs(OUT_DIR)

def get_temp_creds():
    temp_creds_url = s3_cred_endpoint
    return requests.get(temp_creds_url).json()

def read_obs(obs, json_data, exact_date=False, entry=999, lc_idx=None):
    """NOTE: only one of exact_date and approx_date should be True"""

    lat, lon = str(obs['lat']), str(obs['long'])
    dict_key = str(lon) +','+ str(lat)
    logger.debug(f"Getting data for dict_key={dict_key}, date={obs['date']}")

    if exact_date:
        date = obs['date']
    elif entry!=999:
        date = list(json_data[dict_key].keys())[entry]
    else:
        logger.debug('Please select a date or entry')
        return

    try:
        hls_names = json_data[dict_key][date]
    except KeyError:
        logger.error(f"KeyError: {dict_key} not found in `json_data`")
        with open(FAIL_LOG_FP, "a") as fp:
            fp.write(f"[{dict_key}, {date}]: {lc_idx} not found in `json_data`,\n")
        return [] # return no data

    return hls_names

logger.debug("Loading data from reference files")

# data import
aqsat = pd.read_csv(aqsat_path)
with open(ssc_json_path, 'r') as f:
    data = json.load(f)

logger.debug("Getting temp credentials for downloading HLS data")
temp_creds_req = get_temp_creds()

# load boto
session = boto3.Session(
    aws_access_key_id=temp_creds_req['accessKeyId'],
    aws_secret_access_key=temp_creds_req['secretAccessKey'],
    aws_session_token=temp_creds_req['sessionToken'],
    region_name='us-west-2'
)

rio_env = rio.Env(AWSSession(session),
    GDAL_DISABLE_READDIR_ON_OPEN='EMPTY_DIR',
    GDAL_HTTP_COOKIEFILE=os.path.expanduser('~/cookies.txt'),
    GDAL_HTTP_COOKIEJAR=os.path.expanduser('~/cookies.txt')
)

rio_env.__enter__()

for lc_idx in range(len(aqsat)):
    if lc_idx < START_IDX:
        continue
    if END_IDX and lc_idx > END_IDX:
        continue
    logger.info(f"Landsat data {lc_idx:03d}/{len(aqsat)-1}")
    obs = aqsat.iloc[lc_idx]

    lat, long = obs["lat"], obs["long"]
    key = str(long) +','+ str(lat)

    # Get all dates, +/-1 day from listed day (including original date)
    date_orig = obs['date']
    date_obj = datetime.strptime(date_orig, DATE_STR_FMT).date()
    dates = [
        datetime.strftime(date_obj - timedelta(x), DATE_STR_FMT)
        for x in [-1,0,1]
    ]

    for date in dates:
        if not os.path.exists(os.path.join(OUT_DIR, key)):          # make folder with lat,long as name
            os.makedirs(os.path.join(OUT_DIR, key))
        if not os.path.exists(os.path.join(OUT_DIR, key, date)):    # make folder with date as name
            os.makedirs(os.path.join(OUT_DIR, key, date))

        hls_links = read_obs(obs, data, exact_date=True, lc_idx=lc_idx)

        datetime_filter = None
        tile_id_filter = None
        for idx, url in enumerate(sorted(hls_links)):
            # TODO: use time from aqsat to filter the links (i.e., get data closest to aqsat `date_utc`)
            # TODO: need to add time in json file if needed (since there can be multiple tiles in a day)
            logger.debug(f"Getting data from: {url}")

            # send a HTTP request and save
            fp_out = os.path.join(OUT_DIR, key, date, url.split("/")[-1])
            r = requests.get(url) # create HTTP response object
            with open(fp_out,'wb') as f:
                f.write(r.content)

            # rx_data = rioxarray.open_rasterio(url, chuncks=True, masked=False).squeeze()
            # # rx_data = rasterio.open(url, chuncks=True, masked=True, parse_coordinates=True)

            # # Write to TIFF and save
            # fp_out = os.path.join(OUT_DIR, key, date, url.split("/")[-1])
            # logger.debug(f"Saving band to file: {fp_out}")
            # try:
            #     rx_data.rio.to_raster(fp_out)
            # except rasterio.errors.RasterioIOError:
            #     logger.error(f"Error saving file to {fp_out}")
            #     with open(FAIL_LOG_FP, "a") as fp:
            #         fp.write(f"RIO ERROR [{lat},{long}, {date}]: {lc_idx},\n")


from requests.exceptions import RequestException
from tkinter import Button, Tk, Toplevel, Label
from tkinter.messagebox import askyesno
from pkg_resources import parse_version
from configparser import ConfigParser
from tkinter.ttk import Progressbar
from win32api import ShellExecute
from PIL import Image, ImageTk
from os import path as os_path
from threading import Thread
from requests import get
from pathlib import Path

__version__ = '0.8.01'
__author__ = "TerraBoii"
# If you want to get less automated version check this project:
# https://github.com/vsantiago113/Tkinter-MyTestApp
_AppName_ = 'Math problem generator app'

parser = ConfigParser()
parser.read("data.txt")
lang = parser.get("language", "language")

# url for installer
url = "https://github.com/TerraBoii/math_problem_generator_app/raw/main/app_installer/mathproblemgenerator_setup.exe"
file_name = url.split('/')[-1].replace(" ", "_")
file_path = os_path.join("setup", file_name)

class UpdateManager(Toplevel):
    def __init__(self, parent):
        Toplevel.__init__(self, parent)

        self.transient(parent)
        self.attributes("-disabled", True)
        self.grab_set()
        self.geometry(f'{350}x{200}+{int((self.winfo_screenwidth() - 350) / 2)}+{int((self.winfo_screenheight() - 200) / 2)}')
        self.resizable(width=False, height=False)
        self.title('Update Manager')
        self.wm_iconbitmap('images/update_icon.ico')

        image = Image.open('images/update_manager.jpg')
        photo = ImageTk.PhotoImage(image)
        manager_holder = Label(self, image=photo)
        manager_holder.image = photo
        manager_holder.pack()

        def install_update():
            tmp.after(0, tmp.destroy)
            ShellExecute(0, 'open', file_path, None, None, 10)

        def start_update_manager():
            destination_folder = "setup"
            if not os_path.exists(destination_folder):
                destination_folder = str(Path.home() / "Downloads")  # create folder if it does not exist
            with get(url=url, stream=True) as r:
                self.progressbar['maximum'] = int(r.headers.get('Content-Length'))
                r.raise_for_status()
                with open(file_path, 'wb') as f:
                    for chunk in r.iter_content(chunk_size=4096):
                        if chunk:  # filter out keep-alive new chunks
                            f.write(chunk)
                            self.progressbar['value'] += 4096
            self.attributes("-disabled", False)
            install_update()

        self.progressbar = Progressbar(self,
                                       orient='horizontal',
                                       length=200,
                                       mode='determinate',
                                       value=0,
                                       maximum=0)
        self.progressbar.place(relx=0.5, rely=0.5, anchor="center")
        self.wait_text = Button(self, text='Wait!', state="disabled")
        self.wait_text.place(x=-83, relx=1.0, y=-33, rely=1.0)

        self.start_manager = Thread(target=start_update_manager)
        self.start_manager.start()

def run(): # Execute main .exe and destroy tmp window
    tmp.after(0, tmp.destroy)
    ShellExecute(0, 'open', f'binaries\\{_AppName_}.exe', None, None, 10)

def update(tmp, data):
    parser = ConfigParser()
    parser.read("data.txt")
    if parser.get("info", "auto_update") == "False":
        if lang == "rus":
            title = 'Доступно обновление приложения!'
            text = f"{_AppName_} '{__version__}' должно быть обновлено до версии '{data}'."
        else:
            title = 'Software update available!'
            text = f"{_AppName_} '{__version__}' needs to update to version '{data}'."
        get_update = askyesno(title, text)
        if get_update is True:
            UpdateManager(tmp)
        elif get_update is False:
            run()
    else:
        UpdateManager(tmp)

if __name__ == "__main__":

    tmp = Tk()  # Tmp -> short for temporary
    tmp.iconbitmap("images/update_icon.ico")
    # positioning window in middle of the screen with width = 650 and height = 400
    tmp.geometry(f"{650}x{400}+{int((tmp.winfo_screenwidth() - 650) / 2)}+{int((tmp.winfo_screenheight() - 400) / 2)}")
    tmp.withdraw()  # Making tmp window "invisible"

    try:

        response = get('https://raw.githubusercontent.com/TerraBoii/math_problem_generator_app/main/version.txt')
        data = response.text

        if parse_version(data) > parse_version(__version__):
            update(tmp, data)
        else:
            run()
    except RequestException:  # Something went wrong during requesting
        run()

    tmp.mainloop()


from constants import *
from dataset import pad_sequences

from utils import Timer
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split

import numpy as np
import tensorflow as tf
import os
import shutil

if not os.path.exists(MODEL_PATH):
    os.makedirs(MODEL_PATH)

shutil.copy2('constants.py', MODEL_PATH)

seed = 13
np.random.seed(seed)

class Pointwise:
    def __init__(self, embeddings, data):
        self.embeddings = embeddings
        self.data = data

        self.word_embedding_dim = WORD_EMBEDDING_DIM
        self.filter_sizes = FILTER_SIZES
        self.num_filters = NUM_FILTERS
        self.dropout_keep_prob = DROPOUT_KEEP_PROB
        self.l2_reg_lambda = L2_REG_LAMBDA
        self.learning_rate = LEARNING_RATE
        self.max_input_word = MAX_INPUT_WORD
        self.hidden_num = HIDDEN_NUM

        self.batch_size = BATCH_SIZE
        self.trainable = TRAINABLE
        self.num_epochs = NUM_EPOCHS
        self.is_early_stopping = IS_EARLY_STOPPING

        self.para = []

    def load_data(self):
        timer = Timer()
        timer.start("Loading start")

        self.data = self.data.data
        all_idx = list(self.data.keys())

        if self.is_early_stopping:
            self.train_idx, self.dev_idx = train_test_split(all_idx, test_size=0.3, random_state=seed)
            print("Number of validating mentions: {}".format(len(self.dev_idx)))
        else:
            self.train_idx = all_idx
        print("Number of training mentions: {}".format(len(self.train_idx)))

        timer.stop()

    def build(self):
        self._add_placeholder()
        self._add_model_op()
        self._add_train_op()

    def _add_placeholder(self):
        self.m_word_ids = tf.placeholder(tf.int32, [None, self.max_input_word], name="m_word_ids")
        self.n_word_ids = tf.placeholder(tf.int32, [None, self.max_input_word], name="n_word_ids")

        self.y = tf.placeholder(tf.int32, [None, 2], name="input_y")
        self.idx = tf.placeholder(tf.int32, [None], name="idx")

        self.dropout_keep_op = tf.placeholder(tf.float32, name="dropout_keep_prob")

    def _add_model_op(self):
        # embeddings
        with tf.name_scope('embeddings'):
            _word_embeddings = tf.Variable(self.embeddings, name="word_embeddings", dtype=tf.float32, trainable=TRAINABLE)
            self.m_word_embedding = self._get_embedding(_word_embeddings, self.m_word_ids)
            self.n_word_embedding = self._get_embedding(_word_embeddings, self.n_word_ids)

        # convolution
        self.kernels = []
        for i, filter_size in enumerate(self.filter_sizes):
            with tf.name_scope('conv-pool-%s' % filter_size):
                filter_shape = [filter_size, self.word_embedding_dim, 1, self.num_filters]
                W = tf.get_variable('W' + str(i), filter_shape, tf.float32,
                                    tf.contrib.layers.variance_scaling_initializer(factor=1.0, mode='FAN_IN', uniform=True))
                b = tf.get_variable('b' + str(i), [self.num_filters], tf.float32, tf.constant_initializer(0.01))
                self.kernels.append((W, b))
                self.para.append(W)
                self.para.append(b)

        self.num_filters_total = self.num_filters * len(self.filter_sizes)

        self.m_conv = self._convolution(self.m_word_embedding)
        self.n_conv = self._convolution(self.n_word_embedding)

        # max pooling
        with tf.name_scope('max_pooling'):
            self.m_pooling = tf.reshape(self._max_pooling(self.m_conv, self.max_input_word), [-1, self.num_filters_total])
            self.n_pooling = tf.reshape(self._max_pooling(self.n_conv, self.max_input_word), [-1, self.num_filters_total])

        with tf.variable_scope("similarity"):
            W = tf.get_variable(
                name="W",
                shape=[self.num_filters_total, self.num_filters_total],
                initializer=tf.contrib.layers.xavier_initializer()
            )
            self.transform_left = tf.matmul(self.m_pooling, W)
            self.sims = tf.reduce_sum(tf.multiply(self.transform_left, self.n_pooling), axis=1, keep_dims=True)
            self.para.append(W)
            self.see = W

        self.feature = tf.concat([self.m_pooling, self.n_pooling], axis=1, name="feature")

        with tf.name_scope('neural_network'):
            W = tf.get_variable(
                "W_hidden",
                shape=[2 * self.num_filters_total, self.hidden_num],
                initializer=tf.contrib.layers.xavier_initializer()
            )
            b = tf.get_variable('b_hidden', shape=[self.hidden_num], initializer=tf.random_normal_initializer())
            self.para.append(W)
            self.para.append(b)
            self.hidden_output = tf.nn.relu(tf.nn.xw_plus_b(self.feature, W, b, name="hidden_output"))

        with tf.name_scope('dropout'):
            self.h_drop = tf.nn.dropout(self.hidden_output, self.dropout_keep_prob, name="hidden_ouput_drop")

        with tf.name_scope('output'):
            W = tf.get_variable(
                "W_output",
                shape=[self.hidden_num, 2],
                initializer=tf.contrib.layers.xavier_initializer()
            )
            b = tf.get_variable('b_output', shape=[2], initializer=tf.random_normal_initializer())
            self.para.append(W)
            self.para.append(b)
            self.logits = tf.nn.xw_plus_b(self.h_drop, W, b, name="scores")
            self.scores = tf.nn.softmax(self.logits)
            self.predictions = tf.argmax(self.scores, 1, name="predictions")

    def _add_train_op(self):
        l2_loss = tf.constant(0.0)
        for p in self.para:
            l2_loss += tf.nn.l2_loss(p)
        with tf.name_scope("loss"):
            losses = tf.nn.softmax_cross_entropy_with_logits(logits=self.logits, labels=self.y)
            self.loss_op = tf.reduce_mean(losses) + self.l2_reg_lambda * l2_loss

        # Accuracy
        with tf.name_scope("accuracy"):
            correct_predictions = tf.equal(self.predictions, tf.argmax(self.y, 1))
            self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")

        optimizer = tf.train.AdamOptimizer(self.learning_rate)
        self.train_op = optimizer.minimize(self.loss_op)

    def train(self):
        self._train(self.num_epochs)

    def _train(self, epochs, patience=4):

        if not os.path.exists(MODEL_PATH):
            os.makedirs(MODEL_PATH)

        saver = tf.train.Saver(max_to_keep=1)

        timer = Timer()
        timer.start('Start training')

        best_acc = 0
        nepoch_noimp = 0
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())

            num_batch_train = len(self.train_idx) // self.batch_size + 1
            for e in range(epochs):
                total_train_loss = 0
                total_train_acc = 0
                c = 0
                train_idx = shuffle(self.train_idx)
                for idx, batch in enumerate(self._next_batch(data=train_idx, num_batch=num_batch_train)):
                    feed_dict = self._make_feed_dict(batch, self.dropout_keep_prob)
                    _, train_loss, predictions, input_y, train_acc = sess.run([self.train_op, self.loss_op,
                                                                               self.predictions, self.y,
                                                                               self.accuracy],
                                                                              feed_dict=feed_dict)
                    total_train_loss += train_loss
                    total_train_acc += train_acc
                    c += 1

                    if idx % 200 == 0:
                        print("Iter {} - Loss: {} - Acc: {}".format(idx, total_train_loss / c, total_train_acc / c))
                print("End epochs {} ".format(e + 1))

                if self.is_early_stopping:
                    num_batch_val = len(self.dev_idx) // self.batch_size + 1
                    total_val_loss = 0
                    total_val_acc = 0
                    c = 0
                    for idx, batch in enumerate(self._next_batch(data=self.dev_idx, num_batch=num_batch_val)):
                        feed_dict = self._make_feed_dict(batch, self.dropout_keep_prob)
                        _, dev_loss, dev_acc = sess.run([self.train_op, self.loss_op, self.accuracy], feed_dict=feed_dict)
                        total_val_loss += dev_loss
                        total_val_acc += dev_acc
                        c += 1
                    avg_loss = total_val_loss / c
                    avg_acc = total_val_acc / c
                    print("Validation Loss: {} - Acc: {}".format(avg_loss, avg_acc))

                    if avg_acc > best_acc:
                        saver.save(sess, MODEL_PATH)
                        print('Saved the model at epoch {}'.format(e + 1))
                        best_acc = avg_acc
                        nepoch_noimp = 0
                    else:
                        nepoch_noimp += 1
                        print("Number of epochs with no improvement: {}".format(nepoch_noimp))
                        if nepoch_noimp >= patience:
                            break
            if not self.is_early_stopping:
                saver.save(sess, MODEL_PATH)
                print("Saved the model")

        timer.stop()

    def _next_batch(self, data, num_batch):
        start = 0
        c = 0

        while c < num_batch:
            index_batch = data[start:start + self.batch_size]

            X_m_batch = []
            X_n_batch = []
            Y_batch = []
            IDX_batch = []

            for i in index_batch:
                num = len(self.data[i]['y'])
                y_data = self.data[i]['y']
                for y in y_data:
                    if y == 1:
                        Y_batch.append([0, 1])
                    else:
                        Y_batch.append([1, 0])
                IDX_batch.extend([i] * num)
                X_n_batch.extend(self.data[i]['x_n'])
                X_m_batch.extend([self.data[i]['x_m']] * num)

            X_m_batch, X_n_batch, Y_batch, IDX_batch = shuffle(X_m_batch, X_n_batch, Y_batch, IDX_batch)

            m_word_ids = self._get_word_ids(X_m_batch)
            n_word_ids = self._get_word_ids(X_n_batch)

            start += self.batch_size
            c +=1
            yield(m_word_ids, n_word_ids, Y_batch, IDX_batch)

    def _make_feed_dict(self, data, dropout_keep_prob):
        return {
            self.m_word_ids: data[0],
            self.n_word_ids: data[1],
            self.y: data[2],
            self.idx: data[3],
            self.dropout_keep_op: dropout_keep_prob
        }

    def transform(self):
        def one_batch(data):
            X_m_batch = []
            X_n_batch = []
            Y_batch = []

            for i in data:
                num = len(self.data[i]['y'])
                y_data = self.data[i]['y']
                for y in y_data:
                    if y == 1:
                        Y_batch.append([0, 1])
                    else:
                        Y_batch.append([1, 0])
                X_n_batch.extend(self.data[i]['x_n'])
                X_m_batch.extend([self.data[i]['x_m']] * num)

            X_m_batch, X_n_batch, Y_batch = shuffle(X_m_batch, X_n_batch, Y_batch)

            m_word_ids = self._get_word_ids(X_m_batch)
            n_word_ids = self._get_word_ids(X_n_batch)

            yield(m_word_ids, n_word_ids, Y_batch)

        def make_feed_dict(data):
            return {
                self.m_word_ids: data[0],
                self.n_word_ids: data[1],
                self.y: data[2],
                self.dropout_keep_op: 1.0
            }
        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, tf.train.latest_checkpoint('development/Pointwise/data/trained_models/pointwise_100'))

            self.train_idx = shuffle(self.train_idx)

            for idx, batch in enumerate(one_batch(self.train_idx)):
                feed_dict = make_feed_dict(batch)
                predict, acc = sess.run([self.predictions, self.accuracy], feed_dict=feed_dict)

                print(acc)

    def _get_word_ids(self, X):
        word_ids = [x[1] for x in X]
        word_ids, _ = pad_sequences(word_ids, pad_tok=0, fix_length=self.max_input_word)
        return word_ids

    def _get_embedding(self, embeddings, ids):
        embed = tf.nn.embedding_lookup(embeddings, ids)
        embed = tf.reshape(embed, [-1, self.max_input_word, self.word_embedding_dim, 1])
        return embed

    def _convolution(self, embedding):
        cnn_outputs = []
        for i, filter_size in enumerate(self.filter_sizes):
            conv = tf.nn.conv2d(
                embedding,
                self.kernels[i][0],
                strides=[1, 1, self.word_embedding_dim, 1],
                padding='SAME',
                name='conv-1'
            )
            h = tf.nn.relu(tf.nn.bias_add(conv, self.kernels[i][1]), name='relu-1')
            cnn_outputs.append(h)
        cnn_reshaped = tf.concat(cnn_outputs, 3)
        return cnn_reshaped

    def _max_pooling(self, conv, input_length):
        pooled = tf.nn.max_pool(
            conv,
            ksize=[1, input_length, 1, 1],
            strides=[1, 1, 1, 1],
            padding='VALID',
            name="pool")
        return pooled