import sys
from sklearn.ensemble import *
from sklearn.neighbors import *
from sklearn.svm import *
from sklearn.metrics import accuracy_score
import pandas as pd
import json

def check_int(x):
    if x.isdigit():
        return int(x)
    else:
        return x

def parse_model_str(model_str):
    pairs = model_str.split("::")
    model = pairs[0]
    pairs = [s.split("=") for s in pairs[1:]]
    pairs = [(p[0], check_int(p[1])) for p in pairs]

    d = {}
    d["params"] = dict(pairs)
    d["ctor"] = model

    return d

def construct_model(model_dict):

    ctor = globals()[model_dict["ctor"]]
    model = ctor(**model_dict["params"])

    return model

def get_data(x_file, y_file, split_idx_file):

    X_df = pd.read_csv(x_file)
    X_df.set_index("idx", inplace=True)

    y_df = pd.read_csv(y_file)
    y_df.set_index("idx", inplace=True)

    with open(split_idx_file, "r") as f:
        idx_dict = json.load(f)
    train_idx = idx_dict["train_idx"]
    test_idx = idx_dict["test_idx"]

    X_train = X_df.values[train_idx,:]
    y_train = y_df.values[train_idx,0]

    X_test = X_df.values[test_idx,:]
    y_test = y_df.values[test_idx,0]

    return X_train, y_train, X_test, y_test

if __name__=="__main__":

    x_file = sys.argv[1]
    y_file = sys.argv[2]
    split_idx_file = sys.argv[3]
    model_str = sys.argv[4]
    output_file = sys.argv[5]

    if len(sys.argv) == 7:
        best_file = sys.argv[6]
        with open(best_file, "r") as f:
            d = json.load(f)
            model_str = d["model_str"]

    model_dict = parse_model_str(model_str)
    model = construct_model(model_dict)

    X_train, y_train, X_test, y_test = get_data(x_file, y_file,
                                                split_idx_file)

    model.fit(X_train, y_train)

    y_pred = model.predict(X_test)

    acc = accuracy_score(y_test, y_pred)

    results = {"model": model_dict,
               "model_str": model_str,
               "score": acc
               }

    with open(output_file, "w") as f:
        json.dump(results, f)


# File:         model_trainer.py
# Date:         2023/07/13
# Description:  ModelTrainer class to train neural networks for Challenge #2

from os import path, makedirs
from typing import Optional
import sys

import numpy as np
from tqdm import tqdm
import torch
from torch.optim.adamw import AdamW
from torch.nn import Module
from torch.utils.data import Dataset, DataLoader, Subset, random_split
from torchvision.ops import nms

sys.path.append("./utils")
from coco_utils import get_coco_api_from_dataset
from coco_eval import CocoEvaluator
from engine import _get_iou_types

sys.path.append("./models")
from fasterrcnn_models import (
    FasterRcnnResnet50FpnV2,
    FasterRcnnMobileNetV3LargeFpn,
    FasterRcnnMobileNetV3Large320Fpn,
)

class ModelTrainer:
    """
    A training worker for neural network models used in challenge #2

    Loads an initial state of a neural network model, trains it using some parameters
    and a given dataset. After each epoch, the model weights and losses (training &
    validation) are exported in a checkpoint file.
    """

    def __init__(
        self,
        model: Module,
        dataset: Dataset,
        dataset_validation: Optional[Dataset],
        epochs_count: int,
        batch_size: int,
        learning_rate: int,
        workers_count: int,
        device: torch.device = torch.device("cpu"),
        load_checkpoint_file: str = "",
        model_name: str = "model",
        checkpoints_dir: str = "checkpoints",
    ) -> None:
        self.__model = model.to(device)
        self.__device = device

        self.__starting_epoch = 0
        self.__epochs_count = epochs_count
        self.__batch_size = batch_size
        self.__workers_count = workers_count

        self.__optimizer = AdamW(model.parameters(), lr=learning_rate)

        self.__epoch = 0
        self.__train_data_loader = None
        self.__validation_data_loader = None

        # Stored globally to keep track of all losses in each checkpoint
        self.__training_losses = []

        self.__model_name = model_name  # Checkpoints will be saved with this name
        self.__checkpoints_dir = path.abspath(checkpoints_dir)
        if not path.exists(self.__checkpoints_dir):
            makedirs(self.__checkpoints_dir)

        # Load training and validation datasets
        self.__dataset = dataset
        self.__dataset_validation = dataset_validation

        # Check if we actually have a validation dataset, otherwise create one
        if self.__dataset_validation is None:
            self.__dataset, self.__dataset_validation = self.__split_dataset(
                dataset, (0.8, 0.2), randomize=True
            )

        # Just before starting, load a checkpoint file if available
        if len(load_checkpoint_file) > 0:
            self.__load_checkpoint(load_checkpoint_file)

    def __split_dataset(
        self,
        dataset: Dataset,
        ratios: list[float],
        randomize: bool = False,
    ) -> list[Subset]:
        """
        Split a unique dataset into multiple subset datasets

        Parameters
        ----------
        dataset : Dataset
            Main dataset to split
        ratios : list[float]
            Ratios (normalized or not) defining the dataset split (variable length)
        randomize : bool, optional
            Split the main dataset randomly, by default False

        Returns
        -------
        list[Subset]
            Split datasets

        Raises
        ------
        ValueError
            Invalid splitting ratios
        """
        if len(ratios) == 0:
            raise ValueError("Ratios cannot be an empty list")
        if any(r < 0 for r in ratios):
            raise ValueError(f"Each ratio must be in ]0 - 1] ; got {ratios}")

        ratios_sum = sum(ratios)
        normalized_ratios = [r / ratios_sum for r in ratios]

        split_datasets = []

        if randomize:
            split_datasets = random_split(dataset, normalized_ratios)
        else:
            split_datasets = []
            start_idx = 0
            for r in normalized_ratios[:-1]:
                num_data = int(len(dataset) * r)  # type: ignore
                split_datasets.append(
                    Subset(dataset, range(start_idx, start_idx + num_data))
                )
                start_idx += num_data

            # Last subset takes the rest, not using last ratio due to possible rounding difference
            last_subset = Subset(dataset, range(start_idx, len(dataset)))
            split_datasets.append(last_subset)

        return split_datasets

    def run(self):
        """
        Main entry point to start training
        """
        # Prepare validation data (batch size should be 1 for inference)
        self.__validation_data_loader = DataLoader(
            self.__dataset_validation,
            batch_size=1,
            num_workers=self.__workers_count,
            collate_fn=collate_fn,
            drop_last=True,
        )

        # Training
        for self.__epoch in range(
            self.__starting_epoch,
            self.__starting_epoch + self.__epochs_count,
        ):
            # Prepare training data, reshuffle for each epoch
            self.__train_data_loader = DataLoader(
                self.__dataset,
                batch_size=self.__batch_size,
                num_workers=self.__workers_count,
                shuffle=True,
                collate_fn=collate_fn,
            )

            training_losses = self.__train()
            self.__validate()

            # Update the history of training & validation losses
            self.__training_losses.append(training_losses)

            # Save the checkpoint (including training & validation losses)
            self.__save_checkpoint()

    def __train(self) -> np.ndarray:
        """
        Train model weights and track performance

        Returns
        -------
        ndarray (batches x 5)
            Losses of each batch during this epoch
            (combined, classification, localization, objectness, region proposal)
        """
        losses = np.zeros((0, 5))

        # Training -> gradient computation, model outputs losses directly
        self.__model.train()

        # Iterate training data with status bar
        tqdm_iterator = tqdm(self.__train_data_loader, desc=f"Epoch {self.__epoch}")
        for _, (images, targets) in enumerate(tqdm_iterator):
            # Assign tensors to target computing device
            images = list(image.to(self.__device) for image in images)
            targets = [{k: v.to(self.__device) for k, v in t.items()} for t in targets]

            # Set gradient to zero to prevent gradient accumulation
            self.__optimizer.zero_grad()

            # Inference prediction by model
            loss_dict = self.__model(images, targets)

            # Obtain loss function and back propagate to obtain gradient
            combined_loss = sum(loss for loss in loss_dict.values())
            combined_loss.backward()

            # Manually track the individual losses (to print epoch summary)
            if (
                isinstance(self.__model, FasterRcnnResnet50FpnV2)
                or isinstance(self.__model, FasterRcnnMobileNetV3LargeFpn)
                or isinstance(self.__model, FasterRcnnMobileNetV3Large320Fpn)
            ):
                loss_classifier = loss_dict["loss_classifier"].item()
                loss_box_reg = loss_dict["loss_box_reg"].item()
                loss_objectness = loss_dict["loss_objectness"].item()
                loss_rpn_box_reg = loss_dict["loss_rpn_box_reg"].item()
            else:
                loss_classifier = loss_dict["classification"].item()
                loss_box_reg = loss_dict["bbox_regression"].item()
                loss_objectness = 0
                loss_rpn_box_reg = 0

            # Check if the losses are NaN, indicating issues with the gradient
            if (
                np.isnan(loss_classifier)
                or np.isnan(loss_box_reg)
                or np.isnan(loss_objectness)
                or np.isnan(loss_rpn_box_reg)
            ):
                raise RuntimeError("NaN loss during training")

            # Update the global losses array
            losses = np.vstack(
                [
                    losses,
                    [
                        combined_loss.item(),
                        loss_classifier,
                        loss_box_reg,
                        loss_objectness,
                        loss_rpn_box_reg,
                    ],
                ]
            )

            # Weight learning
            self.__optimizer.step()

            # Store performance metrics and update loss on status bar
            tqdm_iterator.set_postfix_str(f"Combined Loss: {combined_loss.item():.3e}")

        print(
            f"Epoch {self.__epoch}: Combined loss {np.mean(losses[:, 0]):.4f} ; "
            f"Classification loss {np.mean(losses[:, 1]):.4f} ; "
            f"Localization loss {np.mean(losses[:, 2]):.4f} ; "
            f"Objectness loss {np.mean(losses[:, 3]):.4f} ; "
            f"Region Proposal loss {np.mean(losses[:, 4]):.4f}"
        )

        return losses

    def __validate(self) -> None:
        """
        Validate model weights and track performance (no training)
        """
        # Convert to COCO format for Pytorch vision to compute mean Average Precision (mAP)
        coco = get_coco_api_from_dataset(self.__validation_data_loader.dataset)
        iou_types = _get_iou_types(self.__model)
        coco_evaluator = CocoEvaluator(coco, iou_types)

        # Evaluation mode (the network will output predictions instead of losses)
        self.__model.eval()

        # Turn off gradient to prevent autograd in backward pass, saves memory
        tqdm_iterator = tqdm(
            self.__validation_data_loader, desc=f"Validation {self.__epoch}"
        )
        with torch.no_grad():
            for images, targets in tqdm_iterator:
                # Assign tensors to target computing device
                images = list(image.to(self.__device) for image in images)
                # Targets will only be used for loss computation
                targets = [
                    {k: v.to(self.__device) for k, v in t.items()} for t in targets
                ]

                # Inference prediction by model and obtain predictions (instead of loss)
                outputs = self.__model(images)
                outputs = [{k: v for k, v in t.items()} for t in outputs]

                # Perform Non-Maximum Suppression (NMS) on the bounding boxes
                bboxes_idx_to_keep = nms(
                    boxes=outputs[0]["boxes"],
                    scores=outputs[0]["scores"],
                    iou_threshold=0.3,
                )
                final_outputs = [
                    {k: v[bboxes_idx_to_keep] for k, v in outputs[0].items()}
                ]

                # Compute mean Average Precision (mAP) using COCO evaluator
                res = {
                    target["image_id"].item(): output
                    for target, output in zip(targets, final_outputs)
                }
                coco_evaluator.update(res)

        # Print mean performance for this epoch
        coco_evaluator.synchronize_between_processes()
        coco_evaluator.accumulate()
        coco_evaluator.summarize()

    def __save_checkpoint(self) -> None:
        """
        Save the model weights, optimizer states, training losses and validation losses
        inside a checkpoint file.
        """
        target_checkpoint_path = path.join(
            self.__checkpoints_dir,
            f"{self.__model_name}_epoch-{self.__epoch}.pt",
        )
        torch.save(
            {
                "epoch": self.__epoch,
                "model_state_dict": self.__model.state_dict(),
                "optimizer_state_dict": self.__optimizer.state_dict(),
                "training_losses": self.__training_losses,
            },
            target_checkpoint_path,
        )

    def __load_checkpoint(self, checkpoint_file_path: str) -> None:
        """
        Load a checkpoint file (model weights, optimizer weights, training losses) to
        resume training a network.

        Parameters
        ----------
        checkpoint_file_path : str
            Absolute path to the checkpoint file to load

        Raises
        ------
        RuntimeError
            Invalid or inexistant checkpoint file
        """
        print(f"ModelTrainer: loading checkpoint {checkpoint_file_path}")

        # Make sure the target checkpoint file exists
        if not path.exists(checkpoint_file_path):
            raise RuntimeError(f"Checkpoint {checkpoint_file_path} does not exist.")
        if not path.isfile(checkpoint_file_path):
            raise (f"{checkpoint_file_path} is not a file.")

        checkpoint = torch.load(checkpoint_file_path, map_location=self.__device)
        self.__starting_epoch = checkpoint["epoch"] + 1
        self.__model.load_state_dict(checkpoint["model_state_dict"])
        self.__optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
        self.__training_losses = checkpoint["training_losses"]

def parameters_count(model: Module) -> tuple[int, int]:
    """
    Compute the numbers of parameters in a model

    Parameters
    ----------
    model : Module
        Neural network model

    Returns
    -------
    tuple[int, int]
        Total count of parameters, Trainable parameters count
    """
    return [
        sum(p.numel() for p in model.parameters()),
        sum(p.numel() for p in model.parameters() if p.requires_grad),
    ]

def collate_fn(batch):
    return tuple(zip(*batch))


# Import libraries
import random
import json
import pickle
import numpy as np
import tensorflow as tf
import nltk
from nltk.stem import WordNetLemmatizer

# Initialize WordNetLemmatizer for word lemmatization
lemmatizer = WordNetLemmatizer()

# Load intents from JSON file
with open('intents.json', 'r') as file:
    intents = json.load(file)

# Lists to store words, classes, and documents
words = []
classes = []
documents = []
ignoreLetters = ['?', '!', '.', ',']

# Preprocess the intents and patterns
for intent in intents['intents']:
    for pattern in intent['patterns']:
        # Tokenize the pattern into a list of words
        wordList = nltk.word_tokenize(pattern)
        words.extend(wordList)
        # Store the document as a tuple of word list and intent tag
        documents.append((wordList, intent['tag']))
        # Add the intent tag to classes if it's not already present
        if intent['tag'] not in classes:
            classes.append(intent['tag'])

# Normalize words by lemmatization and remove duplicates
words = {lemmatizer.lemmatize(word.lower())
         for word in words if word not in ignoreLetters}

classes = sorted(set(classes))

# Save words and classes to pickle files for later use
pickle.dump(list(words), open('words.pkl', 'wb'))
pickle.dump(classes, open('classes.pkl', 'wb'))

# Prepare training data in a format suitable for training a neural network
training = []
outputEmpty = [0] * len(classes)

for document in documents:
    bag = []
    wordPatterns = document[0]
    wordPatterns = [lemmatizer.lemmatize(
        word.lower()) for word in wordPatterns]
    for word in words:
        bag.append(1) if word in wordPatterns else bag.append(0)

    outputRow = list(outputEmpty)
    outputRow[classes.index(document[1])] = 1
    training.append(bag + outputRow)

# Shuffle the training data to ensure randomization
random.shuffle(training)
training = np.array(training)

# Split training data into input features (trainX) and output labels (trainY)
trainX = training[:, :len(words)]
trainY = training[:, len(words):]

# Create a sequential neural network model using Keras
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(
    128, input_shape=(len(trainX[0]),), activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(len(trainY[0]), activation='softmax'))

# Configure the optimizer and compile the model
sgd = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd, metrics=['accuracy'])

# Train the model on the prepared data
model.fit(trainX, trainY, epochs=200, batch_size=5, verbose=1)

# Save the trained model for later use
model.save('sudeep_model.keras')

# Training complete
print('Done Training')


from django.db.models import Q #allows for searches in multiple fields.
from xml.etree.ElementTree import Comment
from django.shortcuts import redirect, render, get_object_or_404

from .models import Post,Category
from .forms import CommentForm

# Create your views here.
def detail(request, category_slug, slug):
    post = get_object_or_404(Post, slug=slug, status=Post.ACTIVE)

    # to verify the input of the comment and save it only if it's valid
    if request.method == 'POST':  #checks if it's a post request which means data has been sent
        form= CommentForm(request.POST) #request.Post gets the data from the post command and sends it to the (comment object)form
        if form.is_valid():
            comment = form.save(commit=False)  # the comment form cannot save without pionting to the foreignkey which is the post(else crash), hence we save the form in a comment object
            comment.post=post  #get the post that is related to the comment and assign it to the foreignkey,else it will throw an error as the [post] attribute is a foreign key and must be known to properly link the comment
            comment.save()  #now save the comment object as all the fields are complete

            return redirect('post_detail',slug=slug)
    else:
        form = CommentForm()

    form = CommentForm()
    return render(request, 'blog/detail.html', {'post':post, 'form':form})

def category(request, slug):
    category = get_object_or_404(Category, slug=slug,)
    posts = category.posts.filter(status=Post.ACTIVE)

    return render(request, 'blog/category.html', {'category':category, 'posts':posts})

def search(request):
    query = request.GET.get('query','') #used to get parameters of the search feature
    posts = Post.objects.filter(status=Post.ACTIVE).filter(Q(title__icontains=query) | Q(intro__icontains= query) | Q(body__icontains= query)) #the icontains, negates the lowercase searching

    return render(request, 'blog/search.html', {'posts': posts, 'query':query})


from typing import List, Tuple

from bip_utils.bech32.bech32 import Bech32Const, Bech32Encodings, Bech32Utils
from bip_utils.bech32.bech32_base import Bech32BaseUtils, Bech32DecoderBase, Bech32EncoderBase
from bip_utils.utils.misc import BytesUtils

class SegwitBech32Const:
    """Class container for Segwit Bech32 constants."""

    # Separator
    SEPARATOR: str = Bech32Const.SEPARATOR
    # Checksum length
    CHECKSUM_STR_LEN: int = Bech32Const.CHECKSUM_STR_LEN
    # Minimum witness program length in bytes
    WITNESS_PROG_MIN_BYTE_LEN: int = 2
    # Maximum witness program length in bytes
    WITNESS_PROG_MAX_BYTE_LEN: int = 40
    # Witness version for Bech32 encoding
    WITNESS_VER_BECH32: int = 0
    # Witness version maximum value
    WITNESS_VER_MAX_VAL: int = 16
    # Accepted data lengths when witness version is zero
    WITNESS_VER_ZERO_DATA_BYTE_LEN: Tuple[int, int] = (20, 32)

class SegwitBech32Encoder(Bech32EncoderBase):
    """
    Segwit Bech32 encoder class.
    It provides methods for encoding to Segwit Bech32 format.
    """

    @classmethod
    def Encode(cls,
               hrp: str,
               wit_ver: int,
               wit_prog: bytes) -> str:
        """
        Encode to Segwit Bech32.

        Args:
            hrp (str)       : HRP
            wit_ver (int)   : Witness version
            wit_prog (bytes): Witness program

        Returns:
            str: Encoded address

        Raises:
            ValueError: If the data is not valid
        """
        return cls._EncodeBech32(hrp,
                                 [wit_ver] + Bech32BaseUtils.ConvertToBase32(wit_prog),
                                 SegwitBech32Const.SEPARATOR)

    @staticmethod
    def _ComputeChecksum(hrp: str,
                         data: List[int]) -> List[int]:
        """
        Compute the checksum from the specified HRP and data.

        Args:
            hrp (str)       : HRP
            data (list[int]): Data part

        Returns:
            list[int]: Computed checksum
        """
        encoding = (Bech32Encodings.BECH32
                    if data[0] == SegwitBech32Const.WITNESS_VER_BECH32
                    else Bech32Encodings.BECH32M)

        return Bech32Utils.ComputeChecksum(hrp, data, encoding)

class SegwitBech32Decoder(Bech32DecoderBase):
    """
    Segwit Bech32 decoder class.
    It provides methods for decoding Segwit Bech32 format.
    """

    @classmethod
    def Decode(cls,
               hrp: str,
               addr: str) -> Tuple[int, bytes]:
        """
        Decode from Segwit Bech32.

        Args:
            hrp (str) : Human readable part
            addr (str): Address

        Returns:
            tuple[int, bytes]: Witness version (index 0) and witness program (index 1)

        Raises:
            Bech32ChecksumError: If the checksum is not valid
            ValueError: If the bech32 string is not valid
        """

        # Decode string
        hrp_got, data = cls._DecodeBech32(addr,
                                          SegwitBech32Const.SEPARATOR,
                                          SegwitBech32Const.CHECKSUM_STR_LEN)
        # Check HRP
        if hrp != hrp_got:
            raise ValueError(
                f"Invalid format (HRP not valid, expected {hrp}, got {hrp_got})"
            )

        # Convert back from base32 (remove witness version)
        conv_data = Bech32BaseUtils.ConvertFromBase32(data[1:])

        # Check data length
        if (len(conv_data) < SegwitBech32Const.WITNESS_PROG_MIN_BYTE_LEN
                or len(conv_data) > SegwitBech32Const.WITNESS_PROG_MAX_BYTE_LEN):
            raise ValueError(f"Invalid format (witness program length not valid: {len(conv_data)})")
        # Check witness version
        wit_ver = data[0]
        if wit_ver > SegwitBech32Const.WITNESS_VER_MAX_VAL:
            raise ValueError(f"Invalid format (witness version not valid: {wit_ver})")
        if wit_ver == 0 and not len(conv_data) in SegwitBech32Const.WITNESS_VER_ZERO_DATA_BYTE_LEN:
            raise ValueError(f"Invalid format (length not valid: {len(conv_data)})")

        return wit_ver, BytesUtils.FromList(conv_data)

    @staticmethod
    def _VerifyChecksum(hrp: str,
                        data: List[int]) -> bool:
        """
        Verify the checksum from the specified HRP and converted data characters.

        Args:
            hrp  (str)      : HRP
            data (list[int]): Data part

        Returns:
            bool: True if valid, false otherwise
        """
        encoding = (Bech32Encodings.BECH32
                    if data[0] == SegwitBech32Const.WITNESS_VER_BECH32
                    else Bech32Encodings.BECH32M)

        return Bech32Utils.VerifyChecksum(hrp, data, encoding)


import numpy as np
import scipy.special as ss
import matplotlib.pyplot as plt
import neupy as ne
import pickle

class neuralNetwork:

    #TODO : add one more layer + dropout
    def __init__(self, inputnodes, hiddennodes, hiddennodes2, outputnodes, learningrate, epochs):
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.hnodes2 = hiddennodes2
        self.onodes = outputnodes
        self.lr = learningrate
        self.epochs = epochs
        self.wih = np.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes, self.inodes))
        self.whh2 = np.random.normal(0.0, pow(self.hnodes2, -0.5), (self.hnodes2, self.hnodes))
        self.wh2o = np.random.normal(0.0, pow(self.onodes, -0.5), (self.onodes, self.hnodes2))
        self.activation_function = lambda x: ss.expit(x)
        pass

    def train(self, inputs_list, targets_list):
        inputs = np.array(inputs_list, ndmin=2).T
        targets = np.array(targets_list, ndmin=2).T

        hidden_inputs = np.dot(self.wih, inputs)
        hidden_outputs = self.activation_function(hidden_inputs)

        hidden2_inputs = np.dot(self.whh2, hidden_outputs)
        hidden2_outputs = self.activation_function(hidden2_inputs)

        final_inputs = np.dot(self.wh2o, hidden2_outputs)
        final_outputs = self.activation_function(final_inputs)

        output_errors = targets - final_outputs
        # output_errors = self.cross_entropy(final_outputs, targets)
        hidden2_errors = np.dot(self.wh2o.T, output_errors)
        hidden_errors = np.dot(self.whh2.T,hidden2_errors)
        # input_errors = np.dot(self.wih,hidden_errors)

        self.wh2o += self.lr * np.dot((output_errors * final_outputs * (1.0 - final_outputs)), np.transpose(hidden2_outputs))
        self.whh2 += self.lr * np.dot((hidden2_errors * hidden2_outputs * (1.0 - hidden2_outputs)), np.transpose(hidden_outputs))
        self.wih += self.lr * np.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), np.transpose(inputs))
        pass

    def query(self, input_list):
        inputs = np.array(input_list, ndmin = 2).T

        hidden_inputs = np.dot(self.wih,inputs)
        hidden_outputs = self.activation_function(hidden_inputs)

        hidden2_inputs = np.dot(self.whh2,hidden_outputs)
        hidden2_outputs = self.activation_function(hidden2_inputs)

        final_inputs = np.dot(self.wh2o, hidden2_outputs)
        final_outputs = self.activation_function(final_inputs)
        # final_outputs = self.softmax(final_inputs)

        return final_outputs

    def deserialize(self,path):
        f = open(path, 'rb')
        weights = pickle.load(f)
        self.wih = weights[0]
        self.whh2 = weights[1]
        self.wh2o = weights[2]
        f.close()

    def train_mnist(self,path):
        file = load(path)
        for e in range(self.epochs):
                for record in file:
                    all_values = record.split(',')
                    inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
                    targets = np.zeros(self.onodes) + 0.01
                    targets[int(all_values[0])] = 0.99
                    self.train(inputs,targets)

    def test_mnist(self, path):
        file2 = load(path)
        for record in file2:
            all_values = record.split(',')
            correct_label = int(all_values[0])
            # print(correct_label, "correct answer")
            inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
            outputs = n.query(inputs)
            label = np.argmax(outputs)
            # print(label, "network's answer")
            if( label == correct_label):
                scorecard.append(1)
            else:
                scorecard.append(0)

        scorecard_array = np.asarray(scorecard)
        print ("performance = ", scorecard_array.sum() /  scorecard_array.size)

    def softmax(self,x):
        return np.exp(x) / np.sum(np.exp(x), axis=0)

    def cross_entropy(self,predictions, targets, epsilon=1e-12):
        predictions = np.clip(predictions, epsilon, 1. - epsilon)
        N = predictions.shape[0]
        ce = -np.sum(targets*np.log(predictions+1e-9))/N
        return ce

def load(path):
    data_file = open(path, "r")
    data_list = data_file.readlines()
    data_file.close()
    return data_list

if __name__ == "__main__":
    # file = load("bigger/mnist_train.csv")
    # file2 = load("bigger/mnist_test.csv")

    input_nodes = 784
    hidden_nodes = 150
    hidden_nodes2 = 150
    output_nodes = 10
    learning_rate = 0.1
    scorecard = []
    epochs = 1

    n = neuralNetwork(input_nodes,hidden_nodes,hidden_nodes2,output_nodes,learning_rate,epochs)

    # train
    # for e in range(epochs):
    #     for record in file:
    #         all_values = record.split(',')
    #         inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
    #         targets = np.zeros(output_nodes) + 0.01
    #         targets[int(all_values[0])] = 0.99
    #         n.train(inputs,targets)

    n.train_mnist("bigger/mnist_train.csv")

    n.serialize()

    # n.deserialize()

    n.test_mnist("bigger/mnist_test.csv")

    # print(file[0].split(',')[0])
    # print(n.query(np.asfarray(file[0].split(',')[1:])))

    #query
    # for record in file2:
    #     all_values = record.split(',')
    #     correct_label = int(all_values[0])
    #     print(correct_label, "correct answer")
    #     inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
    #     outputs = n.query(inputs)
    #     label = np.argmax(outputs)
    #     # print(label, "network's answer")
    #     if( label == correct_label):
    #         scorecard.append(1)
    #     else:
    #         scorecard.append(0)

    # scorecard_array = np.asarray(scorecard)
    # print ("performance = ", scorecard_array.sum() /  scorecard_array.size)


import numpy as np
import csv
import collections

def read_file(file_name):
    global latitude, longitude, altitude, intensity
    with open(file_name) as csvfile:
        r = csv.r(csvfile, delimiter =' ')
        for row in r:
            latitude.append(float(row[0]))
            longitude.append(float(row[1]))
            altitude.append(float(row[2]))
            intensity.append(float(row[3]))

def point_cloud_obj(array, name):
    point_cloud_obj = open(name,"w")
    for j in array:
        point_cloud_obj.write("v "+str(j[0])+" "+str(j[1])+" "+str(j[2])+"\n")
    point_cloud_obj.close

def lat_to_cartesian(latitude, longitude, altitude, intensity):
    earth_rad = (6378.137*1000)
    latitude_cos = np.cos(latitude*np.pi/180)
    latitude_sin = np.sin(latitude*np.pi/180)
    longitude_cos = np.cos(longitude*np.pi/180)
    longitude_sin = np.sin(longitude*np.pi/180)
    f = 1.0 / 298.257224
    C = 1.0/ np.sqrt(latitude_cos**2 + ((1-f)**2)*((latitude_sin)**2))
    S = ((1.0 - f)**2)*(C)
    x = (earth_rad*C)*latitude_cos*longitude_cos
    y = (earth_rad*C)*latitude_cos*longitude_sin
    z = altitude
    return x, y, z, intensity

def planar_filter(cartesian, grid_size, lower_bound, ax):
    ratio = 1 / grid_size
    if ratio > 0:
        ratio = int(ratio)
    xs = np.round(cartesian[:, 0] * ratio)
    ys = np.round(cartesian[:, 1] * ratio)
    zs = np.round(cartesian[:, 2] * ratio)
    delta_x = collections.defaultdict(list)
    delta_y = collections.defaultdict(list)
    delta_z = collections.defaultdict(list)
    for i in range(len(cartesian)):
        xc = xs[i]
        yc = ys[i]
        zc = zs[i]
        x = cartesian[i, 0]
        y = cartesian[i, 1]
        z = cartesian[i, 2]
        delta_z[(xc, yc)].append((i, z))
        delta_y[(xc, zc)].append((i, y))
        delta_x[(yc, zc)].append((i, x))
    planar_res = []
    if ax == 'z':
        for xy, i_and_z in delta_z.items():
            iterator, z_list = zip(*i_and_z)
            if max(z_list) - min(z_list) >= lower_bound:
                planar_res.extend(iterator)
    if ax == 'y':
        for xz, i_and_y in delta_y.items():
            iterator, list_y = zip(*i_and_y)
            if max(list_y) - min(list_y) <= lower_bound:
                planar_res.extend(iterator)
    if ax == 'x':
        for yz, i_and_x in delta_x.items():
            iterator, list_x = zip(*i_and_x)
            if max(list_x) - min(list_x) <= lower_bound:
                planar_res.extend(iterator)
    return planar_res


from flask import Flask, render_template, make_response, request, redirect, url_for

app = Flask('__main__')

@app.route('/')
def main():
    context = {
        'title': 'Главная',
    }

    return render_template('page_1.html', **context)

@app.route('/cookie/', methods=['GET', 'POST'])
def cookie():
    name = request.form.get('name')
    email = request.form.get('email')
    if name and email:
        resp = make_response(redirect(url_for('welcome')))
        resp.set_cookie('name', name)
        resp.set_cookie('email', email)

        return resp
    return redirect(url_for('main'))

@app.route('/welcome/')
def welcome():
    name = request.cookies.get('name')
    context = {
        'title': 'Добро пожаловать!',
        'name': name,
    }
    return render_template('welcome.html', **context)

@app.route('/exit/')
def exit():
    resp = make_response(redirect(url_for('main')))
    resp.delete_cookie('name')
    resp.delete_cookie('email')

    return resp

if __name__ == '__main__':
    app.run()


import torch
import torch.nn as nn
import numpy as np
from operator import itemgetter
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
from torch.nn.init import xavier_uniform_,xavier_normal_
import torch.nn.functional as F
#Pytorch Geometric
from torch_geometric.nn import MessagePassing, LGConv
from torch_geometric.utils import add_self_loops, degree
from torch_geometric.utils import to_undirected

class LightGCN(nn.Module):
    def __init__(self,N,args,device='cpu') -> None:
        """NGCF Model with pytorch geometric framework

        Args:
            N ([int]): Number of nodes in bipartite graph ( Warning it's # users + items ! )
            args:
            - emb_size (int, optional): Size of the embeddings. Defaults to 64.
            - aggr (str,optional): Aggregation function in convolution layer. 'add' 'mean' or 'max'
            - pool (str,optional): Pooling mode for final representation. 'concat' 'mean' or 'sum'
            - negative_slope (float): negative slope for the leakyRelu
            - pretrain (bool): Use the pretrain embeddings matrix

        """
        super().__init__()
        emb_size = args.emb_size

        self.gc1 = LGConv(normalize=True)
        self.gc2 = LGConv(normalize=True) # ATTENTION on ajoute des self loops dans les couches de convolutions.
        self.gc3 = LGConv(normalize=True)
        if args.pretrain:
            try:
                self.E = torch.load('embeddings/pretrain_emb.pt')
                print('sucess to load pretrain embeddings')
            except :
                print('no pretrain embeddings found')
                raise NameError('Didnt find the pretrain embeddings')

        else:
            self.E = nn.Parameter(xavier_normal_(torch.rand((N,emb_size),requires_grad=True)))
        self.device = device
        self.pool = args.pool

    def forward(self,batch):
        """Generate embeddings for a batch of users and items.

        Args:
            batch (tuple(torch.LongTensor)): Sample batch of users, positive and negative items
            batch[0] --> users
            batch[1] --> pos items
            batch[2] --> neg items sampled

            Careful the sample are directed graph from users to items, make it indirect by using 'to_undirect' function
            from pyg.

            Return (Positive similarity, negative similarity)

        """
        users,pos,neg = batch

        users = users.to(self.device)
        pos = pos.to(self.device)
        neg = neg.to(self.device)

        edge_index_direct = torch.stack((users,pos)) # Real connections in graph

        edge_index = to_undirected(edge_index_direct).to(self.device) # Propagate msg for users AND items

        e1 = self.gc1(self.E,edge_index)

        e2 = self.gc2(e1,edge_index)

        e3 = self.gc3(e2,edge_index)

        self.ef = self._pooling((self.E,e1,e2,e3)) # Final representation is the concatenation of rpz of all layers

        users_emb = self.ef[users]
        pos_emb = self.ef[pos]
        neg_emb = self.ef[neg]

        pos_sim = (users_emb * pos_emb).sum(dim=-1) # Compute batch of similarity between users and positive items
        neg_sim = (users_emb * neg_emb).sum(dim=-1) # Compute batch of similarity between users and negative items

        return pos_sim,neg_sim

    def _pooling(self,emb_tuple):
        """

            Pooling of representations at each layers with mode
            mode (str): 'concat', 'sum' or 'mean'
        """
        if self.pool =='concat':
            ef = torch.cat(emb_tuple,1)

        if self.pool =='mean':

            return torch.mean(torch.stack((emb_tuple)),dim=0,keepdim=True)

        elif self.pool =='sum':
            ef = emb_tuple[0]
            for e in emb_tuple[1:]:
                ef = ef + e
        return ef

    def compute_score(self,n_users):
        """

        Args:
            n_users ([type]): Number of users

        return the similarity scores between each users and items of size N*M
        Use for test
        """
        users = self.ef[:n_users] # N first lines of the embeddings matrix are for the users
        items = self.ef[n_users:] # The others are for the items
        scores = users @ items.T # Inner product between all users and items
        return scores


import numpy as np
import pandas as pd
import os
import torch
import random
import math
import scipy.spatial

from atom3d.util.transforms import prot_graph_transform, PairedGraphTransform
from atom3d.datasets import LMDBDataset
from torch_geometric.data import Data, Dataset, Batch
from torch.utils.data import IterableDataset, DataLoader
import atom3d.util.graph as gr
import atom3d.datasets.ppi.neighbors as nb

class PPIDataset(IterableDataset):
    def __init__(self, lmdb_path):
        self.dataset = LMDBDataset(lmdb_path)

    def __iter__(self):
        worker_info = torch.utils.data.get_worker_info()
        if worker_info is None:  # single-process data loading, return the full iterator
            gen = dataset_generator(self.dataset, range(len(self.dataset)),
                      shuffle=True)
        else:  # in a worker process
            # split workload
            per_worker = int(math.ceil(len(self.dataset) / float(worker_info.num_workers)))
            worker_id = worker_info.id
            iter_start = worker_id * per_worker
            iter_end = min(iter_start + per_worker, len(self.dataset))
            gen = dataset_generator(self.dataset, range(len(self.dataset))[iter_start:iter_end],
                      shuffle=True)
        return gen

class CollaterPPI(object):
    def __init__(self, batch_size):
        self.batch_size = batch_size

    def collate(self, data_list):
        if len(data_list) != self.batch_size:
            bs = len(data_list)
        else:
            bs = self.batch_size
        batch_1 = self.adjust_graph_indices(Batch.from_data_list([d[0] for d in data_list]), bs)
        batch_2 = self.adjust_graph_indices(Batch.from_data_list([d[1] for d in data_list]), bs)
        return batch_1, batch_2

    def adjust_graph_indices(self, graph, bs):
        total_n = 0
        for i in range(bs-1):
            n_nodes = graph.n_nodes[i].item()
            total_n += n_nodes
            graph.ca_idx[i+1] += total_n
        return graph

    def __call__(self, batch):
        return self.collate(batch)

def df_to_graph(struct_df, chain_res, label):
    """
    Extracts atoms within 30A of CA atom and computes graph
    """

    chain, resnum = chain_res
    res_df = struct_df[(struct_df.chain == chain) & (struct_df.residue == resnum)]
    if 'CA' not in res_df.name.tolist():
        return None
    ca_pos = res_df[res_df['name']=='CA'][['x', 'y', 'z']].astype(np.float32).to_numpy()[0]

    kd_tree = scipy.spatial.KDTree(struct_df[['x','y','z']].to_numpy())
    graph_pt_idx = kd_tree.query_ball_point(ca_pos, r=30.0, p=2.0)
    graph_df = struct_df.iloc[graph_pt_idx].reset_index(drop=True)
    ca_idx = np.where((graph_df.chain == chain) & (graph_df.residue == resnum) & (graph_df.name == 'CA'))[0]
    if len(ca_idx) > 0:
        return None

    node_feats, edge_index, edge_feats, pos = gr.prot_df_to_graph(graph_df)
    data = Data(node_feats, edge_index, edge_feats, y=label, pos=pos)
    data.ca_idx = torch.LongTensor(ca_idx)
    data.n_nodes = data.num_nodes

    return data

def create_labels(positives, negatives, num_pos, neg_pos_ratio):
    frac = min(1, num_pos / positives.shape[0])
    positives = positives.sample(frac=frac)
    n = positives.shape[0] * neg_pos_ratio
    negatives = negatives.sample(n, random_state=0, axis=0)
    labels = pd.concat([positives, negatives])[['chain0', 'residue0', 'chain1', 'residue1', 'label']]
    return labels

def remove_waters(df):
    # df = df[df['element'].isin(allowed_atoms)]
    df = df[df['hetero'].str.strip()!='W']
    return df

def dataset_generator(dataset, indices, shuffle=True):
    """
    Generator that convert sharded HDF dataset to graphs
    """
    for idx in indices:
        data = dataset[idx]

        neighbors = data['atoms_neighbors']
        pairs = data['atoms_pairs']

        if shuffle:
            groups = [df for _, df in pairs.groupby('ensemble')]
            random.shuffle(groups)
            shard = pd.concat(groups).reset_index(drop=True)

        for i, (ensemble_name, target_df) in enumerate(pairs.groupby(['ensemble'])):

            sub_names, (bound1, bound2, _, _) = nb.get_subunits(target_df)
            positives = neighbors[neighbors.ensemble0 == ensemble_name]
            negatives = nb.get_negatives(positives, bound1, bound2)
            negatives['label'] = 0
            labels = create_labels(positives, negatives, num_pos=10, neg_pos_ratio=1)

            for index, row in labels.iterrows():
                label = float(row['label'])
                chain_res1 = row[['chain0', 'residue0']].values
                chain_res2 = row[['chain1', 'residue1']].values
                graph1 = df_to_graph(bound1, chain_res1, label)
                graph2 = df_to_graph(bound2, chain_res2, label)
                if (graph1 is None) or (graph2 is None):
                    continue
                yield graph1, graph2

if __name__=="__main__":
    from tqdm import tqdm

    dataset = PPIDataset(os.path.join('/scratch/users/raphtown/atom3d_mirror/lmdb/PPI/splits/DIPS-split/data', 'train'))
    dataloader = DataLoader(dataset, batch_size=3, shuffle=False, collate_fn=CollaterPPI(batch_size=3), num_workers=4)
    for graph1, graph2 in dataloader:
        print(graph1)
        print(graph2)
        break


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

# pylint: disable=no-name-in-module
# pylint: disable=import-error

"""
Setup Script
"""

from setuptools import setup
from unittest_prettify import __title__, __version__, __description__, __author__, __author_email__, __license__

with open("README.md", "r") as fh:
    LONG_DESCRIPTION = fh.read()
REQUIRED = []

setup(name=__title__,
      version=__version__,
      description=__description__,
      author=__author__,
      author_email=__author_email__,
      license=__license__,
      url='https://github.com/jonathadv/unittest-prettify/',
      packages=['unittest_prettify'],
      install_requires=REQUIRED,
      python_requires=">=3.5.*",
      long_description=LONG_DESCRIPTION,
      long_description_content_type="text/markdown",
      classifiers=[
          'Development Status :: 4 - Beta',
          'Intended Audience :: Developers',
          'Natural Language :: English',
          'License :: OSI Approved :: MIT License',
          'Programming Language :: Python',
          'Programming Language :: Python :: 3',
          'Programming Language :: Python :: 3.5',
          'Programming Language :: Python :: 3.6',
          'Programming Language :: Python :: Implementation :: CPython',
      ]
)


"""YAML loading and processing functions."""

import yaml
from six import string_types

from .preprocessor import preprocess

def post_process(value, path):
    """Process a parsed YAML document."""
    is_dict = True
    is_array = False
    try:
        generator = value.iteritems()
    except AttributeError:
        is_dict = False
        is_array = not isinstance(value, string_types)
        if is_array:
            try:
                generator = (post_process(x, path) for x in value)
            except TypeError:
                is_array = False

    if is_dict:
        return {k: post_process(v, path) for k, v in generator}

    if is_array:
        return list(generator)

    try:
        value = preprocess(value, path)
    except TypeError:
        pass

    return value

def load(path):
    """Load and process a YAML file."""
    return post_process(yaml.load(open(path)), path)


import boto3
import json
import os
import uuid

def on_event(event, context):
    print(event)
    request_type = event["RequestType"]
    if request_type == "Create":
        return on_create(event)
    if request_type == "Update":
        return on_update(event)
    if request_type == "Delete":
        return on_delete(event)
    raise Exception("Invalid request type: %s" % request_type)

def on_create(event):
    props = event["ResourceProperties"]
    print("create new resource with props %s" % props)

    policy_id = props["PolicyId"]
    account_targets = props["AccountTargets"]
    organization_unit_targets = props["OrganizationUnitTargets"]

    organizations_client = boto3.client("organizations")
    for account in account_targets:
        organizations_client.attach_policy(PolicyId=policy_id, TargetId=account)

    for organization_unit in organization_unit_targets:
        organizations_client.attach_policy(
            PolicyId=policy_id, TargetId=organization_unit
        )

    physical_resource_id = str(uuid.uuid4())
    return {"PhysicalResourceId": physical_resource_id}

def on_update(event):
    physical_id = event["PhysicalResourceId"]
    props = event["ResourceProperties"]
    old_props = event["OldResourceProperties"]
    print(
        "update resource %s with props %s old props %s"
        % (physical_id, props, old_props)
    )

    policy_id = props["PolicyId"]
    account_targets = set(props["AccountTargets"])
    organization_unit_targets = set(props["OrganizationUnitTargets"])

    old_account_targets = set(old_props["AccountTargets"])
    old_organization_unit_targets = set(old_props["OrganizationUnitTargets"])

    account_intersection = old_account_targets.intersection(account_targets)
    to_detach_account_targets = old_account_targets - account_intersection
    to_attach_account_targets = account_targets - account_intersection

    organization_unit_intersection = old_organization_unit_targets.intersection(
        organization_unit_targets
    )
    to_detach_organization_targets = (
        old_organization_unit_targets - organization_unit_intersection
    )
    to_attach_organization_unit_targets = (
        organization_unit_targets - organization_unit_intersection
    )

    organizations_client = boto3.client("organizations")
    for account in to_detach_account_targets:
        organizations_client.detach_policy(PolicyId=policy_id, TargetId=account)

    for organization_unit in to_detach_organization_targets:
        organizations_client.detach_policy(
            PolicyId=policy_id, TargetId=organization_unit
        )

    for account in to_attach_account_targets:
        organizations_client.attach_policy(PolicyId=policy_id, TargetId=account)

    for organization_unit in to_attach_organization_unit_targets:
        organizations_client.attach_policy(
            PolicyId=policy_id, TargetId=organization_unit
        )

def on_delete(event):
    physical_id = event["PhysicalResourceId"]
    print("delete resource %s" % physical_id)
    props = event["ResourceProperties"]
    print("delete resource with props %s" % props)

    policy_id = props["PolicyId"]
    account_targets = props["AccountTargets"]
    organization_unit_targets = props["OrganizationUnitTargets"]

    organizations_client = boto3.client("organizations")
    for account in account_targets:
        organizations_client.detach_policy(PolicyId=policy_id, TargetId=account)

    for organization_unit in organization_unit_targets:
        organizations_client.detach_policy(
            PolicyId=policy_id, TargetId=organization_unit
        )


import utils
from games.GuessGame import GuessGame
from games.MemoryGame import MemoryGame
from games.CurrecyRouletteGame import CurrencyRouletteGame

# can tweak these variables
DEBUG = False

class Live:

    def __init__(self, name, scores):
        self.name = name
        self.Scores = scores

    def welcome(self):
        """Greats the user at the start of the program"""
        return (f"\n--------------------------------------------------------"
                f"\nHello {self.name} and welcome to the World of Games (WoG). "
                f"\nHere you can find many cool games to play."
                f"\n--------------------------------------------------------")

    def load_game(self):
        # menu
        print("Please choose a game to play: "
              "\n\t0. Exit"
              "\n\t1. Guess Game - guess a number and see if you chose like the computer"
              "\n\t2. Memory Game - a sequence of numbers will appear for 1 second and you have to guess it back "
              "\n\t3. Currency Roulette - try and guess the value of a random amount of USD in ILS\n")

        # user input for the menu selection (sent to validation)
        game = int(utils.validate_input_given_list(prompt="Enter your selection: ", options_list=['0', '1', '2', '3']))

        # handling the user choice
        if game == 0:
            utils.clear_screen()
            print("Good bye.")
            return game
        else:
            difficulty = int(utils.validate_input_given_list(prompt="Select difficulty level between 1-5: ",
                                                            options_list=['1', '2', '3', '4', '5']))

        if DEBUG: print(f"\nDEBUG: game: {game}, difficulty: {difficulty}\n")

        if game == 1:
            utils.clear_screen()
            guess_game = GuessGame(difficulty)
            if guess_game.play():
                print('\nYou won the game!')
                self.Scores.add_score(difficulty)
            else:
                print('\nYou lost the game, I wish you better luck next time.')
            return game
        elif game == 2:
            utils.clear_screen()
            memory_game = MemoryGame(difficulty)
            if memory_game.play():
                print("\nYou won the game!")
                self.Scores.add_score(difficulty)
            else:
                print('\nYou lost the game, I wish you better luck next time.')
            return game
        elif game == 3:
            utils.clear_screen()
            currency_roulette_game = CurrencyRouletteGame(difficulty)
            if currency_roulette_game.play():
                print("\nYou won the game!")
                self.Scores.add_score(difficulty)
            else:
                print('\nYou lost the game, I wish you better luck next time.')
            return game


from datetime import datetime
from django.shortcuts import render
from django.urls import reverse
from django.views import View
from django.http import HttpResponseRedirect,HttpResponse
import json
from userAuthentication.models import user
from userProjects.models import userProject,userProjectRecording
from django.http import JsonResponse
from meetingMinutes import settings

# Create your views here.

class createUserProject(View):
    def get(self,request):
        if(request.session.get("userEmail")):
            return render(request,"userProjects/projectCreation.html")
        else:
            loginPage=reverse("sign_In_ULR")
            return HttpResponseRedirect(loginPage)

    def post(self,request):
        if(request.session.get("userEmail")):
            userId=request.session["userEmail"]
            userProjectName=request.POST["projectName"]
            if(userProjectName!=""):
                associatedUser=user.objects.get(userEmail=userId)
                try:
                    if(self.isProjectWithSameNameExist(associatedUser,userProjectName)):
                        return HttpResponse(json.dumps({'message': -3}))
                    else:
                        userCreatedProject=userProject(projectName=userProjectName)
                        userCreatedProject.projectCreator=associatedUser
                        userCreatedProject.save(force_insert=True)
                        return HttpResponse(json.dumps({'message': 0}))
                except:
                    return HttpResponse(json.dumps({'message': -2}))
            else:
                return HttpResponse(json.dumps({'message': -1}))
        else:
            return HttpResponse(json.dumps({'message': 1}))

    def isProjectWithSameNameExist(self,user,projectName):
        filteredProjects=userProject.objects.filter(projectCreator=user,projectName=projectName)
        return True if(len(filteredProjects)>0) else False

class viewUserProjects(View):
    def get(self,request):
        if(request.session.get("userEmail")):
            userId=request.session["userEmail"]
            loggedUser=user.objects.get(userEmail=userId)
            loggedUserProjects=userProject.objects.filter(projectCreator=loggedUser)
            return render(request,"userProjects/recentProjects.html",{"projects":loggedUserProjects})
        else:
            loginPage=reverse("sign_In_ULR")
            return HttpResponseRedirect(loginPage)

def logOutFunction(request):
    request.session.clear()
    redirectPath = reverse("sign_In_ULR")
    return HttpResponseRedirect(redirectPath)

class projectRecordings(View):
    def post(self,request,projectId):
        print("Inside upload view")
        if(request.session.get("userEmail")):
            projectRequested=userProject.objects.get(id=projectId)
            self.__saveFileToDB(request, projectRequested)
            return HttpResponse(json.dumps({'message': 0}))
        else:
            return HttpResponse(json.dumps({'message': 1}))

    def __saveFileToDB(self, request, projectRequested):
        presentDateAndTime = datetime.now()
        presentDateAndTime = str(
                presentDateAndTime.strftime("%d %m %Y %H %M %S"))
        for recording in request.FILES.getlist("userFiles"):
            #default file name is recording.name
            fileName=self.__getFileName()
            uploadedRecording=userProjectRecording(myRecording=recording,folderName=presentDateAndTime,fileName=fileName,associatedProject=projectRequested)
            uploadedRecording.save()

    def __getFileName(self):
        uploadedRecordings=userProjectRecording.objects.all()
        rankNo=1
        if len(uploadedRecordings)>0:
            rankNo=len(uploadedRecordings)+1

        return settings.FILE_INITIAL+str(rankNo)

    def get(self,request,projectId):
        if(request.session.get("userEmail")):
            try:
                userEmail=request.session["userEmail"]
                myUser=user.objects.get(userEmail=userEmail)
                projectRequested=userProject.objects.get(id=projectId,projectCreator=myUser)
                projectAssociatedRecordings=userProjectRecording.objects.filter(associatedProject=projectRequested)
                return render(request,"userProjects/projectRecordings.html",{"recordings":projectAssociatedRecordings,"projectId":projectId})
            except:
                invalidRequest=reverse("Page_404",args=["invalidRequest"])
                return HttpResponseRedirect(invalidRequest)
        else:
            loginPage=reverse("sign_In_ULR")
            return HttpResponseRedirect(loginPage)

class deleteUserProjects(View):
    def get(self,request):
        invalidRequest=reverse("Page_404",args=["invalidRequest"])
        return HttpResponseRedirect(invalidRequest)

    def post(self,request):
        if(request.session.get("userEmail")):
            projectID=request.POST["projectId"]
            try:
                projectToDel=userProject.objects.get(id=projectID)
                projectToDel.delete()
                return HttpResponse(json.dumps({'message': 0}))
            except:
                return HttpResponse(json.dumps({'message': -1}))
        else:
            return HttpResponse(json.dumps({'message': -2}))

class updateUserProjects(View):
    def get(self,request):
        invalidRequest=reverse("Page_404",args=["invalidRequest"])
        return HttpResponseRedirect(invalidRequest)

    def post(self,request):
        if(request.session.get("userEmail")):
            projectID=request.POST["projectId"]
            projectName=request.POST["projectName"]
            userEmail=request.session["userEmail"]
            myValidator=createUserProject()
            if(projectName!=""):
                try:
                    projectToUpdate=userProject.objects.get(id=projectID)
                    if(projectToUpdate.projectName==projectName):
                        return HttpResponse(json.dumps({'message': -4}))
                    else:
                        myUser=user.objects.get(userEmail=userEmail)
                        if(myValidator.isProjectWithSameNameExist(myUser,projectName)):
                            return HttpResponse(json.dumps({'message': -5}))
                        else:
                            projectToUpdate.projectName=projectName
                            projectToUpdate.save()
                            return HttpResponse(json.dumps({'message': 0}))
                except:
                    return HttpResponse(json.dumps({'message': -1}))
            else:
                return HttpResponse(json.dumps({'message': -2}))
        else:
            return HttpResponse(json.dumps({'message': -3}))

class deleteRecordingFile(View):
    def get(self,request):
        invalidRequest=reverse("Page_404",args=["invalidRequest"])
        return HttpResponseRedirect(invalidRequest)

    def post(self,request):
        if(request.session.get("userEmail")):
            recordingFileID=request.POST["recordingID"]
            try:
                fileToDelete=userProjectRecording.objects.get(id=recordingFileID)
                fileToDelete.delete()
                return HttpResponse(json.dumps({'message': 0}))
            except:
                return HttpResponse(json.dumps({'message': -1}))
        else:
            return HttpResponse(json.dumps({'message': -2}))


import argparse, hashlib, csv, random
import numpy as np
import collections

from model_prediction_lib import create_committer_data_dict, create_comments_dict, \
                            train_test_split_idxs, get_index_tokenizer, build_data, \
                            trim_pad_sequences, flatten_comments_dict, mention_preprocess,\
                            remove_short_comments, Using_one_project, set_of_words_only,\
                            remove_set_of_words, remove_non_committer

from model_prediction_lib import COMMIT_METRICS

from keras.models import Model, model_from_json, Sequential
from keras.layers.embeddings import Embedding
from keras.layers import Input, LSTM, Dense
from keras.layers.convolutional import Conv1D
from keras.layers.pooling import MaxPooling1D
from keras.layers.core import Flatten
from keras.callbacks import Callback
from sklearn.metrics import roc_auc_score, confusion_matrix, r2_score
from sklearn.naive_bayes import MultinomialNB

from util import load_cached_data, get_cache_path

DATA_TYPES = ['speaking', 'spoken_to']

if __name__ == '__main__':
    argparser = argparse.ArgumentParser(description='Formats data for LM input')
    argparser.add_argument('--base_comment_dir',
                           action='store',
                           help='Base directory of issue comments.',
                           default='../data/sample_46_projs_issues')
    argparser.add_argument('--base_commit_dir',
                           action='store',
                           help='Base directory of commits.',
                           default='../data/sample_46_projs_commits')
    argparser.add_argument('--comments_file',
                           action='store',
                           help='Comments file name.',
                           default='issue_comments_REPLACECODE.csv')
    argparser.add_argument('--data_type',
                           action='store',
                           help='Data type.',
                           choices=DATA_TYPES,
                           default='speaking')
    argparser.add_argument('--top_perc',
                           action='store',
                           help='Top percent of the response metric to be the \
                           positive class.',
                           type=int,
                           default=10)
    argparser.add_argument('--metric',
                           action='store',
                           help='Metric for the top percent.',
                           choices=COMMIT_METRICS,
                           type=str,
                           default='commits')
    argparser.add_argument('--vocab_size',
                           action='store',
                           help='Max vocab size to consider. Default is 10,000',
                           type=int,
                           default=10000)
    argparser.add_argument('--only_committer',
                           action='store_true',
                           help='If only use data from committers.',
                           default=False)
    argparser.add_argument('--min_sequence_len',
                           action='store',
                           help='Post has less than this number of token is \
                           considered noise and will be removed.',
                           type=int,
                           default=5)

    args = argparser.parse_args()

    print('Model: ' + args.data_type)
    print('Min Sequence Length: %d' % args.min_sequence_len)

    # read in and cache committer and comments data
    committer_dict = create_committer_data_dict(args.base_commit_dir,
        args.top_perc, args.metric)
    h = hashlib.sha1()
    h_name = 'model_prediction_nb' + args.base_comment_dir + args.comments_file
    h.update(h_name.encode('utf-8'))
    comments_dict = create_comments_dict(args.base_comment_dir, committer_dict, args.comments_file, args.data_type, h)

    print('Removing noise')
    comments_dict = remove_short_comments(comments_dict, args.min_sequence_len)

    if args.only_committer:
      	print('Removing non-committers')
      	comments_dict = remove_non_committer(comments_dict, committer_dict, args.data_type)

    print('Splitting idxs')
    train_idxs, test_idxs, project_name = train_test_split_idxs(comments_dict, -1)
    tkr = get_index_tokenizer(comments_dict, train_idxs, args.vocab_size)

    print('Building data')
    X, y, ids = build_data(comments_dict, committer_dict, tkr, "classifier", args.data_type)

    print('Converting texts to sequences')
    X = tkr.texts_to_matrix(X, mode='count')

    X_train = [X[i] for i in train_idxs]
    y_train = [y[i] for i in train_idxs]
    X_test = [X[i] for i in test_idxs]
    y_test = [y[i] for i in test_idxs]

    clf = MultinomialNB()
    clf.fit(X_train, y_train)
    print("Accuracy: %.2f%%" % (clf.score(X_test, y_test)*100))
    y_test_prob = clf.predict_proba(X_test)[:, 1]
    auc = float(roc_auc_score(y_test, y_test_prob))
    print("Auc: %.2f%%" % (auc*100))


import re
import pandas as pd
import pandas as pd
import sqlalchemy as sqlalc
from getpass import getpass
import re
from sqlalchemy import exc

# EXTRACTION

def get_episode(path, episode):
    contenu = open(path, "r")
    script = contenu.read()
    my_serie = re.split(r"\n\n([A-Z]+ ?[A-Z]+)\n", script)
    list_name = []
    for i in range(1, len(my_serie), 2):
        dic = {}
        dic['name'] = my_serie[i]
        dic['text'] = my_serie[i+1]
        list_name.append(dic)

    df = pd.DataFrame(list_name)
    df["episode"] = episode
    return df

# LOADING

def connect_engine(password):
    dbName = "twin_peaks"
    connection_data = f"mysql+pymysql://root:{password}@localhost/{dbName}"
    engine = sqlalc.create_engine(connection_data)
    return engine

# checking existence before creating

def check(something, string):

    if something == "actor":
        query = list(engine.execute(
            f"SELECT name FROM actor WHERE name = '{string}';"))
        if len(query) > 0:
            return True
        else:
            return False

    if something == "episode":
        query = list(engine.execute(
            f"SELECT number FROM episode WHERE name = '{string}';"))
        if len(query) > 0:
            return True
        else:
            return False

# insert characters' names into db

def insertActor(actor_name):
    if check("actor", actor_name):
        return "It already exists"
    else:
        engine.execute(f"INSERT INTO actor (name) VALUES ('{actor_name}');")

# insert text for each character for each episode into script table
def insertScript(script, idepisode, idname):
    queryString = f"insert into script (`text` , idepisode, name) VALUES ('{script}', '{idepisode}', '{idname}');"
    # print(queryString)
    engine.execute(queryString)

# insert episode into episode table

def insertEpisode(number, season):
    engine.execute(
        f"INSERT INTO episode (number, season) VALUES ('{number}','{season}');")

def getcompound(name_):
    sql_df = pd.read_sql_query(
    f"""
    SELECT * from SCRIPT
    WHERE script.name = '{name_}';
    """, engine)
    df_polarity = requests.get(f"http://127.0.0.1:9000/sa/{name_}/").json()
    polarity_series = pd.Series(df_polarity)
    df_with_compound = pd.concat([sql_df,polarity_series], axis=1)
    df_with_compound.rename({0:"Compound"}, axis='columns', inplace=True)
    return df_with_compound


__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"

import datetime

from django.db import models
from django.db.models import Avg, F
from django.urls import reverse
from django.utils import timezone

from scipost.fields import ChoiceArrayField
from series.models import Collection

from ..constants import (
    PUBLISHABLE_OBJECT_TYPE_CHOICES,
    get_publishable_object_types_default_list,
    get_submission_object_types_default,
    JOURNAL_STRUCTURE,
    ISSUES_AND_VOLUMES,
    ISSUES_ONLY,
)
from ..managers import JournalQuerySet
from ..validators import doi_journal_validator

def cost_default_value():
    return {"default": 400}

class Journal(models.Model):
    """Journal is a container of Publications, with a unique issn and doi_label.

    Publications may be categorized into issues or issues and volumes.

    Each Journal falls under the auspices of a specific College, which is ForeignKeyed.
    The only exception is Selections, which does not point to any College
    (in fact: it falls under the auspices of all colleges at the same time).

    A Journal's AcademicField is indirectly specified via the College, since
    College has a ForeignKey to AcademicField.

    Specialties can optionally be specified (and should be consistent with the
    College's `acad_field`). If none are given, the Journal operates field-wide.
    """

    college = models.ForeignKey(
        "colleges.College", on_delete=models.PROTECT, related_name="journals"
    )

    specialties = models.ManyToManyField(
        "ontology.Specialty", blank=True, related_name="journals"
    )

    name = models.CharField(max_length=256, unique=True)
    name_abbrev = models.CharField(
        max_length=128,
        default="SciPost [abbrev]",
        help_text="Abbreviated name (for use in citations)",
    )

    doi_label = models.CharField(
        max_length=200, unique=True, db_index=True, validators=[doi_journal_validator]
    )
    issn = models.CharField(max_length=16, default="2542-4653", blank=True)

    active = models.BooleanField(default=True)

    submission_allowed = models.BooleanField(default=True)

    publishable_object_types = ChoiceArrayField(
        models.CharField(max_length=24, choices=PUBLISHABLE_OBJECT_TYPE_CHOICES),
        default=get_publishable_object_types_default_list,
    )
    submission_object_types = models.JSONField(
        default=get_submission_object_types_default,
    )

    structure = models.CharField(
        max_length=2, choices=JOURNAL_STRUCTURE, default=ISSUES_AND_VOLUMES
    )

    refereeing_period = models.DurationField(default=datetime.timedelta(days=28))

    style = models.TextField(
        blank=True,
        null=True,
        help_text=(
            "CSS styling for the journal; the Journal's DOI " "should be used as class"
        ),
    )

    # For Journals list page
    oneliner = models.TextField(
        blank=True,
        help_text="One-line description, for Journal card. You can use markup",
    )
    blurb = models.TextField(default="[To be filled in; you can use markup]")
    list_order = models.PositiveSmallIntegerField(default=100)

    # For manuscript preparation: templates are given by the SubmissionTemplate related objects

    # For the author guidelines page:
    required_article_elements = models.TextField(
        default="[To be filled in; you can use markup]"
    )
    # For about page:
    description = models.TextField(default="[To be filled in; you can use markup]")
    scope = models.TextField(default="[To be filled in; you can use markup]")
    content = models.TextField(default="[To be filled in; you can use markup]")
    acceptance_criteria = models.TextField(
        default="[To be filled in; you can use markup]"
    )
    submission_insert = models.TextField(
        blank=True, null=True, default="[Optional; you can use markup]"
    )

    minimal_nr_of_reports = models.PositiveSmallIntegerField(
        help_text=(
            "Minimal number of substantial Reports required "
            "before an acceptance motion can be formulated"
        ),
        default=1,
    )

    has_DOAJ_Seal = models.BooleanField(default=False)

    has_CLOCKSS = models.BooleanField(default=True)

    # Templates
    template_latex_tgz = models.FileField(
        verbose_name="Template (LaTeX, gzipped tarball)",
        help_text="Gzipped tarball of the LaTeX template package",
        upload_to="UPLOADS/TEMPLATES/latex/%Y/",
        max_length=256,
        blank=True,
    )
    template_docx = models.FileField(
        verbose_name="Template (.docx)",
        help_text=".docx template",
        upload_to="UPLOADS/TEMPLATES/docx/%Y/",
        max_length=256,
        blank=True,
    )

    # Cost per publication information
    cost_info = models.JSONField(default=cost_default_value)

    # Calculated fields (to save CPU; field name always starts with cf_)
    cf_metrics = models.JSONField(default=dict)

    objects = JournalQuerySet.as_manager()

    class Meta:
        ordering = ["college__acad_field", "list_order"]

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        """Return Journal's homepage url."""
        return reverse("scipost:journal_detail", args=(self.doi_label,))

    @property
    def doi_string(self):
        """Return DOI including the SciPost registrant prefix."""
        return "10.21468/" + self.doi_label

    @property
    def has_issues(self):
        return self.structure in (ISSUES_AND_VOLUMES, ISSUES_ONLY)

    @property
    def has_volumes(self):
        return self.structure in (ISSUES_AND_VOLUMES)

    @property
    def has_collections(self):
        return Collection.objects.filter(series__container_journals=self).exists()

    def get_issues(self):
        from journals.models import Issue

        if self.structure == ISSUES_AND_VOLUMES:
            return Issue.objects.filter(in_volume__in_journal=self).published()
        elif self.structure == ISSUES_ONLY:
            return self.issues.open_or_published()
        return Issue.objects.none()

    def get_latest_issue(self):
        """Get latest existing Issue in database irrespective of its status."""
        from journals.models import Issue

        if self.structure == ISSUES_ONLY:
            return self.issues.order_by("-until_date").first()
        if self.structure == ISSUES_AND_VOLUMES:
            return (
                Issue.objects.filter(in_volume__in_journal=self)
                .order_by("-until_date")
                .first()
            )
        return None

    def get_latest_volume(self):
        """Get latest existing Volume in database irrespective of its status."""
        if self.structure == ISSUES_AND_VOLUMES:
            return self.volumes.order_by("-until_date").first()
        return None

    def get_publications(self):
        from journals.models import Publication

        if self.structure == ISSUES_AND_VOLUMES:
            return Publication.objects.filter(in_issue__in_volume__in_journal=self)
        elif self.structure == ISSUES_ONLY:
            return Publication.objects.filter(in_issue__in_journal=self)
        return self.publications.all()

    def nr_publications(self, tier=None, year=None):
        from journals.models import Publication

        publications = self.get_publications()
        if year:
            publications = publications.filter(publication_date__year=year)
        if tier:
            publications = publications.filter(
                accepted_submission__eicrecommendations__recommendation=tier
            )
        return publications.count()

    def avg_processing_duration(self):
        from journals.models import Publication

        duration = Publication.objects.filter(
            in_issue__in_volume__in_journal=self
        ).aggregate(avg=Avg(F("publication_date") - F("submission_date")))["avg"]
        if duration:
            return duration.total_seconds() / 86400
        return 0

    def citation_rate(self, tier=None):
        """Return the citation rate in units of nr citations per article per year."""
        publications = self.get_publications()
        if tier:
            publications = publications.filter(
                accepted_submission__eicrecommendations__recommendation=tier
            )
        ncites = 0
        deltat = 1  # to avoid division by zero
        for pub in publications:
            if pub.citedby and pub.latest_citedby_update:
                ncites += len(pub.citedby)
                deltat += (pub.latest_citedby_update.date() - pub.publication_date).days
        return ncites * 365.25 / deltat

    def nr_citations(self, year, specialty=None):
        publications = self.get_publications()
        if specialty:
            publications = publications.filter(specialties=specialty)
        ncites = 0
        for pub in publications:
            if pub.citedby and pub.latest_citedby_update:
                for citation in pub.citedby:
                    if citation["year"] == str(year):
                        ncites += 1
        return ncites

    def citedby_impact_factor(self, year, specialty=None):
        """Compute the impact factor for a given year YYYY, from Crossref cited-by data.

        This is defined as the total number of citations in year YYYY
        for all papers published in years YYYY-1 and YYYY-2, divided
        by the number of papers published in year YYYY.
        """
        publications = self.get_publications().filter(
            models.Q(publication_date__year=int(year) - 1)
            | models.Q(publication_date__year=int(year) - 2)
        )
        if specialty:
            publications = publications.filter(specialties=specialty)
        nrpub = publications.count()
        if nrpub == 0:
            return 0
        ncites = 0
        for pub in publications:
            if pub.citedby and pub.latest_citedby_update:
                for citation in pub.citedby:
                    if citation["year"] == str(year):
                        ncites += 1
        return ncites / nrpub

    def citedby_citescore(self, year, specialty=None):
        """Compute the CiteScore for a given year YYYY, from Crossref cited-by data.

        This is defined as the total number of citations in years YYYY to YYYY-3
        for all papers published in years YYYY to YYYY-3, divided
        by the number of papers published in that same set of years.
        """
        publications = self.get_publications().filter(
            publication_date__year__lte=int(year),
            publication_date__year__gte=int(year) - 3,
        )
        if specialty:
            publications = publications.filter(specialties=specialty)
        nrpub = publications.count()
        if nrpub == 0:
            return 0
        ncites = 0
        for pub in publications:
            if pub.citedby and pub.latest_citedby_update:
                for citation in pub.citedby:
                    if (
                        int(citation["year"]) <= year
                        and int(citation["year"]) >= year - 3
                    ):
                        ncites += 1
        return ncites / nrpub

    def cost_per_publication(self, year):
        try:
            return int(self.cost_info[str(year)])
        except KeyError:
            return int(self.cost_info["default"])

    def update_cf_metrics(self):
        """
        Update the `cf_metrics` calculated field for this Journal.
        """
        publications = self.get_publications()
        from submissions.models import Submission

        if publications:
            pubyears = [
                year
                for year in range(
                    publications.last().publication_date.year, timezone.now().year
                )
            ]
        else:
            pubyears = [timezone.now().year]
        from submissions.models import Submission

        submissions = Submission.objects.filter(
            submitted_to=self, is_resubmission_of__isnull=True
        )
        self.cf_metrics["nr_publications"] = {
            "title": "Number of publications per year",
            "years": pubyears,
            "nr_publications": {
                "all": [
                    publications.filter(publication_date__year=year).count()
                    for year in pubyears
                ]
            },
        }
        self.cf_metrics["nr_submissions"] = {
            "title": "Number of submissions per year",
            "years": pubyears,
            "nr_submissions": {
                "all": [
                    submissions.filter(submission_date__year=year).count()
                    for year in pubyears
                ]
            },
        }
        self.cf_metrics["nr_citations"] = {
            "title": "Number of citations per year",
            "years": pubyears,
            "nr_citations": {"all": [self.nr_citations(year) for year in pubyears]},
        }
        self.cf_metrics["citedby_citescore"] = {
            "title": "CiteScore",
            "years": pubyears,
            "citedby_citescore": {
                "all": [self.citedby_citescore(year) for year in pubyears]
            },
        }
        self.cf_metrics["citedby_impact_factor"] = {
            "title": "Impact Factor",
            "years": pubyears,
            "citedby_impact_factor": {
                "all": [self.citedby_impact_factor(year) for year in pubyears]
            },
        }
        for specialty in self.specialties.all():
            self.cf_metrics["nr_publications"]["nr_publications"][specialty.slug] = [
                publications.filter(
                    specialties=specialty, publication_date__year=year
                ).count()
                for year in pubyears
            ]
            self.cf_metrics["nr_submissions"]["nr_submissions"][specialty.slug] = [
                submissions.filter(
                    specialties=specialty, submission_date__year=year
                ).count()
                for year in pubyears
            ]
            self.cf_metrics["nr_citations"]["nr_citations"][specialty.slug] = [
                self.nr_citations(year, specialty) for year in pubyears
            ]
            self.cf_metrics["citedby_citescore"]["citedby_citescore"][
                specialty.slug
            ] = [self.citedby_citescore(year, specialty) for year in pubyears]
            self.cf_metrics["citedby_impact_factor"]["citedby_impact_factor"][
                specialty.slug
            ] = [self.citedby_impact_factor(year, specialty) for year in pubyears]
        self.save()


from sklearn.neighbors import KNeighborsRegressor

import joblib

import os

import csv

from tqdm import tqdm

from sklearn.model_selection import train_test_split

from sklearn.metrics import mean_squared_error

import numpy as np

from sklearn.preprocessing import StandardScaler

def train(X_train: list, y_train: list, k: int = 3):
    # the parameter k can be set by yourself, the default is 3
    if os.path.exists("all label trained models/knn_model_corpus.m"):
        reg = joblib.load('all label trained models/knn_model_corpus.m')
    else:
        reg = KNeighborsRegressor(n_neighbors=k)
        reg.fit(X_train, y_train)
        joblib.dump(reg, "all label trained models/knn_model_corpus.m")
    return reg

def load_model():
    if os.path.exists("all label trained models/knn_model_corpus.m"):
        reg = joblib.load('all label trained models/knn_model_corpus.m')
    else:
        reg = None
    return reg

def predict(reg: object, X_test: list):
    return reg.predict(X_test)

if __name__ == '__main__':
    train_dataset = ["toy_dataset", "car_sales", "hotel_booking"]
    X_train = []
    y_train = []
    X_test = []
    y_test = []
    X = []
    y = []
    cate_X = []
    All_X = []
    for i in range(len(train_dataset)):
        f_r = open(f'dataset_example/test_featured_{train_dataset[i]}_fifo.csv', 'r', encoding='utf-8')
        csv_reader = csv.reader(f_r)
        for row in tqdm(csv_reader):
            y.append(float(row[3]))
            row[4] = row[4].replace("\n", "")
            row[4] = row[4].replace("[", "")
            row[4] = row[4].replace("]", "")
            row[4] = row[4].strip()
            # print(row[5])
            row[4] = ' '.join(row[4].split())
            single_data = []
            cate_data = []
            data_arr = row[4].split(" ")
            category_idx = [0, 2]
            count = 0
            for data in data_arr:
                if count in category_idx:
                    cate_data.append(int(eval(data)))
                else:
                    single_data.append(float(data))
                count += 1
            X.append(single_data)
            cate_X.append(cate_data)

        for i in range(len(X)):
            X[i] = np.array(X[i]).astype('float64')
        f_r.close()

    new_X = X
    new_cate_X = cate_X
    new_y = y
    # data standardization
    scale = StandardScaler()
    new_X = scale.fit_transform(new_X)
    for i in range(len(new_X)):
        l = new_X[i].tolist()
        l.extend(new_cate_X[i])
        All_X.append(l)
    X_train, X_test, y_train, y_test = train_test_split(All_X, new_y, test_size=0.3, random_state=2022)

    model = train(X_train, y_train)
    X_test = np.array(X_test).astype('float64')

    y_predict = predict(model, X_test)
    for i in range(len(y_predict)):
        print(y_test[i], end=' , ')
        print(y_predict[i])
    print(mean_squared_error(y_test, y_predict))


from typing import Any, Union

from bip_utils.addr.addr_dec_utils import AddrDecUtils
from bip_utils.addr.addr_key_validator import AddrKeyValidator
from bip_utils.addr.iaddr_decoder import IAddrDecoder
from bip_utils.addr.iaddr_encoder import IAddrEncoder
from bip_utils.bech32 import Bech32ChecksumError, SegwitBech32Decoder, SegwitBech32Encoder
from bip_utils.ecc import IPoint, IPublicKey, Secp256k1, Secp256k1Point, Secp256k1PublicKey
from bip_utils.utils.crypto import Sha256
from bip_utils.utils.misc import BytesUtils, IntegerUtils

class P2TRConst:
    """Class container for P2TR constants."""

    # Secp256k1 field size
    FIELD_SIZE: int = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
    # SHA256 of "TapTweak"
    TAP_TWEAK_SHA256: bytes = BytesUtils.FromHexString(
        "e80fe1639c9ca050e3af1b39c143c63e429cbceb15d940fbb5c5a1f4af57c5e9"
    )
    # Witness version is fixed to one for P2TR
    WITNESS_VER: int = 1

class _P2TRUtils:
    """Class container for P2TR utility functions."""

    @staticmethod
    def TaggedHash(tag: Union[bytes, str],
                   data_bytes: bytes) -> bytes:
        """
        Implementation of the hash tag function as defined by BIP-0340.
        Tagged hash = SHA256(SHA256(tag) || SHA256(tag) || data)

        Args:
            tag (bytes or str): Tag, if bytes it'll be considered already hashed
            data_bytes (bytes): Data bytes

        Returns:
            bytes: Tagged hash
        """
        tag_hash = Sha256.QuickDigest(tag) if isinstance(tag, str) else tag
        return Sha256.QuickDigest(tag_hash + tag_hash + data_bytes)

    @staticmethod
    def HashTapTweak(pub_key: IPublicKey) -> bytes:
        """
        Compute the HashTapTweak of the specified public key.

        Args:
            pub_key (IPublicKey object): Public key

        Returns:
            bytes: Computed hash
        """

        # Use the pre-computed SHA256 of "TapTweak" for speeding up
        return _P2TRUtils.TaggedHash(
            P2TRConst.TAP_TWEAK_SHA256,
            IntegerUtils.ToBytes(pub_key.Point().X(),
                                 bytes_num=Secp256k1Point.CoordinateLength())
        )

    @staticmethod
    def LiftX(pub_key: IPublicKey) -> IPoint:
        """
        Implementation of the lift_x function as defined by BIP-0340.
        It computes the point P for which P.X() = pub_key.X() and has_even_y(P).

        Args:
            pub_key (IPublicKey object): Public key

        Returns:
            IPoint: Computed point

        Raises:
            ValueError: If the point doesn't exist
        """
        p = P2TRConst.FIELD_SIZE
        x = pub_key.Point().X()
        if x >= p:
            raise ValueError("Unable to compute LiftX point")
        c = (pow(x, 3, p) + 7) % p
        y = pow(c, (p + 1) // 4, p)
        if c != pow(y, 2, p):
            raise ValueError("Unable to compute LiftX point")
        return Secp256k1Point.FromCoordinates(x, y if y % 2 == 0 else p - y)

    @staticmethod
    def TweakPublicKey(pub_key: IPublicKey) -> bytes:
        """
        Tweak a public key as defined by BIP-0086.
        tweaked_pub_key = lift_x(pub_key.X()) + int(HashTapTweak(bytes(pub_key.X()))) * G

        Args:
            pub_key (IPublicKey object): Public key

        Returns:
            bytes: X coordinate of the tweaked public key
        """
        h = _P2TRUtils.HashTapTweak(pub_key)
        out_point = _P2TRUtils.LiftX(pub_key) + (BytesUtils.ToInteger(h) * Secp256k1.Generator())
        return IntegerUtils.ToBytes(out_point.X(),
                                    bytes_num=Secp256k1Point.CoordinateLength())

class P2TRAddrDecoder(IAddrDecoder):
    """
    P2WPKH address decoder class.
    It allows the Pay-to-Witness-Public-Key-Hash address decoding.
    """

    @staticmethod
    def DecodeAddr(addr: str,
                   **kwargs: Any) -> bytes:
        """
        Decode a P2TR address to bytes.

        Args:
            addr (str): Address string

        Other Parameters:
            hrp (str): Expected HRP

        Returns:
            bytes: X coordinate of the tweaked public key

        Raises:
            ValueError: If the address encoding is not valid
        """
        hrp = kwargs["hrp"]

        try:
            wit_ver_got, addr_dec_bytes = SegwitBech32Decoder.Decode(hrp, addr)
        except Bech32ChecksumError as ex:
            raise ValueError("Invalid bech32 checksum") from ex

        # Validate length
        AddrDecUtils.ValidateLength(addr_dec_bytes, Secp256k1PublicKey.CompressedLength() - 1)
        # Check witness version
        if wit_ver_got != P2TRConst.WITNESS_VER:
            raise ValueError(f"Invalid witness version (expected {P2TRConst.WITNESS_VER}, got {wit_ver_got})")
        return addr_dec_bytes

class P2TRAddrEncoder(IAddrEncoder):
    """
    P2TR address encoder class.
    It allows the Pay-to-Taproot address encoding.
    """

    @staticmethod
    def EncodeKey(pub_key: Union[bytes, IPublicKey],
                  **kwargs: Any) -> str:
        """
        Encode a public key to P2TR address.

        Args:
            pub_key (bytes or IPublicKey): Public key bytes or object

        Other Parameters:
            hrp (str): HRP

        Returns:
            str: Address string

        Raises:
            ValueError: If the public key is not valid or cannot be tweaked
            TypeError: If the public key is not secp256k1
        """
        hrp = kwargs["hrp"]

        pub_key_obj = AddrKeyValidator.ValidateAndGetSecp256k1Key(pub_key)
        return SegwitBech32Encoder.Encode(hrp,
                                          P2TRConst.WITNESS_VER,
                                          _P2TRUtils.TweakPublicKey(pub_key_obj))

# Deprecated: only for compatibility, Encoder class shall be used instead
P2TRAddr = P2TRAddrEncoder


import numpy as np
import random

import matplotlib.pyplot as plt

def save_points_to_file(filename, points):
    with open(filename, "w") as f:
        for point in points:
            f.write(f"{point[0]} {point[1]}\n")

def get_points_from_file(filename):
    with open('points/'+filename) as f:
        points = []
        lines = f.read().splitlines()
        for line in lines:
            x, y = map(float, line.split())
            points.append((x, y))
        return points

def gen_shape(a, b, x0, y0, func, radian_low=0, radian_high=np.pi):
    return [func(t, a, b, x0, y0) for t in np.arange(radian_low, radian_high, np.pi/60)]

def gen_rand_shape(min_p, max_p, func, random_limit=10):
    a = random.randint(-random_limit, random_limit)
    b = random.randint(-random_limit, random_limit)

    x0 = random.randint(-random_limit, random_limit)
    y0 = random.randint(-random_limit, random_limit)

    return [func(t, a, b, x0, y0) for t in np.arange(0, np.pi, 4*np.pi / random.randint(min_p, max_p))]

def line(t, a, b, x0, y0):
    return a*t + x0, b*t + y0

def zigzag_line(t, a, b, x0, y0):
    return a*t + x0, b*t + + random.normalvariate(y0, y0 / 100)

def ellipse(t, a, b, x0, y0):
    return a*np.cos(t) + x0, b*np.sin(t) + y0

def plot_points(points):
    plt.plot(*zip(*points), marker='o', markersize=3, color='r', ls='')
    plt.savefig("output//points_plot.png")

if __name__ == "__main__":
    points = gen_shape(10, 10, 30, 20, ellipse, radian_high=np.pi/3)
    points = points + gen_shape(5, 5, 0, 20, ellipse, radian_low=2*np.pi/3, radian_high=1.2*np.pi)
    points = points + gen_shape(1, -0.1, 5, 5, zigzag_line, radian_high=3*np.pi)
    points = points + gen_shape(1.5, 0.5, 20, 8, zigzag_line, radian_high=3*np.pi)
    points = points + gen_shape(1.5, -0.2, 10, 30, line, radian_high=2*np.pi)

    save_points_to_file("points/line.txt", points)
    plot_points(points)


"""ops URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView, RedirectView
from ops.views import *
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    url(r'^favicon\.ico$', RedirectView.as_view(url=r'static/images/favicon.ico')),
    url(r'^robots\.txt', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),

    url(r'^$', index),
    url(r'^index/$', index),
    url(r'^init/$', init),
    url(r'^login/$', Login, name='login'),
    url(r'^dashboard/$', dashboard, name='dashboard'),
    url(r'^lockscreen/$', lockscreen),
    # url(r'^terminal/$', web_terminal),
    url(r'^setting/$', setting, name='setting'),
    url(r'^logout/$', Logout, name='logout'),
    url(r'^404/$', page404),
    url(r'^500/$', page500),
    url(r'^help/$', help, name='help'),

    url(r'^admin/', admin.site.urls),

    # 路由转发
    url(r'^users/', include('users.urls')),
    url(r'^tasks/', include('tasks.urls')),
    url(r'^mysql/', include('mysql.urls')),
    # url(r'^library/', include('library.urls')),
    # url(r'^tools/', include('tools.urls')),

]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


from django.urls import path

from . import views

urlpatterns = [
    path('',views.home,name='home'),
    path('algo_sync_tools/',views.algo_sync_tools,name='algo_sync_tools'),
    path('algo_sync_tool_result/',views.algo_sync_tool_result,name='result_algo_tools'),
    path('interface_test/',views.interface_test,name='interface_test'),
    path('interface_test_result/',views.interface_test_result,name='result_interface')
]


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

__author__ = "ipetrash"

"""
Скрипт для уведомления о изменении цены за интернет.

"""

import sys
from pathlib import Path

DIR = Path(__file__).resolve().parent
ROOT_DIR = DIR.parent

sys.path.append(str(ROOT_DIR))  # Путь к папке выше
sys.path.append(str(ROOT_DIR / "third_party" / "ttk_ru"))  # Путь к папке выше

from formats import Formats
from root_common import run_notification_job
from third_party.ttk_ru.main import URL, get_price

run_notification_job(
    "Изменение цены за интернет",
    DIR,
    lambda _: [get_price()],
    send_new_item_diff=True,
    formats=Formats(
        get_items="Проверка цены",
        no_new_items="Изменений нет",
        prefix="👨💻",
    ),
    url=URL,
)


import tkinter as tk
from PIL import ImageTk
from tkinter import messagebox
from model.test_tf import get_conf
from strategy.particle import advLB
from laserBeam.super_simulation import *
from laserBeam.theta_constraint import set_laser

img = None

# 510 绿色 750 红色  440 蓝色 415 紫色

def show_image_window(image, text):
    # 创建新的Toplevel窗口
    image_window = tk.Toplevel(root)
    image_window.title(text)
    # 显示图片
    image_label = tk.Label(image_window)
    image_label.pack()
    photo = ImageTk.PhotoImage(image=image)
    image_label.configure(image=photo)
    image_label.image = photo

def adv():
    update_setting()
    global img
    root.after_cancel(update_frame.timer_id)
    theta, atk_times = advLB(img, 40, 20, 100)
    if theta is None:
        print('攻击失败')
        return
    img_new = makeLB(theta, img)
    show_image_window(img_new, "攻击后图片")
    # img.show()
    # photo = ImageTk.PhotoImage(image=img_new)
    # video_label.configure(image=photo)
    # video_label.image = photo

def val():
    print(get_conf(img))
    show_image_window(img, "测试图片")

def reset():
    root.after_cancel(update_frame.timer_id)
    global cap
    cap = cv2.VideoCapture(url)
    update_frame()

def update_frame():
    ret, frame = cap.read()
    if not ret:
        print("无法读取视频帧")
        return
    global img
    img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
    img = img.resize((1152, 648), Image.LANCZOS)
    photo = ImageTk.PhotoImage(image=img)
    video_label.configure(image=photo)
    video_label.image = photo
    timer_id = root.after(10, update_frame)  # 每隔10毫秒更新画面
    update_frame.timer_id = timer_id

def update_setting():
    if phi_entry.get() == '' or width_entry.get() == '' or alpha_entry.get() == '':
        messagebox.showwarning("警告", "请输入参数")
        return
    set_laser(float(phi_entry.get()), float(width_entry.get()), float(alpha_entry.get()))

url = "http://192.168.43.1:8080//video"

# 创建视频捕捉对象
cap = cv2.VideoCapture(url)

# 检查视频捕捉对象是否成功打开
if not cap.isOpened():
    print("无法打开视频流")
    exit()
start_time = time.time()
root = tk.Tk()
root.title("实时摄像头画面")

video_label = tk.Label(root)
video_label.pack(padx=10, pady=10)

update_frame()
# 创建按钮1
button1 = tk.Button(root, text="计算", command=lambda: adv())
button1.pack(side=tk.LEFT, padx=10)

# 创建按钮2
button2 = tk.Button(root, text="验证", command=lambda: val())
button2.pack(side=tk.LEFT, padx=10)

# 创建按钮3
button3 = tk.Button(root, text="重置", command=lambda: reset())
button3.pack(side=tk.LEFT, padx=10)

laser_phi = tk.Label(root, text="波长:")
laser_phi.pack(side=tk.LEFT, padx=10)
phi_entry = tk.Entry(root, textvariable=tk.StringVar(value='425'))
phi_entry.pack(side=tk.LEFT, padx=10)
laser_width = tk.Label(root, text="宽度:")
laser_width.pack(side=tk.LEFT, padx=10)
width_entry = tk.Entry(root, textvariable=tk.StringVar(value='30'))
width_entry.pack(side=tk.LEFT, padx=10)
laser_alpha = tk.Label(root, text="强度:")
laser_alpha.pack(side=tk.LEFT, padx=10)
alpha_entry = tk.Entry(root, textvariable=tk.StringVar(value='1.0'))
alpha_entry.pack(side=tk.LEFT, padx=10)
root.mainloop()

# 释放视频捕捉对象和窗口
cap.release()
cv2.destroyAllWindows()