import dolfin
from .block_mat import block_mat
from .block_vec import block_vec
from .block_util import wrap_in_list, create_diagonal_matrix
from .helpers import supports_mpi
from .splitting import split_bcs
import itertools
import numpy

class block_bc:
    """
    This class applies Dirichlet BCs to a block matrix.  It is not a block
    operator itself.

    Creating bcs:

    >>> bcs = block_bc([...], symmetric=...)

    Basic inplace usage, with static BCs:

    >>> rhs_bcs = bcs.apply(A)
    >>> rhs_bcs.apply(b)

    with the shortcut

    >>> bcs.apply(A,b)

    If boundary conditions are applied multiple times, it may be useful
    to keep the original assembled mat/vec unchanged (for example, to
    avoid having to re-assemble the RHS if the BC is time dependent but
    the form itself isn't):

    >>> Ainv = conjgrad(bcs(A))
    >>> while True:
    >>>     source.t = t
    >>>     x = Ainv * bcs(b)
    """
    def __init__(self, bcs, symmetric=False, signs=None, subspace_bcs=None):
        # Clean up self, and check arguments
        self.symmetric = symmetric
        bcs = [wrap_in_list(bc, dolfin.DirichletBC) for bc in bcs]
        if subspace_bcs is not None:
            subspace_bcs = split_bcs(subspace_bcs, None)
            combined_bcs = []
            for ss,ns in itertools.zip_longest(subspace_bcs, bcs):
                combined_bcs.append([])
                if ns is not None: combined_bcs[-1] += ns
                if ss is not None: combined_bcs[-1] += ss
            self.bcs = combined_bcs
        else:
            self.bcs = bcs
        self.signs = signs or [1]*len(self.bcs)
        if not all(s in [-1,1] for s in self.signs):
            raise ValueError('signs should be a list of length n containing only 1 or -1')
        supports_mpi(not symmetric, 'Symmetric BCs not supported in parallel')

    @classmethod
    def from_mixed(cls, bcs, *args, **kwargs):
        return cls([], *args, **kwargs, subspace_bcs=bcs)

    def __call__(self, other, A=None):
        if isinstance(other, block_mat):
            self._A = other # opportunistic, expecting a block_vec later
            A = other.copy()
            self.apply(A)
            return A
        elif isinstance(other, block_vec):
            if A is None:
                A = getattr(self, '_A', None) # opportunistic
            if self.symmetric and A is None:
                raise ValueError('A not available. Call with A first, or pass A=A.')
            rhs = self.rhs(A)
            return rhs(other)
        else:
            raise TypeError('BCs can only act on block_mat (A) or block_vec (b)')

    def apply(self, A, b=None):
        """
        Apply BCs to a block_mat LHS, and return an object which modifies the
        corresponding block_vec RHS.  Typical use:

        b_bcs = A_bcs.apply(A); b_bcs.apply(b)
        """
        if not isinstance(A, block_mat):
            raise RuntimeError('A is not a block matrix')

        A_orig = A.copy() if self.symmetric else A
        self._apply(A)

        # Create rhs_bc with a copy of A before any symmetric modifications
        rhs = self.rhs(A_orig)
        if b is not None:
            rhs.apply(b)
        return rhs

    def rhs(self, A):
        return block_rhs_bc(self.bcs, A, symmetric=self.symmetric, signs=self.signs)

    def _apply(self, A):
        if self.symmetric:
            # dummy vec, required by dolfin api -- we don't use this
            # corrections directly since it may be time dependent
            b = A.create_vec(dim=0)
        for i,bcs in enumerate(self.bcs):
            if bcs:
                for j in range(len(A)):
                    if i==j:
                        if numpy.isscalar(A[i,i]):
                            # Convert to a diagonal matrix, so that the individual rows can be modified
                            A[i,i] = create_diagonal_matrix(dolfin.FunctionSpace(bc.function_space()), A[i,i])
                        if self.symmetric:
                            for bc in bcs:
                                bc.zero_columns(A[i,i], b[i], self.signs[i])
                        else:
                            if self.signs[i] != 1:
                                A[i,i] *= 1/self.signs[i]
                            for bc in bcs:
                                bc.apply(A[i,i])
                            if self.signs[i] != 1:
                                A[i,i] *= self.signs[i]
                    else:
                        if numpy.isscalar(A[i,j]):
                            if A[i,j] != 0.0:
                                dolfin.error("can't modify nonzero scalar off-diagonal block (%d,%d)" % (i,j))
                        else:
                            for bc in bcs:
                                bc.zero(A[i,j])
                        if self.symmetric:
                            if numpy.isscalar(A[j,i]):
                                if A[j,i] != 0.0:
                                    dolfin.error("can't modify nonzero scalar off-diagonal block (%d,%d)" % (i,j))
                            else:
                                for bc in bcs:
                                    bc.zero_columns(A[j,i], b[j])

class block_rhs_bc:
    """
    This class applies Dirichlet BCs to a block block vector.  It can be used
    as a block operator; but since it is nonlinear, it can only operate
    directly on a block vector, and not combine with other operators.
    """
    def __init__(self, bcs, A, symmetric, signs):
        if symmetric:
            assert A is not None
        self.bcs = bcs
        self.A = A
        self.symmetric = symmetric
        self.signs = signs

    def apply(self, b):
        """Apply Dirichlet boundary conditions statically.  In the
        inhomogeneous symmetric case b is changed globally; if BCs are mutable
        (time dependent for example), it is then more convenient to use the
        callable form -- rhs_bc(b) -- to preserve the original contents of b
        for repeated application without reassembly.
        """
        if not isinstance(b, block_vec):
            raise TypeError('not a block vector')

        try:
            b.allocate(self.A, dim=0, alternative_templates=[self.bcs])
        except Exception:
            raise ValueError('Failed to allocate block vector, call b.allocate(something) first')

        # Apply lifting to correct for the matrix columns zeroed by symmetricization
        if self.symmetric:
            # First, collect a vector containing all non-zero BCs.
            g = self.A.create_vec(dim=0)
            g.zero()
            for i,bcs in enumerate(self.bcs):
                for bc in bcs:
                    bc.apply(g[i])
            # The non-zeroes of g are now exactly the inhomogeneous BC
            # values. We can thus create the necessary modifications to b by
            # just multiplying with the un-symmetricised original matrix. The
            # bc values are overwritten below, hence only the non-BC rows of A
            # matter.
            b -= self.A * g

        # Apply the actual BC dofs to b. (This must be done after the symmetric
        # correction above, since A*g is not necessarily zero at BC dofs.)
        # If the sign is negative, we negate twice to effectively apply -bc.
        for i,bcs in enumerate(self.bcs):
            if self.signs[i] != 1 and bcs:
                b[i] *= 1/self.signs[i]
            for bc in bcs:
                bc.apply(b[i])
            if self.signs[i] != 1 and bcs:
                b[i] *= self.signs[i]

        return self

    def __call__(self, other):
        if not isinstance(other, block_vec):
            raise TypeError()
        b = other.copy()
        self.apply(b)
        return b


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

# In[1]:

import numpy as np
import pylab as py

get_ipython().run_line_magic('pylab', 'inline')

#  $$I \dfrac{d^2 \theta}{dt^2} = \Gamma_\textrm{gravity}+\Gamma_\textrm{spring} $$
#
#
# $$  \Gamma_\textrm{res_m} = -m g l sin(\theta) $$
#
# $$  F_\textrm{spring} = -k \vec{ex}$$
#
# $$ ex = (l_\textrm{2} sin(\theta_\textrm{2}) - l_\textrm{1} sin(\theta_\textrm{1})+d)  \hat{x} + ((l_\textrm{1} cos(\theta_\textrm{1})-l_\textrm{2} cos(\theta_\textrm{2})) \hat{y} $$
#
# $$ ex_\textrm{o} = d \hat{x}+(l_\textrm{1}-l_\textrm{2})\hat{y} $$
#
# $$ \vec{E}_x = |\vec{ex}-\vec{ex_\textrm{o}}| \hat{ex} $$
#
# $$ \Gamma_\textrm{_m1} = - mgl_\textrm{1}sin(\theta_\textrm{1})+\vec{r_\textrm{1}} \times\ k \vec{Ex}$$
#
# $$ \Gamma_\textrm{_m2} = - mgl_\textrm{2}sin(\theta_\textrm{2})-\vec{r_\textrm{2}} \times\ k \vec{Ex}$$
#

# In[39]:

#                       ------------------ COUPLED PENDULUM -----------------

class coupled_pend:
    def __init__(self,g=9.8,l1=1,l2=1,m1=1,m2=1,k=5,I=3,d=2,f1=0,f2=0):
        '''
        Inputs:
        Only SI units are used.

        Symbol  Default value   Description
        g       9.8             Acceleration due to gravity
        l1      1               lenght of the pendullum 1
        l2      1               lenght of the pendullum 2
        m1      1               mass of the bob 1
        m2      1               mass of the bob 2
        k       5               spring constant of the spring
        d       2               distance between the two pendullum
        f1      0
        '''

        (self.theta1,self.theta2) = (0,0)
        (self.w1,self.w2) = (0,0)
        (self.l1,self.l2) = (l2,l1)
        (self.m1,self.m2) = (m2,m1)

        self.d = d
        self.g = g
        self.k = k
        self.I = I

    def extension(self,l1,l2,theta1,theta2):
        exo = np.array([self.d,(l1-l2)])
        ex = np.array([l2*np.sin(theta2)-l1*np.sin(theta1)+self.d,l1*np.cos(theta1)-l2*np.cos(theta2)])
        exo_mod = np.linalg.norm(exo,axis=0)
        ex_mod = np.linalg.norm(ex,axis=0)
        Ex = ex_mod-exo_mod
        ex_cap = ex/ex_mod
        def heavy_theta():
            if(Ex<=0):
                return 0
            else:
                return 1
        exten = heavy_theta()*Ex*ex_cap
        return exten

    def Torque(self,r,ex,m,l,theta):
        T_gravity = -m*self.g*l*np.sin(theta)
        T_spring = r[0]*self.k*ex[1]-r[1]*self.k*ex[0]
        return T_spring+T_gravity

    def w_fun1(self):
        r1 = np.array([self.l1*np.sin(self.theta1),-self.l1*np.cos(self.theta1)])
        ex = self.extension(self.l1,self.l2,self.theta1,self.theta2)
        torque = self.Torque(r1,ex,self.m1,self.l1,self.theta1)
        return (torque)/self.I

    def w_fun2(self):
        r2 = np.array([self.l2*np.sin(self.theta2),-self.l2*np.cos(self.theta2)])
        ex = self.extension(self.l1,self.l2,self.theta1,self.theta2)
        torque = self.Torque(r2,-ex,self.m2,self.l2,self.theta2)
        return (torque)/self.I

    def t_fun1(self):
        return self.w1

    def t_fun2(self):
        return self.w2

pendula = coupled_pend()

# In[40]:

pendula.extension(1,1,np.pi/6,0)

# In[41]:

help(coupled_pend)

# In[42]:

pendula = coupled_pend()

def k_loop1(t,theta1,theta2,w1,w2):
    for fun in [pendula.w_fun1, pendula.w_fun2, pendula.t_fun1, pendula.t_fun2]:
        # returns k1w and k1t for ti,wi,thetai
        pendula.theta1 = theta1
        pendula.theta2 = theta2
        pendula.w1 = w1
        pendula.w2 = w2
        yield fun()

def RK4_loop1(t,h,theta1,theta2,w1,w2):
    l=1
    while(l<=len(t)):
        k1 = [val for val in k_loop1(t,      theta1,            theta2,            w1,            w2            )]
        k2 = [val for val in k_loop1(t+0.5*h,theta1+0.5*h*k1[2],theta2+0.5*h*k1[3],w1+0.5*h*k1[0],w2+0.5*h*k1[1])]
        k3 = [val for val in k_loop1(t+0.5*h,theta1+0.5*h*k2[2],theta2+0.5*h*k2[3],w1+0.5*h*k2[0],w2+0.5*h*k2[1])]
        k4 = [val for val in k_loop1(t+h,    theta1+h*k3[2],    theta2+h*k3[3],    w1+h*k3[0],    w2+h*k3[1]    )]
        theta1 = theta1+(k1[2]+2*k2[2]+2*k3[2]+k4[2])*h/6
        theta2 = theta2+(k1[3]+2*k2[3]+2*k3[3]+k4[3])*h/6
        w1     = w1+(k1[0]+2*k2[0]+2*k3[0]+k4[0])*h/6
        w2     = w2+(k1[1]+2*k2[1]+2*k3[1]+k4[1])*h/6
        l=l+1
        yield theta1,theta2,w1,w2

# In[43]:

t_start = 0
t_end = 25
h = 0.01

t = np.arange(t_start,t_end,h)

# initial values of the system

theta1 = 0*np.pi/180.0
theta2 = -5*np.pi/180.0
w1 = 0
w2 = 0
pendula.l1 = 0.5

# In[44]:

D = RK4_loop1(t,h,theta1,theta2,w1,w2)
Ret = [th for th in D]

Ret = np.array(Ret).T

T1 = Ret[0]
T2 = Ret[1]
W1 = Ret[2]
W2 = Ret[3]

# In[45]:

m1 = pendula.m1
m2 = pendula.m2
l1  = pendula.l1
l2  = pendula.l2
g  = pendula.g
k  = pendula.k
I = pendula.I

pen_pot = m1*g*(l1-l1*np.cos(T1))+m2*g*(l2-l2*np.cos(T2))
Ex = [pendula.extension(l1,l2,T1[i],T2[i]) for i in range(len(T1))]
ex_mod = np.linalg.norm(np.array(Ex).T,axis=0)

spr_pot = 0.5*k*np.array(ex_mod)**2

Pot = pen_pot+spr_pot
Kin = 0.5*I*W1**2+0.5*I*W2**2

# In[46]:

py.subplots(figsize=(15,8))
py.plot(t,np.array(T1)*180/np.pi,t,np.array(T2)*180/np.pi)
py.legend(['T1','T2'])
py.show()

# In[47]:

py.subplots(figsize=(15,8))
py.plot(t,W1,t,W2)
py.legend(['W1','W2'])
py.show()

# In[48]:

py.subplots(figsize=(15,8))
py.plot(t,Pot,t,Kin,t,Pot+Kin)
py.legend(['pot','kin','total'])
py.show()

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:


from flask_babel import Babel
from flask_security import Security
from flask_sqlalchemy import SQLAlchemy

from qualitas.ext.flask_github import GitHub, GitHubV4
from qualitas.ext.flask_zenhub import ZenHub

# Instantiate Extensions
from sqlalchemy import MetaData

babel = Babel()
db = SQLAlchemy(
    metadata=MetaData(naming_convention={
        'pk': 'pk_%(table_name)s',
        'fk': 'fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s',
        'uq': 'uq_%(table_name)s_%(column_0_name)s',
        'ix': 'ix_%(table_name)s_%(column_0_name)s',
        'ck': 'ck_%(table_name)s_%(constraint_name)s',
    }),
)
github = GitHub()
githubv4 = GitHubV4()
security = Security()
zenhub = ZenHub()


import numpy as np

def window_average(x, fltr):
    window_shape = len(fltr)
    x = np.lib.stride_tricks.sliding_window_view(np.pad(x,(window_shape-1,0),constant_values=0),window_shape)
    return np.matmul(x,fltr)

def smooth_counts(X, filter_size=4, filter_type='even', var=1):
    if filter_type=='even':
        fltr = np.ones(filter_size)/filter_size
    elif filter_type=='gaussian':
        fltr = np.ones(filter_size)
        for i in range(filter_size):
            fltr[i] = np.exp(-i**2/var**2)
        fltr /= np.sum(fltr)
        fltr = fltr[::-1]

    print("Smoothing filter = ", fltr)
    X = np.apply_along_axis(lambda m: window_average(m, fltr), axis=1, arr=X)
    return X

def make_windows(X, window_length):
    num_samples = X.shape[1] - 2*window_length
    covariates = []
    labels = []

    for i in range(num_samples):
        covariates.append(X[:, i:i + window_length])
        labels.append(X[:, i + window_length:i + window_length + window_length])

    covariates = np.array(covariates, dtype=np.float32) # float64 takes too much space to store, hence using float32
    labels = np.array(labels, dtype=np.float32)
    return covariates, labels

def add_time_information(X, periods, keep_original_time=False):

    if len(periods)==0:
        return X

    X_time_feats = []
    for period in periods:
        X_time_feats.append(np.sin(2*np.pi*X[:,:,-1:]/period))
        X_time_feats.append(np.cos(2*np.pi*X[:,:,-1:]/period))

    if not keep_original_time:
        X = X[:,:,:2]

    X = np.dstack([X]+X_time_feats)
    return X

def seq_from_windows(X, horizon):
    return np.transpose(X[:,:,horizon],(1,0,-1))


import cgi
import hashlib
import http.server
import hmac
import logging
import json
import os
import threading

class WebhookHandler(http.server.BaseHTTPRequestHandler):
    def send(self, status, message, content=None):
        if content is None:
            content = message
        self.send_response(status, message)
        self.send_header('Content-type', 'text/plain; charset=utf-8')
        self.end_headers()

        self.wfile.write(content.encode('utf-8'))

    def do_POST(self):
        try:
            type = cgi.parse_header(self.headers['Content-Type'])[0]
            if type != 'application/json':
                self.send(415, "Payload must be application/json")
                return

            data = self.rfile.read(int(self.headers['Content-Length']))

            if not self.authenticate(data):
                self.send_header('WWW-Authenticate', 'X-Hub-Signature sha1')
                self.send(401, "Valid signature required")
                return

            event = self.headers['X-Github-Event']
            payload = json.loads(data.decode("utf-8", "strict"))

            self.do_webhook(event, payload)
        except:
            self.send(500, "Internal server error")
            raise

    def do_webhook(self, event, payload):
        if self.server.payload_log:
            self.server.payload_log.write(json.dumps({
                "event": event,
                "payload": payload
            }))

        if event == "ping":
            self.send(200, "pong")
        elif event == "repository":
            if payload["action"] == "deleted":
                self.server.manager.delete_mirror(
                    payload["repository"]["full_name"])
            else:
                # There will be a push event too.
                pass
            self.send(202, "Accepted")
        elif event == "push":
            self.server.manager.mirror_repo(
                payload["repository"]["full_name"])
            self.send(202, "Accepted")
        else:
            self.server.logger.error(
                "501 Event not implemented: {}".format(event))
            self.send(501, "Event not implemented")

    def authenticate(self, data):
        if self.server.secret is None:
            # No authentication configured
            return True

        signature = self.headers['X-Hub-Signature']
        if signature is None:
            return False

        secret_string = self.server.secret.encode("utf-8")
        digest = hmac.new(secret_string, data, hashlib.sha1).hexdigest()
        correct = "sha1={}".format(digest)

        return hmac.compare_digest(correct, signature)

class WebhookServer(http.server.HTTPServer):
    def __init__(self, address, secret, manager, payload_log):
        self.manager = manager
        self.secret = secret
        self.timer = None
        self.periodic_interval = 0
        self.logger = logging.getLogger("WebhookServer")
        self.payload_log = payload_log

        http.server.HTTPServer.__init__(self, address, WebhookHandler)

        self.logger.info("Webhook server listening on {}:{}"
            .format(address[0], address[1]))

    def start_periodic(self, periodic_interval=15*60, update_orgs=[],
            update_older_than=0):
        self.periodic_interval = periodic_interval
        self.update_orgs = update_orgs
        self.update_older_than = update_older_than
        self._schedule_periodic()

        self.logger.info("Configuration: run periodic tasks every {} seconds"
            .format(self.periodic_interval))
        self.logger.info("Configuration: keep orgs {} in sync"
            .format(self.update_orgs))
        self.logger.info("Configuration: update mirrors older than {} seconds"
            .format(self.update_older_than))

    def stop_periodic(self):
        if self.timer:
            self.timer.cancel()
            self.timer = None

    def _schedule_periodic(self):
        if self.periodic_interval > 0:
            self.timer = threading.Timer(self.periodic_interval, self._periodic)
            self.timer.start()

    def _periodic(self):
        for org in self.update_orgs:
            self.logger.info("Periodic: synchronizing '{}'".format(org))
            self.manager.sync_org(org)

        if self.update_older_than:
            self.logger.info("Periodic: updating mirrors older than {} seconds"
                .format(self.update_older_than))
            self.manager.update_old_repos(maximum_age=self.update_older_than)

        self._schedule_periodic()

def serve(manager, secret=None, listen=("127.0.0.1", 8080),
        periodic_interval=15*60, update_orgs=[], update_older_than=0,
        payload_log=None):
    """
    Start an HTTP server to serve webhooks.

    This runs in the foreground.

    manager: a MirrorManager object.
    secret: the shared secret used to authenticate GitHub.
    address: (ip, port) to listen on.
    periodic_interval: how frequently to run periodic tasks.
    update_orgs: organizations to keep in sync.
    update_older_than: update mirrors that haven't been updated in this long.
    payload_log: file to log payloads to.
    """
    server = WebhookServer(listen, secret, manager, payload_log)
    if update_orgs or update_older_than:
        server.start_periodic(
            periodic_interval, update_orgs, update_older_than)
    try:
        server.serve_forever()
    finally:
        server.stop_periodic()


"""
Object Detection
Sources: https://www.pyimagesearch.com/2018/11/12/yolo-object-detection-with-opencv/
yolo model: https://pjreddie.com/darknet/yolo/
tf models: https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md

"""

import numpy as np
import tensorflow as tf
import cv2 as cv
import PIL.Image
import PIL.ImageDraw
import time
import ObjectLists

CONFIDENCE_THRESHOLD = 0.5

def faster_rcnn_oi(path):
    tf.reset_default_graph()

    # read the cnn
    with tf.io.gfile.GFile('object detection models/faster_rcnn_oi.pb', 'rb') as f:
        graph_def = tf.compat.v1.GraphDef()
        graph_def.ParseFromString(f.read())

    with tf.Session() as sess1:
        # Restore session
        sess1.graph.as_default()
        tf.import_graph_def(graph_def, name='')

        # read the image, run the model and draw the bounding boxes
        read_image(path)
        run_model(sess1)
        draw_boxes("open images")

    return draw_image

def faster_rcnn_coco(path):
    tf.reset_default_graph()

    # read the cnn
    with tf.io.gfile.GFile('object detection models/faster_rcnn_coco.pb', 'rb') as f:
        graph_def = tf.compat.v1.GraphDef()
        graph_def.ParseFromString(f.read())

    with tf.Session() as sess2:
        # Restore session
        sess2.graph.as_default()
        tf.import_graph_def(graph_def, name='')

        # read the image, run the model and draw the bounding boxes
        read_image(path)
        run_model(sess2)
        draw_boxes("coco")

    return draw_image

def mask_rcnn_coco(path):
    tf.reset_default_graph()

    # read the cnn
    with tf.io.gfile.GFile('object detection models/mask_rcnn_coco.pb', 'rb') as f:
        graph_def = tf.compat.v1.GraphDef()
        graph_def.ParseFromString(f.read())

    with tf.Session() as sess3:
        # Restore session
        sess3.graph.as_default()
        tf.import_graph_def(graph_def, name='')

        # read the image, run the model and draw the bounding boxes
        read_image(path)
        run_model(sess3)
        draw_boxes("coco")

    return draw_image

def ssd_inception_coco(path):
    tf.reset_default_graph()

    # read the cnn
    with tf.io.gfile.GFile('object detection models/ssd_inception_coco.pb', 'rb') as f:
        graph_def = tf.compat.v1.GraphDef()
        graph_def.ParseFromString(f.read())

    with tf.Session() as sess4:
        # Restore session
        sess4.graph.as_default()
        tf.import_graph_def(graph_def, name='')

        # read the image, run the model and draw the bounding boxes
        read_image(path)
        run_model(sess4)
        draw_boxes("coco")

    return draw_image

def ssd_resnet50_coco(path):
    tf.reset_default_graph()

    # read the cnn
    with tf.io.gfile.GFile('object detection models/ssd_resnet50_coco.pb', 'rb') as f:
        graph_def = tf.compat.v1.GraphDef()
        graph_def.ParseFromString(f.read())

    with tf.Session() as sess5:
        # Restore session
        sess5.graph.as_default()
        tf.import_graph_def(graph_def, name='')

        # read the image, run the model and draw the bounding boxes
        read_image(path)
        run_model(sess5)
        draw_boxes("coco")

    return draw_image

# yolo has to be run a bit differently
def yolo_v3_coco(path):
    tf.reset_default_graph()

    # load model
    model_file = "object detection models/yolov3.weights"
    config_file = "object detection models/yolov3.cfg"
    dnn = cv.dnn.readNetFromDarknet(config_file, model_file)

    # read the image, run the model and draw the bounding boxes
    read_image(path)
    run_model_yolo_v3(dnn)
    draw_boxes_yolo_v3("coco")

    return draw_image

def yolo_v3_oi(path):
    tf.reset_default_graph()

    # load model
    model_file = "object detection models/yolov3oi.weights"
    config_file = "object detection models/yolov3oi.cfg"
    dnn = cv.dnn.readNetFromDarknet(config_file, model_file)

    # read the image, run the model and draw the bounding boxes
    read_image(path)
    run_model_yolo_v3(dnn)
    draw_boxes_yolo_v3("open images")

    return draw_image

# helping methods
def read_image(path):
    global image, draw_image, height, width, inp
    image = cv.imread(path)
    draw_image = PIL.Image.fromarray(image)
    (height, width) = image.shape[:2]
    inp = image
    inp = inp[:, :, [2, 1, 0]]  # BGR2RGB

# runs the yolo model
def run_model_yolo_v3(dnn):
    global out, start, end
    layer_names = dnn.getLayerNames()
    layer_names = [layer_names[i[0] - 1] for i in dnn.getUnconnectedOutLayers()]
    # yolo only takes 320x320, 416x416 or 608x608
    blob = cv.dnn.blobFromImage(image, 1 / 255.0, size=(608, 608), swapRB=True, crop=False)
    dnn.setInput(blob)
    start = time.time()
    out = dnn.forward(layer_names)
    end = time.time()

# runs the tensorflow model
def run_model(sess):
    global out, start, end
    start = time.time()
    out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
                    sess.graph.get_tensor_by_name('detection_scores:0'),
                    sess.graph.get_tensor_by_name('detection_boxes:0'),
                    sess.graph.get_tensor_by_name('detection_classes:0')],
                   feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)})
    end = time.time()
    sess.close()

def draw_boxes_yolo_v3(trainingsdataset):
    global number_of_objects_detected, detected_objects
    number_of_objects_detected = 0
    detected_objects = []
    color = "red"

    for o in out:
        for detection in o:

            scores = detection[5:]
            class_id = np.argmax(scores)
            score = scores[class_id]

            if score > CONFIDENCE_THRESHOLD:
                if trainingsdataset == "coco":
                    obj = ObjectLists.get_yolo_v3_coco_object_by_classID(class_id)
                elif trainingsdataset == "open images":
                    obj = ObjectLists.get_oi_object_by_classID(class_id + 1)
                rounded_score_in_percent = 100 * (round(score, 4))
                detected_objects.append("{} {:.2f}%".format(obj, rounded_score_in_percent))
                number_of_objects_detected += 1

                # scale the bounding box coordinates back relative to the
                # size of the image, keeping in mind that YOLO actually
                # returns the center (x, y)-coordinates of the bounding
                # box followed by the boxes' width and height
                box = detection[0:4] * np.array([width, height, width, height])
                (center_x, center_y, w, h) = box.astype("int")
                # use the center (x, y)-coordinates to derive the top and
                # and left corner of the bounding box
                x = int(center_x - (w / 2))
                y = int(center_y - (h / 2))

                left, top, right, bottom = x, y, x + w, y + h
                draw = PIL.ImageDraw.Draw(draw_image)
                draw.rectangle([left, top, right, bottom], outline=color)
                draw.text([left, bottom], obj, fill=color)

# draws the bounding boxes for the tensorflow model
def draw_boxes(trainingsdataset):
    global number_of_objects_detected, detected_objects

    detected_objects = []
    number_of_objects_detected = 0
    number_of_total_detections = int(out[0][0])
    color = "red"

    for i in range(number_of_total_detections):
        score = float(out[1][0][i])
        bbox = [float(v) for v in out[2][0][i]]
        if score > CONFIDENCE_THRESHOLD:

            # append to list of detected objects
            class_id = int(out[3][0][i])
            if trainingsdataset == "coco":
                obj = ObjectLists.get_coco_object_by_classID(class_id)
            elif trainingsdataset == "open images":
                obj = ObjectLists.get_oi_object_by_classID(class_id)
            rounded_score_in_percent = 100 * (round(score, 4))
            detected_objects.append("{} {:.2f}%".format(obj, rounded_score_in_percent))
            number_of_objects_detected += 1

            # draw bounding box and object name
            left = bbox[1] * width
            top = bbox[0] * height
            right = bbox[3] * width
            bottom = bbox[2] * height
            draw = PIL.ImageDraw.Draw(draw_image)
            draw.rectangle([left, top, right, bottom], outline=color)
            draw.text([left, bottom], obj, fill=color)

# get the time it took to detect the objects
def get_detection_time():
    global end, start
    return end - start

# get the number of objects detected
def get_number_of_objects_detected():
    return number_of_objects_detected

# returns a list of all the objects that have been detected
def get_detected_objects():
    return detected_objects


# 17472 다리 만들기 2

import heapq

def island(x, y, n):
    queue = [[x, y]]

    while queue:
        cx, cy = queue.pop(0)
        if 0 > cx or N <= cx or 0 > cy or M <= cy:
            continue
        if not visited[cx][cy] and m[cx][cy] == 1:
            visited[cx][cy] = True
            m[cx][cy] = n
            for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                queue.append([cx + dx, cy + dy])

def bridge(x, y):
    for dx in range(x+1, N):
        if m[x][y] == m[dx][y]:
            break
        if m[dx][y] != 0 and m[x][y] != m[dx][y]:
            weight = abs(x - dx) - 1
            if weight >= 2:
                heapq.heappush(queue, [weight, m[x][y], m[dx][y]])
            break

    for dx in range(x-1, -1, -1):
        if m[x][y] == m[dx][y]:
            break
        if m[dx][y] != 0 and m[x][y] != m[dx][y]:
            weight = abs(x - dx) - 1
            if weight >= 2:
                heapq.heappush(queue, [weight, m[x][y], m[dx][y]])
            break

    for dy in range(y+1, M):
        if m[x][y] == m[x][dy]:
            break
        if m[x][dy] != 0 and m[x][y] != m[x][dy]:
            weight = abs(y - dy) - 1
            if weight >= 2:
                heapq.heappush(queue, [weight, m[x][y], m[x][dy]])
            break

    for dy in range(y-1, -1, -1):
        if m[x][y] == m[x][dy]:
            break
        if m[x][dy] != 0 and m[x][y] != m[x][dy]:
            weight = abs(y - dy) - 1
            if weight >= 2:
                heapq.heappush(queue, [weight, m[x][y], m[x][dy]])
            break

def find(a):
    if parent[a] == a:
        return a
    parent[a] = find(parent[a])
    return parent[a]

def union(a, b):
    A = find(a)
    B = find(b)

    if A == B:
        return False
    parent[A] = B
    return True

def mst(q):
    weight = 0
    while q:
        w, a, b = heapq.heappop(q)
        if union(a, b):
            weight += w

    return weight

N, M = map(int, input().split())

m = [list(map(int, input().split())) for _ in range(N)]
visited = [[False for _ in range(M)] for _ in range(N)]

cnt = 1
for i in range(N):
    for j in range(M):
        if not visited[i][j] and m[i][j] == 1:
            island(i, j, cnt)
            cnt += 1

queue = []
for i in range(N):
    for j in range(M):
        if m[i][j] != 0:
            bridge(i, j)

parent = [x for x in range(cnt)]

weight = mst(queue)

isConnect = True
for i in range(1, cnt):
    if find(i) != find(1):
        isConnect = False
        break
print(weight if isConnect else -1)


'''
Created on 29.05.2018

@author: tpilz
'''
from test.test_decimal import file

students = []

def get_students_titlecase():
    students_titlecase = []
    for student in students:
        students_titlecase.append(student["name"].title());
    return students_titlecase

def print_students_titlecase():
    students_titlecase = get_students_titlecase()
    print(students_titlecase)

def add_student(name, student_id=-1):
    student = {"name" : name,  "student_id" : student_id}
    students.append(student)

def save_file(student):
    try:
        f = open("students.txt", "a")
        f.write(student + "\n")
        f.close()
    except Exception as e:
        print("There is an exception with saving a student: ", e)

def read_file():
    try:
        f = open("students.txt", "r")
        for student in f.readlines():
            add_student(student)
        f.close()
    except Exception as e:
        print("There is an exception with reading a student: ", e)

def enter_students():
    while input("Would you like to add another student? Please type yes or no: ") == "yes":
        student_name = input("Enter student name: ")
        student_id = input("Enter student ID: ")
        add_student(student_name, student_id)
        save_file(student_name)

    for student in students:
        print("Student number {0} is {1} and his ID is {2}".format(students.index(student)+1, student["name"].title(), student["student_id"]))

read_file()
print_students_titlecase()
enter_students()

#student_list = get_students_titlecase()
#add_student(student_name, student_id)
#print_students_titlecase()


from teleportapi import *
import multiprocessing
import os.path
import sys
import json

def moreInfo(loc):
	print("details: {}".format(loc))
	return("More details to come soon for {}".format(loc))

def GUI(GUICmd, seqNum, fullName):
	GUIProc = multiprocessing.Process(target=updateGUI, args=(GUICmd, seqNum, fullName,))
	GUIProc.start()

def updateGUI(GUICmd, seqNum, fullName):
	getImage(fullName)
	GUICom(GUICmd, seqNum)

def GUICom(GUICmd, seqNum):
	# Commands: switchView, help, welcome, goodbye, defaultZoom
	# zoomIn, zoomOut, globetrotters, details, map, about, Option{}
	if(GUICmd == "in"):
		GUICmd = "zoomIn"
	elif(GUICmd == "out"):
		GUICmd = "zoomOut"
	elif(GUICmd == "reset"):
		GUICmd = "defaultZoom"

	print("GUICom: {}. Sequence# {}".format(GUICmd, seqNum))
	# Create JSON file to comm with HTML GUI
	data = {"GUICommand" : GUICmd, "SeqNum" : seqNum}
	with open('/var/www/html/GUICom.json', 'w') as outfile:
		json.dump(data, outfile)

def globeCntrl(command):
	# code to control the rotation and speed of the globe Comm with PIC uC
	print("globeCntrl: {}".format(command))
	with open('/home/pi/globe/Rotation/commands', 'w') as f:
    		f.write(command)


#!/usr/bin/env python3

import sys
import os
import json

json_file = sys.argv[1]
out_file = sys.argv[2]

with open(json_file) as f:
    packages = json.load(f)

dic = {}
for p in packages:
    dic[p['name']] = p

def check(p_name):
    return _check([], p_name)

def _check(visited, p_name):
    if p_name in visited:
        return True
    visited1 = visited + [p_name]
    p = dic[p_name]
    dkeys = p['dependencies_core'].keys()
    for d in dkeys:
        try:
            dp = dic[d]
        except KeyError:
            continue
        if _check(visited1, dp['name']):
            return True
    return False

pkeep = []
for p in packages:
    # if check(p['name']):
    #     print(p['name'])
    if not check(p['name']):
        pkeep.append(p)

with open(out_file, 'w') as f:
    json.dump(pkeep, f, sort_keys=True, indent=2, ensure_ascii=False)


import serial
from pattern.Subject import Subject
from pattern.Observer import Observer
from threading import Thread

class ArduinoSerial(Subject, Observer):

    def __init__(self):
        super().__init__()
        self.stop = False
        try:
            portx = "COM4" #串口名称
            bps = 9600     #波特率
            # 超时设置,None：永远等待操作，0为立即返回请求结果，其他值为等待超时时间(单位为秒）
            timex = None
            self.ser = serial.Serial(portx, bps, timeout=timex) #创建对象同时已经打开串口
            print("Serial:Serial {} opened".format(self.ser.name))

            def readSerial():
                while not self.stop:
                    if self.ser.inWaiting():  # Return the number of bytes in the receive buffer.
                        # Read size bytes from the serial port. If a timeout is set it
                        # may return less characters as requested. With no timeout it
                        # will block until the requested number of bytes is read.
                        data = self.ser.read(self.ser.inWaiting())
                        print("Sensor: <<", data)
                        state = int.from_bytes(data, byteorder='big')
                        if state == 0:  #arduino发送来0，表示传感器检测到磁铁进入
                            print("Serial:Sensor detected Megnet in")
                        elif state == 1: #arduino发送来1，表示传感器检测到磁铁远离
                            print("Serial:Sensor detected Megnet out")

            self.ser_thread = Thread(target=readSerial)  #线程轮训访问串口
            self.ser_thread.start()
        except Exception as e:
            print("Serial:Exception ", e)

    def send(self, msg):
        self.ser.write(msg)
        print("Serial: >> ", msg)

    def onMegnetIn(self):
        for observer in self.observers:
            observer.update("close")

    def onMegnetOut(self):
        pass

    def update(self, state):
        if state == "open":
            self.send(1) #向arduino发送1，使得arduino控制继电器开门
            print("Serial:Lock open")
        elif state == "close":
            self.send(0) #向arduino发送0，arduino控制继电器锁门
            print("Serial:Lock close")

    def close(self):
        self.stop = True
        self.update("close")
        self.ser_thread.join()
        self.ser.close()


import numpy as np
import tensorflow as tf
from tensorflow.python.util import deprecation
import argparse
import json
import os

deprecation._PRINT_DEPRECATION_WARNINGS = False

def get_ground_truth(
        tfrecords,
        num_parts=11,
        num_examples=1500
    ):

    tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.DEBUG)

    with tf.compat.v1.Session() as sess:

        # A producer to generate tfrecord file paths.
        filename_queue = tf.train.string_input_producer(
            tfrecords
        )

        # Construct a Reader to read examples from the tfrecords file
        reader = tf.TFRecordReader()
        _, serialized_example = reader.read(filename_queue)

        # Parse an Example to access the Features
        features = tf.io.parse_single_example(
            serialized_example,
            features={
                'image/id': tf.io.FixedLenFeature([], tf.string),
                'image/filename': tf.io.FixedLenFeature([], tf.string),
                'image/encoded': tf.io.FixedLenFeature([], tf.string),
                'image/height': tf.io.FixedLenFeature([], tf.int64),
                'image/width': tf.io.FixedLenFeature([], tf.int64),
                'image/object/bbox/xmin': tf.io.VarLenFeature(dtype=tf.float32),
                'image/object/bbox/ymin': tf.io.VarLenFeature(dtype=tf.float32),
                'image/object/bbox/xmax': tf.io.VarLenFeature(dtype=tf.float32),
                'image/object/bbox/ymax': tf.io.VarLenFeature(dtype=tf.float32),
                'image/object/bbox/count': tf.io.FixedLenFeature([], tf.int64),
                'image/object/bbox/score': tf.io.VarLenFeature(dtype=tf.float32),
                'image/object/bbox/label': tf.io.VarLenFeature(dtype=tf.int64),
                'image/object/parts/x': tf.io.VarLenFeature(dtype=tf.float32),  # x coord for all parts and all objects
                'image/object/parts/y': tf.io.VarLenFeature(dtype=tf.float32)
            }
        )

        # Get the image id, and the filename
        image_id = features['image/id']
        filename = features['image/filename']

        print(filename)

        num_bboxes = tf.cast(features['image/object/bbox/count'], tf.int32)

        parts_x = tf.expand_dims(features['image/object/parts/x'].values, 0)
        parts_y = tf.expand_dims(features['image/object/parts/y'].values, 0)

        parts = tf.concat(axis=0, values=[parts_x, parts_y])
        parts = tf.transpose(parts, [1, 0])
        parts = tf.reshape(parts, [-1, num_parts * 2])

        # Copies the image_id, [num_bboxes] times
        image_ids = tf.tile([[image_id]], [num_bboxes, 1])
        image_ids.set_shape([None, 1])

        # Initialize the session.
        init_op = tf.initialize_all_variables()
        sess.run(init_op)

        # Start the coordinator and queue runners.
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        # Initialize the dictionary where we'll store everything.
        results = {"image_id":[], "filename":[], "keypoints":[]}

        # Initialize the list of things we're trying to store.
        id_list = []
        keypoints = []
        fname_list = []

        # Parse each example into the relevant parts.
        for i in range(num_examples):
            [example_id, example_fname, example_parts] = sess.run([image_id, filename , parts])

            # Keep track of the id's and the filenames.
            id_list.append(int(example_id))
            fname_list.append(example_fname.decode())

            # print example_parts
            B = example_parts[0]
            W = example_parts[1]

            # Get the X and Y values of the Black and White mice.
            xB = B[0::2]
            yB = B[1::2]
            xW = W[0::2]
            yW = W[1::2]

            # Expand dims so we can stack them.
            xB = np.expand_dims(xB, axis= 0)
            xW = np.expand_dims(xW, axis= 0)
            yB = np.expand_dims(yB, axis= 0)
            yW = np.expand_dims(yW, axis= 0)

            # Stack the x's and y's.
            black_m = np.concatenate((xB,yB), axis=0)
            white_m = np.concatenate((xW,yW), axis=0)

            # Expand dims again so we can stack them.
            black_m = np.expand_dims(black_m, axis=0)
            white_m = np.expand_dims(white_m, axis=0)

            # Stack the black and white mouse together.
            both_m = np.concatenate((black_m, white_m), axis=0)

            # Expand the dims of the frame so we can stack them.
            both_m = np.expand_dims(both_m, axis=0)

            # Stack everything together.
            if i == 0:
                keypoints = both_m
            else:
                keypoints = np.concatenate((keypoints, both_m), axis=0)

        # Store everything as a list of dicts.
        results['keypoints'] = keypoints.tolist()
        results['image_id'] = id_list
        results['filename'] = fname_list

        coord.request_stop()
        coord.join(threads)
    return results

def parse_args():
    parser = argparse.ArgumentParser(description='Test an Inception V3 network')

    parser.add_argument('--tfrecords', dest='tfrecords',
                        help='paths to tfrecords files', type=str,
                        nargs='+', required=True)

    parser.add_argument('--num_parts', dest='num_parts',
                        help='the number of parts in the model', type=int,
                        required=True)

    parser.add_argument('--num_examples', dest='num_examples',
                        help='the number of examples to parse', type=int,
                        required=True)

    parser.add_argument('--savename', dest='savename',
                        help='the fullpath (with extension) to save the file to', type=str,
                        required=True)

    args = parser.parse_args()

    return args

if __name__ == '__main__':
    args = parse_args()

    results= get_ground_truth(
        tfrecords=args.tfrecords,
        num_parts=args.num_parts,
        num_examples=args.num_examples
    )
    # If we get something, dump it.
    if len(results) > 0:
        with open(args.savename, 'w') as fp:
            json.dump(results, fp)
    else:
        print("Ground truth generation failed.")


import logging
import os

# Create logger
logger = logging.getLogger('FLYTHON LOG')
logger.setLevel(logging.DEBUG)
# handlers instaces
fh = NotImplemented
ch = NotImplemented

def file_logger(level):

    global fh

    # create file handler and set level to debug
    if fh is NotImplemented:
        fh = logging.FileHandler(os.path.dirname(__file__) + '/' + 'flython.log')
        fh.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
        logger.addHandler(fh)
    fh.setLevel(getattr(logging, level.upper()))

def console_logger(level):

    global ch

    # create console handler and set level to warning
    if ch is NotImplemented:
        ch = logging.StreamHandler()
        ch.setFormatter(logging.Formatter('%(name)s: %(message)s'))
        logger.addHandler(ch)
    ch.setLevel(getattr(logging, level.upper()))


from django.conf.urls import url, include

from . import views

course_patterns = [

    url(r'^list/$',
        views.CourseList.as_view(),
        name='list'),

    url(r'^(?P<pk>[0-9]+)/$',
        views.CourseDetail.as_view(),
        name='detail'),

    url(r'^add/$',
        views.CourseAdd.as_view(),
        name='add'),

    url(r'^edit/(?P<pk>[0-9]+)/$',
        views.CourseEdit.as_view(),
        name='edit'),

]

urlpatterns = [

    url(r'^course/',
        include(course_patterns,
                namespace='course')),
]


import cv2

img = cv2.imread("Computer-Vision-py/DATA/00-puppy.jpg")
cv2.imshow("sU[p?]", img)
cv2.waitKey()

# proper way to display || uses q to close the windows
while (1):
    cv2.imshow("sU[p?]", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()
# creating shapes on images


class Solution(object):
    def largestRectangleArea(self, heights):
        """
        :type heights: List[int]
        :rtype: int
        """
        maxArea, stack = 0, [-1]
        heights.append(-1)

        i = 0
        while i < len(heights):
            if heights[i] >= heights[stack[-1]]:
                stack.append(i)
                i += 1
            else:
                ind = stack.pop()
                maxArea = max(maxArea, heights[ind] * (i - stack[-1] - 1))
        return maxArea

print(Solution().largestRectangleArea([4, 4, 4]))


#Chapter 21.4
# SEARCHING FOR A TARGET KEY IN A BINARY SEARCH TREE

class tnode:
    def __init__(self):
        self.data = None
        self.lchild = None
        self.rchild = None

def search(p, key):
    temp = p
    while(temp != None):
        if(temp.data == key):
            return(temp)
        else:
            if(temp.data > key):
                temp = temp.lchild
            else:
                temp = temp.rchild
    return(None)

def inorder1(p):
    stack = [None for i in range(100)]
    top = -1
    if(p != None):
        top = top + 1
        stack[top] = p
        p = p.lchild
        while(top >= 0):
            while(p != None):
                top = top + 1
                stack[top] = p
                p = p.lchild
            p = stack[top]
            top = top - 1
            print(str(p.data) +"\t")
            p = p.rchild
            if(p != None):
                top = top + 1
                stack[top] = p
                p = p.lchild

def insert(p, val):
    if(p == None):
        p = tnode()
        if(p == None):
            print("Cannot allocate")
            exit()
        p.data = val
    else:
        temp1 = p
        while(temp1 != None):
            temp2 = temp1
            if(temp1.data >= val):
                temp1 = temp1.lchild
            else:
                temp1 = temp1.rchild
        if(temp2.data >= val):
            temp2.lchild = tnode()
            temp2 = temp2.lchild
            if(temp2 == None):
                print("Cannot allocate")
                exit()
            temp2.data = val
        else:
            temp2.rchild = tnode() # inserts new node to kleft
            temp2 = temp2.rchild
            if(temp2 == None):
                print("Cannot allocate")
                exit()
            temp2.data = val
    return(p)

def main():  # Main function
    root = None
    print("Enter the number of nodes")
    n = int(raw_input())
    while(n > 0):
        print("Enter the data value")
        x = int(raw_input())
        root = insert(root, x)
        n = n - 1
    print("The values are")
    inorder1(root)
    print("\nEnter the value ")
    n = int(raw_input())
    temp = search(root, n)
    if(temp != None):
        print("\nThe data value is present in the tree")
    else:
        print("\nThe data is not present")

main()


# !/usr/bin python3
# encoding: utf-8 -*-
# @author: 沙陌 微信：Matongxue_2
# @Time: 2022-02-27 11:41
# @Copyright：北京码同学
import javaobj
import pytest
from requests_study.mtxshop_api import *
from db_study.db_util import *

@pytest.mark.usefixtures('ceshilei')
class TestBuyNowDataDriver:

    # def setup_class(self):
    #     buyer_login()
    # 测试数据管理对象
    test_data = [
        ['必填参数均正确没有非必填字段',5173,   1,       200,'',''],
        ['产品已下架',               5875,   1,      500,'004','产品已下架'],
        ['产品不存在',              456665,  1,      500,'004','不合法'],
        ['购买数量超过库存',           5173, 99999999, 500,'451','商品库存已不足，不能购买。'],
        ['购买数量为0',               5173,  0,        400,'004','购买数量必须大于0'],
        ['购买数量为负数',             5173,   -1,     400,'004','购买数量必须大于0'],
        ['购买数量为空',               5173,  None,    400,'004','购买数量不能为空'],
        ['产品id为空',                None,      1,    400,'004','产品id不能为空']
    ]
    @pytest.mark.usefixtures('ceshilei') # 这也是一种调用fixture方法的方式，参数是fixture方法的名字
    @pytest.mark.parametrize('casename,sku_id,num,expect_status_code,expect_busi_code,expect_message',test_data)
    def test_buy_now(self,casename,sku_id,num,expect_status_code,expect_busi_code,expect_message,redis_util,get_token):
        # 调用接口时传入测试数据中相关的变量
        resp = buy_now(sku_id=sku_id,num=num)
        # 做断言
        status_code = resp.status_code
        assert status_code == expect_status_code
        print(resp.text) # 由于响应信息可能为空，所以我们使用resp.text打印结果，因为空的字符串是无法使用resp.json()去获取的
        # 注意这个接口如果业务正常成功，那么响应信息是空的
        if status_code!=200:
            # 状态码不是200时，才进行响应信息的校验
            try:
                resp_json = resp.json()
            except:
                pass
            code = resp_json['code']
            # assert code == expect_busi_code
            pytest.assume(code == expect_busi_code)
            # 判断message的值是 商品已失效，请刷新购物车
            message = resp_json['message']
            # assert message == expect_message
            pytest.assume(message == expect_message)
        if status_code == 200:
            # 从redis中获取数据并做校验
            uid = get_token #get_token是个fixture，调用时他就相当于他的返回值，我们返回了uid
            # 格式化字符串时，如果字符串本身就有大括号，那么需要使用双大括号
            res = redis_util.get(f'{{BUY_NOW_ORIGIN_DATA_PREFIX}}_{uid}')
            res_list = javaobj.loads(res)  # 这是得到结果转换成python的列表
            # 业务上这个列表中，只会存储一个数据
            obj = res_list[0]  # 通过索引得到列表里的数据
            redis_num = obj.__getattribute__('num') # 实际结果
            redis_skuId = obj.__getattribute__('skuId') # 实际结果
            pytest.assume(redis_num == num)
            pytest.assume(redis_skuId == sku_id)

class TestCreateTrade:
    # def setup_class(self):
    #     buyer_login()

    client_data = ['PC','WAP','NATIVE','REACT','MINI'] # 5
    way_data = ['BUY_NOW','CART'] # 2
    expect_status_code = [200] # 1
    # 上面三组数据都作为参数，同时组合形成测试数据，组合结果总共5*2*1=10
    # 这种方式叫做 pytest笛卡尔积参数化
    # 笛卡尔积使用场景，在一个接口参数中存在多个参数具备有效值，并且他们需要组合才能覆盖正向场景，就可以使用笛卡尔积
    @pytest.mark.parametrize('client',client_data)
    @pytest.mark.parametrize('way',way_data)
    @pytest.mark.parametrize('expect_status',expect_status_code)
    def test_create_trade(self,client,way,expect_status,db_util):
        # 对于创建交易这个来说，在调用之前需要先调用立即购买或者添加购物车接口
        # 因为创建交易会根据参数的不同去redis缓存中取订单相关的信息，然后创建
        # 当way参数是BUY_NOW会去缓存中读立即购买的数据
        # 当way参数是CART时会去缓存中读添加购物车的数据
        # 如何让缓存中产生创建交易接口需要的数据，就是调用对应的接口
        if way == 'BUY_NOW':
            buy_now()
        elif way == 'CART':
            delete_cart() #先清空购物车，防止多人使用或者购物车里有异常数据
            add_cart()
        resp = create_trade(client=client,way=way)
        status_code = resp.status_code
        print(resp.text)
        assert status_code == expect_status

        # 判断数据库数据是否正确
        # 数据库里是实际值，那么期望数据是谁
        # 可以是接口发起时请求得数据，也可以是接口响应的数据
        data = db_util.select(
            'SELECT * FROM mtxshop_trade.es_order WHERE member_id=59 ORDER BY order_id DESC LIMIT 1 ;')
        db_res=data[0]
        print(db_res)
        db_trade_sn=db_res['trade_sn']
        print(db_trade_sn)
        items_json=db_res['items_json']
        print(items_json)
        items_json=json.loads(items_json)
        print(json.dumps(items_json))
        db_skuid=jsonpath.jsonpath(items_json,'$..sku_id')[0]
        db_num=jsonpath.jsonpath(items_json,'$..num')[0]

        resp_json=resp.json()
        resp_trade_sn=resp_json['trade_sn']
        pytest.assume(db_trade_sn==resp_trade_sn)
        pytest.assume(db_skuid==5173)
        pytest.assume(db_num==1)


from random import randint
import qpageview
import shutil
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
from PyQt5.QtGui import QPixmap, QFont, QCloseEvent
from PyQt5.QtCore import Qt, pyqtSignal
from translation_manager import Translator
import sys
import subprocess
from threading import Thread
import tempfile
import shlex
import os
import signal
import time
from queue import Queue
import zipfile

class PageViewer(QWidget):
    # used to trigger a function in the main thread from the secondary thread.
    # Qt does not allow calling some widget functions from a secondary thread.
    signLoadingArrested = pyqtSignal(bool, bool, bool, str)
    signConversionFinished = pyqtSignal(str)

    def __init__(self, parent=None, app=None):
        super(PageViewer, self).__init__(parent)
        app.aboutToQuit.connect(self.__app_is_closing__)
        self.temp_dir = None
        # a precaution to make sure that the file selected PREVIOUSLY
        #  is not loaded at the end of the conversion to pdf.
        self.current_thread_id = 0
        self.conversion_process = None
        self.is_active_viewer = False
        self.libreoffice_command = self.__get_libreoffice_command__()
        # the page to see if they have problems with libreoffice.
        self.libreoffice_help_page = "https://github.com/Nyre221/dolphin-quick-view/tree/main/extras/Libreoffice%20troubleshooting"
        # to get translations
        self.translator = Translator()
        # name font
        self.font_label_message_name = QFont()
        self.font_label_message_name.setPointSize(14)
        self.font_label_message_name.setBold(True)
        # header font
        self.font_label_message_header = QFont()
        self.font_label_message_header.setPointSize(11)
        self.font_label_message_header.setBold(True)
        # message font
        self.font_label_message = QFont()
        self.font_label_message.setPointSize(10)

        # qpageview
        self.qpage = qpageview.View()
        # set scroll speed
        self.qpage.verticalScrollBar().setSingleStep(50)

        # img
        self.logo_img = QLabel()
        self.logo_img.setAlignment(Qt.AlignCenter)
        # sets the image size based on the screen size
        self.logo_size = QApplication.primaryScreen().size()*0.135
        self.logo_img.setPixmap(QPixmap("/usr/share/icons/breeze-dark/mimetypes/64/x-office-document.svg").scaled(
            self.logo_size, Qt.KeepAspectRatioByExpanding, Qt.FastTransformation))

        # labels
        self.label_message_name = QLabel()
        self.label_message_name.setAlignment(Qt.AlignCenter)
        self.label_message_name.setFont(self.font_label_message_name)
        self.label_message_header = QLabel()
        self.label_message_header.setFont(self.font_label_message_header)
        self.label_message_header.setAlignment(Qt.AlignCenter)
        self.label_message = QLabel()
        self.label_message.setFont(self.font_label_message)
        self.label_message.setAlignment(Qt.AlignCenter)
        # allows to open external links.
        self.label_message.setWordWrap(True)
        self.label_message.setTextFormat(Qt.RichText)
        self.label_message.setTextInteractionFlags(Qt.TextBrowserInteraction)
        self.label_message.setOpenExternalLinks(True)
        # layouts
        # main
        self.layout_main = QVBoxLayout()
        self.setLayout(self.layout_main)
        # message layout
        self.layout_message = QVBoxLayout()

        # labels container
        self.container_message = QWidget()
        self.container_message.setLayout(self.layout_message)

        # adding to layout
        self.layout_main.addWidget(self.container_message)
        self.layout_main.addWidget(self.qpage)
        self.layout_message.addWidget(self.logo_img)
        self.layout_message.addWidget(self.label_message_name)
        self.layout_message.addWidget(self.label_message_header)
        self.layout_message.addWidget(self.label_message)
        self.layout_message.insertStretch(-1)

        # connecting signals
        # used to trigger a function in the main thread from the secondary thread.
        self.signLoadingArrested.connect(self.__loading_arrested__)
        self.signConversionFinished.connect(self.__conversion_finished__)
        # placeholder
        self.__set_placeholder_text__()

    def load_file(self, path, extension):
        # loading message
        self.container_message.show()
        self.__set_placeholder_text__()
        self.label_message_name.setText(path.split("/")[-1])
        # set the focus so you can scroll through the pages with the arrow keys.
        self.qpage.setFocus()
        # gives space to the loading screen.
        self.qpage.hide()
        # removes the previously viewed document.
        self.qpage.clear()
        # It is used to kill the process if the viewer has been hidden.
        self.is_active_viewer = True

        # get a random id for thread
        # used to prevent the converted pdf file from being loaded if it is no longer the selected one.
        random_id = randint(0, 1000)
        # check that the id is not the same as the previous one.
        while random_id == self.current_thread_id:
            random_id = randint(0, 1000)
        # killing the process would have been more logical,
        # but I noticed that it can behave strangely sometimes and I don't want to risk anything.
        # this problem happens when you view a pdf after viewing a file that requires conversion.
        self.current_thread_id = random_id

        # load and set the display mode based on the file.
        if extension == ".pdf":
            self.qpage.loadPdf(path)
            self.qpage.setViewMode(qpageview.FitWidth)
            self.__file_loaded__()
        elif extension in [".png", ".jpeg", ".jpg", ".webp"]:
            self.qpage.loadImages([path])
            self.qpage.setViewMode(qpageview.FitBoth)
            self.__file_loaded__()
        elif extension in [".svg",".svgz"]:
            self.qpage.loadSvgs([path])
            self.qpage.setViewMode(qpageview.FitBoth)
            self.__file_loaded__()
        elif extension in [".doc", ".docx", ".odt", ".ods", ".xlsx", ".xls", ".csv", ".odp", ".ppt", ".pptx"]:
            # converts to pdf.
            self.__convert_document__(path=path)
        elif extension in [".kra"]:
            # converts to pdf.
            self.__open_kra__(path=path)

    def __file_loaded__(self):
        # hides the messages/loading screen and shows qpage.
        self.container_message.hide()
        self.qpage.show()
        # resets the page position to 0.
        # this is necessary because qpageview does not set the position to 0
        #  if the viewing mode has been changed to a different one.
        self.qpage.setPosition((0, 0, 0), False)

    def hide(self) -> None:
        # clears qpageview screen to save memory(doesn't seem to work).
        self.qpage.clear()
        self.is_active_viewer = False
        return super().hide()

    def __open_kra__(self,path):
        # create a temporary folder
        if self.temp_dir is None:
            self.temp_dir = tempfile.mkdtemp()

        # open .kra and extract the preview
        with zipfile.ZipFile(path, 'r') as zip_ref:
            zip_ref.extract("mergedimage.png",self.temp_dir)

        output_dir = self.temp_dir+"/mergedimage.png"
        self.load_file(output_dir,".png")

    def __convert_document__(self, path):
        # shows a message and stops the function if libreoffice was not found.
        if self.libreoffice_command is None:
            self.__loading_arrested__(loffice_not_found=True)
            print("libreoffice not found")
            return

        # create a temporary folder to put the pdfs in.
        if self.temp_dir is None:
            self.temp_dir = tempfile.mkdtemp()

        # use another thread to avoid blocking the interface.
        th = Thread(target=self.__convert_document_thread__,
                    args=(path, self.temp_dir))
        th.setDaemon(True)
        th.start()

    def __convert_document_thread__(self, path, temp_dir):
        # get the thread id
        thread_id = self.current_thread_id
        # kills the process group if the old process is still running.
        self.__kill_conversion_process__(self.conversion_process)

        # libreoffice shell command
        commands = f"{self.libreoffice_command} --headless --nolockcheck --norestore --convert-to pdf '{path}' --outdir {temp_dir} "
        args = shlex.split(commands)
        # preexec_fn=os.setsid is used to create a process group.
        # this is necessary to terminate libreoffice and reopen it later.
        self.conversion_process = subprocess.Popen(
            args=args, preexec_fn=os.setsid, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        # the global variable is used to kill the old process when a new thread starts,
        #  the local one is used to get the return code and information without risking taking those of the new process.
        local_process = self.conversion_process
        # to kill the process after X sec
        # I don't want to make the user wait forever if the file takes too long to load.
        start = time.time()
        end = start + 10
        # checks if the maximum time limit is reached.
        while True:
            if local_process.poll() is not None:
                # exits the loop if the process has finished.
                break

            if time.time() >= end:
                # kill the process
                os.killpg(local_process.pid, signal.SIGTERM)
                # display the message
                self.signLoadingArrested.emit(True, False, False, "")
                # exit
                break

            time.sleep(0.1)

        # store the return code
        return_code = local_process.returncode
        # send the path of the pdf if the return code is 0, the viewer is active and the thread id is the current one
        if thread_id == self.current_thread_id and return_code == 0 and self.is_active_viewer:
            output = local_process.communicate()[0]
            try:
                # get the path
                pdf_path = output.decode(
                    "utf-8").split(">")[1].split("using filter")[0].strip()

                if not os.path.exists(pdf_path):
                    # shows an error if for some reason libreoffice fails to give a path.
                    # True:path error
                    self.signLoadingArrested.emit(False, False, True, "")
                    # kills the process group
                    self.__kill_conversion_process__(local_process)
                    return

                # send
                self.signConversionFinished.emit(pdf_path)
                # kills the process group
                self.__kill_conversion_process__(local_process)
            except IndexError:
                print("IndexError")
                process_error = local_process.communicate()[1]
                # shows an error if for some reason libreoffice fails to give a path.
                self.signLoadingArrested.emit(
                    False, False, False, process_error.decode("utf-8"))
                # kills the process group
                self.__kill_conversion_process__(local_process)
                return

    def __conversion_finished__(self, path: str):
        # calls the function with the path of the new pdf
        self.load_file(path=path, extension=".pdf")
        self.qpage.setFocus()

    def __get_libreoffice_command__(self):
        command = None
        # check if libreoffice is installed.
        if shutil.which('libreoffice'):
            command = "libreoffice"
        elif shutil.which('flatpak'):
            # check if libreoffice is installed with flatpak
            if subprocess.run(["bash", "-c", "flatpak list | grep -i libreoffice"]).returncode == 0:
                command = "flatpak run org.libreoffice.LibreOffice"
        else:
            command = None

        # return the command to use
        return command

    def __loading_arrested__(self, timeout=False, loffice_not_found=False, path_error=False, custom_message=""):
        # sets the information in the labels.
        self.label_message_header.setText(
            self.translator.get_translation("libreoffice_preview_unavailable"))
        html = f"{self.translator.get_translation('click_here')}"
        if timeout:
            self.label_message.setText(
                self.translator.get_translation("libreoffice_timeout"))
        elif loffice_not_found:
            self.label_message.setText(self.translator.get_translation(
                "libreoffice_not_found")+" "+html)
        elif path_error:
            self.label_message.setText(self.translator.get_translation(
                "libreoffice_file_not_found")+" "+html)
        if custom_message != "":

            self.label_message.setText(custom_message + " : " + html)

    def __set_placeholder_text__(self):
        self.label_message_header.setText(
            self.translator.get_translation("loading_placeholder"))
        # there is no need to translate it because the name is set immediately.
        self.label_message_name.setText("Name")
        self.label_message.setText("")

    def __kill_conversion_process__(self, process):
        # terminate the process used for PDF conversion if it is alive.
        if process is not None and process.poll() is None:
            os.killpg(process.pid, signal.SIGTERM)

    def __app_is_closing__(self):
        print("closing pageviewer")
        # closes the libreoffice process if it is still active.
        # This is because the process may remain active and slow down quickview the next time it is started.
        self.__kill_conversion_process__(self.conversion_process)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = PageViewer()
    widget.resize(640, 480)
    widget.show()
    sys.exit(app.exec_())


import numpy as np
import keras
from sklearn.model_selection import train_test_split
from dataset import get_set
from model import getModel
from plots import plot
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt

model_name = 'model/model_train.hdf5'

def train(audio_path,plot_matrix = False):

    x_data, y_data=get_set(26,9,audio_path)
    x_data = keras.preprocessing.sequence.pad_sequences(x_data, maxlen=100)

    x_train, x_test, Y_train, Y_test = train_test_split(x_data, y_data,test_size=0.1, random_state=42)

    y_train = keras.utils.to_categorical(Y_train, 16)
    y_test = keras.utils.to_categorical(Y_test, 16)

    model = getModel((x_train.shape[1],x_train.shape[2]), y_train.shape[1])

    history = model.fit(x_train, y_train,batch_size=10,epochs=137, verbose=1,validation_data=(x_test, y_test))
    # Plot training & validation accuracy values
    plt.plot(history.history['acc'])
    plt.plot(history.history['val_acc'])
    plt.title('Model accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Test'], loc='upper left')
    plt.show()

    # Plot training & validation loss values
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('Model loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Test'], loc='upper left')
    plt.show()
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
    model.save(model_name)

    if plot_matrix:
        plot(x_test, Y_test,model_name)

if __name__=='__main__':
    train('audio/normalized_data',plot_matrix=True)


"""YOLO_v3 Model Defined in Keras."""

from functools import wraps

from keras import backend as K
from keras.layers import Conv2D, Add, ZeroPadding2D, UpSampling2D, Concatenate, MaxPooling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras.regularizers import l2

from .utils import compose

@wraps(Conv2D)
def DarknetConv2D(*args, **kwargs):
    """Wrapper to set Darknet parameters for Convolution2D."""
    darknet_conv_kwargs = {'kernel_regularizer': l2(5e-4),
                           'padding': 'valid' if kwargs.get('strides') == (2, 2) else 'same'}
    darknet_conv_kwargs.update(kwargs)
    return Conv2D(*args, **darknet_conv_kwargs)

def DarknetConv2D_BN_Leaky(*args, **kwargs):
    """Darknet Convolution2D followed by BatchNormalization and LeakyReLU."""
    no_bias_kwargs = {'use_bias': False}
    no_bias_kwargs.update(kwargs)
    return compose(
        DarknetConv2D(*args, **no_bias_kwargs),
        BatchNormalization(),
        LeakyReLU(alpha=0.1))

def resblock_body(x, num_filters, num_blocks):
    '''A series of resblocks starting with a downsampling Convolution2D'''
    # Darknet uses left and top padding instead of 'same' mode
    x = ZeroPadding2D(((1, 0), (1, 0)))(x)
    x = DarknetConv2D_BN_Leaky(num_filters, (3, 3), strides=(2, 2))(x)
    for i in range(num_blocks):
        y = compose(
            DarknetConv2D_BN_Leaky(num_filters // 2, (1, 1)),
            DarknetConv2D_BN_Leaky(num_filters, (3, 3)))(x)
        x = Add()([x, y])
    return x

def darknet_body(x):
    '''Darknent body having 52 Convolution2D layers'''
    x = DarknetConv2D_BN_Leaky(32, (3, 3))(x)
    x = resblock_body(x, 64, 1)
    x = resblock_body(x, 128, 2)
    x = resblock_body(x, 256, 8)
    x = resblock_body(x, 512, 8)
    x = resblock_body(x, 1024, 4)
    return x

def make_last_layers(x, num_filters, out_filters):
    '''6 Conv2D_BN_Leaky layers followed by a Conv2D_linear layer'''
    x = compose(
        DarknetConv2D_BN_Leaky(num_filters, (1, 1)),
        DarknetConv2D_BN_Leaky(num_filters * 2, (3, 3)),
        DarknetConv2D_BN_Leaky(num_filters, (1, 1)),
        DarknetConv2D_BN_Leaky(num_filters * 2, (3, 3)),
        DarknetConv2D_BN_Leaky(num_filters, (1, 1)))(x)
    y = compose(
        DarknetConv2D_BN_Leaky(num_filters * 2, (3, 3)),
        DarknetConv2D(out_filters, (1, 1)))(x)
    return x, y

def yolo_body(inputs, num_anchors, num_classes):
    """Create YOLO_V3 model CNN body in Keras."""
    darknet = Model(inputs, darknet_body(inputs))
    x, y1 = make_last_layers(darknet.output, 512, num_anchors * (num_classes + 5))

    x = compose(
        DarknetConv2D_BN_Leaky(256, (1, 1)),
        UpSampling2D(2))(x)
    x = Concatenate()([x, darknet.layers[152].output])
    x, y2 = make_last_layers(x, 256, num_anchors * (num_classes + 5))

    x = compose(
        DarknetConv2D_BN_Leaky(128, (1, 1)),
        UpSampling2D(2))(x)
    x = Concatenate()([x, darknet.layers[92].output])
    x, y3 = make_last_layers(x, 128, num_anchors * (num_classes + 5))

    return Model(inputs, [y1, y2, y3])

def tiny_yolo_body(inputs, num_anchors, num_classes):
    '''Create Tiny YOLO_v3 model CNN body in keras.'''
    x1 = compose(
        DarknetConv2D_BN_Leaky(16, (3, 3)),
        MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
        DarknetConv2D_BN_Leaky(32, (3, 3)),
        MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
        DarknetConv2D_BN_Leaky(64, (3, 3)),
        MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
        DarknetConv2D_BN_Leaky(128, (3, 3)),
        MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
        DarknetConv2D_BN_Leaky(256, (3, 3)))(inputs)
    x2 = compose(
        MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
        DarknetConv2D_BN_Leaky(512, (3, 3)),
        MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'),
        DarknetConv2D_BN_Leaky(1024, (3, 3)),
        DarknetConv2D_BN_Leaky(256, (1, 1)))(x1)
    y1 = compose(
        DarknetConv2D_BN_Leaky(512, (3, 3)),
        DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x2)

    x2 = compose(
        DarknetConv2D_BN_Leaky(128, (1, 1)),
        UpSampling2D(2))(x2)
    y2 = compose(
        Concatenate(),
        DarknetConv2D_BN_Leaky(256, (3, 3)),
        DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))([x2, x1])

    return Model(inputs, [y1, y2])

def yolo_head(feats, anchors, num_classes, input_shape, calc_loss=False):
    """Convert final layer features to bounding box parameters."""
    num_anchors = len(anchors)
    # Reshape to batch, height, width, num_anchors, box_params.
    anchors_tensor = K.reshape(K.constant(anchors), [1, 1, 1, num_anchors, 2])

    grid_shape = K.shape(feats)[1:3]  # height, width
    grid_y = K.tile(K.reshape(K.arange(0, stop=grid_shape[0]), [-1, 1, 1, 1]),
                    [1, grid_shape[1], 1, 1])
    grid_x = K.tile(K.reshape(K.arange(0, stop=grid_shape[1]), [1, -1, 1, 1]),
                    [grid_shape[0], 1, 1, 1])
    grid = K.concatenate([grid_x, grid_y])
    grid = K.cast(grid, K.dtype(feats))

    feats = K.reshape(
        feats, [-1, grid_shape[0], grid_shape[1], num_anchors, num_classes + 5])

    # Adjust preditions to each spatial grid point and anchor size.
    box_xy = (K.sigmoid(feats[..., :2]) + grid) / K.cast(grid_shape[::-1], K.dtype(feats))
    box_wh = K.exp(feats[..., 2:4]) * anchors_tensor / K.cast(input_shape[::-1], K.dtype(feats))
    box_confidence = K.sigmoid(feats[..., 4:5])
    box_class_probs = K.sigmoid(feats[..., 5:])

    if calc_loss == True:
        return grid, feats, box_xy, box_wh
    return box_xy, box_wh, box_confidence, box_class_probs


import streamlit as st
import requests
import pandas as pd
from datetime import date, timedelta
import base64

st.set_page_config(page_title="Historical Staking Data", page_icon="🔍", layout="wide", initial_sidebar_state="auto")

st.title("Get Historical Data for Assets on StakingRewards.com")

st.info("""
This web app allows you to fetch historical data for various assets on StakingRewards.com.

Step 1: Enter the slug of the asset that you want historical data for

Step 2: Enter the metric of the asset that you want historical data for

Step 3: Enter the date range that you are looking for

Step 4: Click 'Get my Data'

When inputting multiple assets, please separate them by a comma without a space.
""")

today = date.today()

url = 'https://api.stakingrewards.com/public/query'

api_key = st.text_input("Enter your API key", type="password")

headers = {"X-API-Key": api_key}

query = """
query getHistoricalMetrics($slugs:[String!],$limit:Int,$offset:Int, $metricKeys:[String!],$timeStart:Date, $isActive:Boolean){
  assets(where:{slugs:$slugs, isActive:$isActive},limit:20){
    name
    metrics(where:{metricKeys:$metricKeys, createdAt_gt:$timeStart},limit:$limit, offset:$offset, order:{createdAt:asc}){
      defaultValue
      createdAt
      label
    }
  }
}
"""

def fetch_data(slugs, metricKeys, timeStart):
    variables = {
        "slugs": slugs,
        "limit": len(metricKeys),
        "offset": 0,
        "metricKeys": metricKeys,
        "timeStart": str(timeStart),
        "isActive": True,
    }

    response = requests.post(url, headers=headers, json={"query": query, "variables": variables})
    print(response.text)
    response_dict = response.json()
    assets_dict = response_dict['data']['assets']

    return assets_dict

def create_download_link(data_df, title="Download CSV file", filename="comparison_data.csv"):
    csv = data_df.to_csv(index=False)
    b64 = base64.b64encode(csv.encode()).decode()
    return f'{title}'

def prepare_and_display_data(assets_data, metricKeys):
    data = []
    for assets in assets_data:
        for asset in assets:
            asset_data = {
                "Asset Name": asset["name"],
                "Date": asset["metrics"][0]["createdAt"],
            }
            asset_data.update({metric["label"]: metric["defaultValue"] for metric in asset["metrics"]})
            data.append(asset_data)

    data_df = pd.DataFrame(data)

    st.markdown(create_download_link(data_df), unsafe_allow_html=True)
    st.write(data_df)

slugs_input = st.text_input("Enter the Slugs of each Asset (comma-separated without a space)", "polkadot")
metrics_input = st.text_input("Enter the Metrics you are looking for (comma-separated without a space)", "staking_ratio,staking_marketcap")

st.markdown("""

""", unsafe_allow_html=True)

st.markdown("""

    🔍 View available slugs and metrics that you can query for each asset

""", unsafe_allow_html=True)

start_date = st.date_input("Enter the starting Date", value=today - timedelta(days=30))
end_date = st.date_input("Enter the ending Date", value=today)

compare_button = st.button("Get my Data!")

st.markdown("---")

if compare_button:
    slugs = slugs_input.split(',')
    metrics = metrics_input.split(',')

    total_days = (end_date - start_date).days + 1

    assets_data = []
    current_date = start_date
    processed_days = 0

    progress_bar = st.progress(0)

    with st.spinner("Loading..."):
        while current_date <= end_date:
            assets = fetch_data(slugs, metrics, current_date)
            assets_data.append(assets)
            current_date += timedelta(days=1)
            processed_days += 1

            progress = processed_days / total_days
            progress_bar.progress(progress)

    st.balloons()
    prepare_and_display_data(assets_data, metrics)


import os
import numpy as np
import pandas as pd
from multiprocessing import Pool
import glob
import gc

from utils_mod import tf_itr
from methods import *

def conv_pred(el, file):
    t = 10
    idx = np.argsort(el)[::-1]

    id_10 = file[idx[:t]]
    return ' '.join(['{}'.format(i) for i in id_10])

def predict(test_relative_path, submission_path, FOLDER, num=None):
    cate = pd.read_csv('Category.csv', sep=',')['Label_num'].values
    # LR
    # model = LR(3862)

    # shared-bottom/SB
    # model = SB(cate, [16], [2048, 4096])

    # SNR
    # model = SNR_Trans(cate, [16, 16] ,bottom_0_struct=[512,512,512,512,512,512,512,512],bottom_1_struct=[512,512,512,512,512,512,512,512],beta=0.9, zeta=1.1, gamma=-0.5)

    # CNN
    # model = CNN2(3862)

    # FCN
    model = FCN(3862)

    model.build((None, 1152))

    batch = 102400
    label_num = 3862
    wfn = sorted(glob.glob('weights/*.h5'))[-1]
    model.load_weights(wfn)
    print('loaded weight file: %s' % wfn)

    file = pd.read_csv('Mapping_out.csv', sep=',')['OldLabel'].values

    cnt = 0
    for d in tf_itr('', batch, label_num=label_num, FOLDER=FOLDER, num=num):
        cnt += 1
        idx, x1_val, x2_val, _ = d
        val_in = np.concatenate((x1_val, x2_val), axis=1)
        ypd = model.predict(val_in, verbose=1, batch_size=32)
        del x1_val, x2_val

        out = []
        for i in range(len(ypd)):
            out.append(conv_pred(ypd[i], file))

        df = pd.DataFrame.from_dict({'VideoId': idx, 'Label': out})

        if not os.path.exists(submission_path + '/tmp/' + test_relative_path):
            os.mkdir(submission_path + '/tmp/' + test_relative_path)

        df.to_csv(submission_path + '/tmp/' + test_relative_path + '/subm' + str(cnt) + '.csv', header=True,
                  index=False,
                  columns=['VideoId', 'Label'])
        gc.collect()

    f_subs = glob.glob(os.path.join(submission_path + '/tmp/' + test_relative_path + '/subm*.csv'))
    print(f_subs)
    df = pd.concat((pd.read_csv(f) for f in f_subs))
    df.to_csv(os.path.join(submission_path + '/11.csv'), index=None)

if __name__ == '__main__':
    submission_path = './output'

    #predict('', submission_path, 'train_validation')
    #predict('train200', submission_path, '../train_s')
    predict('valid100', submission_path, '../validation_s')


import pandas as pd
import time

#############################################################################
####################################INPUT####################################

# select which scraped comments to process
comments_path = "RawScraped/raw_comments_combined.csv"

#############################################################################
#############################################################################

# Get the scraped comments that you want to clean
df = pd.read_csv(comments_path)
df.set_index("id", inplace=True)
num_initial_comments = len(df)
df_dict = df.to_dict()

# Identify low rated comments
start_time = time.time()
print("Identifying low-rated comments...")
ids_to_drop = set()
for comment_id, comment_score in df_dict['score'].items():
    if int(comment_score) < 3:
        ids_to_drop.add(comment_id)
print("--- %s seconds ---" % (time.time() - start_time))
print()

# Identify very short comments
start_time = time.time()
print("Identifying short comments...")
for comment_id, comment_body in df_dict['body'].items():
    if len(str(comment_body)) < 12:
        ids_to_drop.add(comment_id)
print("--- %s seconds ---" % (time.time() - start_time))
print()

# Drop appropriate comments
ids_to_drop_list = list(ids_to_drop)
df.drop(index=ids_to_drop_list, inplace=True)

num_dropped_comments = len(ids_to_drop_list)
num_remaining_comments = len(df)

print("Initially, there were {} comments!".format(num_initial_comments))
print("A total of {} comments were then dropped on account of their length or score!".format(num_dropped_comments))
print("This leaves us with {} comments!".format(num_remaining_comments))

df.to_csv('NewAttempt/all_comments_cleaned.csv', index=True)


HASH_LENGTH = 64
SEQUENCE_ID_LENGTH = 60
HASH = 'Native'
WINDOWS_SIZE = 3
MUTATION_THRESHOLD = 0

blast_hits_file_path = "/home/weenkus/workspace/Phylo-research/results_simHash/blast_comparison_new/" \
                       "ENSP1146/ENSP00000001146_hits_blast.txt"

hash_hits_file_path = "/home/weenkus/workspace/Phylo-research/results_simHash/blast_comparison_new/" \
                      "ENSP1146/ENSP1146_native_mapper10_hits.txt"

output_file_path = "ENSP1146_native_mapper10_hits_compare.txt"
csv_comparison = "ENSP1146_native_mapper10_hits_compare.csv"

blast_hits_file = open(blast_hits_file_path, "r")
hash_hits_file = open(hash_hits_file_path, "r")
output_file = open(output_file_path, "w")
csv_file = open(csv_comparison, "w")

blast_hits_list = blast_hits_file.read().split("\n")
hash_hits_list = hash_hits_file.read().split("\n")

porgess = 0
csv_string = 'window_size,hash,ham_distance,similarity,error,blast_output,bitscore,eValue,mutation_threshold'
print(csv_string, file=csv_file)

for blast_hit in blast_hits_list:

    porgess += 1
    print(porgess, len(blast_hits_list))

    for hash_hit in hash_hits_list:

        blast_string_chunks = blast_hit.split("\t")
        hash_string_chunks = hash_hit.split(" ")

        if blast_string_chunks[0] == hash_string_chunks[0] and blast_string_chunks[1] == hash_string_chunks[1]:

            sequence_ids = blast_string_chunks[0] + " " + blast_string_chunks[1]

            if len(sequence_ids) < SEQUENCE_ID_LENGTH:
                while len(sequence_ids) < SEQUENCE_ID_LENGTH:
                    sequence_ids += " "

            sim = str(float(HASH_LENGTH-int(hash_string_chunks[2]))/HASH_LENGTH*100)

            out_string = '{sequence_ids} {ham_distance:2} {similarity:9} {error:05.2f}  | {blast_output}'.format(
                sequence_ids=sequence_ids,
                ham_distance=hash_string_chunks[2],
                error=abs(float(sim)-float(blast_string_chunks[2])),
                similarity=sim,
                blast_output=' '.join(blast_string_chunks[2:])
            )

            print(out_string, file=output_file)

            csv_string = '{window_size},{hash},{ham_distance},{similarity},{error:2f},{blast_output},' \
                         '{bitmap},{eValue},{mutation_threshold}'.format(
                window_size=WINDOWS_SIZE,
                hash=HASH,
                sequence_ids=sequence_ids,
                ham_distance=hash_string_chunks[2],
                error=abs(float(sim)-float(blast_string_chunks[2])),
                similarity=sim,
                blast_output=blast_string_chunks[2],
                bitmap=blast_string_chunks[-1],
                eValue=blast_string_chunks[-2],
                mutation_threshold=MUTATION_THRESHOLD
            )

            print(csv_string, file=csv_file)

            hash_hits_list.remove(hash_hit)
            break


import matplotlib.pyplot as plt
import os
import pandas as pd

datafile = os.path.join(os.getcwd(),
                        '../../datasets/sonar.all-data')

rocks_vs_mines = pd.read_csv(datafile, header=None, prefix='V')

data_row2 = rocks_vs_mines.iloc[1, 0:60]     # Row 1 columns 0:60
data_row3 = rocks_vs_mines.iloc[2, 0:60]     # Row 2 columns 0:60

plt.scatter(data_row2, data_row3)

plt.xlabel('2nd attributes')
plt.ylabel('3rd attributes')
plt.show()

data_rows21 = rocks_vs_mines.iloc[20, 0:60]
plt.scatter(data_row2, data_rows21)

plt.xlabel('2nd attribute')
plt.ylabel('21st attribute')
plt.show()


from dao.customer_dao import CustomerDao

class CustomerService:

    def __init__(self):
        self.customer_dao = CustomerDao()

    def get_all_customers(self):
        customers = self.customer_dao.get_all_customers()
        dict = {"customers": []}
        for cust in customers:
            dict["customers"].append(cust.to_dict())
        return dict


# https://codeforces.com/contest/1840/problem/C

def check(seq, n, ts):
    crt, res = 0, 0
    for i in seq:
        if i <= ts:
            crt += 1
        else:
            crt = 0
        if crt >= n:
            res += crt + 1 - n
    return res

for _ in range(int(input())):
    _, n, ts = map(int, input().split())
    seq = tuple(map(int, input().split()))
    print(check(seq, n, ts))


"""С клавиатуры вводится два числа K и N. Квадратная матрица А(N,N), состоящая из 4-х равных по размерам подматриц,
B,C,D,E заполняется случайным образом целыми числами в интервале [-10,10].
Для тестирования использовать не случайное заполнение, а целенаправленное. Вид матрицы А:
 |B|C|
 |D|E|
Формируется матрица F следующим образом: скопировать в нее А и  если в С количество чисел, больших К в нечетных столбцах,
больше чем произведение чисел в нечетных строках, то поменять местами С и В симметрично, иначе С и Е поменять местами несимметрично.
При этом матрица А не меняется. После чего если определитель матрицы А больше суммы диагональных элементов матрицы F,
то вычисляется выражение: A*A^T – K * F^(-1), иначе вычисляется выражение (A^(-1) +G-F^Т)*K, где G-нижняя треугольная матрица, полученная из А.
Выводятся по мере формирования А, F и все матричные операции последовательно.
"""

import numpy as np
import random
import time

try:
    N = int(input("Введите длину матрицы (положительное, целое число, > 3): "))
    while N <= 3:
        N = int(input("Введите длину матрицы (положительное, целое число, > 3): "))
    K = int(input("Введите число K (целое число): "))
    start = time.monotonic()
    np.set_printoptions(linewidth=1000)
    # Создание и заполнение матрицы A:
    A = np.random.randint(-10.0, 10.0, (N, N))
    print("Матрица А:\n", A)
    # Создание подматриц:
    submatrix_length = N // 2                                                       # Длина подматрицы
    sub_matrix_C = np.array(A[:submatrix_length, submatrix_length+N % 2:N])
    sub_matrix_B = np.array(A[:submatrix_length, :submatrix_length])
    sub_matrix_E = np.array(A[submatrix_length+N % 2:N, submatrix_length+N % 2:N])
    # Создание матрицы F:
    F = A.copy()
    print("\nМатрица F: \n", F)
    # Обработка матрицы С:
    count_number_in_column = np.sum(sub_matrix_C[:, 0:submatrix_length:2] > K)
    multiplication_of_numbers = sub_matrix_C[1::2].prod()
    print("Сумма чисел стоящих в нечетных столбцах подматрицы С, которые больше К:", count_number_in_column)
    print("Произведение чисел стоящих в нечетных строках подматрицы С:", multiplication_of_numbers)
    # Формируем матрицу F:
    if count_number_in_column > multiplication_of_numbers:
        F[:submatrix_length, submatrix_length + N % 2:N] = sub_matrix_B[:submatrix_length, ::-1]
        F[:submatrix_length, :submatrix_length] = sub_matrix_C[:submatrix_length, ::-1]
    else:
        F[:submatrix_length, submatrix_length+N % 2:N] = sub_matrix_E
        F[submatrix_length+N % 2:N, submatrix_length+N % 2:N] = sub_matrix_C
    print("\nОтформатированная матрица F: \n", F)
    # Вычисляем выражение:
    try:
        if np.linalg.det(A) > sum(np.diagonal(F)):
            print("\n Результат выражения A*A^T - K*F^(-1): \n", A*A.transpose() - np.linalg.inv(F)*K)
        else:
            G = np.tri(N)*A
            print("\nРезультат выражения (A^(-1) +G-F^Т)*K:\n", (np.linalg.inv(A)+G-F.transpose())*K)
    except np.linalg.LinAlgError:
        print('Одна из матриц является вырожденной(определитель равен 0), поэтому обратную матрицу найти невозможно.')
    finish = time.monotonic()
    print("\nВремя работы программы:", finish - start, "sec.")
except ValueError:
    print("Введены неверные даннные")


"""
Post-processing the results for the electrostatic lens simulation beamline.
"""
from pathlib import Path

import matplotlib.pyplot as plt
from trajectories.post_processing import find_radial_pos_dist
from trajectories.utils import import_sim_result_from_hdf

def main():

    # Get the result from file
    filepath = Path("./saved_data/lens_simulation_beamline.hdf")
    run_name = "Electrostatic lens simulation 11/15/2021"
    result = import_sim_result_from_hdf(filepath, run_name)

    # Plot some trajectories
    result.plot()

    # Plot the radial position distribution of molecules about to enter the detection region
    z = result.beamline.find_element("DR aperture").z0
    rho = find_radial_pos_dist(result, z)

    # Plot a 2D histogram of the position distribution
    fig, ax = plt.subplots(figsize=(16, 9))
    ax.hist2d(rho[:, 0], rho[:, 1], bins=25)
    ax.set_xlabel("X-position / m")
    ax.set_ylabel("Y-position / m")
    plt.show()

    # Plot 1D histograms for each axis
    # X-position
    fig, ax = plt.subplots(figsize=(16, 9))
    ax.hist(rho[:, 0], bins=25)
    ax.set_xlabel("X-position / m")
    plt.show()

    # Y-position
    fig, ax = plt.subplots(figsize=(16, 9))
    ax.hist(rho[:, 1], bins=25)
    ax.set_xlabel("Y-position / m")
    plt.show()

if __name__ == "__main__":
    main()


import os
import pytest
from qubit.parsing import Parser
from qubit.parsing import XYZ

methane = os.path.join(os.path.dirname(__file__), 'test_data/methane.xyz')

@pytest.fixture
def parser():
    return Parser()

def test_default_load(parser):
    with pytest.raises(Exception) as e:
        parser.load()

def test_default_write(parser):
    with pytest.raises(Exception) as e:
        parser.write()

@pytest.fixture
def xyz_parser():
    return XYZ()

def test_XYZ_load(xyz_parser):
    atoms = ['C', 'H', 'H', 'H', 'H']
    xyz = [
        [0.000000, 0.000000, 0.000000],
        [0.000000, 0.000000, 1.089000],
        [1.026719, 0.000000, -0.363000],
        [-0.513360, -0.889165, -0.363000],
        [-0.513360, 0.889165, -0.363000]]

    p_atoms, p_xyz = xyz_parser.load(methane)

    assert (p_atoms, p_xyz) == (atoms, xyz)


import os
import datetime
from collections import Counter

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm

import torch as th
from torch import nn as nn
from torch.nn import functional as F

def mk_dir(data, model):

    today = datetime.date.today()
    date = today.strftime("%y%m%d")
    path = 'result/' + date + '_' + data + '_' + model + '_'

    i = 0
    while 1:
        if os.path.isdir(path + str(i)):
            i += 1
            continue

        path = path + str(i)
        os.mkdir(path)

        return path

def get_non_event_mask(event_target, graph):

    bs = event_target.size(0)
    n_class = len(graph.nodes())
    e_mask = th.zeros([bs, n_class])

    for i in range(e_mask.shape[1]):
        e_mask[:, i] += (event_target.detach().cpu().numpy() == i)

    with th.no_grad():

        n_sample = 10
        for i in range(bs):
            dest = th.LongTensor(np.random.choice(graph.num_nodes(), n_sample, replace=False))
            e_mask[i, dest] += 1

        # bs*n_class, 1
        # for i in range(bs):
        #     src, dest = graph.out_edges(event_target[i])
        #
        #     e_mask[i, dest] += 1.

        e_mask = e_mask.gt(0).float()
    return e_mask.unsqueeze(2)

def log_lmbda(time_logits, target_time, w, b, mask):

    # lmbda = nn.Softplus()(time_logits[:, :, 0] + time_logits[:, :, 1] * target_time + b)
    lmbda = nn.Softplus()(time_logits[:, :, 0] + w * target_time + b)
    # lmbda = nn.Softplus()(time_logits[:, :, 0] + b)
    log_lmbda_all = th.log(lmbda + 1e-6)

    log_lmbda = th.sum(log_lmbda_all * mask, dim=1)

    return log_lmbda

def integral_lmbda_exact(time_logits, target_time, w, b, mask, n_samples=20, device='cuda'):

    bs, n_class, _ = time_logits.size()

    temp_time = target_time.unsqueeze(2) * th.rand([*target_time.size(), n_samples], device=device)
    # lmbda_all = nn.Softplus()((time_logits[:, :, 0]).unsqueeze(2)
    #                           + (time_logits[:, :, 1]).unsqueeze(2)  * temp_time + b) * mask

    # lmbda_all = nn.Softplus()((time_logits[:, :, 0]).unsqueeze(2)
    #                           + w * temp_time + b) / th.sum(mask, 1).unsqueeze(1)

    lmbda_all = nn.Softplus()((time_logits[:, :, 0]).unsqueeze(2)
                              + w * temp_time + b)

    # lmbda_all = nn.Softplus()((time_logits[:, :, 0]).unsqueeze(2) + w * temp_time + b) / th.sum(mask, 1).unsqueeze(1) * mask

    int_lmbda_all = th.sum(lmbda_all, dim=2) / n_samples * target_time
    int_lmbda = th.sum(int_lmbda_all, dim=-1)

    return int_lmbda

def integral_lmbda(time_logits, target_time, b):

    # lmbda_all = nn.Softplus()(
    #     (time_logits[:, :, 0]) + (time_logits[:, :, 1]) * target_time + b)
    # lmbda0_all = nn.Softplus()((time_logits[:, :, 0]) + b)
    # int_lmbda_all = (lmbda_all + lmbda0_all) / 2 * target_time
    # int_lmbda = th.sum(int_lmbda_all, dim=-1)

    lmbda_all = nn.Softplus()(
        (time_logits[:, 0]) + (time_logits[:, 1]) * target_time.squeeze() + b)
    lmbda0_all = nn.Softplus()((time_logits[:, 0]) + b)
    int_lmbda_all = (lmbda_all + lmbda0_all) / 2 * target_time.squeeze()
    int_lmbda = th.sum(int_lmbda_all, dim=-1)

    return int_lmbda


import sys
import re

class Passport:
    required_fields = {'byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid', 'cid'}
    optional_fields = {'cid'}

    def __init__(self, entry):
        fields = re.split(r'\s+',  entry.strip())
        for field in fields:
            sp = field.split(':')
            setattr(self, sp[0], sp[1])

    def is_valid(self):
        return len(self.required_fields - self.optional_fields - set(self.__dict__.keys())) == 0

    ### Part 2 ###
    # Now we gotta do field validation
    def is_valid2(self):
        if not self.is_valid():
            return False

        try:
            # byr: int, 1920 <= byr <= 2002
            self.byr = int(self.byr)
            if not(1920 <= self.byr and self.byr <= 2002):
                return False
            # iyr: int, 2010 <= iyr <= 2020
            self.iyr = int(self.iyr)
            if not(2010 <= self.iyr and self.iyr <= 2020):
                return False
            # eyr: int, 2020 <= eyr <= 2030
            self.eyr = int(self.eyr)
            if not(2020 <= self.eyr and self.eyr <= 2030):
                return False
            # hgt: number followed by unit string
            #  - if cm: 150 <= number <= 193
            #  - if in: 59 <= number <= 76
            num = int(self.hgt[:-2])
            unit = self.hgt[-2:]
            if unit == 'cm':
                if not(150 <= num and num <= 193):
                    return False
            elif unit == 'in':
                if not(50 <= num and num <= 76):
                    return False
            else:
                return False
            # hcl: `#` followed by 6 hex digits
            if re.match(r'^#[0-9a-f]{6}$', self.hcl) is None:
                return False
            # ecl: one of `[amb, blu, brn, gry, grn, hzl, oth]`
            if self.ecl not in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']:
                return False
            # pid: 9 digit number, including leading zeros
            if re.match(r'^\d{9}$', self.pid) is None:
                return False
            # cid: ignored
        except:
            # If we errored, it was probably integer formatting
            return False
        return True

batch = list(map(Passport, ''.join(open(sys.argv[1], 'r').readlines()).split('\n\n')))

### Part 1 ###
# Get the number of valid passports in the batch
# Passports are in the form of `key:value`, with each field separated by
# whitespace and passport entries are separated by empty lines. A valid
# passport must have all required_fields, though it may be missing
# optional_fields

print(f'Part 1: {sum(map(lambda p: p.is_valid(), batch))}')
print(f'Part 2: {sum(map(lambda p: p.is_valid2(), batch))}')


import torch
from transformers import AutoModel, AutoTokenizer

MODEL_PATH = "app/data/model/model.bin"
TRAINING_FILE = "app/data/imdb.csv"

MAX_LEN = 512
TRAIN_BATCH_SIZE = 4
VALID_BATCH_SIZE = 2
EPOCHS = 10
MODEL_NAME = "bert-base-uncased"

MODEL = AutoModel.from_pretrained(MODEL_NAME)
TOKENIZER = AutoTokenizer.from_pretrained(MODEL_NAME)

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
EARLY_STOPPING = False