import pandas as pd
import numpy as np
import random
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, classification_report, confusion_matrix
from sklearn.model_selection import GridSearchCV

df = pd.read_table("dialog_acts.dat", header=None, names=['data'])
df['act'] = df['data'].str.split(' ').str[0]
df['sentence'] = df['data'].str.split(' ').str[1:]
df['sentence'] = df['sentence'].str.join(' ')
df.drop(labels='data', inplace=True, axis=1)

df.act.value_counts()

contraptions = {'doesnt': "does not",
               'im': 'i am',
               'dont': 'do not',
               'id': 'i would'}

df['sentence'] = df['sentence'].replace(contraptions, regex=True)
df['sentence_length'] = df.sentence.str.split().apply(len)
df['sentence_length'].describe()

x = df['sentence']
y = df['act']

train_categories = 0
test_categories = 0

while train_categories < 15 and test_categories < 15:
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.15)
    train_categories = len(y_train.value_counts())
    test_categories = len(y_test.value_counts())

print(f'\nSamples in train set: {len(x_train)}')
print(f'Samples in test set: {len(x_test)}')

i = 0
def create_cf_matrix(y_test, y_pred, save_fig=False):
    global i
    cf_matrix = confusion_matrix(y_test, y_pred, labels=list(y_test.unique()))
    cm_sum = np.sum(cf_matrix, axis=1, keepdims=True)
    f = plt.figure(figsize=(11, 11))
    ax = plt.subplot()
    sns.heatmap(cf_matrix / cm_sum, annot=True, ax=ax, fmt='0.1%', cmap='Blues')  # annot=True to annotate cells

    ax.set_xlabel('Predicted labels')
    ax.set_ylabel('True labels')
    ax.set_title('Confusion Matrix')
    ax.xaxis.set_ticklabels(list(y_test.unique()), rotation=45)
    ax.yaxis.set_ticklabels(list(y_test.unique()), rotation=45)

    titles = ['majority', 'rule_based', 'decision_tree', 'SVM', 'MLP']
    if save_fig:
        plt.savefig(f'confusion_matrix_{titles[i]}')
    i += 1

print('Accuracy of the majority class classifier on the test samples:')
y_pred = len(y_test) * ['inform']
print(f"accuracy: {accuracy_score(y_test, y_pred)}")
print(f"precision: {precision_score(y_test, y_pred, average='weighted')}")
print(f"recall: {recall_score(y_test, y_pred, average='weighted')}")
create_cf_matrix(y_test, y_pred)

print('Accuracy of the rulebased classifier on the test samples')
# dictionary with act as key and corresponding words as values
rules = {'ack': ['okay', 'okay um', 'alright'], 'affirm': ['yes right', 'right', 'yes'],
         'bye': ['see you', 'good bye', 'bye'], 'confirm': ['is it'], 'deny': ['i dont want'],
         'hello': ['hi', 'hello'],
         'inform': ['looking for'], 'negate': ['no'], 'repeat': ['can you repeat that', 'what did you say'],
         'reqalts': ['how about'], 'reqmore': ['more'], 'request': ['what is', 'where'], 'restart': ['start over'],
         'thankyou': ['thank you', 'thanks']}

y_pred = []

for x in x_test:
    acts = []
    for k, v in rules.items():
        if any(keywords in x.lower() for keywords in v):
            acts.append(k)
    if not acts:
        y_pred.append('null')
    else:
        y_pred.append(random.choice(acts))

print(f"accuracy: {accuracy_score(y_test, y_pred)}")
print(f"precision: {precision_score(y_test, y_pred, average='weighted')}")
print(f"recall: {recall_score(y_test, y_pred, average='weighted')}")
create_cf_matrix(y_test, y_pred)

max_sentences = 0.85
min_sentences = 1
BOW_vect = CountVectorizer(max_df=max_sentences, min_df=1, lowercase=True, strip_accents='ascii')
x_train_counts = BOW_vect.fit_transform(x_train)

### Decision Tree
print('Finding best hyperparameters for Decision tree using grid search and cross validation.')
params = {'max_depth': list(range(1,25)),
          'min_samples_split': [10, 50,100]}
grid_search_cv = GridSearchCV(DecisionTreeClassifier(), params, verbose=1, cv=3, n_jobs=-1)
grid_search_cv.fit(x_train_counts, y_train)
x_test_counts = BOW_vect.transform(x_test)
y_pred = grid_search_cv.predict(x_test_counts)
print('Evaluation of the best hyperparameters on the test set')
print(f"accuracy: {accuracy_score(y_test, y_pred)}")
print(f"precision: {precision_score(y_test, y_pred, average='weighted')}")
print(f"recall: {recall_score(y_test, y_pred, average='weighted')}")
create_cf_matrix(y_test, y_pred)

### SVM
print('Finding best parameters for SVM using grid search and cross validation.')
params = params = {
    "loss" : ["hinge", "log", "squared_hinge", "modified_huber"],
    "alpha" : [0.0001, 0.001, 0.01, 0.1],
    "penalty" : ["l2", "l1", "none", "elasticnet"],
}

grid_search_cv = GridSearchCV(SGDClassifier(), params, verbose=1, cv=3, n_jobs=-1)
grid_search_cv.fit(x_train_counts, y_train)

x_test_counts = BOW_vect.transform(x_test)
y_pred = grid_search_cv.predict(x_test_counts)
print('Evaluation of the best hyperparameters on the test set')
print(f"accuracy: {accuracy_score(y_test, y_pred)}")
print(f"precision: {precision_score(y_test, y_pred, average='weighted')}")
print(f"recall: {recall_score(y_test, y_pred, average='weighted')}")
create_cf_matrix(y_test, y_pred)

results = pd.DataFrame({'utterance': x_test, 'act': y_test, 'predicted_act':y_pred})
results['utterance_length'] = results.utterance.str.split().apply(len)
corrects = results[(results.act == results.predicted_act)]
mistakes = results[(results.act != results.predicted_act)]
print(f'Distribution True:\n{mistakes.act.value_counts()}')
print(f'Distribution predicted:\n{mistakes.predicted_act.value_counts()}')
print(f'Utterance length description:\n {results.utterance_length.describe()}')
print(f'Utterance length description:\n {mistakes.utterance_length.describe()}')
print(f'Mistakes: {mistakes}')

### MLP
print('Finding best parameters for MLP using grid search and cross validation.')
params = {
    'hidden_layer_sizes': [(10,30,10),(20, 20), (20)],
    'activation': ['tanh', 'relu'],
    'solver': ['sgd', 'adam'],
    'alpha': [0.0001, 0.05],
    'learning_rate': ['constant','adaptive'],
}

grid_search_cv = GridSearchCV(MLPClassifier(max_iter=100), params, n_jobs=-1, cv=3)
grid_search_cv.fit(x_train_counts, y_train)

x_test_counts = BOW_vect.transform(x_test)
y_pred = grid_search_cv.predict(x_test_counts)

print('Evaluation of the best hyperparameters on the test set')
print(f"accuracy: {accuracy_score(y_test, y_pred)}")
print(f"precision: {precision_score(y_test, y_pred, average='weighted')}")
print(f"recall: {recall_score(y_test, y_pred, average='weighted')}")

create_cf_matrix(y_test, y_pred)

results = pd.DataFrame({'utterance': x_test, 'act': y_test, 'predicted_act':y_pred})
results['utterance_length'] = results.utterance.str.split().apply(len)
corrects = results[(results.act == results.predicted_act)]
mistakes = results[(results.act != results.predicted_act)]
print(f'Distribution True:\n{mistakes.act.value_counts()}')
print(f'Distribution predicted:\n{mistakes.predicted_act.value_counts()}')
print(f'Utterance length description:\n {results.utterance_length.describe()}')
print(f'Utterance length description:\n {mistakes.utterance_length.describe()}')
print(f'Mistakes: {mistakes}')


import pandas as pd
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import fonctions

#Fonction pour récupérer la fiche individuelle d'un joueur à partir du lien de sa page
def Scraping_ficheindiv_from_lien(site_team):
    #l'url ou chercher les données : site_team

    #ouverture de la page html
    html = urlopen(site_team)
    #collecte du Code HTML de la page dans un object Soup :
    soup = BeautifulSoup(html, features="html.parser")
    #création d'une sous-partie de l'html de base
    divb = soup.find("div", {"itemtype":"https://schema.org/Person"})

    #récupération du nom du joueur
    #Pour cela on scrap la balise "span" dans la balise "h1"
    rowss = divb.findAll('h1')[0:]
    nom = [[h1.getText() for h1 in rowss[i].findAll('span')]
                    for i in range(len(rowss))]
    nom=nom[0][0]

    #création des noms de lignes
    headers=['Nom', 'Nom complet', 'Taille', 'Poids', 'Date de naissance', 'Lieu de Naissance', 'College' , 'Equipe de Draft', 'Date de Draft', 'Début en NBA']

    #récupération des données
    #On récupère l'ensemble du contenu des balises "p"
    rows = divb.findAll('p')[1:]

    #Conversion en string
    stri=str(rows)
    #Supression des balises et des espaces du texte
    stri=re.sub('<[^>]+>', '', stri)
    stri=stri.replace('  ','')
    stri = stri.replace('\n', ';')
    stri = stri.replace(';;;', ';;')
    stri = stri.replace(';;', ';')
    stri = stri.replace(';', '\n')
    stri = stri.replace('[', '')
    stri = stri.replace(']', '')
    stri = stri.replace(',', '  ')

    return stri

#Cette fonction permet de récupérer la fiche individuelle du ième joueur
#à partir du lien de la page de l'ensemble joueurs
def Scraping_ficheindiv_players(i,varurl):

    #définition de l'url ou chercher les données :
    html = urlopen(varurl)
    #collecte du Code HTML de la page dans un object Soup :
    soup = BeautifulSoup(html, features="html.parser")
    #création d'une sous-partie de l'html de base
    divb = soup.find("table", {"id":"totals_stats"})
    #récupération des liens des pages des jouerus dans le bon ordre
    liens=divb.findAll('a', attrs={'href': re.compile("^/players/")})
    #récupération de la balise du ième joueur
    balise = liens[i]
    #conversion en type string
    schaine=str(balise)
    #récuperation du lien parmi la balise
    y = schaine.find("html")
    lienprecis = schaine[9:y+4]
    #reconstruction du lien complet
    urlteam="https://www.basketball-reference.com"+lienprecis
    #récupération du sommaire du joueurs grâce à la fonction Scraping_sommaire_from_lien
    ficheindiv=Scraping_ficheindiv_from_lien(urlteam)
    return ficheindiv


import numpy as np
import cv2
import pickle

# mengekstraksi fitur citra

fitur = np.zeros((3040,4),dtype=np.float)
#kelas = np.zeros((3040,1),dtype=np.int)
for i in range(1,20):
    for j in range(1,21):
        nama_file='D:\VK_Python\Dataset Wajah\F'+str(i)+'.'+str(j)+'.jpg'
        img = cv2.imread(nama_file,1)
        for k in range(0, 3):
            fitur[(i-1)*20+j-1,k]=img[:,:,k].mean()
        #kelas[(i-1)*20+j-1]=i
        #fitur[(i-1)*20+j-1,3]=int(i)
        fitur[(i-1)*20+j-1,3]=int(i)

for i in range(1,114):
    for j in range(1,21):
        nama_file='D:\VK_Python\Dataset Wajah\M'+str(i)+'.'+str(j)+'.jpg'
        img = cv2.imread(nama_file,1)
        for k in range(0, 3):
            fitur[380+(i-1)*20+j-1,k]=img[:,:,k].mean()
        #kelas[380+(i-1)*20+j-1]=19+i
        #fitur[380+(i-1)*20+j-1,3]=int(i)
        fitur[380+(i-1)*20+j-1,3]=int(i+19)

for i in range(1,21):
    for j in range(1,21):
        nama_file='D:\VK_Python\Dataset Wajah\MS'+str(i)+'.'+str(j)+'.jpg'
        img = cv2.imread(nama_file,1)
        for k in range(0, 3):
            fitur[2640+(i-1)*20+j-1,k]=img[:,:,k].mean()
        #kelas[2640+(i-1)*20+j-1]=132+i
        #fitur[2640+(i-1)*20+j-1,3]=int(i)
        fitur[2640+(i-1)*20+j-1,3]=int(i+132)

# menyimpan fitur ke dalam file
outputFile = 'FiturWarnaWajah.data'
fw = open(outputFile, 'wb')
pickle.dump(fitur, fw)
fw.close()

#print(fitur)

#ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
#th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
#cv2.THRESH_BINARY,3,3)
#th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
#cv2.THRESH_BINARY,3,3)
#titles = ['Original Image', 'Global Thresholding (v = 127)',
#'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
#images = [img, th1, th2, th3]
#for i in xrange(4):
#    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
#    plt.title(titles[i])
#    plt.xticks([]),plt.yticks([])
#plt.show()

#cv2.namedWindow('image', cv2.WINDOW_NORMAL)
#cv2.imshow('image',img)
#cv2.waitKey(0)
#cv2.destroyAllWindows()


import numpy as np
from vtk.util import numpy_support
import vtk
import cv2

def npMatrixToVtkMatrix(matrix):
    retMat = vtk.vtkMatrix4x4()
    shape = matrix.shape
    for r in range(0,shape[0]):
        for c in range(0,shape[1]):
            retMat.SetElement(r,c,matrix[r,c])
    return retMat

def vtkMatrixtoNpMatrix(matrix):
    return np.array(matrix.GetData(), (4,4))

# Sets up a three channel empty vtkImageData object
def makeVtkImage(imgDims):
    ''' Creates a blank vtkImageData object of specified dimensions
        Input: imgDims (int, int): desired width and height
        Output: imData (vtkImageData): empty VTK image with three
                                       channels for RGB
    '''
    imData = vtk.vtkImageData()
    imData.SetDimensions(imgDims[1],imgDims[0],1) #set dimensions as necessary
    imData.SetOrigin(0,0,0) #set origin as necessary
    imData.SetSpacing(1, 1, 1) #set spacing as necessary
    # Set number of channels (3)
    if vtk.VTK_MAJOR_VERSION <= 5:
        imData.SetNumberOfScalarComponents(3)
        imData.SetScalarTypeToUnsignedChar()
        imData.Update()
    else:
        imData.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 3)
    return imData

def numpyToVtkImage(numpyData,VTKImageData):
    ''' Copies a numpy array to a vtkImageData object in place.
        Used for updating existing vtkImageData with new pixel
        data.
        https://pyscience.wordpress.com/2014/09/06/numpy-to-vtk-converting-your-numpy-arrays-to-vtk-arrays-and-files/
        was extremely useful
    '''
    if len(numpyData.shape)==3 and numpyData.shape[2] > 2 and numpyData.shape[2] <= 4:
        # Data from CV comes as BGR and vtkImageData displays as RGB so we convert
        # rgb = cv2.cvtColor(numpyData, cv2.COLOR_BGR2RGB)
        rgb = numpyData.copy()
        rgb[:,:,0:3] = cv2.cvtColor(numpyData[:,:,0:3], cv2.COLOR_BGR2RGB)
        # Get a vtkDataArray object containing numpy pixel data
        VTK_data = numpy_support.numpy_to_vtk(num_array=rgb[::-1].ravel(),
                                              deep=True,
                                              array_type=vtk.VTK_UNSIGNED_CHAR)
        # Set number of channels in pixel data
        VTK_data.SetNumberOfComponents(numpyData.shape[2])
    else:
        #Get a vtkDataArray object containing pixel data
        VTK_data = numpy_support.numpy_to_vtk(num_array=numpyData[::-1].ravel(),
                                              deep=True,
                                              array_type=vtk.VTK_UNSIGNED_CHAR)
    VTKImageData.GetPointData().SetScalars(VTK_data)

def vtkImageToNumpy(VTKImageData):
    ''' Copies pixel data from a vtkImageData and returns a new numpy array
    '''
    size = VTKImageData.GetDimensions()
    shape = (size[1],size[0],VTKImageData.GetNumberOfScalarComponents())
    VTK_data = VTKImageData.GetPointData().GetScalars()
    numpyData = numpy_support.vtk_to_numpy(VTK_data)
    numpyData = numpyData.reshape(shape)
    numpyData = numpyData[::-1]
    return numpyData

def actorFromStl(stlPath):
    ''' Reads in an STL file and returns a vtkActor
        object that the vtkRenderer can use
    '''
    reader = vtk.vtkSTLReader()
    reader.SetFileName(stlPath)
    mapper = vtk.vtkPolyDataMapper()
    if vtk.VTK_MAJOR_VERSION <= 5:
        mapper.SetInput(reader.GetOutput())
    else:
        mapper.SetInputConnection(reader.GetOutputPort())
    # Create actor that we will position manually
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    return actor

def setupRenWinForRegistration(renWin, bgImage, fgImage, camIntrinsic):
    ''' Sets up the render window for registration in place
        Input:
            renWin (vtkRenderWindow): render window to set up with
                                      actors and renderers
            bgImage (vtkImageData): image to put in the background
                                    that will be updated with video
                                    footage and masking feedback
            camIntrinsic (np.ndarray): 4x4 camera intrinsic matrix from
                                       OpenCV calibration
    '''

    renWin.SetNumberOfLayers(3)

    # Create main renderer for stl object
    ren = vtk.vtkRenderer()
    ren.SetLayer(1)
    renWin.AddRenderer(ren)
    imgDims = bgImage.GetDimensions()
    renWin.SetSize(imgDims[0],imgDims[1])
    camera = cameraFromMatrix(camIntrinsic,imgDims,imgDims)
    ren.SetActiveCamera(camera)

    # Create background renderer for static object
    backgroundRen = vtk.vtkRenderer()
    backgroundRen.SetLayer(0)
    backgroundRen.InteractiveOff()
    backgroundRen.SetBackground(1, 1, 1)
    # Create background object for showing video feed.
    bgCamera, backgroundActor = _makeImagePlane(bgImage)
    # Setup camera
    backgroundRen.AddActor(backgroundActor)
    backgroundRen.SetActiveCamera(bgCamera)
    # Add background renderer to window
    renWin.AddRenderer(backgroundRen)

    # Create background renderer for static object
    foregroundRen = vtk.vtkRenderer()
    foregroundRen.SetLayer(2)
    foregroundRen.InteractiveOff()
    # Create background object for showing video feed.
    fgCamera, foregroundActor = _makeImagePlane(fgImage)
    # Setup camera
    foregroundRen.AddActor(foregroundActor)
    foregroundRen.SetActiveCamera(fgCamera)
    # Add background renderer to window
    renWin.AddRenderer(foregroundRen)

    # Way of getting camera: renWin.GetRenderers().GetFirstRenderer()
    return ren, backgroundRen, foregroundRen

class vtkTimerCallback():
   # Callback that renders on a timer
    def __init__(self, renWin, parent=None):
        self.renderWindows = [renWin]
        self.parent = parent
    def execute(self,obj,event):
        # for renWin in self.renderWindows:
        #     if self.parent is None:
        #         renWin.Render()
        #     else:
        #         if self.parent.isVisible():
        #             renWin.Render()
        self.update()
    def addRenWin(self,renWin):
        self.renderWindows.append(renWin)
    def update(self):
        pass

def matrixFromCamInfo(camInfo):
    # Get intrinsic matrix of rectified image
    intrinsicMatrix = np.reshape(camInfo.P,(3,4))[0:3,0:3]
    # Get camera extrinsic matrix
    extrinsicMatrix = np.identity(4)
    # Get extrinsic rotation
    extrinsicMatrix [0:3,0:3] = np.reshape(camInfo.R,(3,3))
    # Get baseline translation (will usually be zero as this is for left camera)
    xBaseline = camInfo.P[3] / -camInfo.P[0]
    yBaseline = camInfo.P[7] / -camInfo.P[5]
    extrinsicMatrix [0:3,3] = [xBaseline, yBaseline, 0]

    return intrinsicMatrix, extrinsicMatrix

class zBuff:
    def __init__(self,renWin):
        self.renWin = renWin
        self.zBuffFilter = vtk.vtkWindowToImageFilter()
        self.zBuffFilter.SetInput(self.renWin)
        self.zBuffFilter.SetMagnification(1)
        self.zBuffFilter.SetInputBufferTypeToZBuffer()

        self.scaledZBuff = vtk.vtkImageShiftScale()
        self.scaledZBuff.SetOutputScalarTypeToUnsignedChar()
        self.scaledZBuff.SetInputConnection(self.zBuffFilter.GetOutputPort())
        self.scaledZBuff.SetShift(0)
        self.scaledZBuff.SetScale(-255)

    def GetOutput(self):
        # self.renWin.Render()
        self.zBuffFilter.Modified()
        self.scaledZBuff.Update()
        return self.scaledZBuff.GetOutput()

# Loads an image and returns a plane object and a camera pointed towards it
def _makeImagePlane(imageData) :
    ''' Takes a vtkImageData object and
        returns a plane textured with that image
        and a camera for the background renderer
        http://www.vtk.org/Wiki/VTK/Examples/Cxx/Images/BackgroundImage
        was the basis for this function
    '''
    imageActor = vtk.vtkImageActor()

    if vtk.VTK_MAJOR_VERSION <= 5:
        imageActor.SetInput(imageData)
    else:
        imageActor.SetInputData(imageData)

    origin = imageData.GetOrigin()
    spacing = imageData.GetSpacing()
    extent = imageData.GetExtent()

    camera = vtk.vtkCamera()
    camera.ParallelProjectionOn()

    xc = origin[0] + 0.5*(extent[0] + extent[1])*spacing[0]
    yc = origin[1] + 0.5*(extent[2] + extent[3])*spacing[1]
    yd = (extent[3] - extent[2] + 1)*spacing[1]
    d = camera.GetDistance()
    camera.SetParallelScale(0.5*yd)
    camera.SetFocalPoint(xc,yc,0.0)
    camera.SetPosition(xc,yc,d)

    return camera, imageActor

def setCameraExtrinsic(camera,matrix):
    R = matrix[0:3,0:3]
    up = np.dot(R,[0,-1,0])
    forward = np.dot(R,[0,0,1])
    camera.SetViewUp(up)
    camera.SetFocalPoint(forward)
    # eye = (-R'T), center = eye + R'(0,0,1)', up = -R[0][1]
    return

# Sets up a camera based on a camera intrinsics matrix from OpenCV
def cameraFromMatrix(camMatrix, imageDims, windowDims) :
    ''' Takes a camera matrix derived from OpenCV
        returns a camera for registration
        http://stackoverflow.com/questions/25539898/how-to-apply-the-camera-pose-transformation-computed-using-epnp-to-the-vtk-camer
        was a great help
        Input:
            camMatrix (np.ndarray) = 4x4 camera intrinsic matrix
            imageDims ((float, float))  = width and height of image to register
            imageDims ((float, float))  = width and height of render window
        Output:
            camera (vtk.vtkCamera) = camera to use for registration
    '''
    # Initialize camera
    camera = vtk.vtkCamera()

    # Set view angle
    focalLengthY = camMatrix[1,1]
    if( windowDims[1] != imageDims[1] ):
        factor = float(windowDims[1])/float(imageDims[1])
        focalLengthY = camMatrix[1,1] * factor
    viewAngle = np.arctan((windowDims[1]/2.0)/focalLengthY)*360/np.pi
    camera.SetViewAngle(viewAngle)

    #Set window center
    py = 0
    height = 0
    if imageDims[0] != windowDims[0] or imageDims[1] != windowDims[1] :
        factor = float(windowDims[1])/float(imageDims[1])
        px = factor * camMatrix[0,2]
        width = windowDims[0]
        expectedWindowSize = np.round(factor * float(imageDims[0]))
        if( expectedWindowSize != windowDims[0] ):
            diffX = (windowDims[0] - expectedWindowSize) / 2
            px = px + diffX
        py = factor * camMatrix[1,2]
        height = windowDims[1]
    else :
        px = camMatrix[0,2]
        width = imageDims[0]
        py = camMatrix[1,2]
        height = imageDims[1]
    cx = width - px
    cy = py
    windowCenter = [0,0]
    windowCenter[0] = cx / ( (width)/2 ) - 1
    windowCenter[1] = cy / ( (height)/2 ) - 1
    camera.SetWindowCenter(windowCenter[0],windowCenter[1])

    # Set camera to look forward from center
    camera.SetPosition(0,0,0)
    camera.SetViewUp([0, -1, 0])
    camera.SetFocalPoint(0,0,1)

    return camera


# -*- coding: utf-8 -*-
"""
Created on Wed Feb 13 22:58:09 2019

@author: YASH SAINI
"""

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import confusion_matrix

# Importing the dataset
dataset = pd.read_csv('Churn_Modelling.csv')
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values

# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])

#dummy Variable
onehotencoder = OneHotEncoder(categorical_features = [1])
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 1:]

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

import keras
from keras.models import Sequential
from keras.layers import Dense
clf=Sequential()
''' units for no of nodes in hidden layer , avg(input nodes +output),
     input_dim= no of input nodes
     activation= rectifier for hidden layer and sigmoid for output layer
'''
''' First input layer and hidden layer '''
clf.add(Dense(units=6,kernel_initializer='uniform',activation='relu',input_dim=11))
# Second hidden layer
clf.add(Dense(units=6,kernel_initializer='uniform',activation='relu'))
#output layer
clf.add(Dense(units=1,kernel_initializer='uniform',activation='sigmoid'))

#compiling ANN
clf.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])#adam for stochastic gradient descent

#Fit data
clf.fit(X_train,y_train, batch_size=10,epochs=100)
#Predict data
y_pred = clf.predict(X_test)
y_pred = (y_pred > 0.5)

#Print the confusion matrix
cm=confusion_matrix(y_test,y_pred)
acc=(cm[0][0]+cm[1][1] )/(sum(cm[0])+sum(cm[1]))

print ("Confusion Matrix :\n" , cm)
print ("Accuracy: ",acc)


import logging

def logger(exception, mode):
    def decorator(func):
        def wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except exception as e:
                level = logging.ERROR

                if mode == "console":
                    logging.basicConfig(
                        level=level)
                elif mode == "file":
                    logging.basicConfig(
                        filename="error.log",
                        level=level)

                logging.error(e)

        return wrapper

    return decorator


from nltk.corpus.reader.api import CorpusReader
from nltk.corpus.reader.api import CategorizedCorpusReader
import os
import codecs

CAT_PATTERN = r'([a-z_\s]+)/.*'
DOC_PATTERN = r'(?!\.)[a-z_\s]+/[a-f0-9]+\.json'
TAGS = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'p', 'li']

class HTMLCorpusReader(CategorizedCorpusReader, CorpusReader):
  """
    A corpus reader for raw HTML documents to enable preprocessing
  """

  def __init__(self, root, fileids=DOC_PATTERN, encoding='utf8',
                 tags=TAGS, **kwargs):
        """
        Initialize the corpus reader.  Categorization arguments
        (``cat_pattern``, ``cat_map``, and ``cat_file``) are passed to
        the ``CategorizedCorpusReader`` constructor.  The remaining
        arguments are passed to the ``CorpusReader`` constructor.
        """

        # Add the default category pattern if not passed into the class.
        if not any(key.startswith('cat_') for key in kwargs.keys()):
            kwargs['cat_pattern'] = CAT_PATTERN

        CategorizedCorpusReader.__init__(self, kwargs)
        # takes in generic keyword arguments and the CorpusReader will be initialied with the root directory for the copus.
        CorpusReader.__init__(self, root, fileids, encoding)

        self.tags = tags

  def resolve(self, fileids, categories):
    """
    Returns a list of fileids or categories depending on what is passed
    to each internal corpus reader function. Implemented similarly to
    the NLTK ``CategorizedPlaintextCorpusReader``
    """
    if fileids is not None and categories is not None:
      raise ValueError("Specify fileids or categories, not both")

    # it will use CorpusReader method to compute the fileids associated witht the specific categories
    if categories is not None:
      return self.fileids(categories)

    return fileids

  def docs(self, fileids=None, categories=None):
    """
    Returns the complete text of an HTML document, closing the document
    after we are done reading it and yielding it in a memory safe fashion.
    """

    # Resolve the fileids and the categories
    fileids = self.resolve(fileids, categories)

    # create a generator, loading one document into memory at a time.
    for path, encoding in self.abspaths(fileids, include_encoding=True):
      with codecs.open(path, 'r', encoding=encoding) as f:
        yield f.read()

  # monitoring system for ingestion and preprocessing
  def sizes(self, fileids=None, categories=None):
    """
    Returns a list of tuples, the fileid and size on disk of the file.
    This function is used to detect oddly large files in the corpus.
    """
    # Resolve the fileids and the categories
    fileids = self.resolve(fileids, categories)

    # Create a generator, getting every path and computig filesize
    for path in self.abspaths(fileids):
      yield os.path.getsize(path)


# -*- coding: utf-8 -*-
from zope import interface
from zope import schema

from raptus.securelogin import SecureLoginMessageFactory as _

EMAIL = object()
SMS = object()

class InvalidEmailAddress(schema.ValidationError):
    __doc__ = _('error_pattern', default=u'Email pattern has to contain «%(number)s»')

def validateEmail(value):
    if not '%(number)s' in value:
        raise InvalidEmailAddress(value)
    return True

class IConfiguration(interface.Interface):

    ip_bypass = schema.List(
        title=_(u'IP'),
        description=_('description_ip_bypass', default=u'IPs for which to bypass the secure login procedure.'),
        required=False,
        value_type=schema.TextLine()
    )

    groups = schema.Set(
        title=_(u'Groups'),
        description=_('description_groups', default=u'Groups for which to enforce the secure login procedure.'),
        value_type=schema.Choice(
            source='plone.app.vocabularies.Groups',
        ),
        required=False
    )

    email = schema.TextLine(
        title=_(u'Email pattern'),
        description=_('description_email', default=u'The email address (has to contain «%(number)s», which will '
                      'be replaced by the mobile phone number of the user) to send '
                      'the security token to. If omitted the security token will be '
                      'sent to the email address of the user.'),
        required=False,
        constraint=validateEmail
    )

    timeout = schema.Int(
        title=_(u'Validity'),
        description=_('description_timeout', default=u'Number of minutes a security token is valid.'),
        default=5,
        required=True
    )

    token = schema.Int(
        title=_(u'Token length'),
        description=_('description_token', default=u'Length of the generated security token.'),
        default=8,
        required=True
    )

class ISecure(interface.Interface):

    def enabled(username):
        """ Whether secure login is enabled for the provided user
        """

    def has_token(username):
        """ Whether the provided user already received a token
        """

    def check(username, token):
        """ Whether the given token is correct for the provided user
        """

    def send(username):
        """ Send a new security token to the provided user
        """


#!/usr/bin/env python3
# Soubor:  kameny.py
# Datum:   06.11.2018 10:01
# Autor:   Marek Nožka, nozka <@t> spseol <d.t> cz
# Licence: GNU/GPL
############################################################################
import pyglet
import random
from math import sin, cos, radians, pi
import glob

from pyglet.window.key import LEFT, RIGHT, UP, DOWN, LCTRL

# from pyglet.window.mouse import LEFT as MouseLEFT

window = pyglet.window.Window(width=1200, height=950)
batch = pyglet.graphics.Batch()  # pro optimalizované vyreslování objektů
bg_batch = pyglet.graphics.Batch()  # pro optimalizované vyreslování objektů

class SpaceObject:
    def __init__(self, img_file, x=None, y=None):

        # načtu obrázek
        self.image = pyglet.image.load(img_file)
        # střed otáčení dám na střed obrázku
        self.image.anchor_x = self.image.width // 2
        self.image.anchor_y = self.image.height // 2
        # z obrázku vytvořím sprite
        self.sprite = pyglet.sprite.Sprite(self.image, batch=batch)

        # pokud není atribut zadán vytvořím ho náhodně
        self._x = x if x is not None else random.randint(0, window.width)
        self._y = y if y is not None else random.randint(0, window.height)
        # musím správně nastavit polohu sprite
        self.x = self._x
        self.y = self._y

        self.max = max(self.image.height, self.image.width)
        self.min = min(self.image.height, self.image.width)

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, new):
        self._x = self.sprite.x = new

    @property
    def y(self):
        return self._y

    @y.setter
    def y(self, new):
        self._y = self.sprite.y = new

class Meteor(SpaceObject):
    def __init__(
        self,
        img_file=None,
        x=None,
        y=None,
        direction=None,
        speed=None,
        rspeed=None,
    ):
        if img_file is None:
            img_file = random.choice(glob.glob("img/meteor*.png"))
        super().__init__(img_file, x, y=window.height + 20)

        self.direction = (
            direction if direction is not None else random.randint(150, 220)
        )
        self.speed = speed if speed is not None else random.randint(100, 300)
        self.rspeed = rspeed if rspeed is not None else random.randint(-50, 50)

    def tick(self, dt):
        # do promenne dt se uloží doba od posledního tiknutí
        self.x += dt * self.speed * cos(pi / 2 - radians(self.direction))
        self.sprite.x = self.x
        self.y += dt * self.speed * sin(pi / 2 - radians(self.direction))
        self.sprite.y = self.y
        self.sprite.rotation += dt * self.rspeed

    def __del__(self):
        print("Mem free!")

class Laser(SpaceObject):
    def __init__(self, img_file="img/laser.png", speed=1000):

        # načtu obrázek
        self.image = pyglet.image.load(img_file)
        # střed otáčení dám na střed obrázku
        self.image.anchor_x = self.image.width // 2
        self.image.anchor_y = self.image.height
        # z obrázku vytvořím sprite
        self.sprite = pyglet.sprite.Sprite(self.image, batch=batch)
        self.sprite.z = 0
        self.direction = 0
        self.speed = speed

        self.x = ship.x
        self.y = ship.y + 70

    def tick(self, dt):
        self.y += dt * self.speed

class SpaceShip(SpaceObject):
    def __init__(self, x=None, y=None, speed=300):

        self.keys = set()

        self.fire1 = pyglet.image.load("img/fire07.png")
        self.fire3 = pyglet.image.load("img/fire12.png")
        self.fire1.anchor_x = self.fire1.width // 2
        self.fire1.anchor_y = self.fire1.height
        self.fire3.anchor_x = self.fire3.width // 2
        self.fire3.anchor_y = 0
        self.fireL = pyglet.sprite.Sprite(self.fire1, batch=batch)
        self.fireR = pyglet.sprite.Sprite(self.fire1, batch=batch)
        self.fireFL = pyglet.sprite.Sprite(self.fire3, batch=batch)
        self.fireFR = pyglet.sprite.Sprite(self.fire3, batch=batch)

        # vzdálenost ohně motorů od lodi...
        self.fire_x_dist = 30
        self.fire_y_dist = 39

        super().__init__("img/ship.png", x=window.width / 2, y=77)

        self.speed = speed
        self.width = self.image.width
        self.height = self.image.height

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, new):
        self._x = self.sprite.x = new
        self.fireL.x = self.x - self.fire_x_dist
        self.fireR.x = self.x + self.fire_x_dist
        self.fireFL.x = self.x - 12
        self.fireFR.x = self.x + 12

    @property
    def y(self):
        return self._y

    @y.setter
    def y(self, new):
        self._y = self.sprite.y = new
        self.fireL.y = self.y - self.fire_y_dist
        self.fireR.y = self.y - self.fire_y_dist
        self.fireFL.y = self.y + 20
        self.fireFR.y = self.y + 20
        self.fireL.x = self.x - self.fire_x_dist
        self.fireR.x = self.x + self.fire_x_dist

    def tick(self, dt):
        if LEFT in self.keys and self.x > 0:
            self.fire_x_dist = 26
            self.fire_y_dist = 31
            self.x -= dt * self.speed
            self.sprite.rotation = -5
            self.sprite.scale_x = 0.85
        elif RIGHT in self.keys and self.x < window.width:
            self.fire_x_dist = 26
            self.fire_y_dist = 31
            self.x += dt * self.speed
            self.sprite.rotation = 5
            self.sprite.scale_x = 0.85
        else:
            self.sprite.rotation = 0
            self.sprite.scale_x = 1
            self.fire_x_dist = 30
            self.fire_y_dist = 39
        if DOWN in self.keys and self.y > self.height / 2:
            self.y -= dt * self.speed
            self.fireFL.visible = 1
            self.fireFR.visible = 1
        elif UP in self.keys and self.y < window.height - self.height:
            self.y += dt * self.speed
            self.fireL.visible = 1
            self.fireR.visible = 1
        else:
            self.fireL.visible = 0
            self.fireR.visible = 0
            self.fireFL.visible = 0
            self.fireFR.visible = 0

class Meet:
    meteors = list()
    lasers = list()

    def add_meteor(self, dt=None):
        self.meteors.append(Meteor())

    def add_laser(self, dt=None):
        self.lasers.append(Laser())

    def tick(self, dt):
        # pohnu kamenama
        for meteor in self.meteors:
            meteor.tick(dt)
            # vymažu ty, co nejsou vidět
            if (
                meteor.y < 0 - meteor.max
                or meteor.x < 0 - meteor.max
                or meteor.x > window.width + meteor.max
            ):
                meteor.sprite.delete()
                self.meteors.remove(meteor)
            distance = (
                (meteor.x - ship.x) ** 2 + (meteor.y - ship.y) ** 2
            ) ** 0.5
            if distance - meteor.min / 2 - 32 <= 0:
                self.remove = meteor  # schovám si ho abych ho pak vymazal
                self.hit()
                continue
            for laser in self.lasers:
                if (
                    laser.y >= meteor.y
                    and laser.x >= (meteor.x - meteor.min / 3)
                    and laser.x <= (meteor.x + meteor.min / 3)
                ):
                    self.lasers.remove(laser)
                    self.meteors.remove(meteor)
        # pohnu laserama
        for laser in self.lasers:
            laser.tick(dt)
            if laser.y > window.height + laser.sprite.height:
                laser.sprite.delete()
                self.lasers.remove(laser)

    def hit(self):
        pyglet.clock.unschedule(ticktack)
        pyglet.clock.unschedule(meet.add_meteor)
        pyglet.clock.schedule_once(self.renew, 3)

    def renew(self, dt):
        self.remove.sprite.delete()
        self.meteors.remove(self.remove)
        pyglet.clock.schedule_interval(ticktack, 1 / 30)
        pyglet.clock.schedule_interval(meet.add_meteor, 1 / 3)

@window.event
def on_draw():
    window.clear()
    bg_batch.draw()
    batch.draw()

@window.event
def on_key_press(sym, mod):
    # print(sym, mod)
    if sym == LCTRL:
        meet.add_laser()
    else:
        ship.keys |= {
            sym,
        }

@window.event
def on_key_release(sym, mod):
    ship.keys -= {
        sym,
    }

def ticktack(dt):
    meet.tick(dt)
    ship.tick(dt)

# pozadí...
bg = pyglet.image.load("img/bg.img")
x = 0
bg_sprites = ()
while x < window.width:
    y = 0
    while y < window.height:
        bg_sprites += (pyglet.sprite.Sprite(bg, x=x, y=y, batch=bg_batch),)
        y += bg.height
    x += bg.width

ship = SpaceShip()
# print(ship.width)
# print(ship.height)
meet = Meet()
meet.add_meteor()
meet.add_meteor()
meet.add_meteor()
meet.add_meteor()
meet.add_meteor()
pyglet.clock.schedule_interval(ticktack, 1 / 30)
pyglet.clock.schedule_interval(meet.add_meteor, 10 / 3)
pyglet.app.run()


import requests
import ast

class requestHandler():

  def __init__(self):
    self.URL = ''

  def insertURL(self, myURL):
    self.URL = myURL
    if myURL == 'casa':
      self.URL = 'http://127.0.0.1:5000'
    elif myURL == 'taverna':
      self.URL = 'http://jack96bjo.pythonanywhere.com/server_bisca'

  def sendRequest(self, myDict):
    # sending get request and saving the response as response object
    try:
      r = requests.get(url = self.URL, params = myDict)
      #r = requests.post(url = self.URL, data = myDict)
    except:
      print('Error in sending request')
      return -1

    print('Risposta: ', r.content)
    data = r.content
    print(data)
    data = data.decode("UTF-8")
    print(data)
    data = ast.literal_eval(data)
    print(data)
    print(type(data))
    return(data)
    '''
    try:
      # extracting data in json format
      data = r.content
      data = data.decode("UTF-8")
      data = ast.literal_eval(data)
      return(data)
    except:
      print('Error in opening receive data')
      return -1
    '''


from django.urls import path

from . import views

app_name = 'tag'

urlpatterns = [
    path('tag_list/', views.TagListView.as_view(),
         name='tag-list'),
    path('tag_create/', views.TagCreateView.as_view(),
         name='tag-create'),
    path('tag_delete/<tag_id>', views.TagDeleteView.as_view(),
         name='tag-delete'),
    path('games_by_tag/<tag_id>', views.GamesByTagView.as_view(),
         name='games-by-tag')
]


def displayData(day, month, year, sex):  # wyswietla date urodzenia i plec
    from datetime import datetime

    dateOfBirth = str(day) + str(month) + str(year)
    dateOfBirth = datetime.strptime(dateOfBirth, '%d%m%Y').strftime('%d-%m-%Y')

    displayedData = dateOfBirth + " " + sex
    print (displayedData)

def peselToList(pesel):  # zamienia numer PESEL na liste cyfr
    peselList = [None] * 11
    pesel = str(pesel)
    for i in range(0, len(peselList)):
        peselList.__setitem__(i, pesel[i])
    return peselList

def check(peselList):  # sprawdza z uzyciem sumy kontrolnej czy numer PESEL jest poprawny
    checksumList = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3]
    checksum = 0
    for i in range(0, len(checksumList)):
        checksum = checksum + checksumList.__getitem__(i) * int(peselList.__getitem__(i))

    result = checksum % 10
    result = (10 - result) % 10

    if result is int(peselList.__getitem__(len(peselList) - 1)):
        return True
    else:
        return False

def analyze(peselList):  # odczytuje z numeru PESEL date urodzenia i plec
    year = 1900 + int(peselList.__getitem__(0)) * 10 + int(peselList.__getitem__(1))

    month = int(peselList.__getitem__(2)) * 10 + int(peselList.__getitem__(3))

    if 12 < month <= 32:
        month = month - 20
        year = year + 100

    day = int(peselList.__getitem__(4)) * 10 + int(peselList.__getitem__(5))

    sexNumber = int(peselList.__getitem__(9))
    if (sexNumber % 2) == 0:
        sex = "K"
    else:
        sex = "M"

    displayData(day, month, year, sex)

def main():
    import sys

    if len(sys.argv) is 2:

        if len(sys.argv[1]) is 11:

            try:
                pesel = int(sys.argv[1])

                print(pesel)
                peselList = peselToList(pesel)

                if check(peselList):

                    analyze(peselList)
                else:
                    print ("wrong PESEL !")

            except ValueError:
                print ('PESEL contain 11 digit')

        else:
            print ('PESEL contain 11 digit')

    else:
        print ('You should enter only PESEL !')

if __name__ == "__main__":
    main()


from pathlib import Path
from typing import Dict, Any

from ..utils import cached_property, safe_eval

class Setup:

    def __init__(self, directory: Path):
        self.__file = directory / "setup.py"

    @property
    def file(self):
        return self.__file

    def __getattr__(self, item):
        return self.kwargs.get(item)

    @cached_property
    def kwargs(self) -> Dict[str, Any]:
        if not self.file.is_file():
            return {}
        code = self.file.read_text(encoding="utf8", errors="ignore")
        code = code[code.find("setup("):]
        code = code[6:]
        code = code.lstrip().rstrip().rstrip(")").rstrip()
        # heal code
        kwargs = {}
        for line in code.splitlines():
            line = line.lstrip()
            keys = list(kwargs.keys())
            if "=" in line and line[0] not in ["'", "\""]:
                key, value = line.split("=", maxsplit=1)
                kwargs[key] = value
            elif keys:
                kwargs[keys[-1]] += line
        for key, value in kwargs.items():
            if value.endswith(","):
                value = value[:-1]
            if value is not None:
                kwargs[key] = safe_eval(value)
        return kwargs


import matplotlib.pyplot as plt
import numpy as np
import rijecinci as r
import math as m
from matplotlib.animation import FuncAnimation

class Planets:
    def __init__(self, x):
        self.v = (x["brzina"])
        self.m = (x["masa"])
        self.x = (x["polozaj"])
        self.s = (x["velicina"])
        self.label = (x["ime"])
        self.c = (x["boja"])
        self.x_y_z = [self.x]
        self.akceleracija = np.array([0,0,0])
        self.size = (x["size"])

class Universe:
    def __init__(self, x,y,z=0,j=0,k=0):
        tijela1 = [x,y,z,j,k]
        tijela2 = ["Sunce","Merkur","Venera","Zemlja","Mars"]
        self.planeti = []
        for i in range (5):
            x = tijela1[i]
            if x==tijela2[0]:
                p1 = Planets(r.sunce)
                self.planeti.append(p1)
            elif x==tijela2[1]:
                p1 = Planets(r.merkur)
                self.planeti.append(p1)
            elif x==tijela2[2]:
                p1 = Planets(r.venera)
                self.planeti.append(p1)
            elif x==tijela2[3]:
                p1 = Planets(r.zemlja)
                self.planeti.append(p1)
            elif x==tijela2[4]:
                p1 = Planets(r.mars)
                self.planeti.append(p1)

    def reset(self):
        del self.planeti

    def calculate(self):
        G = 6.67408E-11
        t = 0
        T = 5 * 365 * 24 * 3600
        dt = 36000
        while t<T:
            for x in self.planeti:
                for y in self.planeti:
                    if x!=y:
                        udaljenost = np.linalg.norm(abs(np.subtract(x.x,y.x)))**3
                        akc1 = -G * (y.m * (np.subtract(x.x,y.x))/ udaljenost)
                        for i in range(3):
                            if m.isnan(akc1[i])==True:
                                akc1[i ]= 0
                        x.akceleracija = np.add(akc1,x.akceleracija)
            for x in self.planeti:
                x.v1 = np.add(x.v,x.akceleracija*dt)
                x.x = np.add(x.x , x.v1 * dt)
                x.x_y_z.append(x.x)
            t = t + dt

    def plot(self):
        self.calculate()
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        for x in self.planeti:
            x_list = []
            y_list = []
            z_list = []
            for i in range(len(x.x_y_z)):
                polozaj = x.x_y_z[i]
                x_list.append(polozaj[0])
                y_list.append(polozaj[1])
                z_list.append(polozaj[2])
            ax.scatter(x_list[-1],y_list[-1],z_list[-1],s = x.s, c = x.c , label = x.label)
            plt.plot(x_list, y_list, z_list, c = x.c)
        ax.set_facecolor("black")
        ax.legend()
        plt.axis('off')
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_zticks([])
        max = plt.get_current_fig_manager()
        max.full_screen_toggle()       #za izlazak iz full screena stisnuti "ctrl + f" ,a za izlazak iz programa "ctrl + w"
        plt.show()

    def anima(self):
        fig = plt.figure(figsize = (5,5))
        axes = fig.add_subplot(111)
        axes.set_facecolor("black")
        x_liste = []
        y_liste = []
        for x in self.planeti:
            x_list = []
            y_list = []
            for i in range(len(x.x_y_z)):
                polozaj = x.x_y_z[i]
                x_list.append(polozaj[0])
                y_list.append(polozaj[1])
            axes.plot(x_list,y_list,c = x.c)
            for i in range(2):
                del x_list[::2]
                del y_list[::2]
            x_liste.append(x_list)
            y_liste.append(y_list)

        axes.scatter(0,0,s = 500, c = "y" , label = "Sunce")

        point, = axes.plot([x_liste[0][0]],[y_liste[0][0]], 'go')
        def ani(coords):
            point.set_data([coords[0]],[coords[1]])
            if self.planeti[0].label != "Sunce":
                point.set_label(self.planeti[0].label)
            point.set_color(self.planeti[0].c)
            point.set_marker("o")
            point.set_markersize(self.planeti[0].size)
            legend = plt.legend()
            return point,legend
        def frames():
            for acc_11_pos, acc_12_pos in zip(x_liste[0], y_liste[0]):
                yield acc_11_pos, acc_12_pos
        animation = FuncAnimation(fig, ani, frames=frames, interval=1)

        try:
            point1, = axes.plot([x_liste[1][0]],[y_liste[1][0]], 'go')
            def ani1(coords):
                point1.set_data([coords[0]],[coords[1]])
                if self.planeti[1].label != "Sunce":
                    point1.set_label(self.planeti[1].label)
                point1.set_color(self.planeti[1].c)
                point1.set_marker("o")
                point1.set_markersize(self.planeti[1].size)
                return point1
            def frames1():
                for acc_11_pos, acc_12_pos in zip(x_liste[1], y_liste[1]):
                    yield acc_11_pos, acc_12_pos
            animation1 = FuncAnimation(fig, ani1, frames=frames1, interval=1)
        except:
            pass

        try:
            point2, = axes.plot([x_liste[2][0]],[y_liste[2][0]], 'go')
            def ani2(coords):
                point2.set_data([coords[0]],[coords[1]])
                if self.planeti[2].label != "Sunce":
                    point2.set_label(self.planeti[2].label)
                point2.set_color(self.planeti[2].c)
                point2.set_marker("o")
                point2.set_markersize(self.planeti[2].size)
                return point2
            def frames2():
                for acc_11_pos, acc_12_pos in zip(x_liste[2], y_liste[2]):
                    yield acc_11_pos, acc_12_pos
            animation2 = FuncAnimation(fig, ani2, frames=frames2, interval=1)
        except:
            pass

        try:
            point3, = axes.plot([x_liste[3][0]],[y_liste[3][0]], 'go')
            def ani3(coords):
                point3.set_data([coords[0]],[coords[1]])
                if self.planeti[3].label != "Sunce":
                    point3.set_label(self.planeti[3].label)
                point3.set_color(self.planeti[3].c)
                point3.set_marker("o")
                point3.set_markersize(self.planeti[3].size)
                return point3
            def frames3():
                for acc_11_pos, acc_12_pos in zip(x_liste[3], y_liste[3]):
                    yield acc_11_pos, acc_12_pos
            animation3 = FuncAnimation(fig, ani3, frames=frames3, interval=1)
        except:
            pass

        try:
            point4, = axes.plot([x_liste[4][0]],[y_liste[4][0]], 'go')
            def ani4(coords):
                point4.set_data([coords[0]],[coords[1]])
                if self.planeti[4].label != "Sunce":
                    point4.set_label(self.planeti[4].label)
                point4.set_color(self.planeti[4].c)
                point4.set_marker("o")
                point4.set_markersize(self.planeti[4].size)
                return point4
            def frames4():
                for acc_11_pos, acc_12_pos in zip(x_liste[4], y_liste[4]):
                    yield acc_11_pos, acc_12_pos
            animation4 = FuncAnimation(fig, ani4, frames=frames4, interval=1)

        except:
            pass

        maximum = plt.get_current_fig_manager()
        maximum.full_screen_toggle()       #za izlazak iz full screena stisnuti "ctrl + f" ,a za izlazak iz programa "ctrl + w"

        plt.show()


'''
This is an example of the application of DeepESN model for next-step prediction on Mackey Glass time-series.

Reference paper for DeepESN model:
C. Gallicchio, A. Micheli, L. Pedrelli, "Deep Reservoir Computing: A Critical Experimental Analysis",
Neurocomputing, 2017, vol. 268, pp. 87-99

Reference paper for the design of DeepESN model in multivariate time-series prediction tasks:
C. Gallicchio, A. Micheli, L. Pedrelli, "Design of deep echo state networks",
Neural Networks, 2018, vol. 108, pp. 33-47

----

This file is a part of the DeepESN Python Library (DeepESNpy)

Luca Pedrelli
luca.pedrelli@di.unipi.it
lucapedrelli@gmail.com

Department of Computer Science - University of Pisa (Italy)
Computational Intelligence & Machine Learning (CIML) Group
http://www.di.unipi.it/groups/ciml/

----
'''

import numpy as np
import pandas as pd
import random
from DeepESN import DeepESN
from scipy.io import loadmat, savemat
from utils import MSE, config_UWOC, load_UWOC, select_indexes, MAPE
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import time
import itertools

class Struct(object): pass

def saveOutputResults(predict):
    newArr = np.column_stack((predict))
    predict = pd.DataFrame(newArr)

    output_filepath = 'Echo_State_Network_DeepESN\Results\_results_DeepESN.xlsx'
    #fitness_filepath = 'NN_output data\_fitness_results_'+file +'_'+activeFunc+'.xlsx'
    predict.to_excel(output_filepath, index = False)
    #fitness.to_excel(fitness_filepath, index = False)
# sistemare indici per IP in config_pianomidi, mettere da un'altra parte
# sistema selezione indici con transiente messi all'interno della rete

def plotResults(predict):
    plt.plot(predict, label="results")
    plt.legend(loc='upper right')
    plt.xlim([0,100])
    plt.show()

def main():

    # fix a seed for the reproducibility of results
    np.random.seed(7)
    path = ''
    dataset, Nu, error_function, optimization_problem, TR_indexes, VL_indexes, TS_indexes, scaler, data_set_name = load_UWOC(path, MSE)

    # load configuration for UWOC task
    config = config_UWOC(list(TR_indexes) + list(VL_indexes))
    # Be careful with memory usage dummy
    recc_units = 10 # number of recurrent units
    recc_layer = 17 # number of recurrent layers

    reg = 0
    transient = 100
    deepESN = DeepESN(Nu, recc_units, recc_layer, config)

    states = deepESN.computeState(dataset.inputs, deepESN.IPconf.DeepIP)

    train_states = select_indexes(states, list(TR_indexes) + list(VL_indexes), transient)
    train_targets = select_indexes(dataset.targets, list(TR_indexes) + list(VL_indexes), transient)

    test_targets = select_indexes(dataset.targets, TS_indexes)
    start_time_tr = time.time()
    deepESN.trainReadout(train_states, train_targets, reg) #train the network
    end_time_tr = time.time() - start_time_tr
    print('Training Time:', end_time_tr)

    train_outputs = deepESN.computeOutput(train_states)

    train_error = error_function(X = train_outputs,Y = train_targets)
    print('Training MSE: ', np.mean(train_error), '\n')

    start_time = time.time()
    test_states = select_indexes(states, TS_indexes)
    test_outputs = deepESN.computeOutput(test_states)
    end_time = time.time() - start_time

    test_error = error_function(X = test_outputs, Y = test_targets)
    print('Test MSE: ', np.mean(test_error), '\n')
    print("Time to put unseen data through : %s seconds" % (end_time))

    test_targets = np.array(test_targets)
    test_targets = test_targets.flatten()

    test_states = np.array(test_states)
    test_states = test_states.flatten()

    from scipy.io import savemat, loadmat
    #test_outputs
    #test_targets
    myData = loadmat(data_set_name)
    orig_Rx = myData['PAMsymRx']#.reshape((1,-1))#Mat']#.reshape((1,-1))
    orig_Tx = myData['PAMsymTx']#.reshape((1,-1))#Mat']#.reshape((1,-1))

    equalized = test_outputs.reshape(-1,1)
    tr = 20000
    Tx = orig_Tx[tr:240000].reshape(-1,1)
    Input = orig_Rx[tr:240000].reshape(-1,1)

    equalized = scaler.inverse_transform(equalized)

    mdic = {"Rx": equalized, "Tx": Tx ,"input":Input}
    savemat("equalizedOutput.mat", mdic)

    from sklearn.metrics import classification_report
    from sklearn.metrics import confusion_matrix
    import scikitplot as skplt
    import pandas as pd
    import seaborn as sns

    plt.plot(Tx.flatten(), 'r-o', markersize = 4, label="Tx")
    plt.plot(equalized.flatten(), 'b-o', markersize = 4, label="Equalized Rx")
    plt.plot(Input.flatten(), 'g-o', markersize = 4, label="Rx")
    plt.legend(loc='upper right')
    plt.xlabel("Number of Symbol")
    plt.ylabel("Symbol Status")
    plt.xlim([0,50])
    plt.grid()
    #plt.show()

    #print("Layers:", Nl)
    #back_to_original(test_outputs_classified)

    # test_targets_orig = back_to_original(test_targets)
    # class_names = ["8191", "2730","-2730","-8191"]
    # label_numbers = [8191,2730,-2730,-8191]
    #class_names = ["0", "1"]
    #label_numbers = [0,1]

    #print(classification_report)

    #report = classification_report(test_targets, test_outputs_classified, digits = 5, target_names=class_names, output_dict = False)

    #print(report)
    #sns.heatmap(pd.DataFrame(report).iloc[:-1, :].T, annot=True, linewidths=.5)
    #plot_classification_report(report)

    # print("Confusion Matrix")
    # print(print(confusion_matrix(test_targets, test_outputs_classified, labels = label_numbers)))

    # skplt.metrics.plot_confusion_matrix(test_targets, test_outputs_classified, normalize = False, cmap=plt.cm.Blues)

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 30 09:14:31 2021

@author: Rishi Sreedhar ( https://orcid.org/0000-0002-7648-4908 )
"""
"""

This code is for Calculating the P = 1 Landscape of MaxCut problems
that are defined using adjacency matrices C.
This code is used to generate the data displayed in Figure 7, of Section V:
QAOA TRAINING WITH LOW ENTANGLEMENT IN MPS REPRESENTATIONS.

Input Needed::

    1.) C: The adjacency matrix describing the MaxCut problem instance.
    2.) Instance_list: The list of instances to be studied.
    3.) N: The number of grid points along each gamma or beta axis.
    4.) DList: The list of different bond-dimensions to be studied

Output Generated::

    1.) Cost_mps: An [N x N] matrix storing the cost values corresponding to each (gamma,beta) for each Dmax studied.
                  This data is saved as a .npy file in the respective instance folders.

"""

import os
import tensornetwork as tn
from Gates import Gates as g
from Expectation import Expectation as exp
import numpy as np

tn.set_default_backend("numpy")

#%%

##############################
## Parameter Initialization ##
##############################

q = 12 # number of Qubits

l1 = int(np.floor((q)/2))   # Paramteres required for the SWAP network
l2 = int(np.floor((q-1)/2)) # Paramteres required for the SWAP network

N = 100 # Number of sampling points in Gamma and Beta
pi = np.pi

Gamma = [0.0 + x*pi/(N-1) for x in range(N)] # List of 100 Gamma angles between [0 , pi]
Beta = [0.0 + x*0.50*pi/(N-1) for x in range(N)]  # List of 100 Beta angles between [0 , pi/2]

G,B = np.meshgrid(Beta,Gamma) # Creating a mesh for the 3D plots

DList = [64, 48, 32, 24, 16, 12, 8, 6, 4, 2] # List of Bond-dimensions to be simulated

folder_location = os.path.dirname(os.path.realpath(__file__)) # Location of the MPS_QAOA Folder
local_location = '/MxC_Q'+str(q)+'/Q' #Location of files within the MPS_QAOA Folder
location = folder_location + local_location

tag = 'R' # R for Random

D_highest = int(2**np.floor(q/2)) # Highest possible bond-dimension for a q-qubit MPS

Normalize = False # Normalization constraint for the MPS-QAOA states. IMPORTANT: Keep False

Instance_list = [0] # The indices of the instances one wishes to run the calculations for.
                    # Eg: If q = 12, enter [0,42] if the instances of interest are 12R0 and 12R42

#%%

def get_Cost_mpsIX(gamma_beta):

    Cost = 0

    z_i = g.get_Z()
    z_j = g.get_Z()

    for i in range(n-1):

        for j in range((n-1),i,-1):

            if (C[i,j] != 0):

                g_b = gamma_beta.tensors
                GB = tn.FiniteMPS(g_b, canonicalize = False)
                GB_copy = tn.FiniteMPS(g_b, canonicalize = False)

                GB.apply_one_site_gate(z_i,i)

                GB.apply_one_site_gate(z_j,j)

                ci = exp.exp_MPS(GB_copy,GB)
                ci = np.real(ci)

                Cost = Cost - 0.5*C[i,j]*(1 - ci)

    return Cost

#%%

def QAOA_gamma_block(gamma_beta, gamma, C, Dmax):

    '''

    Function that takes in the parameter gamma and applies
    a single layer of the gamma block within a single QAOA layer.

    Input::

        gamma_beta  : An existing QAOA state upon which one applies the Gamma layer
        gamma       : The free parameter that needs to be optimized
        C           : The adjacency matrix describing the MaxCut problem instance.
        Dmax        : The maximum bond-dimension limit imposed on the MPSs

    Output::

        gamma_beta  : The QAOA state with an additional Cost layer of the QAOA added to it

    '''

    gamma_beta = tn.FiniteMPS(gamma_beta.tensors, canonicalize=False)
    gamma_beta.canonicalize(normalize=Normalize)

    # Defining the SWAP network
    Q_ord = np.linspace(start = 0, stop = (n-1), num = n, dtype = int)

    SWAP = g.get_SWAP()

    for i in range(n): #applying all the nearest neighbout gates

        if (i < (n-1)):

            for k in range(n-1):

                if (Q_ord[k] < Q_ord[k+1]):

                    Cij = g.get_Cij(gamma, C[Q_ord[k]][Q_ord[k+1]])

                    gamma_beta.position(site=k, normalize=Normalize)

                    if (Dmax == D_highest):

                        gamma_beta.apply_two_site_gate(Cij, site1 = k,
                                                       site2 = (k+1), center_position=k)

                    else:

                        gamma_beta.apply_two_site_gate(Cij, site1 = k, site2 = (k+1),
                                                   max_singular_values=Dmax, center_position=k)

        if (i%2 == 0): #Doing the even round of SWAPs from the SWAP network

            for s in range(l1):

                Q_ord[2*s],Q_ord[2*s+1] = Q_ord[2*s+1],Q_ord[2*s]

                gamma_beta.position(site=(2*s), normalize=Normalize)

                if (Dmax == D_highest):

                    gamma_beta.apply_two_site_gate(SWAP, site1 = (2*s),
                                                   site2 = (2*s+1), center_position=(2*s))

                else:

                    gamma_beta.apply_two_site_gate(SWAP, site1 = (2*s), site2 = (2*s+1),
                                                   max_singular_values=Dmax, center_position=(2*s))

        else:  #Doing the odd round of SWAPs from the SWAP network

            for s in range(l2):

                Q_ord[2*s+1],Q_ord[2*s+2] = Q_ord[2*s+2],Q_ord[2*s+1]

                gamma_beta.position(site=(2*s+1), normalize=Normalize)

                if (Dmax == D_highest):

                    gamma_beta.apply_two_site_gate(SWAP, site1 = (2*s+1),
                                                   site2 = (2*s+2), center_position=(2*s+1))

                else:

                    gamma_beta.apply_two_site_gate(SWAP,site1 = (2*s+1), site2 = (2*s+2),
                                                   max_singular_values=Dmax, center_position=(2*s+1))

    gamma_beta = gamma_beta.tensors[::-1]
    gamma_beta = [gamma_beta[x].transpose([2,1,0]) for x in range(n)]
    gamma_beta = tn.FiniteMPS(gamma_beta, canonicalize = False)

    return gamma_beta

#%%

def QAOA_beta_block(gamma_beta, beta):

    '''

    Function that takes in the parameter beta and applies
    a single layer of the mixing block within a single QAOA layer.

    Input::

        gamma_beta: An existing QAOA state upon which one applies the Gamma layer
        beta: The free parameter that needs to be optimized

    Output::

        gamma_beta: The QAOA state with an additional mixing layer of the QAOA added to it

    '''

    gamma_beta = tn.FiniteMPS(gamma_beta.tensors, canonicalize=False)

    Rx = g.get_Rx(2*beta)

    for i in range(n):

        gamma_beta.apply_one_site_gate(Rx, i)

    return gamma_beta

#%%

for r in Instance_list:

    C = np.load(location+str(q)+tag+str(r)+'/C_Q'+str(q)+tag+str(r)+'.npy')
    n = len(C)

    for Dmax in DList:

        Cost_mps = np.zeros([N,N])

        for i in range(N):

            print('\nQ'+str(n)+'R'+str(r)+', D = '+str(Dmax)+', i = '+str(i)+'\n')

            plus = tn.FiniteMPS([np.array([[[1/np.sqrt(2)],
                                               [1/np.sqrt(2)]]],
                                             dtype = np.complex128) for x in range(n)])

            Fail = True
            attempt = 1

            while (Fail):

                try:

                    gamma_st = QAOA_gamma_block(plus, Gamma[i], C, Dmax)

                    Fail = False

                except:

                    print('\nSVD Error!! for D = '+str(Dmax)+
                          ', and attempt = '+str(attempt)+'!\n')

                    attempt += 1
                    Gamma[i] = np.round(Gamma[i], decimals=(11-attempt))

                    if (attempt > 10):

                        print('Stopping Repetition at attempt = ',attempt,'\n')
                        raise

            for j in range(N):

                gamma_beta = QAOA_beta_block(gamma_st, Beta[j])

                Cost_mps[i,j] = get_Cost_mpsIX(gamma_beta)

                del gamma_beta

            del gamma_st

        np.save(location+str(n)+'R'+str(r)+'/N'+str(N)+'D'+str(Dmax)+'.npy',Cost_mps)


from builtins import open as _open

import collections
import os
import pathlib
import shutil
import threading
import tempfile

from .log_helpers import StyleAdapter, get_logger

LOGGER = StyleAdapter(get_logger(__name__))

lock = threading.Lock()

class Singleton(type):
    """
    Metaclass for creating singleton objects. Taken from [1]_.

    .. [1] https://stackoverflow.com/questions/50566934/why-is-this-singleton-implementation-not-thread-safe/50567397
    """
    _instances = {}

    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            with lock:
                if cls not in cls._instances:
                    cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class DeferredFileWriter(metaclass=Singleton):
    """
    A singleton class/object that is intended to prevent writing output to files
    that is invalid, due to e.g. warnings further down the pipeline.

    If this class is used to open a file for writing, a temporary file is created
    and returned instead. Once it's clear the output produced is valid the
    :meth:`~vermouth.file_writer.DeferredFileWriter.write` method can be used to
    finalize the written changes by moving them to their intended destination.
    If a file with that name already exists it is backed up according to the
    Gromacs scheme.
    """
    def __init__(self):
        self.open_files = collections.deque()
        self._tmpdir = None

    def open(self, filename, mode='r', *args, **kwargs):
        """
        If mode is either 'w' or 'a', opens and returns a handle to a temporary
        file. If mode is 'r' opens and returns a handle to the file specified.

        Once :meth:`~vermouth.file_writer.DeferredFileWriter.write` is called
        the changes written to all files opened this way are propagated to their
        final destination.

        Parameters
        ----------
        filename: os.PathLike
            The final name of the file to be opened.
        mode: str
            The mode in which the file is to be opened.
        *args: collections.abc.Iterable
            Passed to :func:`os.fdopen`.
        **kwargs: dict
            Passed to :func:`os.fdopen`.

        Returns
        -------
        io.IOBase
            An opened file
        """
        path = pathlib.Path(filename)
        # Make the path absolute, in case the current working directory is
        # changed between now and writing. Can't do path.resolve() due to py35
        # requiring the file to exist.
        path = path.parent.resolve() / path.name
        # Let's see if we already opened this file. If so, get the corresponding
        # temporary file.
        for tmp_path, open_path, _ in self.open_files:
            # Can't use Path.samefile, since the files don't have to exist yet
            if open_path == path:
                return _open(tmp_path, mode, *args, **kwargs)

        if '+' in mode or 'a' in mode or 'w' in mode:  # Append and write
            return self._open_tmp_file(path, *args, mode=mode, **kwargs)
        elif 'r' in mode:  # Read, do nothing special
            return _open(filename, mode, *args, **kwargs)
        raise KeyError('Unknown file mode.')

    def _open_tmp_file(self, filename, mode='w', *args, **kwargs):
        suffix = filename.suffix
        with lock:
            handle, tmp_path = tempfile.mkstemp(suffix=suffix, dir=self._tmpdir)
        self.open_files.append([tmp_path, filename, mode])
        if '+' in mode and 'r' in mode:
            # If r+, preserve original file contents. Otherwise, truncate.
            shutil.copy2(str(filename), tmp_path)
        return os.fdopen(handle, mode, *args, **kwargs)

    @staticmethod
    def _find_free_path(file_path):
        """
        Find the first free (backup) path that looks like `file_path`. If
        file_path does not exist, returns file_path. Else, generates
        `#{name}.{idx}#`, incrementing idx until no file exists.

        Parameters
        ----------
        file_path: pathlib.PathLike

        Returns
        -------
        pathlib.Path
            The first path that does not exist yet
        """
        file_path = pathlib.Path(file_path)
        backup_path = pathlib.Path(file_path)
        idx = 1
        while backup_path.exists():
            backup_path = file_path.with_name('#{name}.{idx}#'.format(name=file_path.name, idx=idx))
            idx += 1
        return backup_path

    def write(self):
        """
        Finalize writing all open files by moving the created temporary files to
        their final destinations.

        Existing file destinations will be backed up according to the Gromacs
        scheme.
        """
        while self.open_files:
            tmp_path, final_path, mode = self.open_files.popleft()
            if 'w' in mode or '+' in mode:  # write
                self._write_file(tmp_path, final_path)
            elif 'r' in mode:  # read = error
                raise AssertionError("Files opened with mode 'r' should not be "
                                     "treated special")
            elif 'a' in mode:  # append
                self._append_file(tmp_path, final_path, mode)
            else:
                raise KeyError('Unknown file mode')

    def _write_file(self, tmp_path, final_path):
        # There is no way to move a file and make it error if the destination
        # already exists, so use a lock instead.
        with lock:
            free_path = self._find_free_path(final_path)
            if free_path != final_path:
                LOGGER.info('Backing up {} to {}.', final_path, free_path, type='general')
                shutil.move(str(final_path), str(free_path))
            LOGGER.debug('Writing output to {}.', final_path, type='general')
            shutil.move(tmp_path, str(final_path))

    @staticmethod
    def _append_file(tmp_path, final_path, mode='a'):
        """
        Append the contents of tmp_path to final_path and remove tmp_path.
        """
        if 'b' in mode:
            tmp_mode = 'rb'
        else:
            tmp_mode = 'r'
        with _open(str(final_path), mode=mode) as final_file, _open(tmp_path, mode=tmp_mode) as tmp_file:
            final_file.write(tmp_file.read())
        os.remove(tmp_path)

    def close(self):
        """
        Remove all produced temporary files.
        """
        while self.open_files:
            tmp_path, *_ = self.open_files.popleft()
            try:
                os.remove(tmp_path)
            except FileNotFoundError:
                pass

    def __del__(self):
        self.close()
        # super().__del__()  # object has no __del__

deferred_open = DeferredFileWriter().open


import sys
import numpy as np
from pathlib import Path
from sklearn.metrics import mean_absolute_error

DTYPES = {
    'acoustic_data': np.int16,
    'time_to_failure': np.float64
}

PREF = sys.argv[0].split("/")[-1].split("_")[0]
ROOT_PATH = Path(__file__).absolute().parents[1]
INPUT_PATH = ROOT_PATH / "input"
CONFIG_PATH = ROOT_PATH / "config"
FEATURE_PATH = ROOT_PATH / "data" / "features"
VALIDATION_PATH = ROOT_PATH / "data" / "validation"
MODEL_PATH = ROOT_PATH / "data" / "model"
OOF_PATH = ROOT_PATH / "data" / "oof"
IMPORTANCE_PATH = ROOT_PATH / "importance"
SUBMIT_PATH = ROOT_PATH / "data" / "submit"

# Train Path
TRAIN_CSV_PATH = INPUT_PATH / "train.csv"
TRAIN_FEATHER_PATH = INPUT_PATH / "train.f"

# Test Path
TEST_CSV_DIRECTORY_PATH = INPUT_PATH / "test"
TEST_CSV_LIST = list(TEST_CSV_DIRECTORY_PATH.glob('**/*.csv'))
TEST_FEATHER_DIRECTORY_PATH = INPUT_PATH / "test_feather"
TEST_FEATHER_LIST = list(TEST_FEATHER_DIRECTORY_PATH.glob('**/*.f'))

# Sample Submission Path
SAMPLE_SUBMISSION_CSV_PATH = INPUT_PATH / "sample_submission.csv"
SAMPLE_SUBMISSION_FEATHER_PATH = INPUT_PATH / "sample_submission.f"

# Const
DATASET_LENGTH = 150000

def save_oof(validity):
    validity = validity.reset_index()
    columns_order = ["index", "time_to_failure", "Predict"]
    validity = validity.sort_values("index").reset_index(drop=True).loc[:, columns_order]
    cv_mae = (mean_absolute_error(validity["time_to_failure"], np.array(validity["Predict"])))
    print("!!!!! CV MAE:{}".format(cv_mae))
    Path.mkdir(OOF_PATH, exist_ok=True, parents=True)
    validity.to_csv(OOF_PATH / "{}.csv".format(sys.argv[1]))

def save_feature_importance(feature_importance):
    feature_importance["median"] = feature_importance.median(axis='columns')
    feature_importance.sort_values("median", ascending=False, inplace=True)
    Path.mkdir(IMPORTANCE_PATH, exist_ok=True, parents=True)
    feature_importance.to_csv(Path(IMPORTANCE_PATH / "{}.csv".format(sys.argv[1])))

def save_model(model, fold):
    model_path = MODEL_PATH / sys.argv[1]
    Path.mkdir(model_path, exist_ok=True, parents=True)
    model.save_model(str(model_path / "fold{}.model".format(fold)))


import json
from pathlib import Path
from typing import Optional, Tuple, Dict

import numpy as np
from PIL import Image

from common import Paths

def load_image_batches(
        path=Paths.data,
        batch_size=32,
        filter='train',
        size: Optional[Tuple[int]] = (1024, 1024, 3),
):
    all_paths = path.glob('**/*.png')
    all_paths = [Path(p) for p in all_paths if
                 filter in p.as_posix() and not p.as_posix().endswith('mask.png')]

    done = False

    while not done:
        images = []
        path_list = []
        done = True
        for p in all_paths:
            if len(images) >= batch_size:
                done = False
                break

            with Image.open(p) as im:
                image = np.array(im)
            if size is None or image.shape == size:
                images.append(image)
                path_list.append(p)
        yield np.array(images), path_list

def load_mask(mask_path=Paths.data / 'mask.png'):
    mask = np.array(Image.open(mask_path))[None, :, :, None]
    mask = mask / mask.max()
    mask = mask.astype(np.uint8)
    return mask

def load_bounding_boxes(path=Paths.validation_labels) -> Dict[str, np.ndarray]:
    with open(path, 'r') as h:
        j = json.load(h)
    return {k: np.array(j[k]) for k in sorted(j.keys())}

def crop190(images: np.ndarray) -> np.ndarray:
    return images[:, 190:-190]

def get_centered_rectangle_slice(
        tensor: np.ndarray,
        dimensions: np.ndarray,
):
    """Returns a centered slice of the tensor with given dimensions."""
    outer_lengths = tensor.shape[:len(dimensions)]
    inner_lengths = dimensions

    offsets = (np.array(outer_lengths) - np.array(inner_lengths)) // 2

    slices = []
    for offset, length in zip(offsets, inner_lengths):
        slices.append(slice(offset, offset + length))

    return tensor[tuple(slices)]

def draw_bounding_boxes(
        image: np.ndarray,
        boxes: np.ndarray,
        box_color=(0, 255, 255),
        line_thickness=2,
        x_offset=0,
        y_offset=0,
):
    t = line_thickness
    for y0, x0, ly, lx in boxes:
        x0 += x_offset
        y0 += y_offset
        image[x0:x0 + lx, y0:y0 + t] = box_color
        image[x0:x0 + lx, y0 + ly - t:y0 + ly] = box_color
        image[x0:x0 + t, y0:y0 + ly] = box_color
        image[x0 + lx:x0 + lx + t, y0:y0 + ly] = box_color

def select_bounding_boxes(
        image_path: Optional[Path],
        bounding_boxes: Dict[str, np.ndarray],
) -> np.ndarray:
    if image_path.parts[-1].endswith('B01.png'):
        key = image_path.parts[-2]
        if key in bounding_boxes:
            return bounding_boxes[key]
    return np.empty(shape=(0, 4))

def test_bounding_boxes():
    bounding_boxes = load_bounding_boxes()
    image_batches = load_image_batches(path=Paths.data, batch_size=770, filter='validation')
    images, paths = next(image_batches)
    rel_paths = [p.relative_to(Paths.data) for p in paths]
    images = crop190(images)

    for image, rel_path in zip(images, rel_paths):
        boxes = select_bounding_boxes(rel_path, bounding_boxes)
        draw_bounding_boxes(image, boxes, x_offset=-190)
        p = Paths.output / 'boxes' / rel_path
        p.parent.mkdir(parents=True, exist_ok=True)
        # Image.fromarray(image).save(p)

if __name__ == '__main__':
    test_bounding_boxes()


"""makemyday URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from register import views as registerViews
# from course import views as courseViews

urlpatterns = [
    path("admin/", admin.site.urls),
    path("register/", registerViews.register, name="register"),
    path("login", registerViews.login_request, name="login"),
    path("logout", registerViews.logout_request, name= "logout"),
    path("", include("main.urls")),
    path("course/",include('course.urls')),

]


# Standard library
import os
from argparse import ArgumentParser
from shutil import rmtree

# Third-party
import git
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.management import BaseCommand, CommandError
from django.urls import reverse

# First-party/Local
from licenses.git_utils import commit_and_push_changes, setup_local_branch
from licenses.models import LegalCode, TranslationBranch
from licenses.utils import relative_symlink, save_url_as_static_file

def list_open_translation_branches():
    """
    Return list of names of open translation branches
    """
    return list(
        TranslationBranch.objects.filter(complete=False).values_list(
            "branch_name", flat=True
        )
    )

class Command(BaseCommand):
    """
    Command to push the static files in the build directory to a specified
    branch in cc-licenses-data repository

    Arguments:
        branch_name - Branch name in cc-license-data to pull translations from
                      and publish artifacts too.
        list_branches - A list of active branches in cc-licenses-data will be
                        displayed

    If no arguments are supplied all cc-licenses-data branches are checked and
    then updated.
    """

    def add_arguments(self, parser: ArgumentParser):
        parser.add_argument(
            "-b",
            "--branch_name",
            help="Translation branch name to pull translations from and push"
            " artifacts to. Use --list_branches to see available branch names."
            " With no option, all active branches are published.",
        )
        parser.add_argument(
            "-l",
            "--list_branches",
            action="store_true",
            help="A list of active translation branches will be displayed.",
        )
        parser.add_argument(
            "--nopush",
            action="store_true",
            help="Update the local branches, but don't push upstream.",
        )
        parser.add_argument(
            "--nogit",
            action="store_true",
            help="Update the local files without any attempt to manage them in"
            " git (implies --nopush)",
        )

    def _quiet(self, *args, **kwargs):
        pass

    def run_django_distill(self):
        """Outputs static files into the output dir."""
        if not os.path.isdir(settings.STATIC_ROOT):
            e = "Static source directory does not exist, run collectstatic"
            raise CommandError(e)
        output_dir = self.output_dir
        if os.path.isdir(output_dir):
            rmtree(output_dir)
        os.makedirs(output_dir)

        self.stdout.write(f"\n{self.output_dir}")
        save_url_as_static_file(output_dir, "/status/", "status/index.html")
        tbranches = TranslationBranch.objects.filter(complete=False)
        for tbranch_id in tbranches.values_list("id", flat=True):
            save_url_as_static_file(
                output_dir,
                f"/status/{tbranch_id}/",
                f"status/{tbranch_id}.html",
            )

        legalcodes = LegalCode.objects.validgroups()
        for group in legalcodes.keys():
            self.stdout.write(f"\n{self.output_dir}")
            for legalcode in legalcodes[group]:
                # deed
                filepath, symlinks = legalcode.get_file_and_links("deed")
                save_url_as_static_file(
                    output_dir,
                    legalcode.deed_url,
                    filepath,
                )
                for symlink in symlinks:
                    relative_symlink(output_dir, filepath, symlink)
                # legalcode
                filepath, symlinks = legalcode.get_file_and_links("legalcode")
                save_url_as_static_file(
                    output_dir,
                    legalcode.license_url,
                    filepath,
                )
                for symlink in symlinks:
                    relative_symlink(output_dir, filepath, symlink)

        self.stdout.write(f"\n{self.output_dir}")
        save_url_as_static_file(
            output_dir, reverse("metadata"), "licenses/metadata.yaml"
        )

    def publish_branch(self, branch: str):
        """Workflow for publishing a single branch"""
        self.stdout.write(f"Publishing branch {branch}")
        with git.Repo(settings.TRANSLATION_REPOSITORY_DIRECTORY) as repo:
            setup_local_branch(repo, branch)
            self.run_django_distill()
            if repo.is_dirty(untracked_files=True):
                # Add any changes and new files

                commit_and_push_changes(
                    repo,
                    "Updated built HTML files",
                    self.relpath,
                    push=self.push,
                )
                if repo.is_dirty(untracked_files=True):
                    raise Exception(
                        "Something went wrong, the repo is still dirty"
                    )
            else:
                self.stdout.write(f"\n{branch} build dir is up to date.\n")

    def publish_all(self):
        """Workflow for checking branches and updating their build dir"""
        branch_list = list_open_translation_branches()
        self.stdout.write(
            f"\n\nChecking and updating build dirs for {len(branch_list)}"
            " translation branches\n\n"
        )
        for b in branch_list:
            self.publish_branch(b)

    def handle(self, *args, **options):
        self.options = options
        self.output_dir = os.path.abspath(settings.DISTILL_DIR)
        git_dir = os.path.abspath(settings.TRANSLATION_REPOSITORY_DIRECTORY)
        if not self.output_dir.startswith(git_dir):
            raise ImproperlyConfigured(
                f"In Django settings, DISTILL_DIR must be inside "
                f"TRANSLATION_REPOSITORY_DIRECTORY, "
                f"but DISTILL_DIR={self.output_dir} is outside "
                f"TRANSLATION_REPOSITORY_DIRECTORY={git_dir}."
            )

        self.relpath = os.path.relpath(self.output_dir, git_dir)
        self.push = not options["nopush"]

        if options.get("list_branches"):
            branches = list_open_translation_branches()
            self.stdout.write("\n\nWhich branch are we publishing to?\n")
            for b in branches:
                self.stdout.write(b)
        elif options.get("nogit"):
            self.run_django_distill()
        elif options.get("branch_name"):
            self.publish_branch(options["branch_name"])
        else:
            self.publish_all()