"""
Normalization functions such as batch norm
"""

from typing import Optional, Sequence, Union, Tuple
from .. import nn

def moments(x: nn.Tensor, axis: Union[nn.Dim, Sequence[nn.Dim]]) -> Tuple[nn.Tensor, nn.Tensor]:
    """
    :param x: input
    :param axis: the axis to be reduced, to calculate statistics over
    :return: mean, variance. it has the same shape as the input with the axis removed
    """
    mean = nn.reduce(x, mode="mean", axis=axis, name="mean")
    # stop_gradient does not change the gradient here
    variance = nn.reduce(nn.squared_difference(x, nn.stop_gradient(mean)), mode="mean", axis=axis, name="variance")
    return mean, variance

class LayerNorm(nn.Module):
    """
    `Layer normalization <https://arxiv.org/abs/1607.06450>`__.

    Note that we *just* normalize over the feature-dim axis here.
    This is consistent to the default behavior of :class:`tf.keras.layers.LayerNormalization`
    and also how it is commonly used in many models, including Transformer.

    However, there are cases where it would be common to normalize over all axes except batch-dim,
    or all axes except batch and time.
    For a more generic variant, see :func:`norm`.
    """

    def __init__(self, in_dim: Union[nn.Dim, Sequence[nn.Dim]], *, eps: float = 1e-6):
        super().__init__()
        self.in_dim = in_dim
        self.eps = eps
        self.scale = nn.Parameter([self.in_dim] if isinstance(self.in_dim, nn.Dim) else self.in_dim)
        self.scale.initial = 1.0
        self.bias = nn.Parameter(self.scale.dims)
        self.bias.initial = 0.0

    def __call__(self, x: nn.Tensor) -> nn.Tensor:
        mean = nn.reduce(x, axis=self.in_dim, mode="mean")
        variance = nn.reduce(nn.squared_difference(x, mean), axis=self.in_dim, mode="mean", name="variance")
        norm_x = (x - mean) * nn.rsqrt(variance + self.eps)
        return norm_x * self.scale + self.bias

class BatchNorm(nn.Module):
    """
    Batch normalization. https://arxiv.org/abs/1502.03167

    Note that the default arguments differ from corresponding batch norm in RETURNN.
    See here for discussion on defaults: https://github.com/rwth-i6/returnn/issues/522

    We calculate statistics over all axes except the given in_dim.
    I.e. all other axes are reduced for the statistics.

    To compensate the normalization, there are learnable parameters gamma and beta
    (optional, used when option `affine` is True).

    The usual behavior depends on whether this is used in training or evaluation,
    although this often configurable in other frameworks.
    The usual behavior, in training::

        # Using statistics from current batch.
        mean_cur_batch, variance_cur_batch = moments(source, reduce_dims)
        y = (x - mean_cur_batch) / sqrt(variance_cur_batch + epsilon)
        y = gamma * y + beta

        # Updating running statistics for later use.
        mean = (1 - momentum) * mean + momentum * mean_cur_batch
        variance = (1 - momentum) * variance + momentum * variance_cur_batch

    The usual behavior, not in training (i.e. in evaluation)::

        # Using collected statistics. Not using statistics from current batch.
        y = (x - mean) / sqrt(variance + epsilon)
        y = gamma * y + beta

    """

    def __init__(
        self,
        in_dim: nn.Dim,
        *,
        affine: bool = True,
        momentum: float = 0.1,
        epsilon: float = 1e-3,
        use_mask: Optional[bool] = None,
    ):
        """
        :param in_dim: the feature dimension of the input
        :param affine: whether to use learnable parameters gamma and beta
        :param momentum: momentum for the running mean and variance
        :param epsilon: epsilon for the variance
        :param use_mask: whether to use a mask for dynamic spatial dims.
          This must be specified if the input has dynamic spatial dims.
          True would use the correct masking then. However, that is inconsistent to all other frameworks
            which ignore the masking, and also slower, and the fused op would not be used.
          False would be consistent to all other frameworks,
            and potentially allows for the use of an efficient fused op internally.
        """
        super().__init__()
        assert isinstance(in_dim, nn.Dim)
        self.in_dim = in_dim
        self.use_mask = use_mask
        self.momentum = momentum
        self.epsilon = epsilon
        self.running_mean = nn.Parameter([in_dim], auxiliary=True)
        self.running_mean.initial = 0.0
        self.running_variance = nn.Parameter([in_dim], auxiliary=True)
        self.running_variance.initial = 1.0
        self.affine = affine
        self.gamma = None  # type: Optional[nn.Parameter]
        self.beta = None  # type: Optional[nn.Parameter]
        if self.affine:
            self.gamma = nn.Parameter([in_dim])
            self.gamma.initial = 1.0
            self.beta = nn.Parameter([in_dim])
            self.beta.initial = 0.0

    def __call__(self, source: nn.Tensor) -> nn.Tensor:
        # We wrap the RETURNN layer because we want efficient handling if possible,
        # which is potentially the use of a fused op,
        # and maybe reordering of dims.
        # https://github.com/rwth-i6/returnn_common/issues/89
        spatial_dims = source.dims_set - {nn.batch_dim, self.in_dim}
        assert len(spatial_dims) == len(source.dims_set) - 2
        if any(d.dimension is None for d in spatial_dims):  # any dynamic spatial dim
            if self.use_mask is None:
                raise ValueError(
                    f"{self}: use_mask must be specified if the input {source} has any dynamic spatial dims"
                )
            use_mask = self.use_mask
        else:
            use_mask = False  # not needed. False because this potentially enables an efficient fused op.
        reuse_params = {
            "batch_norm/v2_mean": self.running_mean,
            "batch_norm/v2_variance": self.running_variance,
        }
        if self.affine:
            reuse_params["batch_norm/v2_gamma"] = self.gamma
            reuse_params["batch_norm/v2_beta"] = self.beta
        reuse_params = {"map": {k: {"layer_output": v} for k, v in reuse_params.items()}}
        return nn.make_layer(
            {
                "class": "batch_norm",
                "from": source,
                "in_dim": self.in_dim,
                "use_std": self.affine,
                "use_shift": self.affine,
                "param_version": 2,
                "reuse_params": reuse_params,
                "momentum": self.momentum,
                "epsilon": self.epsilon,
                "masked_time": use_mask,
            },
            name="batch_norm",
        )

def normalize(a: nn.Tensor, *, axis: Union[nn.Dim, Sequence[nn.Dim]], epsilon: float = 1e-6) -> nn.Tensor:
    """
    Mean- and variance-normalize some input in the given input dimension(s),
    such that the resulting tensor has mean 0 and variance 1.

    If you want that this can be shifted and scaled again,
    you need additional parameters, cf. :class:`Normalize`.

    :param a: input
    :param axis: axis over which the mean and variance are computed
    :param epsilon: epsilon for numerical stability
    :return: (a - mean) / sqrt(variance + epsilon)
    """

    mean, variance = nn.moments(a, axis=axis)
    return (a - mean) * nn.rsqrt(variance + epsilon)

class Normalize(nn.Module):
    """
    :func:`normalize` with additional scale and bias
    """

    def __init__(
        self,
        *,
        param_shape: Union[nn.Dim, Sequence[nn.Dim]],
        epsilon: float = 1e-6,
        scale: bool = True,
        bias: bool = True,
    ):
        """
        :param param_shape: shape of the scale and bias parameters
        :param epsilon: epsilon for numerical stability
        :param scale: whether to include a trainable scale
        :param bias: whether to include a trainable bias
        """
        super(Normalize, self).__init__()
        self.epsilon = epsilon
        if isinstance(param_shape, nn.Dim):
            param_shape = [param_shape]
        self.scale = None
        if scale:
            self.scale = nn.Parameter(shape=param_shape)
            self.scale.initial = 1.0
        self.bias = nn.Parameter(shape=param_shape) if bias else None

    def __call__(self, a: nn.Tensor, *, axis: Union[nn.Dim, Sequence[nn.Dim]]):
        norm = normalize(a, axis=axis, epsilon=self.epsilon)
        if self.scale is not None:
            norm = self.scale * norm
        if self.bias is not None:
            norm = norm + self.bias
        return norm


"""Common utility functions for emotion detection."""
import cv2
import numpy as np

class_indices = {0: 'angry',
                 1: 'confused',
                 2: 'crosseyed',
                 3: 'happy',
                 4: 'neutral',
                 5: 'sad'}

def preprocess_image(image):
    """Scale, reshape etc."""
    image = cv2.resize(image, (480, 360))
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = image / 255.
    image = np.expand_dims(image, axis=0)
    return image

def overlay_text(img, emotion, state, recording, bottomLeftCornerOfText,
                 font, fontScale, fontColor, lineType, row_dy):
    """Overlay status text on image."""
    if state == 'recording' and recording:
        img = cv2.circle(img, (30, 30), 10, (0, 0, 255), -1)
    elif state == 'recording' and not recording:
        img = cv2.circle(img, (30, 30), 10, (255, 255, 0), -1)

    x = bottomLeftCornerOfText[0]
    y = bottomLeftCornerOfText[1]
    rows = emotion.split('\n')
    y_pos = [y - i*row_dy for i in range(len(rows))]
    y_pos.reverse()

    for i, line in enumerate(rows):
        y = y_pos[i]
        textpos = (x, y)
        cv2.putText(
            img,
            line,
            textpos,
            font,
            fontScale,
            fontColor,
            lineType
        )
    return img

def get_top_classes(network_output, n=2):
    """Convert array to n human readable classes and scores."""
    idx = (-network_output[0, :]).argsort()[:n]
    scores = []
    for i in range(n):
        score = network_output[0, idx[i]]
        emotion = class_indices[idx[i]]
        scores.append({'emotion': emotion, 'score': score})
    # emo1 = str(round(score1*100, 2)) + '% ' + class_indices[idx[0]]
    # score2 = network_output[0, idx[1]]
    # emo2 = str(round(score2*100, 2)) + '% ' + class_indices[idx[1]]
    return scores


'''
Created on May 20, 2018

@author: hlv_trinh
'''

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from googleapiclient.http import MediaFileUpload
import resource

ROOT_BACKUP_FOLDER = "server-backup"
MAX_RETRY = 10
CHUNK_SIZE = 5 * 1024 * 1024 #5MB

MAX_MEMORY = 256 * 1024 * 1024

soft, hard = resource.getrlimit(resource.RLIMIT_AS)
resource.setrlimit(resource.RLIMIT_AS, (MAX_MEMORY, hard))
print("Current limit: %s, %s | New: %s" %(soft, hard, MAX_MEMORY))

def FindAndCreateIfNotExist(service, parentFolderId, folderName):
    if parentFolderId == None:
        query = ("name='%s'" % folderName)
    else:
        query = ("name='%s' and '%s' in parents" % (folderName, parentFolderId))

    response = service.files().list(
        q=query,
        spaces='drive',
        fields='nextPageToken, files(id, name)',
        pageToken=None).execute()
    backupDir = response.get('files', [])
    if len(backupDir) > 0:
        backupFile = backupDir[0]
    else:
        print("Not found root folder, trying to create: %s" % folderName)
        if parentFolderId == None:
            body = {'name':folderName,
                    'mimeType':'application/vnd.google-apps.folder'}
        else:
            body = {'name':folderName,
                    'mimeType':'application/vnd.google-apps.folder',
                    'parents': [parentFolderId]}
        backupFile = service.files().create(
            body=body,
            fields='id, name').execute()
    if (backupFile.get('id') != ''):
        print('Backup Root Folder: %s (%s)' % (backupFile.get('name'), backupFile.get('id')))

    return backupFile

def uploadBackupFile(service, parentFolderId, filePath):
    print(filePath)
    import os.path
    fileSize = os.path.getsize(filePath)
    if fileSize <= CHUNK_SIZE:
        media_body = MediaFileUpload(filePath, mimetype='application/octet-stream', resumable=True)
    else:
        media_body = MediaFileUpload(filePath, mimetype='application/octet-stream', chunksize=CHUNK_SIZE, resumable=True)
    fileName = os.path.basename(filePath)
    body = {
        'name': fileName,
        'description': fileName,
        'mimeType': 'application/octet-stream',
        'parents': [parentFolderId]
    }

    retries = 0
    request = service.files().create(body=body, media_body=media_body)
    response = None
    from apiclient import errors
    success = True
    while response is None:
        try:
            status, response = request.next_chunk()
            if status:
                print("Uploaded %.2f%%" % (status.progress() * 100))
                retries = 0
        except errors.HttpError as e:
                if e.resp.status == 404:
                    print("Error 404! Aborting.")
                    success = False
                    break
                else:
                    if retries > MAX_RETRY:
                        print("Retries limit exceeded! Aborting.")
                        success = False
                        break
                    else:
                        retries += 1
                        import time
                        time.sleep(2**retries)
                        print("Error (%d)... retrying." % e.resp.status)
                        continue
    if success:
        print("Upload Complete!")
        os.remove(filePath)

def main():
    """
    Shows basic usage of the Drive v3 API.

    Creates a Drive v3 API service and prints the names and ids of the last 10 files
    the user has access to.
    """

    import sys
    if len(sys.argv) < 1:
        print('Invalid input folder')
        exit(1)

    # Setup the Drive v3 API
    SCOPES = 'https://www.googleapis.com/auth/drive.file'
    store = file.Storage('credentials.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
        creds = tools.run_flow(flow, store)

    http = Http()
    http.redirect_codes = http.redirect_codes - {308}
    service = build('drive', 'v3', http=creds.authorize(http))

    # Find the backup root folder or else, create it
    backupFile = FindAndCreateIfNotExist(service, None, ROOT_BACKUP_FOLDER)

    import socket
    hostname = socket.gethostname()
    if hostname.strip() == "":
        hostname = "Unknown"

    backupFile = FindAndCreateIfNotExist(service, backupFile.get('id'), hostname)

    import time
    timestr = time.strftime("%Y%m%d-%H%M%S")
    backupFile = FindAndCreateIfNotExist(service, backupFile.get('id'), timestr)

    parentFolderId = backupFile.get('id')
    filePath = sys.argv[1]

    import os
    if os.path.isfile(filePath):
        uploadBackupFile(service, parentFolderId, filePath)
    else:
        for f in os.listdir(filePath):
            uploadBackupFile(service, parentFolderId, os.path.join(filePath,f))

    #service.close()

if __name__ == '__main__':
    main()


"Watch the image processing container, once it is done move the data back to swift storage"

from kubernetes import client, config, watch
from put_data import put_data_back
import os

# Keep track of PR https://github.com/kubernetes-client/python-base/pull/36
# it is implementing watching forever and reconnecting if the connection is lost
# If the PR doesn't get in soon enough, need to figure out how to do that here

def main():
    kubecfg_path = os.environ.get('KUBECFG_PATH')
    if kubecfg_path is None:
        config.load_kube_config()
    else:
        config.load_kube_config(config_file=kubecfg_path)
    v1_client = client.CoreV1Api()
    resrc_version = None
    namespace = os.getenv("OPENSHIFTMGR_PROJECT", "myproject")
    w = watch.Watch()

    while True:
        if resrc_version is None:
            stream = w.stream(v1_client.list_namespaced_pod, namespace)
        else:
            stream = w.stream(v1_client.list_namespaced_pod, namespace, resource_version=resrc_version)
        for event in stream:
            resrc_version = event['raw_object']['metadata']['resourceVersion']
            if 'containerStatuses' in event['raw_object']['status']:
                for status in event['raw_object']['status']['containerStatuses']:
                    if status['name'] != 'publish' and 'terminated' in status['state']:
                        w.stop()
                        # It is possible for the image processing container to error out
                        # when that happens, the reason will be "Error" and this will return false
                        return status['state']['terminated']['reason'] == 'Completed'

if __name__ == '__main__':
    if main():
        put_data_back()
    else:
        print("Image processing container failed to complete")


#!/usr/bin/python3
import polib
import re
import sys

module     = sys.argv[1]
lang       = sys.argv[2]
msgextract = sys.argv[3]
pofile     = polib.pofile(sys.argv[4])
cpp        = open(sys.argv[5],'w')

print('// Generated by po-cpp-gen.py', file = cpp)
print('#include "arcollect-i18n-'+module+'.hpp"', file = cpp)
print('void Arcollect::i18n::'+module+'::po_apply_'+lang+'(void) noexcept {', file = cpp)

# Filter-out untranslated entries in non C locale
if msgextract != 'msgid':
	pofile = filter(polib.POEntry.translated,pofile)

for msg in pofile:
	print('\t'+msg.msgctxt,'=','"'+msg.__getattribute__(msgextract).replace('\\','\\\\').replace('\n','\\n').replace('"','\\"')+'";', file = cpp)
print('}', file = cpp)


import webbrowser
import cv2
import pyautogui as pag
import tensorflow.keras
from PIL import Image, ImageOps
import numpy as np
np.set_printoptions(suppress=True)
# pre loading Model
model = tensorflow.keras.models.load_model('D:\Device-Control-Using-OpenCV-and-ML\Device-Control-Using-OpenCV-and-ML\opencv\model\keras_model.h5')
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
pred=[]
def image_prediction(imageOpenCv):
    # Disable scientific notation for clarity
    #np.set_printoptions(suppress=True)
    #nonlocal model
    # Load the model
    #model = tensorflow.keras.models.load_model('D:\Device-Control-Using-OpenCV-and-ML\Device-Control-Using-OpenCV-and-ML\opencv\model\keras_model.h5')

    # Create the array of the right shape to feed into the keras model
    # The 'length' or number of images you can put into the array is
    # determined by the first position in the shape tuple, in this case 1.
    # data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)

    # Replace this with the path to your image
    image = Image.open('D:\Device-Control-Using-OpenCV-and-ML\Device-Control-Using-OpenCV-and-ML\obc.jpg ')

    # resize the image to a 224x224 with the same strategy as in TM2:
    # resizing the image to be at least 224x224 and then cropping from the center
    size = (224, 224)
    image = ImageOps.fit(image, size, Image.ANTIALIAS)

    # turn the image into a numpy array
    image_array = np.asarray(image)
    # display the resized image
    #image.show()
    # Normalize the image
    normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1

    # Load the image into the array
    data[0] = normalized_image_array

    # run the inference
    prediction = model.predict(data)
    print("output")
    print(prediction)
    dito = {0: "5", 1: "2", 2: "3", 3: "4", 4: "1", 5: "fist"}
    l = -1
    indexfind = max(prediction[0])
    for i in  prediction[0]:
        l=l+1
        if indexfind == i:
            break
    pred.append(dito[l])
    print("output is :=> ", dito[l])
c=10
f=0
img=cv2.VideoCapture(0)
hand_roi=-1
hand_xml=cv2.CascadeClassifier('D:/Device-Control-Using-OpenCV-and-ML/Device-Control-Using-OpenCV-and-ML/opencv/full_hand_gesture.xml')
while True:
    rt,frame=img.read()
    cv2.imwrite("obc.jpg",frame)
    image_prediction("obc.jpg")
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    palm = hand_xml.detectMultiScale(gray, 1.1, 4)
    #frame=cv2.cvtColor(frame,cv2.COLOR_RGBA2GRAY) #cv2.COLOR_RGB2BGR working well
    for (x,y,w,h) in palm:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        roi_gray=gray[y:y+h,x:x+w]
        roi_color=frame[y:y+w,x:x+w]
        hand_palm= hand_xml.detectMultiScale(roi_gray)
        if len(hand_palm)==0:
            #c=3
            print("No hand detected")
        else:
            for (ex,ey,ew,eh) in hand_palm:
                hand_roi=roi_color[ey:ey+eh,ex:ex+ew]# gray,frame
                # #c=3
                # #if c==5:
                #     #pag.hotkey('win','1') # pyautogui test
                #  #   print("chrome command executed")
                #    # chromedir= 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
                #     #webbrowser.get(chromedir).open("https://github.com/DR-NEGATIVE")
                # if c%3==0:
                #     #c=10
                #     c=1
                #     print("Second command execution")
                #     #pag.hotkey('win','down','down','down','down')
                #     #pag.press('right')  # for ppt
                #     #pag.press('space')

    cv2.imshow("Gesture Detection ",frame)# gray,frame
    cv2.imshow("crop_part",hand_roi)
    if cv2.waitKey(25) & 0xFF == ord('q') or len(pred)>6:

        break

#img.realese()
cv2.destroyAllWindows()
one=0
two=0
five=0
for i in pred:
    if i==1:
        one+=1
    elif i==2:
        two+=1
    else:
        five+=1
if one>two and one>five:
    from opencv.Aircanvas import paintcv
    paintcv.Aircanva()
elif two>one and two>five:
    pass
else:
    from opencv.BrowserOption import controller
    controller.browser_control()


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import datasets
from myConvexHull.myfunctions import myConvexHull

# visualisasi data iris
data = datasets.load_iris()
# buat DataFrame
df = pd.DataFrame(data.data, columns=data.feature_names)
df['Target'] = pd.DataFrame(data.target)

# visualisasi Petal Width vs Petal Length
plt.figure(figsize=(10, 6))
colors = ['b', 'r', 'g']
plt.title('Petal Width vs Petal Length')
plt.xlabel(data.feature_names[2])
plt.ylabel(data.feature_names[3])

for i in range(len(data.target_names)):
    bucket = df[df['Target'] == i]
    bucket = bucket.iloc[:, [2, 3]].values
    hull = myConvexHull(bucket)
    plt.scatter(bucket[:, 0], bucket[:, 1], label=data.target_names[i])
    for simplex in hull:
        plt.plot(bucket[simplex, 0], bucket[simplex, 1], colors[i])
        plt.legend()

plt.show()

# visualisasi Sepal Width vs Sepal Length
plt.figure(figsize=(10, 6))
colors = ['b', 'r', 'g']
plt.title('Sepal Width vs Sepal Length')
plt.xlabel(data.feature_names[0])
plt.ylabel(data.feature_names[1])

for i in range(len(data.target_names)):
    bucket = df[df['Target'] == i]
    bucket = bucket.iloc[:, [0, 1]].values
    hull = myConvexHull(bucket)
    plt.scatter(bucket[:, 0], bucket[:, 1], label=data.target_names[i])
    for simplex in hull:
        plt.plot(bucket[simplex, 0], bucket[simplex, 1], colors[i])
        plt.legend()

plt.show()


"""
@leofansq
https://github.com/leofansq
"""
import cv2
import numpy as np

from basic_function import dilate

def thinning(img, K):
    """
    细化运算实体
    Parameters:
        img: 待细化图像
        K: 结构子序列
    Return:
        细化后结果图像
    """
    # 归一
    img_result = img/255
    # 初始化用于保存上一次结果的矩阵
    img_old = 1 - img_result

    # 循环细化.直至图像保持不变
    while np.sum(img_result-img_old):
        img_old = img_result
        for i in K:
            # 基于卷积结果的击中击不中
            img_temp = np.where(cv2.filter2D(img_result.copy(),-1,i,borderType=0)==15, 1, 0)
            img_result = img_result - img_temp

    img_result *= 255
    return img_result.astype(np.uint8)

def sk_thin(img):
    """
    细化提取骨架
    Parameter:
        img: 待提取图像
    Return:
        提取骨架结果图像
    """
    # 生成8个结构子序列
    k_1 = np.array([[16,16,16],[0,1,0],[2,4,8]], dtype=np.uint8)
    k_2 = np.array([[0,16,16],[1,2,16],[4,8,0]], dtype=np.uint8)
    k_3 = np.array([[1,0,16],[2,4,16],[8,0,16]], dtype=np.uint8)
    k_4 = np.array([[1,2,0],[4,8,16],[0,16,16]], dtype=np.uint8)
    k_5 = np.array([[1,2,4],[0,8,0],[16,16,16]], dtype=np.uint8)
    k_6 = np.array([[0,1,2],[16,4,8],[16,16,0]], dtype=np.uint8)
    k_7 = np.array([[16,0,1],[16,2,4],[16,0,8]], dtype=np.uint8)
    k_8 = np.array([[16,16,0],[16,1,2],[0,4,8]], dtype=np.uint8)

    K = [k_1, k_2, k_3, k_4, k_5, k_6, k_7, k_8]

    # 细化操作
    img_result = thinning(img, K)

    return img_result

if __name__ == "__main__":
    # 测试用图
    # img = 255 - np.zeros((5, 11))
    # img[1:5,9] = img[1:5,10] = img[4,3:5] = 0

    img = cv2.imread("bin.png", 0)
    img_result = sk_thin(img)
    cv2.imwrite("morph_thin.png", img_result)


from datetime import date
data = date.today().year

total_maiores = 0
total_menores = 0

for pessoa in range(1, 8):
    nascimento = int(input(f'Insira o ano de nascimento da {pessoa}ª Pessoa: '))
    idade = (data - nascimento)
    if idade >= 18:
        total_maiores += 1
        # print('GRUPO +18 ANOS')
    elif nascimento > data:
        print('Insira uma data válida')
        continue
    else:
        total_menores += 1
        # print('GRUPO MENOR')
print(f'TOTAL DE MAIORES (+18) FOI: {total_maiores}')
print(f'TOTAL DE MENORES (-18) FOI: {total_menores}')


# tools
import tarfile
import os
import re
import json
from read import read

class Unlock:
    def __init__(self, path="../media/media", file="submissions.tgz"):
        self.path = path
        self.file = file
        self.extract()

    def extract(self):
        tar = tarfile.open(
            self.file,
        )
        tar.extractall(self.path)
        tar.close()

    def read_paths(self):
        """find files path with .test extension"""
        files = []
        # r=root, d=directories, f = files
        for r, d, f in os.walk(self.path):
            for file in f:
                if ".test" in file:
                    filePath = os.path.join(r, file)
                    files.append(filePath)
        return files

    def read_inputs(self, dir_list):
        """read and save info from .test files into list"""
        inputs = []
        for path in dir_list:
            inputs.append(read(path))
        return inputs

    def extract_info(self, files):
        """extract info like: _id, courseid, graderesult,
        submitted_on, taskid, username and source code from
        .test files located in local folder"""
        info = []
        errors = []
        # cont = 1
        for file in files:
            student = []

            try:
                # find _id
                data = re.search("_id:.*", file)
                data = data.group().split(": ")
                student.append(data)

                # find course id
                data = re.search("courseid: .*", file)
                data = data.group().split(": ")
                student.append(data)

                # find grade
                data = re.search("grade: .*", file)
                data = data.group().split(": ")
                student.append(data)

                # find result
                data = re.search("^result: .*", file, re.MULTILINE)
                data = data.group().split(": ")
                student.append(data)

                # find date
                data = re.search("submitted_on: .*:[0-9]{2}", file)
                data = data.group().split(": ")
                student.append(data)

                # find task id
                data = re.search("^taskid: .*", file, re.MULTILINE)
                data = data.group().split(": ")
                student.append(data)

                # find username
                data = re.search("username:.*\n-.*", file)
                data = data.group().split(":\n- ")
                student.append(data)

                # find source code
                data = extract_code(file)
                # cont += 1
                student.append(data)

                info.append(student)
            except Exception as e:
                print(student)
                errors.append(student)
                continue

        print("Errors: " + str(len(errors)))
        return info

    def write_json(self, data, name, path="/media/files/"):
        try:
            # self.cont = 1
            file = []
            for i in range(len(data)):
                try:
                    file.append(dict(data[i]))
                except:
                    print("WRITE_JSON EXCEPT: " + str(data[i]))
            file_path = f"{path}/{name}.json"
            with open(file_path, "w", encoding="utf-8") as F:
                # Using json dumps method to write the list in the disk
                F.write(json.dumps(file, ensure_ascii=False, indent=4))
            return file  # , file_path
        except Exception as e:
            print(f"Something went wrong: {e}")

# ---------------internal function--------------#

def extract_code(file):
    """extract source code from .test files"""
    try:
        data = re.search("input:(.*\n)*problems: \{\}", file)
        data = data.group().split(":\n    '")
        test = re.search(": \|.?.?.?\n(.*\n)*.*/input:", data[1])
        test = test.group()
        data[1] = test[4:-18]
    except Exception as e:
        data[0] = "input"
        data[1] = f"Error: {type(e).__name__}"
        return data
    return data


from enum import Enum
import typer
from typing_extensions import Annotated
import platform
if platform.system() == "Windows":
    import keyboard
else:
    from winhotkey.dummy_keyboard import keyboard # For testing on non-windows systems
from uvicorn.config import Config
from time import sleep
import logging

from winhotkey.__about__ import __version__
from winhotkey.ThreadedUvicorn import ThreadedUvicorn
from winhotkey.keyboard_wrappers import sender

cli_app = typer.Typer()

@cli_app.command()
def cli(hotkey_prefix: Annotated[str, typer.Option(help="'keyboard' compatible hot key prefix'")] = "shift+ctrl+alt",
        reverse_hotkey_order: Annotated[bool, typer.Option(help="Start with 1 and go to 0 or 0 and go to 1.  I actually prefer 0 to 1")] = True,
        hide_input: Annotated[bool, typer.Option(help="In case your 'phrases' are sensitive, you will not see what you enter")] = True):
    """
    Allows you store up to 10 'phrases' for instant recall by typing 'Shift+Ctrl+Alt[1-0]'\n
    Just hit enter to stop storing 'phrases'\n
    """
    possible_entries = [f"{i}" for i in range(1,10)]
    possible_entries.append("0")
    if reverse_hotkey_order:
        possible_entries = possible_entries[::-1]
    keys_to_add = {}
    for entry in possible_entries:
        key = f'{hotkey_prefix}+{entry}'
        phrase = typer.prompt(f"Please enter phrase for '{key}'", hide_input=hide_input, default="Press Enter when done")
        if phrase == "Press Enter when done":
            break
        keys_to_add[key] = phrase

    for (n, (key_to_add, phrase)) in enumerate(keys_to_add.items()):
        # Convert the phrase to string of characters separated by comma
        # keyboard.add_hotkey(key_to_add, keyboard.write, args=(phrase,))
        keyboard.add_hotkey(key_to_add, sender, args=(phrase,1.0,))

    print("Press SHIFT+CTRL+ALT+ESC to exit.  Leave this window open!")

    # Blocks until you press esc.
    keyboard.wait('shift+ctrl+alt+esc')

class LogLevels(str, Enum):
    CRITICAL = "critical"
    ERROR = "error"
    WARNING = "warning"
    INFO = "info"
    DEBUG = "debug"

@cli_app.command()
def web(hotkey_prefix: Annotated[str, typer.Option(help="'keyboard' compatible hot key prefix'")] = "shift+ctrl+alt",
        type_delay: Annotated[float, typer.Option(help="Set the delay time in seconds when using the delayed typing feature")] = 2.0,
        logging_level: Annotated[LogLevels, typer.Option(help="Set the log level for the web server")] = LogLevels.WARNING.value):
    """
    Starts a web interface to configure winhotkey
    """
    from winhotkey.web import initializeSettings
    initializeSettings(hotkey_prefix, type_delay)

    config = Config("winhotkey.web:api_app", host="127.0.0.1", port=17455, reload=True, log_level=logging_level.value)
    server = ThreadedUvicorn(config)
    server.start()
    if logging_level not in [LogLevels.INFO, LogLevels.DEBUG]:
        print("Access the GUI at: http://127.0.0.1:17455/")
    print("Press SHIFT+ALT+CTRL+C to exit.  Leave this window open!")
    keyboard.wait('shift+alt+ctrl+c')
    server.stop()

def winhotkey():
    if not platform.system() == "Windows":
        print("Winhotkey only fully works on Windows.  It will be started in a test mode.  No hotkeys will actually work.")
    cli_app()


import json
import os
import time

from . import stats
from .stats import Details, SortIndex, StatGroup
from .util.media_type import MediaType
from .util.progress_type import ProgressType

def json_decoder(obj):
    if "server_id" in obj:
        return MediaData(obj)
    if "number" in obj:
        return ChapterData(obj)
    return obj

class State:
    version = 1.4
    cache_version = 1

    def __init__(self, settings, session=None):
        self.settings = settings
        self.session = session
        self.media = {}
        self.all_media = {}
        self.hashes = {}
        self.cookie_hash = None
        self.server_cache = {}

        self.load()

    @staticmethod
    def get_hash(json_dict):
        if not json_dict or not any(map(lambda x: json_dict[x], json_dict)):
            return 0, ""
        json_str = json.dumps(json_dict, indent=4, sort_keys=True)
        return hash(json_str), json_str

    def read_file_as_dict(self, file_name, object_hook=json_decoder):
        try:
            with open(file_name, "r") as jsonFile:
                json_dict = json.load(jsonFile, object_hook=object_hook)
                self.hashes[file_name] = State.get_hash(json_dict)[0]
                return json_dict
        except (json.decoder.JSONDecodeError, FileNotFoundError):
            return {}

    def save_to_file(self, file_name, json_dict):
        h, json_str = State.get_hash(json_dict)
        if self.hashes.get(file_name, 0) == h:
            return False
        self.hashes[file_name] = h
        os.makedirs(os.path.dirname(file_name), exist_ok=True)
        with open(file_name, "w") as jsonFile:
            jsonFile.write(json_str)
        import logging
        logging.info("Persisting state to %s", file_name)

        return True

    def save(self):
        self.save_session_cookies()
        self.save_to_file(self.settings.get_metadata_file(), self.all_media)
        self.save_to_file(self.settings.get_server_cache_file(), self.server_cache)
        for media_data in self.media.values():
            self.save_to_file(self.settings.get_chapter_metadata_file(media_data), media_data.chapters)

    def set_session(self, session, no_load=False):
        self.session = session
        if not no_load and not self.settings.no_load_session:
            self.load_session_cookies()
        self._set_session_hash()

    def load(self):
        self.load_media()
        self.server_cache = self.read_file_as_dict(self.settings.get_server_cache_file())
        if not self.server_cache or self.server_cache.get("version") != self.cache_version:
            self.update_server_cache()

    def load_chapter_data(self, media_data):
        media_data.chapters = self.read_file_as_dict(self.settings.get_chapter_metadata_file(media_data))

    def load_media(self):
        self.all_media = self.read_file_as_dict(self.settings.get_metadata_file())
        if not self.all_media:
            self.all_media = dict(media={}, disabled_media={}, version=State.version)
        self.media = self.all_media["media"]
        self.disabled_media = self.all_media["disabled_media"]

        for key, media_data in list(self.media.items()):
            if key != media_data.global_id:
                del self.media[key]
                self.media[media_data.global_id] = media_data
            assert not media_data.chapters
            self.load_chapter_data(media_data)

    def _set_session_hash(self):
        """
        Sets saved cookie_hash
        @return True iff the hash is different than the already saved one

        """
        cookie_hash = hash(str(self.session.cookies))
        if cookie_hash != self.cookie_hash:
            self.cookie_hash = cookie_hash
            return True
        return False

    def load_session_cookies(self, file=None, domains=None):
        """ Load session from disk """
        try:
            with open(file or self.settings.get_cookie_file(), "r") as f:
                for i, line in enumerate(f):
                    line = line.rstrip()
                    if not line or line[0].startswith("#"):
                        continue
                    try:
                        domain, domain_specified, path, secure, expires, name, value, _ = (line + "\t" * 2).split("\t", 7)
                        if domains is not None and not any(map(lambda x: domain.endswith(x), domains)):
                            continue
                        self.session.cookies.set(name, value, path=path, domain=domain, secure=secure == "TRUE", expires=expires if expires else None)
                    except ValueError:
                        import logging
                        logging.info(f"Error on line {i + 1}: {line}")
        except FileNotFoundError:
            pass

    def save_session_cookies(self, force=False):
        """ Save session to disk """
        if self.settings.no_save_session or not self._set_session_hash():
            return False

        os.makedirs(self.settings.cache_dir, exist_ok=True)
        with open(self.settings.get_cookie_file(), "w") as f:
            f.write("# Netscape HTTP Cookie File\n")
            for cookie in self.session.cookies:
                l = [cookie.domain, str(cookie.domain_specified and cookie.domain.startswith(".")).upper(), cookie.path, str(cookie.secure).upper(), str(cookie.expires) if cookie.expires else "", cookie.name, cookie.value]
                f.write("\t".join(l) + "\n")
        return True

    def is_out_of_date(self):
        return State.version > self.all_media.get("version", 0)

    def is_out_of_date_minor(self):
        """ Is a minor state upgrade needed
        Minor upgrades are those that can before performed safely, without an
        internet connect, without user input and the new state is forwards
        compatible (the upgraded file can be used with an older program (same
        major version) without any state loss
        """
        return self.is_out_of_date() and State.version - self.all_media.get("version", 0) < 1

    def update_verion(self):
        self.all_media["version"] = State.version

    def configure_media(self, server_list):
        for key in list(self.media.keys()):
            if self.media[key]["server_id"] not in server_list:
                self.disabled_media[key] = self.media[key]
                del self.media[key]

        for key in list(self.disabled_media.keys()):
            if self.disabled_media[key]["server_id"] in server_list:
                self.media[key] = self.disabled_media[key]
                self.load_chapter_data(self.media[key])
                del self.disabled_media[key]
        self.update_server_cache(server_list)

    def update_server_cache(self, server_list={}):

        self.server_cache = {"servers": {server.id: {"media_type": server.media_type.value, "has_login": server.has_login()} for server in server_list.values()}}
        auth_servers = {server.id for server in server_list.values() if server.has_login()} | {server.alias for server in server_list.values() if server.has_login() and server.alias}
        self.server_cache["auth_servers"] = sorted(list(auth_servers))
        self.server_cache["version"] = self.cache_version

    def get_all_names(self, media_type=None, disallow_servers=False):
        names = set()
        if not disallow_servers:
            for server_id in self.server_cache["servers"]:
                if not media_type or self.server_cache["servers"][server_id]["media_type"] & media_type:
                    names.add(server_id)
        for media_id, media in self.media.items():
            if not media_type or media["media_type"] & media_type:
                names.add(media_id)
                if media.global_id_alt:
                    names.add(media.global_id_alt)
                names.add(media["dir_name"])
                if not "_ARGCOMPLETE" in os.environ:
                    names.add(media["name"])
                    names.add(str(media["id"]))
        return names

    def get_all_single_names(self, media_type=None):
        return self.get_all_names(media_type=media_type, disallow_servers=True)

    def get_server_ids(self):
        return self.server_cache["servers"].keys()

    def get_server_ids_with_logins(self):
        return self.server_cache["auth_servers"]

    def is_tracked(self, media_data):
        return bool(media_data["trackers"])

    def get_media(self, name=None, media_type=None, tag=None, shuffle=False, tracked=None):
        if isinstance(name, dict):
            yield name
            return
        media = self.media.values()
        if shuffle:
            media = list(media)
            import random
            random.shuffle(media)
        for media_data in media:
            if name is not None and name not in (media_data["server_id"], media_data["name"], media_data.global_id, media_data.global_id_alt, str(media_data["id"]), media_data["id"], media_data["dir_name"]):
                continue
            if media_type and media_data["media_type"] & media_type == 0:
                continue
            if tag and tag not in media_data["tags"] or tag == "" and not media_data["tags"]:
                continue
            if tracked is not None and self.is_tracked(media_data) != tracked:
                continue
            yield media_data

    def get_single_media(self, name=None, media_type=None):
        return next(self.get_media(media_type=media_type, name=name))

    def list_media(self, name=None, media_type=None, out_of_date_only=False, tag=None, csv=False, tracked=None):
        now = time.time()
        for media_data in self.get_media(name=name, media_type=media_type, tag=tag, tracked=tracked):
            last_chapter = media_data.get_last_chapter()
            last_read = media_data.get_last_read_chapter()
            if not out_of_date_only or last_chapter.get("number", 0) != last_read.get("number", 0):
                num_key = "number" if media_data["progress_type"] != ProgressType.CHAPTER_VOLUME else "volume_number"
                next_chapter_date_str = media_data.get_next_chapter_available_str(now)
                args = [media_data.friendly_id, media_data["name"], media_data["season_title"], str(last_read.get(num_key, 0)), str(last_chapter.get(num_key, 0)), next_chapter_date_str, ",".join(media_data["tags"])]
                if csv:
                    yield args
                else:
                    yield "{}\t{} {}\t{}/{} {} {}".format(*args)

    def list_chapters(self, name, show_ids=False):
        media_data = self.get_single_media(name=name)
        for chapter in media_data.get_sorted_chapters():
            yield "{:4}{}:{}{}".format(chapter["number"], "*" if chapter["special"] else " ", chapter["title"], ":" + chapter["id"] if show_ids else "")

    def save_stats(self, identifier, stats):
        stats_file = self.settings.get_stats_file()
        saved_data = self.read_file_as_dict(stats_file, object_hook=lambda obj: TrackerEntry(obj))
        saved_data.update({identifier or "": stats})
        self.save_to_file(stats_file, saved_data)

    def list_stats(self, username=None, media_type=None, stat_group=StatGroup.NAME, sort_index=SortIndex.NAME, reverse=False, min_count=0, min_score=1, time_unit=0, no_header=False, details_type=Details.NAME, details_limit=None):
        saved_data = self.read_file_as_dict(self.settings.get_stats_file(), object_hook=lambda obj: TrackerEntry(obj))
        data = saved_data.get(username if username else "", {})
        if media_type:
            data = list(filter(lambda x: x["media_type"] == media_type, data))
        grouped_data = stats.group_entries(data, min_score=min_score)[stat_group.value]
        sorted_data = stats.compute_stats(grouped_data, sort_index.value, reverse=reverse, min_count=min_count, time_unit=time_unit, details_type=details_type, details_limit=details_limit)
        if not no_header:
            yield stats.get_stat_headers(stat_group, details_type=details_type)
        yield from stats.get_stat_entries(sorted_data, details_type)

class MediaData(dict):
    def __init__(self, backing_map):
        super().__init__(backing_map)
        self.chapters = {}

    def __getitem__(self, key):
        if key == "chapters":
            return self.chapters
        else:
            return super().__getitem__(key)

    def __str__(self):
        return "{}\t{} {} ({})".format(self.global_id, self["name"], self.get("label", self["season_title"]), MediaType(self["media_type"]).name)

    def reset(self, media_data):
        assert media_data is not self
        assert not media_data.chapters
        copy = MediaData(media_data)
        self.copy_fields_to(copy)
        self.clear()
        self.chapters.clear()
        self.update(copy)

    def get_sorted_chapters(self, volume=False, filter_list=[]):
        return sorted(self["chapters"].values(), key=lambda x: (x.get_number(volume=volume), x.get_number()))

    @property
    def global_id(self):
        return "{}:{}{}{}".format(self["server_id"], self["id"], (self["season_id"] if self["season_id"] else ""), self.get("lang", "").split("-")[0][:3])

    @property
    def global_id_alt(self):
        return "{}:{}{}{}".format(self["server_id"], self["alt_id"], (self["season_id"] if self["season_id"] else ""), self.get("lang", "")[:3]) if self.get("alt_id", False) else None

    @property
    def friendly_id(self):
        return self.global_id if len(self.global_id) < 32 or not self.global_id_alt else self.global_id_alt

    def get_next_chapter_available_str(self, time):
        timestamp = self.get("nextTimeStamp") or self.get("nextTimeStampTracker", 0)
        if not timestamp:
            return ""
        delta = max(timestamp - time, 0)
        if delta < 3600:
            return f"{delta/60:.1f} minutes"
        elif delta < 3600 * 24 * 1.5:
            return f"{delta/3600:.1f} hours"
        else:
            return f"{delta/3600/24:.1f} days"

    def copy_fields_to(self, dest):
        for key in ("nextTimeStampTracker", "offset", "progress", "progress_type", "tags", "trackers"):
            if key in self:
                dest[key] = self.get(key)

    def get_last_chapter(self):
        return max(self["chapters"].values(), key=lambda x: x["number"], default={})

    def get_first_chapter_number_greater_than_zero(self):
        return min(self["chapters"].values(), key=lambda x: x["number"] if x["number"] > 0 else float("inf"))["number"]

    def get_chapter_number_to_id(self, chapter_num):
        return max(filter(lambda x: x["number"] == chapter_num, self["chapters"].values()), key=lambda x: x["number"], default={}).get("id")

    def get_last_read_chapter(self, volume=False):
        return max(filter(lambda x: x["read"], self["chapters"].values()), key=lambda x: x.get_number(volume), default=ChapterData({}))

    def get_last_read_chapter_number(self, volume=False):
        return self.get_last_read_chapter(volume=volume).get_number(volume)

    def get_unreads(self, any_unread=False, volume=False):
        lastRead = self.get_last_read_chapter_number(volume=volume)
        for chapter in self.get_sorted_chapters(volume=volume):
            if not chapter["read"] and (any_unread or (chapter.get_number(volume) > lastRead and not chapter["special"])):
                yield chapter

    def get_labels(self):
        return [self.global_id, self["name"], self["server_id"], self["server_alias"], MediaType(self["media_type"]).name]

class ChapterData(dict):
    update_state = False

    def __init__(self, backing_map):
        super().__init__(backing_map)

    def update(self, key_pars):
        super().update(key_pars)
        self.update_state = True

    def get_number(self, volume=False):
        return self.get("volume_number" if volume else "number") or 0

    def check_if_updated_and_clear(self):
        updated = self.update_state
        self.update_state = False
        return updated

class TrackerEntry(dict):
    def __init__(self, backing_map):
        super().__init__(backing_map)

    def __getitem__(self, key):
        if key == "name":
            names = super().get("names")
            return names.get("english") or names.get("romaji") or next(iter(names.values())) if names else super().__getitem__(key)
        elif key == "names":
            names = super().get("names")
            return list(set(filter(lambda x: x, names.values())))
        else:
            return super().__getitem__(key)


from hydra import compose, initialize
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import lightning.pytorch as L
import pickle
import sys
import os
sys.path.append("../")
from models.lightning_modules import CNN_mel, CNN_modgd, CNN_pitch, ListDataset

# global initialization
initialize(version_base=None, config_path="../../configs")
cfg = compose(config_name="config")

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

def calculate_acc(y1, y2):
    ret = 0
    for i, _ in enumerate(y1):
        ret += int(y1[i] == y2[i])
    ret /= len(y1)
    return ret

def calculate_f1(y1, y2):
    tp = 0
    fp = 0
    fn = 0
    for i, _ in enumerate(y1):
        if y1[i] == 1 and y2[i] == 1:
            tp += 1
        elif y1[i] == 1 and y2[i] == 0:
            fp += 1
        elif y1[i] == 0 and y2[i] == 1:
            fn += 1
    f1 = (2 * tp) / (2 * tp + fp + fn)
    return f1

# constants
lr = cfg.training.learning_rate
num_classes = cfg.constants.num_classes
num_workers = os.cpu_count()

# paths
models_dir = cfg.paths.models_folder

# label map
label_map = cfg.label_map

# methods
aggregation_method = cfg.aggregation.method
fusion_method = cfg.fusion.method

# fusion
theta_step = cfg.fusion.theta_step
theta_step_new = cfg.fusion.theta_step_new
theta_max = cfg.fusion.theta_max
version = cfg.fusion.version
optimization_metric = cfg.fusion.optimization_metric

models = []
model_names = []

for root, dirs, files in os.walk(models_dir):
    files.sort()
    for file in files:
        if (file == 'cnn_mel.ckpt'):
            print('success')
            model = CNN_mel.load_from_checkpoint(os.path.join(models_dir, file), lr=lr, num_labels=num_classes, map_location=device)
            model_names.append(file)
            models.append(model)
        elif (file == 'cnn_modgd.ckpt'):
            model = CNN_modgd.load_from_checkpoint(os.path.join(models_dir, file), lr=lr, num_labels=num_classes, map_location=device)
            model_names.append(file)
            models.append(model)
        elif (file == 'cnn_pitch.ckpt'):
            model = CNN_pitch.load_from_checkpoint(os.path.join(models_dir, file), lr=lr, num_labels=num_classes, map_location=device)
            model_names.append(file)
            models.append(model)
num_models = len(models)
print(model_names)

print("Load X_test and y_test")
f = open("../../data/processed/X_test_mel.pkl", "rb")
X_test_mel = pickle.load(f)
f.close()
print("Mel data loaded!")

f = open("../../data/processed/X_test_modgd.pkl", "rb")
X_test_modgd = pickle.load(f)
f.close()
print("Modgd data loaded!")

f = open("../../data/processed/X_test_pitch.pkl", "rb")
X_test_pitch = pickle.load(f)
f.close()
print("Pitch data loaded!")

f = open("../../data/processed/y_test.pkl", "rb")
y_test = pickle.load(f)
f.close()

X_test = [X_test_mel, X_test_modgd, X_test_pitch]

a = y_test
y_test = torch.zeros((len(a), num_classes))
for i in range(len(a)):
    y_test[i] = a[i][0]

print("Loaded!")

print("Computing probabilities")
probabilities = []

for i in range(num_models):
    keys = list(X_test[i].keys())
    vals = list(X_test[i].values())
    dataset = ListDataset(keys, vals)
    predict_loader = DataLoader(dataset, batch_size=1, num_workers=num_workers, shuffle=False)

    model = models[i]
    trainer = L.Trainer()
    trainer.predict(model, dataloaders=predict_loader)

    model_predictions = model.model_predictions
    model_predictions = torch.stack(model_predictions)
    probabilities.append(model_predictions)

probabilities = torch.stack(probabilities)
probabilities = probabilities.detach().cpu().numpy()

print("Probabilities computed!")
print(probabilities.shape)
y_pred = np.zeros((probabilities.shape[1], probabilities.shape[2]))
mul_probabilities = np.zeros((probabilities.shape[1], probabilities.shape[2]))
fusion = np.zeros((num_classes, num_models))
thetas = np.arange(0, theta_max, theta_step)

final_accuracy = 0.0

print("Calculating fusion thresholds!")

if fusion_method == 1:

    thetas_mel = thetas
    if "mel" not in version:
        thetas_mel = [-1]
    thetas_modgd = thetas
    if "modgd" not in version:
        thetas_modgd = [-1]
    thetas_pitch = thetas
    if "pitch" not in version:
        thetas_pitch = [-1]

    for k in range(num_classes):
        best_acc = 0.0
        best_f1 = 0.0
        print(k)
        for theta1 in thetas_mel:
            for theta2 in thetas_modgd:
                for theta3 in thetas_pitch:
                    fusion_thetas = [theta1, theta2, theta3]
                    pred = [(np.array(probabilities[id, :, k]) - theta) for id, theta in enumerate(fusion_thetas)]

                    for i, _ in enumerate(pred):
                        pred[i][pred[i] > 0] = 1
                        pred[i][pred[i] <= 0] = 0

                    for i, _ in enumerate(fusion_thetas):
                        if fusion_thetas[i] == -1:
                            pred[i] = np.ones(pred[i].shape)

                    pred = np.multiply(np.multiply(pred[0], pred[1]), pred[2])

                    if optimization_metric == "acc":
                        acc = calculate_acc(pred, y_test[:, k])
                        if acc > best_acc:
                            best_acc = acc
                            y_pred[:, k] = pred
                            fusion[k, :] = fusion_thetas

                    elif optimization_metric == "f1":
                        f1 = calculate_f1(pred, y_test[:, k])
                        if f1 > best_f1:
                            best_f1 = f1
                            y_pred[:, k] = pred
                            fusion[k, :] = fusion_thetas

        thetas_mel_new = np.arange(fusion[k][0] - 0.05, fusion[k][0] + 0.05, theta_step_new)
        if "mel" not in version:
            thetas_mel_new = [-1]
        thetas_modgd_new = np.arange(fusion[k][1] - 0.05, fusion[k][1] + 0.05, theta_step_new)
        if "modgd" not in version:
            thetas_modgd_new = [-1]
        thetas_pitch_new = np.arange(fusion[k][2] - 0.05, fusion[k][2] + 0.05, theta_step_new)
        if "pitch" not in version:
            thetas_pitch_new = [-1]

        for theta1 in thetas_mel_new:
            for theta2 in thetas_modgd_new:
                for theta3 in thetas_pitch_new:
                    fusion_thetas = [theta1, theta2, theta3]
                    pred = [(np.array(probabilities[id, :, k]) - theta) for id, theta in enumerate(fusion_thetas)]

                    for i, _ in enumerate(pred):
                        pred[i][pred[i] > 0] = 1
                        pred[i][pred[i] <= 0] = 0

                    for i, _ in enumerate(fusion_thetas):
                        if fusion_thetas[i] == -1:
                            pred[i] = np.ones(pred[i].shape)

                    pred = np.multiply(np.multiply(pred[0], pred[1]), pred[2])

                    if optimization_metric == "acc":
                        acc = calculate_acc(pred, y_test[:, k])
                        if acc > best_acc:
                            best_acc = acc
                            y_pred[:, k] = pred
                            fusion[k, :] = fusion_thetas

                    elif optimization_metric == "f1":
                        f1 = calculate_f1(pred, y_test[:, k])
                        if f1 > best_f1:
                            best_f1 = f1
                            y_pred[:, k] = pred
                            fusion[k, :] = fusion_thetas

        final_accuracy += calculate_acc(y_pred[:, k], y_test[:, k])

final_accuracy = final_accuracy / num_classes

print("Final accuracy:", final_accuracy)

# Save fusion thresholds
save_str = "fusion_thresholds_"
for model_name in version:
    save_str += model_name
    save_str += "_"
save_str += optimization_metric
save_str += ".npy"
np.save(save_str, fusion)


import os
import argparse
import importlib
from glob import glob
import json

import ml.utils as utils

model_names = ['basic_kmer', 'basic_vector', 'basic_onehot', 'cnn_qrcode', 'rnn', 'lstm_vae_ordinal', 'lstm_vae_kmer_4', 'lstm_vae_kmer_5',
               'hybrid_vae_ordinal', 'hybrid_vae_kmer_4',
               'hybrid_vae_kmer_5']

def parse_args():
    parser = argparse.ArgumentParser(description='Taxonomic Classification')

    parser.add_argument('--model-name', type=str,
                        help=('Name of the model to train. Possible values ' +
                              'are {}'.format(','.join(model_names))),
                        choices=set(model_names))

    parser.add_argument('--is-demo', action='store_true',
                        help='If True, train script is run in demo mode with limited number of epochs (typically 1 or 2)')

    return parser.parse_args()

def get_interface_module(model_name):
    for fpath_interface in glob('ml/**/interface.py', recursive=True):
        module_name = fpath_interface.replace('/', '.').replace('.py', '')
        interface_module = importlib.import_module(module_name)

        if model_name in interface_module.RUN_OPTIONS:
            return interface_module

    raise ValueError('Model not implemented')

def train():
    args = parse_args()

    if not args.model_name:
        raise ValueError(
            'Model name is mandatory and should be one of - {}'.format(','.join(model_names)))

    data_dirpaths = ['cnn', 'hierarchy', 'vae', 'kmer']

    for data_dirpath in data_dirpaths:
        dirpath = os.path.join('data', data_dirpath)
        if not os.path.exists(dirpath):
            raise ValueError(
                'Directory \'{}\' not found. Please run process_data.py before proceeding!'.format(dirpath))

    path_config = json.loads(open('config/paths.json', 'r').read())

    interface = get_interface_module(args.model_name)
    interface.train_model(path_config, args=args)

if __name__ == '__main__':
    train()


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

class MaskedMultiHeadAttention(nn.Module):
    def __init__(self, hidden_size, n_ctx, config):
        super(MaskedMultiHeadAttention, self).__init__()
        self.n_head = config.n_head
        assert hidden_size % self.n_head == 0

        self.query = nn.Linear(hidden_size, hidden_size)
        self.key = nn.Linear(hidden_size, hidden_size)
        self.value = nn.Linear(hidden_size, hidden_size)
        self.linear = nn.Linear(hidden_size, hidden_size)

        self.attn_dropout = nn.Dropout(config.attn_pdrop)
        self.resid_dropout = nn.Dropout(config.resid_pdrop)

        # (1, 1, 1024, 1024)
        self.masked = torch.tril(torch.ones(n_ctx, n_ctx).view(1, 1, n_ctx, n_ctx))
        self.masked_bias = torch.tensor(-1e4)

    def split_heads(self, x, k=False):
        # size is (batch_size, seq_len, num_head, head_features)
        new_x_shape = x.size()[:-1] + (self.n_head, x.size(-1) // self.n_head)
        x = x.view(*new_x_shape)

        # key is transpose
        if k:
            # (batch_size, num_head, head_features, seq_len)
            return x.permute(0, 2, 3, 1)
        else:
            # (batch_size, num_head, seq_len, head_features)
            return x.permute(0, 2, 1, 3)

    @staticmethod
    def merge_heads(x):
        x = x.permute(0, 2, 1, 3).contiguous()
        new_x_shape = x.size()[:-2] + (x.size(-2) * x.size(-1),)
        return x.view(*new_x_shape)

    def forward(self, hidden_states, layer_past=None, attention_mask=None):
        query = self.query(hidden_states)
        key = self.key(hidden_states)
        value = self.value(hidden_states)

        query = self.split_heads(query)
        key = self.split_heads(key, k=True)
        value = self.split_heads(value)

        if layer_past is not None:
            past_key, past_value = layer_past[0].transpose(-2, -1), layer_past[1]
            # (batch_size, num_head, head_features, sql_len+1)
            key = torch.cat((past_key, key), dim=-1)
            # (batch_size, num_head, sql_len+1, head_features)
            value = torch.cat((past_value, value), dim=-2)
        present = torch.stack((key.transpose(-2, -1), value))

        '''
        for first iteration: the query、key、value seq_len is seq_len

        for second or others iteration: the query、key、value seq_len is 1
        # query: (batch_size, num_head, 1, head_features)
        # key: (batch_size, num_head, head_features, sql_len+1)
        # value: (batch_size, num_head, sql_len+1, head_features)
        # w: (batch_size, num_head, 1, sql_len+1)
        '''
        w = torch.matmul(query, key)
        w = w / (float(value.size(-1)) ** 0.5)

        # masked
        nd, ns = w.size(-2), w.size(-1)
        mask = self.masked[:, :, ns - nd: ns, :ns]
        w = torch.where(mask.bool(), w, self.masked_bias.to(w.dtype))

        # process attention mask
        if attention_mask is not None:
            w = w + attention_mask

        w = nn.Softmax(dim=-1)(w)
        w = self.attn_dropout(w)

        # attention_output: (batch_size, num_head, sql_len, head_features)
        attention_output = torch.matmul(w, value)
        attention_output = self.merge_heads(attention_output)
        attention_output = self.linear(attention_output)
        attention_output = self.resid_dropout(attention_output)

        return attention_output, present

class MLP(nn.Module):
    def __init__(self, inner_dim, config):
        super(MLP, self).__init__()
        self.linear_1 = nn.Linear(config.n_embd, inner_dim)
        self.linear_2 = nn.Linear(inner_dim, config.n_embd)
        self.gelu = nn.GELU()

        nn.init.normal_(self.linear_1.weight, std=0.02)
        nn.init.normal_(self.linear_2.weight, std=0.02)

    def forward(self, inputs):
        outputs = self.gelu(self.linear_1(inputs))
        outputs = self.linear_2(outputs)
        return outputs

class Block(nn.Module):
    def __init__(self, n_ctx, config):
        super(Block, self).__init__()
        hidden_size = config.n_embd
        inner_dim = config.n_inner if config.n_inner is not None else 4 * hidden_size

        self.layer_norm_1 = nn.LayerNorm(hidden_size, config.layer_norm_epsilon)
        self.attention = MaskedMultiHeadAttention(hidden_size, n_ctx, config)
        self.layer_norm_2 = nn.LayerNorm(hidden_size, config.layer_norm_epsilon)
        self.mlp = MLP(inner_dim, config)

    def forward(self, hidden_states, layer_past=None, attention_mask=None):
        # first: Masked Multi Head Self Attention
        hidden_states = self.layer_norm_1(hidden_states)
        attention_output, present = self.attention(hidden_states,
                                                   layer_past=layer_past,
                                                   attention_mask=attention_mask)
        hidden_states = hidden_states + attention_output

        # second: MLP layer
        hidden_states = self.layer_norm_2(hidden_states)
        mlp_out = self.mlp(hidden_states)
        hidden_states = hidden_states + mlp_out

        return hidden_states, present

class GPT2Model(nn.Module):
    def __init__(self, config):
        super(GPT2Model, self).__init__()
        # initialize word embedding and position embedding
        self.word_embedding = nn.Embedding(config.vocab_size, config.n_embd)
        init.normal_(self.word_embedding.weight, std=0.02)
        self.position_embedding = nn.Embedding(config.n_positions, config.n_embd)
        init.normal_(self.position_embedding.weight, std=0.01)
        self.drop_out = nn.Dropout(config.embd_pdrop)

        # transformer decoder block
        self.blocks = nn.ModuleList([Block(config.n_ctx, config) for _ in range(config.n_layer)])
        self.layer_norm = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon)

    def forward(self, input_ids, attention_mask=None, position_ids=None, past_key_values=None):
        if input_ids is not None:
            input_shape = input_ids.size()
            input_ids = input_ids.view(-1, input_shape[-1])
            batch_size = input_ids.shape[0]
        else:
            raise ValueError("input_ids is None")

        if past_key_values is None:
            past_length = 0
            past_key_values = [None] * len(self.blocks)
        else:
            past_length = past_key_values[0][0].size(-2)

        if position_ids is None:
            device = input_ids.device
            position_ids = torch.arange(past_length, input_shape[-1] + past_length, dtype=torch.long, device=device)
            position_ids = position_ids.unsqueeze(0).view(-1, input_shape[-1])

        if attention_mask is not None:
            assert batch_size > 0, "batch_size has to be defined and > 0"
            attention_mask = attention_mask.view(batch_size, -1)
            attention_mask = attention_mask[:, None, None, :]
            attention_mask = (1.0 - attention_mask) * -10000.0

        # embedding layer
        word_embeds = self.word_embedding(input_ids)
        position_embeds = self.position_embedding(position_ids)
        hidden_states = word_embeds + position_embeds
        hidden_states = self.drop_out(hidden_states)

        # transformer block layer
        presents = []
        for i, (block, layer_past) in enumerate(zip(self.blocks, past_key_values)):
            hidden_states, present = block(
                hidden_states,
                layer_past=layer_past,
                attention_mask=attention_mask
            )
            presents.append(present)

        hidden_states = self.layer_norm(hidden_states)
        output_shape = input_shape + (hidden_states.size(-1),)
        return {
            'hidden_states': hidden_states.view(*output_shape),
            'past_key_values': presents
        }

class GPT2LMHeadModel(nn.Module):
    def __init__(self, config):
        super(GPT2LMHeadModel, self).__init__()
        self.gpt = GPT2Model(config)
        self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False)

    def forward(self,
                input_ids,
                attention_mask=None,
                position_ids=None,
                past_key_values=None,
                lm_labels=None):
        hidden_states, presents = self.gpt(
            input_ids,
            attention_mask=attention_mask,
            position_ids=position_ids,
            past_key_values=past_key_values)

        lm_logits = self.lm_head(hidden_states)

        if lm_labels is not None:
            shift_logits = lm_logits[..., :-1, :].contiguous()
            shift_labels = lm_labels[..., 1:].contiguous()
            loss_fct = nn.CrossEntropyLoss()
            loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
            return loss, lm_logits, presents
        return {
            'logits': lm_logits,
            'past_key_values': presents
        }


from datetime import timedelta
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from datetime import datetime
import pandas as pd

def chumma_sample():

    print("hello bro")

with DAG(
        dag_id="hello_world",
        schedule_interval="@daily",
        default_args={
            "owner": "airflow",
            "retries": 1,
            "retry_delay": timedelta(minutes=5),
            "start_date": datetime(2021, 1, 1),
        },
        catchup=False) as f:

    first_function_execute = PythonOperator(
        task_id="chumma_task",
        python_callable=chumma_sample
    )


# encoding: utf-8

from __future__ import print_function, unicode_literals

import sys

import pytest
from pytablewriter.style import Align, FontSize, FontStyle, FontWeight, Style, ThousandSeparator

class Test_Style_constructor(object):
    @pytest.mark.parametrize(
        ["value", "expected"],
        [
            [
                {
                    "align": Align.RIGHT,
                    "font_size": FontSize.TINY,
                    "font_weight": FontWeight.BOLD,
                    "thousand_separator": ThousandSeparator.SPACE,
                },
                {
                    "align": Align.RIGHT,
                    "font_size": FontSize.TINY,
                    "font_weight": FontWeight.BOLD,
                    "thousand_separator": ThousandSeparator.SPACE,
                },
            ],
            [
                {
                    "align": "left",
                    "font_size": "small",
                    "font_weight": "bold",
                    "thousand_separator": ",",
                },
                {
                    "align": Align.LEFT,
                    "font_size": FontSize.SMALL,
                    "font_weight": FontWeight.BOLD,
                    "thousand_separator": ThousandSeparator.COMMA,
                },
            ],
            [
                {"font_size": "TINY"},
                {
                    "align": Align.AUTO,
                    "font_size": FontSize.TINY,
                    "font_weight": FontWeight.NORMAL,
                    "thousand_separator": ThousandSeparator.NONE,
                },
            ],
        ],
    )
    def test_normal(self, value, expected):
        style = Style(**value)

        print("expected: {}\nactual: {}".format(expected, style), file=sys.stderr)

        assert style.align is expected.get("align")
        assert style.font_size is expected.get("font_size")
        assert style.font_weight is expected.get("font_weight")
        assert style.thousand_separator is expected.get("thousand_separator")

class Test_Style_eq(object):
    @pytest.mark.parametrize(
        ["lhs", "rhs", "expected"],
        [
            [Style(), Style(), True],
            [Style(align=Align.RIGHT), Style(align=Align.RIGHT), True],
            [Style(align=Align.RIGHT), Style(align=Align.LEFT), False],
            [Style(align=Align.RIGHT), Style(align="right"), True],
            [Style(align=Align.RIGHT), Style(align=Align.RIGHT, font_size=FontSize.TINY), False],
            [Style(font_size=FontSize.TINY), Style(font_size=FontSize.TINY), True],
            [Style(font_size=FontSize.TINY), Style(font_size="tiny"), True],
            [Style(font_size=FontSize.TINY), Style(font_size=FontSize.LARGE), False],
            [Style(font_weight="bold"), Style(font_weight=FontWeight.BOLD), True],
            [Style(font_weight="bold"), Style(font_weight="normal"), False],
            [Style(font_style="italic"), Style(font_style=FontStyle.ITALIC), True],
            [Style(font_style="italic"), Style(font_style="normal"), False],
            [Style(thousand_separator=","), Style(thousand_separator=","), True],
            [Style(thousand_separator=","), Style(thousand_separator="comma"), True],
            [Style(thousand_separator=""), Style(thousand_separator=","), False],
            [
                Style(thousand_separator=ThousandSeparator.COMMA),
                Style(thousand_separator=ThousandSeparator.COMMA),
                True,
            ],
            [
                Style(thousand_separator="space"),
                Style(thousand_separator=ThousandSeparator.SPACE),
                True,
            ],
            [
                Style(thousand_separator=ThousandSeparator.COMMA),
                Style(thousand_separator=ThousandSeparator.COMMA, font_size=FontSize.TINY),
                False,
            ],
            [
                Style(
                    align=Align.LEFT,
                    font_size=FontSize.TINY,
                    font_style=FontStyle.ITALIC,
                    font_weight=FontWeight.BOLD,
                    thousand_separator=ThousandSeparator.COMMA,
                ),
                Style(
                    align="left",
                    font_size="tiny",
                    font_style="italic",
                    font_weight="bold",
                    thousand_separator=",",
                ),
                True,
            ],
            [Style(), None, False],
        ],
    )
    def test_normal(self, lhs, rhs, expected):
        assert (lhs == rhs) == expected
        assert (lhs != rhs) != expected

    @pytest.mark.parametrize(
        ["align", "font_size", "thousand_separator", "expected"],
        [
            ["invali", None, None, TypeError],
            [FontSize.TINY, None, None, TypeError],
            [None, "invali", None, TypeError],
            [None, Align.LEFT, None, TypeError],
            [None, None, "invalid", TypeError],
        ],
    )
    def test_exception(self, align, font_size, thousand_separator, expected):
        with pytest.raises(expected):
            Style(align=align, font_size=font_size, thousand_separator=thousand_separator)

class Test_Style_repr(object):
    @pytest.mark.parametrize(
        ["value", "expected"],
        [
            [
                Style(
                    align="left",
                    font_size="tiny",
                    font_style="italic",
                    font_weight="bold",
                    thousand_separator=",",
                ),
                "(align=left, font_size=tiny, font_style=italic, font_weight=bold, thousand_separator=comma)",
            ],
            [Style(), "(align=auto, font_style=normal, font_weight=normal)"],
        ],
    )
    def test_normal(self, value, expected):
        assert str(value) == expected


from common import Problem, exec_template_methods
from typing import List
import heapq

class Solution(Problem):

    class KthLargest:
        def __init__(self, k: int, nums: List[int]):

            self.pool = nums
            self.size = len(self.pool)
            self.k = k
            heapq.heapify(self.pool)
            while self.size > k:
                heapq.heappop(self.pool)
                self.size -= 1

        def add(self, val: int) -> int:
            if self.size < self.k:
                heapq.heappush(self.pool, val)
                self.size += 1
            elif val > self.pool[0]:
                heapq.heapreplace(self.pool, val)

            return self.pool[0]

if __name__ == '__main__':
    Solution.test(__file__)


from flask import render_template, flash, request, redirect, url_for, session, jsonify, Blueprint
from functools import wraps
from tools import MySQLTools

user = Blueprint('user', __name__)
# 初始化数据库
util = MySQLTools('localhost', 'root', 'cfpl', 'sharebike', 'utf8')

# 判断用户是否登录成功
def login_required(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        if session.get('user', None):
            return func(*args, **kwargs)
        else:
            flash("请先登录", 'danger')
            return redirect(url_for('login'))
    return wrapper

# 主页
@user.route('/index', methods = ['GET', 'POST'])
@login_required
def index():
    user_cnt, bike_cnt, order_cnt, news_cnt = util.dataStatic()
    n1, n2, n3, n4 = util.numStatic()
    return render_template("/user/index.html",
                           user_cnt = user_cnt, bike_cnt = bike_cnt,
                           order_cnt = order_cnt, news_cnt = news_cnt,
                           n1 = n1, n2 = n2, n3 = n3, n4 = n4)

数据库课程设计-李文根/1853790_庄镇华_应用系统开发/源代码/user/__init__.py


'''
Open Addressing Hash Table with Linear Probing and Lazy Deletions
By: Alex Mirov
Feb 2019
'''

import sympy

######## Hash Functions ########

def modulo (val, tableSize):
	return val % tableSize

def quadratic (val, tableSize):
	return val ** 2

######## Hash Table ##########

class HashTable:

	def __init__ (self, initialSize=7, loadFactor=0.5, hashFunction=modulo):
		self.table = [None] * initialSize
		self.loadFactor = loadFactor
		self.hashFunction = hashFunction

	def _getLoad (self):
		'''
		Returns the current percent utilized capacity (load) of the table.
		'''
		ct = 0
		for el in self.table:
			if el is not None: ct += 1

		return ct / len(self.table)

	def _rehash (self):
		'''
		When the capacity (load) of the table reaches the loadFactor threshold, _rehash creates a new
		table of size == the next prime number after 2x the current table size.
		'''
		# Get new table size
		newTableSize = len(self.table) * 2
		while not sympy.isprime(newTableSize): newTableSize += 1

		# Init empty new table
		oldTable = self.table
		self.table = [None] * newTableSize

		# Re-insert every element from old table into new table
		for el in oldTable:
			if el is not None:
				self.insert(el)

	def insert (self, data):
		'''
		Inserts 'data' into the table using linear probing
		'''
		index = self.hashFunction(data, len(self.table))
		for i in range(0, len(self.table)):
			index = (index + i) % len(self.table)

			if self.table[index] is None:
				self.table[index] = data
				break

		# Check if current utilized capacity has exceeded load factor
		if self._getLoad() >= self.loadFactor: self._rehash()

	def find (self, data):
		'''
		Returns index into hash table with matching data value.
		Returns None if data is not found.
		'''
		index = self.hashFunction(data, len(self.table))
		for i in range(0, len(self.table)):
			index = (index + i) % len(self.table)

			if self.table[index] is data:
				print("FOUND:", data, "at", index, "in", i + 1, "searches")
				return index

		return None

	def delete(self, data):
		'''
		Removes matching data from hash table. Implements Lazy Deletions -- meaning that the table will not
		rehash once elements have been removed.
		Returns True if successfully deleted.
		Returns False if data is not found in the table.
		'''
		# Find data in table
		index = self.find(data)
		if index is None: return False

		# Remove data
		self.table[index] = None
		return True

	def printTable(self):
		print("Hash Table:  Size:", len(self.table), "  Load Factor:", self.loadFactor, "  Current Capcaity:", self._getLoad())
		for el, i in zip(self.table, range(0, len(self.table))):
			print("  ", i, el)

if __name__ == "__main__":

	myHash = HashTable(hashFunction=quadratic)

	myHash.insert(33)
	myHash.insert(11)
	myHash.insert(1)
	myHash.insert(2)
	myHash.insert(7)
	myHash.insert(12)
	myHash.insert(17)
	myHash.insert(16)
	myHash.insert(8)

	myHash.printTable()

	myHash.delete(12)

	myHash.printTable()


from tkinter.tix import DECREASING
from django.http import HttpResponse
from django.shortcuts import render
from zeep import Client

client = Client(wsdl='https://www.openligadb.de/Webservices/Sportsdata.asmx?wsdl')
lastmatch = client.service.GetLastMatch('bl1')
season = lastmatch['leagueSaison']

def win_loss(allTeams,all_matches):
    for team in allTeams:
        all_matches_home = []
        all_matches_away = []
        win_counter = 0
        lose_counter = 0
        for i in all_matches:
            if i['idTeam1'] == team['teamID']:
                all_matches_home.append(i)
            elif i['idTeam2'] == team['teamID']:
                all_matches_away.append(i)

        for z in all_matches_home:
            if z['pointsTeam1'] > z['pointsTeam2']:
                win_counter += 1
            elif z['pointsTeam1'] < z['pointsTeam2']:
                lose_counter += 1

        for y in all_matches_away:
            if y['pointsTeam1'] > y['pointsTeam2']:
                lose_counter += 1
            elif y['pointsTeam1'] < y['pointsTeam2']:
                win_counter += 1

        team['win_counter'] = win_counter
        team['lose_counter'] = lose_counter
        team['win_percentage'] = int((win_counter/(win_counter+lose_counter))*100)

    return allTeams

def win_loss_detail(all_matches,id):
        team = []
        all_matches_home = []
        all_matches_away = []
        win_counter = 0
        lose_counter = 0
        for i in all_matches:
            if i['idTeam1'] == id:
                all_matches_home.append(i)
            elif i['idTeam2'] == id:
                all_matches_away.append(i)

        for z in all_matches_home:
            if z['pointsTeam1'] > z['pointsTeam2']:
                win_counter += 1
            elif z['pointsTeam1'] < z['pointsTeam2']:
                lose_counter += 1

        for y in all_matches_away:
            if y['pointsTeam1'] > y['pointsTeam2']:
                lose_counter += 1
            elif y['pointsTeam1'] < y['pointsTeam2']:
                win_counter += 1

        win_percentage = int((win_counter/(win_counter+lose_counter))*100)

        return win_counter,lose_counter,win_percentage

# Create your views here.

def teams(request):
    allTeams = client.service.GetTeamsByLeagueSaison('bl1', season)
    all_matches = client.service.GetMatchdataByLeagueSaison('bl1', season)

    allTeams = win_loss(allTeams,all_matches)

    return render(request, 'teams/teams.html',{
        'allTeams':allTeams,
    })

def team_detail(request, id):
    team = []
    allID = []
    allTeams = client.service.GetTeamsByLeagueSaison('bl1', season)
    for i in allTeams:
        allID.append(i['teamID'])
    if id in allID:
        all_matches = client.service.GetMatchdataByLeagueSaison('bl1', season)
        for y in allTeams:
            if y['teamID'] == id:
                team.append(y)
        win_counter,lose_counter,win_percentage = win_loss_detail(all_matches,id)
        print(win_counter,lose_counter,win_percentage)

        for y in team:
            y['win_counter'] = win_counter
            y['lose_counter'] = lose_counter
            y['win_percentage'] = win_percentage

        next_match = client.service.GetNextMatchByLeagueTeam(4500, id)
        latest_results = []
        for y in all_matches:
            if y['idTeam1'] == id and y['pointsTeam1'] == True  or y['idTeam2'] == id and y['pointsTeam1'] == True:
                latest_results.append(y)

        return render(request, 'teams/team_details.html',{
            'team':team,
            'next_match':next_match,
            'latest_results':reversed(latest_results[-5:]),
        })
    else:
        return HttpResponse('
Team not found
')


import PIL
import numpy as np
import cv2
import shapely
import matplotlib.pyplot as plt

import wwtool

def get_palette(num_classes):
    n = num_classes
    palette = [0]*(n*3)
    for j in range(0, n):
        lab = j
        palette[j*3+0] = 0
        palette[j*3+1] = 0
        palette[j*3+2] = 0
        i = 0
        while (lab > 0):
            palette[j*3+0] |= (((lab >> 0) & 1) << (7-i))
            palette[j*3+1] |= (((lab >> 1) & 1) << (7-i))
            palette[j*3+2] |= (((lab >> 2) & 1) << (7-i))
            i = i + 1
            lab >>= 3
    return palette

def colorize_mask(mask, palette):
    zero_pad = 256 * 3 - len(palette)
    for _ in range(zero_pad):
        palette.append(0)
    new_mask = PIL.Image.fromarray(mask.astype(np.uint8)).convert('P')
    new_mask.putpalette(palette)
    new_mask = new_mask.convert('RGB')
    return new_mask

def show_mask(mask,
              num_classes=2,
              wait_time=0,
              out_file=None):
    palette = get_palette(num_classes)
    colorized_mask = colorize_mask(mask, palette)
    colorized_mask_np = np.array(colorized_mask)
    colorized_mask_np = colorized_mask_np[:, :, ::-1].copy()
    wwtool.show_image(colorized_mask_np,
                      wait_time=wait_time,
                      win_name='mask',
                      save_name=out_file)

def show_polygons_on_image(masks,
                           img,
                           alpha=0.4,
                           output_file=None):
    """show masks on image

    Args:
        masks (list): list of coordinate
        img (np.array): original image
        alpha (int): compress
        output_file (str): save path
    """
    color_list = list(wwtool.COLORS.keys())
    img_h, img_w, _ = img.shape

    foreground = wwtool.generate_image(img_h, img_w, (0, 0, 0))
    for idx, mask in enumerate(masks):
        mask = np.array(mask).reshape(1, -1, 2)
        cv2.fillPoly(foreground, mask, (wwtool.COLORS[color_list[idx % 20]][2], wwtool.COLORS[color_list[idx % 20]][1], wwtool.COLORS[color_list[idx % 20]][0]))

    heatmap = wwtool.show_grayscale_as_heatmap(foreground / 255.0, show=False, return_img=True)
    beta = (1.0 - alpha)
    fusion = cv2.addWeighted(heatmap, alpha, img, beta, 0.0)

    if output_file is not None:
        cv2.imwrite(output_file, fusion)
    else:
        wwtool.show_image(fusion, save_name=None)

    return fusion

def show_polygon(polygon):
    if type(polygon) == str:
        polygon = shapely.wkt.loads(polygon)

    plt.plot(*polygon.exterior.xy)
    plt.show()


import argparse
import glob
import os
import cv2
import copy
import numpy as np
from collections import deque
THRES_IOU = 0.0

def get_args():

	parser = argparse.ArgumentParser()
	parser.add_argument('--type', default=0, type=int)
	parser.add_argument('--video', default=3, type=int)
	parser.add_argument('--start', default=890, type=int)
	parser.add_argument('--end', default=1080, type=int)
	parser.add_argument('--object', default='pedestrian', type=str)
	parser.add_argument('--object_idx', default=0, type=int)
	parser.add_argument('--output_dir', default='/tmp2/frank840306/demo_video', type=str)

	return parser.parse_args()

def mid_point(bbox):
	x = (bbox[0] + bbox[2]) / 2
	y = (bbox[1] + bbox[3]) / 2
	return (x, y)

def iou(bbox1, bbox2):
	one_x = bbox1[0]
	one_y = bbox1[1]
	one_w = bbox1[2] - bbox1[0]
	one_h = bbox1[3] - bbox1[1]

	two_x = bbox2[0]
	two_y = bbox2[1]
	two_w = bbox2[2] - bbox2[0]
	two_h = bbox2[3] - bbox2[1]

	if((abs(one_x - two_x) < ((one_w + two_w) / 2.0)) and (abs(one_y - two_y) < ((one_h + two_h) / 2.0))):
		lu_x_inter = max((one_x - (one_w / 2.0)), (two_x - (two_w / 2.0)))
		lu_y_inter = min((one_y + (one_h / 2.0)), (two_y + (two_h / 2.0)))

		rd_x_inter = min((one_x + (one_w / 2.0)), (two_x + (two_w / 2.0)))
		rd_y_inter = max((one_y - (one_h / 2.0)), (two_y - (two_h / 2.0)))

		inter_w = abs(rd_x_inter - lu_x_inter)
		inter_h = abs(lu_y_inter - rd_y_inter)

		inter_square = inter_w * inter_h
		union_square = (one_w * one_h) + (two_w * two_h) - inter_square

		IOU = inter_square / union_square * 1.0
		#print("calcIOU:", IOU)
	else:
		IOU = 0
		#print("No intersection!")

	return IOU

def read_bbox(bbox_dir, cfg):
	bbox_type = ['pedestrian', 'bicycle']
	object_dict = {}
	for bt in bbox_type:
		if bt not in object_dict:
			object_dict[bt] = {}
		bbox_file = os.path.join(bbox_dir, 'comp4_det_demo_type_{}_{}.txt'.format(cfg['type'], bt))
		with open(bbox_file) as f:
			content = f.readlines()
		for line in content:
			fname, prob, xmin, ymin, xmax, ymax = line[:-1].split()
			frame = int(fname.split('_')[-1])
			if frame > cfg['fend'] or frame < cfg['fstart']:
				continue
			#if frame < 1080 and frame > 870:
			#	print(fname)
			if int(fname[13]) != cfg['video']:
				continue

			if fname not in object_dict[bt]:
				object_dict[bt][fname] = []
			#print(xmin, ymin, xmax, ymax)
			object_dict[bt][fname].append([float(xmin), float(ymin), float(xmax), float(ymax), float(prob)])
			#print(object_dict[bt][fname][-1])
	return object_dict

def trace(cfg):
	bbox_dir = os.path.join('../dataset', 'car_type_{}'.format(cfg['type']), 'results', 'demo_type_{}'.format(cfg['type']))

	points = []
	bbox = read_bbox(bbox_dir, cfg)

	#print(bbox['bicycle']['type_0_video_3_frame_870'])
	#print(bbox['bicycle']['type_0_video_3_frame_871'])
	#print(bbox['bicycle']['type_0_video_3_frame_872'])
	#print(bbox['bicycle']['type_0_video_3_frame_873'])
	#print(bbox['bicycle']['type_0_video_3_frame_874'])
	prev_bbox = bbox[cfg['object']]['type_{}_video_{}_frame_{}'.format(cfg['type'], cfg['video'], cfg['fstart'])][cfg['object_idx']] # 26
	print('start: ', prev_bbox)
	points.append(mid_point(prev_bbox))
	for fidx in range(cfg['fstart'] + 1, cfg['fend']):
		frame = 'type_{}_video_{}_frame_{}'.format(cfg['type'], cfg['video'], fidx)
		if frame in bbox[cfg['object']]:

			bboxs = bbox[cfg['object']][frame]
			# print(bboxs)
			best_score = 0
			best_bbox = None
			for bx in bboxs:
				score = iou(prev_bbox, bx)
				if score > best_score:
					best_bbox = copy.deepcopy(bx)
					best_score = score
			if best_bbox is None or best_score < THRES_IOU:
				# no suitable bbox, use the prev one
				best_bbox = prev_bbox

			prev_bbox = best_bbox
		print(mid_point(prev_bbox))
		points.append(mid_point(prev_bbox))
	return points

def output_video(cfg, points):
	img_dir = os.path.join('../dataset', 'car_type_{}'.format(cfg['type']), 'data', 'Images')
	# glob.glob(os.path.join(img_dir, 'type_{}_video_{}_frame_{}'.format(cfg['type'], cfg['video'])))
	pts = deque(maxlen=200)

	img = cv2.imread(os.path.join(img_dir, 'type_{}_video_{}_frame_0.png'.format(cfg['type'], cfg['video'])))
	h, w, c = img.shape
	print(img.shape)
	out = cv2.VideoWriter(os.path.join(cfg['output_dir'], 'demo_type_{}_video_{}_movement.mp4'.format(cfg['type'], cfg['video'])), 0x7634706d, 30, (w, h))
	for idx in range(len(points)):
		fidx = cfg['fstart'] + idx
		img_file = os.path.join(img_dir, 'type_{}_video_{}_frame_{}.png'.format(cfg['type'], cfg['video'], fidx))
		img = cv2.imread(img_file)

		pts.appendleft((int(points[idx][0]), int(points[idx][1])))
		for i in range(1, len(pts)):
			if pts[i - 1] is None or pts[i] is None:
				continue
			thickness = int(np.sqrt(32 / float(i + 1)) * 2.5)
			#print(thickness)
			cv2.line(img, pts[i - 1], pts[i], (0, 0, 255), thickness)
		out.write(img)
		print('write frame {} / {}'.format(idx, len(points)))
	out.release()

def main():
	args = get_args()
	cfg = {
		'type': args.type,
		'video': args.video,
		'fstart': args.start,
		'fend': args.end,
                'object': args.object,
                'object_idx': args.object_idx,
		'output_dir': args.output_dir
	}
	points = trace(cfg)

	# print('size: {}'.format(len(points)))
	# for i, p in enumerate(points):
	# 	print('frame: {}, ({}, {})'.format(i + 1, p[0], p[1]))
	output_video(cfg, points)
if __name__ == '__main__':
	main()


from datetime import datetime

from flask import Flask, render_template

import requests

app = Flask(__name__)

year = datetime.now().year

@app.route("/")
def home():
    return render_template("index.html", year=year)

# print()

@app.route("/guess/<name>")
def guess(name):
    gender = requests.get(f"https://api.genderize.io?name={name}").json()['gender']
    age = requests.get(f"https://api.agify.io?name={name}").json()['age']
    return render_template("guess.html", name=name.capitalize(), gender=gender.capitalize(), age=age)

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=8080)


from ee.clickhouse.system_status import system_status

def test_system_status(db):
    results = list(system_status())
    assert [row["key"] for row in results] == [
        "clickhouse_alive",
        "clickhouse_event_count",
        "clickhouse_event_count_last_month",
        "clickhouse_event_count_month_to_date",
        "clickhouse_disk_0_free_space",
        "clickhouse_disk_0_total_space",
        "clickhouse_table_sizes",
        "clickhouse_system_metrics",
    ]
    assert len(results[-2]["subrows"]["rows"]) > 0
    assert len(results[-1]["subrows"]["rows"]) > 0