import numpy as np
from scipy.spatial.transform import Rotation

class SE3Control(object):
    """

    """
    def __init__(self, quad_params):
        """
        Parameters:
            quad_params, dict with keys specified in rotorpy/vehicles
        """

        # Quadrotor physical parameters.
        # Inertial parameters
        self.mass            = quad_params['mass'] # kg
        self.Ixx             = quad_params['Ixx']  # kg*m^2
        self.Iyy             = quad_params['Iyy']  # kg*m^2
        self.Izz             = quad_params['Izz']  # kg*m^2
        self.Ixy             = quad_params['Ixy']  # kg*m^2
        self.Ixz             = quad_params['Ixz']  # kg*m^2
        self.Iyz             = quad_params['Iyz']  # kg*m^2

        # Frame parameters
        self.c_Dx            = quad_params['c_Dx']  # drag coeff, N/(m/s)**2
        self.c_Dy            = quad_params['c_Dy']  # drag coeff, N/(m/s)**2
        self.c_Dz            = quad_params['c_Dz']  # drag coeff, N/(m/s)**2

        self.num_rotors      = quad_params['num_rotors']
        self.rotor_pos       = quad_params['rotor_pos']

        # Rotor parameters
        self.rotor_speed_min = quad_params['rotor_speed_min'] # rad/s
        self.rotor_speed_max = quad_params['rotor_speed_max'] # rad/s

        self.k_eta           = quad_params['k_eta']     # thrust coeff, N/(rad/s)**2
        self.k_m             = quad_params['k_m']       # yaw moment coeff, Nm/(rad/s)**2
        self.k_d             = quad_params['k_d']       # rotor drag coeff, N/(m/s)
        self.k_z             = quad_params['k_z']       # induced inflow coeff N/(m/s)
        self.k_flap          = quad_params['k_flap']    # Flapping moment coefficient Nm/(m/s)

        # Motor parameters
        self.tau_m           = quad_params['tau_m']     # motor reponse time, seconds

        # You may define any additional constants you like including control gains.
        self.inertia = np.array([[self.Ixx, self.Ixy, self.Ixz],
                                 [self.Ixy, self.Iyy, self.Iyz],
                                 [self.Ixz, self.Iyz, self.Izz]]) # kg*m^2
        self.g = 9.81 # m/s^2

        # Gains
        self.kp_pos = np.array([6.5,6.5,15])
        self.kd_pos = np.array([4.0, 4.0, 9])
        self.kp_att = 544
        self.kd_att = 46.64

        # Linear map from individual rotor forces to scalar thrust and vector
        # moment applied to the vehicle.
        k = self.k_m/self.k_eta

        # Below is an automated generation of the control allocator matrix. It assumes that all thrust vectors are aligned
        # with the z axis and that the "sign" of each rotor yaw moment alternates starting with positive for r1.
        self.f_to_TM = np.vstack((np.ones((1,self.num_rotors)),np.hstack([np.cross(self.rotor_pos[key],np.array([0,0,1])).reshape(-1,1)[0:2] for key in self.rotor_pos]), np.array([k*(-1)**i for i in range(self.num_rotors)]).reshape(1,-1)))
        self.TM_to_f = np.linalg.inv(self.f_to_TM)

    def update_ref(self, t, flat_output):
        """
        This function receives the current time, and desired flat
        outputs. It returns the reference command inputs.
        Follows https://repository.upenn.edu/edissertations/547/

        Inputs:
            t, present time in seconds
            flat_output, a dict describing the present desired flat outputs with keys
                x,        position, m
                x_dot,    velocity, m/s
                x_ddot,   acceleration, m/s**2  a
                x_dddot,  jerk, m/s**3          a_dot
                x_ddddot, snap, m/s**4          a_ddot
                yaw,      yaw angle, rad
                yaw_dot,  yaw rate, rad/s
                yaw_ddot, yaw acceleration, rad/s**2  #required! not the same if computing command using controller

        Outputs:
            control_input, a dict describing the present computed control inputs with keys
                cmd_motor_speeds, rad/s
                cmd_thrust, N (for debugging and laboratory; not used by simulator)
                cmd_moment, N*m (for debugging; not used by simulator)
                cmd_q, quaternion [i,j,k,w] (for laboratory; not used by simulator)
                cmd_w, angular velocity
                cmd_a, angular acceleration
        """
        cmd_motor_speeds = np.zeros((4,))
        cmd_q = np.zeros((4,))

        def normalize(x):
            """Return normalized vector."""
            return x / np.linalg.norm(x)

        # def vee_map(S):
        #     """Return vector corresponding to given skew symmetric matrix."""
        #     return np.array([-S[1,2], S[0,2], -S[0,1]])

        # Desired force vector.
        t = flat_output['x_ddot']+ np.array([0, 0, self.g])
        b3 = normalize(t)
        F_des = self.mass * (t)# this is vectorized

        # Desired thrust is force projects onto b3 axis.
        # R = Rotation.from_quat(state['q']).as_matrix() #this is where most of the problem is, there is no error in rotation!
        # b3 = R @ np.array([0, 0, 1])
        u1 = np.dot(F_des, b3)

        # Desired orientation to obtain force vector.
        b3_des = normalize(F_des) #b3_des and b3 are the same
        yaw_des = flat_output['yaw']
        c1_des = np.array([np.cos(yaw_des), np.sin(yaw_des), 0])
        b2_des = normalize(np.cross(b3_des, c1_des))
        b1_des = np.cross(b2_des, b3_des)
        R_des = np.stack([b1_des, b2_des, b3_des]).T

        R = R_des# assume we have perfect tracking on rotation
        # Orientation error.
        # S_err = 0.5 * (R_des.T @ R - R.T @ R_des)
        # att_err = vee_map(S_err)

        # Following section follows Mellinger paper to compute reference angular velocity
        dot_u1 = np.dot(b3,flat_output['x_dddot'])
        hw = self.mass/u1*(flat_output['x_dddot']-dot_u1*b3)
        p  = np.dot(-hw, b2_des)
        q  = np.dot(hw, b1_des)
        w_des = np.array([0, 0, flat_output['yaw_dot']])
        r  = np.dot(w_des, b3_des)
        Omega = np.array([p, q, r])

        wwu1b3 = np.cross(Omega, np.cross(Omega, u1*b3))
        ddot_u1 = np.dot(b3, self.mass*flat_output['x_ddddot']) - np.dot(b3, wwu1b3)
        ha = 1.0/u1*(self.mass*flat_output['x_ddddot'] - ddot_u1*b3 - 2*np.cross(Omega,dot_u1*b3) - wwu1b3)
        p_dot = np.dot(-ha, b2_des)
        q_dot = np.dot(ha, b1_des)
        np.cross(Omega, Omega)
        r_dot = flat_output['yaw_ddot'] *np.dot(np.array([0,0,1.0]), b3_des) #uniquely need yaw_ddot
        Alpha = np.array([p_dot, q_dot, r_dot])

        u2 =  self.inertia @ Alpha + np.cross(Omega, self.inertia @ Omega)
        # print(u1,u2)
        TM = np.array([u1, u2[0], u2[1], u2[2]])
        cmd_motor_forces = self.TM_to_f @ TM
        cmd_motor_speeds = cmd_motor_forces / self.k_eta
        cmd_motor_speeds = np.sign(cmd_motor_speeds) * np.sqrt(np.abs(cmd_motor_speeds))

        cmd_q = Rotation.from_matrix(R_des).as_quat()

        control_input = {'cmd_motor_speeds':cmd_motor_speeds,
                        'cmd_thrust':u1,
                        'cmd_moment':u2,
                        'cmd_q':cmd_q,
                        'cmd_w':Omega,
                        'cmd_a':Alpha}
        return control_input

    def update(self, t, state, flat_output):
        """
        This function receives the current time, true state, and desired flat
        outputs. It returns the command inputs.

        Inputs:
            t, present time in seconds
            state, a dict describing the present state with keys
                x, position, m
                v, linear velocity, m/s
                q, quaternion [i,j,k,w]
                w, angular velocity, rad/s
            flat_output, a dict describing the present desired flat outputs with keys
                x,        position, m
                x_dot,    velocity, m/s
                x_ddot,   acceleration, m/s**2
                x_dddot,  jerk, m/s**3
                x_ddddot, snap, m/s**4
                yaw,      yaw angle, rad
                yaw_dot,  yaw rate, rad/s

        Outputs:
            control_input, a dict describing the present computed control inputs with keys
                cmd_motor_speeds, rad/s
                cmd_thrust, N
                cmd_moment, N*m
                cmd_q, quaternion [i,j,k,w]
        """
        cmd_motor_speeds = np.zeros((4,))
        cmd_thrust = 0
        cmd_moment = np.zeros((3,))
        cmd_q = np.zeros((4,))

        def normalize(x):
            """Return normalized vector."""
            return x / np.linalg.norm(x)

        def vee_map(S):
            """Return vector corresponding to given skew symmetric matrix."""
            return np.array([-S[1,2], S[0,2], -S[0,1]])

        # Desired force vector.
        pos_err  = state['x'] - flat_output['x']
        dpos_err = state['v'] - flat_output['x_dot']
        F_des = self.mass * (- self.kp_pos*pos_err
                             - self.kd_pos*dpos_err
                             + flat_output['x_ddot']
                             + np.array([0, 0, self.g]))

        # Desired thrust is force projects onto b3 axis.
        R = Rotation.from_quat(state['q']).as_matrix()
        b3 = R @ np.array([0, 0, 1])
        u1 = np.dot(F_des, b3)

        # Desired orientation to obtain force vector.
        b3_des = normalize(F_des)
        yaw_des = flat_output['yaw']
        c1_des = np.array([np.cos(yaw_des), np.sin(yaw_des), 0])
        b2_des = normalize(np.cross(b3_des, c1_des))
        b1_des = np.cross(b2_des, b3_des)
        R_des = np.stack([b1_des, b2_des, b3_des]).T

        # Orientation error.
        S_err = 0.5 * (R_des.T @ R - R.T @ R_des)
        att_err = vee_map(S_err)

        # Angular velocity error (this is oversimplified).
        w_des = np.array([0, 0, flat_output['yaw_dot']])
        w_err = state['w'] - w_des

        # Angular control; vector units of N*m.
        u2 = self.inertia @ (-self.kp_att*att_err - self.kd_att*w_err)

        # Compute motor speeds. Avoid taking square root of negative numbers.
        TM = np.array([u1, u2[0], u2[1], u2[2]])
        cmd_motor_forces = self.TM_to_f @ TM
        cmd_motor_speeds = cmd_motor_forces / self.k_eta
        cmd_motor_speeds = np.sign(cmd_motor_speeds) * np.sqrt(np.abs(cmd_motor_speeds))

        cmd_thrust = u1
        cmd_moment = u2
        cmd_q = Rotation.from_matrix(R_des).as_quat()

        control_input = {'cmd_motor_speeds':cmd_motor_speeds,
                         'cmd_thrust':cmd_thrust,
                         'cmd_moment':cmd_moment,
                         'cmd_q':cmd_q}
        return control_input


# crie um programa que leia duas notas de um aluno e calcule sua média, mostrando uma mensagem no final, de acordo com a média atingida:
n1 = float(input('Nota 1: '))
n2 = float(input('Nota 2: '))
media = (n1 + n2) / 2
print('Sua nota é', media)
if media < 5:
    print('\033[31mREPROVADO\033[m')
elif media < 6.9:
    print('\033[1;33mRECUPERAÇÃO\033[m')
else:
    print('\033[1;94mAPROVADO\033[m')


# -*- coding: utf-8 -*-
"""
Editor de Spyder

Write a function that recive 2 numers and return the biggest one
Este es un archivo temporal.
"""
"""
Return the max of 2 num
http://www.pythondiario.com/2013/05/ejercicios-en-python-parte-1.html
"""

#list.append(elem)

# 1
def max2(num1,num2):
    if(num1 > num2):
        return num1
    else:
        return num2

# Return the max of 3 num ie 2

def max3(num1, num2, num3):
    if(num1 > num2 and num1 > num3):
        return num1
    elif (num2 > num1 and num2 > num3):
        return num2
    else:
        return num3

# Count the number of elemnts in a list i.e 3
def lenght_List(lista):
    cont = 0
    for i in lista:
        cont+= 1
    return cont

# validate if the char is a vocal or not 4
def vocal(x):
    if x == "a" or x == "e" or x == "i" or x == "o" or x == "u":
        return True
    if x == "A" or x == "E" or x == "I" or x == "O" or x == "U":
        return True
    return False

#  create a function for addition and one for multiplication of all the elements of a LIST 5
def suma(lista):
    suma = 0
    for i in range (0,(len(lista)),1):
        suma = suma + lista[i]

    return suma

def mult(lista):
    multi = 1
    for i in range (0,(len(lista)),1):
        multi = multi * lista[i]

    return multi

# print the invers value that was received 6
def invers(cadena):
    cont = len(cadena)
    cadena2 = ""
    for i in range (cont-1,-1,-1):
        cadena2 += cadena[i]
    return cadena2

# Check if a word is palindrome or no 7
def palindrome(cadena):

    cadena2 = invers(cadena)
    if cadena == cadena2:
        return True
    return False

# Compare 2 list and check if one of the elemnts of 1 list is in the other one 8
def superposicion (lista1, lista2):
    for i in lista1:
        for x in lista2:
            if i == x:
                return True
    return False

'''
Create a function the get an int n and a char as parameters the return
the character repeated 9
'''
def generated_n_char (n, c):
    result = ""
    '''
    Another way to do this will be only writing this
    return n * c
    '''
    for i in range(n):
        result += c
    return result

'''
Definir un histograma procedimiento() que tome una lista
de números enteros e imprima un histograma en la pantalla. 10
'''
def procedimiento(values):
    for i in values:
        print( i*"x")

# Print the first repeat letter inside a word 11
def repeat(word):

    for i in range(len(word)):
        print(i)

        for j in range(i +1, len(word), +1):
            if(word[i] == word[j]):
                return word[i]

    return False
# Part 2

# Print the biggest number on a lis 12
def max_in_list(lista):
    mayor=0
    for i in range (len(lista)):
        if lista[i]>mayor:
            mayor=lista[i]
    return mayor

# Print the biggest string of a list 13
def longer_String (lista):
    mayor = 0
    largo = ""
    for i in lista:
        #print(i)
        if len(i) > mayor:
            mayor = len(i)
            largo = i
    return largo

'''Wirte a function with 2 input, a list of words and the other only a number n. The
function should return the words that have more letters that the secon parameter n 14
'''
def filter_words(lista,n):
    result = []
    for i in lista:
        if len(i)>n:
            result.append(i)

    return result

'''
Ask to the user to input a string a return  how many upper case are in the list 15
'''
def input_list():

   txt = input("Type something to test this out: ")
   print( "Is this what you just said?", txt)
   print(txt[1])
   count = 0
   for i in range(len(txt)):
       if txt[i].isupper():
           count +=1

   return count

#Input a bin number and convert to int 16
def binary_hex(num):

    return int(num,2)

#Input a list of numbers and print the ones that are bigger than 20
def filter_ages(lista):
    lista2 = []
    for i in lista:
        if i>20:
            lista2.append(i)
    return lista2

#Input a list of names and find which ones start with the second parameter
# Print the biggest string of a list 13
def ini_name(lista, n):
    result = []
    for i in range (len(lista)):
        if lista[i][0] == "A":
            result.append(lista[i])
        print(lista[i][0])

    return result


#!/usr/bin/env python

import os
import sys

import setuptools

if sys.argv[-1] == "publish":
    os.system("python setup.py sdist upload")
    sys.exit()

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="pyflowater",
    version="0.5.2",
    packages=["pyflowater"],
    description="Python interface for Flo by Moen API",
    #      long_description=long_description,
    url="https://github.com/rsnodgrass/pyflowater",
    author="Ryan Snodgrass",
    author_email="rsnodgrass@gmail.com",
    license="Apache Software License",
    install_requires=["requests>=2.0", "google-cloud-firestore"],
    keywords=["flo", "home automation", "water monitoring"],
    zip_safe=True,
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: Apache Software License",
        "Operating System :: OS Independent",
    ],
)


#!/usr/bin/python3
"""script that send a POST request"""
import urllib.request as req
import urllib.parse as parse
from sys import argv

def request_param(url, email):
    """sen a POST request"""
    params = {'email': email}
    data = parse.urlencode(params).encode('ascii')
    request = req.Request(url, data)
    with req.urlopen(request) as res:
        print("{}".format(res.read().decode("utf-8")))

if __name__ == '__main__':
    request_param(argv[1], argv[2])


#Secuencia de impares. Cargar por teclado dos números, e imprimir los números impares que se encuentran comprendidos entre ellos, en forma ascendente y descendente.

#Variables
num_1=0
num_2=0

#Proceso

print("A continuación deberá ingresar los 2 números:");
num_1=int(input("Ingrese el primer número: "))
num_2=int(input("Ingrese el segundo número: "))

print("Números impares en forma ascendente:")
for i in range(num_1, num_2 + 1):
    if i % 2 != 0:
        print(i, end=" ")

print("\nNúmeros impares en forma descendente:")
for i in range(num_2, num_1 - 1, -1):
    if i % 2 != 0:
        print(i, end=" ")


import copy
from XmlElement import XmlElement
import os
import re
class XmlTree:
    def __init__ (self, root = XmlElement(), size = 0):
        self.root = copy.deepcopy(root)
        self.size = size

    @staticmethod
    def createFromFile (filePath):
        root = XmlElement()
        del root.children[:]
        size = 0
        exp = "(?<=<)(\/?[a-zA-Z1-9_][a-zA-Z1-9_.:-]+)|(\S+)=[\"']?((?:.(?![\"']?\s+(?:\S+)=|[>\"']))+.)[\"']?|((?<=>)[^<\n]*(?=<|\n))"
        if os.path.isfile(filePath):
            with open(filePath, 'r') as read_file:
                elements = re.findall(exp, read_file.read())
                currentTag = XmlElement()
                #assign each element according to it's type and position
                for i in range(len(elements)):
                    #if the element is a tag
                    if elements[i][0]:
                        #if the tag was a closing tag
                        if elements[i][0][0] == '/':
                            currentTag = currentTag.parent
                        #else then it is a new tag
                        else:
                            #if it is the root tag
                            if size == 0:
                                root.name = elements[i][0]
                                currentTag = root
                                size+=1
                            #if it is not the root tag
                            else:
                                newTag = XmlElement(elements[i][0], currentTag, [], {}, '')
                                currentTag.children.append(newTag)
                                currentTag = newTag
                                size+=1
                    #if the element is an attribute
                    elif elements[i][1]:
                        #if it is the root tag
                        if size == 0:
                            root.attributes.update({elements[i][1] : elements[i][2]})
                            currentTag = root
                        #if it is not the root tag
                        else:
                            currentTag.attributes.update({elements[i][1] : elements[i][2]})
                    elif elements[i][3]:
                        #if it is the root tag
                        if size == 0:
                            root.text = elements[i][3]
                            currentTag = root
                        #if it is not the root tag
                        else:
                            currentTag.text = elements[i][3]

                    else:
                        pass
        return XmlTree(root, size)

    @staticmethod
    def printPreorder(root):
        xmlStr = ''
        if root:
            # First print the data of the root tag
            xmlStr += '<'+root.name
            for attribute in root.attributes:
                xmlStr += ' ' + attribute + '=' + '\"'+root.attributes[attribute]+'\"'
            xmlStr += '>' + ' ' + root.text
            # Then recur on childs
            for child in root.children:
                xmlStr += '\n\t' + XmlTree.printPreorder(child) +'\n'

            xmlStr +='</' + root.name + '>'
        return xmlStr

    def saveToFile(self, filePath = 'saved.xml'):
        with open(filePath, 'w+') as write_file:
            xmlStr = XmlTree.printPreorder(self.root)
            write_file.write(xmlStr)


# -*- coding: utf-8 -*-
"""Loading a pre-trained model from disk.

Learn how to load a pre-trained model from disk. We will classify individual images from the
Animals dataset and then display the classified images to our screen.

Example:
    $ python shallownet_load.py --dataset ../datasets/animals --model shallownet_weights.hdf5

Attributes:
    dataset (str):
        The path to where our input image dataset resides on disk.
    model (str):
        The path to the pre-trained model.
"""
import argparse
import cv2
import numpy as np
from keras.models import load_model
from imutils import paths
from pyimagesearch.preprocessing import ImageToArrayPreprocessor
from pyimagesearch.preprocessing import SimplePreprocessor
from pyimagesearch.datasets import SimpleDatasetLoader

def main():
    """Load pre-trained model from disk
    """
    # construct the argument parse and parse the arguments
    args = argparse.ArgumentParser()
    args.add_argument("-d", "--dataset", required=True, help="path to input dataset")
    args.add_argument("-m", "--model", required=True, help="path to pre-trained model")
    args = vars(args.parse_args())

    # initialize the class labels
    class_labels = ["cat", "dog", "panda"]

    # grab the list of images in the dataset then randomly sample indexes into the image paths list
    print("[INFO] sampling images...")
    image_paths = np.array(list(paths.list_images(args["dataset"])))
    idxs = np.random.randint(0, len(image_paths), size=(10,))
    image_paths = image_paths[idxs]

    # initialize the image preprocessors
    simple_preprocessor = SimplePreprocessor(32, 32)
    image_to_array_preprocessor = ImageToArrayPreprocessor()

    # load the dataset from disk then scale the raw pixel intensities to the range [0, 1]
    dataset_loader = SimpleDatasetLoader(preprocessors=[simple_preprocessor, image_to_array_preprocessor])
    (data, _) = dataset_loader.load(image_paths)
    data = data.astype("float") / 255.0

    # load the pre-trained network
    print("[INFO] loading pre-trained network...")
    model = load_model(args["model"])

    # make predictions on the images
    print("[INFO] predicting...")
    preds = model.predict(data, batch_size=32).argmax(axis=1)
    # loop over the sample images
    for (i, image_path) in enumerate(image_paths):
        # load the example image, draw the prediction, and display it to our screen
        image = cv2.imread(image_path)
        cv2.putText(
            image, "Label: {}".format(class_labels[preds[i]]), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2
        )
        cv2.imshow("Image", image)
        cv2.waitKey(0)

if __name__ == "__main__":
    main()


#PyPoll
# First we'll import the os module
# This will allow us to create file paths across operating systems
import os

#Module for reading CSV file
import csv

#Name the .csv file path
csvpath = os.path.join('election_data.csv')
#output path for text file
output_path = ('election_result.txt')

#Declare variables
total_votes = 0
winner = ''
Winner_total_votes = 0

# Declare dictionary to store total vots per candidates
candidates = {}

#Open the .csv file
with open(csvpath) as csvfile:

    csvreader = csv.reader(csvfile, delimiter=',')
    #print(csvreader)

    # Read the header row first
    csv_header = next(csvreader)

    for row in list(csvreader)[1:]:
        #count the total number of votes
        total_votes += 1

        if (row[2] in candidates):
            candidates[row[2]] += 1
        else:
            candidates[row[2]] = 1

#Print all of the results
print("Election Results")
print("----------------------------------------------------------")
print(f"Total Votes: {total_votes}")
print("----------------------------------------------------------")
for key in list(candidates):
        candidates_percentage = (((candidates[key])*100)/total_votes)
        #print(candidates_percentage)
        print(key + ': ' + ("{:.3f}%".format(candidates_percentage)) + '   (' + str(candidates[key]) +')')
        if Winner_total_votes < candidates[key]:
             Winner_total_votes = candidates[key]
             winner = key
print("----------------------------------------------------------")
print(f"Winner: {winner}")
print("----------------------------------------------------------")

#Output the results into a new text file
with open(output_path, 'w') as textfile:

    textfile.write("Election Results\n")
    textfile.write("------------------------------------------------------\n")
    textfile.write(f"Total Votes: {total_votes}\n")
    textfile.write("------------------------------------------------------\n")
    for key in list(candidates):
        candidates_percentage = (((candidates[key])*100)/total_votes)
        textfile.write(key + ': ' + ("{:.3f}%".format(candidates_percentage)) + '   (' + str(candidates[key]) +')\n')
    textfile.write("------------------------------------------------------\n")
    textfile.write(f"Winner: {winner}\n")
    textfile.write("------------------------------------------------------\n")


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 28 08:19:12 2019

@author: rastf
"""

# -*- coding: utf-8 -*-
"""
Created on Mon Oct 21 07:26:54 2019

@author: rastf
"""

from astropy.io import fits
from astropy.wcs import WCS
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
from matplotlib.colors import LogNorm
from matplotlib.patches import Circle
from astropy.visualization import simple_norm

#finding out what the coordinate system for each image is
wcs24 = WCS(fits.getheader('G54_Spitzer_24.fits'))
wcs70 = WCS(fits.getheader('G54_PACS_70.fits'))
wcs160 = WCS(fits.getheader('G54_PACS_160.fits'))
wcs250 = WCS(fits.getheader('G54_SPIRE_250.fits'))
wcs350 = WCS(fits.getheader('G54_SPIRE_350.fits'))
wcs500 = WCS(fits.getheader('G54_SPIRE_500.fits'))
wcs870 = WCS(fits.getheader('G54.1_FINAL_870.fits'))

#setting up the grid for plotting
f= plt.figure(figsize = (5,5))
gs1 = gridspec.GridSpec(1,1)
gs1.update(wspace=0.025, hspace=0)

#importing the data in
SPITZER24=fits.getdata('G54_Spitzer_24.fits')
PACS70=fits.getdata('G54_PACS_70.fits')
PACS160=fits.getdata('G54_PACS_160.fits')
SPIRE250=fits.getdata('G54_SPIRE_250.fits')
SPIRE350=fits.getdata('G54_SPIRE_350.fits')
SPIRE500=fits.getdata('G54_SPIRE_500.fits')
FINAL870=fits.getdata('G54.1_FINAL_870.fits')

#setting vmin and vmax
vmin=-0.03
vmax=1
cmap='cubehelix'
#defining some lists that will be needed for plotting
ax = []
lat = []
lon = []

# =============================================================================
# SPITZER 24 IMAGE
# =============================================================================
t=0
ax.append(plt.subplot(gs1[t], projection=wcs24)) #creating subplot and assigning correct wcs
img = ax[t].imshow(SPITZER24, cmap=cmap, origin = 'lower', norm=simple_norm(SPITZER24)) #plotting image with LogNorm
ax[t].text(0.5,0.92,'Spitzer MIPS 24$\mu$m', fontsize=13, family='serif', ha = 'left', va = 'center', transform = ax[t].transAxes, color='white') #adding text
c = Circle((292.6167, 18.8683), 0.025, edgecolor='white', facecolor='none',alpha=0.5,transform=ax[t].get_transform('fk5')) #defining circle
p = Circle((292.6292, 18.8667), 0.001, edgecolor='white', facecolor='none',alpha=0.5,transform=ax[t].get_transform('fk5')) #defining circle
ax[t].add_patch(c) #adding the circle
ax[t].add_patch(p)
#c2 = Circle((292.6167, 18.8683), 0.022, edgecolor='white', facecolor='none',alpha=0.5,transform=ax[t].get_transform('fk5')) #defining circle
#ax[t].add_patch(c2) #adding the circle2
lon.append(ax[t].coords[0]) #defining x axis
lat.append(ax[t].coords[1]) #defining y axis
lon[t].set_ticklabel_visible(True) #choosing to show or hide the x axis
lon[t].set_ticks_visible(True)
lat[t].set_ticklabel_visible(True) #choosing to show or hide the y axis
lat[t].set_ticks_visible(True)
ax[t].set_xlim((51,101)) #restricting field of view
ax[t].set_ylim((51,101)) #restricting field of view


import os
import flask
from flask import Flask, render_template, request, redirect
from src.components.inference import  Inference

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import nltk
from string import punctuation
import re
from nltk.corpus import stopwords
from flask import jsonify

nltk.download('stopwords')

set(stopwords.words('english'))

app = Flask(__name__, template_folder='template')

inferencing = Inference()

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

@app.get("/image_classifier")
def image_classifier():
    return render_template("./image_classifier.html")

@app.get("/sentiment_analysis")
def sentiment_analysis():
    return render_template("./sentiment_analysis.html")

@app.route('/image_classifier', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        if 'file' not in request.files:
            return redirect(request.url)
        file = request.files.get('file')
        if not file:
            return
        img_bytes = file.read()
        class_id, class_name = inferencing.predict_class(image=img_bytes)
        return jsonify(class_name=class_name)
    return render_template('./home.html')

@app.route('/sentiment_analysis', methods=['POST'])
def my_sentiment_analysis_post():
    stop_words = stopwords.words('english')

    text1 = request.form['text1'].lower()

    text_final = ''.join(c for c in text1 if not c.isdigit())

    processed_doc1 = ' '.join([word for word in text_final.split() if word not in stop_words])

    sa = SentimentIntensityAnalyzer()
    dd = sa.polarity_scores(text=processed_doc1)
    compound = round((1 + dd['compound'])/2, 2)

    return render_template('sentiment_analysis.html', final=compound, text1=text_final, text2=dd['pos'], text3=dd['neu'], text4=dd['neg'])

if __name__ == '__main__':
    app.run(debug=True, port=int(os.environ.get('PORT', 5000)))


from django.contrib import admin

# Register your models here.
from .models import Exam, AttemptExam
# from .models import ExamQuestions

class ExamAdminConfig(admin.ModelAdmin):
    list_display = ('name','number_of_questions','duration','course','teacher')
    list_filter = ('name','duration','teacher')
    search_fields = ('name','teacher','course','duration')
    # ordering = ('-start_date',)

    fieldsets = (
        (None, {
            'fields':('name','description','number_of_questions','duration')
            }),
        ('Details', {
            'fields':('course','teacher','student')
            }),
    )

    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields':('full_name','email','department','qualification','password1','password2','profile_img') # for now we cannot add a username to a student while adding a student. add the username when editing an existing user.
            }),
        ('Permissions', {
            'classes': ('wide',),
            'fields':('is_staff','is_superuser','is_active')
            }),
        ('Groups', {
            'classes': ('wide',),
            'fields':('groups','user_permissions')
            })
    )

admin.site.register(Exam, ExamAdminConfig) # , ExamAdminConfig
# admin.site.register(ExamQuestions)
# admin.site.register(AttemptExam)


#!/usr/bin/env python3

#-- Imprimir los numeros de fibonacci hasta el número n
def fibo(n: int) -> list:
    """Lista con los numeros de fibonacci hasta el numero n"""
    a,b = 0,1

    #- Lista donde almacenar la lista de fibonacci
    result = []

    while b < n:
        #-- Añadir elemento a la lista
        result.append(b)

        #-- Actualizar termino de fibonacci
        a,b = b, a+b

    #-- Devolver la lista
    return result

#-- Llamar a la funcion
l=fibo(100)
print("Lista de fibonacci: ", l)


import logging
import queue
import re

from .mdlist import MangaDexList
from .errors import HTTPException, InvalidURL, MangaDexException, NotLoggedIn
from .network import Net, base_url
from .manga import ContentRating, Manga
from .fetcher import get_list
from .user import User
from .language import get_language
from .config import ConfigTypeError, _validate_bool as validate_boolean
from .utils import validate_url, get_local_attr

log = logging.getLogger(__name__)

class BaseIterator:
    def __init__(self):
        self.queue = queue.Queue()
        self.offset = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.queue.empty():
            # Maximum number of results from MangaDex API
            if self.offset >= 10000:
                raise StopIteration()
            else:
                self.fill_data()

        try:
            return self.next()
        except queue.Empty:
            raise StopIteration()

    def fill_data(self):
        raise NotImplementedError

    def next(self):
        return self.queue.get_nowait()

class SearchFilterError(MangaDexException):
    def __init__(self, key, msg):
        text = f"Search filter error '{key}' = {msg}"

        super().__init__(text)

class IteratorManga(BaseIterator):
    def __init__(
        self,
        title,
        authors=None,
        artists=None,
        year=None,
        included_tags=None,
        included_tags_mode=None,
        excluded_tags=None,
        excluded_tags_mode=None,
        status=None,
        original_language=None,
        excluded_original_language=None,
        available_translated_language=None,
        publication_demographic=None,
        content_rating=None,
        created_at_since=None,
        updated_at_since=None,
        has_available_chapters=None,
        group=None,
        order=None
    ):
        super().__init__()

        _default_content_ratings = [
            'safe',
            'suggestive',
            'erotica',
            'pornographic'
        ]

        self.limit = 100
        self.title = title

        # Validation
        value_and_or = ['AND', 'OR']

        if year:
            m = re.match(r'[0-9]{4}')
            if not m:
                raise SearchFilterError(
                    "year",
                    f"value must be integer and length must be 4"
                )

        _locals = locals()
        def validate_tags_mode(key):
            value = _locals[key]
            if value and value.upper() not in value_and_or:
                raise SearchFilterError(
                    key,
                    f"value must be 'OR' or 'AND', not '{value}'"
                )

        validate_tags_mode("included_tags_mode")
        validate_tags_mode("excluded_tags_mode")

        def validate_uuid(key):
            new_values = []
            values = _locals[key]
            if values is None:
                return

            if isinstance(values, str):
                values = [values]

            for value in values:
                # Get the id
                try:
                    _id = validate_url(value)
                except InvalidURL:
                    raise SearchFilterError(
                        key,
                        f"'{value}' is not valid UUID"
                    )
                else:
                    new_values.append(_id)

            return new_values

        group = validate_uuid("group")

        tags = self._get_tags()
        def validate_tags(key):
            new_values = []
            values = _locals[key]
            if values is None:
                return

            if isinstance(values, str):
                values = [values]

            # Lowercase to prevent error
            values = [i.lower() for i in values]

            for value in values:
                # Try to match the keyword tags
                try:
                    _id = tags[value]
                except KeyError:
                    pass
                else:
                    new_values.append(_id)
                    continue

                # Try to get uuid
                try:
                    _id = validate_url(value)
                except InvalidURL:
                    raise SearchFilterError(
                        key,
                        f"'{value}' is not valid keyword or uuid tag"
                    )

                new_values.append(_id)

            return new_values

        included_tags = validate_tags("included_tags")
        excluded_tags = validate_tags("excluded_tags")

        def validate_values_from_list(key, array):
            values = _locals[key]
            if values is None:
                return

            if isinstance(values, str):
                values = [values]

            for value in values:
                if value.lower() not in array:
                    raise SearchFilterError(
                        key,
                        f"Value must be one of {array}, not {value}"
                    )

        _status_values = [
            'ongoing',
            'completed',
            'hiatus',
            'cancelled'
        ]
        validate_values_from_list("status", _status_values)

        def validate_language(key):
            new_values = []
            values = _locals[key]
            if values is None:
                return

            if isinstance(values, str):
                values = [values]

            for value in values:
                try:
                    lang = get_language(value)
                except ValueError as e:
                    raise SearchFilterError(key, e)
                else:
                    new_values.append(lang.value)

            return new_values

        original_language = validate_language("original_language")
        excluded_original_language = validate_language("excluded_original_language")
        available_translated_language = validate_language("available_translated_language")

        _pub_demo_values = [
            'shounen',
            'shoujo',
            'josei',
            'seinen',
            'none'
        ]
        validate_values_from_list("publication_demographic", _pub_demo_values)

        _content_rating_values = [a.value for a in ContentRating]
        validate_values_from_list("content_rating", _content_rating_values)

        if has_available_chapters:
            try:
                validate_boolean(has_available_chapters)
            except ConfigTypeError as e:
                raise SearchFilterError("has_available_chapters", e)

        # Validate orders
        def validate_order(order):
            new_order = {}
            ascending = ['asc', 'ascending']
            descending = ['desc', 'descending']
            for key, value in order.items():
                # Validate order keys
                re_order_key = r'order\[(' \
                               r'title|' \
                               r'year|' \
                               r'createdAt|' \
                               r'updatedAt|' \
                               r'latestUploadedChapter|' \
                               r'followedCount|' \
                               r'relevance|' \
                               r'rating|' \
                               r')\]'
                match = re.match(re_order_key, key)
                if match is None:
                    raise SearchFilterError(
                        key,
                        "Invalid order key"
                    )

                if value in ascending:
                    new_order[key] = ascending[0]
                elif value in descending:
                    new_order[key] = descending[0]
                else:
                    raise SearchFilterError(
                        key,
                        f"invalid value must be one of {ascending} or {descending}"
                    )

            return new_order

        order = validate_order(order)

        self._param_init = {
            "authors[]": authors,
            "artists[]": artists,
            "year": year,
            "includedTags[]": included_tags,
            "includedTagsMode": included_tags_mode,
            "excludedTags[]": excluded_tags,
            "excludedTagsMode": excluded_tags_mode,
            "status[]": status,
            "originalLanguage[]": original_language,
            "excludedOriginalLanguage[]": excluded_original_language,
            "availableTranslatedLanguage[]": available_translated_language,
            "publicationDemographic[]": publication_demographic,
            "contentRating[]": content_rating or _default_content_ratings,
            "createdAtSince": created_at_since,
            "updatedAtSince": updated_at_since,
            "hasAvailableChapters": has_available_chapters,
            "group": group,
        }

        self._param_init.update(**order)

    def _get_tags(self):
        tags = {}
        r = Net.mangadex.get(f'{base_url}/manga/tag')
        data = r.json()

        for item in data['data']:
            _id = item['id']
            attr = item['attributes']
            name = get_local_attr(attr['name']).lower()
            tags[name] = _id

        return tags

    def _get_params(self):
        includes = ['author', 'artist', 'cover_art']

        params = {
            'includes[]': includes,
            'title': self.title,
            'limit': self.limit,
            'offset': self.offset,
        }
        params.update(self._param_init.copy())

        return params

    def fill_data(self):
        params = self._get_params()
        url = f'{base_url}/manga'
        r = Net.mangadex.get(url, params=params)
        data = r.json()

        if r.status_code >= 400:
            err = data['errors'][0]['detail']
            raise MangaDexException(err)

        items = data['data']

        for item in items:
            self.queue.put(Manga(data=item))

        self.offset += len(items)

class IteratorUserLibraryManga(BaseIterator):
    statuses = [
        'reading',
        'on_hold',
        'plan_to_read',
        'dropped',
        're_reading',
        'completed'
    ]

    def __init__(self, status=None):
        super().__init__()

        self.limit = 100
        self.offset = 0

        if status is not None and status not in self.statuses:
            raise MangaDexException(f"{status} are not valid status, choices are {set(self.statuses)}")

        self.status = status

        lib = {}
        for stat in self.statuses:
            lib[stat] = []
        self.library = lib

        logged_in = Net.mangadex.check_login()
        if not logged_in:
            raise NotLoggedIn("Retrieving user library require login")

        self._parse_reading_status()

    def _parse_reading_status(self):
        r = Net.mangadex.get(f'{base_url}/manga/status')
        data = r.json()

        for manga_id, status in data['statuses'].items():
            self.library[status].append(manga_id)

    def _check_status(self, manga):
        if self.status is None:
            return True

        manga_ids = self.library[self.status]
        return manga.id in manga_ids

    def next(self) -> Manga:
        while True:
            manga = super().next()

            if not self._check_status(manga):
                # Filter is used
                continue

            return manga

    def fill_data(self):
        includes = [
            'artist', 'author', 'cover_art'
        ]
        params = {
            'includes[]': includes,
            'limit': self.limit,
            'offset': self.offset,
        }
        url = f'{base_url}/user/follows/manga'
        r = Net.mangadex.get(url, params=params)
        data = r.json()

        items = data['data']

        for item in items:
            self.queue.put(Manga(data=item))

        self.offset += len(items)

class IteratorMangaFromList(BaseIterator):
    def __init__(self, _id=None, data=None):
        if _id is None and data is None:
            raise ValueError("atleast provide _id or data")
        elif _id and data:
            raise ValueError("_id and data cannot be together")

        super().__init__()

        self.id = _id
        self.data = data
        self.limit = 100
        self.name = None # type: str
        self.user = None # type: User

        self.manga_ids = []

        self._parse_list()

    def _parse_list(self):
        if self.id:
            data = get_list(self.id)['data']
        else:
            data = self.data

        self.name = data['attributes']['name']

        for rel in data['relationships']:
            _type = rel['type']
            _id = rel['id']
            if _type == 'manga':
                self.manga_ids.append(_id)
            elif _type == 'user':
                self.user = User(_id)

    def fill_data(self):
        ids = self.manga_ids
        includes = ['author', 'artist', 'cover_art']
        content_ratings = [
            'safe',
            'suggestive',
            'erotica',
            'pornographic' # Filter porn content will be done in next()
        ]

        limit = self.limit
        if ids:
            param_ids = ids[:limit]
            del ids[:len(param_ids)]
            params = {
                'includes[]': includes,
                'limit': limit,
                'contentRating[]': content_ratings,
                'ids[]': param_ids
            }
            url = f'{base_url}/manga'
            r = Net.mangadex.get(url, params=params)
            data = r.json()

            notexist_ids = param_ids.copy()
            copy_data = data.copy()
            for manga_data in copy_data['data']:
                manga = Manga(data=manga_data)
                if manga.id in notexist_ids:
                    notexist_ids.remove(manga.id)

            if notexist_ids:
                for manga_id in notexist_ids:
                    log.warning(f'There is ghost (not exist) manga = {manga_id} in list {self.name}')

            for manga_data in data['data']:
                self.queue.put(Manga(data=manga_data))

class IteratorUserLibraryList(BaseIterator):
    def __init__(self):
        super().__init__()

        self.limit = 100
        self.offset = 0

        logged_in = Net.mangadex.check_login()
        if not logged_in:
            raise NotLoggedIn("Retrieving user library require login")

    def fill_data(self):
        params = {
            'limit': self.limit,
            'offset': self.offset,
        }
        url = f'{base_url}/user/list'
        r = Net.mangadex.get(url, params=params)
        data = r.json()

        items = data['data']

        for item in items:
            self.queue.put(MangaDexList(data=item))

        self.offset += len(items)

class IteratorUserList(BaseIterator):
    def __init__(self, _id=None):
        super().__init__()

        self.limit = 100
        self.user = User(_id)

    def fill_data(self):
        params = {
            'limit': self.limit,
            'offset': self.offset,

        }
        url = f'{base_url}/user/{self.user.id}/list'
        try:
            r = Net.mangadex.get(url, params=params)
        except HTTPException:
            # Some users are throwing server error (Bad gateway)
            # MD devs said it was cache and headers issues
            # Reference: https://api.mangadex.org/user/10dbf775-1935-4f89-87a5-a1f4e64d9d94/list
            # For now the app will throw error and tell the user cannot be fetched until it's get fixed

            # HTTPException from session only giving "server throwing ... code" message
            raise HTTPException(
                f"An error occured when getting mdlists from user \"{self.user.id}\". " \
                f"The app cannot fetch all MangaDex lists from user \"{self.user.id}\" " \
                "because of server error. The only solution is to wait until this get fixed " \
                "from MangaDex itself."
            ) from None

        data = r.json()

        items = data['data']

        for item in items:
            self.queue.put(MangaDexList(data=item))

        self.offset += len(items)

class IteratorUserLibraryFollowsList(BaseIterator):
    def __init__(self):
        super().__init__()

        self.limit = 100

        logged_in = Net.mangadex.check_login()
        if not logged_in:
            raise NotLoggedIn("Retrieving user library require login")

    def fill_data(self):
        params = {
            'limit': self.limit,
            'offset': self.offset,
        }
        url = f'{base_url}/user/follows/list'
        r = Net.mangadex.get(url, params=params)
        data = r.json()

        items = data['data']

        for item in items:
            self.queue.put(MangaDexList(data=item))

        self.offset += len(items)

class IteratorSeasonalManga(IteratorMangaFromList):
    owner_list = 'd2ae45e0-b5e2-4e7f-a688-17925c2d7d6b'

    def __init__(self, season):
        seasons = self._get_seasons()

        try:
            mdlist = seasons[season]
        except KeyError:
            raise MangaDexException(f"invalid season, available choices are {list(seasons.keys())}")

        super().__init__(mdlist.id)

    @classmethod
    def _get_seasons(self):
        seasons = {}
        for mdlist in IteratorUserList(self.owner_list):
            name = mdlist.name.lower().replace('seasonal: ', '')
            seasons[name] = mdlist

        return seasons

def iter_random_manga(content_ratings):
    ids = []
    while True:
        params = {
            'includes[]': ['author', 'artist', 'cover_art'],
            "contentRating[]": content_ratings
        }
        r = Net.mangadex.get(f'{base_url}/manga/random', params=params)
        data = r.json()['data']
        manga = Manga(data=data)

        if manga.id not in ids:
            # Make sure it's not duplicated manga
            ids.append(manga.id)
            yield manga

        continue


import json
import argparse

parser = argparse.ArgumentParser(description='main', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--dataset', default='MAGCS')
parser.add_argument('--model', default='ensemble')
args = parser.parse_args()
dataset = args.dataset
model_name = args.model

label2id = {}
id2label = {}
with open(f'./Sandbox/Data/{dataset}/label2id.txt') as fin:
	for line in fin:
		data = line.strip().split()
		label = data[0]
		idx = data[1]
		label2id[label] = idx
		id2label[idx] = label

papers = []
preds_prev = []
with open(f'../{dataset}/{dataset}_predictions_{model_name}.json') as fin:
	for line in fin:
		data = json.loads(line)
		papers.append(data['paper'])
		pred_prev = [label2id[x[0]] for x in data['predictions']]
		preds_prev.append(pred_prev)

preds = []
with open(f'./Sandbox/Results/{dataset}/score_mat.txt') as fin, \
	 open(f'../{dataset}/{dataset}_predictions_futex.json', 'w') as fout:
	for idx, line in enumerate(fin):
		if idx == 0:
			continue
		data = line.strip().split()
		scores = {}
		for y in data:
			y_tup = y.split(':')
			scores[y_tup[0]] = float(y_tup[1])
		scores_sorted = sorted(scores.items(), key=lambda x:x[1], reverse=True)

		pred_prev = preds_prev[idx-1]
		pred = pred_prev + [y[0] for y in scores_sorted if y[0] not in pred_prev]

		out = {}
		out['paper'] = papers[idx-1]
		out['predictions'] = [[id2label[x], 1] for x in pred if x in id2label]
		fout.write(json.dumps(out)+'\n')


import gym
import numpy as np
import itertools
from urdfenvs.robots.generic_urdf import GenericUrdfReacher
from MotionPlanningEnv.dynamicSphereObstacle import DynamicSphereObstacle
'''
class steering():
    def right(v,r):

    def left(v,r):

    def straight(v, dist):
'''

def initEnv(goal=False, obstacles=False, maps=0,):
    if maps == 1:
        result = []
        robots = [
            GenericUrdfReacher(urdf="pointRobot.urdf", mode="vel"),
            GenericUrdfReacher(urdf="pointRobot.urdf", mode="vel"),
            GenericUrdfReacher(urdf="pointRobot.urdf", mode="vel"),
            GenericUrdfReacher(urdf="pointRobot.urdf", mode="vel")
        ]

        m = len(robots)
        env = gym.make(
            "urdf-env-v0",
            dt=0.01, robots=robots, render=True
        )

        n = env.n()
        pos0 = np.zeros(n)
        pos0[1] = -0.0
        ns_per_robot = env.ns_per_robot()

        initialPositions = np.array(
            [
                (0,0,0), (0,0,1), (0,0,0), (0,0,np.deg2rad(90))
            ]
        )

        mountPositions = np.array(
            [
                #(0,0,0), (-8,6,0), (8,-6,0), (-8,-8.75,0)
                (0,0,0), (-2,0,0), (-3,0,0), (-4,0,0)
            ]
        )
        env.reset(pos=initialPositions,mount_positions=mountPositions)

        #from env.gym_envs_urdf.steering import steeringInput

        from env.gym_envs_urdf.scene_objects.obstacles import (
            walls1,
            #boxes1,
            #obstacles1

        )

        for wall in walls1:
            env.add_shapes(shape_type=wall[0], dim=wall[1], poses_2d=wall[2])

        for i in range(len(walls1)):
            dimensions = walls1[i][1][0:2]
            coordinates = [coord[:2] for coord in walls1[i][2]]

            result.append([[coordinates[0], coordinates[1], dimensions[0], dimensions[1]] for coordinates in coordinates])

        obstacles = list(itertools.chain(*result))

        '''
        for box in boxes1:
            env.add_shapes(shape_type=box[0], dim=box[1],mass =box[2], poses_2d=box[3] , place_height =box[4])

        for obstacle in obstacles1:
            env.add_obstacle(obstacle)

        '''
    else:
        pass

    # Set goal to follow
    if goal:
        from env.gym_envs_urdf.scene_objects.goal import dynamicGoal

        env.add_goal(dynamicGoal)

    if obstacles:
        from env.gym_envs_urdf.scene_objects.obstacles import (
            sphereObst1,
            sphereObst2,
            urdfObst1,
            dynamicSphereObst3,
        )

        env.add_obstacle(sphereObst1)
        env.add_obstacle(sphereObst2)
        env.add_obstacle(urdfObst1)
        env.add_obstacle(dynamicSphereObst3)

    return env , m, mountPositions[:,:2], obstacles, initialPositions[:,2]+np.deg2rad(90)#, #, steeringInput

def robotMain(pos, vel, current_orientations, omega, otherRobots, env, render=False, dt=0.01):
    # Extract position and orientation from the inputs
    x = pos[0][0]
    y = pos[0][1]
    theta = current_orientations[0]
    # Calculate the velocity vector in the original orientation
    v_vector = np.array([vel, 0])

    # Create a rotation matrix to rotate the velocity vector
    rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],
                                [np.sin(theta), np.cos(theta)]])
    # Rotate the velocity vector
    v_rotated = rotation_matrix @ v_vector

    # Calculate the new position and orientation
    x_new = x + v_rotated[0] * dt
    y_new = y + v_rotated[1] * dt
    theta_new = theta + omega * dt

    # Create the new position and orientation arrays
    pos_new = np.array([[x_new, y_new]])
    orientation_new = np.array([theta_new])

    # Create the action array for the current robot
    action_new = np.append(v_rotated, omega)

    # Create an action array for the other robots
    action_other_bots = np.zeros(9)

    # Concatenate the action arrays for all robots
    action_new = np.concatenate((action_new, action_other_bots))
    vel_new = np.array([vel])

    # Step the environment and return the new position, velocity, and orientation
    ob, _, _, _ = env.step(action_new)
    return pos_new, vel_new, orientation_new


import os
from collections import defaultdict

from django.apps import apps
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.db import models

class Command(BaseCommand):
    help = "Prints a list of all files in MEDIA_ROOT that are not referenced in the database."

    def handle(self, *args, **options):
        if not getattr(settings, 'MEDIA_ROOT'):
            raise CommandError("MEDIA_ROOT is not set, nothing to do")

        # Get a list of all files under MEDIA_ROOT
        media = set()
        for root, dirs, files in os.walk(settings.MEDIA_ROOT):
            for f in files:
                media.add(os.path.abspath(os.path.join(root, f)))

        # Get list of all fields (value) for each model (key)
        # that is a FileField or subclass of a FileField
        model_dict = defaultdict(list)
        for model in apps.get_models():
            for field in model._meta.fields:
                if issubclass(field.__class__, models.FileField):
                    model_dict[model].append(field)

        # Get a list of all files referenced in the database
        referenced = set()
        for model in model_dict:
            for db_object in model.objects.all().iterator():
                for field in model_dict[model]:
                    target_file = getattr(db_object, field.name)
                    if target_file:
                        referenced.add(os.path.abspath(target_file.path))

        # Delete and print each file in MEDIA_ROOT that is not referenced in the database
        not_referenced = media - referenced
        print(media)
        print(referenced)
        for filename in not_referenced:
            try:
                os.remove(filename)
                print(f"Removed {filename}")
            except OSError as e:
                print(f"Can not remove {filename}! {e}")


# import python modules
import os

from docxtpl import DocxTemplate, InlineImage

# create a document object
# Документ -шаблон
doc = DocxTemplate("shablon_mnz.docx")

DATE = "25.01.2023"

###################################### ОБРАБОТКА катлога с файлами ############

##УКАзываем путь к директории с файлами
path = "c:/PYTHON/diplom_Viligdanov T/Files"

# Получаем список всех файлов в каталоге
fun = lambda x: os.path.isfile(os.path.join(path, x))

files_list = filter(fun, os.listdir(path))

# Создайте список файлов в каталоге вместе с указанием размера
size_of_file = [
    (f, os.stat(os.path.join(path, f)).st_size)
    for f in files_list
]

# Iterate over list of files along with size
# and print them one by one.
# Выполнить итерацию по списку файлов с указанием размера
# и распечатайте их один за другим

# count = int(0)
# Функция для обработки списка

# format(GH, df, inDx1, f[0:], NumFile, inDx2, s)

# print("df=", GH + df)
# print("inDx1=", inDx1)
# print("inDx2=", inDx2)
# print("f=", f)
# print(type(f))
# print("size_file=", size)
#
# print("NumFile=", NumFile)
# print(size_of_file)
#
# print(len(size_of_file))

############################---Заполнение МНЗ---##########################

# create data for reports
salesTblRows = []
# for k in range(len(size_of_file)):

for f, size in size_of_file:
    k = 0
    g = str(f)
    start = g.find('_')
    end = g.find('.d')

    asg = g.find('сб')  # если нашли буквы вп, то к номеру файла прибавляем ВП
    asg1 = g.find('СБ')

    asg2 = g.find('вп')  # если нашли буквы вп, то к номеру файла прибавляем ВП
    asg22 = g.find('ВП')

    asg3 = g.find('тэ4')  # если нашли буквы вп, то к номеру файла прибавляем ВП
    asg33 = g.find('ТЭ4')

    asg4 = g.find('мэ')  # если нашли буквы мэ, то к номеру файла прибавляем МЭ
    asg44 = g.find('МЭ')

    if asg > 0 or asg1 > 0:
        inDx1 = 'Сборочный чертеж'
        inDx2 = 'СБ'
    elif asg2 > 0 or asg22 > 0:
        inDx1 = 'Ведомость покупных изделий'
        inDx2 = 'ВП'
    elif asg3 > 0 or asg33 > 0:
        inDx1 = 'Таблица соединений'
        inDx2 = 'ТЭ4'
    elif asg4 > 0 or asg44 > 0:
        inDx1 = 'Электромонтажный чертеж'
        inDx2 = 'МЭ'

    else:
        inDx1 = ''
        inDx2 = ''

    # print(end)
    df = g[start + 2:end]
    # print(df)
    GH = g[start + 1].upper()
    NameFile = f[0:15]  # обозначение конструкторского документа состоит из первых 15 символов

    # разделяем размер файла пробелами
    num = int(size)
    num = '{0:,}'.format(num).replace(',', ' ')

    # проверка на тип файла документа (dwg или xls)
    data = str(size_of_file[k])
    flag = data.find("dwg") != -1

    if (flag == True):
        name_program = "AutoCad"
    else:
        name_program = "Exel"

    salesTblRows.append(
        {"sNo": k + 1, "name_chertega": GH + df + "\n" + inDx1, "name_file": f, "program": name_program,
         "oboznach_file": NameFile + " " + inDx2, "size": num})
    k += 1

# create context to pass data to template
context = {

    "salesTblRows": salesTblRows,
    "topItemsRows": " ",
    "date": DATE,
    "kolich_file": len(size_of_file)
}

# render context into the document object
doc.render(context)

# save the document object as a word file
reportWordPath = 'ITOG-MNZ-FILE.docx'
doc.save(reportWordPath)

print("Формирование документа завершено.")


import os
from unittest import mock

from absl import flags
from absl.testing import absltest, parameterized

try:
  import tensorflow_model_analysis as tfma
  from ml_metadata.proto import metadata_store_pb2
except ImportError:
  pass

from model_card_toolkit import core, dependencies, model_card

try:
  from model_card_toolkit.utils import tf_graphics, tf_sources
  from model_card_toolkit.utils.testdata import tf_testdata_utils
  from model_card_toolkit.utils.testdata.tfxtest import TfxTest
  from model_card_toolkit.utils.tf_utils import (
      _TFX_METRICS_TYPE, _TFX_STATS_TYPE
  )
except ImportError:
  tf_graphics = None
  TfxTest = absltest.TestCase

_MOCK_TENSORFLOW_EXTRA_MISSING_DEP = {
    dependencies._TENSORFLOW_EXTRA_DEPS[0]: None,
}

_IS_MISSING_OPTIONAL_DEPS = not dependencies.has_tensorflow_extra_deps()

class TfCoreTest(parameterized.TestCase, TfxTest):
  def setUp(self):
    super().setUp()
    if _IS_MISSING_OPTIONAL_DEPS:
      self.skipTest('Missing optional dependencies.')
    test_dir = self.create_tempdir()
    self.tmp_db_path = os.path.join(test_dir, 'test_mlmd.db')
    self.mct_dir = test_dir.mkdir(
        os.path.join(test_dir, 'model_card')
    ).full_path

  @mock.patch.dict('sys.modules', _MOCK_TENSORFLOW_EXTRA_MISSING_DEP)
  def test_init_with_store_and_missing_tensorflow_extra_deps(self):
    store = tf_testdata_utils.get_tfx_pipeline_metadata_store(self.tmp_db_path)
    with self.assertRaises(ImportError):
      core.ModelCardToolkit(
          output_dir=self.mct_dir, mlmd_source=tf_sources.MlmdSource(
              store=store, model_uri=tf_testdata_utils.TFX_0_21_MODEL_URI
          )
      )

  @mock.patch.dict('sys.modules', _MOCK_TENSORFLOW_EXTRA_MISSING_DEP)
  def test_init_with_source_and_missing_tensorflow_extra_deps(self):
    with self.assertRaises(ImportError):
      core.ModelCardToolkit(source=tf_sources.Source())

  def test_init_with_store_model_uri_not_found(self):
    store = tf_testdata_utils.get_tfx_pipeline_metadata_store(self.tmp_db_path)
    unknown_model = 'unknown_model'
    with self.assertRaisesRegex(
        ValueError, f'"{unknown_model}" cannot be found in the `store`'
    ):
      core.ModelCardToolkit(
          mlmd_source=tf_sources.MlmdSource(
              store=store, model_uri=unknown_model
          )
      )  # yapf: disable

  @mock.patch.object(
      tf_graphics, 'annotate_dataset_feature_statistics_plots', autospec=True
  )
  @mock.patch.object(tf_graphics, 'annotate_eval_result_plots', autospec=True)
  def test_scaffold_assets_with_store(
      self, mock_annotate_data_stats, mock_annotate_eval_results
  ):
    num_stat_artifacts = 2
    num_eval_artifacts = 1
    output_dir = self.mct_dir
    store = tf_testdata_utils.get_tfx_pipeline_metadata_store(self.tmp_db_path)
    toolkit = core.ModelCardToolkit(
        output_dir=output_dir, mlmd_source=tf_sources.MlmdSource(
            store=store, model_uri=tf_testdata_utils.TFX_0_21_MODEL_URI
        )
    )
    mc = toolkit.scaffold_assets()
    self.assertIsNotNone(mc.model_details.name)
    self.assertIsNotNone(mc.model_details.version.name)
    self.assertIn(
        'default_template.html.jinja',
        os.listdir(os.path.join(output_dir, 'template/html'))
    )
    self.assertIn(
        'default_template.md.jinja',
        os.listdir(os.path.join(output_dir, 'template/md'))
    )
    self.assertEqual(mock_annotate_data_stats.call_count, num_stat_artifacts)
    self.assertEqual(mock_annotate_eval_results.call_count, num_eval_artifacts)

  @parameterized.parameters(
      ('', True), ('', False), ('tfrecord', True), ('tfrecord', False)
  )
  def test_scaffold_assets_with_source(
      self, output_file_format: str, artifacts: bool
  ):

    train_dataset_name = 'Dataset-Split-train'
    train_features = ['feature_name1']
    eval_dataset_name = 'Dataset-Split-eval'
    eval_features = ['feature_name2', 'feature_name3']

    test_dir = self.create_tempdir()
    tfma_path = os.path.join(test_dir, 'tfma')
    tfdv_path = os.path.join(test_dir, 'tfdv')
    pushed_model_path = os.path.join(test_dir, 'pushed_model')

    add_metrics_callbacks = [
        tfma.post_export_metrics.example_count(),
        tfma.post_export_metrics.calibration_plot_and_prediction_histogram(
            num_buckets=2
        ),
    ]

    if artifacts:
      mlmd_store = self._set_up_mlmd()
      self._write_tfma(
          tfma_path, output_file_format, add_metrics_callbacks, mlmd_store
      )
      self._write_tfdv(
          tfdv_path, train_dataset_name, train_features, eval_dataset_name,
          eval_features, mlmd_store
      )
      model_evaluation_artifacts = mlmd_store.get_artifacts_by_type(
          _TFX_METRICS_TYPE
      )
      example_statistics_artifacts = mlmd_store.get_artifacts_by_type(
          _TFX_STATS_TYPE
      )
      # Use placeholder artifact to avoid introducing tfx as a dependency
      pushed_model_artifact = metadata_store_pb2.Artifact(
          uri=pushed_model_path
      )
      tfma_src = tf_sources.TfmaSource(
          model_evaluation_artifacts=model_evaluation_artifacts,
          metrics_exclude=['average_loss']
      )
      tfdv_src = tf_sources.TfdvSource(
          example_statistics_artifacts=example_statistics_artifacts,
          features_include=['feature_name1', 'feature_name3']
      )
      model_src = tf_sources.ModelSource(
          pushed_model_artifact=pushed_model_artifact
      )
    else:
      self._write_tfma(tfma_path, output_file_format, add_metrics_callbacks)
      self._write_tfdv(
          tfdv_path, train_dataset_name, train_features, eval_dataset_name,
          eval_features
      )
      tfma_src = tf_sources.TfmaSource(
          eval_result_paths=[tfma_path], metrics_exclude=['average_loss']
      )
      tfdv_src = tf_sources.TfdvSource(
          dataset_statistics_paths=[tfdv_path],
          features_include=['feature_name1', 'feature_name3']
      )
      model_src = tf_sources.ModelSource(pushed_model_path=pushed_model_path)

    mc = core.ModelCardToolkit(
        source=tf_sources.Source(
            tfma=tfma_src, tfdv=tfdv_src, model=model_src
        )
    ).scaffold_assets()  # yapf: disable

    with self.subTest(name='quantitative_analysis'):
      list_to_proto = lambda lst: [x.to_proto() for x in lst]
      expected_performance_metrics = [
          model_card.PerformanceMetric(
              type='post_export_metrics/example_count', value='2.0'
          )
      ]
      self.assertCountEqual(
          list_to_proto(mc.quantitative_analysis.performance_metrics),
          list_to_proto(expected_performance_metrics)
      )
      self.assertLen(mc.quantitative_analysis.graphics.collection, 1)

    with self.subTest(name='model_parameters.data'):
      self.assertLen(mc.model_parameters.data, 2)  # train and eval
      for dataset in mc.model_parameters.data:
        for graphic in dataset.graphics.collection:
          self.assertIsNotNone(
              graphic.image,
              msg=f'No image found for graphic: {dataset.name} {graphic.name}'
          )
          graphic.image = None  # ignore graphic.image for below assertions
      self.assertIn(
          model_card.Dataset(
              name=train_dataset_name, graphics=model_card.GraphicsCollection(
                  collection=[
                      model_card.Graphic(name='counts | feature_name1')
                  ]
              )
          ), mc.model_parameters.data
      )
      self.assertIn(
          model_card.Dataset(
              name=eval_dataset_name, graphics=model_card.GraphicsCollection(
                  collection=[
                      model_card.Graphic(name='counts | feature_name3')
                  ]
              )
          ), mc.model_parameters.data
      )
      self.assertNotIn(
          model_card.Dataset(
              name=eval_dataset_name, graphics=model_card.GraphicsCollection(
                  collection=[
                      model_card.Graphic(name='counts | feature_name2')
                  ]
              )
          ), mc.model_parameters.data
      )

    with self.subTest(name='model_details.path'):
      self.assertEqual(mc.model_details.path, pushed_model_path)

  def test_scaffold_assets_with_empty_source(self):
    core.ModelCardToolkit(source=tf_sources.Source()).scaffold_assets()

  def test_scaffold_assets_with_invalid_tfma_source(self):
    with self.assertRaisesWithLiteralMatch(
        ValueError,
        'Only one of TfmaSource.metrics_include and TfmaSource.metrics_exclude '
        'should be set.'
    ):
      core.ModelCardToolkit(
          source=tf_sources.Source(
              tfma=tf_sources.TfmaSource(
                  eval_result_paths=['dummy/path'], metrics_include=[
                      'false_positive_rate'
                  ], metrics_exclude=['false_negative_rate']
              )
          )
      )

  def test_scaffold_assets_with_invalid_tfdv_source(self):
    with self.assertRaisesWithLiteralMatch(
        ValueError, 'Only one of TfdvSource.features_include and '
        'TfdvSource.features_exclude should be set.'
    ):
      core.ModelCardToolkit(
          source=tf_sources.Source(
              tfdv=tf_sources.TfdvSource(
                  dataset_statistics_paths=['dummy/path'], features_include=[
                      'brand_confidence'
                  ], features_exclude=['brand_prominence']
              )
          )
      )

if __name__ == '__main__':
  absltest.main()
else:
  # Manually pass and parse flags to prevent UnparsedFlagAccessError when using
  # pytest or unittest as a runner.
  flags.FLAGS(['--test_tmpdir'])


import PyPDF2
from pathlib import Path
from gtts import gTTS
from googletrans import Translator

def define_language(text):
    """Defines language received text and return it"""
    translator = Translator()
    language = translator.detect(text).lang
    return language

def pdf_to_mp3(pdf_file_path, output_dir_path):
    """Extract text from pdf file and convert it to mp3 file. Return output audiofile path or None in case if passed
    incorrect file path or file is not pdf."""
    if Path(pdf_file_path).is_file() and Path(pdf_file_path).suffix == '.pdf':
        with open(pdf_file_path, 'rb') as pdf:
            reader = PyPDF2.PdfFileReader(pdf, strict=False)
            all_pages = [page.extract_text() for page in reader.pages]
        text_from_pdf = ''.join(all_pages).replace('\n', '')  # line breaks are deleted to sound without long pauses

        output_audio = gTTS(text=text_from_pdf, lang=define_language(text_from_pdf))
        mp3_file_name = f'{Path(pdf_file_path).stem}.mp3'
        output_audio.save(f'{output_dir_path}/{mp3_file_name}')

        return mp3_file_name

pdf_to_mp3('uploads/Test_PDF.pdf', 'downloads')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 21 18:43:37 2019

@author: zhanghuangzhao
"""

import numpy
import random
from mnist import MNIST

class dataset(object):

    def __init__(self, x, y):

        self.__x = numpy.asarray(x).reshape((-1, 28, 28, 1))
        self.__y = numpy.asarray(y)
        self.size = self.__x.shape[0]
        self.__idx = []
        self.__shuffle()
        self.images = self.__x
        self.labels = self.__y

    def __shuffle(self):

        self.__idx = random.sample(range(self.size), self.size)

    def reset_epoch(self):

        self.__shuffle()

    def next_batch(self, batch_size, dtype=numpy.float32):

        if len(self.__idx) < batch_size:
            self.__shuffle()
        assert batch_size <= self.size, \
            "Batch size %d is larger than dataset size %d" % (batch_size, self.size)

        idx = self.__idx[:batch_size]
        self.__idx = self.__idx[batch_size:]
        x, y = ([], [])
        for i in idx:
            x.append(self.__x[i])
            y.append([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
            y[-1][self.__y[i]] = 1
        return numpy.asarray(x, dtype=dtype), numpy.asarray(y, dtype=dtype)

class Fashion_MNIST(object):

    def __init__(self, validation_size=10000, data_dir="../data"):

        fmnist = MNIST(data_dir, return_type="lists")
        train = fmnist.load_training()
        test = fmnist.load_testing()

        assert validation_size >= 0 and validation_size <= len(train[0]), \
            "Invalid validattion ratio %.3f, should be within 0 to %d" \
            % (validation_size, len(train[0]))

        idx = random.sample(range(len(train[0])), len(train[0]))
        tr, va = ([[], []], [[], []])
        for i in idx[:validation_size]:
            va[1].append(train[1][i])
            va[0].append(train[0][i])
        for i in idx[validation_size:]:
            tr[1].append(train[1][i])
            tr[0].append(train[0][i])
        self.train = dataset(tr[0], tr[1])
        self.valid = dataset(va[0], va[1])
        self.test = dataset(test[0], test[1])

        self.__label_dict = ["t-shirt/top", "trouser", "pullover", "dress",
                             "coat", "sandal", "shirt", "sneaker", "bag",
                             "ankle boot"]

    def get_label(self, label_idx):

        return self.__label_dict[label_idx]

    def get_labels(self):

        return self.__label_dict

if __name__ == "__main__":

    fmnist = Fashion_MNIST()

    try:
        import matplotlib.pyplot as plt
        for i in range(10):
            x, y = fmnist.train.next_batch(1)
            while not numpy.argmax(y) == i:
                x, y = fmnist.train.next_batch(1)
            plt.imshow(x.reshape([28, 28]))
            plt.imsave("../images/fmnist_%d.jpg"%i, x.reshape([28, 28]))
    except Exception as e:
        print(e)


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

"""Integration test for `cycle_calendar_generator` package."""

import unittest
import os
import sys
import shutil
import subprocess
from pathlib import Path
import datetime

from cycle_calendar_generator import cycle_calendar_generator

import ics
import arrow

CURRENT_WORKING_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
INTEGRATION_TEST_FOLDER = os.path.join(
    CURRENT_WORKING_DIRECTORY,
    'integration_test'
    )
TEST_FILES_FOLDER = os.path.join(INTEGRATION_TEST_FOLDER, 'testing_files')
TEST_EXPECTED_OUTPUT_FOLDER = os.path.join(INTEGRATION_TEST_FOLDER, 'expected')
TEST_TEMP_FOLDER = os.path.join(INTEGRATION_TEST_FOLDER, 'temp')
TEST_OUTPUT_FOLDER = os.path.join(TEST_TEMP_FOLDER, 'output')
SCRIPT_NAME = 'cycle_calendar_generator'

class Test_integration(unittest.TestCase):
    """Tests function to get folder argument and give default if none given"""

    @classmethod
    def setUpClass(cls):
        if (not os.path.exists(TEST_EXPECTED_OUTPUT_FOLDER)):
            os.mkdir(TEST_EXPECTED_OUTPUT_FOLDER)
        if (not os.path.exists(TEST_TEMP_FOLDER)):
            os.mkdir(TEST_TEMP_FOLDER)

    def setUp(self):
        # copy test input files from TEST_FILES_FOLDER to TEST_TEMP_FOLDER
        for file in cycle_calendar_generator.scandir_with_version_check(
                TEST_FILES_FOLDER,
                cycle_calendar_generator.VERSION_MAJOR,
                cycle_calendar_generator.VERSION_MINOR):
            source_path = file.path
            dest_path = os.path.join(TEST_TEMP_FOLDER, file.name)
            shutil.copy(source_path, dest_path)

    def tearDown(self):
        # delete all files and folders in TEST_TEMP_FOLDER
        for file in cycle_calendar_generator.scandir_with_version_check(
                TEST_TEMP_FOLDER,
                cycle_calendar_generator.VERSION_MAJOR,
                cycle_calendar_generator.VERSION_MINOR):
            if file.is_dir():
                shutil.rmtree(file.path)
            else:
                os.remove(file.path)

    def test_script_works_in_normal_case(self):
        # run script
        exit_code = subprocess.run(['python3', '-m',
                                    SCRIPT_NAME, TEST_TEMP_FOLDER])
        # read output icals into dictionary (user name as key)
        output_files = {}
        for file in cycle_calendar_generator.scandir_with_version_check(
                TEST_OUTPUT_FOLDER,
                cycle_calendar_generator.VERSION_MAJOR,
                cycle_calendar_generator.VERSION_MINOR):
            if file.is_file():
                path, filename = os.path.split(file.path)
                user_name = os.path.splitext(filename)[0]
                with open(file.path) as ical:
                    calendar = ics.Calendar(ical.read())
                sorted_events = sorted(calendar.events,
                                       key=lambda event:event.begin)
                output_files[user_name] = sorted_events
        # read expected output icals into similar dictionary
        expected_files = {}
        for file in cycle_calendar_generator.scandir_with_version_check(
                TEST_EXPECTED_OUTPUT_FOLDER,
                cycle_calendar_generator.VERSION_MAJOR,
                cycle_calendar_generator.VERSION_MINOR):
            if file.is_file():
                path, filename = os.path.split(file.path)
                user_name = os.path.splitext(filename)[0]
                with open(file.path) as ical:
                    calendar = ics.Calendar(ical.read())
                sorted_events = sorted(calendar.events,
                                       key=lambda event:event.begin)
                expected_files[user_name] = sorted_events
        # assert both dicts have same size
        self.assertEqual(len(output_files), len(expected_files))
        # assert each key in output has matching in expected
        for key in output_files.keys():
            self.assertTrue(key in expected_files)
            # assert values from matching keys are the same
            output_events = output_files[key]
            expected_events = expected_files[key]
            for i in range(len(output_events)):
                output_event = output_events[i]
                expected_event = expected_events[i]
                self.assertEqual(output_event.name, expected_event.name)
                # Output times already use local timezone,
                # but expected times use tz in iCal file (China Std. Time)
                # Expected times need to have tz replaced with local tz
                # without changing anything else
                output_event_start = output_event.begin.to('utc')
                expected_event_start = expected_event.begin.replace(
                    tzinfo=cycle_calendar_generator.LOCAL_TIMEZONE
                ).to('utc')
                output_event_end = output_event.end.to('utc')
                expected_event_end = expected_event.end.replace(
                    tzinfo=cycle_calendar_generator.LOCAL_TIMEZONE
                ).to('utc')
                self.assertEqual(output_event_start, expected_event_start)
                self.assertEqual(output_event_end, expected_event_end)


from flask_wtf import FlaskForm
from flask import Flask, session, request, redirect, url_for
from wtforms import TextField, SubmitField
from flask_wtf.file import FileField, FileRequired, FileAllowed
from random import randint
import numpy as np
from cv2 import cv2 as cv2
import os
from werkzeug.utils import secure_filename

root = os.path.dirname(os.path.abspath(__file__))

class Edge(FlaskForm):
    validators = [FileRequired(message='There was no file!'),
                  FileAllowed(['png', 'jpg'], message='ທ່ານຕ້ອງເລືອກໄຟຣ png, jpg ເທົ່ານັ້ນ')]
    photo = FileField('', validators=validators)

    def UploadPhoto(self, form):
        if form.validate_on_submit():
            f = form.photo.data
            filename = secure_filename(f.filename)
            ext = filename.rsplit(".", 1)[1]
            filename = str(randint(1000000000, 9999999999)) + '.' + ext
            f.save(os.path.join(root, '..', 'static', 'photos', filename))
            session['img_name_org_edge'] = filename

    def CovertCanny(self):
        img = cv2.imread(os.path.join(root, '..', 'static', 'photos', session['img_name_org_edge']))
        edges = cv2.Canny(img, 100, 200)
        filename = str(randint(1000000000, 9999999999)) + session['img_name_org_edge']
        cv2.imwrite(os.path.join(root, '..', 'static', 'photos', filename), edges)
        session['img_name_covert_edge'] = filename
        session['covert_title_edge'] = "Detection By Canny"

    def CovertSobel(self):
        img = cv2.imread(os.path.join(root, '..', 'static', 'photos', session['img_name_org_edge']))
        sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)  # x
        # sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)  # y
        filename = str(randint(1000000000, 9999999999)) + session['img_name_org_edge']
        cv2.imwrite(os.path.join(root, '..', 'static', 'photos', filename), sobelx)
        session['img_name_covert_edge'] = filename
        session['covert_title_edge'] = "Detection By Sobel"

    def CovertLaplacian(self):
        img = cv2.imread(os.path.join(root, '..', 'static', 'photos', session['img_name_org_edge']))
        laplacian = cv2.Laplacian(img, cv2.CV_64F)
        filename = str(randint(1000000000, 9999999999)) + session['img_name_org_edge']
        cv2.imwrite(os.path.join(root, '..', 'static', 'photos', filename), laplacian)
        session['img_name_covert_edge'] = filename
        session['covert_title_edge'] = "Detection By Laplacian"

    def CovertCountour(self):
        img = cv2.imread(os.path.join(root, '..', 'static', 'photos', session['img_name_org_edge']))
        imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        ret, thresh = cv2.threshold(imgray, 127, 255, 0)
        contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        cv2.drawContours(img, contours, -1, (0, 255, 0), 3)

        filename = str(randint(1000000000, 9999999999)) + session['img_name_org_edge']
        cv2.imwrite(os.path.join(root, '..', 'static', 'photos', filename), img)
        session['img_name_covert_edge'] = filename
        session['covert_title_edge'] = "Detection By Countour"


import os
from datetime import datetime, date
import django

# from django.contrib.auth.models import User
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'social_media.settings')
django.setup()
from core.attachement.models import AttachementModel

# AUTH_USER_MODEL = "core.UserModel"
attachement = AttachementModel(
    id=1,
    messagesUserId=1,
    fileUrl='https:k/#@sangnt84',
    thumbUrl='https:k/#@sangnt84',
    createdAt='2016-03-09T03:30:25.1263499Z',
    updatedAt='2016-03-09T03:30:25.1263499Z',
    messengesGroupId=None,
)
attachement.save()


from core.social_auth.oauth import oauth
from core.social_auth.constants import GITHUB_USERINFO_URL, GITHUB_EMAIL_URL
from werkzeug.exceptions import Unauthorized, BadRequest, InternalServerError
from requests.exceptions import RequestException
from authlib.integrations.flask_client import OAuthError
from flask import request

class GithubAuth:
    NAME = "github"

    def __init__(self):
        self.client = oauth.create_client("github")
        if not self.client:
            raise BadRequest("Github is not register for oauth in the backend.")

    def get_mail(self):
        try:
            response = self.client.get(GITHUB_EMAIL_URL)
        except RequestException as e:
            raise InternalServerError(f"Server failed to fetch detailed from {GITHUB_USERINFO_URL}")

        data = response.json()
        if response.status_code == 401:
            raise Unauthorized(data)
        elif response.status_code != 200:
            raise BadRequest(data)
        return next((item['email'] for item in data if item['primary']), None)

    def get_data(self):
        """
        This function fetches the data from github API.
        Parameter
        ---------
        Return
        ------
        data: dict
        """
        token = request.get_json(force=True, silent=True).get('token')
        if not token:
            raise BadRequest("The Token is not provided.")

        self.client.token = token

        data = token.get('userinfo')
        if not data:
            try:
                response = self.client.get(GITHUB_USERINFO_URL, params={'skip_status': True})
            except RequestException as e:
                raise InternalServerError(f"Server failed to fetch detailed from {GITHUB_USERINFO_URL}")
            except OAuthError as error:
                raise InternalServerError(f"Server failed to fetch detailed from {GITHUB_USERINFO_URL}, errors: {error.error}")
            data = response.json()
            if response.status_code == 401:
                raise Unauthorized(data)
            elif response.status_code != 200:
                raise BadRequest(data)

            if not data.get('email'):
                data['email'] = self.get_mail()

        return data

    def auth(self):
        """
        This function handles social logins for github.
        Parameter
        ---------
        Return
        ------
        """
        return self.get_data()


from pyrep.robots.arms.jaco import Jaco
from pyrep.robots.arms.mico import Mico
from pyrep.robots.arms.panda import Panda
from pyrep.robots.arms.sawyer import Sawyer
from pyrep.robots.arms.ur5 import UR5
from pyrep.robots.end_effectors.baxter_gripper import BaxterGripper
from pyrep.robots.end_effectors.jaco_gripper import JacoGripper
from pyrep.robots.end_effectors.mico_gripper import MicoGripper
from pyrep.robots.end_effectors.panda_gripper import PandaGripper
from pyrep.robots.end_effectors.robotiq85_gripper import Robotiq85Gripper

colors = [
    ('red', (1.0, 0.0, 0.0)),
    ('maroon', (0.5, 0.0, 0.0)),
    ('lime', (0.0, 1.0, 0.0)),
    ('green', (0.0, 0.5, 0.0)),
    ('blue', (0.0, 0.0, 1.0)),
    ('navy', (0.0, 0.0, 0.5)),
    ('yellow', (1.0, 1.0, 0.0)),
    ('cyan', (0.0, 1.0, 1.0)),
    ('magenta', (1.0, 0.0, 1.0)),
    ('silver', (0.75, 0.75, 0.75)),
    ('gray', (0.5, 0.5, 0.5)),
    ('orange', (1.0, 0.5, 0.0)),
    ('olive', (0.5, 0.5, 0.0)),
    ('purple', (0.5, 0.0, 0.5)),
    ('teal', (0, 0.5, 0.5)),
    ('azure', (0.0, 0.5, 1.0)),
    ('violet', (0.5, 0.0, 1.0)),
    ('rose', (1.0, 0.0, 0.5)),
    ('black', (0.0, 0.0, 0.0)),
    ('white', (1.0, 1.0, 1.0)),
]

# Arms from PyRep need to be modified to include a wrist camera.
# Currently, only the arms/grippers below are supported.
SUPPORTED_ROBOTS = {
    'panda': (Panda, PandaGripper, 7),
    'jaco': (Jaco, JacoGripper, 6),
    'mico': (Mico, MicoGripper, 6),
    'sawyer': (Sawyer, BaxterGripper, 7),
    'ur5': (UR5, Robotiq85Gripper, 6),
}


def merge_cells(cell1, cell2, direction):
    """
    Combine the side of cell1's grid text with cell2's text.

    For example::

         cell1    cell2      merge "RIGHT"

        +-----+  +------+   +-----+------+
        | foo |  | dog  |   | foo | dog  |
        |     |  +------+   |     +------+
        |     |  | cat  |   |     | cat  |
        |     |  +------+   |     +------+
        |     |  | bird |   |     | bird |
        +-----+  +------+   +-----+------+

    Parameters
    ----------
    cell1 : dashtable.data2rst.Cell
    cell2 : dashtable.data2rst.Cell
    """

    cell1_lines = cell1.text.split("\n")
    cell2_lines = cell2.text.split("\n")

    if direction == "RIGHT":
        for i in range(len(cell1_lines)):
            cell1_lines[i] = cell1_lines[i] + cell2_lines[i][1::]
        cell1.text = "\n".join(cell1_lines)
        cell1.column_count += cell2.column_count

    elif direction == "TOP":
        if cell1_lines[0].count('+') > cell2_lines[-1].count('+'):
            cell2_lines.pop(-1)
        else:
            cell1_lines.pop(0)
        cell2_lines.extend(cell1_lines)
        cell1.text = "\n".join(cell2_lines)
        cell1.row_count += cell2.row_count
        cell1.row = cell2.row
        cell1.column = cell2.column

    elif direction == "BOTTOM":
        if (cell1_lines[-1].count('+') > cell2_lines[0].count('+') or
                cell1.is_header):
            cell2_lines.pop(0)
        else:
            cell1_lines.pop(-1)
        cell1_lines.extend(cell2_lines)
        cell1.text = "\n".join(cell1_lines)
        cell1.row_count += cell2.row_count

    elif direction == "LEFT":
        for i in range(len(cell1_lines)):
            cell1_lines[i] = cell2_lines[i][0:-1] + cell1_lines[i]
        cell1.text = "\n".join(cell1_lines)
        cell1.column_count += cell2.column_count
        cell1.row = cell2.row
        cell1.column = cell2.column


# Python Program To Read Five Characters Which Start With ‘A’ And End With ‘Z’
import re
s = input("Enter the string: ")
pat = re.compile(r'^a[a-zA-Z]{3}z$')
matched = pat.search(s)
if matched != None:
    print("Search is successful")
else:
    print("Search is unsuccessful")


#import sys
#print(sys.path)
#sys.path.append('/Users/takumi/opt/anaconda3/envs/qc/lib/python3.9/site-packages')
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensornetwork as tn
from opt_einsum import contract
import time

"""
The code is the fc2 neural network for mnist whose linear operations are replaced by mpo.
(28,28)->(4,7,7,4)->(4,4,4,4)->(1,1,10,1)->(10)
The optimizer uses SGD.
###This code costs a lot of time for fitting.###
"""

def X_tensor(X_data):
    now_data = [[[[[X_data[i][7*j+k][7*m+l]/255.0 for m in range(4)]for l in range(7)]for k in range(7)]for j in range(4)] for i in range(len(X_data))]
    return np.array(now_data)

def y_initialize(y_data):
        y=[]
        for _ in range(len(y_data)):
            now=np.zeros(10)
            now[y_data[_]]=1.0
            y.append(now)

        return np.array(y)

def set_w1(d):
    W=[]
    for i in range(4):
        if i==0 or i==3:
            #now_w=[[[random.random()for l in range(d)]for j in range(4)] for k in range(4)]
            now_w = np.random.random((4,4,d))-0.5
        else :
            #now_w=[[[[random.random()for l in range(d)]for j in range(d)] for k in range(4)]for m in range(7)]
            now_w = np.random.random((7,4,d,d))-0.5
        W.append(now_w)
    return W

def set_w2(d):
    W=[]
    for i in range(4):
        if i==0 or i==3:
            #now_w=[[[random.random()for l in range(d)]for j in range(1)] for k in range(4)]
            now_w = np.random.random((4,1,d))-0.5
        elif i==1:
            #now_w=[[[[random.random()for l in range(d)]for j in range(d)] for k in range(1)]for m in range(4)]
            now_w = np.random.random((4,1,d,d))-0.5
        else :
            #now_w=[[[[random.random()for l in range(d)]for j in range(d)] for k in range(10)]for m in range(4)]
            now_w = np.random.random((4,10,d,d))-0.5
        W.append(now_w)
    return W

class mpo_fc2:

    mnist = tf.keras.datasets.mnist
    (X_train, y_train),(X_test, y_test) = mnist.load_data()
    #tX_train = X_tensor(X_train)
    #tX_test = X_tensor(X_test)
    tX_train = X_train.reshape((60000,4,7,7,4))/255.0
    tX_test = X_test.reshape((10000,4,7,7,4))/255.0
    d = 4
    w1 = set_w1(d)
    w2 = set_w2(d)
    yr_train = y_initialize(y_train)
    yr_test = y_initialize(y_test)

    def __init__(self,eta=0.01,n_iter=1000,batch_size=100):
        self.eta=eta
        self.n_iter=n_iter
        self.batch_size = batch_size
        self.train_loss = []
        self.test_loss = []
        self.outputs = []
        self.train_accuracy = []
        self.test_accuracy = []
        self.time = time.time()

    def relu(self,input_tensor):
        shape = input_tensor.shape
        shape_pro = 1
        for i in shape :
            shape_pro = shape_pro*i
        output_tensor = input_tensor.copy().reshape(shape_pro)
        for i in range(len(output_tensor)):
            output_tensor[i] = output_tensor[i] if output_tensor[i]>0 else 0
        output_tensor = output_tensor.reshape(shape)

        return output_tensor

    def soft_max(self,input_vector):
        return np.exp(input_vector)/(np.exp(input_vector).sum())

    def delta_relu(self,input_tensor,ref_tensor):
        shape = input_tensor.shape
        shape_pro = 1
        for i in shape :
            shape_pro = shape_pro*i
        finput_tensor = input_tensor.copy().reshape(shape_pro)
        fref_tensor = ref_tensor.copy().reshape(shape_pro)
        for i in range(len(finput_tensor)):
            finput_tensor[i] = finput_tensor[i] if fref_tensor[i]>0 else 0
        finput_tensor = finput_tensor.reshape(shape)

        return finput_tensor

    def forward(self,X_data):
        self.outputs.clear()
        now = X_data.copy()

        self.outputs.append(now)

        now = contract('ija,klab,mnbc,opc,ikmo->jlnp',self.w1[0],self.w1[1],self.w1[2],self.w1[3],now)
        #now = np.einsum('ija,klab,mnbc,opc,ikmo->jlnp',self.w1[0],self.w1[1],self.w1[2],self.w1[3],now)

        self.outputs.append(now)
        now = self.relu(now)

        self.outputs.append(now)

        now = contract('ija,klab,mnbc,opc,ikmo->jlnp',self.w2[0],self.w2[1],self.w2[2],self.w2[3],now)
        now = now.reshape(10)

        self.outputs.append(now)

        now = self.soft_max(now)

        self.outputs.append(now)

        return now
        """"
        outputs=[入力(4,7,7,4),w1入力(4,4,4,4),relu後(4,4,4,4),w2後(10),soft_max(10)]
        """
    def backward(self,y):
        delta0 = self.outputs[4]-y

        delta0 = delta0.reshape(1,1,10,1)
        deltaw20 = contract('xbcd,bqzj,cwjk,dek,yqwe->xyz',self.outputs[2],self.w2[1],self.w2[2],self.w2[3],delta0)
        deltaw21 = contract('axcd,aqz,cwpk,dek,qywe->xyzp',self.outputs[2],self.w2[0],self.w2[2],self.w2[3],delta0)
        deltaw22 = contract('abxd,aqi,bwiz,drp,qwyr->xyzp',self.outputs[2],self.w2[0],self.w2[1],self.w2[3],delta0)
        deltaw23 = contract('abcx,aqi,bwij,crjz,qwry->xyz',self.outputs[2],self.w2[0],self.w2[1],self.w2[2],delta0)

        delta1 = contract('aqi,bwij,crjz,dyz,qwry->abcd',self.w2[0],self.w2[1],self.w2[2],self.w2[3],delta0)

        delta2 = self.delta_relu(delta1,self.outputs[1])

        deltaw10 = contract('xbcd,bqzj,cwjk,dek,yqwe->xyz',self.outputs[0],self.w1[1],self.w1[2],self.w1[3],delta2)
        deltaw11 = contract('axcd,aqz,cwpk,dek,qywe->xyzp',self.outputs[0],self.w1[0],self.w1[2],self.w1[3],delta2)
        deltaw12 = contract('abxd,aqi,bwiz,drp,qwyr->xyzp',self.outputs[0],self.w1[0],self.w1[1],self.w1[3],delta2)
        deltaw13 = contract('abcx,aqi,bwij,crjz,qwry->xyz',self.outputs[0],self.w1[0],self.w1[1],self.w1[2],delta2)

        self.w2[0]-=self.eta*deltaw20
        self.w2[1]-=self.eta*deltaw21
        self.w2[2]-=self.eta*deltaw22
        self.w2[3]-=self.eta*deltaw23
        self.w1[0]-=self.eta*deltaw10
        self.w1[1]-=self.eta*deltaw11
        self.w1[2]-=self.eta*deltaw12
        self.w1[3]-=self.eta*deltaw13

    def loss_function(self,x_data,y_data):
        return -(y_data.T@np.log(x_data))

    def fit(self):
        n_sample = int(np.ceil(len(self.tX_train)/self.batch_size))
        for _ in range(self.n_iter):
            for i in range(n_sample):
                index = np.random.randint(0,len(self.tX_train))
                batch_x=self.tX_train[index]
                batch_y = self.yr_train[index]

                output = self.forward(batch_x)

                self.backward(batch_y)

            #print('iter:',_,'time:',time.time()-self.time)
            #self.time = time.time()

            if _%100==0:
                now_acc =0
                now_loss = 0.
                #for i in range(len(self.X_train)):
                #    output = self.forward(self.X_train[i])
                #    if np.argmax(output)==self.y_train[i]:
                #        now_acc+=1
                #
                #    now_loss += self.loss_function(output,self.yr_train[i])
                for i in range(len(self.tX_test)):
                    output = self.forward(self.tX_test[i])
                    if np.argmax(output)==self.y_test[i]:
                        now_acc+=1
                    now_loss += self.loss_function(output,self.yr_test[i])

                #now_acc/=(len(self.X_train))
                #now_loss/=(len(self.X_train))
                now_acc/=(len(self.X_test))
                now_loss/=(len(self.X_test))
                #self.train_accuracy.append(now_acc)
                self.test_accuracy.append(now_acc)
                self.train_loss.append(now_loss)
                #print('iter:',_,'accuracy:',now_acc,'loss_function:',now_loss,'time:',time.time()-self.time)
                print('iter:',_,'accuracy:',now_acc,'loss_function:',now_loss,'time:',time.time()-self.time)
                self.time = time.time()

        plt.plot(self.test_accuracy)
        plt.show()

    def test(self):
        now_acc =0
        now_loss = 0.
        for i in range(len(self.tX_test)):
            output = self.forward(self.tX_test[i])
            if np.argmax(output)==self.y_test[i]:
                now_acc+=1
            now_loss += self.loss_function(output,self.yr_test[i])

        now_acc/=(len(self.tX_test))
        now_loss/=(len(self.tX_test))
        self.test_accuracy.append(now_acc)
        self.test_loss.append(now_loss)
        print('test_accuracy:',now_acc,'test_loss_function:',now_loss)

if __name__ == '__main__':
    cnn = mpo_fc2(eta=0.01,n_iter=1000,batch_size=100)
    cnn.fit()
    cnn.test()