# -*- coding: utf-8 -*-
"""
Created on Tue Mar 15 23:18:35 2022

@author: lamya
"""

from flask import Flask, render_template, request
import pickle

import re
#from farasa.segmenter import FarasaSegmenter
from arabert.preprocess import ArabertPreprocessor
from pyarabic.araby import strip_tashkeel,strip_tatweel
from nltk.tokenize import TweetTokenizer
model_name = "aubmindlab/bert-base-arabertv2"
arabert_prep = ArabertPreprocessor(model_name=model_name)
def preprocess_text(tweet):
    outputs=[]
    tokenizer = TweetTokenizer()
    tweet = str(tweet)
    # remove old style retweet text "RT"
    tweet = re.sub(r'^RT[\s]+', '', tweet)
    # remove hyperlinks
    tweet = re.sub(r'https?:\/\/.*[\r\n]*', '', tweet)
    # remove hashtags
    # only removing the hash # sign from the word
    tweet = re.sub(r'#', '', tweet)
    #removing mentions
    tweet = re.sub(r':', '', tweet)
    tweet = re.sub(r'@[\w]+','',tweet)
    #replace punctuations with space
    tweet = re.sub(r"[,.;@#?!&$_]+\ *", " ", tweet)
    #find arabic letters only
    tweet = ' '.join(re.findall(r'[\u0600-\u06FF]+',tweet))
    #remove tashkeel
    tweet = strip_tashkeel(tweet)
    #remove tatweel
    tweet = strip_tatweel(tweet)
    #remove numbers from string
    pattern = r'[0-9]'
    # Match all digits in the string and replace them with an empty string
    tweet = re.sub(pattern, '', tweet)
    #tokenize tweets
    tweet_tokens = tokenizer.tokenize(tweet)
    tweet_clean=[]
    for word in tweet_tokens: # Go through every word in your tokens list
        #if word not in string.punctuation:  # remove punctuation
        word_reg = re.compile(r'\w')
        if word_reg.search(word):
            tweet_clean.append(word)
    outputs.append((' '.join(tweet_clean)))
    text_preprocessed=arabert_prep.preprocess(outputs)
    return text_preprocessed

#Flask App
app = Flask(__name__)

#Load LSTM Model
lstm_model = pickle.load(open('ls_model.pkl', 'rb'))

#Home
@app.route('/')#, methods=['GET'])
def home():
    return 'Hello World!'
    #return render_template('index.html',variable=None, variables=None)

#Model Prediction
@app.route('/predict',methods=['POST'])
def predict():
    tweet = request.form['text']
    result = list()
    result.append(dict())
    result[0]['tweet'] = tweet

    dialect_tweet = preprocess_text(tweet)

    dialect_prediction = lstm_model.predict(dialect_tweet)

    dialect_proba = lstm_model.predict_proba(dialect_tweet)[0]

    #18 Classes: ['EG', 'PL', 'KW', 'LY', 'QA', 'JO', 'LB', 'SA',
    # 'AE', 'BH', 'OM', 'SY', 'DZ', 'IQ', 'SD', 'MA', 'YE', 'TN']

    result[0]['dialect'] = {}
    if dialect_prediction == "EG":
        result[0]['dialect']["EG"] = round(dialect_proba[0]*100, 2)
    if dialect_prediction == "PL":
        result[0]['dialect']["PL"] = round(dialect_proba[1]*100, 2)
    if dialect_prediction == "KW":
        result[0]['dialect']["KW"] = round(dialect_proba[2]*100, 2)
    if dialect_prediction == "LY":
        result[0]['dialect']["LY"] = round(dialect_proba[3]*100, 2)
    if dialect_prediction == "QA":
        result[0]['dialect']["QA"] = round(dialect_proba[4]*100, 2)
    if dialect_prediction == "JO":
        result[0]['dialect']["JO"] = round(dialect_proba[5]*100, 2)
    if dialect_prediction == "LB":
        result[0]['dialect']["LB"] = round(dialect_proba[6]*100, 2)
    if dialect_prediction == "SA":
        result[0]['dialect']["SA"] = round(dialect_proba[7]*100, 2)
    if dialect_prediction == "AE":
        result[0]['dialect']["AE"] = round(dialect_proba[8]*100, 2)
    if dialect_prediction == "BH":
        result[0]['dialect']["BH"] = round(dialect_proba[9]*100, 2)
    if dialect_prediction == "OM":
        result[0]['dialect']["OM"] = round(dialect_proba[10]*100, 2)
    if dialect_prediction == "SY":
        result[0]['dialect']["SY"] = round(dialect_proba[11]*100, 2)
    if dialect_prediction == "DZ":
        result[0]['dialect']["DZ"] = round(dialect_proba[12]*100, 2)
    if dialect_prediction == "IQ":
        result[0]['dialect']["IQ"] = round(dialect_proba[13]*100, 2)
    if dialect_prediction == "SD":
        result[0]['dialect']["SD"] = round(dialect_proba[14]*100, 2)
    if dialect_prediction == "MA":
        result[0]['dialect']["MA"] = round(dialect_proba[15]*100, 2)
    if dialect_prediction == "YE":
        result[0]['dialect']["YE"] = round(dialect_proba[16]*100, 2)
    if dialect_prediction == "TN":
        result[0]['dialect']["TN"] = round(dialect_proba[17]*100, 2)

    return render_template('index.html', variable=result, variables=None)

# App Main
if __name__ == "__main__":
    app.run(debug=False)

#Check
print("Final Done!")


#2 map two ist into a dictionary

list1=[]
list2=[]

size = int(input("please insert the size for the list :"))

print("insert values into the list 1")
for i in range(size):
    x = input("value 1 : ")
    list1.append(int(x))
print("insert values into the list 2")
for i in range(size):
    y = input("value 2 : ")
    list2.append(y)
dict = {}

for i in range(size):
    dict[list1[i]] = list2[i]

print("The dictionary is :",dict)


import functools
import inspect
import logging
import os

import torch
import torch.nn as nn
from torch.nn import init

from .helpers import build_model_with_cfg
from .registry import register_model

__all__ = ['UnetGenerator']

def _cfg(url='', **kwargs):
    return {
        'url': url,
        # 'interpolation': 'trilinear',
        'num_classes': 1000,
        **kwargs
    }

default_cfgs = {
    "cyclegan_2d\tlung_ct": _cfg(
        url='https://github.com/songphilips/ptimz/releases/download/v0.0.1-hrnet/cyclegan_net_G_A.pth.tar',
        paper='https://doi.org/10.3348/kjr.2020.0988',
        input_details='CT',
        slice_thickness=1,
        first_conv='model.model.0',
        num_classes=1, input_size=(1, 512, 512),
        last_layer='model.model.3',
    ),
}

def __patch_instance_norm_state_dict(state_dict, module, keys, i=0):
    """Fix InstanceNorm checkpoints incompatibility (prior to 0.4)"""
    key = keys[i]
    if i + 1 == len(keys):  # at the end, pointing to a parameter/buffer
        if module.__class__.__name__.startswith('InstanceNorm') and \
                (key == 'running_mean' or key == 'running_var'):
            if getattr(module, key) is None:
                state_dict.pop('.'.join(keys))
        if module.__class__.__name__.startswith('InstanceNorm') and \
                (key == 'num_batches_tracked'):
            state_dict.pop('.'.join(keys))
    else:
        __patch_instance_norm_state_dict(state_dict, getattr(module, key), keys, i + 1)

def load_networks(net, save_dir, epoch='latest'):
    """Load all the networks from the disk.

    Parameters:
        epoch (int) -- current epoch; used in the file name '%s_net_%s.pth' % (epoch, name)
    """
    name = 'G_A'
    load_filename = 'cyclegan_net_%s.pth' % name
    load_path = os.path.join(save_dir, load_filename)
    if isinstance(net, torch.nn.DataParallel):
        net = net.module
    print('loading the model from %s' % load_path)
    # if you are using PyTorch newer than 0.4 (e.g., built from
    # GitHub source), you can remove str() on self.device
    state_dict = torch.load(load_path, map_location=str('cpu'))
    if hasattr(state_dict, '_metadata'):
        del state_dict._metadata

    # patch InstanceNorm checkpoints prior to 0.4
    for key in list(state_dict.keys()):  # need to copy keys here because we mutate in loop
        __patch_instance_norm_state_dict(state_dict, net, key.split('.'))
    net.load_state_dict(state_dict)

def get_norm_layer(norm_type='instance'):
    """Return a normalization layer

    Parameters:
        norm_type (str) -- the name of the normalization layer: batch | instance | none

    For BatchNorm, we use learnable affine parameters and track running statistics (mean/stddev).
    For InstanceNorm, we do not use learnable affine parameters. We do not track running statistics.
    """
    if norm_type == 'batch':
        norm_layer = functools.partial(nn.BatchNorm2d, affine=True, track_running_stats=True)
    elif norm_type == 'instance':
        norm_layer = functools.partial(nn.InstanceNorm2d, affine=False, track_running_stats=False)
    elif norm_type == 'none':
        norm_layer = lambda x: Identity()
    else:
        raise NotImplementedError('normalization layer [%s] is not found' % norm_type)
    return norm_layer

class UnetSkipConnectionBlock(nn.Module):
    """Defines the Unet submodule with skip connection.
        X -------------------identity----------------------
        |-- downsampling -- |submodule| -- upsampling --|
    """

    def __init__(self, outer_nc, inner_nc, input_nc=None,
                 submodule=None, outermost=False, innermost=False, norm_layer=nn.BatchNorm2d, use_dropout=False):
        """Construct a Unet submodule with skip connections.

        Parameters:
            outer_nc (int) -- the number of filters in the outer conv layer
            inner_nc (int) -- the number of filters in the inner conv layer
            input_nc (int) -- the number of channels in input images/features
            submodule (UnetSkipConnectionBlock) -- previously defined submodules
            outermost (bool)    -- if this module is the outermost module
            innermost (bool)    -- if this module is the innermost module
            norm_layer          -- normalization layer
            user_dropout (bool) -- if use dropout layers.ConvTranspose2d
        """
        super(UnetSkipConnectionBlock, self).__init__()
        self.outermost = outermost
        if type(norm_layer) == functools.partial:
            use_bias = norm_layer.func == nn.InstanceNorm2d
        else:
            use_bias = norm_layer == nn.InstanceNorm2d
        if input_nc is None:
            input_nc = outer_nc
        downconv = nn.Conv2d(input_nc, inner_nc, kernel_size=4,
                             stride=2, padding=1, bias=use_bias)
        downrelu = nn.LeakyReLU(0.2, True)
        downnorm = norm_layer(inner_nc)
        uprelu = nn.ReLU(True)
        upnorm = norm_layer(outer_nc)

        if outermost:
            upconv = nn.ConvTranspose2d(inner_nc * 2, outer_nc,
                                        kernel_size=4, stride=2,
                                        padding=1)
            down = [downconv]
            up = [uprelu, upconv, nn.Tanh()]
            model = down + [submodule] + up
        elif innermost:
            upconv = nn.ConvTranspose2d(inner_nc, outer_nc,
                                        kernel_size=4, stride=2,
                                        padding=1, bias=use_bias)
            down = [downrelu, downconv]
            up = [uprelu, upconv, upnorm]
            model = down + up
        else:
            upconv = nn.ConvTranspose2d(inner_nc * 2, outer_nc,
                                        kernel_size=4, stride=2,
                                        padding=1, bias=use_bias)
            down = [downrelu, downconv, downnorm]
            up = [uprelu, upconv, upnorm]

            if use_dropout:
                model = down + [submodule] + up + [nn.Dropout(0.5)]
            else:
                model = down + [submodule] + up

        self.model = nn.Sequential(*model)

    def forward(self, x):
        if self.outermost:
            return self.model(x)
        else:  # add skip connections
            return torch.cat([x, self.model(x)], 1)

class ResnetGenerator(nn.Module):
    """Resnet-based generator that consists of Resnet blocks between a few downsampling/upsampling operations.

    We adapt Torch code and idea from Justin Johnson's neural style transfer project(https://github.com/jcjohnson/fast-neural-style)
    """

    def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=6,
                 padding_type='reflect'):
        """Construct a Resnet-based generator

        Parameters:
            input_nc (int)      -- the number of channels in input images
            output_nc (int)     -- the number of channels in output images
            ngf (int)           -- the number of filters in the last conv layer
            norm_layer          -- normalization layer
            use_dropout (bool)  -- if use dropout layers
            n_blocks (int)      -- the number of ResNet blocks
            padding_type (str)  -- the name of padding layer in conv layers: reflect | replicate | zero
        """
        assert (n_blocks >= 0)
        super(ResnetGenerator, self).__init__()
        if type(norm_layer) == functools.partial:
            use_bias = norm_layer.func == nn.InstanceNorm2d
        else:
            use_bias = norm_layer == nn.InstanceNorm2d

        model = [nn.ReflectionPad2d(3),
                 nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0, bias=use_bias),
                 norm_layer(ngf),
                 nn.ReLU(True)]

        n_downsampling = 2
        for i in range(n_downsampling):  # add downsampling layers
            mult = 2 ** i
            model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, padding=1, bias=use_bias),
                      norm_layer(ngf * mult * 2),
                      nn.ReLU(True)]

        mult = 2 ** n_downsampling
        for i in range(n_blocks):  # add ResNet blocks

            model += [ResnetBlock(ngf * mult, padding_type=padding_type, norm_layer=norm_layer, use_dropout=use_dropout,
                                  use_bias=use_bias)]

        for i in range(n_downsampling):  # add upsampling layers
            mult = 2 ** (n_downsampling - i)
            model += [nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2),
                                         kernel_size=3, stride=2,
                                         padding=1, output_padding=1,
                                         bias=use_bias),
                      norm_layer(int(ngf * mult / 2)),
                      nn.ReLU(True)]
        model += [nn.ReflectionPad2d(3)]
        model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)]
        model += [nn.Tanh()]

        self.model = nn.Sequential(*model)

    def forward(self, input):
        """Standard forward"""
        return self.model(input)

class UnetGenerator(nn.Module):
    """Create a Unet-based generator"""

    def __init__(self, in_chans, num_classes, num_downs, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False):
        """Construct a Unet generator
        Parameters:
            input_nc (int)  -- the number of channels in input images
            output_nc (int) -- the number of channels in output images
            num_downs (int) -- the number of downsamplings in UNet. For example, # if |num_downs| == 7,
                                image of size 128x128 will become of size 1x1 # at the bottleneck
            ngf (int)       -- the number of filters in the last conv layer
            norm_layer      -- normalization layer

        We construct the U-Net from the innermost layer to the outermost layer.
        It is a recursive process.
        """
        super(UnetGenerator, self).__init__()
        # construct unet structure
        unet_block = UnetSkipConnectionBlock(ngf * 8, ngf * 8, input_nc=None, submodule=None, norm_layer=norm_layer,
                                             innermost=True)  # add the innermost layer
        for i in range(num_downs - 5):  # add intermediate layers with ngf * 8 filters
            unet_block = UnetSkipConnectionBlock(ngf * 8, ngf * 8, input_nc=None, submodule=unet_block,
                                                 norm_layer=norm_layer, use_dropout=use_dropout)
        # gradually reduce the number of filters from ngf * 8 to ngf
        unet_block = UnetSkipConnectionBlock(ngf * 4, ngf * 8, input_nc=None, submodule=unet_block,
                                             norm_layer=norm_layer)
        unet_block = UnetSkipConnectionBlock(ngf * 2, ngf * 4, input_nc=None, submodule=unet_block,
                                             norm_layer=norm_layer)
        unet_block = UnetSkipConnectionBlock(ngf, ngf * 2, input_nc=None, submodule=unet_block, norm_layer=norm_layer)
        self.model = UnetSkipConnectionBlock(num_classes, ngf, input_nc=in_chans, submodule=unet_block, outermost=True,
                                             norm_layer=norm_layer)  # add the outermost layer

    def forward(self, input):
        """Standard forward"""
        return self.model(input)

def init_net(net, init_type='normal', init_gain=0.02, gpu_ids=[]):
    """Initialize a network: 1. register CPU/GPU device (with multi-GPU support); 2. initialize the network weights
    Parameters:
        net (network)      -- the network to be initialized
        init_type (str)    -- the name of an initialization method: normal | xavier | kaiming | orthogonal
        gain (float)       -- scaling factor for normal, xavier and orthogonal.
        gpu_ids (int list) -- which GPUs the network runs on: e.g., 0,1,2

    Return an initialized network.
    """
    if len(gpu_ids) > 0:
        assert (torch.cuda.is_available())
        net.to(gpu_ids[0])
        net = torch.nn.DataParallel(net, gpu_ids)  # multi-GPUs
    init_weights(net, init_type, init_gain=init_gain)
    return net

def init_weights(net, init_type='normal', init_gain=0.02):
    """Initialize network weights.

    Parameters:
        net (network)   -- network to be initialized
        init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal
        init_gain (float)    -- scaling factor for normal, xavier and orthogonal.

    We use 'normal' in the original pix2pix and CycleGAN paper. But xavier and kaiming might
    work better for some applications. Feel free to try yourself.
    """

    def init_func(m):  # define the initialization function
        classname = m.__class__.__name__
        if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
            if init_type == 'normal':
                init.normal_(m.weight.data, 0.0, init_gain)
            elif init_type == 'xavier':
                init.xavier_normal_(m.weight.data, gain=init_gain)
            elif init_type == 'kaiming':
                init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
            elif init_type == 'orthogonal':
                init.orthogonal_(m.weight.data, gain=init_gain)
            else:
                raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
            if hasattr(m, 'bias') and m.bias is not None:
                init.constant_(m.bias.data, 0.0)
        elif classname.find(
                'BatchNorm2d') != -1:  # BatchNorm Layer's weight is not a matrix; only normal distribution applies.
            init.normal_(m.weight.data, 1.0, init_gain)
            init.constant_(m.bias.data, 0.0)

    print('initialize network with %s' % init_type)
    net.apply(init_func)  # apply the initialization function <init_func>

def _build_cycleGAN_G(pretrained_name, ngf=64,
                      norm='instance', use_dropout=False, init_type='normal',
                      init_gain=0.02, in_chans=None, num_classes=None, **kwargs):
    net = None
    norm_layer = get_norm_layer(norm_type=norm)
    # if netG == 'resnet_9blocks':
    #     net = ResnetGenerator(input_nc, output_nc, ngf, norm_layer=norm_layer, use_dropout=use_dropout, n_blocks=9)
    # elif netG == 'resnet_6blocks':
    #     net = ResnetGenerator(input_nc, output_nc, ngf, norm_layer=norm_layer, use_dropout=use_dropout, n_blocks=6)
    # elif netG == 'unet_128':
    #     net = UnetGenerator(input_nc, output_nc, 7, ngf, norm_layer=norm_layer, use_dropout=use_dropout)
    # elif netG == 'unet_256':
    #     net = UnetGenerator(input_nc, output_nc, 8, ngf, norm_layer=norm_layer, use_dropout=use_dropout)
    # else:
    #     raise NotImplementedError('Generator model name [%s] is not recognized' % netG)
    # return init_net(net, init_type, init_gain, gpu_ids)
    pretrained = False if pretrained_name is False or pretrained_name is None else True

    # if not specify the in_chans or num_classes, this will load the default from default_cfg
    if pretrained and pretrained_name in default_cfgs.keys():
        if in_chans is None:
            input_size = default_cfgs[pretrained_name].get('input_size', None)
            if input_size is not None:
                in_chans = input_size[0]
                print(f'Load default in_chans for {pretrained_name} as {in_chans}')
            else:
                raise ValueError(f"Please specify in_chans for {pretrained_name}")

        if num_classes is None:
            num_classes = default_cfgs[pretrained_name].get('num_classes', None)
            if num_classes is None:
                raise ValueError(f"Please specify num_classes for {pretrained_name}")
            else:
                print(f'Load default num_classes for {pretrained_name} as {num_classes}')

    return build_model_with_cfg(model_cls=UnetGenerator, variant='', pretrained=pretrained,
                                default_cfg=default_cfgs.get(pretrained_name, None), pretrained_strict=False,
                                # model config
                                in_chans=in_chans,
                                num_classes=num_classes,
                                num_downs=kwargs.get('num_downs', 8))

@register_model
def cyclegan_2d(pretrained='cyclegan_2d\tlung_ct', **kwargs):
    func_name = str(inspect.stack()[0][3])
    if pretrained is True:
        pretrained = f'{func_name}\tlung_ct'
    elif isinstance(pretrained, str):
        pretrained = func_name + '\t' + pretrained
        if pretrained not in default_cfgs.keys():
            _logger = logging.getLogger(__name__)
            _logger.warning(f"No {pretrained} pretrained weights, use random initialization.")
            pretrained = False
    model = _build_cycleGAN_G(pretrained, num_downs=8, **kwargs)
    return model


#coding:utf-8
import numpy as np
import os,pathlib
import torch

root = pathlib.Path(os.path.abspath(__file__)).parent.parent.parent

class TextcnnConfig(object):
    def __init__(self):
        self.corpus_path = os.path.join(root,"data","dataset","origin","qa_corpus.xlsx")
        self.answer_map_path = os.path.join(root,"data","dataset","origin","answer_map.pkl")
        self.proc_dir = os.path.join(root,"data","dataset","proc")
        self.class_map = {"chat":0, "busi": 1,}
        self.class_map_reverse = {0:"chat",1:"busi"}
        self.stopwords_path = os.path.join(root,"data","stopwords","哈工大停用词表.txt")
        self.w2v_dir = os.path.join(root,"data","w2v")
        self.save_path = os.path.join(root,"data","textcnn_results","question_cls.h5")
        self.vocab_path = os.path.join(root,"data","w2v","vocab.pickle")
        self.embed_path = os.path.join(root,"data","w2v","embed_matrix.npy")
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

        self.vocab_size = 11368
        self.batch_size = 64
        self.max_lengths = 15
        self.pad_idx = 1
        self.embed_dim = 300
        self.filter_sizes = [2,3,4]
        self.num_filters = 64
        self.dense_units = 32
        self.num_classes = 2
        self.dropout = 0.5
        self.learning_rate = 1e-3
        self.num_epochs = 50
        self.max_grad_norm = 2.0
        self.gamma = 0.9
        self.require_improve = 2000


from fractions import Fraction

x = Fraction(2, 7) * Fraction(5, 6) * Fraction(1, 5)
y = Fraction(5, 7) * Fraction(2, 6) * Fraction(1, 5)
z = Fraction(5, 7) * Fraction(4, 6) * Fraction(2, 5)

p = x + y + z

print("show probability: ", p)
print(" ------------------------------------ ")


N = int(input())

_max = 0

for i in range(1, N+1):
    arr = [N, i]
    while True:
        if arr[-2] - arr[-1] < 0:
            break
        arr.append(arr[-2]-arr[-1])

    if _max < len(arr):
        _max = len(arr)
        result = arr

print(_max)
print(*result)


#!/usr/bin/env python3
'''
Created on 20190209
Update on 20190209
@author: Eduardo Pagotto
'''

#pylint: disable=C0301
#pylint: disable=C0103
#pylint: disable=W0703
#pylint: disable=R0913

import logging
import subprocess

def execute_conversao(comando):
    separado = comando.split(' ')
    proc = subprocess.Popen(separado,
                            #shell=True,
                            #preexec_fn=os.setsid,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT)
    proc.wait()

    logging.debug('Comando: %s status: %d', comando, proc.returncode)

    if proc.returncode != 0: # saiu com erro
        msg_erro = ''
        if proc.stderr is not None:
            for line in iter(proc.stderr.readline, b''):
                linhaErro = line.decode('utf-8')
                msg_erro = msg_erro + str(linhaErro) + ' '

        raise Exception('Falha Comando: {0} Erro [{1}]'.format(comando, msg_erro))

    # Saiu de forma correta
    msg_ok = ''
    for line in iter(proc.stdout.readline, b''):
        linhaOk = line.decode('utf-8').replace('\n', '').replace('\r', '').replace('/', '\\')
        logging.debug('%s', linhaOk)
        msg_ok = msg_ok + linhaOk + ' '

    return msg_ok


from looker_sdk import models, error
from lmanage.configurator.user_group_configuration.create_role_base import CreateRoleBase
from lmanage.utils.errorhandling import return_error_message
from tqdm import tqdm
from tenacity import retry, wait_random, wait_fixed, stop_after_attempt

class CreateInstanceRoles(CreateRoleBase):
    def __init__(self, roles, sdk, logger):
        self.role_metadata = roles
        self.sdk = sdk
        self.logger = logger

    def create_permission_lookup(self):
        all_permission_sets = self.get_all_permission_sets()
        instance_permission_set_dict = {
            perm.name: perm.id for perm in all_permission_sets}
        return instance_permission_set_dict

    def create_model_lookup(self):
        all_model_sets = self.get_all_model_sets()
        instance_model_set_dict = {
            model.name: model.id for model in all_model_sets}
        return instance_model_set_dict

    def create_allgroup_lookup(self):
        all_groups = self.sdk.all_groups()
        group_dict = {group.name: group.id for group in all_groups}
        return group_dict

    def create_allrole_lookup(self):
        all_roles = self.sdk.all_roles()
        role_dict = {role.name: role.id for role in all_roles}
        return role_dict

    @retry(wait=wait_fixed(3) + wait_random(0, 2), stop=stop_after_attempt(5))
    def update_looker_role(self, role_id: str, body: models.WriteRole):
        role = self.sdk.update_role(role_id=role_id, body=body)
        return role

    def create_instance_roles(self):
        model_lookup = self.create_model_lookup()
        permission_lookup = self.create_permission_lookup()

        role_output = []

        for r_metadata in tqdm(self.role_metadata, desc="User Role Creation", unit=" user roles", colour="#2c8558"):
            role_name = r_metadata.get('name')
            model_set_id = model_lookup.get(
                r_metadata.get('model_set') if r_metadata.get('model_set') == 'All' else r_metadata.get('model_set').lower())
            permission_set_id = permission_lookup.get(
                r_metadata.get('permission_set').lower())
            if role_name != 'Admin':
                body = models.WriteRole(
                    name=role_name,
                    permission_set_id=str(permission_set_id),
                    model_set_id=str(model_set_id)
                )
                try:
                    role = self.sdk.create_role(
                        body=body
                    )

                except error.SDKError as roleerror:
                    err_msg = return_error_message(roleerror)
                    self.logger.debug(
                        'You have hit a warning creating your role \'%s\'; warning = %s', role_name, err_msg)
                    role_id = self.sdk.search_roles(name=role_name)[0].id
                    role = self.update_looker_role(
                        role_id=str(role_id), body=body)
                temp = {}
                temp['role_id'] = role.id
                temp['role_name'] = role_name
                role_output.append(temp)
            else:
                pass
        self.logger.debug(role_output)
        return role_output

    def set_role(self, role_id: str, group_id: list) -> str:
        try:
            self.sdk.set_role_groups(role_id, group_id)
            return self.logger.debug(f'attributing {group_id} permissions on instance')
        except error.SDKError as role_err:
            err_msg = return_error_message(role_err)
            self.logger.debug(
                'You have hit a warning setting your role, warning = %s', err_msg)
            self.logger.debug(role_err)

    def attribute_instance_roles(self, created_role_metadata: list):
        role_lookup = self.create_allrole_lookup()
        group_lookup = self.create_allgroup_lookup()

        for r_metadata in tqdm(self.role_metadata, desc="Instance Role Attribution", unit=" roles", colour="#2c8558"):
            role_name = r_metadata.get('name')
            teams = r_metadata.get('teams')
            role_id = role_lookup.get(role_name)
            group_id_list = []
            for team in teams:
                group_id = group_lookup.get(team)
                group_id_list.append(group_id)
            self.set_role(role_id=role_id, group_id=group_id_list)

    def sync_roles(self):
        all_role_lookup = self.create_allrole_lookup()
        all_role_lookup.pop('Admin')
        yaml_role = [role.get('name') for role in self.role_metadata]

        for role_name in all_role_lookup.keys():
            if role_name not in yaml_role:
                role_id = all_role_lookup.get(role_name)
                self.sdk.delete_role(role_id=role_id)

    def execute(self):
        created_roles = self.create_instance_roles()
        self.attribute_instance_roles(created_role_metadata=created_roles)
        self.sync_roles()


from __future__ import annotations

__all__ = [
    "check_jax",
    "check_numpy",
    "check_pandas",
    "check_polars",
    "check_torch",
    "check_xarray",
    "is_jax_available",
    "is_numpy_available",
    "is_pandas_available",
    "is_polars_available",
    "is_torch_available",
    "is_xarray_available",
]

from importlib.util import find_spec

def is_jax_available() -> bool:
    r"""Indicates if the ``jax`` package is installed or not.

    Returns:
    -------
        bool: ``True`` if ``jax`` is available otherwise ``False``.
    """
    return find_spec("jax") is not None

def check_jax() -> None:
    r"""Checks if the ``jax`` package is installed.

    Raises:
    ------
        RuntimeError if the ``jax`` package is not installed.
    """
    if not is_jax_available():
        raise RuntimeError(
            "`jax` package is required but not installed. "
            "You can install `jax` package with the command:\n\n"
            "pip install jax\n"
        )

def is_numpy_available() -> bool:
    r"""Indicates if the ``numpy`` package is installed or not.

    Returns:
    -------
        bool: ``True`` if ``numpy`` is available otherwise ``False``.
    """
    return find_spec("numpy") is not None

def check_numpy() -> None:
    r"""Checks if the ``numpy`` package is installed.

    Raises:
    ------
        RuntimeError if the ``numpy`` package is not installed.
    """
    if not is_numpy_available():
        raise RuntimeError(
            "`numpy` package is required but not installed. "
            "You can install `numpy` package with the command:\n\n"
            "pip install numpy\n"
        )

def is_pandas_available() -> bool:
    r"""Indicates if the ``pandas`` package is installed or not.

    Returns:
    -------
        bool: ``True`` if ``pandas`` is available otherwise ``False``.
    """
    return find_spec("pandas") is not None

def check_pandas() -> None:
    r"""Checks if the ``pandas`` package is installed.

    Raises:
    ------
        RuntimeError if the ``pandas`` package is not installed.
    """
    if not is_pandas_available():
        raise RuntimeError(
            "`pandas` package is required but not installed. "
            "You can install `pandas` package with the command:\n\n"
            "pip install pandas\n"
        )

def is_polars_available() -> bool:
    r"""Indicates if the ``polars`` package is installed or not.

    Returns:
    -------
        bool: ``True`` if ``polars`` is available otherwise ``False``.
    """
    return find_spec("polars") is not None

def check_polars() -> None:
    r"""Checks if the ``polars`` package is installed.

    Raises:
    ------
        RuntimeError if the ``polars`` package is not installed.
    """
    if not is_polars_available():
        raise RuntimeError(
            "`polars` package is required but not installed. "
            "You can install `polars` package with the command:\n\n"
            "pip install polars\n"
        )

def is_torch_available() -> bool:
    r"""Indicates if the ``torch`` package is installed or not.

    Returns:
    -------
        bool: ``True`` if ``torch`` is available otherwise ``False``.
    """
    return find_spec("torch") is not None

def check_torch() -> None:
    r"""Checks if the ``torch`` package is installed.

    Raises:
        RuntimeError if the ``torch`` package is not installed.
    """
    if not is_torch_available():
        raise RuntimeError(
            "`torch` package is required but not installed. "
            "You can install `torch` package with the command:\n\n"
            "pip install torch\n"
        )

def is_xarray_available() -> bool:
    r"""Indicates if the ``xarray`` package is installed or not.

    Returns:
    -------
        bool: ``True`` if ``xarray`` is available otherwise ``False``.
    """
    return find_spec("xarray") is not None

def check_xarray() -> None:
    r"""Checks if the ``xarray`` package is installed.

    Raises:
    ------
        RuntimeError if the ``xarray`` package is not installed.
    """
    if not is_xarray_available():
        raise RuntimeError(
            "`xarray` package is required but not installed. "
            "You can install `xarray` package with the command:\n\n"
            "pip install xarray\n"
        )


import discord
import os
import gdb
import botio
from dconsts import DefaultConstants as const, DefaultRegexes as regex
from p_action import PlayerAction
from keep_alive import keep_alive

# editar: TALVEZ transformar usar a propria PlayerAction ao inves de uma instancia da classe
bot = discord.Client()
cmd = botio.CommandAnalyzer()
reply = botio.AutoResponder()
guild_member = gdb.GuildMember()
guild = gdb.GuildDB()
action = PlayerAction()

@bot.event
async def on_ready():
  print('We have logged in as {0.user}'.format(bot))
  await bot.change_presence(activity=discord.Game(name='RPG'))

@bot.event
async def on_message(msg):
  # nao responda a si mesmo nem a outros bots
  if msg.author.bot:
    return

  # ----------------------------------------------------------------------------------------------
  # respostas padrao

  # mencionou meu nome? vou contar-lhe quem sou
  elif str(bot.user.id) in msg.content:
    await reply.whoami(msg)

  # aqui vai um proverbio lendario
  elif cmd.match('legen!dary', msg, cmd.ONLY):
    await reply.legendary(msg)

  # quer saber o que eu faco?
  elif cmd.match('legen!help', msg, cmd.AND_TEXT_BODY):
    embedBox = discord.Embed()
    await reply.help(msg, cmd, bot, embedBox)

  # ----------------------------------------------------------------------------------------------
  # operacoes com banco de dados

  # definir ou consultar o canal secreto
  elif cmd.match('legen!sch', msg, cmd.AND_TEXT_BODY):
    if guild_member.is_manager(msg, guild):
      guild.update_sch(msg, cmd)
      await reply.db_sch_query(msg, guild)
    else:
      await reply.sorry(msg)

  # adicionar cargos a funcao de gerencia do canal secreto
  elif cmd.match('legen!mgmt', msg, cmd.AND_TEXT_BODY):
    if msg.author.guild_permissions.administrator:
      guild.update_mgmt_roles(msg, cmd)
      await reply.db_mgmt_query(msg, guild)
    else:
      await reply.sorry(msg)

  # remover algum registro do BD do servidor
  # editar: atualmente gerentes tem permissao para atualizar sch, mas nao deleta-lo, entretanto apenas mudar a condicao nao funcionara, afinal os gerentes nao podem modificar os membros da gerencia
  # editar: legen!del @<role_name> para remover um cargo da gerencia
  elif cmd.match('legen!del', msg, cmd.AND_TEXT_BODY):
    if msg.author.guild_permissions.administrator:
      guild.remove_record(msg, cmd)
      await reply.op_status(msg, guild)
    else:
      await reply.sorry(msg)

  # ----------------------------------------------------------------------------------------------
  # rolar dados

  elif cmd.match(regex.PLAYER_ACTION, msg):
      # await msg.channel.send(':warning: BOT UNDER MAINTENANCE :warning:')
    try:
      actionResult = action.compute(msg.content)
      actionBehavior = actionResult['behavior']

      if actionBehavior == const.HIDDEN_ACTION:
        await msg.delete()
        await reply.action_result(msg, actionResult, guild, bot)

      elif actionBehavior == const.SECRET_ACTION:
        await reply.challenge(msg)
        await reply.action_result(msg, actionResult, guild, bot)

      else:
        await reply.action_result(msg, actionResult)

    except:
      return

  # ----------------------------------------------------------------------------------------------

@bot.event
async def on_guild_remove (guild_server):
  guild.remove_from_db(guild_server)

keep_alive()
bot.run(os.getenv('TOKEN'))


# Authors: Eric Larson <larson.eric.d@gmail.com>
#          Sheraz Khan <sheraz@khansheraz.com>
#          Denis Engemann <denis.engemann@gmail.com>
#
# License: BSD (3-clause)

import numpy as np

from ..filter import next_fast_len
from ..source_estimate import _BaseSourceEstimate
from ..utils import verbose, _check_combine, _check_option

@verbose
def envelope_correlation(data, combine='mean', orthogonalize="pairwise",
                         verbose=None):
    """Compute the envelope correlation.

    Parameters
    ----------
    data : array-like, shape=(n_epochs, n_signals, n_times) | generator
        The data from which to compute connectivity.
        The array-like object can also be a list/generator of array,
        each with shape (n_signals, n_times), or a :class:`~mne.SourceEstimate`
        object (and ``stc.data`` will be used). If it's float data,
        the Hilbert transform will be applied; if it's complex data,
        it's assumed the Hilbert has already been applied.
    combine : 'mean' | callable | None
        How to combine correlation estimates across epochs.
        Default is 'mean'. Can be None to return without combining.
        If callable, it must accept one positional input.
        For example::

            combine = lambda data: np.median(data, axis=0)

    orthogonalize : 'pairwise' | False
        Whether to orthogonalize with the pairwise method or not.
        Defaults to 'pairwise'. Note that when False,
        the correlation matrix will not be returned with
        absolute values.

        .. versionadded:: 0.19
    %(verbose)s

    Returns
    -------
    corr : ndarray, shape ([n_epochs, ]n_nodes, n_nodes)
        The pairwise orthogonal envelope correlations.
        This matrix is symmetric. If combine is None, the array
        with have three dimensions, the first of which is ``n_epochs``.

    Notes
    -----
    This function computes the power envelope correlation between
    orthogonalized signals [1]_ [2]_.

    References
    ----------
    .. [1] Hipp JF, Hawellek DJ, Corbetta M, Siegel M, Engel AK (2012)
           Large-scale cortical correlation structure of spontaneous
           oscillatory activity. Nature Neuroscience 15:884–890
    .. [2] Khan S et al. (2018). Maturation trajectories of cortical
           resting-state networks depend on the mediating frequency band.
           Neuroimage 174:57–68
    """
    _check_option('orthogonalize', orthogonalize, (False, 'pairwise'))
    from scipy.signal import hilbert
    n_nodes = None
    if combine is not None:
        fun = _check_combine(combine, valid=('mean',))
    else:  # None
        fun = np.array

    corrs = list()
    # Note: This is embarassingly parallel, but the overhead of sending
    # the data to different workers is roughly the same as the gain of
    # using multiple CPUs. And we require too much GIL for prefer='threading'
    # to help.
    for ei, epoch_data in enumerate(data):
        if isinstance(epoch_data, _BaseSourceEstimate):
            epoch_data = epoch_data.data
        if epoch_data.ndim != 2:
            raise ValueError('Each entry in data must be 2D, got shape %s'
                             % (epoch_data.shape,))
        n_nodes, n_times = epoch_data.shape
        if ei > 0 and n_nodes != corrs[0].shape[0]:
            raise ValueError('n_nodes mismatch between data[0] and data[%d], '
                             'got %s and %s'
                             % (ei, n_nodes, corrs[0].shape[0]))
        # Get the complex envelope (allowing complex inputs allows people
        # to do raw.apply_hilbert if they want)
        if epoch_data.dtype in (np.float32, np.float64):
            n_fft = next_fast_len(n_times)
            epoch_data = hilbert(epoch_data, N=n_fft, axis=-1)[..., :n_times]

        if epoch_data.dtype not in (np.complex64, np.complex128):
            raise ValueError('data.dtype must be float or complex, got %s'
                             % (epoch_data.dtype,))
        data_mag = np.abs(epoch_data)
        data_conj_scaled = epoch_data.conj()
        data_conj_scaled /= data_mag
        # subtract means
        data_mag_nomean = data_mag - np.mean(data_mag, axis=-1, keepdims=True)
        # compute variances using linalg.norm (square, sum, sqrt) since mean=0
        data_mag_std = np.linalg.norm(data_mag_nomean, axis=-1)
        data_mag_std[data_mag_std == 0] = 1
        corr = np.empty((n_nodes, n_nodes))
        for li, label_data in enumerate(epoch_data):
            if orthogonalize is False:  # the new code
                label_data_orth = data_mag
                label_data_orth_std = data_mag_std
            else:
                label_data_orth = (label_data * data_conj_scaled).imag
                label_data_orth -= np.mean(label_data_orth, axis=-1,
                                           keepdims=True)
                label_data_orth_std = np.linalg.norm(label_data_orth, axis=-1)
                label_data_orth_std[label_data_orth_std == 0] = 1
            # correlation is dot product divided by variances
            corr[li] = np.dot(label_data_orth, data_mag_nomean[li])
            corr[li] /= data_mag_std[li]
            corr[li] /= label_data_orth_std
        if orthogonalize is not False:
            # Make it symmetric (it isn't at this point)
            corr = np.abs(corr)
            corr = (corr.T + corr) / 2.
        corrs.append(corr)
        del corr

    corr = fun(corrs)
    return corr


current_movies = {
    'The Matrix': '10:00',
    'Finding Dory': '12:00',
    'Jaws': '14:00',
    'Star Wars: The Force Awakens': '16:00',
}

print("We're showing these movies:")
for key in current_movies:
    print(key)

movie = input("What movie would you like the showtime for?\n")

showtime = current_movies.get(movie)
if showtime:
    print('The showtime for ' + movie + ' is ' + showtime)
else:
    print("We don't have that movie")


def print_post(preorder: list, inorder: list):
    if not preorder or not inorder:
        return

    root = preorder[0]
    mid = inorder.index(root)

    left_inorder = inorder[:mid]
    right_inorder = inorder[mid+1:]

    left_preorder = preorder[1:len(left_inorder)+1]
    right_preorder = preorder[len(left_inorder)+1:]

    print_post(left_preorder, left_inorder)
    print_post(right_preorder, right_inorder)
    print(root, end=" ")

C = int(input())
for _ in range(C):
    N = int(input())
    preorder = input().split()
    inorder = input().split()

    print_post(preorder, inorder)
    print()


import cv2
from os import listdir
import numpy as np
from datetime import datetime

path = 'C:\\Users\\ravel\\Google Drive\\M2\\Projet\\amicorpus\\IS1000a\\video\\IS1000a.C.avi'

array = [] #On sauvegarde le nombre de visages detecté

def difference(oldFrame,newFrame):

    print("Difference : ")
    diff = cv2.absdiff(oldFrame,newFrame)
    print('diff : ',diff)
#    contours = cv2.findContours(diff,mode=RETR_LIST)
#    cv2.drawContours(contours)
#    cv2.imshow(contours)
    input("Suivant")

def mse(imageA, imageB):
    # the 'Mean Squared Error' between the two images is the
    # sum of the squared difference between the two images;
    # NOTE: the two images must have the same dimension
    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
    err /= float(imageA.shape[0] * imageA.shape[1])

    # return the MSE, the lower the error, the more "similar"
    # the two images are
    return err

def detect():

    face_cascade = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml') #On import les face pour la detection
    face_cascade = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier('./cascades/haarcascade_eye.xml')
    camera = cv2.VideoCapture(path)

    camera.set(cv2.CAP_PROP_FPS, 1) #On reduit le nombre de frame a 1 pour faciliter les calculs
    i = 0

    while(True):
        ret,frame = camera.read()
        # print(type(ret),type(frame),ret)
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)

        #calcul de la difference

        try:
            difference(oldFrame,gray)
            # err = mse(oldFrame,gray)
            # print("erreur : ",err)
            # if err > 200:
            #     cv2.imshow("oldFrame",oldFrame)
            #     cv2.imshow("gray",gray)
            #     input('attente...')
        except NameError:
            pass

        array.append(len(faces))

        for (x, y, w, h) in faces:
            img = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0),2)
            roi_gray = gray[y:y + h, x:x + w]
            eyes = eye_cascade.detectMultiScale(roi_gray, 1.03,5, 0, (40, 40))

            for (ex, ey, ew, eh) in eyes:
                cv2.rectangle(img, (ex, ey), (ex + ew, ey + eh),(0, 255, 0), 2)

            # cv2.imshow("camera", frame) #cv2.imwrite('./vikings.jpg', img)
            pathRes = './contours/' + str(i) + '.jpg'
            print(pathRes , len(faces))
            cv2.imwrite(pathRes, img)
            i+=1
        # if cv2.waitKey(1000 / 12) & 0xff == ord("q"):
        #     break

        print('Nombres de visages trouvés en moyenne :' , np.mean(array))

        oldFrame = gray

    camera.release()
    cv2.destroyAllWindows()

detect()


from django.urls import path
from . import views
from .views import DeletePostView, AddDislikes,AddLikes, AddCommentDislikes, AddCommentLikes, CommentReplyView, PostDetailView, EditPostView, ProfileView,ProfileEditView,PostNotificationView

urlpatterns = [
    path("", views.starting_page_view, name="home-page"),
    path('add_post/',views.create_post_view, name="add-post"),
    path("posts", views.posts_view, name="share-thoughts"),
    path("posts/search", views.posts_search_view, name="post-search"),
    path("posts/<int:pk>", PostDetailView.as_view(), name="post-detail-page"),
    path("research-agenda/", views.research_agenda_view, name="research-agenda"),
    path("research-community/", views.research_community_view, name="research-community"),
    path("what-if/", views.what_if_view, name="what-if"),
    path("contact-us/", views.contact_us_view, name="contact-us"),
    path("about-us/", views.about_us_view, name="about-us"),
    path("process/", views.about_us_process_view, name="process"),
    path("post/<int:pk>/edit", EditPostView.as_view(), name="post-edit"),
    path("register/", views.register_view, name="register-user"),
    path("login/",views.login_view, name="user-login"),
    path("add_tag/",views.add_tag_view, name="add-tag"),
    path("add_tag/<int:post_pk>/", views.add_tag_from_post_view, name="add-tag-post"),
    path("logout/",views.logout_view, name="user-logout"),
    path("post/<int:post_pk> <int:comment_pk>/delete", views.delete_comment, name="delete-comment"),
    path("post/<int:pk>/delete", DeletePostView.as_view(), name="delete-post"),
    path("post/<int:pk>/likes", AddLikes.as_view(), name="likes"),
    path("post/<int:pk>/dislikes", AddDislikes.as_view(), name="dislikes"),
    path("comment/<int:pk>/like", AddCommentLikes.as_view(), name="comment-likes"),
    path("comment/<int:pk>/dislike", AddCommentDislikes.as_view(), name="comment-dislikes"),
    path("post/<int:post_pk>/comment/<int:pk>/reply",CommentReplyView.as_view(),name="comment-reply"),
    path("profile/<int:pk>", ProfileView.as_view(), name="profile"),
    path("profile/edit/<int:pk>", ProfileEditView.as_view(), name="profile-edit"),
    path("notification/<int:notification_pk>/post<int:post_pk>",PostNotificationView.as_view(),name="post-notification"),
]


import os
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

from data_source import DataSource
from settings import videos_folder
from util import parseTimestampToDate

class DataPlot:

    """ This class creates the video with plotted data. """

    axes_all = []
    """ All the axes """
    lines_all = []
    """ All the lines """
    sources_all = []
    """ Data sources where each value is the data to be plotted in a corresponding line """
    axes_legend = None
    """ Axes for legend """
    data_source = None
    """ Instance of DataSource """
    x_points = None
    """ Points to X axis """
    window = 1000
    """ Window of plot (amount of data by second in a frame)"""
    count = None
    """ Amount of data to be ploted in this video part. Equals to length of interval between start and end indexes. """
    part = None
    """ Video part (sequence) """
    start = None
    """ Index the refer the first sample to be plotted in this video part """
    end = None
    """ Index the refer the last sample to be plotted in this video part """
    fig = None
    """ Matplot Figure, where axes and lines are plotted """
    gs = None
    """ Matplotlib Grid Specification """

    @property
    def data(self):
        """Get data loaded.

        Returns:
            dataframe: the data.
        """
        return self.data_source.data

    @property
    def interval(self):
        """
        Get interval between two samples (in ms).

        Returns:
            int: the interval.
        """
        return self.data_source.interval

    @property
    def fps(self):
        """
        Frames per second to create video.

        Returns:
            int: the fps.
        """
        return self.data_source.fps

    @property
    def size(self):
        """
        The dataset size.

        Returns:
            int: the size.
        """
        return self.data_source.size

    @property
    def sample_start(self):
        """
        Sample where starts de video creation.
        It is used for show sample number in the legend.

        Returns:
            int: the index of start.
        """
        return self.data_source.sample_start

    def __init__(self, part, count):
        """
        Initialize the plot class.
        Instanciate a DataSource and load data.

        Args:
            part (int): index sequence of video parts.
            count (int): amount of data to be ploted in this video part.
        """

        self.data_source = DataSource()
        self.data_source.load()
        self.x_points = np.arange(self.size)

        self.part = part
        self.count = self.size if count == -1 else count

        self.start = self.part * self.count
        self.end = min(((self.part + 1) * self.count), self.size) - 1
        self.count = self.end - self.start + 1

    def createLine(self, axes, ydata, color, label):
        """
        Create a line in a plot area.

        Args:
            axes (axes): axes from the subplot.
            ydata (dataframe): data to plot in Y axis.
            color (str): color of line.
            label (str): line label (to legend).
        """

        line, = axes.plot(self.x_points, ydata, color=color, label=label)

        axes.ticklabel_format(useOffset=False, style="plain")
        self.axes_all.append(axes)
        self.lines_all.append(line)
        self.sources_all.append(ydata)

    def createSubPlotLegend(self):
        """
        Create the subplot where is plotted the legend.
        """

        self.axesLegend = self.fig.add_subplot(self.gs[0, 2])
        self.axesLegend.set_axis_off()
        legend = self.axesLegend.legend(handles=self.lines_all[0:4], loc='center')

    def createSubPlotSpeed(self):
        """
        Create the subplot where is plotted speed data in km/h.
        """

        speed_color = 'k'
        speed_label = 'Speed'
        ydata = self.data['speed'] * 3.6

        axes = self.fig.add_subplot(self.gs[0, 0:2])
        axes.set_title('', fontdict={'fontsize': 10})
        axes.set_xlabel('Sample Number')
        axes.set_ylabel('Speed (km/h)')

        self.createLine(axes=axes, ydata=ydata, color=speed_color, label=speed_label)

    def createSubPlot(self, loc, title='', ylabel='', xlabel='', field=''):
        """
        Create the subplot where is plotted the data from accelerometer, gyroscope and magnetometer.

        Args:
            loc (list): location in grid spec.
            title (str, optional): title of subplot. Defaults to ''.
            ylabel (str, optional): label of subplot Y axis. Defaults to ''.
            xlabel (str, optional): label of subplot X axis. Defaults to ''.
            field (str, optional): preffix corresponding to field data type and axis. Defaults to ''.
        """

        axes = self.fig.add_subplot(self.gs[loc[0], loc[1]])
        axes.set_title(title, fontdict={'fontsize': 10})
        axes.set_xlabel(xlabel)
        axes.set_ylabel(ylabel)

        if not (field.startswith('mag')):
            bellow_suspension_color = 'g'
            bellow_suspension_label = 'Below Suspension'
            ydata = self.data[field + '_below_suspension']
            self.createLine(axes=axes, ydata=ydata, color=bellow_suspension_color, label=bellow_suspension_label)

        above_suspension_color = 'b'
        above_suspension_label = 'Above Suspension'
        ydata = self.data[field + '_above_suspension']
        self.createLine(axes=axes, ydata=ydata, color=above_suspension_color, label=above_suspension_label)

        dashboard_color = 'r'
        dashboard_label = 'Dashboard'
        ydata = self.data[field + '_dashboard']
        self.createLine(axes=axes, ydata=ydata, color=dashboard_color, label=dashboard_label)

    def create(self):
        """
        Create the plot area.
        """

        self.fig = plt.figure(self.part, figsize=(16, 9))
        self.gs = self.fig.add_gridspec(nrows=4, ncols=3, wspace=0.25, hspace=0.5)

        self.createSubPlotSpeed()

        self.createSubPlot(loc=[1, 0], field='gyro_x', title='X-Axis', ylabel='Rotation Rate (°/s)')
        self.createSubPlot(loc=[1, 1], field='gyro_y', title='Y-Axis')
        self.createSubPlot(loc=[1, 2], field='gyro_z', title='Z-Axis')

        self.createSubPlot(loc=[2, 0], field='acc_x', ylabel='Acceleration (m/s²)')
        self.createSubPlot(loc=[2, 1], field='acc_y')
        self.createSubPlot(loc=[2, 2], field='acc_z')

        self.createSubPlot(loc=[3, 0], xlabel='Sample Number', field='mag_x', ylabel='Magnetic Field (μT)')
        self.createSubPlot(loc=[3, 1], xlabel='Sample Number', field='mag_y')
        self.createSubPlot(loc=[3, 2], xlabel='Sample Number', field='mag_z')

        self.createSubPlotLegend()

    def show(self):
        """
        Shows a live data plotting.
        """
        self.plot(show=True)

    def save(self):
        """
        Save a video with plotted data.
        """
        self.plot(save=True)

    def clear(self):
        """
        Clear all the lines.
        """

        for line in self.lines_all:
            line.set_data([], [])

    def point(self, i):
        """
        Draw the data of point i in all subplots (lines/axes).

        Args:
            i (int): index point.
        """

        position = self.start + i - 1
        startWindow = max(0, position - self.window)
        endWindow = min(max(1, position), self.end)

        xdata = self.x_points[startWindow:endWindow + 1]

        for j in range(0, len(self.lines_all)):
            ydata = self.sources_all[j][startWindow:endWindow + 1]
            self.lines_all[j].set_data(xdata, ydata)

        for j in range(0, len(self.axes_all)):
            self.axes_all[j].set_xlim(left=startWindow, right=endWindow)
            # self.axes_all[j].relim()
            # self.axes_all[j].autoscale_view()

        self.axesLegend.set_title("Time: " + parseTimestampToDate(self.data['timestamp'].iloc[position]) + " | Sample: " + str(self.sample_start + position), fontdict={'fontsize': 10})

    def plot(self, save=False, show=False):
        """
        Starts process to create a animated data plot (video).
        Each video part generation progress is showed in a loading bar.

        Args:
            save (bool, optional): if plot must be saved to a .mp4 file. Defaults to False.
            show (bool, optional): if plot must be showed in live plot. Defaults to False.
        """

        linesTuple = tuple(self.lines_all)
        load_bar = tqdm(total=self.count, position=self.part, desc='Part ' + str(self.part), ascii=True, ncols=200)

        def init():
            self.clear()
            return linesTuple

        def animate(i):
            self.point(i)
            # fig.canvas.draw()
            return linesTuple

        def progress(current_frame: int, total_frames: int):
            load_bar.update(1)

        anim = FuncAnimation(self.fig, animate, init_func=init, frames=self.count, repeat=False, interval=self.interval)

        if save:
            anim.save(os.path.join(videos_folder, "part_" + str(self.part) + ".mp4"), fps=self.fps, extra_args=['-vcodec', 'libx264'], progress_callback=progress)

        if show:
            plt.show()


from bs4 import BeautifulSoup
import requests
import re
from selenium import webdriver

def importUBetNBAPage():
    #Opens the page and returns the html inside
    browser = webdriver.Chrome()
    url = "https://ubet.com/sports/basketball/nba/nba-matches"
    browser.get(url)
    # innerHTML = browser.execute_script("return document.body.innerHTML")

    innerHTML = browser.page_source
    return innerHTML

def createUBetNBAMatchup():
    matchups = []
    currMatchup = ()
    pageHTML = importUBetNBAPage()
    #
    # ###################################
    # # print(pageHTML)
    output = open('ubetoutput.html', 'w', encoding='utf-8')
    output.write(pageHTML)
    # pageHTML = open('ubetoutput.html', 'r')
    ###################################
    soup = BeautifulSoup(pageHTML, 'lxml')
    #This gets the divs for each matchup
    teamDivs = soup.findAll('div', {"ng-multi-transclude":"content"})
    output = open('ubetoutput2.html', 'w', encoding='utf-8')
    output.write(str(teamDivs))
    for row in teamDivs:

        #Then we find the "Head to head" section,
        headToHead = row.find('div', {"class":"ubet-sub-events-summary"})

        #And then we find the name and odds for each team, which are contained in divs
        try:
            nameAndOddDivs = headToHead.findAll("div", {"class":"ubet-offer-win-only"})
        except AttributeError:
            return []

        #Then we retrieve the name and the odds, which are contained in spans of each divs

        #matchupInfo is a list of lists, with each inner list containing name and Odds
        matchupInfo = []
        for team in nameAndOddDivs:
            spans = team.findAll("span")
            spans = [span.text for span in spans]
            matchupInfo.append(spans)
        # print(matchupInfo)
        teamName1 = matchupInfo[0][0]
        teamOdds1 = matchupInfo[0][1]
        teamName2 = matchupInfo[1][0]
        teamOdds2 = matchupInfo[1][1]

        nameAndOdds1 = (teamName1, teamOdds1)
        nameAndOdds2 = (teamName2, teamOdds2)
        currMatchup = (nameAndOdds1, nameAndOdds2)
        sortedMatchup = sorted(currMatchup, key=lambda odds: odds[1], reverse=True)
        matchups.append(sortedMatchup)

    return matchups


import os
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.models as models
import torchvision.transforms as transforms
import argparse
import importlib
from PIL import ImageFile

ImageFile.LOAD_TRUNCATED_IMAGES = True

def test(model, test_loader, criterion, device, hook, args):
    smd = importlib.import_module('smdebug')
    modes = getattr(smd, 'modes')
    model.eval()
    hook.set_mode(modes.EVAL)
    running_loss = 0
    running_corrects = 0

    for inputs, labels in test_loader:
        inputs = inputs.to(device)
        labels = labels.to(device)
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        _, predictions = torch.max(outputs, 1)
        running_loss += loss.item() * inputs.size(0)
        running_corrects += torch.sum(predictions == labels.data).item()

    total_loss = running_loss / len(test_loader.dataset)
    total_acc = running_corrects / len(test_loader.dataset)
    print(
        f"Test for Hyperparameters, lr: {args.lr}, "
        f"batch size: {args.batch_size}")

    print(f"Testing Loss: {total_loss}, "
          f"Testing Accuracy: {100 * total_acc}")

def train(model, train_loader, validation_loader, loss_criterion, optimizer, device, hook):
    smd = importlib.import_module('smdebug')
    loss_counter = 0
    best_loss = 1e6
    epochs = 30
    hook.set_mode(smd.modes.TRAIN)  # set debugging hook
    image_dataset = {'train': train_loader, 'valid': validation_loader}

    for epoch in range(epochs):
        print(f"Epoch:{epoch}")
        for phase in ['train', 'valid']:
            if phase == 'train':
                model.train()
                hook.set_mode(smd.modes.TRAIN)  # set debugging hook
            else:
                model.eval()
                hook.set_mode(smd.modes.EVAL)
            running_loss = 0.0
            running_corrects = 0

            for inputs, labels in image_dataset[phase]:
                inputs = inputs.to(device)
                labels = labels.to(device)
                outputs = model(inputs)
                loss = loss_criterion(outputs, labels)

                if phase == 'train':
                    optimizer.zero_grad()
                    loss.backward()
                    optimizer.step()

                _, preds = torch.max(outputs, 1)
                running_loss += loss.item() * inputs.size(0)
                running_corrects += torch.sum(preds == labels.data)

            epoch_loss = running_loss // len(image_dataset[phase])
            epoch_acc = running_corrects // len(image_dataset[phase])

            if phase == 'valid':
                if epoch_loss < best_loss:
                    best_loss = epoch_loss
                else:
                    loss_counter += 1

            print('{} loss: {:.4f}, acc: {:.4f}, best loss: {:.4f}'.format(phase,
                                                                           epoch_loss,
                                                                           epoch_acc,
                                                                           best_loss))
        if loss_counter == 1:
            break
        if epoch == 0:
            break
    return model

def model_fn(model_dir):
    model = net()
    with open(os.path.join(model_dir, "model.pth"), "rb") as f:
        model.load_state_dict(torch.load(f))
    return model

def save_model(model, model_dir):
    print(f"Saving the model.")
    path = os.path.join(model_dir, "model.pth")
    torch.save(model.cpu().state_dict(), path)

def net():
    model = models.resnet50(pretrained=True)

    for param in model.parameters():
        param.requires_grad = False
        nfeatures = model.fc.in_features
    model.fc = nn.Sequential(
        nn.Linear(nfeatures, 512),
        nn.ReLU(inplace=True),
        nn.Linear(512, 256),
        nn.ReLU(inplace=True),
        nn.Linear(256, 133))

    return model

def create_data_loaders(data, batch_size):
    transform = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
    ])

    dataset = torchvision.datasets.ImageFolder(data, transform=transform)
    data_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True)

    return data_loader

def main(args):
    pytorch = importlib.import_module('smdebug.pytorch')

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

    print(f"Device: {device}")
    print(f'Hyperparameters: {args.lr}, Batch Size: {args.batch_size}')
    print(f'Data paths: {args.data}')

    train_loader = create_data_loaders(os.environ['SM_CHANNEL_TRAIN'], args.batch_size)
    validation_loader = create_data_loaders(os.environ['SM_CHANNEL_VALID'], args.batch_size)
    test_loader = create_data_loaders(os.environ['SM_CHANNEL_TEST'], args.batch_size)

    model = net()
    model = model.to(device)

    criterion = nn.CrossEntropyLoss(ignore_index=133)
    optimizer = optim.SGD(model.fc.parameters(), lr=args.lr)

    hook = pytorch.Hook.create_from_json_file()
    hook.register_hook(model)
    hook.register_loss(criterion)
    print("Training started......")
    model = train(model, train_loader, validation_loader, criterion, optimizer, device, hook)

    print("Testing started......")
    test(model, test_loader, criterion, device, hook, args)

    torch.save(model.cpu().state_dict(), os.path.join(args.model_dir, "model.pth"))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "--batch_size",
        type=int,
        default=64,
        metavar="N",
        help="input training batch size (default: 64)",
    )
    parser.add_argument(
        "--epochs",
        type=int,
        default=30,
        metavar="N",
        help="number of epochs (default : 30)"
    )
    parser.add_argument(
        "--lr",
        type=float,
        default=0.1,
        metavar="LR",
        help="learning rate (default: 0.1)")

    parser.add_argument('--data', type=str, default=os.environ['SM_CHANNEL_TRAIN'])
    parser.add_argument('--model_dir', type=str, default=os.environ['SM_MODEL_DIR'])
    parser.add_argument('--output_dir', type=str, default=os.environ['SM_OUTPUT_DATA_DIR'])

    args = parser.parse_args()

    main(args)


#!/usr/bin/python
# Support for python2
from __future__ import print_function
#Modify system path
import sys
sys.path.append('../pycThermopack/')
# Importing pyThermopack
from pyctp import quantum_cubic
# Importing Numpy (math, arrays, etc...)
import numpy as np
# Importing Matplotlib (plotting)
import matplotlib.pyplot as plt

# Instanciate and init Quantum-cubic object
qPR = quantum_cubic.qcubic()
qPR.init("He,H2,Ne")
qPR.set_tmin(temp=2.0)

# Plot phase envelope
z = np.array([0.01, 0.1, 0.89])
T, P, v = qPR.get_envelope_twophase(1.0e4, z, maximum_pressure=1.5e7,
                                    minimum_temperature=2.0, calc_v=True)
Tc, vc, Pc = qPR.critical(z)
plt.plot(T, P*1.0e-6)
plt.plot([Tc], [Pc*1.0e-6], "ko")
plt.ylabel(r"$P$ (MPa)")
plt.xlabel(r"$T$ (K)")
plt.title("Quantum-cubic phase diagram")
plt.show()
plt.clf()


import pandas as pd

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

class Tweet():
    '''
    Extract froms selenium tweet objects the information needed, so it can be
    added to the csv file with the rest of the user's tweets
    '''
    def __init__(self,tweet):
        '''
        Initializate all the css tags of interest.
        '''
        self.tweet = tweet
        self.css_username = 'div.css-901oao.css-bfa6kz.r-18jsvk2.r-1qd0xha.r-a023e6.r-b88u0q.r-ad9z0x.r-bcqeeo.r-3s2u2q.r-qvutc0'
                     # 'div.css-901oao.css-bfa6kz.r-hkyrab.r-1qd0xha.r-a023e6.r-b88u0q.r-ad9z0x.r-bcqeeo.r-3s2u2q.r-qvutc0'
        self.css_user_tag = 'div.css-901oao.css-bfa6kz.r-m0bqgq.r-18u37iz.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-qvutc0'
                     # 'div.css-901oao.css-bfa6kz.r-1re7ezh.r-18u37iz.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-qvutc0'
        self.css_tweet = 'div.css-901oao.r-18jsvk2.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-bnwqim.r-qvutc0'
                  # 'div.css-901oao.r-hkyrab.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-bnwqim.r-qvutc0'
        self.css_date =  'div.css-1dbjc4n.r-1d09ksm.r-18u37iz.r-1wbh5a2'
                  # 'a.css-4rbku5.css-18t94o4.css-901oao.r-m0bqgq.r-1loqt21.r-1q142lx.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-3s2u2q.r-qvutc0'
                  # 'a.r-1re7ezh.r-1loqt21.r-1q142lx.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-3s2u2q.r-qvutc0.css-4rbku5.css-18t94o4.css-901oao'
        self.css_retweet = 'a.css-4rbku5.css-18t94o4.css-901oao.r-m0bqgq.r-1loqt21.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-qvutc0'
                    # 'a.css-4rbku5.css-18t94o4.css-901oao.r-1re7ezh.r-1loqt21.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-qvutc0'
    def descomposing_tweet(self,driver):

        '''
        Decompose the selenium web element into the elements of interest, return a dictionary to
        to the pandas variable along the rest of the tweets of the user
        '''
        #USERNAME
        self._waiting_func(driver,self.css_username)
        username = self.tweet.find_element_by_css_selector(self.css_username).text

        #USER_TAG
        self._waiting_func(driver,self.css_user_tag)
        user_tag = self.tweet.find_element_by_css_selector(self.css_user_tag).text

        # TEXT
        try:
            self._waiting_func(driver,self.css_tweet)
            text = self.tweet.find_element_by_css_selector(self.css_tweet).text
        except:
            text = 'No text'

        #DATE
        self._waiting_func(driver,self.css_date)
        date_ob = tweet.find_element_by_css_selector(self.css_date)
        ad = date_ob.find_elements_by_tag_name("time")
        #attrs = driver.execute_script('var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;', ad[0])
        last_date = datetime.datetime.strptime(ad[0].get_attribute('datetime'), "%Y-%m-%dT%H:%M:%S.000Z")

        # Replies, Retweets & Likes
        self._waiting_func(driver,"div[data-testid='reply']")
        replies = self.tweet.find_element_by_css_selector("div[data-testid='reply']").text
        retweets = self.tweet.find_element_by_css_selector("div[data-testid='retweet']").text
        likes = self.tweet.find_element_by_css_selector("div[data-testid='like']").text

        # Retweet?
        # Needs to be reviewed
        self._waiting_func(driver,self.css_retweet)
        try:
            if 'Retweeted' in tweet.find_element_by_css_selector(self.css_retweet).text:
                retweet = True
            else:
                retweet = False
        except:
            retweet = False

        # New row for the pandas with all the info of the current tweet
        tweet_row = {'Username':username,'User_tag':user_tag,'Date':last_date,'Tweet':text,'Replies':replies,'Retweets':retweets,'Likes':likes,'Retweet':retweet}
        return tweet_row

    def _waiting_func(self,driver,my_element_id):
        ignored_exceptions=(NoSuchElementException,StaleElementReferenceException)
        try: #$find_element_by_class_name
            WebDriverWait(driver, 4,ignored_exceptions=ignored_exceptions).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, my_element_id)))
        except : #(NoSuchElementException, TimeoutException)
            print('Loading took too much time, may be not results')

class User():
    '''
    Class to track all the tweets of the user given the period of time
    '''
    def __init__(self,joined_date,user_tag):
        # time variables
        self.joined_date = joined_date
        self.finish_time  = date(2020,11,13)
        self.proposed_start_time = date(2019,10,1)
        # self.start_time is a dynamic variable that will constanly change
        self.start_time = _find_start_time()

        self.user_tag = user_tag
        self.status = status
        self.tweets = pd.DataFrame(columns=['Username','User_tag','Date','Tweet','Replies','Retweets','Likes','Retweet'])

    def get_timeframe_for_crawling(self):
        end_time = self.start_time + timedelta(days=5)
        start_time_str = str(self.start_time.year) + '-' + str(self.start_time.month) + '-' + str(self.start_time.day)
        end_time_str = str(end_time.year) + '-' + str(end_time.month) + '-' + str(end_time.day)
        print('Period: ' + start_time_str + ' / ' + end_time_str)
        # at the end of the iteration end_time has to be the new self.start_time
        return start_time_str,end_time_str,end_time

    def retrieving_tweets_from_user(self,driver,end_time):
        '''
        Given a timeframe and an username, this method find all the tweet this users
        made during the period of time given. It scrolls down the page until it has
        collected all the tweets
        '''

        times = 0
        previous_location = None
        red_flag = 0
        done = False
        last_location = 0
        while True:
            print('**********************')
            print('Number of time it has scrolled down: ',times)
            print('**********************')
            times += 1
            # Check if there are tweets for this period of time
            # $$$$$$$$$$$$$$$$$$$$$$$$$$$$
            # CREATE A METHOD TO CHECK RIGHT AWAY IF THERE IS ANY TWEET IN THIS PERIOD OF TIME
            # $$$$$$$$$$$$$$$$$$$$$$$$$$$$
            init_height = driver.execute_script("return document.body.scrollHeight")
            time.sleep(0.2)

            # 'div.css-1dbjc4n.r-my5ep6.r-qklmqi.r-1adg3ll.r-1ny4l3l'
            css_tweets = 'div.css-1dbjc4n.r-j7yic.r-qklmqi.r-1adg3ll.r-1ny4l3l'
            self._waiting_func(driver,css_tweets)
            tweets = driver.find_elements_by_css_selector(css_tweets)

            print('Number of tweets found: ',len(tweets))
            if len(tweets) > 0:
                for i in range(len(tweets)):
                    # checking each of the tweets

                    cur_tweet = Tweet(tweets[i])
                    tweet_row = cur_tweet.descomposing_tweet(driver)
                    self.tweets = pd_user.append(tweet_row,ignore_index=True)
                    time.sleep(0.2)
                    last_location = tweets[i].location['y']
                    print(self.tweets.shape)

            driver.execute_script("window.scrollBy(0, 1700);")
            time.sleep(1)
            finish_height = driver.execute_script("return document.body.scrollHeight")

            if last_location == previous_location:
                red_flag += 1

            if red_flag > 1:
                print('Reached bottom')
                done = True
                driver.close()
                break

            previous_location = last_location
            self.start_time = end_time

    def stop_crawling_user(self):
        '''
        Checks if we have reach the end of the timeframe that we are crawling
        '''
        if self.start_time >= self.finish_time:
            return True
        else:
            return False

    def _waiting_func(self,driver,my_element_id):
        ignored_exceptions=(NoSuchElementException,StaleElementReferenceException)
        try: #$find_element_by_class_name
            WebDriverWait(driver, 4,ignored_exceptions=ignored_exceptions).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, my_element_id)))
        except : #(NoSuchElementException, TimeoutException)
            print('Loading took too much time, may be not results')

    def _find_start_time(self):
        '''
        Establish as start date the lastest out of the proposed and the joined date
        '''
        if self.joined_date > self.proposed_start_time:
            start_time = self.joined_date
        else:
            start_time = self.proposed_start_time
        return start_time

    def save_tweets(self):
        self.tweets = self.tweets.drop_duplicates()
        self.tweets.to_csv('Users_tweets/' + self.user_tag + '.csv')

    @classmethod
    def init_user(cls,username):

        # Get the date when the user joined Twitter
        joined_date = cls.user_join_date(username)

        return cls(joined_date,username)

    @staticmethod
    def user_join_date(user_tag):
        '''
        Finds the date a user joined to twitter, by getting this date, we avoid the cralwer to
        scrape dates when the user was not even on twotter, makes things faster

        Args:   @input - user_tag of the user e.g.: @realDonalTrump
                @output - datetime variable
        '''
        query = "https://twitter.com/" + str(user_tag)

        #driver = webdriver.Chrome(chrome_driver_binary,options=options)
        driver = webdriver.Chrome(options=options)

        driver.get(query)
        time.sleep(1)
        aa = driver.find_elements_by_css_selector('span.css-901oao.css-16my406.r-m0bqgq.r-4qtqp9.r-1qd0xha.r-ad9z0x.r-zso239.r-bcqeeo.r-qvutc0')
        joined_date = datetime.datetime.strptime(aa[-1].text.replace('Joined ',''), "%B %Y").date()
        return joined_date


import struct
from random import randint
def read_file():
    #读取data里面的二进制文件
    with open("data.txt",'rb')as fp:
        size=struct.calcsize('i')
        f=fp.read(size)
        nums=[]
        while f:
            num=struct.unpack('i',f)[0]
            nums.append(num)
            f=fp.read(size)
    fp.close()
    #print(nums)
    return nums

def is_prime(n):
    #检测数字n是否为素数
    if n<2:
        return False
    if n==2:
        return True
    edg=int(n**0.5)+1
    for i in range(2,edg):
        if n%i==0:
            return False
    return True

def implement(nums:list):
    #实现函数
    nums.sort(reverse=True)
    n=len(nums)-nums.count(0)
    print('非零数的个数为:%d'%n)
    nums=nums[0:n]
    print("最大的数为:%d，最小的数为:%d"%(nums[0],nums[n-1]))
    for i in nums:
        if is_prime(i):
            print("最大的素数为:%d"%i)
            break
    s=int(n/3 if n/3==int(n/3) else n/3+1) #s代表每一段的个数
    print("中间段的最大数为:%d，最小数为:%d"%(nums[s],nums[2*s-1]))

def main():

    #生成二进制文件
    with open('data.txt','wb')as fq:
        for i in range(2048):
            num=struct.pack('i',randint(0,80))
            fq.write(num)
    fq.close()

    nums=read_file()
    implement(nums)

if __name__=='__main__':
    main()


#!/usr/bin/python
#coding=utf-8
import os
'''
	input 和 raw_input 区别：
	后者可以任意输入
'''

while True:
	word = raw_input("input word: ")
	if word == '--help' or word == '-help':
		print('input  \'--quite\'  stop me.')
		continue
	if word == '--quite':
		print('祝愿你武运隆昌！')
		break
	os.system('say ' + word)


import pandas as pd
from matplotlib import pyplot
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_squared_error
import warnings

warnings.simplefilter('ignore', category=UserWarning)

dataset = "Paleo"

series = pd.read_csv(f'{dataset}.csv', header=0, index_col=0, parse_dates=True, squeeze=True)

for header in series.iloc[:,11:17]:
    index_no = series.columns.get_loc(f'{header}')
    arima_model = ARIMA(series[f'{header}'], order=(1,0,1)).fit()
    pred = arima_model.predict(dynamic=False)
    rmse = mean_squared_error(series[f'{header}'], pred, squared=False)

    print(arima_model.summary())
    print(f"RMSE = {rmse}")

    pyplot.plot(series[f'{header}'])
    pyplot.plot(pred, color='red')
    pyplot.title(f'{series.columns[index_no]} intake over time ({dataset})')
    pyplot.xlabel('Date')
    pyplot.ylabel(f'{series.columns[index_no]}')
    pyplot.show()


#!python

__author__ = "DMcG"
__date__ = "$Jun 23, 2015 10:27:29 AM$"

import socket
import time

from io import BytesIO

from opendis.DataOutputStream import DataOutputStream
from opendis.dis7 import EntityStatePdu
from opendis.RangeCoordinates import *

UDP_PORT = 3001
DESTINATION_ADDRESS = "127.0.0.1"

udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

gps = GPS() # conversion helper

def send():
    pdu = EntityStatePdu()
    pdu.entityID.entityID = 42
    pdu.entityID.siteID = 17
    pdu.entityID.applicationID = 23
    pdu.marking.setString('Igor3d')

     # Entity in Monterey, CA, USA facing North, no roll or pitch
    montereyLocation = gps.llarpy2ecef(deg2rad(36.6),   # longitude (radians)
                                       deg2rad(-121.9), # latitude (radians)
                                       1,               # altitude (meters)
                                       0,               # roll (radians)
                                       0,               # pitch (radians)
                                       0                # yaw (radians)
                                       )

    pdu.entityLocation.x = montereyLocation[0]
    pdu.entityLocation.y = montereyLocation[1]
    pdu.entityLocation.z = montereyLocation[2]
    pdu.entityOrientation.psi = montereyLocation[3]
    pdu.entityOrientation.theta = montereyLocation[4]
    pdu.entityOrientation.phi = montereyLocation[5]

    memoryStream = BytesIO()
    outputStream = DataOutputStream(memoryStream)
    pdu.serialize(outputStream)
    data = memoryStream.getvalue()

    while True:
        udpSocket.sendto(data, (DESTINATION_ADDRESS, UDP_PORT))
        print("Sent {}. {} bytes".format(pdu.__class__.__name__, len(data)))
        time.sleep(60)

send()


import psycopg2

def gerer_user(cur, login):

    print("1 pour sanctionner un adhérent\n")
    print("2 pour blacklister un adhérent\n")
    print("3 pour mettre a jour la dette de l'adhérent\n")
    choix1 = input("Selectionnez un choix : \n")
    if choix1 == "1":
        choix2 = input("Selectionnez 1 pour suspendre temporairement un adhérent\n")
        if choix2 == "1":
            dureesanction = input("Inserez la durée de la sanction \n")
            sql = "insert into sanction values ('%s');" % (dureesanction)
            cur.execute(sql)
        else:
            exit()
    elif choix1 == "2":
        print("Etes vous sur de vouloir blacklister l'adherent ? \n")
        choix3 = input("Selectionnez 1 pour Oui et 0 pour Non\n")
        if choix3 == "1":
            numeroadherent=input("Inserez le numero de l'adherent que vous souhaitez blackliste : \n")
            sql = "update compte_adherent set blackliste='1' where numeroCarte='%s';" % (numeroadherent)
            cur.execute(sql)
        else:
            exit()

    elif choix1 == "3":
        sql = "select sommeDu from compte_adherent;"
        cur.execute(sql)
        montant = cur.fetchall()
        print("La somme due est de ", montant)
        choix4 = input("Selectionnez 1 pour mettre à jour le montant de la somme de et 0 pour quitter\n")
        if choix4 == "1":
            montantDette = input("Inserez la durée de la sanction \n")
            sql = "insert into sanction values ('%s');" % (montantDette)
            cur.execute(sql)
        else:
            exit()


from hydra._internal.hydra import Hydra

def test_config_installed():
    """
    Tests that color options are available for both hydra/hydra_logging and hydra/job_logging
    """
    hydra = Hydra(
        calling_file=None,
        calling_module="hydra_plugins.hydra_colorlog",
        config_path="conf/",
        task_function=None,
        strict=False,
    )
    assert "colorlog" in hydra.config_loader.get_group_options("hydra/job_logging")
    assert "colorlog" in hydra.config_loader.get_group_options("hydra/hydra_logging")


import numpy as np
import pandas as pd

from os.path import exists, join
from sklearn.metrics import mean_squared_error as mse

from datasets.dataloading import get_all_datasets
from experiments import results_path

def scale_loss(L, L_min, L_max):
    return (L - L_min) / (L_max - L_min)

def main():

    data = get_all_datasets()
    predictor_names = sorted(pd.read_csv('results/test_electricity_hourly_#0.csv', index_col=0).drop(columns=['y', 'best-case-real', 'worst-case-real']).columns.tolist())
    print(predictor_names)
    n_predictors = len(predictor_names)
    n_datasets = len(data)

    norm_loss_matrix = np.zeros((n_datasets, n_predictors))

    for row_idx, (ds_name, ds_index) in enumerate(data):
        ds_path = join(results_path, f'test_{ds_name}_#{str(ds_index)}.csv')
        df_test = pd.read_csv(ds_path, index_col=0)

        # Establish upper and lower loss bounds with worst/bestcase selection
        y = df_test['y'].to_numpy().squeeze()
        best_case_prediction = df_test['best-case-real'].to_numpy().squeeze()
        worst_case_prediction = df_test['worst-case-real'].to_numpy().squeeze()
        L_min = mse(y, best_case_prediction, squared=False)
        L_max = mse(y, worst_case_prediction, squared=False)

        for col_idx, model_name in enumerate(predictor_names):
            pred = df_test[model_name].to_numpy().squeeze()
            L = mse(y, pred, squared=False)
            L_scaled = scale_loss(L, L_min, L_max)
            assert L_scaled >= 0 and L_scaled <= 1, model_name

            norm_loss_matrix[row_idx, col_idx] = L_scaled

    mean_losses = np.mean(norm_loss_matrix, axis=0)
    loss_ranking = np.argsort(-mean_losses)
    for m_name, s_loss in zip(np.array(predictor_names)[loss_ranking], mean_losses[loss_ranking]):
        print(m_name, s_loss)

    print('---------------------------------------')

    loss_ranking = np.argsort(np.argsort(norm_loss_matrix, axis=1), axis=1)
    mean_losses = np.mean(loss_ranking, axis=0)

    sorting = np.argsort(-mean_losses)
    for m_name, m_mean_rank in zip(np.array(predictor_names)[sorting], mean_losses[sorting]):
        print(m_name, m_mean_rank)

if __name__ == '__main__':
    main()


_base_ = './htc_r50_fpn_1x_nuim.py'
model = dict(
    pretrained='open-mmlab://resnext101_64x4d',
    backbone=dict(
        type='ResNeXt',
        depth=101,
        groups=64,
        base_width=4,
        num_stages=4,
        out_indices=(0, 1, 2, 3),
        frozen_stages=1,
        norm_cfg=dict(type='BN', requires_grad=True),
        norm_eval=True,
        style='pytorch',
        dcn=dict(type='DCN', deform_groups=1, fallback_on_stride=False),
        stage_with_dcn=(False, True, True, True)))

# learning policy
lr_config = dict(step=[16, 19])
runner = dict(max_epochs=20)

optimizer = dict(type='SGD', lr=0.0025, momentum=0.9, weight_decay=0.0001)
load_from = 'http://download.openmmlab.com/mmdetection/v2.0/htc/htc_x101_64x4d_fpn_dconv_c3-c5_mstrain_400_1400_16x1_20e_coco/htc_x101_64x4d_fpn_dconv_c3-c5_mstrain_400_1400_16x1_20e_coco_20200312-946fd751.pth'  # noqa

img_norm_cfg = dict(
    mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
test_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(
        type='MultiScaleFlipAug',
        img_scale=(1600, 900),
        flip=False,
        transforms=[
            dict(type='Resize', keep_ratio=True),
            dict(type='RandomFlip'),
            dict(type='Normalize', **img_norm_cfg),
            dict(type='Pad', size_divisor=32),
            dict(type='ImageToTensor', keys=['img']),
            dict(
                type='Collect',
                keys=['img'],
                meta_keys=[
                    'filename', 'ori_filename', 'ori_shape', 'img_shape',
                    'pad_shape', 'scale_factor', 'flip', 'flip_direction',
                    'img_norm_cfg', 'img_info'
                ]),
        ])
]

data_root = 'data/nuscenes__/'
data = dict(
    samples_per_gpu=2,
    workers_per_gpu=3,
    train=dict(
        ann_file=data_root + 'annotations/nuimages_v1.0-train.json',
        img_prefix=data_root,
    ),
    val=dict(
        ann_file=data_root + 'nuscenes_infos_train_mono3d.coco.json',
        img_prefix=data_root,
        pipeline=test_pipeline),
    test=dict(
        ann_file=data_root + 'nuscenes_infos_train_mono3d.coco.json',
        img_prefix=data_root,
        pipeline=test_pipeline))


from cProfile import label
import sys,os
sys.path.append(os.path.join(os.getcwd(), '.'))
import aditofpython.aditofpython as tof
import numpy as np
import cv2 as cv

from enum import Enum

import matplotlib.pyplot as plt
import time

from time import perf_counter

from scipy.interpolate import make_interp_spline, BSpline
from scipy import interpolate

WINDOW_NAME_DEPTH = "Display Depth"
WINDOW_NAME_COLOR = "Display Color"
configFile = "aditofpython/tof-viewer_config.json"

class ModesEnum(Enum):
    MODE_NEAR = 0
    MODE_MEDIUM = 1
    MODE_FAR = 2

# def fps_plot(data_list, data):
#     raw_data = data[1:len(data)-1]
#     data_con = raw_data.split(", ")

#     for i in data_con :
#         i_int = int(float(i))
#         data_list.append(i_int)
#     y_axis = np.array(data_list)
#     x_axis = np.array([x for x in range (len(y_axis))])
#     nx_axis = np.linspace(1, len(x_axis), 800)
#     bspline = interpolate.make_interp_spline(x_axis,y_axis)
#     ny_axis = bspline(nx_axis)

#     plt.figure("Frame Rate")
#     plt.plot(x_axis, y_axis)
#     plt.plot(nx_axis, ny_axis)
#     plt.xlabel('Sample')
#     plt.ylabel('Frame')
#     plt.show()
#     plt.close()

# def time_plot(data_list, data):
#     raw_data = data[1:len(data)-1]
#     data_con = raw_data.split(", ")

#     for i in data_con :
#         i_int = float (i)
#         data_list.append(i_int)
#     y_axis = np.array(data_list)
#     x_axis = np.array([x for x in range (len(y_axis))])

#     # Smoothen
#     nx_axis = np.linspace(0, len(x_axis), 600)
#     bspline = interpolate.make_interp_spline(x_axis,y_axis)
#     ny_axis = bspline(nx_axis)

#     # Plot
#     plt.figure("Processing Time")
#     plt.plot(x_axis, y_axis)
#     plt.plot(nx_axis, ny_axis)
#     plt.xlabel('Sample')
#     plt.ylabel('Depth Time')
#     plt.show()
#     plt.close()

def fps_time_plot(data_list1, data1, data_list2, data2):

    # FPS Plot
    fps_raw_data = data1[1:len(data1)-1]
    fps_data_con = fps_raw_data.split(", ")

    for i in fps_data_con :
        i_int = int(float (i))
        data_list1.append(i_int)
    y_axis1 = np.array(data_list1)
    x_axis1 = np.array([x for x in range (len(y_axis1))])

    # Smoothen
    nx_axis1 = np.linspace(0, len(x_axis1)-1, 1000)
    bspline1 = make_interp_spline(x_axis1,y_axis1)
    ny_axis1 = bspline1(nx_axis1)

    # Time Plot
    time_raw_data = data2[1:len(data2)-1]
    time_data_con = time_raw_data.split(", ")

    for i in time_data_con :
        i_int = float (i)
        data_list2.append(i_int)
    y_axis2 = np.array(data_list2)
    x_axis2 = np.array([x for x in range (len(y_axis2))])

    # Smoothen
    nx_axis2 = np.linspace(1, len(x_axis2)-1, 800)
    bspline2 = make_interp_spline(x_axis2,y_axis2)
    ny_axis2 = bspline2(nx_axis2)

    fig, (ax1, ax2) = plt.subplots(1,2)
    fig.suptitle("ADTF3175D")
    ax1.plot(nx_axis1,ny_axis1)
    ax1.set_title('Frame Rate (fps)')
    ax1.set(xlabel='Sample', ylabel='FPS')
    ax2.plot(nx_axis2, ny_axis2, 'tab:orange')
    ax2.set_title('Processing Time (ms)')
    ax2.set(xlabel='Sample', ylabel='Depth time')

    plt.tight_layout()
    plt.show()
    plt.close()

if __name__ =="__main__":

    system = tof.System()

    cameras = []
    status = system.getCameraListAtIp(cameras,"10.42.0.1")
    if not status:
        print("system.getCameraList() failed with status: ", status)
    status = cameras[0].setControl("initialization_config", configFile)
    if not status:
        print("cameras[0].setControl() failed with status: ", status)

    status = cameras[0].initialize()
    if not status:
        print("cameras[0].initialize() failed with status: ", status)

    modes = []
    status = cameras[0].getAvailableModes(modes)
    if not status:
        print("system.getAvailableModes() failed with status: ", status)

    status = cameras[0].setMode(modes[ModesEnum.MODE_MEDIUM.value])
    if not status:
        print("cameras[0].setMode() failed with status: ", status)

    print (f"\nstatus {status} setMode\n")

    types = []
    status = cameras[0].getAvailableFrameTypes(types)
    if not status:
        print("system.getAvailableFrameTypes() failed with status: ", status)

    print (f"\nstatus {status}getavailableframetype {types} \n")

    status = cameras[0].setFrameType(types[0]) # types[2] is 'mp_pcm' type.

    print (f"\nstatus {status} the error is{cameras[0].setFrameType(types[0])}\n ")

    if not status:
        print("cameras[0].setFrameType() failed with status:", status)

    status = cameras[0].start()
    if not status:
        print("cameras[0].start() failed with status:", status)

    camDetails = tof.CameraDetails()
    status = cameras[0].getDetails(camDetails)
    if not status:
        print("system.getDetails() failed with status: ", status)

    # Enable noise reduction for better results
    smallSignalThreshold = 100
    cameras[0].setControl("noise_reduction_threshold", str(smallSignalThreshold))

    camera_range = 5000
    bitCount = 9
    frame = tof.Frame()

    max_value_of_IR_pixel = 2 ** bitCount - 1
    distance_scale_ir = 255.0 / max_value_of_IR_pixel
    distance_scale = 255.0 / camera_range

    # Time -initial
    prev_frame_time = 0
    new_frame_time = 0

    pre_fps = 0
    smoothing = 0.9

    before_get_data = 0
    after_get_data = 0

    ir_prev_time = 0
    ir_aft_time = 0

    dp_prev_time = 0

    # Data stamps -initial
    save_fps = []
    save_time = []

    while True:
        # Capture frame-by-frame
        status = cameras[0].requestFrame(frame)
        if not status:
            print("cameras[0].requestFrame() failed with status: ", status)

        # Capture depth -frame -time
        dp_prev_time = time.perf_counter()
        depth_data = frame.getData("depth")
        after_get_data = perf_counter()
        dp_time = after_get_data - dp_prev_time

        # Capture ir -frame -time
        ir_prev_time = time.perf_counter()
        ir_data = frame.getData("ir")
        ir_aft_time = time.perf_counter()
        ir_time = ir_aft_time -ir_prev_time

        # Capture processing -time
        total_time = dp_time - ir_time
        ct_time = total_time*1000

        display_total_time = str("{:.2f}".format(ct_time)) + " ms"

        # Latency
        latency = after_get_data - before_get_data
        before_get_data = after_get_data
        # latency = int(latency)

        depth_map = np.array(depth_data, dtype="uint16", copy=False)
        ir_map = np.array(ir_data, dtype="uint16", copy=False)

        new_shape = (int(depth_map.shape[0] / 2), depth_map.shape[1])
        depth_map = np.resize(depth_map, new_shape)
        depth_map = cv.flip(depth_map, 1)
        distance_map = depth_map
        depth_map = distance_scale * depth_map
        depth_map = np.uint8(depth_map)
        depth_map = cv.applyColorMap(depth_map, cv.COLORMAP_RAINBOW)

        # ir_map = ir_map[0: int(ir_map.shape[0] / 2), :]
        # ir_map = distance_scale_ir * ir_map
        # ir_map = np.uint8(ir_map)
        # ir_map = cv.flip(ir_map, 1)
        # ir_map = cv.cvtColor(ir_map, cv.COLORMAP_RAINBOW)

        # result = cv.addWeighted(ir_map, 0.4, depth_map, 0.6, 0)

        new_frame_time=perf_counter()

        acutal = 1 / (new_frame_time-prev_frame_time)
        prev_frame_time = new_frame_time

        fps = (pre_fps * smoothing) + (acutal * (1-smoothing))
        pre_fps = fps

        display_fps = str(int(fps)) + " fps"

        save_fps.append(fps)
        save_time.append(ct_time)

        # print (f"\nthe latency is : {latency}\n")
        print ("Frame: " + display_fps)
        print("Time: " + display_total_time)

        cv.putText(depth_map, display_fps, (7, 20), cv.FONT_HERSHEY_DUPLEX, 0.7, (255, 255, 255), 1)
        cv.putText(depth_map, display_total_time, (7, 45), cv.FONT_HERSHEY_DUPLEX, 0.7, (255, 255, 255), 1)
        # cv.putText(depth_map, display_total_time_converted, (7, 90), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv.LINE_8)
        # cv.putText(depth_map, display_fps, (7, 90), cv.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2, cv.LINE_8)

        cv.namedWindow(WINDOW_NAME_DEPTH, cv.WINDOW_AUTOSIZE)
        cv.imshow(WINDOW_NAME_DEPTH, depth_map)

        if cv.waitKey(1) >= 0:
            break
        if len(save_fps) == 1000:
            break

    # Fps data
    with open ("fps-log.txt","w") as file :
        file.write(str(save_fps))

    with open ("fps-log.txt","r") as file :
        fps_data = file.read()
    fps_data_list = []

    # Time data
    with open ("time-log.txt","w") as file :
        file.write(str(save_time))
    with open ("time-log.txt","r") as file :
        time_data = file.read()
    time_data_list = []

    # fps_plot(fps_data_list, fps_data)
    # time_plot(time_data_list, time_data)

    fps_time_plot(fps_data_list, fps_data, time_data_list, time_data)