import base
import collections
import parse_cve_tags
import parse_shortlog
import parse_signed_off_by
import pyparsing
import subprocess
from data import PatchTestInput

def headlog():
    output = subprocess.check_output(
        "cd %s; git log --pretty='%%h#%%aN#%%cD:#%%s' -1" % PatchTestInput.repodir,
        universal_newlines=True,
        shell=True
        )
    return output.split('#')

class TestMbox(base.Base):

    auh_email = 'auh@auh.yoctoproject.org'

    invalids = [pyparsing.Regex("^Upgrade Helper.+"),
                pyparsing.Regex(auh_email),
                pyparsing.Regex("uh@not\.set"),
                pyparsing.Regex("\S+@example\.com")]

    rexp_detect = pyparsing.Regex('\[\s?YOCTO.*\]')
    rexp_validation = pyparsing.Regex('\[(\s?YOCTO\s?#\s?(\d+)\s?,?)+\]')
    revert_shortlog_regex = pyparsing.Regex('Revert\s+".*"')
    prog = parse_cve_tags.cve_tag
    patch_prog = parse_cve_tags.patch_cve_tag
    signoff_prog = parse_signed_off_by.signed_off_by
    revert_shortlog_regex = pyparsing.Regex('Revert\s+".*"')
    maxlength = 90

    # base paths of main yocto project sub-projects
    paths = {
        'oe-core': ['meta-selftest', 'meta-skeleton', 'meta', 'scripts'],
        'bitbake': ['bitbake'],
        'documentation': ['documentation'],
        'poky': ['meta-poky','meta-yocto-bsp'],
        'oe': ['meta-gpe', 'meta-gnome', 'meta-efl', 'meta-networking', 'meta-multimedia','meta-initramfs', 'meta-ruby', 'contrib', 'meta-xfce', 'meta-filesystems', 'meta-perl', 'meta-webserver', 'meta-systemd', 'meta-oe', 'meta-python']
        }

    # scripts folder is a mix of oe-core and poky, most is oe-core code except:
    poky_scripts = ['scripts/yocto-bsp', 'scripts/yocto-kernel', 'scripts/yocto-layer', 'scripts/lib/bsp']

    Project = collections.namedtuple('Project', ['name', 'listemail', 'gitrepo', 'paths'])

    bitbake = Project(name='Bitbake', listemail='bitbake-devel@lists.openembedded.org', gitrepo='http://git.openembedded.org/bitbake/', paths=paths['bitbake'])
    doc     = Project(name='Documentantion', listemail='yocto@yoctoproject.org', gitrepo='http://git.yoctoproject.org/cgit/cgit.cgi/yocto-docs/', paths=paths['documentation'])
    poky    = Project(name='Poky', listemail='poky@yoctoproject.org', gitrepo='http://git.yoctoproject.org/cgit/cgit.cgi/poky/', paths=paths['poky'])
    oe      = Project(name='oe', listemail='openembedded-devel@lists.openembedded.org', gitrepo='http://git.openembedded.org/meta-openembedded/', paths=paths['oe'])

    def test_signed_off_by_presence(self):
        for commit in TestMbox.commits:
            # skip those patches that revert older commits, these do not required the tag presence
            if self.revert_shortlog_regex.search_string(commit.shortlog):
                continue
            if not self.signoff_prog.search_string(commit.payload):
                self.fail('Mbox is missing Signed-off-by. Add it manually or with "git commit --amend -s"',
                          commit=commit)

    def test_shortlog_format(self):
        for commit in TestMbox.commits:
            shortlog = commit.shortlog
            if not shortlog.strip():
                self.skip('Empty shortlog, no reason to execute shortlog format test')
            else:
                # no reason to re-check on revert shortlogs
                if shortlog.startswith('Revert "'):
                    continue
                try:
                    parse_shortlog.shortlog.parseString(shortlog)
                except pyparsing.ParseException as pe:
                    self.fail('Commit shortlog (first line of commit message) should follow the format "<target>: <summary>"',
                              commit=commit)

    def test_shortlog_length(self):
        for commit in TestMbox.commits:
            # no reason to re-check on revert shortlogs
            shortlog = commit.shortlog
            if shortlog.startswith('Revert "'):
                continue
            l = len(shortlog)
            if l > self.maxlength:
                self.fail('Edit shortlog so that it is %d characters or less (currently %d characters)' % (self.maxlength, l),
                          commit=commit)

    def test_series_merge_on_head(self):
        self.skip("Merge test is disabled for now")
        if PatchTestInput.repo.branch != "master":
            self.skip("Skipping merge test since patch is not intended for master branch. Target detected is %s" % PatchTestInput.repo.branch)
        if not PatchTestInput.repo.ismerged:
            commithash, author, date, shortlog = headlog()
            self.fail('Series does not apply on top of target branch. Rebase your series and ensure the target is correct',
                      data=[('Targeted branch', '%s (currently at %s)' % (PatchTestInput.repo.branch, commithash))])

    def test_target_mailing_list(self):
        """In case of merge failure, check for other targeted projects"""
        if PatchTestInput.repo.ismerged:
            self.skip('Series merged, no reason to check other mailing lists')

        # a meta project may be indicted in the message subject, if this is the case, just fail
        # TODO: there may be other project with no-meta prefix, we also need to detect these
        project_regex = pyparsing.Regex("\[(?P<project>meta-.+)\]")
        for commit in TestMbox.commits:
            match = project_regex.search_string(commit.subject)
            if match:
                self.fail('Series sent to the wrong mailing list. Check the project\'s README (%s) and send the patch to the indicated list' % match.group('project'),
                          commit=commit)

        for patch in self.patchset:
            folders = patch.path.split('/')
            base_path = folders[0]
            for project in [self.bitbake, self.doc, self.oe, self.poky]:
                if base_path in  project.paths:
                    self.fail('Series sent to the wrong mailing list or some patches from the series correspond to different mailing lists. Send the series again to the correct mailing list (ML)',
                              data=[('Suggested ML', '%s [%s]' % (project.listemail, project.gitrepo)),
                                    ('Patch\'s path:', patch.path)])

            # check for poky's scripts code
            if base_path.startswith('scripts'):
                for poky_file in self.poky_scripts:
                    if patch.path.startswith(poky_file):
                        self.fail('Series sent to the wrong mailing list or some patches from the series correspond to different mailing lists. Send the series again to the correct mailing list (ML)',
                                  data=[('Suggested ML', '%s [%s]' % (self.poky.listemail, self.poky.gitrepo)),('Patch\'s path:', patch.path)])

    def test_mbox_format(self):
        if self.unidiff_parse_error:
            self.fail('Series cannot be parsed correctly due to malformed diff lines. Create the series again using git-format-patch and ensure it can be applied using git am',
                      data=[('Diff line',self.unidiff_parse_error)])

    def test_commit_message_presence(self):
        for commit in TestMbox.commits:
            if not commit.commit_message.strip():
                self.fail('Mbox is missing a descriptive commit message. Please include a commit message on your patch explaining the change', commit=commit)

    def test_cve_presence_in_commit_message(self):
        if self.unidiff_parse_error:
            self.skip('Parse error %s' % self.unidiff_parse_error)

        # we are just interested in series that introduce CVE patches, thus discard other
        # possibilities: modification to current CVEs, patch directly introduced into the
        # recipe, upgrades already including the CVE, etc.
        new_patches = [p for p in self.patchset if p.path.endswith('.patch') and p.is_added_file]
        if not new_patches:
            self.skip('No new patches introduced')

        for commit in TestMbox.commits:
            # skip those patches that revert older commits, these do not required the tag presence
            if self.revert_shortlog_regex.search_string(commit.shortlog):
                continue
            if not self.patch_prog.search_string(commit.payload):
                self.skip("No CVE tag in added patch, so not needed in mbox")
            elif not self.prog.search_string(commit.payload):
                self.fail('Missing or incorrectly formatted CVE tag in mbox. Correct or include the CVE tag in the mbox with format: "CVE: CVE-YYYY-XXXX"',
                          commit=commit)

    def test_bugzilla_entry_format(self):
        for commit in TestMbox.commits:
            if not self.rexp_detect.search_string(commit.commit_message):
                self.skip("No bug ID found")
            elif not self.rexp_validation.search_string(commit.commit_message):
                self.fail('Bugzilla issue ID is not correctly formatted - specify it with format: "[YOCTO #<bugzilla ID>]"', commit=commit)

    def test_author_valid(self):
        for commit in self.commits:
            for invalid in self.invalids:
                if invalid.search_string(commit.author):
                    self.fail('Invalid author %s. Resend the series with a valid patch author' % commit.author, commit=commit)

    def test_non_auh_upgrade(self):
        for commit in self.commits:
            if self.auh_email in commit.payload:
                self.fail('Invalid author %s. Resend the series with a valid patch author' % self.auh_email, commit=commit)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Yiming Yang
# Date: 08/07/2022
# Email: y.yang2@napier.ac.uk
# Description: Define the Probabilistic UNet Model.
#              Referenced from Simon A. A. Kohl et al. (2018): A Probabilistic U-Net for Segmentation of Ambiguous Images
#              Implementation: https://github.com/stefanknegt/Probabilistic-Unet-Pytorch

import torch
import numpy as np
from torch.nn import (Module,Conv2d, Conv2d, init, ModuleList, Sequential)
from torch.distributions import Normal, Independent, kl
from .Blocks import _Conv_BN_Activation, _Resnet_Conv_BN_ReLU, init_weights, init_weights_orthogonal_normal
from .Res_UNet import Res_UNet

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# ResNet Encoder
class Encoder(Module):
    """
    A convolutional neural network, consisting of len(num_filters) times a block of no_convs_per_block convolutional layers,
    after each block a pooling operation is performed. And after each convolutional layer a non-linear (ReLU) activation function is applied.
    """
    def __init__(self, input_channels, activation='relu', padding=True, posterior=False):
        super(Encoder, self).__init__()
        self.contracting_path = ModuleList()
        self.input_channels = input_channels

        if posterior:
            #To accomodate for the mask that is concatenated at the channel axis, we increase the input_channels.
            self.input_channels += 1

        layers = []

        conv_block = _Conv_BN_Activation(num_in_channels=self.input_channels, num_out_channels=32, kernel_size=(3, 3), activation=activation)
        resnet_block_1 = _Resnet_Conv_BN_ReLU(num_in_channels=32, num_out_channels=64, kernel_size=(3, 3), activation=activation)
        resnet_block_2 = _Resnet_Conv_BN_ReLU(num_in_channels=64, num_out_channels=128, kernel_size=(3, 3), activation=activation)

        layers = [conv_block, resnet_block_1, resnet_block_2]

        self.layers = Sequential(*layers)

        self.layers.apply(init_weights)

    def forward(self, input):
        output = self.layers(input)
        return output

class AxisAlignedConvGaussian(Module):
    """
    A convolutional net that parametrizes a Gaussian distribution with axis aligned covariance matrix.
    """
    def __init__(self, input_channels, latent_dim, initializers, posterior=False):
        super(AxisAlignedConvGaussian, self).__init__()
        self.input_channels = input_channels
        self.channel_axis = 1
        # self.num_filters = num_filters
        # self.no_convs_per_block = no_convs_per_block
        self.latent_dim = latent_dim
        self.posterior = posterior
        if self.posterior:
            self.name = 'Posterior'
        else:
            self.name = 'Prior'
        self.encoder = Encoder(self.input_channels, posterior=self.posterior)
        self.conv_layer = Conv2d(128, 2 * self.latent_dim, (1,1), stride=1)
        self.show_img = 0
        self.show_seg = 0
        self.show_concat = 0
        self.show_enc = 0
        self.sum_input = 0

        init.kaiming_normal_(self.conv_layer.weight, mode='fan_in', nonlinearity='relu')
        init.normal_(self.conv_layer.bias)

    def forward(self, input, segm=None):

        #If segmentation is not none, concatenate the mask to the channel axis of the input
        if segm is not None:
            self.show_img = input
            self.show_seg = segm
            input = torch.cat((input, segm), dim=1)
            self.show_concat = input
            self.sum_input = torch.sum(input)

        encoding = self.encoder(input)
        self.show_enc = encoding

        #We only want the mean of the resulting hxw image
        # encoding = torch.mean(encoding, dim=2, keepdim=True)
        # encoding = torch.mean(encoding, dim=3, keepdim=True)

        encoding = torch.mean(encoding, dim=(2,3), keepdim=True)

        #Convert encoding to 2 x latent dim and split up for mu and log_sigma
        mu_log_sigma = self.conv_layer(encoding)

        #We squeeze the second dimension twice, since otherwise it won't work when batch size is equal to 1
        mu_log_sigma = torch.squeeze(mu_log_sigma, dim=2)
        mu_log_sigma = torch.squeeze(mu_log_sigma, dim=2)

        mu = mu_log_sigma[:,:self.latent_dim]
        log_sigma = mu_log_sigma[:,self.latent_dim:]

        #This is a multivariate normal with diagonal covariance matrix sigma
        #https://github.com/pytorch/pytorch/pull/11178
        dist = Independent(Normal(loc=mu, scale=torch.exp(log_sigma)),1)
        return dist

class Fcomb(Module):
    """
    A function composed of no_convs_fcomb times a 1x1 convolution that combines the sample taken from the latent space,
    and output of the UNet (the feature map) by concatenating them along their channel axis.
    """
    def __init__(self, num_filters, latent_dim,  num_classes, initializers, use_tile=True):
        super(Fcomb, self).__init__()
        self.num_classes = num_classes
        self.channel_axis = 1
        self.spatial_axes = [2,3]
        self.num_filters = num_filters
        self.latent_dim = latent_dim
        self.use_tile = use_tile
        self.name = 'Fcomb'

        if self.use_tile:

            resnet_block_1 = _Resnet_Conv_BN_ReLU(num_in_channels=32+self.latent_dim, num_out_channels=32, kernel_size=(1, 1))
            resnet_block_2 = _Resnet_Conv_BN_ReLU(num_in_channels=32, num_out_channels=16, kernel_size=(1, 1))
            conv_block_1 = _Conv_BN_Activation(num_in_channels=16, num_out_channels=self.num_classes, kernel_size=(1, 1))

            self.layers = [resnet_block_1, resnet_block_2]
            self.last_layer = conv_block_1

            self.layers = Sequential(*self.layers)

            if initializers['w'] == 'orthogonal':
                self.layers.apply(init_weights_orthogonal_normal)
                self.last_layer.apply(init_weights_orthogonal_normal)
            else:
                self.layers.apply(init_weights)
                self.last_layer.apply(init_weights)

    def tile(self, a, dim, n_tile):
        """
        This function is taken form PyTorch forum and mimics the behavior of tf.tile.
        Source: https://discuss.pytorch.org/t/how-to-tile-a-tensor/13853/3
        """
        init_dim = a.size(dim)
        repeat_idx = [1] * a.dim()
        repeat_idx[dim] = n_tile
        a = a.repeat(*(repeat_idx))
        order_index = torch.LongTensor(np.concatenate([init_dim * np.arange(n_tile) + i for i in range(init_dim)])).to(device)
        return torch.index_select(a, dim, order_index)

    def forward(self, feature_map, z):
        """
        Z is batch_sizexlatent_dim and feature_map is batch_sizexno_channelsxHxW.
        So broadcast Z to batch_sizexlatent_dimxHxW. Behavior is exactly the same as tf.tile (verified)
        """
        if self.use_tile:
            z = torch.unsqueeze(z,2)
            z = self.tile(z, 2, feature_map.shape[self.spatial_axes[0]])
            z = torch.unsqueeze(z,3)
            z = self.tile(z, 3, feature_map.shape[self.spatial_axes[1]])

            #Concatenate the feature map (output of the UNet) and the sample taken from the latent space
            feature_map = torch.cat((feature_map, z), dim=self.channel_axis)
            output = self.layers(feature_map)
            return self.last_layer(output)

class ProbabilisticUnet(Module):
    """
    A probabilistic UNet (https://arxiv.org/abs/1806.05034) implementation.
    input_channels: the number of channels in the image (1 for greyscale and 3 for RGB)
    num_classes: the number of classes to predict
    num_filters: is a list consisint of the amount of filters layer
    latent_dim: dimension of the latent space
    no_cons_per_block: no convs per block in the (convolutional) encoder of prior and posterior
    """

    def __init__(self, num_in_channels, num_classes, latent_dim, criterion, beta=10.0):
        super(ProbabilisticUnet, self).__init__()
        self.input_channels = num_in_channels
        self.num_classes = num_classes
        self.latent_dim = latent_dim
        self.no_convs_per_block = 3
        self.initializers = {'w':'he_normal', 'b':'normal'}
        self.beta = beta
        self.z_prior_sample = 0
        self.criterion = criterion

        self.unet = Res_UNet(self.input_channels).to(device)
        self.prior = AxisAlignedConvGaussian(self.input_channels, self.latent_dim, self.initializers).to(device)
        self.posterior = AxisAlignedConvGaussian(self.input_channels, self.latent_dim, self.initializers, posterior=True).to(device)
        self.fcomb = Fcomb(self.input_channels, self.latent_dim, self.num_classes, initializers={'w':'orthogonal', 'b':'normal'}, use_tile=True).to(device)

    def forward(self, patch, segm=None, training=True):
        """
        Construct prior latent space for patch and run patch through UNet,
        in case training is True also construct posterior latent space
        """
        if training:
            self.posterior_latent_space = self.posterior.forward(patch, segm)
        self.prior_latent_space = self.prior.forward(patch)
        self.unet_features = self.unet.forward(patch)

    def sample(self, testing=False):
        """
        Sample a segmentation by reconstructing from a prior sample
        and combining this with UNet features
        """
        if testing == False:
            z_prior = self.prior_latent_space.rsample()
            self.z_prior_sample = z_prior
        else:
            #You can choose whether you mean a sample or the mean here. For the GED it is important to take a sample.
            #z_prior = self.prior_latent_space.base_dist.loc
            z_prior = self.prior_latent_space.sample()
            self.z_prior_sample = z_prior
        return self.fcomb.forward(self.unet_features,z_prior)

    def reconstruct(self, use_posterior_mean=False, calculate_posterior=False, z_posterior=None):
        """
        Reconstruct a segmentation from a posterior sample (decoding a posterior sample) and UNet feature map
        use_posterior_mean: use posterior_mean instead of sampling z_q
        calculate_posterior: use a provided sample or sample from posterior latent space
        """
        if use_posterior_mean:
            z_posterior = self.posterior_latent_space.loc
        else:
            if calculate_posterior:
                z_posterior = self.posterior_latent_space.rsample()
        return self.fcomb.forward(self.unet_features, z_posterior)

    def kl_divergence(self, analytic=True, calculate_posterior=False, z_posterior=None):
        """
        Calculate the KL divergence between the posterior and prior KL(Q||P)
        analytic: calculate KL analytically or via sampling from the posterior
        calculate_posterior: if we use samapling to approximate KL we can sample here or supply a sample
        """
        if analytic:
            #Neeed to add this to torch source code, see: https://github.com/pytorch/pytorch/issues/13545
            kl_div = kl.kl_divergence(self.posterior_latent_space, self.prior_latent_space)
        else:
            if calculate_posterior:
                z_posterior = self.posterior_latent_space.rsample()
            log_posterior_prob = self.posterior_latent_space.log_prob(z_posterior)
            log_prior_prob = self.prior_latent_space.log_prob(z_posterior)
            kl_div = log_posterior_prob - log_prior_prob
        return kl_div

    def elbo(self, segm, analytic_kl=True, reconstruct_posterior_mean=False, loss_weight=None):
        """
        Calculate the evidence lower bound of the log-likelihood of P(Y|X)
        """

        # criterion = nn.BCEWithLogitsLoss(size_average = False, reduce=False, reduction=None)
        z_posterior = self.posterior_latent_space.rsample()

        self.kl = torch.mean(self.kl_divergence(analytic=analytic_kl, calculate_posterior=False, z_posterior=z_posterior))

        #Here we use the posterior sample sampled above
        self.reconstruction = self.reconstruct(use_posterior_mean=reconstruct_posterior_mean, calculate_posterior=False, z_posterior=z_posterior)
        reconstruction_loss = self.criterion(preds=self.reconstruction, targets=segm, pixelwise_weights=loss_weight)
        self.reconstruction_loss = torch.sum(reconstruction_loss)
        self.mean_reconstruction_loss = torch.mean(reconstruction_loss)

        return -(self.reconstruction_loss + self.beta * self.kl)


"""Models for Blogly."""
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

def connect_db(app):
    db.app = app
    db.init_app(app)

class User(db.Model):

    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    first_name = db.Column(db.String(50), nullable=False)
    last_name = db.Column(db.String(50), nullable=False)
    img_url = db.Column(db.String())

    posts = db.relationship('Post', cascade="all, delete", backref='user')

    def __repr__(self):
        return f"<User {self.first_name} {self.last_name} {self.img_url}>"

    def full_name(self):
        return f"{self.first_name} {self.last_name}"

    @classmethod
    def get_all_users(cls):
        """Get all users in the database"""
        return cls.query.order_by(cls.last_name)

    @classmethod
    def get_single_user(cls, id):
        """Get single user details from database"""
        return cls.query.get(id)

    @classmethod
    def add_user(cls, new_user):
        """Add a new user to the database"""
        db.session.add(new_user)
        db.session.commit()

    @classmethod
    def update_user(cls, user, user_data):
        """Update existing user on database"""
        user.first_name = user_data[0]
        user.last_name = user_data[1]
        user.img_url = user_data[2]
        db.session.add(user)
        db.session.commit()
        return cls.get_single_user(user.id)

    @classmethod
    def delete_user(cls, user):
        """Delete user from database"""
        cls.query.filter(cls.id == user).delete()
        db.session.commit()

class Post(db.Model):

    __tablename__ = "posts"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(50), nullable=False)
    content = db.Column(db.String(), nullable=False)
    create_at = db.Column(db.TIMESTAMP(timezone=False),
                          server_default=datetime.now().strftime("%a %b %-d  %Y, %-I:%M %p"))
    user_id = db.Column(db.Integer, db.ForeignKey(
        'users.id', ondelete="cascade"), nullable=False)

    tags = db.relationship('PostTag', cascade="all, delete", backref="post")

    def __repr__(self):
        return f"<User {self.title} {self.content} {self.create_at} {self.user_id}>"

    @classmethod
    def get_recent_posts(cls):
        """Get the 5 most recent posts"""
        return cls.query.all()

    @classmethod
    def add_post(cls, new_post):
        """Add new post to the database"""
        db.session.add(new_post)
        db.session.commit()

    @classmethod
    def get_single_post(cls, id):
        """Get single post details from database"""
        return cls.query.get(id)

    @classmethod
    def update_post(cls, post, post_data):
        """Update existing post on database"""
        post.title = post_data[0]
        post.content = post_data[1]
        db.session.add(post)
        db.session.commit()
        return cls.get_single_post(post.id)

    @classmethod
    def delete_post(cls, post):
        """Delete user from database"""
        cls.query.filter(cls.id == post).delete()
        db.session.commit()

class Tag(db.Model):

    __tablename__ = 'tags'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50), nullable=False, unique=True)

    posts = db.relationship('PostTag', cascade="all, delete", backref="tag")

    @classmethod
    def get_all_tags(cls):
        """Get all tags in the database"""
        return cls.query.all()

    @classmethod
    def add_tag(cls, new_tag):
        """Add a new tag to the database"""
        db.session.add(new_tag)
        db.session.commit()

    @classmethod
    def get_single_tag(cls, id):
        """Get single tag details from database"""
        return cls.query.get(id)

    @classmethod
    def delete_tag(cls, tag):
        """Delete tag from database"""
        cls.query.filter(cls.id == tag).delete()
        db.session.commit()

    @classmethod
    def update_tag(cls, tag, tag_data):
        """Update existing tag on database"""
        tag.name = tag_data
        db.session.add(tag)
        db.session.commit()
        return cls.get_single_tag(tag.id)

class PostTag(db.Model):
    """Tags on posts"""

    __tablename__ = "posts_tags"

    post_id = db.Column(db.Integer, db.ForeignKey(
        'posts.id', ondelete="cascade"), primary_key=True)
    tag_id = db.Column(db.Integer, db.ForeignKey(
        'tags.id', ondelete="cascade"), primary_key=True)

    @classmethod
    def add_post_tag(cls, PostTag):
        """Add a post tag relationship to the database"""
        db.session.add(PostTag)
        db.session.commit()

    @classmethod
    def delete_post_tags(cls, post_id):
        """Delete user from database"""
        cls.query.filter(cls.post_id == post_id).delete()
        db.session.commit()


import requests
import re

# function to check if URL is malicious
def is_malicious(url):
    try:
        response = requests.get(url, timeout=7)
        page_content = response.text
    except:
        return False

    # check for presence of viruses in the page content
    if re.search(r'malware|virus|trojan', page_content, re.IGNORECASE):
        return True

    # check for presence of cookies in the page content
    if re.search(r'cookie', page_content, re.IGNORECASE):
        return True

    # check if the URL is a known phishing site
    if "phishing" in url:
        return True
    else:
          return False

# main function to calculate the percentage of malicious URLs in a list
def calculate_percentage(urls):
    total_count = len(urls)
    malicious_count = 0

    for url in urls:
        if is_malicious(url):
            malicious_count += 1

    percentage = (malicious_count / total_count) * 100

    return percentage

# example usage
urls = [
    'https://www.gardensrestaurantandcatering.com',

]

percentage = calculate_percentage(urls)
print(f"Percentage of malicious URLs: {percentage}%")


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

"""Note that all formatting functions require a config that has gone through normalizer.normalize()

Only regression tests are used for these, because it's string output.
Just don't forget to replace data files if input file changes.
"""

# pylama: ignore=D102
# pylint: disable=C0116,C0330,W0212,C0103

from copy import deepcopy

import pytest

import pytabby.formatting as formatting
import pytabby.normalizer as normalizer

@pytest.mark.regression
@pytest.mark.run(order=2)
def test_regress__format_headers(data_regression, config_multiple):
    """Only multiple-type schema has headers; no need to normalize"""
    tab_num = 1  # minimum non-first ordinal of a multiple tab layout
    c = deepcopy(config_multiple)
    result = formatting._format_headers(c["tabs"], tab_num, 80)
    data = {"data": result}
    data_regression.check(data)

@pytest.mark.regression
@pytest.mark.run(order=3)
def test_regress__format_menu(data_regression, config_all_with_id):
    """Normalize first; call with different kinds of messages"""
    config, id_ = config_all_with_id
    c = deepcopy(config)
    data = {}
    if id_.find("multiple") != -1:
        tab_num = 1
    else:
        tab_num = 0
    c = normalizer.normalize(c)
    for message_type in ["string", "None"]:
        if message_type == "None":
            message = None
        elif message_type == "string":
            message = "This is a magic string, but it's okay"
    result = formatting.format_menu(c, tab_num, 80, message).split("\n")
    data["message={}".format(message)] = result
    data_regression.check(data)

@pytest.mark.regression
@pytest.mark.run(order=3)
class TestEdgeCases:
    """Edge cases to get 100% coverage. Will use only one config each"""

    def test_regress_tab_header_description_none(self, data_regression, config_multiple):
        _ = self.__class__  # just to get rid of codacy warning, I know, it's stupid
        c = deepcopy(config_multiple)
        c["tabs"][0]["tab_header_description"] = None
        result = formatting._format_headers(c["tabs"], 0, 80)
        data = {"data": result}
        data_regression.check(data)

    def test_regress_missing_tab_header_description(self, data_regression, config_multiple):
        _ = self.__class__  # just to get rid of codacy warning, I know, it's stupid
        c = deepcopy(config_multiple)
        if "tab_header_description" in c["tabs"][0].keys():
            del c["tabs"][0]["tab_header_description"]
        result = formatting._format_headers(c["tabs"], 0, 80)
        data = {"data": result}
        data_regression.check(data)

    def test_many_tabs_with_long_headers(self, data_regression, config_multiple):
        _ = self.__class__  # just to get rid of codacy warning, I know, it's stupid
        c = deepcopy(config_multiple)
        c["tabs"][0]["tab_header_input"] = "abcdefghij"
        c["tabs"][0]["tab_header_description"] = "klmnopqrstuvwxyz"
        c["tabs"][1]["tab_header_input"] = "abcdefghijx"
        c["tabs"][1]["tab_header_description"] = "klmnopqrstuvwxyzx"
        c["tabs"].append(c["tabs"][0])
        c["tabs"][-1]["tab_header_input"] = "xabcdefghij"
        c["tabs"][-1]["tab_header_description"] = "xklmnopqrstuvwxyz"
        result = formatting._format_headers(c["tabs"], 0, 80)
        data = {"data": result}
        data_regression.check(data)


#! /usr/bin/env python

import time
import rospy
import json
from geometry_msgs.msg import Twist

initial_time = -1

def callback(msg):
    # read previous data
    print('here')

    with open('velocity_x.json', 'r') as openfile:
        jsonObject = json.load(openfile)
        openfile.close()
    print(time.time())

    # append the current coordinates
    (jsonObject['velocity']).append(msg.linear.x)

    global initial_time

    if initial_time == -1:
        (jsonObject['time']).append(0)
        initial_time = time.time()
    else:
        (jsonObject['time']).append(time.time() - initial_time)

    # write the updated data into json file

    with open("velocity_x.json", "w") as outfile:
        json.dump(jsonObject, outfile)
        outfile.close()

rospy.init_node('global_Subscriber_vel_x') #change name
sub=rospy.Subscriber('/mobile_base_controller/cmd_vel',Twist,callback)

rospy.spin()

if __name__ == '__main__':
    jsonObject = {'xCoordinates': []}

    callback()


# jikwon 테이블을 대상으로 사번과 이름을 입력(로그인)해 해당 자료가 있다면
# 해당 직원이 근무하는 부서 직원을 직급별 오름차순으로 모두 출력, 직급이 같다면 이름별 오름차순 출력
# 출력 형태 : 사번, 이름, 부서명, 직급, 성별
# 다음으로 해당 직원이 관리하는 고객 자료 출력
# 출력 형태 : 고객번호, 고객명, 고객전화, 나이
import MySQLdb
import pickle
with open('mydb.dat', 'rb') as obj:
    config = pickle.load(obj)

def start():
    try:
        conn = MySQLdb.connect(**config)
        cursor = conn.cursor()
        jikwon_no = input('사번 입력:')
        jikwon_name = input('이름 입력:')
        sql = '''
                select jikwon_no, jikwon_name, buser_name, jikwon_jik, jikwon_gen from jikwon
                join buser on buser_num = buser_no
                where buser_num = (select buser_num from jikwon where jikwon_no={0} and jikwon_name="{1}")
                order by jikwon_jik, jikwon_name
                '''.format(jikwon_no, jikwon_name)

        cursor.execute(sql)
        datas = cursor.fetchall()

        if len(datas) == 0:
            return
        for r in cursor:
            print(r[0], r[1], r[2], r[3], r[4])
        print('인원수 : ' + str(len(datas)))

        sql = '''
                select gogek_no, gogek_name, gogek_tel, cast(2023 -((left(gogek_jumin, 2))+1900)as signed integer) from gogek
                inner join jikwon on gogek_damsano = jikwon_no
                where gogek_damsano = (select jikwon_no from jikwon where jikwon_no={0} and jikwon_name="{1}")
                '''.format(jikwon_no, jikwon_name)

        cursor.execute(sql)
        datas2 = cursor.fetchall()

        print("\n-고객 정보-")
        if len(datas2) == 0:
            print(jikwon_name + '의 담당 고객은 없습니다')
            return
        for gogek_no, gogek_name, gogek_tel, gogek_jumin in datas2:
            print(gogek_no, gogek_name, gogek_tel, gogek_jumin)
        print('인원수 : ' + str(len(datas2)))

    except Exception as e:
        print("err : ", e)
    finally:
        cursor.close()
        conn.close()

if __name__ == '__main__':
    start()


from datetime import datetime

class Trade:
    def __init__(self, date, time, type, qty, strike, expiry, kind, exchange, tradeId, product, side=None):
        self.date = date
        self.time = time
        self.type = type
        self.qty = qty
        self.strike = strike
        self.expiry = expiry
        self.kind = kind
        self.exchange = exchange
        self.tradeId = tradeId
        self.product = product
        self.side = side

    def isWithinOneMinute(self, anotherTrade):
        t1 = datetime.strptime(self.time, "%H:%M:%S")
        t2 = datetime.strptime(anotherTrade.time, "%H:%M:%S")
        delta = t1 - t2
        return 0 <= delta.total_seconds() <= 60

    def isFrontRunner(self, anotherTrade):
        if self.type == anotherTrade.type:
            return False
        if not self.isWithinOneMinute(anotherTrade):
            return False
        if self.product != anotherTrade.product:
            return False
        if self.kind != anotherTrade.kind:
            return False
        if self.exchange == 'CBOE':
            if (self.qty < 0 and anotherTrade.qty >= 0) or (self.qty >= 0 and anotherTrade.qty < 0):
                return False
        else:
            if self.side != anotherTrade.side:
                return False
        if self.expiry != anotherTrade.expiry:
            return False
        if self.strike != anotherTrade.strike:
            return False

        # All conditions have been met for the front runner
        return True


# Import necessary libraries
import json
from flask import Flask, render_template, request, url_for, send_from_directory, jsonify
from werkzeug.utils import secure_filename
from flask_cors import CORS, cross_origin
import service as serve

app = Flask(__name__) # Create an instance of Flask
cors = CORS(app) # Enable CORS for the app

# Define the default route for the app
@app.route('/')
def hello_world():
    # When the root route is accessed, return the string 'Safe Vision'
    return 'Safe Vision'

# Define a new route for file uploading
@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
    abFrames = []  # Initialize an empty list for abnormal frames
    if request.method == 'POST': # If the request method is POST, process the uploaded file
        f = request.files['file'] # Retrieve the file from the request
        f.save(secure_filename("input." + f.filename.split('.')[-1]))  # Save the file securely
        filename = str("input." + f.filename.split('.')[-1])  # Store the filename for later use
        abFrames = serve.makePredict(filename) # Call a function to make a prediction on the uploaded file
        # serve.makeVideo()
        serve.objectDetection(filename)  # Call a function to perform object detection on the uploaded file
        # Prepare a response with the video and report file URLs and prediction results
        result = jsonify(
            {"url": url_for('static', filename='output.mp4'), "report": url_for('static', filename='report.txt'),
             "abnormals": abFrames})
        f = open("static/data.json", "w")  # Open a file to save the response
        json.dump(result.get_json(), f)  # Write the response to the file
        f.close()  # Close the file
        return result # Return the result

# Define a new route for serving video files
@app.route('/videos/<path:path>')
def serve_static(path):
    # When the route is accessed, return the corresponding video file
    return send_from_directory('videos', path, mimetype='video/mp4')

# Run the Flask app only if the script is executed directly
if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')


import pygame
import random

group = []
class Particles(pygame.sprite.Sprite):
    "Creates particles starting from pos with a color"

    def __init__(self, pos, y_dir, color, sparse=0, turn="off"):
        super(Particles, self).__init__()
        self.particles_list = []
        self.pos = pos
        self.color = color
        self.y_dir = y_dir
        self.sparse = sparse # generate particles not from the same starting point
        # self.generate_particles()
        group.append(self)
        self.turn = turn # this makes the effect visible

    def choose_y_dir(self):
        "Makes particles go in every direction you want"

        # Make the flow go down
        if self.y_dir == "down":
            y_dir = 2

        elif self.y_dir == "up":
            y_dir = -2

        # Make the particles spread all y_dirs
        elif self.y_dir == "all":
            y_dir = random.randrange(-2, 2, 1)

        return y_dir

    def generate_particles(self):
        "List with position etc of particles"

        if self.sparse == 1:
            self.pos[0] = random.randint(0, 600)

        # setting the data for each particles
        origin = [self.pos[0], self.pos[1]] # Starting here each particles
        y_dir = self.choose_y_dir()
        x_dir = random.randint(0, 20) / 10 - 1
        dirs = [x_dir, y_dir] # movement
        radius = random.randint(4,6) # radius
        # Appending data to the list
        self.particles_list.append([origin, dirs, radius])
        self.generate_movements()

    def generate_movements(self):

        # Moving the coordinates and size of self.particles_list
        for particle in self.particles_list[:]:
            particle[0][0] += particle[1][0] # x pos += x_dir
            particle[0][1] += particle[1][1] # y pos += y_dir
            particle[2] -= 0.05 # how fast circles shrinks
            particle[1][1] += 0.01 # circles speed
            # if particle[2] <= 0:
            if particle[2] >= 0:
                self.particles_list.remove(particle)
            # do not call draw from here: it slows down the frame rate
            # self.draw()

    def draw(self):
        "Draws particles based on data in the self.particles_list"
        if self.turn == "on":
            for particle in self.particles_list:
                 pygame.draw.circle(
                    screen, (self.color),
                (round(particle[0][0]), round(particle[0][1])),
                 round(particle[2]))


""" Make input station file for HypoDD
"""
import os
import config

# i/o paths
cfg = config.Config()
fout = open('input/station.dat','w')

done_list = []
f=open(cfg.fsta); lines=f.readlines(); f.close()
for line in lines:
    codes = line.split(',')
    net, sta = codes[0].split('.')
    if sta in done_list: continue
    done_list.append(sta)
    lat, lon = [float(code) for code in codes[1:3]]
    fout.write('{} {} {}\n'.format(sta, lat, lon))
fout.close()


from google.appengine.ext import db#
import logging#
import myutils#
import cacheMgr

class StockPrice(db.Model):
    stock_id = db.StringProperty(required=True)
    price = db.StringProperty(required=True)
    detail = db.StringProperty(required=True)
    date = db.DateProperty(auto_now_add=True)

def getPriceList(stock_id):
    priceList = cacheMgr.getStockPrice(stock_id)
    logging.debug("get price from cache for price%s is None? %s" % (stock_id, \
                  str(priceList == None)))
    if priceList:
        return priceList
    query = StockPrice.gql("WHERE stock_id=:1 order by date desc",
		                                stock_id)
    query = query.fetch(limit=myutils.PER_PRICE_COUNT*myutils.PRICE_LINES)
    logging.debug("get price for %s , count is: %s" % (stock_id, str(len(query))))
    cacheMgr.cacheStockPrice(stock_id, query)
    return query


from elasticsearch import Elasticsearch

def analyze(sentence):
    results = Elasticsearch().indices.analyze(
        index = "data",
        body={
            "analyzer": "my_analyzer",
            "text": sentence
        }
    )
    print([key["token"] for key in results["tokens"]])

while(True):
    analyze(input(" : "))


### resize image to 1920x1080 pixels ###

import cv2, math, os, glob, shutil

folder_fish = ['pod', 'ku_lare', 'see_kun', 'too',
                'khang_pan', 'hang_lueang', 'sai_dang', 'sai_dum']

for name_image in folder_fish:

    # create folder resized for add image resize here
    name_dir = f"resized/{name_image}"
    if not os.path.isdir(name_dir):
        os.makedirs(name_dir)

    images =[]
    images_path = glob.glob(f"{name_image}/*.jpg")

    for img_path in images_path:
        name_im = img_path.split("\\")[1]
        im = cv2.imread(img_path)

        height_img, width_img, channel = im.shape

        # resize to 1920, 1080
        if height_img < width_img and height_img > 1920 and width_img > 1080:
            div = width_img / 1920
            new_height = math.ceil(height_img / div)
            new_width = math.ceil(width_img /div)
            reimg = cv2.resize(im, (new_width, new_height))
            cv2.imwrite(f"resized/{name_image}/{name_im}",reimg)

        # image vertical
        elif height_img > width_img and height_img > 1920 and width_img > 1080:
            a = height_img / 1920
            new_width = math.ceil(width_img /a)
            new_height = math.ceil(height_img / a)
            shutil.copy2(f"augmented/{name_image}/{name_im}", f"resized/{name_image}")

        # image size small
        elif height_img <= 1920 or width_img <= 1920:
            shutil.copy2(f"augmented/{name_image}/{name_im}", f"resized/{name_image}")


from flask import Flask, request, render_template, redirect, session
import random
import datetime
app = Flask(__name__)
app.secret_key = "SuchWowMuchNinja"

@app.route('/')
def root():
    if 'gold' not in session:
        session['gold'] = 0
    if 'strs' not in session:
        session['strs'] = []
    return render_template('index.html')

@app.route('/process/<location>', methods=['POST'])
def process_post(location):
    logStr = ""
    if location == "farm":
        result = random.randint(10,20)
    if location == "cave":
        result = random.randint(5,10)
    if location == "house":
        result = random.randint(2,5)
    if location == "casino":
        result = random.randint(-50,50)
        if result > 0:
            logStr = ("won", "Entered a Casino and won " + str(result) + " gold. Sick! " + str(datetime.datetime.now()))
        elif result < 0:
            logStr = ("lose", "Entered a Casino and lost " + str(result) + " gold. Sucks to suck. " + str(datetime.datetime.now()))
    if location != "casino":
        logStr = ("black", "Earned " + str(result) + " gold from the " + location + "! " + str(datetime.datetime.now()))
    session['gold'] += result
    new_list = session['strs']
    new_list.append(logStr)
    session['strs'] = new_list
    return redirect('/')

@app.route('/reset')
def reset():
    session.clear()
    return redirect('/')

app.run(debug=True)


"""
Python script to summarize small and large text
"""
# =====================================================================
# Import
# =====================================================================

# import internal modules
import re
import requests
from typing import List, Set, Dict, TypedDict, Tuple, Optional

# import 3rd-party modules
from bs4 import BeautifulSoup as bs
from gensim.summarization import summarize as gensim_summarize
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline

# import local modules
from app.decorators import timer

# load pre-trained summarization pipeline
# global summarizer
# summarizer = pipeline("summarization")

# =====================================================================
# Functions
# =====================================================================

# decorator to measure runtime of function
@timer
def gensim_summary(text, ratio=0.2, word_count=None, split=False):
    """
    Function to perform extractive text summarization
    with Gensim
    * ratio = proportion of summary compared with text
    * word_count = number of words in summary
    """
    return gensim_summarize(text, ratio, word_count, split)

# =====================================================================
# Classes
# =====================================================================

class TransformersPipeline():
    """
    TransformersPipeline class is a wrapper for HuggingFace transformers
    """
    def __init__(self, model_identifier: str, pipeline) -> None:
        """
        Function to create an instance of PipelineSummarizer class
        model_identifier = string identifier of model:
        e.g. "facebook/bart-large-cnn"
        """
        self.tokenizer = AutoTokenizer.from_pretrained(model_identifier)
        self.model = AutoModelForSeq2SeqLM.from_pretrained(model_identifier)
        self.summarizer = pipeline("summarization", model=self.model, tokenizer=self.tokenizer)

    def chunk_text(self, text: str) -> List[str]:
        """
        Function to chunk long text
        according to tokenizer.model_max_length
        """
        # Get max number of inputs possible for model
        max_chunk_len = self.tokenizer.model_max_length

        # Replace [.?!] by [.?!]<EOS>
        pattern = re.compile(r'(?P<punctuation>[.?!])')
        text = pattern.sub('\g<punctuation><EOS>', text)

        # split in sentences
        sentences: str = text.split('<EOS>')

        # create list and current_chunk counter
        chunks: List[str] = []
        current_chunk: int = 0

        for sentence in sentences:
            if len(chunks) == current_chunk + 1:

                # if adding a sentence doesn't exceed max_chunk_len
                if len(chunks[current_chunk]) + len(sentence.split(' ')) <= max_chunk_len:

                    # add tokens to current chunk
                    chunks[current_chunk].extend(sentence.split(' '))

                # else add tokens to next chunk
                else:
                    current_chunk += 1
                    chunks.append(sentence.split(' '))

            # add tokens to 1st chunk
            else:
                chunks.append(sentence.split(' ')[:max_chunk_len])

        # for each chunk, join tokens to make a single string
        for i in range(len(chunks)):
            chunks[i] = ' '.join(chunks[i])

        return chunks

    # decorator to measure runtime of function
    @timer
    def summarize(self, text, min_length=30, max_length=200):
        """
        Function to summarize a text
        or list of text(to deal with long documents)
        """
        # summarize text
        summaries = self.summarizer(text, min_length=min_length, max_length=max_length, do_sample=False)
        summary = ' '.join([summary['summary_text'] for summary in summaries])

        return summary

# =====================================================================
# Main Function
# =====================================================================

def main():
    book_html_link = "https://www.gutenberg.org/files/103/103-h/103-h.htm"

    # scrape book page
    response = requests.get(book_html_link)
    print(book_html_link, response.status_code)

    # scrape
    soup = bs(response.text, 'html.parser')
    results = soup.find_all(['h4', 'p'])
    text = [result.text for result in results]
    book = ' '.join(text)

    # Extractive summary
    extractive_summary = gensim_summary(book)

    # Instantiate TransformersPipeline class
    facebook_bart_large_cnn = TransformersPipeline("facebook/bart-large-cnn", pipeline)
    extractive_summary_chunks = facebook_bart_large_cnn.chunk_text(extractive_summary)

    summary = facebook_bart_large_cnn.summarize(extractive_summary_chunks)

    # export to txt files
    with open("static/data/extractive_summary_gensim.txt", "w") as file:
        file.write(extractive_summary)

    with open("static/data/summary_facebook_bart.txt", "w") as file:
        file.write(summary)

# =====================================================================
# Run
# =====================================================================

# execute main() if you directly run this program
if __name__ == '__main__':
    main()


# image_segmentation.py
"""Volume 1: Image Segmentation.
Marcelo Leszynski
Math 345 Sec 005
11/06/20
"""

import numpy as np
import math
from scipy import linalg as la
from scipy import sparse
from imageio import imread
from scipy.sparse.linalg import eigsh
from matplotlib import pyplot as plt

# Problem 1
def laplacian(A):
    """Compute the Laplacian matrix of the graph G that has adjacency matrix A.

    Parameters:
        A ((N,N) ndarray): The adjacency matrix of an undirected graph G.

    Returns:
        L ((N,N) ndarray): The Laplacian matrix of G.
    """
    # calculate the degree matrix
    D = np.diag(np.sum(A, axis = 0))
    # return the laplacian matrix
    return D - A

# Problem 2
def connectivity(A, tol=1e-8):
    """Compute the number of connected components in the graph G and its
    algebraic connectivity, given the adjacency matrix A of G.

    Parameters:
        A ((N,N) ndarray): The adjacency matrix of an undirected graph G.
        tol (float): Eigenvalues that are less than this tolerance are
            considered zero.

    Returns:
        (int): The number of connected components in G.
        (float): the algebraic connectivity of G.
    """
    # create a laplacian of A and find its eigenvalues #########################
    L = laplacian(A)
    eigens = np.sort(np.real(la.eigvals(L)))  # sort to make finding algebraic connectivity easier
    num_connected = 0

    # calculate number of connected nodes ######################################
    for i in eigens:
        if i < tol:
            num_connected += 1

    return num_connected, eigens[1]

# Helper function for problem 4.
def get_neighbors(index, radius, height, width):
    """Calculate the flattened indices of the pixels that are within the given
    distance of a central pixel, and their distances from the central pixel.

    Parameters:
        index (int): The index of a central pixel in a flattened image array
            with original shape (radius, height).
        radius (float): Radius of the neighborhood around the central pixel.
        height (int): The height of the original image in pixels.
        width (int): The width of the original image in pixels.

    Returns:
        (1-D ndarray): the indices of the pixels that are within the specified
            radius of the central pixel, with respect to the flattened image.
        (1-D ndarray): the euclidean distances from the neighborhood pixels to
            the central pixel.
    """
    # Calculate the original 2-D coordinates of the central pixel.
    row, col = index // width, index % width

    # Get a grid of possible candidates that are close to the central pixel.
    r = int(radius)
    x = np.arange(max(col - r, 0), min(col + r + 1, width))
    y = np.arange(max(row - r, 0), min(row + r + 1, height))
    X, Y = np.meshgrid(x, y)

    # Determine which candidates are within the given radius of the pixel.
    R = np.sqrt(((X - col)**2 + (Y - row)**2))
    mask = R < radius
    return (X[mask] + Y[mask]*width).astype(np.int), R[mask]

# Problems 3-6
class ImageSegmenter:
    """Class for storing and segmenting images."""

    # Problem 3
    def __init__(self, filename):
        """Read the image file. Store its brightness values as a flat array."""
        self._original = imread(filename) / 255
        self._is_grayscale = True
        self._num_rows = np.shape(self._original)[0]
        self._num_cols = np.shape(self._original)[1]
        if len(np.shape(self._original)) != 2:
            self._is_grayscale = False
        if self._is_grayscale:
            self._brightness = np.ravel(self._original)
        else:
            self._brightness = np.ravel(self._original.mean(axis = 2))

    # Problem 3
    def show_original(self):
        """Display the original image."""
        if self._is_grayscale:
            plt.imshow(self._original, cmap='gray')
        else:
            plt.imshow(self._original)
        plt.axis('off')
        plt.show()

    # Problem 4
    def adjacency(self, r=5., sigma_B2=.02, sigma_X2=3.):
        """Compute the Adjacency and Degree matrices for the image graph."""
        # initialize A and D ###################################################
        dim_A = self._num_rows * self._num_cols
        A = sparse.lil_matrix((dim_A, dim_A), dtype='float')
        D = np.array([0] * dim_A, dtype='float')

        # compute weights and degrees ##########################################
        for i in range(0, dim_A):
            neighbors, distances = get_neighbors(i, r, self._num_rows, self._num_cols)
            # calculate weights ################################################
            weights = [math.exp(-abs(self._brightness[i]-self._brightness[neighbors[j]]) / sigma_B2 - distances[j] / sigma_X2) for j in range(len(neighbors))]
            A[i, np.array(neighbors)] = weights
            # calculate D ######################################################
            D[i] = np.sum(weights)

        return A.tocsc(), D

    # Problem 5
    def cut(self, A, D):
        """Compute the boolean mask that segments the image."""
        # construct Laplacian and D^(-1/2) #####################################
        L = sparse.csgraph.laplacian(A)
        processed_D = sparse.diags(D)
        processed_D = processed_D.power(-1/2)  # TODO: seems sketch

        # perform DLD operation, calculate eigenvector, and construct mask #####
        DLD = processed_D @ L @ processed_D
        e_vals, e_vectors = eigsh(DLD, k=2, which='SM')
        mask = e_vectors[:,1].reshape((self._num_rows, self._num_cols)) > 0

        return mask

    # Problem 6
    def segment(self, r=5., sigma_B=.02, sigma_X=3.):
        """Display the original image and its segments."""
        # create the image mask ################################################
        A, D = self.adjacency(r, sigma_B, sigma_X)
        mask = self.cut(A, D)

        # plot a grayscale image ###############################################
        if self._is_grayscale:
            plt.subplot(131)
            plt.imshow(self._original, cmap='gray')
            plt.axis('off')
            plt.subplot(132)
            plt.imshow(self._original*mask, cmap='gray')
            plt.axis('off')
            plt.subplot(133)
            plt.imshow(self._original*(~mask), cmap='gray')
            plt.axis('off')

        # plot a colored image #################################################
        else:
            shape_size = [mask] * np.shape(self._original)[2]
            mask = np.dstack(shape_size)
            plt.subplot(131)
            plt.imshow(self._original)
            plt.axis('off')
            plt.subplot(132)
            plt.imshow(self._original*mask)
            plt.axis('off')
            plt.subplot(133)
            plt.imshow(self._original*(~mask))
            plt.axis('off')
        plt.show()


import numpy as np
from PIL import ImageGrab
import cv2
import time
from threading import Thread

class Recorder:
    def __init__(self, path:str, length=20, fps=24) -> None:
        im = ImageGrab.grab()
        self.width, self.high = im.size  # 获取屏幕的宽和高
        fourcc = cv2.VideoWriter_fourcc(*'avc1')  # 设置视频编码格式
        self.fps = fps # 设置帧率
        self.video = cv2.VideoWriter(path, fourcc, fps, (self.width, self.high))
        self.length = length
        self.is_running=True

    def run(self):
        t_start = time.time()
        while self.is_running:  # 开始录制
            im = ImageGrab.grab()
            im_cv = cv2.cvtColor(np.array(im), cv2.COLOR_BGR2RGB)
            # 图像写入
            self.video.write(im_cv)
            # time.sleep(1/self.fps)
            if time.time()-t_start>=self.length:  # 当某某条件满足中断循环
                break

        self.video.release()  # 释放缓存，持久化视频
        print(f'录制结束，共录制{time.time()-t_start}秒')

    def close(self):
        self.is_running=False

if __name__ == "__main__":
    r = Recorder('./test.mp4')
    t = Thread(target=r.run)
    t.start()
    time.sleep(5)
    r.close()


from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split, StratifiedKFold, cross_val_score
from sklearn.pipeline import make_pipeline, Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score, f1_score,precision_score,recall_score
import numpy as np
import pandas as pd

#code inspired by
    # https://www.kaggle.com/lbronchal/sentiment-analysis-with-svm

def fit(train,test):
    np.random.seed(1)

    kfolds = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
    np.random.seed(1)

    vectorizer = TfidfVectorizer(min_df = 5,
                                max_df = 0.8,
                                sublinear_tf = True,
                                use_idf = True)

    pipeline_svm = make_pipeline(vectorizer,
                                SVC(probability=True, kernel="rbf",
                                class_weight="balanced"))

    grid_svm = GridSearchCV(pipeline_svm,
                        param_grid = {'svc__C': [0.01, 0.1, 1]},
                        cv = kfolds,
                        scoring = "accuracy",
                        verbose=1,
                        n_jobs=-1)

    grid_svm.fit(train["text"], train["labels"])
    grid_svm.score(test["text"], test["labels"])
    return grid_svm

def report_results(model, X, y):
    pred_proba = model.predict_proba(X)[:, 1]
    pred = model.predict(X)

    acc = accuracy_score(y, pred)
    f1 = f1_score(y, pred,average = 'macro')
    prec = precision_score(y, pred,average = 'macro')
    rec = recall_score(y, pred,average = 'macro')
    result = { 'f1': f1, 'acc': acc, 'precision': prec, 'recall': rec}
    print(result)

def analysis():
    print("analysis! \n")
    data = pd.read_csv("Data/clean.csv")
    data['text'] = data["headline"] + " " + data["summary"]
    train, test = train_test_split(data, test_size=0.2, random_state=1)
    model = fit(train,test)
    report_results(model,test["text"],test["labels"])


#!usr/bin/env python
#coding: utf-8
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response,get_object_or_404
from django.core.context_processors import csrf
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.utils import simplejson
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.contrib.auth.decorators import login_required
from django.db.models import Q

from organization.models import Organization

from neptune.settings import DEFAULT_FROM_EMAIL
from_email = DEFAULT_FROM_EMAIL
from django.template import loader
from utils.utils import send_mail

from utils.constants import applyonline_status_dict
from utils import utils

import time
import datetime

from log.models import Log
from applyonline.models import ApplyOnline

from utils.utils import isEquipmentStatus, isDate, isInt

@login_required
def index(request):
    '''判断是否具备管理功能,如果具备则可以查看所有的申请单，如果不具备只能查看当前用户自己提交的申请单'''
    buttonlist = request.session["authority_list_button"]
    if 'applyonlinemanage' in buttonlist:
        applyonlines = ApplyOnline.objects.filter(flag=1).order_by('-id')
    else:
        applyonlines = ApplyOnline.objects.filter(flag=1, apply_user__iexact=request.user.username).order_by('-id')

    retdir = {}
    if 'applyid' in request.POST and request.POST['applyid']:
        applyid = request.POST['applyid']
        retdir['applyid'] = applyid
        applyonlines = applyonlines.filter(applyid__icontains = applyid)
    if 'status' in request.POST and request.POST['status']:
        status = request.POST['status']
        if status == 'all':
            retdir['status'] = status
        else:
            retdir['status'] = int(status)
            applyonlines = applyonlines.filter(status__iexact = int(status))
    if 'dateStart' in request.POST and request.POST['dateStart']:
        dateStart = request.POST['dateStart']
        retdir['dateStart'] = dateStart
        dateStart = time.strptime(dateStart, "%Y-%m-%d")
        dateStart = time.strftime("%Y-%m-%d %H:%M:%S", dateStart)
        dateStart = utils.secs2datestr(utils.datestr2secs(dateStart))
        applyonlines = applyonlines.filter(Q(apply_time__gte = dateStart))
    if 'dateEnd' in request.POST and request.POST['dateEnd']:
        dateEnd = request.POST['dateEnd']
        retdir['dateEnd'] = dateEnd
        dateEnd = time.strptime(dateEnd, "%Y-%m-%d")
        dateEnd = time.strftime("%Y-%m-%d %H:%M:%S", dateEnd)
        dateEnd = utils.secs2datestr(utils.datestr2secs(dateEnd) + 23*60*60 + 59*60 + 59)
        applyonlines = applyonlines.filter(Q(apply_time__lte = dateEnd))

    orderField = request.POST.get('orderField', None)
    orderDirection = request.POST.get('orderDirection', None)
    if orderField != None and orderField != '' and orderDirection != None and orderDirection != '':
        retdir['orderField'] = orderField
        retdir['orderDirection'] = orderDirection
        if orderDirection == 'asc':
            applyonlines = applyonlines.order_by(orderField)
        elif orderDirection == 'desc':
            applyonlines = applyonlines.order_by('-' + orderField)

    if 'numPerPage' in request.POST and request.POST['numPerPage']:
        numPerPage = request.POST['numPerPage']
    else:
        numPerPage = 10
    paginator = Paginator(applyonlines, numPerPage)
    page = request.POST.get('pageNum', 1)
    try:
        if int(page) > paginator.num_pages:
            page = str(paginator.num_pages)
        applyonlines = paginator.page(page)
    except (EmptyPage, InvalidPage):
        applyonlines = paginator.page(paginator.num_pages)
    tmpdir = {'applyonlines':applyonlines, 'currentPage':page, 'numPerPage':numPerPage, 'applyonline_status_dict':applyonline_status_dict}
    retdir.update(tmpdir)
    return render_to_response('applyonline/basepage.html', retdir, context_instance=RequestContext(request))

@login_required
def detail(request, id):
    # 显示申请单的详情
    applyonline = ApplyOnline.objects.get(id = id)
    return render_to_response('applyonline/detail.html', {'applyonline':applyonline, 'applyonline_status_dict':applyonline_status_dict}, context_instance=RequestContext(request))

@login_required
def add(request):
    if request.POST:
#        applyid = request.POST.get('applyid', None)

        service_name = request.POST.get('service_name', None)
        service_domainname = request.POST.get('service_domainname', None)
        version = request.POST.get('version', None)
#        apply_user = request.POST.get('apply_user', None)
        apply_user = request.user.username
        apply_time = request.POST.get('apply_time', None)
        priority = request.POST.get('priority', None)
        file_name = request.POST.get('file_name', None)
        file_url = request.POST.get('file_url', None)
        online_time = request.POST.get('online_time', None)
        develop_user = request.POST.get('develop_user', None)
        test_user = request.POST.get('test_user', None)
        operate_user = request.POST.get('operate_user', None)

        is_system_test = request.POST.get('is_system_test', None)
        is_function_test = request.POST.get('is_function_test', None)
        is_capability_test = request.POST.get('is_capability_test', None)
        is_pressure_test = request.POST.get('is_pressure_test', None)
        is_ui_test = request.POST.get('is_ui_test', None)
        is_special_test = request.POST.get('is_special_test', None)
        is_uat_test = request.POST.get('is_uat_test', None)
        is_stability_test = request.POST.get('is_stability_test', None)
        is_version_control = request.POST.get('is_version_control', None)
        is_train_complete = request.POST.get('is_train_complete', None)
        is_datatransfer_complete = request.POST.get('is_datatransfer_complete', None)
        is_document_complete = request.POST.get('is_document_complete', None)
        is_environment_complete = request.POST.get('is_environment_complete', None)
        is_backup_plan = request.POST.get('is_backup_plan', None)
        is_paramconf_complete = request.POST.get('is_paramconf_complete', None)
        is_can_online = request.POST.get('is_can_online', None)
        is_check_url = request.POST.get('is_check_url', None)

        check_url = request.POST.get('check_url', None)

        deploy_step = request.POST.get('deploy_step', None)
        backup_method = request.POST.get('backup_method', None)
        update_check = request.POST.get('update_check', None)

        '''自动生成申请单号'''
        prefix = 'OP-SR-'
        time_list = apply_time.split('-')
        timestr = ''.join(time_list)
        applyonline_count = ApplyOnline.objects.filter(apply_time=datetime.date(int(time_list[0]), int(time_list[1]), int(time_list[2]))).count()
        applyid = prefix + timestr + '-' + str(applyonline_count+1)

        '''验证重复申请单号'''
        applyids = ApplyOnline.objects.filter(applyid__iexact=applyid)
        if applyids:
            return HttpResponse(simplejson.dumps({"statusCode":302, "navTabId":request.POST.get('navTabId', 'applyonlineindex'), "callbackType":request.POST.get('callbackType',None), "message":u'申请单号已经存在不能添加'}), mimetype='application/json')
        applyonline = ApplyOnline(applyid=applyid, service_name=service_name, service_domainname=service_domainname,
                                  version=version, apply_user=apply_user, apply_time=apply_time, file_name=file_name,
                                  file_url=file_url, online_time=online_time, develop_user=develop_user, test_user=test_user,
                                  operate_user=operate_user, check_url=check_url, deploy_step=deploy_step, backup_method=backup_method,
                                  update_check=update_check, status=0, flag=1)

        applyonline.save()

        applyonline.priority = int(priority)
        applyonline.is_system_test = int(is_system_test)
        applyonline.is_function_test = int(is_function_test)
        applyonline.is_capability_test = int(is_capability_test)
        applyonline.is_pressure_test = int(is_pressure_test)
        applyonline.is_ui_test = int(is_ui_test)
        applyonline.is_special_test = int(is_special_test)
        applyonline.is_uat_test = int(is_uat_test)
        applyonline.is_stability_test = int(is_stability_test)
        applyonline.is_version_control = int(is_version_control)
        applyonline.is_train_complete = int(is_train_complete)
        applyonline.is_datatransfer_complete = int(is_datatransfer_complete)
        applyonline.is_document_complete = int(is_document_complete)
        applyonline.is_environment_complete = int(is_environment_complete)
        applyonline.is_backup_plan = int(is_backup_plan)
        applyonline.is_paramconf_complete = int(is_paramconf_complete)
        applyonline.is_can_online = int(is_can_online)
        applyonline.is_check_url = int(is_check_url)

        applyonline.save()

        '''申请人添加完申请单后向运维相关人员发送邮件提示'''
        subject = u'服务上线申请'
        to_mail_list = []
        if operate_user != None and operate_user != '':
            usernamelist = operate_user.split(',')
            for username in usernamelist:
                username += '@autonavi.com'
                to_mail_list.append(username)
        # 定义邮件内容
        email = u"您好，现在已向您发送了服务器上线申请,以下是基本信息,详细信息请登录CMDB系统查询："
        body = loader.render_to_string('applyonline/mail_operate.html', {'email':email, 'applyonline':applyonline})
        send_mail(subject, body, from_email, to_mail_list, html="text/html")

        Log(username=request.user.username, content="execute add applyonline:" + applyonline.applyid + " success!", level=1).save()
        return HttpResponse(simplejson.dumps({"statusCode":200, "navTabId":request.POST.get('navTabId', 'equipmentindex'), "callbackType":request.POST.get('callbackType','closeCurrent'), "message":u'添加成功'}), mimetype='application/json')
    else:
        # 弹出新建窗口
        return render_to_response('applyonline/add.html', {'applyonline_status_dict':applyonline_status_dict})

@login_required
def edit(request, id):
    applyonline = get_object_or_404(ApplyOnline, pk=int(id))
    if request.POST:
        applyonline_desc = request.POST.get('applyonline_desc', None)
        applyonline.applyonline_desc = applyonline_desc
        applyonline.save()
        return HttpResponse(simplejson.dumps({"statusCode":200, "navTabId":request.POST.get('navTabId','applyonlineindex'), "callbackType":request.POST.get('callbackType','closeCurrent'), "message":u'编辑成功'}), mimetype='application/json')
    return render_to_response('applyonline/edit.html', {'applyonline':applyonline, 'applyonline_status_dict':applyonline_status_dict})

@login_required
def deploy_success(request, id):
    applyonline = get_object_or_404(ApplyOnline, pk=int(id))
    if request.POST:
        applyonline_desc = request.POST.get('applyonline_desc', None)
        if applyonline.applyonline_desc != None:
            applyonline.applyonline_desc += '\n' + applyonline_desc
        else:
            applyonline.applyonline_desc = applyonline_desc
        applyonline.status = 1
        applyonline.save()

        '''运维人员部署成功后向相关人员发送邮件提示，提醒相关人员进行验证'''
        subject = u'服务上线验证'
        to_mail_list = []
        if applyonline.apply_user != None and applyonline.apply_user != '':
            usernamelist = applyonline.apply_user.split(',')
            for username in usernamelist:
                username += '@autonavi.com'
                to_mail_list.append(username)
        # 定义邮件内容
        email = u"您好，服务部署成功，请进行相应的验证："
        body = loader.render_to_string('applyonline/mail_deploy_success.html', {'email':email, 'applyonline':applyonline})
        send_mail(subject, body, from_email, to_mail_list, html="text/html")

        return HttpResponse(simplejson.dumps({"statusCode":200, "navTabId":request.POST.get('navTabId','applyonlineindex'), "callbackType":request.POST.get('callbackType','closeCurrent'), "message":u'操作成功'}), mimetype='application/json')
    return render_to_response('applyonline/deploy_success.html', {'applyonline':applyonline, 'applyonline_status_dict':applyonline_status_dict})

@login_required
def deploy_failure(request, id):
    applyonline = get_object_or_404(ApplyOnline, pk=int(id))
    if request.POST:
        applyonline_desc = request.POST.get('applyonline_desc', None)
        if applyonline.applyonline_desc != None:
            applyonline.applyonline_desc += '\n' + applyonline_desc
        else:
            applyonline.applyonline_desc = applyonline_desc
        applyonline.status = 3
        applyonline.save()

        '''运维人员部署失败后向申请人发送邮件提示'''
        subject = u'服务上线失败'
        to_mail_list = []
        if applyonline.apply_user != None and applyonline.apply_user != '':
            usernamelist = applyonline.apply_user.split(',')
            for username in usernamelist:
                username += '@autonavi.com'
                to_mail_list.append(username)
        # 定义邮件内容
        email = u"您好，服务器上线失败，请检查申请单并重新填写一份申请单："
        body = loader.render_to_string('applyonline/mail_deploy_failure.html', {'email':email, 'applyonline':applyonline})
        send_mail(subject, body, from_email, to_mail_list, html="text/html")

        return HttpResponse(simplejson.dumps({"statusCode":200, "navTabId":request.POST.get('navTabId','applyonlineindex'), "callbackType":request.POST.get('callbackType','closeCurrent'), "message":u'操作成功'}), mimetype='application/json')
    return render_to_response('applyonline/deploy_failure.html', {'applyonline':applyonline, 'applyonline_status_dict':applyonline_status_dict})

@login_required
def validate_success(request, id):
    applyonline = get_object_or_404(ApplyOnline, pk=int(id))
    if request.POST:
        applyonline_desc = request.POST.get('applyonline_desc', None)
        if applyonline.applyonline_desc != None:
            applyonline.applyonline_desc += '\n' + applyonline_desc
        else:
            applyonline.applyonline_desc = applyonline_desc
        applyonline.status = 2
        applyonline.save()

        '''申请人验证成功后向运维人员发送邮件提示'''
        subject = u'服务验证成功'
        to_mail_list = []
        if applyonline.apply_user != None and applyonline.apply_user != '':
            usernamelist = applyonline.apply_user.split(',')
            for username in usernamelist:
                username += '@autonavi.com'
                to_mail_list.append(username)
        # 定义邮件内容
        email = u"您好，服务验证成功，谢谢您的大力支持！"
        body = loader.render_to_string('applyonline/mail_validate_success.html', {'email':email, 'applyonline':applyonline})
        send_mail(subject, body, from_email, to_mail_list, html="text/html")

        return HttpResponse(simplejson.dumps({"statusCode":200, "navTabId":request.POST.get('navTabId','applyonlineindex'), "callbackType":request.POST.get('callbackType','closeCurrent'), "message":u'操作成功'}), mimetype='application/json')
    return render_to_response('applyonline/validate_success.html', {'applyonline':applyonline, 'applyonline_status_dict':applyonline_status_dict})

@login_required
def validate_failure(request, id):
    applyonline = get_object_or_404(ApplyOnline, pk=int(id))
    if request.POST:
        applyonline_desc = request.POST.get('applyonline_desc', None)
        if applyonline.applyonline_desc != None:
            applyonline.applyonline_desc += '\n' + applyonline_desc
        else:
            applyonline.applyonline_desc = applyonline_desc
        applyonline.status = 3
        applyonline.save()

        '''申请人验证失败后向运维人员发送邮件提示'''
        subject = u'服务验证失败'
        to_mail_list = []
        if applyonline.apply_user != None and applyonline.apply_user != '':
            usernamelist = applyonline.apply_user.split(',')
            for username in usernamelist:
                username += '@autonavi.com'
                to_mail_list.append(username)
        # 定义邮件内容
        email = u"您好，服务验证失败，我会重新发一份申请单，请您重新部署："
        body = loader.render_to_string('applyonline/mail_validate_failure.html', {'email':email, 'applyonline':applyonline})
        send_mail(subject, body, from_email, to_mail_list, html="text/html")

        return HttpResponse(simplejson.dumps({"statusCode":200, "navTabId":request.POST.get('navTabId','applyonlineindex'), "callbackType":request.POST.get('callbackType','closeCurrent'), "message":u'操作成功'}), mimetype='application/json')
    return render_to_response('applyonline/validate_failure.html', {'applyonline':applyonline, 'applyonline_status_dict':applyonline_status_dict})

@login_required
def delete(request,id):
    applyonline = ApplyOnline.objects.get(id=id)
    Log(username=request.user.username, content="execute delete applyonline:" + applyonline.applyid + " success!", level=1).save()
    applyonline.flag = 0
    applyonline.save()
    return HttpResponse(simplejson.dumps({"statusCode":200, "navTabId":request.POST.get('navTabId','applyonlineindex'), "callbackType":request.POST.get('callbackType',''), "message":u'删除成功'}), mimetype='application/json')

@login_required
def selecteddelete(request):
    ids = request.POST.get('ids', None)
    if ids:
        applyonlines = ApplyOnline.objects.extra(where=['id IN ('+ ids +')'])
        applyonlines.update(flag=0)
        for applyonline in applyonlines:
            Log(username=request.user.username, content="execute selecteddelete applyonline:" + applyonline.applyid + " success!", level=1).save()
            applyonline.save()
        return HttpResponse(simplejson.dumps({"statusCode":200, "navTabId":request.POST.get('navTabId','applyonlineindex'), "callbackType":request.POST.get('callbackType',''), "message":u'选中项删除成功'}), mimetype='application/json')


# -*- coding: utf-8 -*

######### From Segment Models #########
import segmentation_models as sm
from keras.layers import Input, Conv2D
from tensorflow.keras.optimizers import Adam, RMSprop
from keras.models import Model

if __name__ == "__main__":
    import train_utils
else:
    from . import train_utils

RESNET_BACKBONE = 'resnet34'
INCEPTION_BACKBONE = 'inceptionv3'
INCEPTION_RESNET_BACKBONE = 'inceptionresnetv2'
VGG_BACKBONE = 'vgg19'
EFFICIENTNET_BACKBONE = 'efficientnetb7'

sm.set_framework('tf.keras')
sm.framework()

###################################################################################
def get_sm_preproc_data(x_train_raw, x_test_raw, backbone):
    preprocess_input = sm.get_preprocessing(backbone)

    # preprocess input
    xtrain = preprocess_input(x_train_raw)
    xtest = preprocess_input(x_test_raw)
    return xtrain, xtest

def add_input_layer(backbone, numChannels):
    base_model = sm.Unet(backbone_name=backbone, encoder_weights='imagenet')

    input = Input(shape=(None, None, numChannels))
    l1 = Conv2D(3, (1, 1))(input) # map N channels data to 3 channels
    output = base_model(l1)

    model = Model(inputs=input, outputs=output, name=base_model.name)
    return model

def get_target_backbone(backbone):
    if 'inceptionresnet' in backbone:
        target_backbone = INCEPTION_RESNET_BACKBONE
    elif 'resnet' in backbone:
        target_backbone = RESNET_BACKBONE
    elif 'inception' in backbone:
        target_backbone = INCEPTION_BACKBONE
    elif 'efficientnet' in backbone:
        target_backbone = EFFICIENTNET_BACKBONE
    elif 'vgg' in backbone:
        target_backbone = VGG_BACKBONE
    return target_backbone

def get_sm_model(backbone, height, width, numChannels, numClasses):
    target_backbone = get_target_backbone(backbone)
    if 'pretrained' in backbone:
        model = add_input_layer(target_backbone, numChannels)
    else:
        model = sm.Unet(target_backbone, input_shape=(None, None, numChannels), encoder_weights=None, classes=numClasses)
    return model

def build_sm_model(framework, x_train_raw, x_test_raw, height, width, numChannels, numClasses,
        optimizerName = "Adam", learning_rate =  0.0001, decay = 0, lossFunction = "BCE+JC"):

    target_backbone = get_target_backbone(framework)
    x_train_preproc, x_test_preproc = get_sm_preproc_data(x_train_raw, x_test_raw, target_backbone)
    model = get_sm_model(framework, height, width, numChannels, numClasses)
    model = train_utils.compile_custom(framework, model, optimizerName, learning_rate, decay, lossFunction)

    return model, x_train_preproc, x_test_preproc

def fit_sm_model(framework, x_train_raw, ytrain, x_test_raw, ytest, height, width, numChannels, numClasses, numEpochs,
    optimizerName = "Adam", learning_rate =  0.0001, decay = 0, lossFunction = "BCE+JC"):

    model, x_train_prep, x_test_prep = build_sm_model(framework, x_train_raw, x_test_raw, height, width, numChannels,
        numClasses,  optimizerName, learning_rate, decay, lossFunction)

    model, history = train_utils.fit_model(framework, model, x_train_prep, ytrain, x_test_prep, ytest, numEpochs, batchSize = 64)

    return model, history


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

"""
Created on a windy day, unlike australia

@author: Helen Daly
@id: R00142752
@Cohort: SOFT8032_26756
"""

from sklearn import datasets
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import random as rnd

from sklearn import datasets
from sklearn import tree
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.model_selection import KFold
from sklearn.model_selection import RepeatedKFold
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

from sklearn.svm import SVC

from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier

from sklearn.preprocessing import KBinsDiscretizer
from sklearn.model_selection import cross_validate
from sklearn.cluster import KMeans
from sklearn import preprocessing
from sklearn.preprocessing import KBinsDiscretizer

df = pd.read_csv("weatherAUS.csv", encoding = 'utf8')

def Task1():

    # Use 3 attributes to use machine learning to predict if it will rain tomorrow.
    # Experiment with 1 - 35 depth of decision tree.

    #print(df.info())
    newdf = df[['MinTemp', 'WindGustSpeed', 'Rainfall', 'RainTomorrow']].copy()
    #print(newdf.info())
    newdf = newdf.dropna()

    def testTrainPlot(X, y, df):

        attributes = list(df.columns)
        X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.33, random_state=42)
        trainResult = []
        testResult = []

        for maxdepthNo in range(1,36):
            tree_clf = tree.DecisionTreeClassifier(max_depth=maxdepthNo)
            tree_clf.fit(X_train, y_train)
            trainResult.append(tree_clf.score(X_train, y_train))
            testResult.append(tree_clf.score(X_test, y_test))

        title = ''
        for colTitle in attributes:
            title = title + colTitle + ' + '
        title = title + 'used to predict ' + y.columns
        plt.title(title)
        plt.plot(trainResult)
        plt.plot(testResult)
        plt.legend(['Train', 'Test'])
        plt.show()

    newdf1 = newdf[['MinTemp', 'WindGustSpeed', 'Rainfall']]
    newdf2 = newdf[['MinTemp', 'WindGustSpeed']]
    newdf3 = newdf[['MinTemp', 'Rainfall']]
    newdf4 = newdf[['WindGustSpeed', 'Rainfall']]

    X = (newdf1)
    y = newdf[['RainTomorrow']]
    testTrainPlot(X, y, newdf1)

    X = (newdf2)
    testTrainPlot(X, y, newdf2)

    X = (newdf3)
    testTrainPlot(X, y, newdf3)

    X = (newdf4)
    testTrainPlot(X, y, newdf4)

def Task2():
    newdf = df[['Pressure9am', 'Pressure3pm', 'Humidity9am', 'Humidity3pm', 'RainToday']]
    print(newdf.head)
    # To get an idea of how many null values in columns
    print(newdf.info())
    # To fill in blanks for pressure and humidity, to ensure average is correct
    #newdf.loc[newdf['Pressure9am'].isnull(), 'Pressure9am'] = newdf['Pressure3pm']
    #newdf.loc[newdf['Pressure3pm'].isnull(), 'Pressure3pm'] = newdf['Pressure9am']
    #newdf.loc[newdf['Humidity9am'].isnull(), 'Humidity9am'] = newdf['Humidity3pm']
    #newdf.loc[newdf['Humidity3pm'].isnull(), 'Humidity3pm'] = newdf['Humidity9am']

    # Fill up the remaining null values with average
    #newdf.loc[newdf['Pressure9am'].isnull(), 'Pressure9am'] = newdf['Pressure9am'].mean()
    #newdf.loc[newdf['Pressure3pm'].isnull(), 'Pressure3pm'] = newdf['Pressure3pm'].mean()
    #print(newdf.info())

    #newdf.dropna(subset=['RainToday'], inplace = True)
    #newdf.loc[newdf['Humidity9am'].isnull(), 'Humidity9am'] = newdf['Humidity9am'].mean()
    #newdf.loc[newdf['Humidity3pm'].isnull(), 'Humidity3pm'] = newdf['Humidity3pm'].mean()
    #print(newdf.info())

    newdf = newdf.dropna()
    newdf['Pressure'] = (newdf['Pressure9am'] + newdf['Pressure3pm'])/2
    newdf['Humidity'] = (newdf['Humidity9am'] + newdf['Humidity3pm'])/2
    print(newdf.info())

    models = []
    models.append(('KNN', KNeighborsClassifier()))
    models.append(('DTC', tree.DecisionTreeClassifier()))
    models.append(('NB', GaussianNB()))
    models.append(('SVM', SVC(kernel='linear')))
    models.append(('RFS',RandomForestClassifier()))

    #names = []
    results = {}

    X = newdf[['Pressure', 'Humidity']]
    y = newdf[['RainToday']]

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

    for name,model in models:
        cv_results = cross_validate(model, X, y, cv=5, scoring='accuracy', return_train_score=True)
        results[name] = cv_results
        print(cv_results)
    # for models in results:
    #     print(models)
    #     print('Training  ',results[models]['train_score'].mean())
    #     print('Test  ',results[models]['test_score'].mean())
    # return results

    df = pd.DataFrame()

    for models in results:

        print(models)
        model = []
        model.append(models)
        data = []
        print('Training  ',results[models]['train_score'].mean())
        data.append(results[models]['train_score'].mean())
        print('Test  ',results[models]['test_score'].mean())
        data.append(results[models]['test_score'].mean())
        ser = pd.Series(data=results[models])
        print(ser)
        df[models] = data

    df = df.transpose()

    df.columns = ['Test Score', 'Train Score']

    df.plot.bar(rot=0)

def Task3():

    return

def Task4():
    return

results = Task2()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^update_session/(?P<id_producte>[0-9]+)/$', views.update_session, name='update_session'),
    url(r'^update_sessionPost/$', views.update_sessionPost, name='update_sessionPost'),

    url(r'^veure_comande/$', views.veure_comande, name='veure_comande'),
    url(r'^veure_comande/esborra_carro/$', views.esborra_carro, name='esborra_carro'),
    url(r'^veure_comande/esborra_linia/(?P<id_producte>[0-9]+)/$', views.esborra_linia, name='esborra_linia'),
    url(r'^veure_comande/comfimar_carro/$', views.comfimar_carro, name='comfimar_carro'),

    url(r'^llista_comandes/$', views.llista_comandes, name='llista_comandes'),
    url(r'^llista_comandes/detall_comanda/(?P<id_comanda>[0-9]+)/$', views.detall_comanda, name='detall_comanda'),

]


#!/usr/bin/env python3

import asyncio
import websockets

import json
from types import SimpleNamespace

from pyfirmata import Arduino, util
from time import sleep

board = Arduino("/dev/ttyACM0", baudrate=57600)

#print(board.get_firmata_version())
# #checks generell woking connection

flagIncomingFromVirtual = False
flagHoererOben = False
data = '{"event": "RingsMaybe"}'

# Start iterator to receive input data
it = util.Iterator(board)
it.start()
board.analog[0].enable_reporting()

async def server(websocket, path):
    # Get received data from websocket
    JSONIncomingFromVirtual = await websocket.recv()
    DictIncomingFromVirtual = eval(JSONIncomingFromVirtual)
    flagIncomingFromVirtual = DictIncomingFromVirtual["event"]
    print("flagIncomingFromVirtual type: " + type(flagIncomingFromVirtual))
    print(flagIncomingFromVirtual)

    #ring the bell
    if flagIncomingFromVirtual == "RingingStart":

        for x in range(10):
            board.digital[2].write(1)
            sleep(0.03)
            board.digital[2].write(0)
            board.digital[3].write(1)
            sleep(0.03)
            board.digital[3].write(0)

        print("ring")
        sleep(2)
    else:
        pass

    await asyncio.sleep(0.05)

    """ readResult = board.analog[0].read()
    print(readResult)
    if readResult <= 0.2:
        flagHoererOben = 1
    else:
        flagHoererOben = 0
    await asyncio.sleep(0.2)

    #if different stat: build JSON
    flagHoererObenJSON =

    # Send response back to client to acknowledge receiving message
    await websocket.send(flagHoererObenJSON) """

# Create websocket server
start_server = websockets.serve(server, "localhost", 3003)

# Start and run websocket server forever
# Server has to be running before browser ist loaded
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()


# coding=utf-8
from application import bot
from boto3.session import Session
from model.song import Song
import requests
import os
import uuid
import spotipy

humming_mode = False

TOKEN = os.environ.get('BOT_TOKEN')
AUDD_API_TOKEN = os.environ.get('AUDD_API_TOKEN')

SPOTIFY_API_TOKEN = os.environ.get('SPOTIFY_API_TOKEN')
spotify = spotipy.Spotify(auth=SPOTIFY_API_TOKEN)

session = Session(aws_access_key_id=os.environ.get('AWS_S3_ACCESS_KEY'),
                  aws_secret_access_key=os.environ.get('AWS_S3_SECRET_KEY'))
s3 = session.resource('s3')
s3Client = session.client('s3')

@bot.message_handler(commands=['start'])
def start(message):
    bot.reply_to(message, 'Hello, ' + message.from_user.first_name)

@bot.message_handler(commands=['humming'])
def set_humming(message):
    global humming_mode
    humming_mode = True
    bot.reply_to(message, 'Se reconocerá como tarareo')

@bot.message_handler(commands=['recording'])
def set_recording(message):
    global humming_mode
    humming_mode = False
    bot.reply_to(message, 'Se reconocerá como grabación')

def get_song_info(title, artist, album, score = None):
    message = ""
    if title: message = message + "*Titulo:* " + title + "\n"
    if artist: message = message + "*Artista:* " + artist.translate({ord(c): None for c in '!@#$()_'}) + "\n"
    if album: message = message + "*Album:* " + album + "\n"
    if score: message = message + "*Probabilidad:* " + str(score) + "\n"

    return message

@bot.message_handler(func=lambda message: True, content_types=['voice'])
def recognize_song(message):

    bot.reply_to(message, "⚙ Procesando tu audio...", parse_mode="Markdown")

    fileId = message.voice.file_id
    getPathRequest = requests.get('https://api.telegram.org/bot' + TOKEN + '/getFile?file_id=' + fileId)
    if getPathRequest.status_code == 200:
        path = getPathRequest.json()['result']['file_path']
        rawFileRequest = requests.get('https://api.telegram.org/file/bot'+ TOKEN + '/' + path)
        if rawFileRequest.status_code == 200:
            fileKey = str(uuid.uuid4())

            upload = s3.Bucket('musictelegram').put_object(Key=fileKey, Body=rawFileRequest.content, ACL='public-read')

            bucket_location = s3Client.get_bucket_location(Bucket='musictelegram')
            object_url = "http://s3-{0}.amazonaws.com/{1}/{2}".format(
            bucket_location['LocationConstraint'],
            'musictelegram',
            fileKey)

            method = "recognizeWithOffset/" if humming_mode else ""

            getSongInfo = requests.get('https://api.audd.io/' + method +'?api_token=' + AUDD_API_TOKEN + '&url=' + object_url)
            if getSongInfo.status_code == 200:
                songInfo = getSongInfo.json()['result']
                if songInfo:
                    if humming_mode:
                        if songInfo['count']:
                            predictionList = songInfo['list']
                            bot.send_message(message.chat.id, "Tu canción puede ser una de estas:")
                            for prediction in predictionList:
                                title = None
                                artist = None
                                album = None
                                score = None
                                if prediction.has_key('title'): title = prediction['title']
                                if prediction.has_key('artist'): artist = prediction['artist']
                                if prediction.has_key('album'): album = prediction['album']
                                if prediction.has_key('score'): score = prediction['score']
                                bot.send_message(message.chat.id, get_song_info(title, artist, album, score).encode('utf-8'), parse_mode="Markdown")
                        else:
                            bot.reply_to(message, "Lo has hecho fatal, intenta tararear de nuevo.")
                    else:
                        songTitle = songInfo['title']
                        songArtist = songInfo['artist']
                        songAlbum = songInfo['album']

                        messageToSend = "Tu cancion es:\n" + get_song_info(songTitle, songArtist, songAlbum)

                        bot.send_message(message.chat.id, messageToSend, parse_mode="Markdown")

                        spotifySearch = spotify.search(q='track:' + songTitle.encode('utf-8'), type='track')
                        if(len(spotifySearch)):
                            spotifyURL = spotifySearch['tracks']['items'][0]['external_urls']['spotify']

                        bot.send_message(message.chat.id, "*URL Spotify:* " + spotifyURL, parse_mode="Markdown")
                        Song.set_config(message.chat.id, 'memory', songTitle, songAlbum, songArtist)
                else:
                    bot.reply_to(message, "No se ha podido reconocer ninguna canción. Inténtalo de nuevo.")
            else:
                bot.reply_to(message, "Error de reconocimiento de audio.")
        else:
            bot.reply_to(message, "Error procesando tu audio. Intentalo de nuevo.")
    else:
            bot.reply_to(message, "Error procesando tu audio. Intentalo de nuevo.")


# -*- coding: utf-8 -*-
from typing import TYPE_CHECKING, Dict, Optional

from django.db import models
from jsonfield import JSONField

from paas_wl.platform.applications.models import UuidAuditedModel
from paas_wl.utils.models import validate_procfile

if TYPE_CHECKING:
    from paas_wl.platform.applications.models.app import EngineApp
    from paas_wl.platform.applications.models.build import Build
    from paas_wl.platform.applications.models.config import Config

class ReleaseManager(models.Manager):
    def new(
        self,
        owner: str,
        build: 'Build',
        procfile: Dict[str, str],
        summary: str = None,
        config: Optional['Config'] = None,
    ):
        """Create a new release

        :params owner str: 发布者, 目前该字段无意义
        :params build Build: 应用构建记录
        :params summary str:
        :params config Optional[Config]: 应用配置, 包含环境变量和资源限制等信息. 如果不提供, 则获取上一个发布版本的应用配置
        """
        from paas_wl.platform.applications.models.app import App

        # Get the largest(latest) version and increase it by 1.
        if not hasattr(self, "instance"):
            raise RuntimeError("Only call `new` method from RelatedManager.")

        if not isinstance(self.instance, App):
            raise RuntimeError("Only call from app.release_set.")

        if build is None:
            raise RuntimeError('No build associated with this release.')

        app = self.instance
        latest_release = self.order_by('-version').first()
        if latest_release:
            new_version = latest_release.version + 1
            cfg = config or latest_release.config
        else:
            new_version = 1
            cfg = config or app.config_set.latest()

        release = Release.objects.create(
            app=app,
            version=new_version,
            summary=summary,
            config=cfg,
            build=build,
            owner=owner,
            procfile=procfile or build.procfile,
        )
        return release

    def any_successful(self, app: 'EngineApp') -> bool:
        """Check if engine app has any successful releases"""
        # In legacy versions, "workloads" will create an initial release object for
        # apps after creation, the object's "build" field was set to `None` and
        # "version" is 1, it must be excluded or current method will always return
        # true.
        qs = self.get_queryset().exclude(version=1, build=None)
        return qs.filter(app=app, failed=False).exists()

    def get_latest(self, app: 'EngineApp', ignore_failed: bool = False) -> 'Release':
        """获取最后一次发布对象(不管是否成功或失败), 如果不存在, 则根据 allow_null 返回 None 或抛异常.

        :param EngineApp app: engine app 对象
        :param bool ignore_failed: 寻找最后一次发布对象时, 是否忽略发布失败
        """
        qs = self.filter(app=app).all()
        if ignore_failed:
            qs = qs.filter(failed=False)

        return qs.latest('version')

    def get_by_version(self, app: 'EngineApp', version: int) -> 'Release':
        """根据指定的 App，Version 获取对应的 Release"""
        return self.get(app=app, version=version)

class Release(UuidAuditedModel):
    """
    Software release deployed by the application platform

    Releases contain a class`Build` and a class`Config`.
    """

    owner = models.CharField(max_length=64)
    app = models.ForeignKey('App', on_delete=models.CASCADE)
    version = models.PositiveIntegerField()
    summary = models.TextField(blank=True, null=True)
    failed = models.BooleanField(default=False)
    procfile = JSONField(default={}, blank=True, validators=[validate_procfile])

    config = models.ForeignKey('Config', on_delete=models.CASCADE)
    build = models.ForeignKey('Build', null=True, on_delete=models.CASCADE)

    objects = ReleaseManager()

    class Meta:
        get_latest_by = 'created'
        ordering = ['-created']
        unique_together = (('app', 'version'),)

    def __str__(self):
        return 'Release: %s' % self.uuid

    @property
    def region(self):
        return self.app.region

    def fail(self, summary: str):
        self.failed = True
        self.summary = summary
        self.save(update_fields=['failed', 'summary', 'updated'])

    def get_procfile(self) -> Dict:
        """获取与这个发布对象关联的 procfile"""
        if self.procfile:
            return self.procfile
        return self.build.procfile

    def get_envs(self) -> Dict:
        """获取与这个发布对象关联的环境变量"""
        envs = {}
        if self.build:
            envs.update(self.build.get_env_variables())
        envs.update(self.config.envs)
        return envs

    def get_previous(self) -> 'Release':
        """获取上一次的 release

        :raise ObjectDoesNotExist: 如果不存在上一次的 release, 则抛该异常
        """
        return Release.objects.filter(app=self.app, version__lt=self.version).latest('version')