'''
This script performs a time-series performance evaluation on unseen data
for models trained on a supervised learning dataset with applied feature
scaling.

Note: This script is hard coded for the intended project that takes the
      first 9 years of the dataset for model training and the rest for
      final performance evaluation.

The script does the following:
    1. Load and apply feature scaling to supervised learning dataset
    2. Create models
    3. Predict forecast
    4. Performance evaluation
    5. Visualise results
'''

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from keras.models import Sequential
from keras.layers import Dense, LSTM

def load_dataset(file_name):
    '''
    Function:
        Read data from .csv file into returned Pandas DataFrame of Series.
    Arguments:
        file_name: Name of .csv file
    Returns:
        Pandas DataFrame of series.
    '''
    return pd.read_csv(file_name)

def split_dataset(dataset):
    '''
    Function:
        Split the dataset into its training and validation sets as well as
        its feature matrix (X) and output vector (y).
    Arguments:
        dataset: Sequence of observations as a Pandas DataFrame of Series.
    Returns:
        Pandas DataFrames of series of the feature matrix (X) and output
        vector (y) from its training and validation set, respectively.

    Note:
        This function is hard coded for a specific training-test split.
    '''
    # Split dataset into training and test set
    split = 365 * 24 * 9
    train = dataset[:split, :]    # Training set - Observations up to 31 Dec 2014
    test = dataset[split:, :]     # Test set - Observations from 1 Jan 2015 and onwards

    # Split each set into feature matrix (X) and output vector (y)
    train_X, train_y = train[:, :-1], train[:, -1]
    test_X, test_y = test[:, :-1], test[:, -1]

    return train_X, train_y, test_X, test_y

def create_mlr_model(train_X, train_y):
    '''
    Function:
        Create multi-linear regression (MLR) model and fit to training set.
    Arguments:
        train_X: Training set's feature matrix as Pandas Dataframe.
        train_y: Training set's output vector as Pandas Dataframe.
    Returns:
        sklearn LinearRegression object fitted to the training set.
    '''
    # Design MLR model
    model = LinearRegression()

    # Fit MLR model
    model.fit(train_X, train_y)
    return model

def create_svr_model(train_X, train_y):
    '''
    Function:
        Creates support vector regression (SVR) model and fit to training set.
    Arguments:
        train_X: Training set's feature matrix as Pandas Dataframe.
        train_y: Training set's output vector as Pandas Dataframe.
    Returns:
        sklearn SVR object fitted to the training set.
    '''
    # Design SVR model
    model = SVR(kernel='rbf')

    # Fit SVR model
    model.fit(train_X, train_y)
    return model

def create_lstm_model(train_X, test_X, train_y, test_y, n_neuron, n_epoch, n_batch):
    '''
    Function:
        Creates long short-term memory (LSTM) model and fit to training set.
    Arguments:
        train_X: Training set's feature matrix as Pandas Dataframe.
        train_y: Training set's output vector as Pandas Dataframe.
        valid_X: Validation set's feature matrix as Pandas Dataframe.
        valid_y: Validation set's output vector as Pandas Dataframe.
        n_neuron: Number of memory units.
        n_epoch: Number of training epochs.
        n_batch: Number of mini batch size.
    Returns:
        keras Sequential object fitted to the training set.
    '''
    # Reshape feature input into 3D [samples, timesteps, features]
    train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
    test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))

    # Design LSTM network
    model = Sequential()
    model.add(LSTM(n_neuron, input_shape=(train_X.shape[1], train_X.shape[2])))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')

    # Fit network
    model.fit(train_X, train_y, epochs=n_epoch, batch_size=n_batch, validation_data=(test_X, test_y), verbose=2, shuffle=False)
    return model

def create_model(dataset, algorithm):
    '''
    Function:
        Creates either an 'MLR', 'SVR', or 'LSTM' model object based on the
        specified 'algorithm'.
    Arguments:
        dataset: Sequence of observations as a Pandas DataFrame of Series.
        algorithm: String text of learning model's abbreviation.
    Returns:
        Either an sklearn or keras object fitted to the training set.
    '''

    # Split dataset
    train_X, train_y, test_X, test_y = split_dataset(dataset)

    # Create model based on 'algorithm' argument
    if algorithm == 'MLR':
        print("Training MLR Model.")
        model = create_mlr_model(train_X, train_y)
    elif algorithm == 'SVR':
        print("Training SVR Model.")
        model = create_svr_model(train_X, train_y)
    elif algorithm == 'LSTM':
        print("Training LSTM Model.")
        model = create_lstm_model(train_X, test_X, train_y, test_y, n_neuron=50, n_epoch=50, n_batch=72)
    else:
        print("Algorithm does not exist in this function")

    print("Model Training complete.")
    return model

def performance_test(dataset, model, scaler, input_dim):
    '''
    Function:
        Compute the predicted output vector using the 'predict' method by
        the 'model' object. Inverse transformation is applied to revert
        both predicted and observed output vectors back to its original scale.
    Arguments:
        dataset: Sequence of observations as a Pandas DataFrame of Series.
        model: Object created via 'create_model' function.
        scaler: sklearn object associated with feature scaling.
        input_dim: Input dimensions that the model object is compatible with where
            'input_dim=2' represent 2D and 'input_dim=3' represent 3D. Any other
            value will cause the function to return None values.
    Returns:
        Pandas DataFrame of the predicted and observed output vectors respectively
    '''
    # Split dataset
    train_X, train_y, test_X, test_y = split_dataset(dataset)

    # Compute predicted outputs
    if input_dim == 2:
        y_predict = model.predict(test_X)
        y_predict = y_predict.reshape((len(y_predict),1))
    elif input_dim == 3:
        y_predict = model.predict(test_X.reshape((test_X.shape[0], 1, test_X.shape[1])))
    else:
        print("Invalid Dimensions for model prediction")
        inv_y_predict, inv_y = None, None
        return inv_y_predict, inv_y

    # Invert scaling for predicted output
    inv_y_predict = np.concatenate((test_X, y_predict), axis=1)
    inv_y_predict = scaler.inverse_transform(inv_y_predict)
    inv_y_predict = inv_y_predict[:,-1]

    # Invert scaling for actual output
    inv_y = np.concatenate((test_X, test_y.reshape((len(test_y),1))), axis=1)
    inv_y = scaler.inverse_transform(inv_y)
    inv_y = inv_y[:,-1]

    return inv_y_predict, inv_y

def performance_evaluate(dataset, y_pred, y):
    '''
    Function:
        Compute performance metrics that includes:
            - mean square error
            - root mean square error
            - mean absolute error
            - mean bias error
            - R-squared
            - adjusted R-squared
    Arguments:
        dataset: Sequence of observations as a Pandas DataFrame of Series.
        y_pred: Predicted output vector as a Pandas DataFrame of Series.
        y: Observed output vector as a Pandas DataFrame of Series.
    Returns:
        List of performance metric values
    '''
    # Performance Metrics
    mbe = sum(y-y_pred)/len(y)
    mse = np.sqrt(mean_squared_error(y, y_pred))
    rmse = np.sqrt(mse)
    mae = mean_absolute_error(y, y_pred)
    r2 = r2_score(y, y_pred)
    adj_r2 = r2*((dataset.shape[0]-1)/(dataset.shape[0]-(dataset.shape[1]-1)-1))

    result = pd.DataFrame([mse, rmse, mae, mbe, r2, adj_r2]).T
    result.columns = ['MSE', 'RMSE', 'MAE', 'MBE', 'R2', 'adj R2']

    return result

# 1. Load and apply feature scaling to non-stationary supervised learning dataset
dataset = load_dataset("Case 5.2 - non stationary.csv")
scaler = MinMaxScaler(feature_range=(-1, 1))
dataset = scaler.fit_transform(dataset)

# 2. Create Models
mlr_model = create_model(dataset, 'MLR')
svr_model = create_model(dataset, 'SVR')
lstm_model = create_model(dataset, 'LSTM')

# 3. Predict forecast
y_pred_mlr, y_mlr = performance_test(dataset, mlr_model, scaler, 2)
y_pred_svr, y_svr = performance_test(dataset, svr_model, scaler, 2)
y_pred_lstm, y_lstm = performance_test(dataset, lstm_model, scaler, 3)

# 4. Evaluate performance
mlr_result = performance_evaluate(dataset, y_pred_mlr, y_mlr)
svr_result = performance_evaluate(dataset, y_pred_svr, y_svr)
lstm_result = performance_evaluate(dataset, y_pred_lstm, y_lstm)

# 5. Visualise Results
plt.plot(y_mlr, label='actual', color='blue')
plt.plot(y_pred_mlr, label='predicted (MLR)', color='red')
plt.plot(y_pred_svr, label='predicted (SVR)', color='orange')
plt.plot(y_pred_lstm, label='predicted (LSTM)', color='green')
plt.legend()
plt.show()


import tensorflow as tf
import numpy as np
import pandas as pd
import random
import sklearn
import os
import cv2
import csv
import math
import matplotlib.pyplot as plt
import pydot as pyd
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from scipy import ndimage
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Activation, Flatten, merge, Input, Lambda
from keras.layers import Convolution2D, MaxPooling2D, GlobalAveragePooling2D
from keras.layers.convolutional import Conv2D
from keras.utils import plot_model
lines = []
with open('data/driving_log.csv') as csvfile:
    reader = csv.reader(csvfile)
    for line in reader:
        lines.append(line)
lines = lines[1:]

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

def load(index, sample):
    return cv2.imread('data/data/IMG/' + sample[index].split('/')[-1])
train_samples, validation_samples = train_test_split(lines, test_size=0.2)

def flip(image, angle):
    processed_image = cv2.flip(image,1)
    processed_angle = angle*-1.0
    return (processed_image, processed_angle)

def generator(samples, batch_size=32):
    num_samples = len(samples)
    while 1:
        shuffle(samples)
        for offset in range(0, num_samples, batch_size):
            batch_samples = samples[offset:offset+batch_size]

            images = []
            angles = []
            correction = 0.2
            for batch_sample in batch_samples:

                # load center image / angle
                center_image = load(0, batch_sample)
                center_angle = float(batch_sample[3])
                # flip center image / angle
                center_flipped = flip(center_image, center_angle)
                images.extend([center_image, center_flipped[0]])
                angles.extend([center_angle, center_flipped[1]])

                # load left image / angle
                left_image = load(1, batch_sample)
                left_angle = center_angle + correction
                # flip left image /angle
                left_flipped = flip(left_image, left_angle)
                images.extend([left_image, left_flipped[0]])
                angles.extend([left_angle, left_flipped[1]])

                # load right image / angle
                right_image = load(2, batch_sample)
                right_angle = center_angle - correction
                # load right image / angle
                right_flipped = flip(right_image, right_angle)
                images.extend([right_image, right_flipped[0]])
                angles.extend([right_angle, right_flipped[1]])

            X_train = np.array(images)
            y_train = np.array(angles)

            yield sklearn.utils.shuffle(X_train, y_train)

from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, ELU, Cropping2D, Dropout
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D
from keras.models import Model
from keras.layers import BatchNormalization
from keras.optimizers import Adam
import matplotlib.pyplot as plt

batch_size = 32
kp = 0.5
epochs = 5
train_generator = generator(train_samples, batch_size=batch_size)
validation_generator = generator(validation_samples, batch_size=batch_size)
model = Sequential()
model.add(Lambda(lambda x: x/255.0 - 0.5, input_shape=(160,320,3)))
model.add(Cropping2D(cropping=((70,25),(0,0))))
# Convolution 5x5 Layers
model.add(Conv2D(24,5,5,subsample=(2,2),activation='relu'))
model.add(Conv2D(36,5,5,subsample=(2,2),activation='relu'))
model.add(Conv2D(48,5,5,subsample=(2,2),activation='relu'))
# Convolution 3x3 Layers
model.add(Conv2D(64,3,3,activation='relu'))
model.add(Conv2D(64,3,3,activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), dim_ordering="th"))
model.add(Dropout(kp))
model.add(Flatten())
# Full-Connected Layers
model.add(Dense(100))
model.add(Dense(50))
model.add(Dense(10))
model.add(Dense(1))
print(model.summary())
model.compile(loss='mse', optimizer='adam')
#model.fit(X_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=10)
model.fit_generator(train_generator,
            steps_per_epoch=np.ceil(len(train_samples)/batch_size),
            validation_data=validation_generator,
            validation_steps=np.ceil(len(validation_samples)/batch_size),
            epochs=5, verbose=1)
#Save the model visualization and weights.
model.save('model.h5')
plot_model(model, to_file='model.png')


from django.db import models
from datetime import datetime

class Company(models.Model):
    name = models.CharField(max_length=120)
    email = models.EmailField(null=True)
    contact_no = models.CharField(max_length=20, null=True, blank=True)
    address = models.TextField(null=True, blank=True)
    created_at = models.DateTimeField(default=datetime.now)
    updated_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ("-id", )

    def __str__(self):
        return str(self.id)

    # Get company object by pk
    def get_company_obj_by_pk(self, pk):
        try:
            company_obj = Company.objects.get(pk=pk)
        except Company.DoesNotExist:
            return None
        else:
            return company_obj


import pandas as pd
base = pd.read_csv('credit-data.csv')
base.head()

base.shape

# Apagandoa a coluna i#clientid pois seu conteúdo não ajudará nos algoritmos
base = base.drop('i#clientid', axis = 1)
base.head()

base = base.dropna() # Apagando os registros que não tiveram seus valores preenchidos (NAN)
base.shape

# Realizando o escalonamento e padroizando(standardscaler)
from sklearn.preprocessing import StandardScaler
scaler_x = StandardScaler()
base[['income', 'age', 'loan']] = scaler_x.fit_transform(base[['income', 'age', 'loan']])
base.head()

# Variável que recebe os atributos previsores.
x = base.drop('c#default', axis = 1) # c#default retira a classe (coluna) que mostra quem vai pagar e quem não

# Variável que recebe o C#default que o conjunto de 0 e 1
y = base['c#default']

x.head()

y.head()

x.columns

# Importando o framework
import tensorflow as tf
colunas = [tf.feature_column.numeric_column(key = column) for column in x.columns]

# Visualizando a variável colunas criada
print(colunas[2]) # Basta colocar o número da coluna

# Divisão da base de dados de treinamento e teste
from sklearn.model_selection import train_test_split
x_treinamento, x_teste, y_treinamento, y_teste = train_test_split(x, y, test_size = 0.3, random_state = 0)

x_treinamento.shape

x_teste.shape

# Criando a função para o treinamento com estimators
funcao_teste = tf.estimator.inputs.pandas_input_fn(x = x_treinamento,
                                                         y = y_treinamento,
                                                         batch_size = 8,
                                                         num_epochs = None,
                                                         shuffle = True)

# Criando a rede neural
classificador = tf.estimator.DNNClassifier(feature_columns = colunas, hidden_units = [4, 4])
classificador.train(input_fn = funcao_treinamento, steps = 1000)

# Criando a variável para fazer avaliação na base de dados de teste
metricas_teste = classificador.evaluate(input_fn = funcao_teste, steps = 1000)

metricas_teste


# -*- coding: utf-8 -*-
__author__ = 'Triantafyllos Paschaleris'

import collections
import pprint

alphabet = "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ"

##### VALUES TO BE CHANGED
wordLength = 10

fixedLetters = {
#position:letter
    1:"Π",#First Given Letter.
    wordLength:"Ρ",#Last Given Letter.
}
##### END OF CHANGEABLE VALUES

matchedWordsList = []
lettersFrequency = {}

fileName = r'words\searchReadyWordsOfLength' + str(wordLength) + '.txt'
with open(fileName,"r",encoding='utf-8') as f:
    wordsList = f.readlines()

for word in wordsList:
    word = word.replace("\n","")#words have a newline character as read from the file, so it has to be removed
    for position,fixedLetter in  fixedLetters.items():
        if not word[position-1]==fixedLetter:
            break
    else:
        if word[0]==fixedLetters[1]:
            if fixedLetters[wordLength] == "" or word[-1] == fixedLetters[wordLength]:
                matchedWordsList.append(word)#append the words in the list
                for letter in word[1:-1]:#Counting the frequency of the letters in the word, apart from the given first and last letters
                    lettersFrequency[letter] = lettersFrequency.setdefault(letter, 0) + 1#increase by one the counter of the letter (if it doesn't exist just get a default value of zero)

pprint.pprint(matchedWordsList)
lettersFrequency = collections.OrderedDict(sorted(lettersFrequency.items(), key=lambda t: t[1],reverse=True))#sort the dictionary by values in descending order
pprint.pprint(lettersFrequency)


import os
from setuptools import setup

# Utility function to read the README file.
# Used for the long_description.  It's nice, because now 1) we have a top level
# README file and 2) it's easier to type in the README file than to put a raw
# string in below ...
def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
    name = "speechgraph",
    version = "0.0.1",
    author = "facuzeta",
    description = ("speechgraph"),
    license = "BSD",
    keywords = "",
    url = "https://ar.linkedin.com/in/facuzeta",
    packages=['speechgraph'],
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Topic :: Utilities",
        "License :: OSI Approved :: BSD License",
    ],
    install_requires=['numpy','scipy','nltk','networkx'],
    data_files=[],
    )


from django.urls import path
from . import views

app_name = 'contacts'
urlpatterns = [
    path('', views.index, name='index'),
    path('<int:contact_id>', views.details, name='details' ),
    path('new/', views.new, name='new'),
    path('save/submit/', views.save_submit, name='save_submit')
]


import tkinter as tk
import tkinter.messagebox
import tkinter.font
import tkinter.ttk

import os
import sys

import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

import numpy as np
from sat_tracker.tracker import *
from sat_tracker.database import *

class PySat_GUI(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("PySat")
        self.protocol('WM_DELETE_WINDOW', self.on_close)
        ##################  PARAMETERS  ###################
        ##################  MENU  ##################
        menubar = tk.Menu(self)
        self.config(menu=menubar)
        orbit_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Orbit", menu=orbit_menu)
        propagate_menu = tk.Menu(orbit_menu, tearoff=0)
        orbit_menu.add_cascade(label="Propagated Recently", menu=propagate_menu)
        orbit_menu.add_separator()
        orbit_menu.add_command(label="New Orbit", command=self.new_orbit)
        orbit_menu.add_command(label="Open Orbit", command=self.open_orbit)
        orbit_menu.add_command(label="Save Orbit", command=self.save_orbit)
        orbit_menu.add_command(label="Save Orbit As", command=self.save_orbit_as)

        track_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Tracker", menu=track_menu)
        tracked_menu = tk.Menu(orbit_menu, tearoff=0)
        track_menu.add_cascade(label="Tracked Recently", menu=tracked_menu)
        track_menu.add_separator()
        track_menu.add_command(label="Import TLE File", command=self.import_tle)
        track_menu.add_command(label="Preferences", command=self.tracker_preferences_open)

        help_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Help", menu=help_menu)

        ##################  VARIABLES  ##################
        # ENGINE
        self.database = None
        self.database_toggle = []
        self.database_categories = []
        self.database_sources = []
        self.database_supp = []
        self.trackable_names = None
        self.trackable_numbers = None
        self.tracker = None
        # COLORS
        self.main_color = "#282828"
        self.secondary_color = "#484848"
        self.text_color = "white"
        self.config(bg=self.main_color)
        # WINDOWS
        # Tracker Preferences
        self.tracker_pref_window = None
        self.previous_configuration = None
        self.tracker_pref_default_label = None
        self.tracker_pref_default_frame = None
        self.tracker_pref_active = None
        self.tracker_pref_molniya = None
        self.tracker_pref_iridium = None
        self.tracker_pref_cosmos = None
        self.tracker_pref_fengyun = None
        self.tracker_pref_indian_asat = None
        self.tracker_pref_russian_asat = None
        self.supp_data_var = None
        self.tracker_pref_entry = None
        self.tracker_pref_buttons = None
        self.tracker_pref_accept = None
        self.tracker_pref_cancel = None
        # WIDGETS
        # Search Bar
        self.search_frame = None
        self.search_var = None
        self.search_entry = None
        self.search_list_box = None
        self.select_button = None
        self.search_results = []
        # Selection Bar
        self.selected_frame = None
        self.tracked_satellites = []
        self.selected_numbers = []
        self.selected_list_box = None
        self.delete_button = None
        # Notebooks
        self.frame_notebook = None
        # Tracking Tab
        self.tracking_tab = None
        self.tracking_figure = None
        self.tracking_ax = None
        self.night_shade = None
        self.tracking_canvas = None
        self.tracking_lines = []
        self.tracking_points = []
        # 3D View Tab
        self.view_3D_tab = None
        self.data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
        ##################  INITIALIZATION  ##################
        self.initialize_UI()
        ################# REFRESHING LOOP ##################
        self.refresh()

    def initialize_UI(self):
        self.load_configuration()
        #### SATELLITES DATABASE ####
        toggles_default = [i.get() for i in self.database_toggle]
        self.database = TLE_Database(self.data_dir, self.database_sources, toggles_default, load_online=True)
        self.tracker = Tracker(self.database)

        #### MAIN TABS ####
        self.frame_notebook = tk.ttk.Notebook(self, width=1000, height=500)
        self.tracking_tab = tk.Frame(self.frame_notebook, bg=self.main_color)
        self.view_3D_tab = tk.Frame(self.frame_notebook, bg=self.main_color)
        self.frame_notebook.add(self.tracking_tab, text="Live Tracking")
        self.frame_notebook.add(self.view_3D_tab, text="3D View")
        self.frame_notebook.grid(column=0, row=0, rowspan=2)

        #### SEARCH BAR ####
        self.search_frame = tk.Frame(self)
        names = self.database.deconstructed_data[0]
        numbers = self.database.deconstructed_data[1]
        names, numbers = (list(t) for t in zip(*sorted(zip(names, numbers))))
        self.trackable_names = names
        self.trackable_numbers = numbers
        self.search_var = tk.StringVar()
        self.search_var.trace("w", self.update_search_list)
        self.search_entry = tk.Entry(self.search_frame, textvariable=self.search_var, width=30)
        self.search_list_box = tk.Listbox(self.search_frame, selectmode=tk.MULTIPLE, width=40, height=12)
        self.search_entry.grid(row=0, column=0, padx=10, pady=3)
        self.search_list_box.grid(row=1, column=0, padx=10, columnspan=2)
        self.select_button = tk.Button(self.search_frame, text="+", command=self.search_select, width=3)
        self.select_button.grid(column=1, row=0, sticky="w")
        self.update_search_list()
        self.search_frame.grid(column=1, row=0, sticky="ne")

        #### SELECTED BAR ####
        self.selected_frame = tk.Frame(self)
        self.tracked_satellites = []
        self.selected_list_box = tk.Listbox(self.selected_frame, selectmode=tk.MULTIPLE, width=40, height=12)
        self.selected_list_box.bind("<<ListboxSelect>>", self.satellite_selected)
        self.delete_button = tk.Button(self.selected_frame, text="-", command=self.selection_list_delete, width=3)
        self.selected_list_box.grid(row=1, column=0)
        self.delete_button.grid(row=0, column=0)
        self.update_selection_list()
        self.selected_frame.grid(column=1, row=1, sticky="new")

        #### LIVE TRACKING TAB ####
        self.tracking_figure = Figure(dpi=100)
        self.tracking_figure.patch.set_facecolor(self.main_color)
        self.tracking_ax = self.tracking_figure.add_subplot(111, projection=ccrs.PlateCarree())
        self.tracking_ax.set_extent([-180, 180, -90, 90])
        self.tracking_ax.stock_img()
        self.tracking_ax.coastlines()
        current_time = datetime.datetime.utcnow()
        self.night_shade = self.tracking_ax.add_feature(Nightshade(current_time, alpha=0.4))
        self.tracking_ax.gridlines(draw_labels=False, linewidth=1, color='blue', alpha=0.3, linestyle='--')
        self.live_tracking_plot()
        self.tracking_canvas = FigureCanvasTkAgg(self.tracking_figure, master=self.tracking_tab)
        self.tracking_canvas.draw()
        self.tracking_canvas.get_tk_widget().grid(row=0, column=0, sticky="nsew", rowspan=2)

        #### 3D VIEW TAB ####

        #### COLUMN CONFIGURATION ####
        self.grid_columnconfigure(0, weight=1)
        self.tracking_tab.grid_columnconfigure(0, weight=1)

        #### ROW CONFIGURATION ####
        self.grid_rowconfigure(0, weight=1)
        self.tracking_tab.grid_rowconfigure(0, weight=1)

    def load_configuration(self):
        with open("configuration", "r") as file:
            lines = file.readlines()
        # DATABASE = LINE 1
        self.database_toggle = []
        self.database_categories = []
        line_1 = lines[0].replace("{", " ").replace("}", " ").split()[1].split(";")
        for i in range(len(line_1)):
            self.database_toggle.append(tk.BooleanVar(self, bool(int(line_1[i][-1]))))
            line_1[i] = line_1[i].split("=")
            self.database_categories.append(line_1[i][0].replace("_", " "))
        # LINKS = LINE 2
        line_2 = lines[1].replace("{", " ").replace("}", " ").split()[1].split(";")
        self.database_sources = [None] * len(self.database_categories)
        for i in range(len(self.database_sources)):
            self.database_sources[i] = line_2[i]
        # SUPP LINKS = LINE 3
        self.database_supp = []
        line_3 = lines[2].replace("{", " ").replace("}", " ").split()
        if len(line_3) != 1:
            line_3 = line_3[1].split(";")
            for i in range(len(line_3)):
                self.database_supp.append(line_3[i].split("="))

    def save_configuration(self):
        with open("configuration", "r") as file:
            lines = file.readlines()
        file = open("configuration", "w")
        # UPDATING DATABASE TOGGLE
        file.write("DEFAULT_TOGGLE{")
        name = self.database_categories[0]
        file.write(name + "=" + str(int(self.database_toggle[0].get())))
        for i in range(1, len(self.database_toggle)):
            name = self.database_categories[i].replace(" ", "_")
            file.write(";" + name + "=" + str(int(self.database_toggle[i].get())))
        file.write("}\n")
        # NOT CHANGING DEFAULT LINKS
        file.write(lines[1])
        # UPDATING SUPPLEMENTARY SOURCES
        file.write("SUPP_SOURCES{")
        if len(self.database_supp) != 0:
            name = self.database_supp[0][0]
            link = self.database_supp[0][1]
            file.write(name + "=" + link)
            for i in range(len(self.database_supp) - 1):
                name = self.database_supp[i][0]
                link = self.database_supp[i][1]
                file.write(";" + name + "=" + link)
        file.write("}\n")
        # CLOSING FILE
        file.close()

    def live_tracking_plot(self):
        # Removing past Nightshade and adding current
        self.night_shade.remove()
        current_time = datetime.datetime.utcnow()
        self.night_shade = self.tracking_ax.add_feature(Nightshade(current_time, alpha=0.4))
        # Removing past Ground-Tracks
        if len(self.tracking_lines) != 0:
            for i in self.tracking_lines:
                self.tracking_ax.lines.pop(0)
            self.tracking_lines = []
        # Removing past Nadirs
        if len(self.tracking_points) != 0:
            for i in self.tracking_points:
                i.remove()
            self.tracking_points = []
        # Getting Estimations from Tracker
        t0 = time.time()
        N = 241
        times = np.linspace(t0 - 3600, t0 + 3600, num=241)
        if len(self.tracked_satellites) != 0:
            Long, Lat, H = self.tracker.sub_points(times)
            Long = np.degrees(Long)
            Lat = np.degrees(Lat)
        # Plotting Ground Tracks and current Nadir Positions
        selection = self.selected_list_box.curselection()
        middle = int(N/2)
        for i in range(len(self.tracked_satellites)):
            if i in selection:
                line = self.tracking_ax.plot(Long[i, :], Lat[i, :], transform=ccrs.Geodetic(), color="red", zorder=1)
                point = self.tracking_ax.scatter(Long[i, middle], Lat[i, middle], color="white", s=25,
                                                 alpha=1, transform=ccrs.PlateCarree(), zorder=2, edgecolors='black')
                self.tracking_lines.append(line)
            else:
                point = self.tracking_ax.scatter(Long[i, middle], Lat[i, middle], color="white", s=25,
                                                 alpha=1, transform=ccrs.PlateCarree(), zorder=2, edgecolors='black')
            self.tracking_points.append(point)

    def update_search_list(self, *args):
        search_term = self.search_var.get()
        self.search_list_box.delete(0, tk.END)
        self.search_results = []
        for i in range(len(self.trackable_names)):
            sat_name = self.trackable_names[i]
            sat_number = self.trackable_numbers[i]
            if search_term.lower() in sat_name.lower() or search_term in str(sat_number):
                self.search_list_box.insert(tk.END, sat_name)
                self.search_results.append(sat_number)

    def search_select(self):
        selection = self.search_list_box.curselection()
        names = self.tracked_satellites
        numbers = self.selected_numbers
        for i in selection:
            number = self.search_results[i]
            entered = self.search_list_box.get(i)
            if number not in self.selected_numbers:
                numbers.append(number)
                names.append(entered)
        names, numbers = (list(t) for t in zip(*sorted(zip(names, numbers))))
        self.tracked_satellites, self.selected_numbers = names, numbers
        self.update_selection_list()
        # Updating Tracker
        self.tracker.update_objects(self.selected_numbers)
        # Updating Plots
        self.update_plots()

    def update_selection_list(self, *args):
        self.selected_list_box.delete(0, tk.END)
        for item in self.tracked_satellites:
            self.selected_list_box.insert(tk.END, item)

    def satellite_selected(self, *args):
        self.update_plots()

    def delete_all(self, *args):
        self.tracked_satellites = []
        self.update_selection_list()
        # Updating Tracker
        self.tracker.update_objects(self.selected_numbers)
        # Updating Plots
        self.update_plots()

    def selection_list_delete(self):
        selection = self.selected_list_box.curselection()
        selection = list(selection)
        for i in selection[::-1]:
            self.tracked_satellites.pop(i)
            self.selected_numbers.pop(i)
        self.update_selection_list()
        # Updating Tracker
        self.tracker.update_objects(self.selected_numbers)
        # Updating Plots
        self.update_plots()

    def find_current_tab(self, *args):
        name = self.frame_notebook.tab(self.frame_notebook.select(), "text")
        return name

    def update_plots(self):
        if self.find_current_tab() == "Live Tracking":
            self.live_tracking_plot()
            self.tracking_canvas.draw()
        if self.find_current_tab() == "3D View":
            pass

    def refresh(self):
        self.update_plots()
        self.after(500, self.refresh)

    def new_orbit(self):
        pass

    def save_orbit(self):
        pass

    def open_orbit(self):
        pass

    def save_orbit_as(self):
        pass

    def import_tle(self):
        pass

    def tracker_preferences_open(self):
        # Main Window
        self.tracker_pref_window = tk.Toplevel(self)
        self.previous_configuration = self.database_toggle
        self.tracker_pref_window.grab_set()
        self.tracker_pref_window.title("Tracker Preferences")
        # self.tracker_pref_window.geometry("250x250")
        # Default Database Frame
        self.tracker_pref_default_label = tk.Label(self.tracker_pref_window, text="Default Databases")
        self.tracker_pref_default_label.grid(row=0, column=0, padx=10)
        self.tracker_pref_default_frame = tk.Frame(self.tracker_pref_window,
                                                   highlightbackground="gray", highlightthickness=2)
        self.tracker_pref_default_frame.grid(row=1, column=0, padx=10)
        self.tracker_pref_active = tk.Checkbutton(self.tracker_pref_default_frame, text="Active Satellites",
                                                  variable=self.database_toggle[0])
        self.tracker_pref_active.grid(row=1, column=0, sticky="w")
        self.tracker_pref_molniya = tk.Checkbutton(self.tracker_pref_default_frame, text="Molniya",
                                                   variable=self.database_toggle[1])
        self.tracker_pref_molniya.grid(row=2, column=0, sticky="w")
        self.tracker_pref_iridium = tk.Checkbutton(self.tracker_pref_default_frame, text="Iridium 33 Debris",
                                                   variable=self.database_toggle[2])
        self.tracker_pref_iridium.grid(row=3, column=0, sticky="w")
        self.tracker_pref_cosmos = tk.Checkbutton(self.tracker_pref_default_frame, text="Cosmos 2251 Debris",
                                                  variable=self.database_toggle[3])
        self.tracker_pref_cosmos.grid(row=4, column=0, sticky="w")
        self.tracker_pref_fengyun = tk.Checkbutton(self.tracker_pref_default_frame, text="Fengyun 1C Debris",
                                                   variable=self.database_toggle[4])
        self.tracker_pref_fengyun.grid(row=5, column=0, sticky="w")
        self.tracker_pref_indian_asat = tk.Checkbutton(self.tracker_pref_default_frame, text="Indian ASAT Test Debris",
                                                       variable=self.database_toggle[5])
        self.tracker_pref_indian_asat.grid(row=6, column=0, sticky="w")
        self.tracker_pref_russian_asat = tk.Checkbutton(self.tracker_pref_default_frame, text="Russian ASAT Test Debris",
                                                        variable=self.database_toggle[6])
        self.tracker_pref_russian_asat.grid(row=7, column=0, sticky="w")
        # Supplementary Databases
        self.tracker_pref_default_label = tk.Label(self.tracker_pref_window, text="Extra Databases")
        self.tracker_pref_default_label.grid(row=2, column=0, sticky="w", padx=10)
        self.supp_data_var = tk.StringVar()
        if len(self.database_supp) == 0:
            self.supp_data_var.set("")
        else:
            self.supp_data_var.set(self.database_supp.join(";"))
        self.tracker_pref_entry = tk.Entry(self.tracker_pref_window, textvariable=self.supp_data_var, width=30)
        self.tracker_pref_entry.grid(row=3, column=0, padx=10)
        # Buttons
        self.tracker_pref_buttons = tk.Frame(self.tracker_pref_window)
        self.tracker_pref_buttons.grid(row=4, column=0, padx=10, pady=10)
        self.tracker_pref_accept = tk.Button(self.tracker_pref_buttons, text="Accept",
                                             command=self.tracker_preferences_accept, width=10)
        self.tracker_pref_accept.grid(row=0, column=0, padx=10)
        self.tracker_pref_cancel = tk.Button(self.tracker_pref_buttons, text="Cancel",
                                             command=self.tracker_preferences_cancel, width=10)
        self.tracker_pref_cancel.grid(row=0, column=1, padx=10)

    def tracker_preferences_accept(self):
        #### CHANGING CONFIGURATION FILE ####
        if len(self.supp_data_var.get()) != 0:
            self.database_supp = self.supp_data_var.get().split(";")
        else:
            self.database_supp = []
        self.save_configuration()
        #### RELOADING DATABASE ####
        # Reloading Database
        toggles_default = [i.get() for i in self.database_toggle]
        self.database = TLE_Database(self.data_dir, self.database_sources, toggles_default,
                                     self.database_supp, load_online=True)
        self.tracker = Tracker(self.database)
        # Getting trackable names and numbers for search list
        names = self.database.deconstructed_data[0]
        numbers = self.database.deconstructed_data[1]
        names, numbers = (list(t) for t in zip(*sorted(zip(names, numbers))))
        self.trackable_names = names
        self.trackable_numbers = numbers
        #### DESTROYING WINDOW ####
        self.tracker_pref_window.destroy()
        self.update_search_list()

    def tracker_preferences_cancel(self):
        self.database_toggle = self.previous_configuration
        self.tracker_pref_window.destroy()

    def on_close(self):  # Is called when we want to exit the application
        # Creation of a message choice box
        response = tkinter.messagebox.askyesno('Exit', 'Are you sure you want to exit?')
        if response:
            self.destroy()

    def toggleFullScreen(self):  # Is called when we want to change into fullscreen or remove it.
        self.attributes("-fullscreen", not self.attributes("-fullscreen"))


import importlib
import logging
import re
from collections import namedtuple
from os import listdir
from os.path import isfile, join, abspath

from mongoengine import connect

from nlp.relation_extraction.data_source import MODEL_PATH
from nlp.relation_extraction.data_source import MONGO_HOST, MONGO_PORT

logger = logging.getLogger(__name__)

PY_FILE_REGEX = re.compile(".*\.py$")
ModelIdentifier = namedtuple('ModelIdentifier', ['db_name', 'db_alias', 'collection_name',
                                                 'fields', 'payload_fields', 'function_fields',
                                                 'model_class'])

class SourceLoader:
    """
    Relation Extraction Data Source
    """
    def __init__(self, connect_str=None):
        self.model_location = abspath(MODEL_PATH)
        self.load_path = ".".join(MODEL_PATH.split("/"))
        self.connect_str = connect_str
        self.models = list()
        self.data_sources = dict()

    def form_sources(self):
        model_modules = [c for c in listdir(self.model_location) if
                         isfile(join(self.model_location, c)) if c != '__init__.py']

        model_modules = [m for m in model_modules if PY_FILE_REGEX.match(m)]
        for model_module in model_modules:
            # get the name of the class
            model_module = model_module.split('.')[0]
            try:
                module_path = self.load_path + '.' + model_module
                model_class = model_module
                module = importlib.import_module(module_path)
                self.models.append(ModelIdentifier(db_name=module.db_name, db_alias=module.db_alias,
                                                   collection_name=module.collection_name,
                                                   fields=module.relation_fields,
                                                   payload_fields = module.payload_fields,
                                                   function_fields = module.function_fields,
                                                   model_class=module.model_class))
            except (ImportError, Exception) as e:
                raise RuntimeError("Error importing the module ", e)

        for model in self.models:
            if self.connect_str:
                conn = connect(model.db_name, model.db_alias, host=self.connect_str)
            else:
                conn = connect(model.db_name, model.db_alias, host=MONGO_HOST, port=MONGO_PORT)
                model_name = model.db_name + "." + model.db_alias + "." + model.collection_name
            data_source = MongoDataSource(model_name, conn, model)
            self.data_sources[model_name] = data_source

    def close_sources(self):
        for data_source in self.data_sources.values():
            data_source.conn.close()

class MongoDataSource:
    """
    Mongo Data Source should be used in a with clause
    # TO DO : Enhance to allow filters in the query, also include support
    for cursor managers to kill cursors on demand and misc features
    to handle cursor timeouts, batch read on cursor etc.
    """
    def __init__(self, name, db_conn, model_identifer):
        self.name = name
        self.db_conn = db_conn
        self.model_identifier = model_identifer

    def start(self, query_filter=None):
        self.query_filter = query_filter
        self.items = self.model_identifier.model_class.objects[9000:10000]

    def __iter__(self):
        return self

    def __getstate__(self):
        state = dict()
        return state

    def __setstate__(self, d):
        self.__dict__.update(d)

    def next(self):
        try:
            item = self.items.next()
            data_field_values = []

            for f in self.model_identifier.fields:
                field_value = item[f]
                if field_value[-1] not in ['.', '?']:
                    field_value += '.'
                data_field_values.append((f, field_value))

            if self.model_identifier.payload_fields:
                for f in self.model_identifier.payload_fields:
                    field_value = item[f]
                    data_field_values.append(("payload", field_value))

            if self.model_identifier.function_fields:
                for f in self.model_identifier.function_fields:
                    field_value = item[f]
                    field_value = field_value.split(" ")
                    i = [index for index, e in enumerate(field_value) if e.find(":") != -1]
                    if i and i[0] != -1:
                        field_value = " ".join(field_value[:i[0]])
                        data_field_values.append(("ff", field_value))

            return tuple(data_field_values)

        except StopIteration as e:
            raise e
        except Exception as e:
            raise RuntimeError("Unknown error on Source", e)


import pygame
from pygame.locals import*
import time
import narrative

class Backdrop(object):
    def __init__(self, image_name, size):

        im = pygame.image.load(image_name)
        self.size = (size[0], int(size[1]*4/5))
        self.image = pygame.transform.scale(im, self.size)
        self.rect = self.image.get_rect()

    def draw(self, scrn, loc = (0,0)):
        scrn.blit(self.image, loc)

class Textbox(pygame.Surface):
    """Changed so that it now takes text argument as a tuple,
    to make printing lines on the same thing easier"""

    # Constructor. Pass in the color of the block,
    # and its x and y position
    def __init__(self, size, text):
        pygame.Surface.__init__(self,size)
        pygame.sprite.Sprite.__init__(self)
        self.width = size[0]
        self.height = size[1]/5
        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.imageout = pygame.Surface((self.width,self.height))
        self.imageout.fill((0, 50, 60))

        # Fetch the rectangle object that has the dimensions of the image
        # Update the position of this object by setting the values of rect.x and rect.y
        self.rectout = self.imageout.get_rect()
        self.rectout.x=0
        self.rectout.y= size[1]- self.height

        pygame.sprite.Sprite.__init__(self)

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.imagein = pygame.Surface((self.width*0.9,self.height*0.75))
        self.imagein.fill((250,255,255))

        # Fetch the rectangle object that has the dimensions of the image
        # Update the position of this object by setting the values of rect.x and rect.y
        self.rectin = self.imagein.get_rect()
        self.rectin.x=  int((self.width - self.width*0.9)/2)
        self.rectin.y= size[1]-self.height +  int((self.height-self.height*0.75)/2)

        pygame.font.init()
        self.font = pygame.font.SysFont('Comic Sans MS', 28)
        self.text = text

    def draw(self, screen):
        screen.blit(self.imageout, (0, self.rectout.y))
        self.imagein.fill((250,255,255))
        if isinstance(self.text, tuple):
            loc_y = 5
            for line in self.text:
                self.textsurface = self.font.render(line, False,(0,0,0))
                self.imagein.blit(self.textsurface,(5,loc_y))
                loc_y += 25
        elif isinstance(self.text, str):
            self.textsurface = self.font.render(self.text, False,(0,0,0))
            self.imagein.blit(self.textsurface,(5,5))
        screen.blit(self.imagein, (self.rectin.x,self.rectin.y))

    def update(self, words):
        self.text = words

class Item(pygame.sprite.Sprite):
    def __init__(self, name, filename, loc, scl, take = False):
        pygame.sprite.Sprite.__init__(self)
        im = pygame.image.load(filename)
        orig_size = im.get_rect()
        image = pygame.transform.scale(im, (int(orig_size.w*scl), int(orig_size.h*scl)))
        self.image= image
        self.Rect = pygame.Rect(self.image.get_rect()).move(loc)
        self.name = name
        self.loc = loc
        self.hidd= False
        self.take = take

    def draw(self, scrn):
        scrn.blit(self.image, self.loc)

    def click(self,pos):
        if pygame.Rect(self.Rect).collidepoint(pos[0], pos[1]):
            return self.name
        if pos[0] in range(self.loc[0], self.loc[0] + self.Rect.width):
            if pos[1] in range(self.loc[1], self.loc[1]+self.Rect.height):
                return self.name
        else:
            return False

    def update(self, pos):
        if self.click(pos):
            if self.take:
                self.hidd = True

class Inventory(pygame.sprite.Group):
    def __init__(self):
        pygame.sprite.Group.__init__(self)
        self.items = []
    def add_item(self, item):
        self.items.append(item.name)
        self.add(item)

class Room(pygame.sprite.LayeredUpdates):
    def __init__(self, items, backdrops):
        pygame.sprite.LayeredUpdates.__init__(self)

        self.items = []
        for item in items:
            self.add(item)
            self.items.append(item)
        self.backdrop = backdrops
        self.items_vis = []
        for item in self.items:
            if not item.hidd:
                self.items_vis.append(item)

    def draw(self, scrn):
        self.backdrop.draw(scrn)
        for item in self.items_vis:
            item.draw(scrn)

    def update(self, pos):
        for item in self.items_vis:
            if item.click(pos) and item.take:
                self.items_vis.remove(item)
            item.update(pos)

class SpaceGameModel(object):
    """Model of the game"""
    def __init__(self, size, rooms, doors,puzzles):
        self.allrooms = rooms #dictionary of all rooms
        self.room = self.allrooms["startRoom"]
        self.inventor = Inventory()
        self.messages = narrative.messages
        self.textbox= Textbox(size, self.messages['game_intro'])
        self.doors = doors
        self.puzzles=puzzles
        self.choices=0

    def draw(self, scrn):
        self.room.draw(scrn)
        self.textbox.draw(scrn)

    def get_clicked(self, pos):
        clicked_item = False
        for item in self.room.items:
            if item.click(pos):
                clicked_item = item.click(pos)
        return clicked_item

    def update(self, pos, words = None):
        """Changes the model based upon new information"""

        if words:
            self.textbox.update(words)
        for key in self.allrooms:
            self.allrooms[key].update(pos)
        for item in self.room.items:
            if item.hidd and item.take:
                self.inventor.add_item(item)
        # if self.get_clicked(pos) in self.doors:
        #     self.room = self.allrooms[self.doors.get(self.get_clicked(pos))]

class PygameWindowView(object):
    def __init__(self, model, size):
        pygame.init()
        pygame.display.init()
        self.screen = pygame.display.set_mode(size)
        self.model = model

    def draw(self):
        self.model.draw(self.screen)
        pygame.display.update()

class MouseController(object):
    """ Handles input for space game """
    def __init__(self, model):
        self.model = model

    def handle_event(self, event):
        """ Event handler"""
        if event.type == MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            itemC = self.model.get_clicked(pos)
            if itemC:
                self.model.choices+=1
            #print(itemC)
            #print(itemC+"2")
            msg = self.model.messages.get(itemC)

            door = self.model.doors.get(itemC)
            if door:
                self.model.room = self.model.allrooms[door]
                # print(self.model.room)
            if self.model.puzzles.get(itemC) in self.model.inventor.items:
                msg = self.model.messages.get(itemC+'2')
            if str(self.model.choices) in self.model.messages and msg!=None:
                msg = msg + self.model.messages.get(str(self.model.choices))
            elif str(self.model.choices) in self.model.messages and msg==None:
                msg=self.model.messages.get(str(self.model.choices))
            self.model.update(pos, msg)

            # if self.model.doors.get(itemC):
            #     self.model.room = self.model.allrooms[self.model.doors[itemC]]
            #     self.model.update(pos)
            #     print(self.model.room)
        if event.type != KEYDOWN:
            return

def ratio_scale(filename, scl_factor):
    im = pygame.image.load(filename)
    orig_size = im.get_rect()
    image = pygame.transform.scale(im, (int(orig_size.w*scl_factor), int(orig_size.h*scl_factor)))
    return images

def mainz():
    size = (1152,864+36) #(2048, 1536)
    Scl = 1152/2048

    redB1 = Item('scene1', 'Rbutton1.PNG', (550, 500), .75)
    greenB1 = Item("scene2", "Gbutton1.PNG", (600, 500), .75)
    blueB1 = Item('scene3', 'Bbutton1.PNG', (650, 500), .75)
    unlock = Item('unlock', 'special.png', (650, 500), .75)

    tohall = Item('halldo','docor.jpeg',(400,300),.75)

    tostor = Item('stordo','spook.png',(200,300),.75)

    tocock = Item('cockdo','liki.png',(600,300),.75)

    tocomm = Item('commdo','vommere.png',(400,300),.10)
    wrench = Item("wrench","wrench.png", (200,200), .75, True)

    toobs = Item('obsdo','obdoror.png',(600,300),.75)

    toair = Item('airdo','lockor.png',(500,100),.1)

    totank = Item('tankdo','tankor.png',(600,700),.75)

    cockpit = Room([tohall],Backdrop("cock.jpg",size))
    hallway = Room([tostor,redB1,tocomm],Backdrop("Hallway1.PNG", size))
    obser = Room([tocomm],Backdrop("obby.jpg", size))
    comms = Room([tohall,toobs,wrench],Backdrop("comm.jpg", size))
    bridge = Room([tohall,tocock,unlock], Backdrop("Bridge.PNG", size))
    staht = Room([greenB1, redB1, blueB1], Backdrop("blackiv_thumbnail.jpg", size))
    storage = Room([tohall, toair, totank], Backdrop("stormagore.jpg", size))
    oxytank = Room([tostor], Backdrop("tank.jpg", size))
    airlock = Room([tostor], Backdrop("lcok.jpg", size))

    rooms = {"bridge":bridge, 'startRoom':staht,"hallway":hallway,"storage":storage,"cockpit":cockpit,"commroom":comms,"observation":obser,"tank":oxytank,"lock":airlock}
    doors ={"scene1":"bridge","halldo":"hallway","stordo":"storage","cockdo":"cockpit","commdo":"commroom","obsdo":"observation","airdo":"lock","tankdo":"tank"}
    puzzles ={"unlock":"wrench"}

    return SpaceGameModel(size, rooms, doors,puzzles)

if __name__ == '__main__':
    def main_loop():
        size = (1152,864+36) #(2048, 1536)
        pygame.init()
        running = True
        gameover=False
        while running:
            pygame.display.init()
            Modl=mainz()
            SCRNtemp = PygameWindowView(Modl,size)
            Contrl = MouseController(Modl)
            while not gameover:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        running=False
                    Contrl.handle_event(event)
                time.sleep(1/8)
                SCRNtemp.draw()
                print(Modl.choices)
                if Modl.choices>=20:
                    gameover=True
            time.sleep(1/8)
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if event.key==K_SPACE:
                        gameover=False
                    if event.type == pygame.QUIT:
                        running=False
        pygame.quit()

    main_loop()


#!/usr/bin/python3
#  -*-  coding:  iso-8859-1  -*-

"""Test CamCASP using He2 examples.
"""

import argparse
from glob import glob
import re
import os
from pathlib import Path
import string
import subprocess
from shutil import rmtree
from sys import stdout

parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""Test procedure using He2 examples.
""",epilog="""
This test runs the He2 test examples for each basis set type (MC, MC+, DC, DC+)
and compares the result summaries with the results in the check directories,
using extract_saptdft.py to obtain the result summaries.

It's only necessary to use the --clean option to tidy up when testing is finished.

""")

parser.add_argument("--dirname", "-d", help="Name for test directories",
                    default="test")
parser.add_argument("--verbosity", help="verbosity level",
                    type=int, default=0)
parser.add_argument("--clean", action="store_true",
                    help="Delete files created by previous tests and exit")
parser.add_argument("--debug", help="Keep scratch files for debugging purposes",
                    action="store_true")
args = parser.parse_args()

ok = True
camcasp = os.getenv("CAMCASP")
if not camcasp:
    print("""Environment variable CAMCASP must be set to the base CamCASP directory
If that hasn't been done you probably also need to run the setup.py script""")
    ok = False
cores = os.getenv("CORES")
if not cores:
    print("""Environment variable CORES must be set to the number of processors
available to CamCASP""")
    ok = False
if not ok:
    exit(1)

basis_types = ["DC+", "DC", "MC+", "MC"]

base = os.path.join(camcasp,"tests","He2")
dirname = args.dirname

if args.clean:
    os.chdir(base)
    if os.path.exists("test_results"):
        os.rename("test_results","previous_test_results")
    for type in basis_types:
        name = "aTZ_" + type
        os.chdir(os.path.join(base,name))
        files = glob("*")
        for file in files:
            if file in ["README", "He2.clt", "check"]:
                pass
            elif Path(file).is_dir():
                rmtree(file)
            else:
                os.remove(file)
    exit(0)

failed = 0
test_results = os.path.join(base,"test_results")
if os.path.exists(test_results):
    os.remove(test_results)
results = ""
#  Set jobs running
for type in basis_types:
    os.chdir(base)
    name = "aTZ_" + type
    print(f"\n\nHelium dimer, basis type {name}\n")
    stdout.flush()
    os.chdir(name)
    arglist = ["runcamcasp.py", "He2", "--directory", dirname,
               "--ifexists", "delete", "--work", "He2_"+type]
    if args.verbosity > 0:
        arglist.extend(["--verbosity", "{:1d}".format(args.verbosity)])
    if args.debug: arglist.append("--debug")
    rc = subprocess.call(arglist, stderr=subprocess.STDOUT)
    if rc > 0:
        print(f"{name} job failed")
        print("See ", os.path.join(base,name,name+".log"))
        exit(4)

    out = os.path.join(base,name,dirname,"OUT")
    if os.path.exists(out):
        os.chdir(out)
        if os.path.exists(os.path.join(out,"He2.log")):
            with open(os.path.join(out,"He2.log")) as LOG:
                log = LOG.read()
                if re.search("CamCASP finished normally", log):
                    results += "\nBasis type {}:\n".format(type)
                    os.chdir(os.path.join(base,name))
                    with open("/tmp/temp","w") as Z:
                        subprocess.call(["extract_saptdft.py",
                             "--unit", "kelvin", "check", dirname], stdout=Z)
                    with open("/tmp/temp") as Y:
                        results += Y.read()
                elif re.search(r'CamCASP finished with error|Task abandoned', log):
                    results += f"\nBasis type {type}\:\nCalculation failed -- see log"
                    failed += 1

print("Finished")
print(results)

lines = results.split("\n")

ok = True
while len(lines) > 0:
    line = lines.pop(0)
    if re.match(r'Basis', line):
        print(line)
    elif re.match(r'check', line):
        check = re.sub(r'check', "", line)
    elif re.match(r'test ', line):
        test = re.sub(r'test ', "", line)
        if test == check:
            print("Test successful")
        else:
            checkvalue = [float(g) for g in check.split()]
            testvalue = [float(g) for g in test.split()]
            dmax = 0.0
            for n in range(len(checkvalue)):
                dmax = max(dmax, abs(testvalue[n] - checkvalue[n]))
            ok = False
            print(check)
            print(test)
            print(f"Differences between test and check values up to {dmax:10.2e}")
if ok and failed == 0:
    print("All He2 tests completed successfully")
    exit(0)
elif failed == 0:
    print("All He2 tests completed. Some results were different from check results")
    exit(3)
else:
    print(f"{failed:1d} of the 4 tests failed. See full report for details")
    exit(2)


import threading
import torch
import cv2
import math

from ..object_detection.yolov7.utils.plots import plot_one_box
from ..object_detection.yolov7.utils.labels import Labels
from ....pkg.config.config import config

PERSON_THRESHOLD = config["detection"]["person_threshold"]
IOT_EVENT_ZONE_LABEL = "Iot event zone"
CAMERA_EVENT_ZONE_LABEL = "Camera event zone"
CAMERA_ZONE_COLOR = [0, 0, 255] # in BGR format
IOT_ZONE_COLOR = [0, 170, 255]
PERSON_COLOR = [255, 0, 0]

palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1)
"""Function to Get color from tracking ID"""
def compute_color_for_labels(label):
    """
    Simple function that adds fixed color depending on the class
    """
    color = [int((p * (label ** 2 - label + 1)) % 255) for p in palette]
    return tuple(color)

#............................... Bounding Boxes Drawing ............................
"""Function to Draw Bounding boxes"""
def draw_boxes(img, bbox, identities=None, categories=None, names=None, offset=(0, 0)):
    for i, box in enumerate(bbox):
        x1, y1, x2, y2 = [int(i) for i in box]
        x1 += offset[0]
        x2 += offset[0]
        y1 += offset[1]
        y2 += offset[1]
        cat = int(categories[i]) if categories is not None else 0
        id = int(identities[i]) if identities is not None else 0
        data = (int((box[0]+box[2])/2),(int((box[1]+box[3])/2)))
        color = compute_color_for_labels(id)

        if id == 0:
            id = "Unconfirmed ID"

        label = str(id) + ": "+ names[cat]
        (w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1)
        cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
        cv2.rectangle(img, (x1, y1 - 20), (x1 + w, y1), color, -1)
        cv2.putText(img, label, (x1, y1 - 5),cv2.FONT_HERSHEY_SIMPLEX,
                    0.6, [255, 255, 255], 1)
        # cv2.circle(img, data, 6, color,-1)   #centroid of box
    return img

def draw_trails(img, trajectories):
    for key in trajectories:
        trajectory = trajectories[key]
        for i in range(1, len(trajectory)):
            color = compute_color_for_labels(key)
            cur = trajectory[i]
            prev = trajectory[i - 1]
            cv2.line(img, (int(prev[0]), int(prev[1])), (int(cur[0]), int(cur[1])), color, thickness=2)

class DetectionUtils:
    def satisfy_person_condition(self, label, confident):
        return confident >= PERSON_THRESHOLD and label == Labels.PERSON

    def in_event_zone(self, center_w_h, zone_coords):
        if not zone_coords:
            return True
        center_x = center_w_h[0]
        center_y = center_w_h[1]
        [from_x, from_y, to_x, to_y] = zone_coords
        return center_x >= from_x and center_x <= to_x and center_y >= from_y and center_y <= to_y

    def get_zone_coords(self, iot_event_zone_coords, camera_event_zone_coords, is_ai_event):
        return camera_event_zone_coords if is_ai_event else iot_event_zone_coords

    def get_zone_label(self, is_ai_event):
        return CAMERA_EVENT_ZONE_LABEL if is_ai_event else IOT_EVENT_ZONE_LABEL

    def get_zone_color(self, is_ai_event):
        return CAMERA_ZONE_COLOR if is_ai_event else IOT_ZONE_COLOR

    def filter_results(self, detection_results, iot_event_zone_coords, camera_event_zone_coords, is_ai_event):
        results = list()
        zone_coords = self.get_zone_coords(iot_event_zone_coords, camera_event_zone_coords, is_ai_event)

        for i in range(len(detection_results.results)):
            result = detection_results.results[i]

            if self.satisfy_person_condition(result.class_id, result.confident) and self.in_event_zone(result.center_w_h, zone_coords):
                results.append(result)
        detection_results.results = results
        return detection_results

    def equal_xyxy(self, result_xyxy, track_xyxy):
        [x, y, z, t] = result_xyxy
        [a, b, c, d] = track_xyxy

        cx = x + abs(z - x) / 2
        cy = y + abs(t - y) / 2
        ca = a + abs(c - a) / 2
        cb = b + abs(d - b) / 2
        threshold = max(abs(z - x), abs(t - y)) / 2
        dis = math.sqrt((cx - ca) ** 2 + (cb - cy) ** 2)
        if dis > threshold:
            return False

        # if abs(x - a) > threshold or abs(y - b) > threshold or abs(z - c) > threshold or abs(t - d) > threshold:
        #     return False
        return True

    def concatenate_unconfirmed_tracking_object(self, detection_results, xyxy_boxes, identities, categories):
        indices = list()
        xyxy_boxes = list(xyxy_boxes)
        identities = list(identities)
        categories = list(categories)
        results = detection_results.results
        for idx, result in enumerate(results):
            found = False
            for box in xyxy_boxes:
                if self.equal_xyxy(result.xyxy, box):
                    found = True
            if not found:
                indices.append(idx)
        for idx in indices:
            xyxy_boxes = xyxy_boxes + [results[idx].xyxy]
            identities = identities + [0]
            categories = categories + [results[idx].class_id]
        return xyxy_boxes, identities, categories

    def preprocess_frame(self, detection_results, iot_event_zone_coords, camera_event_zone_coords, is_ai_event, tracker):
        tracker.update(detection_results)
        zone_coords = self.get_zone_coords(iot_event_zone_coords, camera_event_zone_coords, is_ai_event)
        zone_label = self.get_zone_label(is_ai_event)
        zone_color = self.get_zone_color(is_ai_event)
        if zone_coords:
            plot_one_box(zone_coords, detection_results.img_frame_with_box, label=zone_label, color=zone_color, line_thickness=1)

        print("Length of detection results: ", len(detection_results.results))
        print("Length of tracking objects: ", len(tracker.get_identities()))
        xyxy_boxes = tracker.get_xyxy_boxes()
        identities = tracker.get_identities()
        categories = tracker.get_categories()
        xyxy_boxes, identities, categories = self.concatenate_unconfirmed_tracking_object(detection_results, xyxy_boxes, identities, categories)
        draw_boxes(detection_results.img_frame_with_box, xyxy_boxes, identities, categories, detection_results.names)

        cur_trajectories = tracker.get_current_frame_trajectories()
        # print("Trajectories: ", cur_trajectories)
        draw_trails(detection_results.img_frame_with_box, cur_trajectories)


#!/usr/bin/env python
#
# Creates two charts on the same figure.
#
#
# agomez (at) tacc.utexas.edu
# 30 Oct 2014
#
# ---------------------------------------------------------------------

import numpy as np
import matplotlib.pyplot as plt

def f(t):
  return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)

plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
plt.subplot(212)
plt.plot(t2, 2.5+np.cos(2*np.pi*t2), color="orange",  linewidth=2.5, linestyle="-")
plt.plot(t2, t2, 'r--')
plt.plot(t2, 5.0-t2, 'g--')
plt.show()


"""Short workflow as an exercise for working with Apache Airflow.
It imports a covid xml file from S3, transforms data and loads to Postgres tabel .
+ Fetch covid data from API and imports to Postgres database.
output : two tables in DB
countries2020_2021 =  vaccinations given in 2020/2021 per country
totals2020_2021 = total vaccinations given per world in 2020/2021, total deaths per 2020/2021

"""

import csv
import requests
import json
import pandas as pd
import pandasql as ps
import xml.etree.ElementTree as ET
from sqlalchemy import create_engine
from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database
import psycopg2
from airflow.hooks.postgres_hook import PostgresHook
import boto3
from datetime import date

class ImportCovidS3dPercountry:
    """Basic python callable class to initialize and run the `VaccinatedPercountry` class.

    Functions:
        run(loperation, table_name, **kwargs):
            Initializes and runs the `CovidRefresh` class, calling the `main` function.
    """

    @staticmethod
    def run(operation, table_name, **kwargs):
        """Initializes and runs the `VaccinatedPercountry` class, calling the `main` function."""
        vaccinated_percountry = VaccinatedPercountry(task_instance=kwargs['task_instance'])
        result = vaccinated_percountry.main(
            table_name=table_name
            , task=operation)
        return result

class VaccinatedPercountry:
    """Class to import covid data

    Attributes:
        task_instance: the airflow task instance object used for Xcoms
    Functions:
        __init__(dag_info, task_instance, logger): Initializes the CovidRefresh class
        main(table_name, task): Wrapper to call the right function for a given operation
        start_etl(): Exctracts data from S3, transform and load to postgreSQL
        create_table(table_name): Create the destination table in the database
        start_etl_totals():
            exctracts data from table countries2020_2021, transform and load to postgreSQL
        start_etl_deaths:
            Fetch covid data from API , transform data and loads to Postgres totals2020_2021

    """

    def __init__(self, task_instance):
        """Initializes the CovidRefresh class"""
        self.task_instance = task_instance

    def main(self, table_name, task):
        """Wrapper to call the right function for a given operation

        Parameters:
            table_name(string): The name of the table data is being imported into
            task(string): The specific subtask that needs to be executed
        Returns:
            bool: True if everything successful
        """

        if task == 'start_etl':
            self.start_etl(table_name)
        elif task == 'create_table_countries2020_2021':
            self.table_name = table_name
            self.create_table_countries2020_2021(table_name)
        elif task == 'create_table_totals2020_2021':
            self.create_table_totals2020_2021(table_name)
        elif task == 'start_etl_totals':
            self.start_etl_totals(table_name)
        elif task == 'start_etl_deaths':
            self.start_etl_deaths(table_name)
        else:
            return False
        return True

    def create_table_countries2020_2021(self, table_name):
        """Create the destination table in the database

        Parameters:
            table_name(string): The name of the table that should be created
        Returns:
            bool: True if everything successful
        """

        # get the xml file's path that holds the SQL
        path = __file__.split('.')[-2] + '.xml'
        create_table_query = ET.parse(path).getroot().find('create_table_countries2020_2021').text
        pg_hook = PostgresHook(postgres_conn_id='covid_id')
        pg_hook.run(create_table_query.format(table_name=table_name))
        return True

    def start_etl(self, table_name):
        """Download the data from S3 bucket,  transform data, load to table(countries2020_2021)

        Parameters:
            table_name(string): The name of the table that will be used for loading data
        Returns:
            bool: True if everything successful
        """

        # 1. extract  data from s3 to dataframe
        client = boto3.client('s3')
        path = 's3://covid-exercise/covid-data.csv'
        df = pd.read_csv(path)

        # 2. transform data(dataframe) with pandas for loading to postgreSQL
        q2021 = """SELECT location, SUM (new_vaccinations) FROM df
                WHERE new_vaccinations IS NOT NULL AND date LIKE '%2021%' GROUP BY location"""
        q2020 = """SELECT location, SUM (new_vaccinations) FROM df WHERE
                new_vaccinations IS NOT NULL AND date LIKE '%2020%' GROUP BY location"""
        df2021 = ps.sqldf(q2021, locals())
        df2020 = ps.sqldf(q2020, locals())
        df2021 = df2021.rename(columns = {'location': 'Country',
                                        'SUM (new_vaccinations)': 'Total_2021'}, inplace = False)
        df2020 = df2020.rename(columns = {'location': 'Country',
                                        'SUM (new_vaccinations)': 'Total_2020'}, inplace = False)
        dict1 = df2020.set_index('Country')['Total_2020'].to_dict()
        df2021['Total_2020']= df2021['Country'].map(dict1)
        df2021['Total_2021'] = df2021['Total_2021'].fillna(0).astype(int)
        df2021['Total_2020'] = df2021['Total_2020'].fillna(0).astype(int)
        df2021=df2021.reindex(columns=['Country','Total_2020','Total_2021'])

        # 3. load to posgreSQL
        connection = PostgresHook(postgres_conn_id='covid_id').get_conn()
        cursor = connection.cursor()

        for index, row in df2021.iterrows():
            cursor.execute("""INSERT INTO countries2020_2021
                    (Country, Total_2020, Total_2021)
                    VALUES (%s, %s, %s)""",
                    (row["Country"], row["Total_2020"], row["Total_2021"]) )
        connection.commit()
        return True

    def create_table_totals2020_2021(self, table_name):
        """Create the destination table in the database

        Parameters:
            table_name(string): The name of the table that should be created
        Returns:
            bool: True if everything successful
        """

        # get the xml file's path that holds the SQL
        path = __file__.split('.')[-2] + '.xml'
        create_table_query = ET.parse(path).getroot().find('create_table_totals2020_2021').text
        pg_hook = PostgresHook(postgres_conn_id='covid_id')
        pg_hook.run(create_table_query.format(table_name=table_name))
        return True

    def start_etl_totals(self, table_name):
        """Gets the data from postgreSQL(countries2020_2021),
            transform data(world sum vaccined people), load to table(totals2020_2021)

        Parameters:
            table_name(string): The name of the table that will be used for loading data
        Returns:
            bool: True if everything successful
        """
        # 1. extract/SELECT  data from postgreSQL
        path = __file__.split('.')[-2] + '.xml'
        create_select_query =  ET.parse(path).getroot().find('select_query').text.format(table_name=table_name)
        df = pd.read_sql_query(create_select_query,con = PostgresHook(postgres_conn_id='covid_id').get_conn())

        # 2. transform data with pandas for loading to postgreSQL
        df = df.drop('country', 1)
        dfs = df.sum(axis=0)

        # 3. load to posgreSQL
        connection = PostgresHook(postgres_conn_id='covid_id').get_conn()
        cursor = connection.cursor()
        cursor.execute("""INSERT INTO totals2020_2021
                    (fields, total_2020, total_2021)
                    VALUES ('vaccinated', %s, %s)""",
                    (int(dfs[0]), int(dfs[1])) )
        connection.commit()
        return True

    def start_etl_deaths(self, table_name):
        """Fetch covid data from API , transform data and loads to Postgres totals2020_2021

        Parameters:
            table_name(string): The name of the table that should be used for loading
        Returns:
            bool: True if everything successful
        """
        #fetch API data
        response = requests.get('https://corona-api.com/countries?include=timeline')
        data = json.loads(response.text)

        #transform data to totals per 2020 and 2021/today
        def countries_deaths_yearly(date):

            total_deaths = 0
            for i in range(len(data['data'])):
                for x in range(len( data['data'][i]['timeline'])):
                    if data['data'][i]['timeline'][x]['date'] == date:
                        total_deaths += data['data'][i]['timeline'][x]['deaths']

            return total_deaths

        deaths_2020 = countries_deaths_yearly('2020-12-31')
        deaths_2021 = countries_deaths_yearly(str(date.today())) - deaths_2020

        # load to posgreSQL
        connection = PostgresHook(postgres_conn_id='covid_id').get_conn()
        cursor = connection.cursor()
        cursor.execute("""INSERT INTO totals2020_2021
                    (fields, total_2020, total_2021)
                    VALUES ('deaths', %s, %s)""",
                    (deaths_2020, deaths_2021) )
        connection.commit()
        return True


from rest_framework import viewsets, mixins
from django_filters.rest_framework import DjangoFilterBackend

from api.utils.household_viewset_utils import PreventCrossHouseholdUpdates
from .serializer import  (
  UpdateCreateIngredientSerializer,
  IngredientDetailSerializer,
)
from .model import Ingredient

from api import filters

__all__ = [
  'IngredientViewSet'
]

class IngredientViewSet(
  PreventCrossHouseholdUpdates,
  viewsets.GenericViewSet,
  mixins.ListModelMixin,
  mixins.CreateModelMixin,
  mixins.DestroyModelMixin,
  mixins.RetrieveModelMixin,
  mixins.UpdateModelMixin
):
  queryset = Ingredient.objects.all()
  # filterset_fields = ['active', 'referenceable']
  filter_backends = [filters.CurrentHouseholdFilterBackend, DjangoFilterBackend,]

  def get_serializer_class(self):
    if self.action in ['create', 'update', 'partial_update', 'delete']:
      return UpdateCreateIngredientSerializer
    # Default is to serializer all the way down to give all the data.
    return IngredientDetailSerializer


import logging
import os

from flask import Flask
from flask_migrate import Migrate

from library.models import db
from library.urls import app_urls
from library.utils import before_request

logging.basicConfig()
logging.root.setLevel(getattr(logging, os.getenv("LOG_LEVEL", "INFO")))

migrate = Migrate()

def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_object("config.Config")
        # app.config.from_pyfile("config.py")
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    logging.info(f"Flask App: The following configuration were loaded:: {app.config}")

    # Register Extentions
    db.init_app(app)
    migrate.init_app(app, db)

    # Register URLs
    app_urls(app)

    # Register Middleware
    before_request(app)

    return app


import config
import torch
import transformers
import torch.nn as nn
from torchcrf import CRF

class EntityModel(nn.Module):
    def __init__(self, num_tag, num_pos):
        super(EntityModel, self).__init__()
        self.num_tag = num_tag
        self.num_pos = num_pos
        self.bert = transformers.BertModel.from_pretrained(config.BASE_MODEL_PATH,return_dict=False)
        self.bilstm= nn.LSTM(768, 1024 // 2, num_layers=1, bidirectional=True, batch_first=True)

        self.dropout_tag = nn.Dropout(0.3)
        self.dropout_pos = nn.Dropout(0.3)

        self.hidden2tag_tag = nn.Linear(1024, self.num_tag)
        self.hidden2tag_pos = nn.Linear(1024, self.num_pos)

        self.crf_tag = CRF(self.num_tag, batch_first=True)
        self.crf_pos = CRF(self.num_pos, batch_first=True)

    # return the loss only, not encode the tag
    def forward(self, ids, mask, token_type_ids, target_pos, target_tag):
        # Bert - BiLSTM
        x, _ = self.bert(ids, attention_mask=mask, token_type_ids=token_type_ids)
        h, _ = self.bilstm(x)

        # drop out
        o_tag = self.dropout_tag(h)
        o_pos = self.dropout_pos(h)

        # Hidden2Tag (Linear)
        tag = self.hidden2tag_tag(o_tag)
        pos = self.hidden2tag_pos(o_pos)

        # Create Mask for crf layer - the current torchcrf require boolen mask tensor
        mask = torch.where(mask==1, True, False)

        # compute log-likelihood loss through crf.forward(),
        # and use 'token_mean' reduction method for more stable loss
        # by default, reduction = 'sum'
        loss_tag = - self.crf_tag(tag, target_tag, mask=mask, reduction='token_mean')
        loss_pos = - self.crf_pos(pos, target_pos, mask=mask, reduction='token_mean')
        loss = (loss_tag + loss_pos) / 2

        return loss

    # encode the tag, dont return loss
    def encode(self, ids, mask, token_type_ids, target_pos, target_tag):
        # Bert - BiLSTM
        x, _ = self.bert(ids, attention_mask=mask, token_type_ids=token_type_ids)
        h, _ = self.bilstm(x)

        # drop out
        o_tag = self.dropout_tag(h)
        o_pos = self.dropout_pos(h)

        # Hidden2Tag (Linear)
        tag = self.hidden2tag_tag(o_tag)
        pos = self.hidden2tag_pos(o_pos)

        # Create Mask for crf layer
        mask = torch.where(mask==1, True, False)

        # encode the tag by crf.decode
        tag = self.crf_tag.decode(tag, mask=mask)
        pos = self.crf_pos.decode(pos, mask=mask)

        return tag, pos


import pygame
import random
from pygame import Vector2

from sys import exit as sexit

# constants
BG_COLOR = (10, 70, 10)

# pygame init
pygame.init()

pygame.display.set_caption('Card Game')

width, height = (1000, 300)
screen = pygame.display.set_mode((width, height),
                                 flags=pygame.SCALED)

f = random.choice(('co', 'ca', 'p', 't'))
cards = [
    pygame.image.load(f"assets/{f}{i}.gif")
    for i in range(2, 15)
]
random.shuffle(cards)

pos = [
    Vector2(x * width / 13, height / 16)
    for x in range(13)
]

card_surf = pygame.Surface((71, 96))
rectdet = [screen.blit(card_surf, (x, y)) for (x, y) in pos]

click = False
running = True
clicked_card = -1
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sexit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            position_souris = event.pos
            for a in range(13):
                if rectdet[a].collidepoint(position_souris):
                    clicked_card = a
                    click = True
            #pos.append(pos.pop(clicked_card))
            rectdet = [screen.blit(card_surf, (x, y)) for (x, y) in pos]
            #cards.append(cards.pop(clicked_card))

        if event.type == pygame.MOUSEBUTTONUP:
            click = False

        if event.type == pygame.MOUSEMOTION:
            if click == True and clicked_card != -1:
                try:
                    pos[clicked_card] += event.rel
                except AttributeError:
                    pass
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                pass

    rectdet = [screen.blit(card_surf, p) for p in pos]
    screen.fill(BG_COLOR)
    for i, card in enumerate(cards):
        screen.blit(card, pos[i])
    pygame.display.flip()

pygame.quit()

# thanks https://iteroni.com/watch?v=dQw4w9WgXcQ for helping


from os import environ
import requests
from bs4 import BeautifulSoup
from sys import argv, exit

class Student:
    #set base year as current year
    base_year = 17
    try:
        proxyDict = {
                    'http_proxy': environ['http_proxy'],
                    'https_proxy': environ['https_proxy'],
                    'ftp_proxy': environ['ftp_proxy']
                }
    except KeyError:
        proxyDict = None

    def __init__(self,num):
        num = str(num)
        self.year = num[0:2]
        if(num[2]=='m' or num[2]=='M'):
            self.branch=num[2:4].lower()
            self.branch+=str(num[4])
            self.roll=num[5:7]
        else:
            self.branch=num[2]
            self.roll=num[3:5]
        self.lastsem = (Student.base_year-int(self.year))*2
        self.rollno = self.year+self.branch+self.roll
        try:
            url = "http://14.139.56.15/scheme"+self.year+"/studentresult/details.asp"
            page = requests.post(url,data={'RollNumber':self.rollno},proxies=Student.proxyDict,verify=False)
            soup = BeautifulSoup(page.text,'lxml')
            self.all_data = soup.find_all(class_='ewTable')
            self.name=self.all_data[0].find_all('tr')[0].find_all('td')[1].text.strip()
            res = self.all_data[self.lastsem*2].find_all('tr')[1].find_all('td')
            self.sgpa = res[0].text.strip().split("=")[1]
            cgpa_ = res[2].text.strip()
            self.points = cgpa_.split("/")[0]
            self.cgpa = cgpa_.split("=")[1]
        except:
            self.name = '-'
            self.sgpa = self.points = self.cgpa = '0'

    def display_result(self):
        out = self.name + "\n\t" + self.sgpa + "\n\t" +self.points+ "\n\t" + self.cgpa
        return out

if(len(argv)==2):
    roll=argv[1]
else:
    print("enter ur roll : ",end='')
    roll = str(input())

std = Student(roll)

print(std.display_result())

def sort_std(std):
    return float(std.sgpa)

print("do you want relult of whole class y or n : ",end='')
data=[]
ans = input()
if(ans!='y'):
    exit()
print("enter roll number except last 2 digits ex \'15mi5\' : ",end='')
temp = input()
print("enter range of roll numbers : ",end='')
a , b = input().split(' ')
a = int(a)
b = int(b)
if(ans=='y'):
    for i in range(a,b):
        roll = temp
        roll += "%02d"%(i)
        std = Student(roll)
        if(std.name!='0'):
            data.append(std)
            print(std.display_result())
            print()
    data.sort(key=sort_std,reverse=True)
    print("sorting....\n\n\n")
    for item in data:
        print(item.display_result())
        print()


from numpy import *
import numpy as np
from imutils.video import FileVideoStream
from imutils.video import FPS
import os
import time
import cv2
import socket
from neopixel import *
import sys
import pafy
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", required=True, help="path to input video file")
args = vars(ap.parse_args())

w = 16 # width of pixel matrix
h = 12 # height of pixel matrix
img_rgb_matrix = [[[] for x in range(h)] for y in range(w)] # construct matrix to hold rgb vals

# LED strip configuration:
LED_COUNT      = w*h      # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 10   # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP      = ws.WS2811_STRIP_BGR # Strip type and colour ordering

myvid = pafy.new(args['video'])
#best = myvid.getbestvideo(preftype="any")
streams = myvid.streams
stream = streams[1].url
print(streams[1].title)
cap = FileVideoStream(stream).start()
time.sleep(1)
#cap = cv2.VideoCapture(stream)
# while True:
#     ret, frame = cap.read()
#
# #    cv2.imshow('frame', frame)
#     k = cv2.waitKey(100) & 0xff
#     if k == 27:
#         break
#
# cap.release()
# cv2.destroyAllWindows()
def display_img(strip):
    print("Diplaying image")
    while cap.more():
        frame = cap.read()
        frame = cv2.resize(frame, (w, h), cv2.INTER_LINEAR)
        vals = frame.tolist()
        for j in range(h):
            for i in range(w):
                ind_i, ind_j = i,j
                if i%2 != 0:
                    ind_j = h-j-1
                led_index = ind_i * h + ind_j + 1
                color = vals[j][i]
                #print(led_index)
                #print("Color: {}".format(color))
                if type(color) == int:
                    color = [color]*3
                strip.setPixelColor(led_index, Color(*color))
            strip.show()

# Main program logic follows:
if __name__ == '__main__':
    # Create NeoPixel object with appropriate configuration.
    strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
    # Intialize the library (must be called once before other functions).
    strip.begin()
    display_img(strip)


import capstone
import copy
import io
import keystone
import os
import re

try:
    from . import analyze, isa
except ImportError:
    import sys

    sys.path.append(os.path.realpath(__file__))

    import analyze
    import isa

__doc__ = "instruction de/serialization"

class BaseInstructionIO:
    """
    instruction de/serialization

    files are always expected to be in byte mode

    instructions are formed in extents of the form:
        ```
        {
            "base": base,
            "instructions": iterable of capstone.CsInsn,
            "isa": ISA,
            "offset": offset from the base (for variable-length ISAs)
        }
        ```
    and multiple extents are grouped like so:
        `{(name, offset): extent}`
    """

    @staticmethod
    def dump(fp, extent):
        """load a single executable extent to a file"""
        raise NotImplementedError()

    @staticmethod
    def load(fp, _isa, base=0, offset = 0):
        """load a single executable extent from a file"""
        raise NotImplementedError()

    @staticmethod
    def loadall(fp, base = 0, extents=None):
        """load all executable extents from a file"""
        raise NotImplementedError()

    @staticmethod
    def pdump(path, extent):
        """dump a single executable extent to a path"""
        raise NotImplementedError()

    @staticmethod
    def pload(path, _isa, base=0, offset = 0):
        """load a single executable extent at a path"""
        raise NotImplementedError()

    @staticmethod
    def ploadall(path, base = 0, extents=None):
        """load all executable extents at a path"""
        raise NotImplementedError()

class AssemblyIO(BaseInstructionIO):
    """assembly de/serialization"""

    @staticmethod
    def dump(fp, extent):
        """load a single executable extent to a file"""
        for i in extent["instructions"]:
            fp.write(b"%b %b\n" % tuple((s.encode("ascii") for s in (i.mnemonic, i.op_str))))

    @staticmethod
    def load(fp, _isa, base=0, offset = 0):
        """load a single executable extent from a file"""
        _isa = isa.correlate(copy.deepcopy(_isa))

        # first, assemble

        fp.seek(offset, os.SEEK_CUR)
        assembled = bytes(keystone.Ks(_isa["keystone"]["arch"],
            _isa["keystone"]["endianness"] + _isa["keystone"]["mode"]).asm(
                fp.read())[0])

        # disassemble

        return MachineCodeIO.load(io.BytesIO(assembled), _isa, base)

    @staticmethod
    def pdump(path, extent):
        """dump a single executable extent to a path"""
        with open(path, "wb") as fp:
            AssemblyIO.dump(fp, extent)

    @staticmethod
    def pload(path, _isa, base=0, offset = 0):
        """load a single executable extent at a path"""
        _isa = isa.correlate(copy.deepcopy(_isa))

        with open(path, "rb") as fp:
            return AssemblyIO.load(fp, _isa, base, offset)

class MachineCodeIO(AssemblyIO):
    """machine code de/serialization"""

    @staticmethod
    def dump(fp, extent):
        """load a single executable extent to a file"""
        _isa = isa.correlate(copy.deepcopy(extent["isa"]))

        # dump assembly

        b = io.BytesIO()
        AssemblyIO.dump(b, extent)

        # assemble

        fp.write(bytes(keystone.Ks(_isa["keystone"]["arch"],
                           _isa["keystone"]["endianness"] + _isa["keystone"]["mode"]).asm(
            b.getvalue())[0]))

    @staticmethod
    def load(fp, _isa, base=0, offset = 0):
        """load a single executable extent from a file"""
        _isa = isa.correlate(copy.deepcopy(_isa))

        # load

        fp.seek(offset, os.SEEK_CUR)
        return {
            "base": base,
            "instructions": capstone.Cs(_isa["capstone"]["arch"],
                _isa["capstone"]["endianness"]
                    + _isa["capstone"]["mode"]).disasm(fp.read(),
                        base + offset),
            "isa": _isa,
            "offset": offset
        }

    @staticmethod
    def loadall(fp, base = 0, extents=None):
        """load all executable extents from a file"""

        # load the binary

        binary = analyze.Binary(fp.read())

        # compute the complete _isa

        _isa = isa.correlate({
            "capstone": {
                "arch": binary.arch,
                "endianness": binary.endianess,
                "mode": binary.mode
            }})

        # load extents

        extents = dict(extents) if isinstance(extents, dict) \
            else {".text": None}
        extents = {k: {"base": v} for k, v in extents.items()}

        for extent in binary.executable_sections:
            _base = extents[extent.name] \
                if isinstance(extents.get(extent.name, None), int) \
                else extent.addr
            code = io.BytesIO(extent.code)

            for offset in range(min(16, extent.size)): # Intel-specific##############################################################
                print("Loading extent from object (extent \"%s\", offset %u)..." % (extent.name, offset))
                code.seek(0, os.SEEK_SET)
                extents[(extent.name, offset)] = MachineCodeIO.load(
                    io.BytesIO(code.read()), _isa, base + _base, offset)

        # filter out unmatched extents

        return {k: v for k, v in extents.items() if len(v.keys()) > 1}

    @staticmethod
    def pdump(path, extent):
        """load a single executable extent to a path"""
        _isa = isa.correlate(copy.deepcopy(extent["isa"]))

        with open(path, "wb") as fp:
            MachineCodeIO.dump(fp, extent)

    @staticmethod
    def pload(path, _isa, base=0, offset = 0):
        """load a single executable extent at a path"""
        _isa = isa.correlate(copy.deepcopy(_isa))

        with open(path, "rb") as fp:
            return MachineCodeIO.load(fp, base, offset)

    @staticmethod
    def ploadall(path, base = 0, extents=None):
        """load all executable extents at a path"""
        with open(path, "rb") as fp:
            return MachineCodeIO.loadall(fp, base, extents)

if __name__ == "__main__":
    # test loading from a binary

    compiled = MachineCodeIO.ploadall("../libc.so.6")

    for name_offset, extent in compiled.items():
        print("\"%s\", offset %u" % name_offset)

        for i in extent["instructions"]:
            print("\t0x%.16x\t%s\t%s" % (i.address, i.mnemonic, i.op_str))
    sys.exit()
    print(compiled)
    source = AssemblyIO.pload("../x86-32-little.S", isa.parse("x86-32-little"))
    print(source)

    # test dumping to assembly/machine code (not a full binary)

    with os.fdopen(sys.stdout.fileno(), "wb") as fp:
        AssemblyIO.dump(fp, source)
        MachineCodeIO.dump(fp, source)