"""
Created on Thu Mar 12 23:43:41 2020

@author: Mathias Byskov Nielsen

"""

import os
os.chdir('C:/Users/mathi/Desktop/Thesis/Implementations')

import torch
import numpy as np
from torch import nn
from torch.utils import data
import torch.nn.functional as F
from torch.utils.data import Dataset

from utils import read_data, clean_seqs, onehot_encoder, eight_to_three_state

############################################################
#
#   Helper functions for running the RNN
#
############################################################

def pad_input(sequences):
    """  Helper function: Pad sequences to correct format. """
    from copy import deepcopy

    # Deepcopy list
    padded_sequences = deepcopy(sequences)

    # Define length of alphabet (int)
    len_alphabet = len(padded_sequences[0][0])

    # Find max length of a seq (int)
    max_len = 0
    for seq1 in padded_sequences:
        if len(seq1) > max_len:
            max_len = len(seq1)

    # Define vector to append (list)
    pad = [0 for _ in range(len_alphabet)]

    # Add padding to all sequences
    for idx, seq in enumerate(padded_sequences):
        length = max_len - len(seq)
        for _ in range(length):
            padded_sequences[idx].append(pad)

    return padded_sequences

def int_encode_Y(structures, vocab_dict):
    """ Helper function: Integer encode structures {0, 1, 2} """
    import numpy as np

    int_structures = []

    # Convert to integers
    for structure in structures:
            int_structures.append([vocab_dict[char] for char in structure])

    # Find max length
    max_len = 0
    for structure in int_structures:
        if len(structure) > max_len:
            max_len = len(structure)

    # Pad seqeucens with ''
    for idx, structure in enumerate(int_structures):
        for _ in range(max_len + 1):
            if _ > len(structure):
                int_structures[idx].append(np.nan)

    return int_structures

class sample_loader(Dataset):
    """ Class for the samples to input into pyTorch."""

    def __init__(self, X, y, transform=None):
        """
        Args:
            X:
            y:
            transform: Optional transform to be applied on a sample.
        """
        self.X = X
        self.y = y
        self.transform = transform

    def __len__(self):
        return len(self.X)

    def __getitem__(self, idx):

        sample_X = self.X[idx]
        sample_y = self.y[idx]
        sample = {'target': sample_y, 'sequence': sample_X}

        if self.transform:
            sample = self.transform(sample)

        return sample

def train_test_split(X, y, split = 0.20):
    """
    Create train and test samples.

    Input:

    """

    import random

    lst = [i for i in range(len(X))]
    random.shuffle(lst)
    train_amount = int((1 - split) * len(X))

    train_samples_idx = lst[:train_amount]
    test_samples_idx = lst[train_amount:]

    X_train = [X[i] for i in train_samples_idx]
    X_test = [X[i] for i in test_samples_idx]

    y_train = [y[i] for i in train_samples_idx]
    y_test = [y[i] for i in test_samples_idx]

    return X_train, X_test, y_train, y_test

def get_accuracy(output, labels):
    """
    Computes accuracy of output-classification and labels.

    """

    correct = 0

    for idx in range(len(output)):
        if torch.argmax(output[idx]).item() == labels[idx].item():
            correct += 1

    acc = correct / len(output)
    return acc

def the_nn(input_size, hidden_size, n_layers, output_size, layer_type = 'RNN', nonlinearity = 'tanh', dropout = 0, bidirectional = False):
    """
    Creates and fits the recurrent neural network.

    """

    class Model(nn.Module):
        def __init__(self):
            super(Model, self).__init__()

            # Defining layers
            self.hidden_size = hidden_size
            self.n_layers = n_layers

            # RNN Layer
            if layer_type == 'RNN':
                self.rnn = nn.RNN(input_size = input_size, hidden_size = hidden_size,
                                  num_layers = n_layers, batch_first = True,
                                  bidirectional = bidirectional, nonlinearity = nonlinearity, dropout = dropout)

            if layer_type == 'LSTM':
                self.rnn = nn.LSTM(input_size = input_size, hidden_size = hidden_size,
                                  num_layers = n_layers, batch_first = True,
                                  bidirectional = bidirectional, dropout = dropout)

            if layer_type == 'GRU':
                self.rnn = nn.GRU(input_size = input_size, hidden_size = hidden_size,
                                  num_layers = n_layers, batch_first = True,
                                  bidirectional = bidirectional, dropout = dropout)

            if bidirectional == False:
                self.fc = nn.Linear(hidden_size, output_size)

            if bidirectional == True:
                self.fc = nn.Linear(hidden_size*2, output_size)

        def forward(self, x):
            batch_size = x.size(0)

            #Initializing hidden state for first input using method defined below
            hidden = self.init_hidden(batch_size, self.hidden_size)

            # Find sequence lengths (for packing)
            x_lengths = self.find_lengths(x)

            # Pack sequences
            x = torch.nn.utils.rnn.pack_padded_sequence(x, x_lengths, batch_first=True, enforce_sorted=False)

            # Run the network
            out, hidden = self.rnn(x, hidden)

            # Unpack the sequences again
            out, _ = torch.nn.utils.rnn.pad_packed_sequence(out, batch_first=True)

            # Run through the linear layer
            out = F.relu(self.fc(out))

            # Perform log_softmax on output (WORSE PERFORMANCE!)
            #x = F.log_softmax(x, dim = 2)

            return out, hidden

        def init_hidden(self, batch_size, hidden_size):
            # This method generates the first hidden state of zeros which we'll use in the forward pass

            if layer_type == 'RNN' or layer_type == 'GRU':

                if bidirectional == False:
                    hidden = torch.zeros(self.n_layers, batch_size, self.hidden_size)

                if bidirectional == True:
                    hidden = torch.zeros(2, batch_size, self.hidden_size)

            if  layer_type == 'LSTM':

                if bidirectional == False:
                    hidden = (torch.zeros(self.n_layers, batch_size, self.hidden_size),
                              torch.zeros(self.n_layers, batch_size, self.hidden_size))

                if bidirectional == True:
                    hidden = (torch.zeros(2, batch_size, self.hidden_size),
                              torch.zeros(2, batch_size, self.hidden_size))

            return hidden

        def find_lengths(self, input_seq):
            # Find seq-lengths of each sequence (used to pack sequences)
            x_lengths = []
            for seq in input_seq:
                for idx, vec in enumerate(seq):
                    if sum(vec).item() != 1:
                        x_lengths.append(idx)
                        break
                    if idx == 752:
                        x_lengths.append(len(seq))
            return x_lengths

    net = Model()
    return net

def the_nn_article(input_size, dropout):
    """
    Creates and fits the recurrent neural network from the article.

    """

    class Model(nn.Module):
        def __init__(self):
            super(Model, self).__init__()

            # Defining layers
            self.hidden_size = 256
            self.first_layer = 512
            self.second_layer = 1024
            self.n_layers = 2
            self.bidirectional = True
            self.dropout = dropout

            # RNN Layer
            self.rnn = nn.LSTM(input_size = input_size, hidden_size = self.hidden_size,
                                  num_layers = self.n_layers, batch_first = True,
                                  bidirectional = self.bidirectional, dropout = self.dropout)

            self.fc1 = nn.Linear(self.first_layer, self.second_layer)
            self.fc2 = nn.Linear(self.second_layer, 3)

        def forward(self, x):
            batch_size = x.size(0)

            #Initializing hidden state for first input using method defined below
            hidden = self.init_hidden(batch_size, self.hidden_size)

            # Find sequence lengths (for packing)
            x_lengths = self.find_lengths(x)

            # Pack sequences
            x = torch.nn.utils.rnn.pack_padded_sequence(x, x_lengths, batch_first=True, enforce_sorted=False)

            # Run the network
            out, hidden = self.rnn(x, hidden)

            # Unpack the sequences again
            out, _ = torch.nn.utils.rnn.pad_packed_sequence(out, batch_first=True)

            # Run through the linear layer
            out = F.relu(self.fc1(out))
            out = F.relu(self.fc2(out))

            # Perform log_softmax on output (WORSE PERFORMANCE!)
            #x = F.log_softmax(x, dim = 2)

            return out, hidden

        def init_hidden(self, batch_size, hidden_size):
            # This method generates the first hidden state of zeros which we'll use in the forward pass

            hidden = (torch.zeros(2*self.n_layers, batch_size, self.hidden_size),
                      torch.zeros(2*self.n_layers, batch_size, self.hidden_size))

            return hidden

        def find_lengths(self, input_seq):
            # Find seq-lengths of each sequence (used to pack sequences)
            x_lengths = []
            for seq in input_seq:
                for idx, vec in enumerate(seq):
                    if sum(vec).item() != 1:
                        x_lengths.append(idx)
                        break
                    if idx == 752:
                        x_lengths.append(len(seq))
            return x_lengths

    net = Model()
    return net

def train(trainloader, testloader, net, criterion = nn.CrossEntropyLoss(), num_classes = 3,
          epochs = 20, optimizer = 'SGD', lr = 0.01, weight_decay = 0, momentum = 0):
    """
    Trains the network.

    """

    if optimizer == 'SGD':
        optimizer = torch.optim.SGD(net.parameters(), lr = lr, weight_decay = weight_decay, momentum = momentum)

    if optimizer == 'RMSprop':
        optimizer = torch.optim.RMSprop(net.parameters(), lr = lr, weight_decay = weight_decay, momentum = momentum)

    if optimizer == 'Adam':
        optimizer = torch.optim.Adam(net.parameters(), lr = lr)

    res_list = []
    for epoch in range(epochs):
        running_loss_train = 0
        running_accuracy_train = 0
        for sample in trainloader:

            # Define input and target
            input_seq = sample['sequence']
            target_seq = sample['target']

            # Clear existing gradients
            optimizer.zero_grad()

            # Runs the RNN
            output, hidden = net(input_seq)

            # Slice target-seq (to fit with max_length in batch)
            target_seq = target_seq.narrow(1, 0, output.shape[1]).contiguous().view(-1).long()

            # Get mask (values that have been padded)
            mask = (target_seq > -1)

            # Flatten output
            output = output.contiguous().view(-1, num_classes)

            # Filter values in target_seq and output
            target_seq = target_seq[mask]
            output = output[mask, :]

            # Calculate loss and update weights accordingly
            loss = criterion(output, target_seq)
            loss.backward()
            optimizer.step()

            running_loss_train += loss.item()
            running_accuracy_train += get_accuracy(output, target_seq)

        with torch.no_grad():
            running_loss_test = 0
            running_accuracy_test = 0
            for sample in testloader:

                input_seq = sample['sequence']
                target_seq = sample['target']

                output, _ = net(input_seq)

                target_seq = target_seq.narrow(1, 0, output.shape[1]).contiguous().view(-1).long()
                mask = (target_seq > -1)
                output = output.contiguous().view(-1, num_classes)
                target_seq = target_seq[mask]
                output = output[mask, :]

                loss = criterion(output, target_seq)

                running_loss_test += loss.item()
                running_accuracy_test += get_accuracy(output, target_seq)

        epoch_res = f"Epoch: {epoch + 1} - Loss: {round(running_loss_train/len(trainloader), 4)} - Accuracy: {round(running_accuracy_train/len(trainloader), 4)} - Val_Loss: {round(running_loss_test/len(testloader), 4)} - Val_accuracy: {round(running_accuracy_test/len(testloader), 4)}"
        print(epoch_res)

        res_list.append(epoch_res)

    return res_list

####################################################
#
#   Running the RNN
#
####################################################

def main(path, article = False, method = 'DSSP', conversion_method = 'method_1', batch_size = 50,
         split = 0.2, shuffle = True, epochs = 20, lr = 0.01,  optimizer = 'SGD',
         layer_type = 'RNN', nonlinearity = 'tanh', dropout = 0,
         hidden_size = 20, weight_decay = 0, momentum = 0, bidirectional = False):

    print('\n|------  Running RNN  ------|\n')
    print('Read and modify seqs + structures...\n')

    parameters = locals()

    # Define int-encoding
    acids_to_int = {'A': 0, 'R': 1, 'N': 2, 'D': 3, 'B': 4, 'C': 5, 'Q': 6, 'E': 7, 'Z': 8, 'G': 9, 'H': 10, 'I': 11, 'L': 12, 'K':13, 'M':14, 'F':15, 'P':16, 'S':17, 'T':18, 'W':19, 'Y':20, 'V':21}
    structures_to_int = {'C': 0, 'H': 1, 'E': 2}

    # Read files
    files, seqs, structures = read_data(path, method)
    seqs, proteins = clean_seqs(seqs, structures)

    # Create features (X) and targets (Y)
    onehotX = onehot_encoder(seqs, acids_to_int)
    padded_X = pad_input(onehotX)

    structures = eight_to_three_state(structures, conversion_method)
    int_Y = int_encode_Y(structures, structures_to_int)

    # Create train and test loader
    X_train, X_test, y_train, y_test = train_test_split(padded_X, int_Y, split = split)

    train_loader = sample_loader(torch.Tensor(np.array(X_train, dtype=float)), torch.Tensor(y_train), transform = None)
    train_loader = data.DataLoader(train_loader, batch_size=batch_size, shuffle = shuffle)

    test_loader = sample_loader(torch.Tensor(np.array(X_test, dtype=float)), torch.Tensor(y_test), transform = None)
    test_loader = data.DataLoader(test_loader, batch_size=batch_size, shuffle = shuffle)

    # Instantiate the model with hyperparameters
    input_size = len(acids_to_int)
    output_size = len(structures_to_int)

    if article == False:
        net = the_nn(input_size = input_size, hidden_size = hidden_size, n_layers = 1,
                     output_size = output_size, bidirectional = bidirectional,
                     layer_type = layer_type, nonlinearity = nonlinearity, dropout = dropout)

    if article == True:
        net = the_nn_article(input_size, dropout = dropout)

    # Train the RNN
    res = train(train_loader, test_loader, net, criterion = nn.CrossEntropyLoss(), epochs = epochs,
              optimizer = optimizer, lr = lr, weight_decay = weight_decay, momentum = momentum)

    return str(parameters), res


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

# # Double Deep Q Networks

# ## Imports

# In[1]:

import gym
import numpy as np
import torch
import torch.optim as optim

from IPython.display import clear_output
from matplotlib import pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')

from timeit import default_timer as timer
from datetime import timedelta
import math

from utils.wrappers import *
from networks.networks import DQN
from agents.DQN import Model as DQN_Agent
from networks.network_bodies import AtariBody
from utils.ReplayMemory import ExperienceReplayMemory

from utils.hyperparameters import Config

# ## Hyperparameters

# In[2]:

config = Config()

config.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

#epsilon variables
config.epsilon_start = 1.0
config.epsilon_final = 0.01
config.epsilon_decay = 30000
config.epsilon_by_frame = lambda frame_idx: config.epsilon_final + (config.epsilon_start - config.epsilon_final) * math.exp(-1. * frame_idx / config.epsilon_decay)

#misc agent variables
config.GAMMA=0.99
config.LR=1e-4

#memory
config.TARGET_NET_UPDATE_FREQ = 1000
config.EXP_REPLAY_SIZE = 100000
config.BATCH_SIZE = 32

#Learning control variables
config.LEARN_START = 10000
config.MAX_FRAMES=1000000

#Nstep controls
config.N_STEPS=1

# ## Agent
class Model(DQN_Agent):
    def __init__(self, static_policy=False, env=None, config=None):
        super(Model, self).__init__(static_policy, env, config)

    def declare_networks(self):
        self.model = DQN(self.num_feats, self.num_actions, body=AtariBody)
        self.target_model = DQN(self.num_feats, self.num_actions, body=AtariBody)

    def get_max_next_state_action(self, next_states):
        return self.model(next_states).max(dim=1)[1].view(-1, 1)

# ## Plot Results
def plot(frame_idx, rewards, losses, sigma, elapsed_time):
    clear_output(True)
    plt.figure(figsize=(20,5))
    plt.subplot(131)
    plt.title('frame %s. reward: %s. time: %s' % (frame_idx, np.mean(rewards[-10:]), elapsed_time))
    plt.plot(rewards)
    if losses:
        plt.subplot(132)
        plt.title('loss')
        plt.plot(losses)
    if sigma:
        plt.subplot(133)
        plt.title('noisy param magnitude')
        plt.plot(sigma)
    plt.show()

# ## Training Loop
start=timer()

env_id = "PongNoFrameskip-v4"
env    = make_atari(env_id)
env    = wrap_deepmind(env, frame_stack=False)
env    = wrap_pytorch(env)
model = Model(env=env, config=config)

episode_reward = 0

observation = env.reset()
for frame_idx in range(1, config.MAX_FRAMES + 1):
    epsilon = config.epsilon_by_frame(frame_idx)

    action = model.get_action(observation, epsilon)
    prev_observation = observation
    observation, reward, done, _ = env.step(action)
    observation = None if done else observation

    model.update(prev_observation, action, reward, observation, frame_idx)
    episode_reward += reward

    if done:
        model.finish_nstep()
        model.reset_hx()
        observation = env.reset()
        model.save_reward(episode_reward)
        episode_reward = 0

        if np.mean(model.rewards[-10:]) > 19:
            plot(frame_idx, model.rewards, model.losses, model.sigma_parameter_mag, timedelta(seconds=int(timer()-start)))
            break

    if frame_idx % 10000 == 0:
        plot(frame_idx, model.rewards, model.losses, model.sigma_parameter_mag, timedelta(seconds=int(timer()-start)))

model.save_w()
env.close()

# In[ ]:


import pandas as pd
import matplotlib as plt
from datetime import datetime
import os

def main():
    # read datetime
    now = datetime.now()
    date = now.strftime("%D").replace("/", "-")

    # read csv data into pandas dataframe
    df = pd.read_csv('data.csv')
    df['time'] = pd.to_datetime(df['time'])

    # initialize plots
    temp = df.plot(x='time', y='temp',  xlabel='Time', ylabel='Temperature', legend=None)
    sig = df.plot(x='time', y='sigstrength',  xlabel='Time', ylabel='Signal Strength', legend=None)
    battery = df.plot(x='time', y='batterylevel', xlabel='Time', ylabel='Battery Level', legend=None)

    # initialize arrays for looping
    plots = [temp, sig, battery]
    names = ["temp", "sig", "battery"]

    # save each plot as a .png in our folder
    if not os.path.isdir("node/graphs/" + date + "/"):
        os.mkdir("node/graphs/" + date + "/")
    for i in range(3):
        fig = plots[i].get_figure()
        fig.savefig("node/graphs/" + date + "/" + names[i] + ".png")

if __name__ == "__main__":
    main()


import os

from utils import list_tracks, exclude_tags, match_tags, \
                  match_any_tag

# plists = {'all_shuffle' : {'mode' : 'exclude',
#                            'tags' : ['protected']}}

class Plists_Generator():

    def __init__(self, audio_dirs, plist_dir,
                 plists, extensions=['.mp3', '.flac']):

        self.audio_dirs = audio_dirs
        self.plist_dir = plist_dir
        self.plists = plists
        self.extensions = extensions

    def _add_output_array(self):
        for fname in self.plists.keys():
            self.plists[fname]['tracks']=[]

    def _match_track_to_plist(self, track, fname):

        if self.plists[fname]['mode'] == 'match':
            if match_tags(track, self.plists[fname]['tags']):
                self.plists[fname]['tracks'].append(track)

        elif self.plists[fname]['mode'] == 'any':
            if match_any_tag(track, self.plists[fname]['tags']):
                self.plists[fname]['tracks'].append(track)

        elif self.plists[fname]['mode'] == 'exclude':
            if not exclude_tags(track, self.plists[fname]['tags']):
                self.plists[fname]['tracks'].append(track)

    def scan_audio_dirs_and_add_tracks(self):

        for folder in self.audio_dirs:

            for track in list_tracks(folder, self.extensions):

                for fname in self.plists.keys():
                    # add_to_plist(track, fname)
                    # self.plists[fname]['tracks'].append(track)
                    self._match_track_to_plist(track, fname)

    def save_plists(self):

        for fname in self.plists.keys():

            fpath = os.path.join(self.plist_dir, fname + '.m3u8')

            tracks = [os.path.relpath(track, self.plist_dir)
                        for track in self.plists[fname]['tracks']]

            tracks = map(lambda x: x + '\n', tracks)

            with open(fpath, 'w') as file_m3u8:
                file_m3u8.writelines(tracks)

    def generate(self):

        self._add_output_array()
        self.scan_audio_dirs_and_add_tracks()

        # for fname in self.plists.keys():
        #     print(self.plists[fname]['tracks'])

        # print(self.plists)

        self.save_plists()


import math
import numpy as np
import string
import re
from nltk import word_tokenize, pos_tag
from nltk.corpus import wordnet, stopwords
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.stem import WordNetLemmatizer
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer, TfidfVectorizer
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
#from sklearn import cross_validation
from sklearn import linear_model
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.neural_network import MLPClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.metrics import precision_score, accuracy_score, recall_score
from sklearn.decomposition import TruncatedSVD
from sklearn.naive_bayes import GaussianNB

#reading the data from the CSV file
def create_data():
    with open("./datasets/IMDB_review_labels.txt", encoding="utf8") as f:
        text = []
        label = []
        for line in f:
            temp = line.split('  \t')
            text.append(temp[0])
            label.append(temp[1])

        return text, label

#removing stopwords, punctuations and special characters from the description
#lemmatizing the words
def clean_data(text):
    wordnet_lemmatizer = WordNetLemmatizer()
    stops = stopwords.words('english')
    nonan = re.compile(r'[^a-zA-Z ]')
    output = []
    for i in range(len(text)):
        sentence = nonan.sub('', text[i])
        words = word_tokenize(sentence.lower())
        filtered_words = [w for w in words if not w.isdigit() and not w in stops and not w in string.punctuation]
        tags = pos_tag(filtered_words)
        cleaned = ''
        for word, tag in tags:
          if tag == 'NN' or tag == 'NNS' or tag == 'VBZ' or tag == 'JJ' or tag == 'RB' or tag == 'NNP' or tag == 'NNPS' or tag == 'RBR':
            cleaned = cleaned + wordnet_lemmatizer.lemmatize(word) + ' '
        output.append(cleaned.strip())
    return output

#feature extraction - creating a tf-idf matrix
def calculate_tfidf(text, unique_words):
    text_list = list()
    for i in text:
        print(i)
        line = i.split(' ')
        word_list = {}
        line_list = list()
        for k in line:
            if k not in word_list:
                word_list[k]=1
            else:
                word_list[k]=word_list[k]+1

        for j in unique_words.keys():
            #print(j)
            if j in word_list.keys():
                line_list.append(math.log(1+word_list[j])+math.log(len(text)/unique_words[j]))
            else:
                line_list.append(math.log(len(text)/unique_words[j]))
        text_list.append(line_list)
    return text_list

def get_word_frequency(text):
    unique_words = {}
    for i in text:
        line = i.split(' ')
        for j in line:
            if j not in unique_words:
                unique_words[j] = 1
            else:
                unique_words[j] = unique_words[j] + 1
    return unique_words

#Naive Bayes classifier
def test_NaiveBayes(x_train, x_test, y_train, y_test):
    gnb = GaussianNB()
    NBClassifier = gnb.fit(x_train, y_train)
    predictions = NBClassifier.predict(x_test)
    a = accuracy_score(y_test, predictions)
    p = precision_score(y_test, predictions, average = 'weighted')
    r = recall_score(y_test, predictions, average = 'weighted')
    return a, p, r

#SVM classifier
def test_SVM(x_train, x_test, y_train, y_test):
    SVM = SVC(kernel = 'linear')
    SVMClassifier = SVM.fit(x_train, y_train)
    predictions = SVMClassifier.predict(x_test)
    a = accuracy_score(y_test, predictions)
    p = precision_score(y_test, predictions, average = 'weighted')
    r = recall_score(y_test, predictions, average = 'weighted')
    return a, p, r

#Multilayer Perceptron classfier
def test_NN(x_train, x_test, y_train, y_test):
    NN = MLPClassifier(solver = 'lbfgs', alpha = 0.00095, learning_rate = 'adaptive', learning_rate_init = 0.005, max_iter = 300, random_state = 0)
    Perceptron = NN.fit(x_train, y_train)
    predictions = Perceptron.predict(x_test)
    a = accuracy_score(y_test, predictions)
    p = precision_score(y_test, predictions, average = 'weighted')
    r = recall_score(y_test, predictions, average = 'weighted')
    return p, r

#SGD classifier
#classifier that minimizes the specified loss using stochastic gradient descent
#hinge loss works pretty well too, the modified huber reports highest precision
def test_SGD(x_train, x_test, y_train, y_test):
    SGD = SGDClassifier(loss = 'modified_huber')
    SGDC = SGD.fit(x_train1, y_train)
    predictions = SGDC.predict(x_test1)
    a = accuracy_score(y_test, predictions)
    p = precision_score(y_test, predictions, average = 'weighted')
    r = recall_score(y_test, predictions, average = 'weighted')
    return p, r

#Voting Classifiers
#SVC and SGD combined with equal weights gave 1% lower precision than SVC
def test_voting(x_train, x_test, y_train, y_test):
    SVM = SVC(kernel = 'linear', probability = True)
    SGD = SGDClassifier(loss = 'modified_huber')
    EnsembleClassifier = VotingClassifier(estimators = [('sgd', SGD), ('svc', SVM)], voting = 'soft', weights = [1,1])
    EnsembleClassifier = EnsembleClassifier.fit(x_train, y_train)
    predictions = EnsembleClassifier.predict(x_test)
    a = accuracy_score(y_test, predictions)
    p = precision_score(y_test, predictions, average = 'weighted')
    r = recall_score(y_test, predictions, average = 'weighted')
    return p, r

text, label = create_data()
text = clean_data(text)
label = [x[1] for x in label[:]]

#print(text)
#print(len(text))
unique_words = get_word_frequency(text)
#print(len(unique_words))

data = calculate_tfidf(text, unique_words)
print(len(data), len(data[1]))

#joining the title and description to create a single text document for each product
#combined = text[:]
#for i in range(len(combined)):
#    combined[i] = text[i]
#print(combined)

#feature extraction
#training = tfidf(text)
#print(training)
#training and test data splits
x_train, x_test, y_train, y_test = train_test_split(data, label, test_size = 0.2, random_state = 0)

accuracy, precision, recall = test_NaiveBayes(x_train, x_test, y_train, y_test)
print(accuracy, precision, recall)

#gnb = GaussianNB()
#y_pred = gnb.fit(x_train, y_train).predict(x_test)
#print("Number of mislabeled points out of a total %d points : %d"% (x_test.shape[0], (y_test != y_pred).sum()))

#print(len(x_train),len(x_test),len(y_train),len(y_test))
#test a classifier
accuracy, precision, recall = test_SVM(x_train, x_test, y_train, y_test)
print(accuracy, precision, recall)


import streamlit as st
import sqlite3
import pandas as pd

def adminCandidatureApprove():
    # Create a connection to the SQLite database
    conn = sqlite3.connect('candidates.db')

    # Streamlit app title
    st.title("Candidate Data Editor and Approver")

    # Retrieve data from the database
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM candidates")
    data = cursor.fetchall()

    # Convert the data to a Pandas DataFrame for easier editing
    df = pd.DataFrame(data, columns=["ID", "Name", "Email", "Phone", "DOB", "Age", "Skills", "Documents", "Approved"])

    # Display the data in a Streamlit table
    st.write("Candidate Data:")
    st.write(df)

    # Edit and approve data
    selected_id = st.number_input("Enter the ID of the candidate to edit and approve:")
    if selected_id > 0:
        candidate_data = df[df['ID'] == selected_id]

        # Display the candidate data for editing
        st.write("Candidate Data for Editing:")
        st.write(candidate_data)

        # Allow editing and approval
        new_name = st.text_input("Name", candidate_data["Name"].values[0])
        new_email = st.text_input("Email", candidate_data["Email"].values[0])
        new_phone = st.text_input("Phone", candidate_data["Phone"].values[0])
        new_skills = st.text_area("Skills", candidate_data["Skills"].values[0])
        approval = st.checkbox("Approve")

        if st.button("Update Data"):
            # Update the data in the DataFrame
            df.loc[df['ID'] == selected_id, ["Name", "Email", "Phone", "Skills", "Approved"]] = [new_name, new_email, new_phone, new_skills, approval]

            # Update the database
            cursor.execute("UPDATE candidates SET name=?, email=?, phone=?, skills=?, approved=? WHERE id=?",
                           (new_name, new_email, new_phone, new_skills, approval, selected_id))
            conn.commit()

    # Close the database connection
    conn.close()


# Задача №2 "Аудиоколлекция". Продолжение
class Track:
    def __init__(self, title, duration: int):
        self.title = title
        self.duration = duration

    def __str__(self):
        return str(f'{self.title}-'
                   f'{self.duration}min')

    def __lt__(self, other):
        if not isinstance(other, Track):
            return "Not a Track!"
        return self.duration < other.duration

    def __gt__(self, other):
        if not isinstance(other, Track):
            return "Not a Track!"
        return self.duration > other.duration

    def __eq__(self, other):
        if not isinstance(other, Track):
            return "Not a Track!"
        return self.duration == other.duration

class Album:
    def __init__(self, title, group, list_tracks: list):
        self.title = title
        self.group = group
        self.list_tracks = list_tracks

    def get_tracks(self):
        list_track_info = []
        for track in self.list_tracks:
            list_track_info.append(track)
        return list_track_info

    def add_track(self, track: Track):
        if not isinstance(track, Track):
            return
        self.list_tracks.append(track)

    def get_duration(self):
        duration_all_tracks = 0
        for track in self.list_tracks:
            duration_all_tracks = duration_all_tracks + track.duration
        return duration_all_tracks

    def __str__(self):
        list_out = ''
        for track in self.list_tracks:
            list_out = list_out + ('        ' +
                                   track.title + '-' +
                                   str(track.duration) +
                                   'min\n')
        return str(f'Name group: {self.group}\n'
                   f'Name album: {self.title}\n'
                   f'Tracks:\n' + list_out)

def main():
    kino_1 = Track('Группа крови', 5)
    kino_2 = Track('Спокойная ночь', 6)
    kino_3 = Track('В наших глазах', 4)
    kino_4 = Track('Легенда', 4)
    kino_list = [kino_1, kino_2, kino_3]

    ariya_1 = Track('Раскачаем этот мир', 6)
    ariya_2 = Track('Раб страха', 5)
    ariya_3 = Track('Бой продолжается', 6)
    ariya_4 = Track('Искушение', 4)
    ariya_list = [ariya_1, ariya_2, ariya_3]

    album_kino = Album('Группа крови', 'Кино', kino_list)
    album_ariya = Album('Игра с огнём', 'Ария', ariya_list)

    album_kino.add_track(kino_4)
    album_ariya.add_track(ariya_4)

    print(kino_3)
    print(ariya_2)
    print()
    print(album_kino)
    print(album_ariya)
    print(kino_2 < ariya_3)

if __name__ == '__main__':
    main()


import gradio as gr
from gradio.themes.utils import colors
from utils import *
from task import *

def prepare_theme():
    theme = gr.themes.Default(primary_hue=colors.gray,
                            secondary_hue=colors.emerald,
                            neutral_hue=colors.emerald).set(
        body_background_fill="*primary_800",
        body_background_fill_dark="*primary_800",

        block_background_fill="*primary_700",
        block_background_fill_dark="*primary_700",

        border_color_primary="*secondary_300",
        border_color_primary_dark="*secondary_300",

        block_border_width="3px",
        input_border_width="2px",

        input_background_fill="*primary_700",
        input_background_fill_dark="*primary_700",

        background_fill_primary="*neutral_950",
        background_fill_primary_dark="*neutral_950",

        background_fill_secondary="*primary_700",
        background_fill_secondary_dark="*primary_700",

        body_text_color="white",
        body_text_color_dark="white",

        block_label_text_color="*secondary_300",
        block_label_text_color_dark="*secondary_300",

        block_label_background_fill="*primary_800",
        block_label_background_fill_dark="*primary_800",

        color_accent_soft="*primary_600",
        color_accent_soft_dark="*primary_600",
    )
    return theme

manager = TaskManager()

def submit(namebox, tags, priority, time_options, start_date, start_time, end_date, end_time, repeat_options, repeat_value, repeat_unit, repeat_end_date, repeat_end_time):
    app = TaskApplication()
    app.name = namebox
    app.tags = tags
    app.priority = priority

    app.allow_time_specification = 'allow time specification' in time_options
    if app.allow_time_specification:
        app.use_start_time = 'use start time' in time_options
        app.use_end_time = 'use end time' in time_options
        if app.use_start_time:
            app.start_date = make_date(start_date)
            app.start_time = make_time(start_time)
        if app.use_end_time:
            app.end_date = make_date(end_date)
            app.end_time = make_time(end_time)

    app.allow_repeat = 'allow repeat' in repeat_options
    if app.allow_repeat:
        app.repeat_unit = repeat_unit
        app.repeat_value = repeat_value
        app.infinite = 'infinite' in repeat_options
        if not app.infinite:
            app.repeat_end_date = make_date(repeat_end_date)
            app.repeat_end_time = make_time(repeat_end_time)

    manager.enroll(app)
    return manager.dataframe

with gr.Blocks(theme=prepare_theme()) as demo:
    with gr.Tabs():
        with gr.Tab(label="Task Insertion"):
            with gr.Column():
                with gr.Row():
                    namebox = gr.Textbox(value="태스크 명을 입력하세요", interactive=True, label='Name', show_label=True, scale=5)
                    submit_button = gr.Button('태스크 생성', interactive=True)
                with gr.Row():
                    tags = gr.Dropdown(['Coursework', 'Daily Quest', 'Game', 'Project', 'Habby', 'Meeting', 'Seminar', 'Sleep'], multiselect=True, allow_custom_value=True, label='Tags', scale=4)
                    priority = gr.Slider(0, 1, 0.5, label='Priority', interactive=True)

                with gr.Tabs():
                    # Start Time
                    with gr.Tab(label='Time'):
                        with gr.Column():
                            time_options = gr.CheckboxGroup(choices=['allow time specification', 'use start time', 'use end time'], value=['use start time', 'use end time'], show_label=False, interactive=True)
                            with gr.Row():
                                with gr.Row():
                                    start_date = gr.Textbox("2023년 11월 16일", interactive=True, label='Start Date')
                                    start_time = gr.Textbox("00:00", interactive=True, label='Time')
                                # End Time
                                with gr.Row():
                                    end_date = gr.Textbox("2023년 11월 16일", interactive=True, label='End Date')
                                    end_time = gr.Textbox("00:00", interactive=True, label='Time')
                    with gr.Tab(label='Repeat'):
                        repeat_options = gr.CheckboxGroup(choices=['allow repeat', 'infinite'], show_label=False)
                        with gr.Row():
                            with gr.Row():
                                repeat_value = gr.Textbox(value='1', interactive=True)
                                repeat_unit = gr.Dropdown(choices=['초마다', '분마다', '시간마다', '일마다', '주마다', '달마다', '년마다'], value='일마다', interactive=True)
                            with gr.Row():
                                repeat_end_date = gr.Textbox("2023년 11월 16일", interactive=True, label='End Date')
                                repeat_end_time = gr.Textbox("00:00", interactive=True, label='Time')

        with gr.Tab(label="Task Viewer"):
            with gr.Column():
                df = gr.Dataframe(manager.dataframe)

        submit_button.click(submit, [namebox, tags, priority, time_options, start_date, start_time, end_date, end_time, repeat_options, repeat_value, repeat_unit, repeat_end_date, repeat_end_time], [df])

demo.launch(debug=True, share=True)


from .check import *

class CheckMuxingMode(Check):
    def __init__(self, reporter, mediainfo):
        super().__init__(
            reporter,
            mediainfo,
            "Error checking muxing mode",
        )

    # overriding abstract method
    def get_reply(self):
        reply, is_valid = "", True

        for section in ["general", "video", "audio", "text"]:
            for i, _ in enumerate(self.mediainfo[section]):
                if "muxing_mode" in self.mediainfo[section][i]:
                    reply += self.reporter.print_report(
                        "error",
                        section.capitalize()
                        + " #"
                        + self.mediainfo[section][i]["id"]
                        + " has muxing mode: `"
                        + self.mediainfo[section][i]["muxing_mode"]
                        + "`",
                    )
                    is_valid = False

        if is_valid:
            reply += self.reporter.print_report(
                "correct", "All tracks do not have a muxing mode"
            )

        return reply


from setuptools import setup
import os
from glob import glob
from setuptools import find_packages

package_name = 'video_capture'

setup(
 name=package_name,
 version='0.0.0',
 packages=[package_name],
 data_files=[
     ('share/ament_index/resource_index/packages',
             ['resource/' + package_name]),
     ('share/' + package_name, ['package.xml']),
     (os.path.join('share', package_name), glob('./launch/*.launch.py')),
     #(os.path.join('share', package_name), glob('./config/*config.rviz'))
   ],
 install_requires=['setuptools'],
 zip_safe=True,
 maintainer='TODO',
 maintainer_email='TODO',
 description='TODO: Package description',
 license='TODO: License declaration',
 tests_require=['pytest'],
 entry_points={
     'console_scripts': [
             'resize_node = video_capture.resize_node:main',
             'capture_node = video_capture.capture_node:main',
             'process_node = video_capture.process_node:main'
     ],
   },
)


import streamlit as st
from sympy import *
from sympy.solvers import solve

x= symbols('x'); F0, F1 ,F2= symbols('F0 F1 F2', cls=Function)

st.title("""関数の極大点，極小点，変曲点""")
#st.latex('\\frac{1}{2}mx^2')
FORM_INPUT = st.text_input('極大点，極小点，変曲点を調べたい関数を入力してください．')
'調べる関数$f(x)$は'
F0 = sympify(FORM_INPUT)
st.latex(F0)
'です.'

'入力された関数の１次導関数は'

F1 = simplify(diff(F0))
st.latex(F1)
'です．また2次導関数は'
F2 = simplify(diff(F1))
st.latex(F2)
'です．'


from typing import List, Any

from fastapi import Depends, APIRouter
from sqlalchemy.ext.asyncio import AsyncSession
from starlette import status
from starlette.exceptions import HTTPException
from starlette.responses import Response

import bot_admin_service.crud as crud
import bot_admin_service.schemas as schemas
from bot_admin_service.api.deps import auth, params
from bot_admin_service.db import models, BotUser
from bot_admin_service.db.dependency import get_session
from bot_admin_service.schemas import RequestParams

router = APIRouter()

@router.get(
    "/",
    response_model=List[schemas.BotUser],
)
async def read_bot_users(
    response: Response,
    current_user: models.User = Depends(auth.get_active_current_user),
    request_params: RequestParams = Depends(
        params.parse_params(BotUser),
    ),
    db: AsyncSession = Depends(get_session),
) -> Any:
    """
    Retrieve bot users.
    """
    users, total = await crud.bot_user.get_multi(db, request_params)
    response.headers[
        "Content-Range"
    ] = f"{request_params.skip}-{request_params.skip + len(users)}/{total}"
    return users

@router.get(
    "/{id}",
    response_model=schemas.User,
)
async def read_user_by_id(
    id: int,
    db: AsyncSession = Depends(get_session),
    current_user: models.User = Depends(auth.get_active_current_user),
) -> Any:
    """
    Get a specific bot user.
    """
    user = await crud.bot_user.get(db, id)
    if not user:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
    return user


import sys
sys.stdin = open('input.txt')
from collections import deque
# input = sys.stdin.readline

def bfs():
    v = [-1 for _ in range(101)]
    v[1] = 0
    queue = deque()
    queue.append(1)

    while queue:
        pos = queue.popleft()
        # print(pos, v[pos])
        if pos == 100:
            print(v[pos])
            return
        for i in range(1, 7):
            npos = pos + i
            # print(npos)
            if 0<=npos<=100 and v[npos] == -1:
                if arr[npos] != -1:
                    if v[arr[npos]] == -1:
                        v[arr[npos]] = v[pos] + 1
                        npos = arr[npos]
                        queue.append(npos)
                else:
                    v[npos] = v[pos] + 1
                    queue.append(npos)

N, M = map(int, input().split())
arr = [-1 for _ in range(101)]
for _ in range(N+M):
    x, y = map(int, input().split())
    arr[x] = y

bfs()


#__author__ = ritvikareddy2
#__date__ = 2019-02-19

def sortArrayByParity(A):
    """
    :type A: List[int]
    :rtype: List[int]
    """
    left = 0
    right = len(A) - 1
    while left < right:
        if A[left] % 2 == 1 and A[right] % 2 == 0:
            x = A[left]
            A[left] = A[right]
            A[right] = x
        if A[left] % 2 == 0:
            left += 1
        if A[right] % 2 == 1:
            right -= 1

    return A


from ctre.sensors import (
    AbsoluteSensorRange,
    SensorInitializationStrategy,
    CANCoder,
    CANCoderSimCollection,
)
from wpimath.geometry import Rotation2d

from util.ctrecheck import ctreCheckError

import constants

class CTREEncoder:
    def __init__(self, canId: int, offset: float, canbus: str = "") -> None:
        self.encoder = CANCoder(canId, canbus)
        self.offset = offset
        if not ctreCheckError(
            "configFactoryDefault",
            self.encoder.configFactoryDefault(constants.kConfigurationTimeoutLimit),
        ):
            return
        if not ctreCheckError(
            "configSensorInitializationStrategy",
            self.encoder.configSensorInitializationStrategy(
                SensorInitializationStrategy.BootToAbsolutePosition,
                constants.kConfigurationTimeoutLimit,
            ),
        ):
            return
        if not ctreCheckError(
            "configMagnetOffset",
            self.encoder.configMagnetOffset(
                -1 * self.offset,  # invert the offset to zero the encoder
                constants.kConfigurationTimeoutLimit,
            ),
        ):
            return
        if not ctreCheckError(
            "configAbsoluteSensorRange",
            self.encoder.configAbsoluteSensorRange(
                AbsoluteSensorRange.Signed_PlusMinus180,
                constants.kConfigurationTimeoutLimit,
            ),
        ):
            return
        if not ctreCheckError(
            "setPositionToAbsolute",
            self.encoder.setPositionToAbsolute(
                constants.kConfigurationTimeoutLimit,
            ),
        ):
            return

    def getPosition(self) -> Rotation2d:
        return Rotation2d.fromDegrees(self.encoder.getAbsolutePosition())

    def getSimCollection(self) -> CANCoderSimCollection:
        return self.encoder.getSimCollection()


import pandas as pd
import numpy as np

dfx = pd.read_csv('C:/Users/JAINY/Downloads/Train/Train.csv')
dfx.head()

x = dfxx[:30000,0]
y = dfxx[:30000,1]

print(x.shape)

from nltk.tokenize import RegexpTokenizer
from nltk.stem.porter import PorterStemmer
from nltk.corpus import stopwords

# Init Objects
tokenizer = RegexpTokenizer(r'\w+')
en_stopwords = set(stopwords.words('english'))
ps = PorterStemmer()

def getStemmedReview(review):

    review = review.lower()
    review = review.replace("

"," ")

    #Tokenize
    tokens = tokenizer.tokenize(review)
    new_tokens = [token for token in tokens if token not in en_stopwords]
    stemmed_tokens = [ps.stem(token) for token in new_tokens]

    cleaned_review = ' '.join(stemmed_tokens)

    return cleaned_review

    x_clean = [getStemmedReview(i) for i in x]

    from sklearn.feature_extraction.text import CountVectorizer

cv = CountVectorizer()
x_vec = cv.fit_transform(x_clean).toarray()
print(x_vec)
print(x_vec.shape)


import pandas as pd
import dash
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objs as go
import plotly.express as px
from dash.dependencies import Output, Input
import dash_bootstrap_components as dbc
from utils import load_dataset

from app import app

df = load_dataset.load_covid()
df = df.rename(index=str, columns={"location": "Country", "total_cases": "Total_cases",
                                   "date": "Date", "total_deaths": "Total_deaths",
                                   "stringency_index": "Stringency_index", "population": "Pop"})

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
#app = dash.Dash(__name__, external_stylesheets =[dbc.themes.DARKLY])

all_location = df.Country.dropna().unique()

layout = html.Div([
        dbc.Row(dbc.Col(html.H1('Influence of restrictions on total deaths and cases of Covid-19', style={'text-align': 'center'}),
                        ),
                ),
        dbc.Row(dbc.Col(html.H5("Select at least two countries:"),
                        ),
                ),
        dbc.Row(dbc.Col(dcc.Dropdown(
            id='country-dropdown',
            options=[{'label': i, 'value': i} for i in all_location],
            multi=True,
            value=['Afghanistan', "France"],
            clearable = False,
            style= { "hight": "100px",'color': '#212121', 'background-color': '#212121', "font-size":"24px"}
                                   ),
                        width={'size': 4, 'offset': 0},
                        ),
                ),
        dbc.Row(dbc.Col(dcc.Graph(id='timeseries-graph', figure={},
                   config={
                       'staticPlot': False,  # True, False
                       'scrollZoom': True,  # True, False
                       'doubleClick': 'reset',  # 'reset', 'autosize' or 'reset+autosize', False
                       'showTips': True,  # True, False
                       'displayModeBar': True,  # True, False, 'hover'
                       'watermark': True,
                   }
                                ),
            width={'size': 6, 'offset': 3},
                        ),
                ),
        dbc.Row(
            [
                dbc.Col(dcc.Graph(id="pie-graph", figure={}),
                        width=8, lg={'size': 3,  "offset": 2, 'order': 'first'}
                        ),
                dbc.Col(dcc.Graph(id="pie-graph1", figure={}),
                        width=4, lg={'size': 3,  "offset": 2, 'order': 'last'}
                        ),
            ]
                )
])

@app.callback(
    Output('timeseries-graph', 'figure'),
    [Input('country-dropdown', 'value')])
def update_graph(country_values):
    dff = df.loc[df['Country'].isin(country_values)]
    fig = px.line(dff, x='Date', y='Stringency_index', color='Country',
                  custom_data=['Country', 'Total_cases', 'Total_deaths', "Pop"])
    fig.update_traces(mode='lines+markers')
    fig.update_layout(plot_bgcolor= 'rgb(17,17,17)', paper_bgcolor ='#3E4449', font_color="white", font_size= 16)
    return fig

@app.callback(
    Output("pie-graph", "figure"),
    [Input("timeseries-graph", "hoverData"),
     #Input("timeseries-graph", "clickData"),
     #Input("timeseries-graph", "selectedData"),
     Input("country-dropdown", "value")])
def generate_chart(hov_data, country_values):
    if hov_data is None:
        dff1 = df[df.Country.isin(country_values)]
        dff1 = dff1[dff1.Date == "2020-10-11"]
        fig1 = px.pie(dff1, values="Total_cases", names="Country", color='Country')
        fig1.update_layout(plot_bgcolor='rgb(17,17,17)', paper_bgcolor ='#3E4449', font_color="white")
        return fig1
    else:
        print(f'hover data: {hov_data}')
        dff1 = df[df.Country.isin(country_values)]
        hov_date = hov_data['points'][0]['x']
        dff1 = dff1[dff1.Date == hov_date]
        fig2 = px.pie(dff1, values='Total_cases', names='Country', color='Country', title=f'Total cases: {hov_date}')
        fig2.update_layout(plot_bgcolor='rgb(17,17,17)', paper_bgcolor ='#3E4449', font_color="white")
        return fig2

@app.callback(
    Output("pie-graph1", "figure"),
    [Input("timeseries-graph", "hoverData"),
     #Input("timeseries-graph", "clickData"),
     #Input("timeseries-graph", "selectedData"),
     Input("country-dropdown", "value")])
def generate_chart(hov_data, country_values):
    if hov_data is None:
        dff1 = df[df.Country.isin(country_values)]
        dff1 = dff1[dff1.Date == "2020-10-11"]
        fig1 = px.pie(dff1, values="Total_deaths", names="Country", color='Country')
        fig1.update_layout(plot_bgcolor='rgb(17,17,17)', paper_bgcolor ='#3E4449', font_color="white")
        return fig1
    else:
        print(f'hover data: {hov_data}')
        dff1 = df[df.Country.isin(country_values)]
        hov_date = hov_data['points'][0]['x']
        dff1 = dff1[dff1.Date == hov_date]
        fig2 = px.pie(dff1, values='Total_deaths', names='Country', color='Country', title=f'Total deaths: {hov_date}')
        fig2.update_layout(plot_bgcolor='rgb(17,17,17)', paper_bgcolor ='#3E4449', font_color="white")
        return fig2


import  requests
from bs4 import BeautifulSoup

def getPageString(url):
    data = requests.get(url)
    bsObj = BeautifulSoup(data.content, "html.parser")

    print(bsObj)

    return data.content

url = "https://www.coupang.com/np/categories/186764"

print(getPageString(url))


from map_generator import MapGenerator
from breadth_first import breadthFirst
import numpy as np
import pygame
import random
def main():
    # here pygame is initialized
    pygame.init()

    # then we take some user input for the map width, map height, and number of points
    print('map sizes above 200x125 may cause lag')
    mapWidth = int(input("Enter map width: "))
    mapHeight = int(input("Enter map height: "))
    points = int(input("Enter number of points: "))

    # here we set the size scaler which is the size of each node in the pygame window
    sizeScaler = 5

    # here we set the pygame window and the caption
    screen = pygame.display.set_mode((mapWidth * sizeScaler, mapHeight * sizeScaler))
    pygame.display.set_caption("Breadth First")

    # here we initialize the map generator and the breadth first algorithm
    map_generator = MapGenerator(mapWidth, mapHeight, points)
    bfs = breadthFirst(map_generator.generateMap(), [0, 0], [mapWidth - 1, mapHeight - 1])

    # here we define the draw_map function which draws the map
    def draw_map():
        # first the screen is filled with black
        screen.fill((0, 0, 0))
        # then we loop through the map and draw each node
        for x in range(mapWidth):
            for y in range(mapHeight):
                # if the map node is 255, we draw a white node to represent an unwalkable node
                if map_generator.map[x][y] == 255:
                    pygame.draw.rect(screen, (255, 255, 255), (x * sizeScaler, y * sizeScaler, sizeScaler, sizeScaler))
                # otherwise we draw a black node to represent a walkable node
                else:
                    pygame.draw.rect(screen, (0, 0, 0), (x * sizeScaler, y * sizeScaler, sizeScaler, sizeScaler))

        # and here we draw the start and end nodes
        pygame.draw.rect(screen, (0, 255, 0), (bfs.start[0] * sizeScaler, bfs.start[1] * sizeScaler, sizeScaler, sizeScaler))
        pygame.draw.rect(screen, (255, 0, 0), (bfs.end[0] * sizeScaler, bfs.end[1] * sizeScaler, sizeScaler, sizeScaler))

        # and update the pygame window
        pygame.display.update()

    # here we define the draw_path function which draws the path
    def draw_path(path):
        # here we loop through the path-list and draw each node
        for node in path:
            pygame.draw.circle(screen, (255, 255, 0), (node[0] * sizeScaler + sizeScaler // 2, node[1] * sizeScaler + sizeScaler // 2), sizeScaler // 3)
        # and update the pygame window
        pygame.display.update()

    # here we draw the map and update the pygame window
    draw_map()
    pygame.display.update()

    # here we set the running boolean to true
    running = True

    # this is the main loop which runs until the user closes the pygame window
    while running:
        # here we check all the events in pygame
        for event in pygame.event.get():
            # if the pygame window is closed, we set the running boolean to false
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_q:
                running = False
            # if the user presses the space bar, we generate a new map and find a new path
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                # first we generate new start and end nodes on nodes that are walkable
                start = [random.randint(0, mapWidth - 1), random.randint(0, mapHeight - 1)]
                while map_generator.map[start[0]][start[1]] == 255:
                    start = [random.randint(0, mapWidth - 1), random.randint(0, mapHeight - 1)]
                end = [random.randint(0, mapWidth - 1), random.randint(0, mapHeight - 1)]
                while map_generator.map[end[0]][end[1]] == 255:
                    end = [random.randint(0, mapWidth - 1), random.randint(0, mapHeight - 1)]
                # then we generate a new map and run the breadth first algorithm
                bfs = breadthFirst(map_generator.generateMap(), start, end)
                # then we draw the map and update the pygame window
                draw_map()
                pygame.display.update()
        # if the algorithm has not found the end node, we find the path
        if not bfs.found:
            bfs.findPath()
            draw_path(bfs.path)

    # here we quit pygame
    pygame.quit()

# here we run the main function
if __name__ == '__main__':
    main()


import pandas as pd
import os
from monitor_app import SeleniumMonitor
from monitor_app import *
import jmespath
# os.environ["no_proxy"]="*"

pd.options.display.max_columns = None

# Temporal CSV file
TEMPORAL_FILE_NAME = "temporal_file.csv"

# CoinGecko asset ids that will be monitored by the script ("id" field must be provided)
SYMBOLS = os.environ["SYMBOLS"].split(",")

# Threshold value after which an alert message will be sent
THRESHOLD = float(os.environ["THRESHOLD"])

# Number of minutes that the script will take into account for Moving Averaging process
LOOKBACK_MINUTES = int(os.environ["LOOKBACK_MINUTES"])

# Time interval between each consequent alerts (e.g., %5 change continues for 60 secs but the alert should be sent once instead of 30 repetitions.)
# Send the alert every <ALERT_REPEAT_CYCLE_FREQ> sencods.
ALERT_REPEAT_CYCLE_FREQ = int(os.environ["ALERT_REPEAT_CYCLE_FREQ"])

# chromedriver.exe path
CHROME_EXE_PATH = os.environ["CHROME_EXE_PATH"]

def run_monitor():
    try:
        monitor_obj = SeleniumMonitor(threshold=THRESHOLD, executable_path=CHROME_EXE_PATH)
        monitor_obj.start_monitoring()
    except Exception as exc:
        monitor_obj.terminate_session()
        raise exc

if __name__ == "__main__":
    monitor = RequestMonitor()
    monitor.run_request_monitoring()


from Bio.PDB import PDBParser, PDBIO, Select, PDBList

class ProteinOnly(Select):
    def accept_residue(self, residue):
        hetero_flag = residue.get_id()[0].strip()
        if hetero_flag in ["W", "H", "L"]:
            return False
        if hetero_flag == "M":
            return False
        if residue.get_resname() not in ["ALA", "ARG", "ASN", "ASP", "CYS", "GLU", "GLN", "GLY", "HIS", "ILE", "LEU", "LYS", "MET", "PHE", "PRO", "SER", "THR", "TRP", "TYR", "VAL"]:
            return False
        return True

def remove_ligands(pdb_code, output_file):
#      Download PDB file
    pdbl = PDBList()
    pdb_file = pdbl.retrieve_pdb_file(pdb_code, file_format='pdb')

    # Parse the input PDB file
    parser = PDBParser(QUIET=True)
    structure = parser.get_structure(pdb_code, pdb_file)

    # Remove hetero atoms and non-amino acid residues
    io = PDBIO()
    io.set_structure(structure)
    io.save(output_file, select=ProteinOnly())
    print(f"Ligands removed from {pdb_code}. Output saved as {output_file}.")

# Example usage
remove_ligands("7pom", "7pom.pdb")


"""
-------------------------------------------------------------------------------
Name: ICS2O-CPT 2021.py
Purpose: To demostrate knowledge on certain aspects of coding.

Author: Schwarzenberg.Maximilian

Created: 23/03/2021
------------------------------------------------------------------------------
"""

import pygame

# Define some colors
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
DRED = (139,0,0)
ORANGE = (255,165,0)
YELLOW = (255,255,0)
GREEN = (0,255,0)
DGREEN = (49,99,0)
DDGREEN = (0,128,0)
BLUE = 	(0,0,255)
LBLUE =	(154,203,255)
PURPLE = (128,0,128)

pygame.init()

button_colour1 = GREEN
button_colour2 = RED
text_colour1 = BLACK
text_colour2 = BLACK

font = pygame.font.SysFont('Courier ', 30, True, False)
font1 = pygame.font.SysFont('Courier ', 75, True, False)
font2 = pygame.font.SysFont('Courier ', 60, True, True)

# Set the width and height of the screen [width, height]
size = (800, 600)
screen = pygame.display.set_mode(size)

player_avatar = 1
plyr_changed = False

"""
 Scene number that controls the scene
0 = Title screen
1 = Gameplay
2 = Pause screen
3 = Game over screen
"""
scene = 0

 # The score that the player changes
score = 0
point_given = False
level = 1
next_level = 0

 # Loading images
cermit = pygame.image.load("cermit1.png").convert_alpha()
bingus = pygame.image.load("bingus.png").convert_alpha()
bee = pygame.image.load("bee.png").convert_alpha()
virus_image = pygame.image.load("virus.png").convert_alpha()
day_texture = pygame.image.load("day_sky.png").convert()
day_texture2 = pygame.image.load("day_sky2.png").convert()
after_texture = pygame.image.load("afternoon_sky.png").convert()
after_texture2 = pygame.image.load("afternoon_sky2.png").convert()
night_texture = pygame.image.load("night_sky.png").convert()
night_texture2 = pygame.image.load("night_sky2.png").convert()
grass_texture1 = pygame.image.load("grass-texture.png").convert()
grass_texture2 = pygame.image.load("grass-texture2.png").convert()
grass_texture3 = pygame.image.load("grass-texture3.png").convert()
arrow = pygame.image.load("long-arrow-left.png").convert_alpha()
iamspeed = pygame.image.load("iamspeed.png").convert_alpha()

pygame.display.set_caption("ICS2O-CPT 2021")

 #Loop until the user clicks the close button.
done = False

 # Used to manage how fast the screen updates
clock = pygame.time.Clock()

 # Positions of the parts
plyr_x = 100
plyr_y = 461
plyr_width = 56
plyr_height = 64

virus_x = 960
virus_box_close = 960
virus_box_far = virus_box_close + 50
grass_x = 0
sky_x = 0
grass_x2 = 1600
sky_x2 = 1600
speed_x = 3200

 # Speed of the parts
speed_virus = 7
speed_grass = 7
speed_sky = 0.7

hack = False
h_given = False
level_7 = False

 # Mouse and button click detection
button_pressed = False
mouse_click = False

 # Initializing the jumping variables
isJump = False
jumpcount = 9

# -------- Main Program Loop -----------
while not done:
    # --- Main event loop
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # Flag that we are done so we exit this loop
        elif plyr_y > 461:
          plyr_y += 6
        elif event.type == pygame.KEYDOWN:
            print("k_down")
        elif event.type == pygame.KEYUP:
            print("k_up")
            plyr_changed = False
        keys = pygame.key.get_pressed()
        if event.type == pygame.MOUSEBUTTONDOWN:
            print("mouse button down")
            mouse_click = True
        if event.type == pygame.MOUSEBUTTONUP:
            print("mouse button up")
            mouse_click = False
        if keys[pygame.K_q] and h_given == False:
            hack = not(hack)
            h_given = True
            print(" hack: " + str(hack))

     # mouse variables
    mouse_position = pygame.mouse.get_pos()
    mouse_x = mouse_position[0]
    mouse_y = mouse_position[1]
    #print(mouse_position)

     # buttons in the scenes
    if scene == 0:
      if mouse_x > 50 and mouse_y > 350 and mouse_x < 375 and mouse_y < 425:
        button_colour1 = DDGREEN
        text_colour1 = WHITE
        if mouse_click == True:
          print("stareted game")
          scene = 1
      if mouse_x < 50 or mouse_y < 350 or mouse_x > 375 or mouse_y > 425:
        button_colour1 = GREEN
        text_colour1 = BLACK
      if mouse_x > 425 and mouse_y > 350 and mouse_x < 750 and mouse_y < 425:
        button_colour2 = DRED
        text_colour2 = WHITE
        if mouse_click == True:
          print("quit game")
          done = True
      if mouse_x < 425 or mouse_y < 350 or mouse_x > 750 or mouse_y > 425:
        button_colour2 = RED
        text_colour2 = BLACK

    if scene == 1:
      if mouse_x < 52 and mouse_y < 52 and mouse_click == True:
          print("paused game")
          scene = 2

    if scene == 2:
      if mouse_x > 50 and mouse_y > 350 and mouse_x < 375 and mouse_y < 425:
        button_colour1 = DDGREEN
        text_colour1 = WHITE
        if mouse_click == True:
          print("unpaused game")
          scene = 1
      if mouse_x < 50 or mouse_y < 350 or mouse_x > 375 or mouse_y > 425:
        button_colour1 = GREEN
        text_colour1 = BLACK
      if mouse_x > 425 and mouse_y > 350 and mouse_x < 750 and mouse_y < 425:
        button_colour2 = DRED
        text_colour2 = WHITE
        if mouse_click == True:
          print("quit to title")
          scene = 0
      if mouse_x < 425 or mouse_y < 350 or mouse_x > 750 or mouse_y > 425:
        button_colour2 = RED
        text_colour2 = BLACK

    if scene == 3:
      if mouse_x > 50 and mouse_y > 350 and mouse_x < 375 and mouse_y < 425:
        button_colour1 = DDGREEN
        text_colour1 = WHITE
        if mouse_click == True:
          print("stareted game")
          virus_x = 960
          grass_x = 0
          sky_x = 0
          grass_x2 = 1600
          sky_x2 = 1600
          speed_x = 3200
          speed_virus = 7
          virus_box_close = 960
          virus_box_far = virus_box_close + 50
          speed_grass = 7
          speed_sky = 0.7
          score = 0
          point_given = False
          level = 1
          hack = False
          h_given = False
          scene = 1
      if mouse_x < 50 or mouse_y < 350 or mouse_x > 375 or mouse_y > 425:
        button_colour1 = GREEN
        text_colour1 = BLACK
      if mouse_x > 425 and mouse_y > 350 and mouse_x < 750 and mouse_y < 425:
        button_colour2 = DRED
        text_colour2 = WHITE
        if mouse_click == True:
          print("quit game")
          done = True
      if mouse_x < 425 or mouse_y < 350 or mouse_x > 750 or mouse_y > 425:
        button_colour2 = RED
        text_colour2 = BLACK

    # --- Game logic should go here

     # The jumping function. This makes use of a quadratic formula to make the arc properly.
    if isJump == False:
      if keys[pygame.K_SPACE]: #to jump
        isJump = True
    if isJump == True:
      if jumpcount >= -9:
        negative = 1
        if jumpcount < 0:
          negative = -1
        plyr_y -= (jumpcount ** 2 ) / 2 * negative
        jumpcount -= 0.75
      else:
        isJump = False
        jumpcount = 9

    if score >= 10:
        speed_virus = 10
        speed_grass = 10
        speed_sky = 1.0
        level = 2

    if score >= 30:
        speed_virus = 15
        speed_grass = 15
        speed_sky = 1.5
        level = 3

    if score >= 50:
        speed_virus = 20
        speed_grass = 20
        speed_sky = 2.0
        level = 4

    if score >= 70:
        speed_virus = 25
        speed_grass = 25
        speed_sky = 2.5
        level = 5

    if score >= 90:
        speed_virus = 30
        speed_grass = 30
        speed_sky = 3.0
        level = 6

     # The last level 7
    if score >= 200 and scene == 1:
        speed_virus = 50
        speed_grass = 50
        speed_sky = 5.0
        level = 7
        level_7 = True
        next_level = "[X]"
        screen.fill(LBLUE)
        level_7 = font.render("U beat the game! Thank you for playing.", True, BLACK)
        screen.blit(level_7, [25, 200])
        credits_title = font2.render("CREDITS:", True, BLACK)
        screen.blit(credits_title, [225, 250])
        credits_1 = font.render("nikokouk", True, BLACK)
        screen.blit(credits_1, [255, 310])
        credits_2 = font.render("TheFaded", True, BLACK)
        screen.blit(credits_2, [255, 360])
        credits_3 = font.render("Cat Goes Brrr", True, BLACK)
        screen.blit(credits_3, [255, 410])
        credits_4 = font.render("TypeL3mon", True, BLACK)
        screen.blit(credits_4, [255, 460])
        credits_5 = font.render("Dan_Does_Music", True, BLACK)
        screen.blit(credits_5, [255, 510])
        credits_6 = font.render("and YOU for playing.", True, BLACK)
        screen.blit(credits_6, [255, 560])
        level_display = font.render("Level: " + str(level) + "", True, BLUE)
        screen.blit(level_display, [2, 64])
        if scene == 1:
            speed_x = speed_x - 50
        screen.blit(iamspeed, [speed_x, 410])
        if isJump == True:
            score = score + 2
            print("   score: " + str(score))
            point_given = True

    if score >= 10500:
        scene = 4
        if isJump == True:
            score = score + 2
            print("   score: " + str(score))
            point_given = True

    if score >= 11500:
        pygame.time.wait(3600)
        done = True

     # The function that makes the running seem infinite by looping
    if scene == 1:
      virus_x = virus_x - speed_virus
      if virus_x <= -160 and level_7 == False:
        virus_x = 1040
      virus_box_close = virus_x - speed_virus
      if virus_box_close <= -160 and level_7 == False:
        virus_box_close = 1040
      virus_box_far = virus_x + 50 - speed_virus
      if virus_box_far <= -80 and level_7 == False:
        virus_box_far = 1120
      sky_x = sky_x - speed_sky
      if sky_x <= -1600 and level_7 == False:
        sky_x = 1600
      sky_x2 = sky_x2 - speed_sky
      if sky_x2 <= -1600 and level_7 == False:
        sky_x2 = 1600
      grass_x = grass_x - speed_grass
      if grass_x <= -1600 and level_7 == False:
        grass_x = 1600
      grass_x2 = grass_x2 - speed_grass
      if grass_x2 <= -1600 and level_7 == False:
        grass_x2 = 1600
      addtoscore = font.render("+1", True, PURPLE)

     # The scoring function
    if virus_x > 100 and virus_x < 156 and point_given == False:
        if plyr_y < 461:
            score = score + 1
            print("   ")
            print("   score: " + str(score))
            print("   ")
            point_given = True
    if plyr_x + 50 > virus_box_close and plyr_x + 22 < virus_box_far and plyr_y > 370 and virus_x > 0 and hack == False:
        scene = 3
        point_given = True
    if virus_x < 100:
        point_given = False
        h_given = False

    if level == 1:
        next_level = 10 - score
    if level == 2:
        next_level = 30 - score
    if level == 3:
        next_level = 50 - score
    if level == 4:
        next_level = 70 - score
    if level == 5:
        next_level = 90 - score
    if level == 6:
        next_level = 200 - score

    # --- Drawing code should go here

     # The function to change characters (Press E to change in the title screen)
    if keys[pygame.K_e] and scene == 0 and plyr_changed == False:
        player_avatar = player_avatar + 1
        if player_avatar == 1 or player_avatar == 4 and plyr_changed == False:
            print("Player selected: Cermit")
        if player_avatar == 2 and plyr_changed == False:
            print("Player selected: Bingus")
        if player_avatar == 3 and plyr_changed == False:
            print("Player selected: Bee")
        plyr_changed = True
        if player_avatar > 3:
            player_avatar = 1

     # Drawing the visuals on the screen
    if scene == 0:
      screen.fill(LBLUE)
      pygame.draw.rect(screen, DGREEN, [50, 100, 700, 200])
      if player_avatar == 1:
        screen.blit(cermit, [122, 125])
      if player_avatar == 2:
        screen.blit(bingus, [122, 125])
      if player_avatar == 3:
        screen.blit(bee, [122, 125])
      title = font2.render("CERMIT VIRUS JUMPER", True, BLACK)
      screen.blit(title, [51, 160])
      subtitle = font.render("ICS2O1b CPT", True, BLACK)
      screen.blit(subtitle, [310, 250])
      pygame.draw.rect(screen, button_colour1, [50, 350, 325, 75])
      play = font1.render("PLAY", True, text_colour1)
      screen.blit(play, [115, 350])
      pygame.draw.rect(screen, button_colour2, [425, 350, 325, 75])
      quit = font1.render("QUIT", True, text_colour2)
      screen.blit(quit, [490, 350])
      howtoplay0 = font.render("How to play:", True, BLACK)
      screen.blit(howtoplay0, [50, 450])
      howtoplay1 = font.render("You are a player that jumps over viruses.", True, BLACK)
      screen.blit(howtoplay1, [50, 485])
      howtoplay2 = font.render("Press space to jump.", True, BLACK)
      screen.blit(howtoplay2, [50, 510])
    if scene == 1:
      if level == 1 or level == 2:
          screen.blit(day_texture, [sky_x, 0])
          screen.blit(day_texture2, [sky_x2, 0])
          screen.blit(grass_texture1, [grass_x, 525])
          screen.blit(grass_texture1, [grass_x2, 525])
      if level == 3 or level == 4:
          screen.blit(after_texture, [sky_x, 0])
          screen.blit(after_texture2, [sky_x2, 0])
          screen.blit(grass_texture2, [grass_x, 525])
          screen.blit(grass_texture2, [grass_x2, 525])
      if level == 5 or level == 6:
          screen.blit(night_texture, [sky_x, 0])
          screen.blit(night_texture2, [sky_x2, 0])
          screen.blit(grass_texture3, [grass_x, 525])
          screen.blit(grass_texture3, [grass_x2, 525])
      if player_avatar == 1:
        screen.blit(cermit, [plyr_x, plyr_y])
      if player_avatar == 2:
        screen.blit(bingus, [plyr_x, plyr_y])
      if player_avatar == 3:
        screen.blit(bee, [plyr_x, plyr_y])
      screen.blit(virus_image, [virus_x, 450])
      pygame.draw.rect(screen, RED, [0, 0, 52, 52])
      screen.blit(arrow, [1, 1])
      """
      pygame.draw.line(screen, BLACK, [plyr_x + 50, 280], [plyr_x + 50, 525], 1)
      pygame.draw.line(screen, BLACK, [plyr_x + 22, 280], [plyr_x + 22, 525], 1)
      pygame.draw.line(screen, BLACK, [virus_box_close, 370], [virus_box_close, 525], 1)
      pygame.draw.line(screen, BLACK, [virus_box_far, 370], [virus_box_far, 525], 1)
      pygame.draw.line(screen, BLACK, [virus_box_close, 370], [virus_box_far, 370], 1)
      pygame.draw.line(screen, BLACK, [virus_box_close, 450], [virus_box_far, 450], 1)
      #"""
      pygame.draw.rect(screen, WHITE, [60, 9, 204, 33])
      score_display = font.render("Score: " + str(score), True, BLACK)
      screen.blit(score_display, [60, 9])
      level_display = font.render("Level: " + str(level) + " ("+ str(next_level) + " point(s) left to level " + str(level + 1) + ".)", True, BLUE)
      screen.blit(level_display, [2, 64])
      if point_given == True:
        screen.blit(addtoscore, [274, 9])
      if virus_x < 0:
        screen.blit(addtoscore, [274, -25])

    if scene == 2:
      screen.fill(LBLUE)
      pygame.draw.rect(screen, DGREEN, [50, 100, 700, 200])
      title = font2.render("CERMIT VIRUS JUMPER", True, BLACK)
      screen.blit(title, [51, 160])
      subtitle = font.render("PAUSED", True, BLACK)
      screen.blit(subtitle, [335, 250])
      pygame.draw.rect(screen, button_colour1, [50, 350, 325, 75])
      play = font1.render("PLAY", True, text_colour1)
      screen.blit(play, [115, 350])
      pygame.draw.rect(screen, button_colour2, [425, 350, 325, 75])
      quit = font1.render("QUIT", True, text_colour2)
      screen.blit(quit, [490, 350])
    if scene == 3:
      screen.fill(LBLUE)
      pygame.draw.rect(screen, DGREEN, [50, 100, 700, 200])
      title = font2.render("CERMIT VIRUS JUMPER", True, BLACK)
      screen.blit(title, [51, 160])
      subtitle = font.render("GAME OVER", True, RED)
      screen.blit(subtitle, [335, 250])
      pygame.draw.rect(screen, button_colour1, [50, 350, 325, 75])
      play = font1.render("AGAIN", True, text_colour1)
      screen.blit(play, [100, 350])
      pygame.draw.rect(screen, button_colour2, [425, 350, 325, 75])
      quit = font1.render("QUIT", True, text_colour2)
      screen.blit(quit, [490, 350])
      virus_x = 960

    if scene == 4:
      screen.fill(BLACK)
      pygame.draw.rect(screen, DGREEN, [50, 100, 700, 200])
      why = font2.render("why are you still here?", True, WHITE)
      screen.blit(why, [1, 160])
      nothing = font.render("there is nothing left,", True, WHITE)
      screen.blit(nothing, [230, 360])
      leave = font.render("leave already.", True, WHITE)
      screen.blit(leave, [280, 510])

    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # --- Limit to 60 frames per second
    clock.tick(60)

# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()


class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        if not haystack and not needle:
            return 0
        elif needle in haystack:
            l = len(needle)
            for i in range(len(haystack)):
                if haystack[i:i+l] == needle:
                    return i
                else:
                    continue
        else:
            return -1


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

# Zope3 imports
from zope.interface import implements
# Security
from AccessControl import ClassSecurityInfo

# Archetypes & ATCT imports
from Products.Archetypes import atapi
from Products.ATContentTypes.content import schemata
from Products.ATContentTypes.content.base import ATCTContent
from Products.ATContentTypes.lib.historyaware import HistoryAwareMixin

# Product imports
from lx.demanda import demandaMessageFactory as _
from lx.demanda.interfaces.contents import IAtividade
from lx.demanda import config

# Schema definition
schema = ATCTContent.schema.copy() + atapi.Schema((
    atapi.StringField(
        name='projeto',
        required=False,
        searchable=True,
        widget=atapi.StringWidget(
            label=_(u"Projeto"),
            description=_(u"Nome do projeto"),
            size=30,)
    ),

    atapi.DateTimeField(
        name='data_inicio',
        required=False,
        searchable=True,
        widget=atapi.CalendarWidget(
            label=_(u'Data de início'),
        ),
    ),

    atapi.DateTimeField(
        name='data_fim',
        required=False,
        searchable=True,
        widget=atapi.CalendarWidget(
            label=_(u'Data de conclusão'),
        ),
    ),

    atapi.FloatField(
        name='duracao',
        required=True,
        searchable=True,
        widget=atapi.StringWidget(
            label=_(u"Duração"),
            description=_(u"Duração em horas da atividade."),
            size=10,)
    ),

    atapi.FloatField(
        name='quantHST',
        required=True,
        searchable=True,
        widget=atapi.StringWidget(
            label=_(u"QTDE de HST"),
            size=10,)
    ),

    atapi.TextField(
        name='observacao',
        required=False,
        searchable=True,
        default_output_type='text/x-html-safe',
        widget=atapi.RichWidget(
            label='Observação',
            rows=10,
        ),
    ),

),)

schema['description'].widget.visible['edit'] = 'invisible'
schema['excludeFromNav'].default = True

schemata.finalizeATCTSchema(schema)

class Atividade(ATCTContent, HistoryAwareMixin):
    """
    """
    security = ClassSecurityInfo()
    implements(IAtividade)

    meta_type = 'Atividade'
    portal_type = 'Atividade'

    _at_rename_after_creation = True
    schema = schema

atapi.registerType(Atividade, config.PROJECTNAME)


'''
Complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained.

Examples
"This is an example!" ==> "sihT si na !elpmaxe"
"double  spaces"      ==> "elbuod  secaps"
'''

def reverse_words(text):
    text = list(text)
    aux = []
    pos = 0
    for i in range(len(text)):
        if text[i] == ' ':
            for j in range(i-1,pos-1,-1):
                aux.append(text[j])

            aux.append(text[i])
            pos = i+1
        elif i == len(text)-1:
            for j in range(i,pos-1,-1):
                aux.append(text[j])

    return ''.join(aux)

print(reverse_words('hola mundo'))


from django.urls import path
from django.views.generic import TemplateView
from MainSiteApp import views
from django.contrib.auth.views import LogoutView
from django.conf.urls import handler404

from .views import *

handler404 = error_404_view

urlpatterns = [
    # Tasks
    path('', AllTaskView.as_view(), name="Dashboard"),
    path('create-comment/<int:id>', create_comment, name="CreateComment"),
    path('view-comments/<int:id>', view_comments, name="ViewComments"),
    path('list-tasks/', TaskList.as_view(), name="ListTasks"),
    path('detail-tasks/<pk>', TaskDetail.as_view(), name="DetailTasks"),
    path('create-task', CreateTask.as_view(), name="CreateTask"),
    path('delete-task/<pk>', DeleteTask.as_view(), name="DeleteTask"),
    path('edit-task/<pk>', EditTaskView.as_view(), name="EditTask"),
    # Usuarios
    path('login/', login_view, name="Login"),
    path('register', register, name="Register"),
    path('logout/', LogoutView.as_view(next_page="Login"), name="Logout"),
    path('edit-perfil/', edit_perfil, name="EditPerfil"),
    path('add-avatar/', add_avatar, name="AddAvatar"),
    path('edit-avatar/', edit_avatar, name="EditAvatar"),

    path('about/', TemplateView.as_view(template_name='about.html'), name='About'),
    path('help/', help, name='Help'),
    # Mensajeria
    path('enviar-mensaje/', views.enviar_mensaje, name='enviar_mensaje'),
    path('conversations/', views.conversation_list, name='conversation_list'),
    path('conversation/<int:conversation_id>/', views.conversation_detail, name='conversation_detail'),
    path('update_comment_state/<int:comment_id>/<str:comment_state>/', update_comment_state, name='update_comment_state'),
]


def operation(s,mode,op):
    if(mode == 'T'):
        s = s + op
    elif(mode == 'U'):
        undo = int(op)
        s = s[:-undo]
    elif(mode == 'Q'):
        print(s[int(op) - 1])
    return s

t = int(input())
s = ''
for i in range(0,t):
    op = input().split()
    s = operation(s,op[0],op[1])


class Match:
    def __init__(self, player1, player2):
        """
        Inicializamos la clase asignandole los correspondientes caracteres y oponente a cada jugador
        Por simplicidad el player1 siempre sera 'x'
        """
        player1.char = "x"
        player2.char = "o"
        player1.opponent = player2

        self._players = {"x": player1, "o": player2}
        self._round_robbin = [player1, player2]

    @property
    def next_player(self):
        """
        Propiedad de la clase que utilizaremos para saber los turnos de cada jugador.
        Devuelve el jugador al cual le corresponderia jugar
        """
        next_one = self._round_robbin[0]
        self._round_robbin.reverse()
        return next_one

    def get_player(self, char):
        """
        Devuelve el jugador con el caracter que se le envia como parametro
        """
        return self._players[char]

    def get_winner(self, board):
        """
        Dado un tablero, evalua que jugador (Player1 o Player2, resulta vencedor).
        En caso que no haya vencedor, devuelve None
        """
        if board.is_victory("x"):
            return self.get_player("x")

        elif board.is_victory("o"):
            return self.get_player("o")

        else:
            return None

    def is_match_over(self):
        """
        Le preguntamos al usuario si quiere jugar otra partida
        """
        finish = True
        while True:
            answer = input("Quieres jugar otra partida? (Y/N)").upper()
            if answer == "Y":
                finish = False
                break
            elif answer == "N":
                break
        return finish


import multiprocessing
import traceback

import numpy as np
import cv2
from shapely.geometry import LineString, Polygon
from shapely.geometry.polygon import orient
import math
from colicoords import Data, Cell, config
from multiprocessing import Pool
import psutil
import warnings
import pickle
from skimage import exposure
from napari.utils.notifications import show_info

def normalize99(X):
    """ normalize image so 0.0 is 0.01st percentile and 1.0 is 99.99th percentile """

    if np.max(X) > 0:
        X = X.copy()
        v_min, v_max = np.percentile(X[X != 0], (1, 99))
        X = exposure.rescale_intensity(X, in_range=(v_min, v_max))

    return X

def rescale01(x):
    """ normalize image from 0 to 1 """

    if np.max(x) > 0:
        x = (x - np.min(x)) / (np.max(x) - np.min(x))

    return x

def find_contours(img):
    # finds contours of shapes, only returns the external contours of the shapes
    contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)
    return contours

def rotate_contour(cnt, angle=90, units="DEGREES"):
    x = cnt[:, :, 1].copy()
    y = cnt[:, :, 0].copy()

    x_shift, y_shift = sum(x) / len(x), sum(y) / len(y)

    # Shift to origin (0,0)
    x = x - int(x_shift)
    y = y - int(y_shift)

    # Convert degrees to radians
    if units == "DEGREES":
        angle = math.radians(angle)

    # Rotation matrix multiplication to get rotated x & y
    xr = (x * math.cos(angle)) - (y * math.sin(angle)) + x_shift
    yr = (x * math.sin(angle)) + (y * math.cos(angle)) + y_shift

    cnt[:, :, 0] = yr
    cnt[:, :, 1] = xr

    shift_xy = [x_shift[0], y_shift[0]]

    return cnt, shift_xy

def rotate_image(image, shift_xy, angle=90):
    x_shift, y_shift = shift_xy

    (h, w) = image.shape[:2]

    # Perform image rotation
    M = cv2.getRotationMatrix2D((y_shift, x_shift), angle, 1.0)
    image = cv2.warpAffine(image, M, (w, h))

    return image, shift_xy

def rotate_model(model, shift_xy, angle=-90, units="DEGREES"):
    x = model[:, 1].copy()
    y = model[:, 0].copy()

    x_shift, y_shift = shift_xy[0], shift_xy[1]

    # Shift to origin (0,0)
    x = x - x_shift
    y = y - y_shift

    # Convert degrees to radians
    if units == "DEGREES":
        angle = math.radians(angle)

    # Rotation matrix multiplication to get rotated x & y
    xr = (x * math.cos(angle)) - (y * math.sin(angle)) + x_shift
    yr = (x * math.sin(angle)) + (y * math.cos(angle)) + y_shift

    model[:, 0] = yr
    model[:, 1] = xr

    return model

def get_cell_images(self, image, mask, mask_ids=None):
    if mask_ids == None:
        mask_ids = np.unique(mask)

    cell_data = {}

    for i in range(len(mask_ids)):
        mask_id = mask_ids[i]

        if mask_id != 0:
            cell_image = image.copy()

            cell_mask = np.zeros(mask.shape, dtype=np.uint8)
            cell_mask[mask == mask_id] = 1

            inverted_cell_mask = np.zeros(mask.shape, dtype=np.uint8)
            inverted_cell_mask[mask != 0] = 1
            inverted_cell_mask[mask == mask_id] = 0

            cnt = find_contours(cell_mask)[0]

            x, y, w, h = cv2.boundingRect(cnt)

            if h > w:
                vertical = True
                cell_mask = np.zeros(mask.shape, dtype=np.uint8)
                cnt, shift_xy = rotate_contour(cnt, angle=90)
                cell_image, shift_xy = rotate_image(cell_image, shift_xy, angle=90)
                inverted_cell_mask, shift_xy = rotate_image(inverted_cell_mask, shift_xy, angle=90)
                cv2.drawContours(cell_mask, [cnt], -1, 1, -1)
            else:
                vertical = False
                shift_xy = None

            x, y, w, h = cv2.boundingRect(cnt)
            y1, y2, x1, x2 = y, (y + h), x, (x + w)

            m = 5

            edge = False

            if y1 - 5 > 0:
                y1 = y1 - 5
            else:
                y1 = 0
                edge = True

            if y2 + 5 < cell_mask.shape[0]:
                y2 = y2 + 5
            else:
                y2 = cell_mask.shape[0]
                edge = True

            if x1 - 5 > 0:
                x1 = x1 - 5
            else:
                x1 = 0
                edge = True

            if x2 + 5 < cell_mask.shape[1]:
                x2 = x2 + 5
            else:
                x2 = cell_mask.shape[1]
                edge = True

            h, w = y2 - y1, x2 - x1

            inverted_cell_mask = inverted_cell_mask[y1:y2, x1:x2]
            cell_mask = cell_mask[y1:y2, x1:x2]
            cell_image = cell_image[y1:y2, x1:x2]

            cell_image[inverted_cell_mask == 1] = 0
            cell_image = normalize99(cell_image)

            offset = [y1, x1]
            box = [y1, y2, x1, x2]

            cell_data[i] = dict(cell_image=cell_image, cell_mask=cell_mask, offset=offset, shift_xy=shift_xy, box=box, edge=edge, vertical=vertical, mask_id=mask_id, contour=cnt)

    return cell_data

def resize_line(mesh, mesh_length):
    distances = np.linspace(0, mesh.length, mesh_length)
    mesh = LineString([mesh.interpolate(distance) for distance in distances])

    return mesh

def line_to_array(mesh):
    mesh = np.array([mesh.xy[0][:], mesh.xy[1][:]]).T.reshape(-1, 1, 2)

    return mesh

def euclidian_distance(x1, y1, x2, y2):
    distance = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5

    return distance

def polyarea(x, y):
    return 0.5 * np.abs(np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1)))

def compute_line_metrics(mesh):
    steplength = euclidian_distance(mesh[1:, 0] + mesh[1:, 2], mesh[1:, 1] + mesh[1:, 3], mesh[:-1, 0] + mesh[:-1, 2], mesh[:-1, 1] + mesh[:-1, 3]) / 2

    steparea = []
    for i in range(len(mesh) - 1):
        steparea.append(polyarea([*mesh[i:i + 2, 0], *mesh[i:i + 2, 2][::-1]], [*mesh[i:i + 2, 1], *mesh[i:i + 2, 3][::-1]]))

    steparea = np.array(steparea)

    d = euclidian_distance(mesh[:, 0], mesh[:, 1], mesh[:, 2], mesh[:, 3])
    stepvolume = (d[:-1] * d[1:] + (d[:-1] - d[1:]) ** 2 / 3) * steplength * math.pi / 4

    return steplength, steparea, stepvolume

def get_colicoords_mesh(cell, dat, statistics=False, pixel_size=1):
    offset, vertical, shift_xy = dat["offset"], dat["vertical"], dat["shift_xy"]

    numpoints = 20
    t = np.linspace(cell.coords.xl, cell.coords.xr, num=numpoints)
    a0, a1, a2 = cell.coords.coeff

    x_top = t + cell.coords.r * ((a1 + 2 * a2 * t) / np.sqrt(1 + (a1 + 2 * a2 * t) ** 2))
    y_top = a0 + a1 * t + a2 * (t ** 2) - cell.coords.r * (1 / np.sqrt(1 + (a1 + 2 * a2 * t) ** 2))

    x_bot = t + - cell.coords.r * ((a1 + 2 * a2 * t) / np.sqrt(1 + (a1 + 2 * a2 * t) ** 2))
    y_bot = a0 + a1 * t + a2 * (t ** 2) + cell.coords.r * (1 / np.sqrt(1 + (a1 + 2 * a2 * t) ** 2))

    # Left semicirlce
    psi = np.arctan(-cell.coords.p_dx(cell.coords.xl))

    th_l = np.linspace(-0.5 * np.pi + psi, 0.5 * np.pi + psi, num=numpoints)
    cl_dx = cell.coords.r * np.cos(th_l)
    cl_dy = cell.coords.r * np.sin(th_l)

    cl_x = cell.coords.xl - cl_dx
    cl_y = cell.coords.p(cell.coords.xl) + cl_dy

    # Right semicircle
    psi = np.arctan(-cell.coords.p_dx(cell.coords.xr))

    th_r = np.linspace(0.5 * np.pi - psi, -0.5 * np.pi - psi, num=numpoints)
    cr_dx = cell.coords.r * np.cos(th_r)
    cr_dy = cell.coords.r * np.sin(th_r)

    cr_x = cr_dx + cell.coords.xr
    cr_y = cr_dy + cell.coords.p(cell.coords.xr)

    # generate differnet halfs
    x1 = np.concatenate((cl_x[::-1][numpoints // 2:], x_top, cr_x[::-1][:numpoints // 2]))
    y1 = np.concatenate((cl_y[::-1][numpoints // 2:], y_top, cr_y[::-1][:numpoints // 2]))

    mesh1 = np.array([x1, y1])

    x2 = np.concatenate((np.flip(cl_x[::-1][:numpoints // 2 - 1]), x_bot, np.flip(cr_x[::-1][numpoints // 2 - 1:])))
    y2 = np.concatenate((np.flip(cl_y[::-1][:numpoints // 2 - 1]), y_bot, np.flip(cr_y[::-1][numpoints // 2 - 1:])))

    mesh2 = np.array([x2, y2])
    mesh2 = np.fliplr(mesh2)

    length = mesh1.shape[1]

    model = np.hstack([mesh1, mesh2]).T

    model = np.append(model, [model[0]], 0)

    model[:, 0] += offset[1]
    model[:, 1] += offset[0]

    if vertical:
        model = rotate_model(model, shift_xy)

    mesh1 = model[:length + 1, :]
    mesh2 = model[length:, :]

    mesh = np.hstack((mesh1, np.flipud(mesh2))).reshape(-1, 4)

    left_line = mesh[:, :2]
    right_line = mesh[:, 2:]

    mid_line = (left_line + right_line) / 2

    distances, area, volume = compute_line_metrics(mesh)

    # q = np.vstack([left_line, right_line]).reshape(-1,2)
    polygon = Polygon(model)
    polygon = orient(polygon)

    boundingbox = np.asarray(polygon.bounds)

    boundingbox[0:2] = np.floor(boundingbox[0:2])
    boundingbox[2:4] = np.ceil(boundingbox[2:4])
    boundingbox[2:4] = boundingbox[2:4] - boundingbox[0:2]

    dat[
        "statistics"] = dict(colicoords=True, length=cell.length * pixel_size, radius=cell.radius * pixel_size, area=cell.area * pixel_size ** 2, circumference=cell.circumference * pixel_size, aspect_ratio=cell.length / (
                cell.radius * 2))

    dat["mask_id"] = dat["mask_id"]

    dat["oufti"] = dict(mesh=mesh, model=model, boundingbox=boundingbox, distances=distances, area=area, volume=volume)

    dat["refined_cnt"] = model.reshape(-1, 1, 2).astype(int)

    return dat

def colicoords_fit(dat, colicoords_channel='Mask', statistics=False, pixel_size=1):
    config.cfg.IMG_PIXELSIZE = 1000 * pixel_size

    results = {}

    try:
        cell_dat = np.load(dat["cell_images_path"], allow_pickle=True).item()

        dat = {**dat, **cell_dat}

        if dat['edge'] == False:
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore")

                cell_image = dat["cell_image"]

                data = Data()
                data.add_data(dat['cell_mask'], 'binary')

                for i in range(len(cell_image)):
                    data.add_data(cell_image[i], 'fluorescence', name=dat["channels"][i])

                cell = Cell(data)

                cell.optimize()

                if colicoords_channel != 'Mask':
                    cell.optimize(colicoords_channel)
                    cell.measure_r(colicoords_channel, mode='mid')

                results = get_colicoords_mesh(cell, dat, statistics, pixel_size)

                channel_list = sorted(list(cell.data.data_dict.keys()))

                results["ldist"] = {}

                for channel in channel_list:
                    if channel != 'binary':
                        ldist = get_cell_ldist(cell, channel)
                        results["ldist"][channel] = ldist

                results["cell"] = cell
                results["colicoords_channel"] = colicoords_channel

                results = {**dat, **results["statistics"]}

        else:
            results["cell"] = None

    except:
        print(traceback.format_exc())
        results["cell"] = None

    return results

def get_cell_ldist(cell, channel):
    nbins = config.cfg.L_DIST_NBINS
    sigma = config.cfg.L_DIST_SIGMA
    sigma_arr = sigma / cell.length

    x_arr, out_arr = cell.l_dist(nbins, data_name=channel, norm_x=True, sigma=sigma_arr)

    max_val = np.max(out_arr)

    if max_val > 0:
        out_arr = out_arr + np.flip(out_arr)

        out_arr -= np.min(out_arr)
        out_arr = out_arr / np.max(out_arr)

    else:
        out_arr = None

    return out_arr

def process_cell_ldist(cell_statistics):
    data = [dat["ldist"] for dat in cell_statistics if dat["cell"] != None]

    channels = np.unique([list(dat.keys()) for dat in data]).tolist()

    ldist_data = {}

    for channel in channels:
        ldist = [dat[channel] for dat in data if dat[channel] is not None]

        ldist = np.stack(ldist)

        ldist_mean = np.nanmean(ldist, axis=0)
        ldist_std = np.std(ldist, axis=0)

        channel_dat = {channel + " mean": ldist_mean, channel + " std": ldist_std}

        ldist_data = {**ldist_data, **channel_dat}

    return ldist_data

def run_colicoords(self, cell_data, colicoords_channel, statistics=False, pixel_size=1, progress_callback=None, multithreaded=True):
    from sys import getsizeof

    processes = multiprocessing.cpu_count() - 1

    free_memory = psutil.virtual_memory().available / 1e6
    cell_data_size = getsizeof(cell_data)

    # assumes spawned python processes use 500mb of RAM
    if processes > free_memory // 500:
        processes = int(free_memory // 500)

    if multithreaded:
        with Pool(processes=processes) as pool:
            iter = []

            def callback(*args):
                iter.append(1)
                progress = (len(iter) / len(cell_data)) * 100

                if progress_callback != None:
                    progress_callback.emit(progress)

                return

            results = [pool.apply_async(colicoords_fit, args=(i,), kwds={'colicoords_channel': colicoords_channel, 'statistics': statistics, 'pixel_size': pixel_size}, callback=callback) for i in
                       cell_data]

            try:
                results[-1].get()
            except:
                print(traceback.format_exc())
            else:
                cell_statistics = [r.get() for r in results]
                pool.close()
                pool.join()

    else:
        iter = []
        cell_statistics = []

        for i in range(len(cell_data)):
            dat = cell_data[i]

            stats = colicoords_fit(dat, colicoords_channel=colicoords_channel, statistics=statistics, pixel_size=pixel_size)

            cell_statistics.append(stats)

            iter.append(1)
            progress = (len(iter) / len(cell_data)) * 100

            if progress_callback != None:
                progress_callback.emit(progress)

    ldist_data = {}

    if statistics is True:
        ldist_data = process_cell_ldist(cell_statistics)

    colicoords_data = dict(cell_statistics=cell_statistics, ldist_data=ldist_data)

    return colicoords_data

def get_l_dist(cell_list, channel, force_symmetry=True):
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore")

        nbins = config.cfg.L_DIST_NBINS
        sigma = config.cfg.L_DIST_SIGMA
        sigma_arr = sigma / cell_list.length

        x_arr, out_arr = cell_list.l_dist(nbins, data_name=channel, norm_x=True, sigma=sigma_arr)

        x = x_arr[0]

        maxes = np.max(out_arr, axis=1)
        bools = maxes != 0
        n = np.sum(~bools)

        out_arr = out_arr[bools]

        if force_symmetry == True:
            out_arr = np.array([array + np.flip(array) for array in out_arr])

        a_max = np.max(out_arr, axis=1)
        out_arr = out_arr / a_max[:, np.newaxis]

        ldist_mean = np.nanmean(out_arr, axis=0)
        ldist_std = np.std(out_arr, axis=0)

    return ldist_mean, ldist_std

def process_colicoords(self, colicoords_data):
    colicoords_data = colicoords_data["cell_statistics"]

    current_fov = self.viewer.dims.current_step[0]
    colicoords_channel = self.cellpose_segchannel.currentText()

    image_stack = self.viewer.layers[colicoords_channel].data
    label_stack = self.classLayer.data
    mask_stack = self.segLayer.data

    image = image_stack[current_fov, :, :].copy()
    mask = mask_stack[current_fov, :, :].copy()
    label = label_stack[current_fov, :, :].copy()

    for i in range(len(colicoords_data)):
        dat = colicoords_data[i]

        if dat["cell"] != None:
            mask_id = dat["mask_id"]
            cnt = dat["refined_cnt"]

            new_mask = np.zeros_like(mask)
            cv2.drawContours(new_mask, [cnt], -1, 1, -1)

            current_label = np.unique(label[mask == mask_id])

            label[mask == mask_id] = 0
            mask[mask == mask_id] = 0

            mask[new_mask == 1] = mask_id
            label[new_mask == 1] = current_label

            mask_stack[current_fov, :, :] = mask
            label_stack[current_fov, :, :] = label

            self.segLayer.data = mask_stack
            self.classLayer.data = label_stack