from __future__ import print_function

import collections
import logging
import os
import subprocess
import textwrap

import drmr
import drmr.drm.base
import drmr.util

class Slurm(drmr.drm.base.DistributedResourceManager):
    name = 'Slurm'

    default_job_template = textwrap.dedent(
        """
        #!/bin/bash

        ####  Slurm preamble

        #SBATCH --export=ALL
        #SBATCH --job-name={{job_name}}
        {% if nodes %}
        #SBATCH --nodes={{nodes}}
        {% endif %}
        {% if node_properties %}
        #SBATCH --constraint="{{'&'.join(node_properties.split(','))}}"
        {% endif %}
        #SBATCH --cpus-per-task={{processors|default(1)}}
        {% if memory %}
        #SBATCH --mem={{memory|default('4000')}}
        {% else %}
        #SBATCH --mem-per-cpu={{processor_memory|default('4000')}}
        {% endif %}
        {% if time_limit %}
        #SBATCH --time={{time_limit}}
        {% endif %}
        {% if array_controls %}
        #SBATCH --output "{{control_directory}}/{{job_name}}_%A_%a_%j.out"
        {% else %}
        #SBATCH --output "{{control_directory}}/{{job_name}}_%j.out"
        {% endif %}
        {% if account %}
        #SBATCH --account={{account}}
        {% endif %}
        {% if destination %}
        #SBATCH --partition={{destination}}
        {% endif %}
        {% if email %}
        #SBATCH --mail-user={{email}}
        {% endif %}
        {% if mail_event_string %}
        #SBATCH --mail-type={{mail_event_string}}
        {% endif %}
        {% if dependencies %}
        #SBATCH --dependency={{resource_manager.make_dependency_string(dependencies)}}
        {% endif %}
        {% if working_directory %}
        #SBATCH --workdir={{working_directory}}
        {% endif %}
        {% if array_controls %}
        #SBATCH --array {{array_controls['array_index_min']|default(1)}}-{{array_controls['array_index_max']|default(1)}}{% if array_controls['array_concurrent_jobs'] %}%{{array_controls['array_concurrent_jobs']}}{% endif %}
        {% endif %}
        {% if raw_preamble %}
        {{raw_preamble}}
        {% endif %}

        ####  End Slurm preamble

        {% if notes %}
        ####  Notes
        {{notes}}
        {% endif %}

        {% if environment_setup %}
        ####  Environment setup
        {% for line in environment_setup %}
        {{line}}
        {% endfor %}
        {% endif %}

        ####  Commands

        {{command}}

        """
    ).lstrip()

    default_array_command_template = textwrap.dedent(
        """
        if [ "$SLURM_ARRAY_TASK_ID" = "{{index}}" ]; then
            {{command}}
        fi
        """
    )

    job_dependency_states = [
        'any',
        'notok',
        'ok',
    ]

    job_state_map = {
        'any': 'any',
        'notok': 'notok',
        'ok': 'ok',
        'start': '',
    }

    mail_event_map = {
        'BEGIN': 'BEGIN',
        'END': 'END',
        'FAIL': 'FAIL',
    }

    def delete_jobs(self, job_ids=None, job_name=None, job_owner=None, dry_run=False):
        logger = self.get_method_logger()

        if job_ids is None:
            job_ids = []

        targets = set(job_ids)
        targets.update(self.get_active_job_ids(job_ids, job_name, job_owner))

        if targets:
            if dry_run:
                logger.info(self.explain_job_deletion(targets, job_name, job_owner, dry_run))
            else:
                if logger.isEnabledFor(logging.DEBUG):
                    logger.debug(self.explain_job_deletion(targets, job_name, job_owner, dry_run))
                command = ['scancel'] + list(targets)
                try:
                    subprocess.check_call(command)
                except subprocess.CalledProcessError as e:
                    raise drmr.exceptions.DeletionError(e.returncode, e.cmd, e.output, targets)

    def get_active_job_ids(self, job_ids=None, job_name=None, job_owner=None):
        logger = self.get_method_logger()

        if job_ids is None:
            job_ids = []

        jobs = set([])

        command = [
            'squeue',
            '-r',
            '--format=%A,%j,%u',
            '--states=CONFIGURING,COMPLETING,PENDING,PREEMPTED,RUNNING,SUSPENDED'
        ]

        squeue_lines = self.capture_process_output(command).splitlines()
        if 1 < len(squeue_lines):
            squeue_lines = set(squeue_lines[1:])
            for job in squeue_lines:
                job_id, name, owner = job.split(',')
                if job_owner and owner != job_owner:
                    continue

                if job_name and job_name not in name:
                    continue

                if job_ids and job_id not in job_ids:
                    continue

                jobs.add(job_id)

        if jobs:
            logger.debug('Found {} active jobs'.format(len(jobs)))
        else:
            logger.debug('No active jobs found.')

        return jobs

    def is_installed(self):
        output = ''
        try:
            output = self.capture_process_output(['scontrol', 'version'])
        except:
            pass

        return 'slurm' in output

    def write_cancel_script(self, job_data, job_ids):
        logger = self.get_method_logger()
        logger.debug('Writing canceller script for {}'.format(job_data))

        self.set_control_directory(job_data)
        filename = drmr.util.absjoin(job_data['control_directory'], job_data['job_name'])
        if not os.path.exists(job_data['control_directory']):
            os.makedirs(job_data['control_directory'])
        with open(filename, 'w') as canceller:
            canceller.write('#!/bin/sh\n\n')
            for job in job_ids:
                canceller.write('scancel %s\n' % job)
            os.chmod(filename, 0o755)

    def make_dependency_string(self, dependencies):
        dependency_string = ''
        if dependencies:
            dependency_list = []
            if not isinstance(dependencies, collections.Mapping):
                raise ValueError('Job data does not contain a map under the "dependencies" key.')
            for state, job_ids in dependencies.items():
                if state not in self.job_dependency_states:
                    raise ValueError('Unsupported dependency state: %s' % state)

                dependency_list.append('after%s:%s' % (state, ':'.join(str(job_id) for job_id in job_ids)))
            dependency_string = ','.join(dependency_list)

        return dependency_string

    def set_mail_event_string(self, job_data):
        if job_data.get('mail_events'):
            job_data['mail_event_string'] = ','.join(
                sorted(
                    self.mail_event_map[event] for event in job_data['mail_events']
                )
            )

    def submit(self, job_filename, hold=False):
        if not self.is_installed():
            raise drmr.exceptions.ConfigurationError('{} is not installed or not usable.'.format(self.name))

        try:
            command = ['sbatch', '--parsable', job_filename]
            if hold:
                command.insert(1, '--hold')

            job_id = self.capture_process_output(command)
            return job_id.strip().split(';')[0]
        except subprocess.CalledProcessError as e:
            raise drmr.exceptions.SubmissionError(e.returncode, e.cmd, e.output)

    def validate_destination(self, destination):
        if not self.is_installed():
            raise drmr.exceptions.ConfigurationError('{} is not installed or not usable.'.format(self.name))

        valid = False
        try:
            command = ['scontrol', 'show', 'partition', destination]
            status = self.capture_process_output(command)
            if status.startswith('PartitionName={}'.format(destination)):
                valid = True
        except:
            pass

        return valid


"""
"""

from random import sample
from abc import ABC, abstractmethod
from typing import Optional, Tuple, Union, List, Any, Sequence, NoReturn

import numpy as np
import torch
from torch import Tensor

from .base import Augmenter
from .baseline_wander import BaselineWanderAugmenter
from .label_smooth import LabelSmooth
from .mixup import Mixup
from .random_flip import RandomFlip
from .random_masking import RandomMasking
from .random_renormalize import RandomRenormalize
from .stretch_compress import StretchCompress
from ..utils.misc import default_class_repr

__all__ = ["AugmenterManager",]

class AugmenterManager(torch.nn.Module):
    """

    The `Module` to manage the augmenters

    Examples
    --------
    ```python
    import torch
    from easydict import EasyDict as ED
    from torch_ecg.augmenters import AugmenterManager

    config = ED(
        random=False,
        fs=500,
        baseline_wander={},
        label_smooth={},
        mixup={},
        random_flip={},
        random_masking={},
        random_renormalize={},
        stretch_compress={},
    )
    am = AugmenterManager.from_config(config)
    sig, label, mask = torch.rand(2,12,5000), torch.rand(2,26), torch.rand(2,5000,1)
    sig, label, mask = am(sig, label, mask)
    ```
    """
    __name__ = "AugmenterManager"

    def __init__(self, *augs:Optional[Tuple[Augmenter,...]], random:bool=False) -> NoReturn:
        """ finished, checked,

        Parameters
        ----------
        aug: tuple of `Augmenter`, optional,
            the augmenters to be added to the manager
        random: bool, default False,
            whether to apply the augmenters in random order
        """
        super().__init__()
        self.random = random
        self._augmenters = list(augs)

    def _add_baseline_wander(self, **config:dict) -> NoReturn:
        self._augmenters.append(BaselineWanderAugmenter(**config))

    def _add_label_smooth(self, **config:dict) -> NoReturn:
        self._augmenters.append(LabelSmooth(**config))

    def _add_mixup(self, **config:dict) -> NoReturn:
        self._augmenters.append(Mixup(**config))

    def _add_random_flip(self, **config:dict) -> NoReturn:
        self._augmenters.append(RandomFlip(**config))

    def _add_random_masking(self, **config:dict) -> NoReturn:
        self._augmenters.append(RandomMasking(**config))

    def _add_random_renormalize(self, **config:dict) -> NoReturn:
        self._augmenters.append(RandomRenormalize(**config))

    def _add_stretch_compress(self, **config:dict) -> NoReturn:
        self._augmenters.append(StretchCompress(**config))

    def forward(self, sig:Tensor, label:Optional[Tensor], *extra_tensors:Sequence[Tensor], **kwargs:Any) -> Union[Tensor,Tuple[Tensor]]:
        """ finished, checked,

        Parameters
        ----------
        sig: Tensor,
            the ECGs to be augmented, of shape (batch, lead, siglen)
        label: Tensor, optional,
            labels of the ECGs
        extra_tensors: Tensor(s), optional,
            extra tensors to be augmented, e.g. masks for custom loss functions, etc.
        kwargs: keyword arguments

        Returns
        -------
        Tensor(s), the augmented ECGs, labels, and optional extra tensors
        """
        if len(self.augmenters) == 0:
            # raise ValueError("No augmenters added to the manager.")
            return (sig, label, *extra_tensors)
        ordering = list(range(len(self.augmenters)))
        if self.random:
            ordering = sample(ordering, len(ordering))
        for idx in ordering:
            sig, label, *extra_tensors = self.augmenters[idx](sig, label, *extra_tensors, **kwargs)
        return (sig, label, *extra_tensors)

    @property
    def augmenters(self) -> List[Augmenter]:
        return self._augmenters

    def extra_repr(self) -> str:
        indent = 4*" "
        s = f"augmenters = [\n{indent}" + \
            f",\n{2*indent}".join(default_class_repr(aug, depth=2) for aug in self.augmenters) + \
            f"{indent}\n]"
        return s

    @classmethod
    def from_config(cls, config:dict) -> "AugmenterManager":
        """ finished, checked,

        Parameters
        ----------
        config: dict,
            the configuration of the augmenters,
            better to be an `OrderedDict`

        Returns
        -------
        am: AugmenterManager,
            a new instance of `AugmenterManager`
        """
        am = cls(random=config.get("random", False))
        _mapping = {
            "baseline_wander": am._add_baseline_wander,
            "label_smooth": am._add_label_smooth,
            "mixup": am._add_mixup,
            "random_flip": am._add_random_flip,
            "random_masking": am._add_random_masking,
            "random_renormalize": am._add_random_renormalize,
            "stretch_compress": am._add_stretch_compress,
        }
        for aug_name, aug_config in config.items():
            if aug_name in ["fs", "random",]:
                continue
            elif aug_name in _mapping and isinstance(aug_config, dict):
                _mapping[aug_name](fs=config["fs"], **aug_config)
            else:
                # just ignore the other items
                pass
                # raise ValueError(f"Unknown augmenter name: {aug_name}")
        return am

    def rearrange(self, new_ordering:List[str]) -> NoReturn:
        """ finished, checked,

        Parameters
        ----------
        new_ordering: list of str,
            the list of augmenter names in the new order
        """
        _mapping = {
            "".join([w.capitalize() for w in k.split("_")]): k \
                for k in "label_smooth,mixup,random_flip,random_masking,random_renormalize,stretch_compress".split(",")
        }
        _mapping.update({"BaselineWanderAugmenter": "baseline_wander"})
        for k in new_ordering:
            if k not in _mapping:
                _mapping.update({k: k})
        self._augmenters.sort(key=lambda aug: new_ordering.index(_mapping[aug.__name__]))


class TreeNode:

    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

    def bfs(self) -> list[any]:

        levels = []

        def helper(node, level: int):
            if len(levels) == level:
                levels.append([])
            if node == None:
                levels[level].append('null')
            else:
                levels[level].append(node.val)
                helper(node.left,level+1)
                helper(node.right,level+1)

        helper(self,0)
        return levels

    def equal(self, A) -> bool:
        return self.bfs() == A.bfs()


# Faça um Programa para uma loja de tintas. O programa deverá pedir o tamanho em metros quadrados da área a ser pintada. Considere que a cobertura da tinta é de 1 litro para cada 6 metros quadrados e que a tinta é vendida em latas de 18 litros, que custam R$ 80,00 ou em galões de 3,6 litros, que custam R$ 25,00. Informe ao usuário as quantidades de tinta a serem compradas e os respectivos preços em 3 situações:
# comprar apenas latas de 18 litros;
# comprar apenas galões de 3,6 litros;
# misturar latas e galões, de forma que o desperdício de tinta seja menor. Acrescente 10% de folga e sempre arredonde os valores para cima, isto é, considere latas cheias.

area = float(input("Informe a área a ser pintada (m²): "))

litrosNecessarios = area / 6
# latas18 = litrosNecessarios / 18
# latas3_6 = litrosNecessarios / 3.6

if ( litrosNecessarios % 18 == 0):
    latas = litrosNecessarios / 18
    precoTotal = latas * 80
    print("latas de tinta a serem compradas: " + str(round(latas)) + " lata(s) de 18L\n + Preço Total: "+str(round(precoTotal)))

elif(litrosNecessarios % 3.6 == 0):
    latas = litrosNecessarios / 3.6
    precoTotal = latas * 25
    print("latas de tinta a serem compradas: " + str(round(latas)) + " lata(s) de 3,6L\n + Preço Total: "+str(round(precoTotal)))

else:
    if(litrosNecessarios > 18):
        latas18 = litrosNecessarios // 18
        resto = litrosNecessarios % 18

        preco18 = latas18 * 80

        latas3_6 = litrosNecessarios / 3.6
        preco3_6 = latas3_6 * 25

        precoTotal = preco18 + preco3_6;

        print("latas de tinta a serem compradas: " + str(round(latas18)) + " latas de 18L e " + str(round(latas3_6)) + " latas de 3.6L \nPreço Total: "+str(round(precoTotal)))


import os
from subprocess import TimeoutExpired
from time import sleep
import unittest

from METNO.HPC import HPC, StatusFile, QJobStatus
from METNO.PBSQueue import PBSQueue
from METNO.SSHConnection import SSHConnection

class Vilje(HPC):
    '''
    Implementation of a HPC machine for vilje.notur.ntnu.no
    '''

    def __init__(self):
        '''
        Constructor
        '''
        connection = SSHConnection(username="forecast", machine="vilje.hpc.ntnu.no", port=22)
        queue = PBSQueue()
        super().__init__(connection, queue)

class TestVilje(unittest.TestCase):
    '''tests for vilje, only working when having an existing forecast account on vilje'''
    def setUp(self):
        unittest.TestCase.setUp(self)
        self.vilje = HPC.by_name("vilje")
        self.rdir = "/work/forecast/metno_hpc_test"
        self.testFiles = ["script.job", "status"]

    def tearDown(self):
        unittest.TestCase.tearDown(self)
        for f in self.testFiles:
            if (os.path.exists(f)):
                os.unlink(f)

    def test_connect(self):
        (out, error, retval) = self.vilje.syscall("echo", ["5"])
        self.assertEqual(retval, 0, "command succeeded")
        self.assertEqual(int(out), 5, "command output correct")

    def test_timeout(self):
        with self.assertRaises(TimeoutExpired):
            self.vilje.syscall("sleep", ["5"], timeout=1)

    def test_full(self):
        status_file = os.path.join(self.rdir, "status")
        self.vilje.syscall("rm", ["-r", self.rdir])
        self.vilje.syscall("mkdir", [self.rdir])
        with open(self.testFiles[0], "w") as fh:
            fh.write('''
#! /bin/bash
#PBS -l select=1:ncpus=1:mpiprocs=1:mem=1GB
#PBS -lwalltime=00:00:10
#PBS -W umask=0022
#PBS -A mipa01kl

sleep 8

echo "finished" > {}
            '''.format(status_file))
        self.vilje.put_files([self.testFiles[0]], self.rdir)
        qjob = self.vilje.submit_job(os.path.join(self.rdir, self.testFiles[0]), [])
        self.assertIsNotNone(qjob, "job submitted")
        qjob.status_file = StatusFile(status_file, "finished")
        count = 100
        status = self.vilje.get_status(qjob)
        while not (status == QJobStatus.finished or status == QJobStatus.failed):
            sleep(5)
            count -= 1
            if count == 0:
                break
            status = self.vilje.get_status(qjob)

        self.assertEqual(status, QJobStatus.finished, "remote job successfully running")

        self.vilje.get_files([status_file])
        self.assertTrue(os.path.exists("status"))
        with open("status", "r") as sh:
            content = sh.read()
            self.assertEqual(content, "finished\n")

        self.vilje.syscall("rm", ["-r", self.rdir])

if __name__ == "__main__":
    unittest.main()


import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-0.1, 1.1, 0.01)

def f(x):
    y = x**2
    return y

def f2(x):
    y = x
    return y

def ReglaSimpson(a, b, n):
    h = (b - a) / n
    x = np.linspace(a, b, n+1)
    y = f(x)

    integral = 0
    for i in range(0, n-1, 2):
        integral += h/3 * (y[i] + 4*y[i+1] + y[i+2])

    return np.pi * integral

a = 0
b = 2
n = 4000

solucion = ReglaSimpson(a, b, n)

fig, (ax3,ax1,ax2) = plt.subplots(1, 3, figsize=(12, 4))

ax1.plot(x, f(x))
ax1.plot(x, f2(x))
ax1.plot(0, f(0), 'ro')
ax1.plot(1, f2(1), 'ro')

ax1.set_title(u"$\ Gráfica\ de\ los\ lugares\ geométricos$")
ax1.set_xlabel('x')
ax1.set_ylabel('y')

ax1.axhline(0, color='black', linewidth=0.5)
ax1.axvline(0, color='black', linewidth=0.5)
footer_text= f"$\ El\ volumen\ del\ sólido\ de\ revolución$\n $es\ aproximadamente: {solucion}$"
ax1.text(-0.75, -0.3, footer_text, transform=ax2.transAxes,fontsize=12, ha='center')

ax2.fill_betweenx(f(x), 0, x, color='blue', alpha=0.5)
ax2.fill_betweenx(f(x), 0, -x, color='blue', alpha=0.5)
ax2.set_xlabel('Eje X')
ax2.set_ylabel('Eje Y')
ax2.set_title(u"$\ Sólido\ de\ Revolución:\ y\ =\ x^2\ (alrededor\ del\ eje\ Y)$")

ax1.axhline(0, color='black', linewidth=0.5)
ax1.axvline(0, color='black', linewidth=0.5)

ax3.axis('off')

theta = np.linspace(0, 2.*np.pi, 100)
r, th = np.meshgrid(x, theta)
X = r * np.cos(th)
Y = r * np.sin(th)
Z = r**2

ax = fig.add_subplot(131, projection='3d')
ax.plot_surface(X, Y, Z, color='b')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title(u"$\ Sólido\ de\ revolución$\n $generado\ por\ y\ =\ x^2$")

plt.tight_layout()

plt.show()


# 신규 아이디 추천

import re

def solution(new_id):
  answer = []
  for i in new_id:
    upper = re.compile('[A-Z_]')
    upperCheck = upper.match(i)
    if upperCheck != None:
      answer.append(i.lower())
      continue
    sChar = re.compile('[A-Za-z0-9\-_\.]')
    sCharCheck = sChar.match(i)
    if sCharCheck == None:
      continue
    else:
      answer.append(i)
  answer = ''.join(answer)
  while '..' in answer:
    answer = answer.replace('..','.')
  if answer[0] == '.':
    answer = answer[1:]
    if len(answer) <= 1:
      answer = 'a'
  if answer[-1] == '.':
    answer = answer[0:len(answer)-1]
  if len(answer) == 0:
    answer = 'a'
  if len(answer) >= 16:
    answer = answer[0:15]
    if answer[-1] == '.':
      answer = answer[0:14]
  if len(answer) <= 3:
    answer = answer + answer[-1] * (3 - len(answer))
  return answer

# print(solution('...!@BaT#*..y.abcdefghijklm'))
# print(solution("z-+.^."))
# print(solution("=.="))
# print(solution("123_.def"))
# print(solution("abcdefghijklmn.p"))

# 다른 사람 풀이 1 -> 이렇게 풀고 싶었는데 정규식을 몰라서...흑흑....
import re

def solution(new_id):
    st = new_id
    st = st.lower() # lower()는 소문자에 해당하는 글자'만' 바꿔준다.
    st = re.sub('[^a-z0-9\-_.]', '', st) # re.sub（정규 표현식, 치환 문자, 대상 문자열）
    st = re.sub('\.+', '.', st) # .여러개를 하나만 있게 변경
    st = re.sub('^[.]|[.]$', '', st) # 시작과 끝 검사
    st = 'a' if len(st) == 0 else st[:15] # 빈문자열일 때 a와 15자로 자르기를 한번에 했다.
    st = re.sub('^[.]|[.]$', '', st) # 자르고 한번 더 검사
    st = st if len(st) > 2 else st + "".join([st[-1] for i in range(3-len(st))])
    return st

# 다른 사람 풀이 2
from re import sub # 구조분해할당처럼 이렇게 쓸 수도 있다.

def solution(new_id):
    new_id = new_id.lower()
    new_id = sub("[^a-z0-9-_.]", "", new_id)
    new_id = sub("\.+", ".", new_id)
    new_id = sub("(^\.|\.$)", "", new_id)
    new_id = new_id if new_id else "a"
    new_id = sub("\.$", "", new_id[:15])
    new_id = new_id if len(new_id) > 3 else new_id + new_id[-1] * (3 - len(new_id))
    return new_id

신규 아이디 추천.py


import pygame, os, sys
from pygame.locals import *
pygame.init()
main_clock = pygame.time.Clock()
WIDTH, HEIGHT = 400 * 2, 300 * 2
BG_COLOUR = (124, 124, 124)
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
font = pygame.font.SysFont('Arial', 20, True)
block_list = os.listdir('Assets/block')
block_database = {}
for block in block_list:
    img = pygame.image.load(f'Assets/block/{block}')
    block_database[block] = img.copy()

block_map = {}

def Text_to_List(Text, Divider ,is_int=False):
    List = []
    Current = ''
    for char in Text:
        if char != Divider:
            Current += char
        else:
            if is_int == True:
                try:
                    List.append(int(Current))
                except:
                    List.append(Current)
            else:
                List.append(Current)
            Current = ''
    return List


from Point import Point
from Vector import Vector

class Triangle:

    def __init__(self, a: Point, b: Point, c: Point):
        self.a: a
        self.b: b
        self.c: c

        abVector = b - a
        bcVector = c - b

        self.normalVector = abVector.crossProduct(bcVector).normalize()

    def normal(self) -> Vector:
        return self.normalVector


#This file includes the core function for text detection in a given image

import sys
import os
import argparse
import numpy as np
import re
import cv2
import time
import logging
from ctypes import *
from PIL import Image, ImageEnhance
from itertools import takewhile
import xml.etree.cElementTree as ET

#Define logging level
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# create XML file from text by xml.etree.cElementTree
def xml_create_unit(text_block, duration, stime, keywords, filename):
    hits = ET.Element("HITLIST")

    for i, text in enumerate(text_block):

        tb = (' ').join(ks for ks in text).strip()

        wordsli = re.split('[ )(":.\[\];/,\n]', tb) # split sentence into words
        if '' in wordsli:
            wordsli.remove('')
        begrec_all=[]
        wordrec_all=[]
        temprec_all=[]
        lenrec_all=[]
        # match with keywords from file
        for wordnum in xrange(len(wordsli),0,-1):
            for indword in xrange(len(wordsli)-wordnum+1): # check multiple words unit in a line
                word =u''
                for s in xrange(wordnum):
                    word += (u' ' + wordsli[indword+s])
                word_without_acc = unicodedata.normalize('NFKD', word.strip()).encode('ASCII','ignore') # convert characters with French accent to normal English chracters
                word=word.strip().encode('utf8')
                flag2=0
                # French accent matching
                if (word.lower() in keywords)>0:#np.sum([find_substring(word.lower(), s) for s in keywords]) > 0:# French words matching
                    pla = np.where(keywords==word.lower())[0]
                    temprec_all = temprec_all + pla.tolist()
                    begrec_all = begrec_all+ [indword]*len(pla)
                    wordrec_all = wordrec_all+ [word]*len(pla)
                    lenrec_all = lenrec_all + [wordnum]*len(pla)
                    flag2=1
                # matching without accent
                if flag2==0 and (word_without_acc.lower() in keywords)>0:#np.sum([find_substring(word.lower(), s) for s in keywords]) > 0:# converted English words matching
                    pla = np.where(keywords==word_without_acc.lower())[0]
                    temprec_all = temprec_all + pla.tolist()
                    begrec_all = begrec_all+ [indword]*len(pla)
                    wordrec_all = wordrec_all+ [word]*len(pla)
                    lenrec_all = lenrec_all + [wordnum]*len(pla)

        # save to xml structure
        if len(temprec_all)==0:#if no matched keywords with Keywords list
            hit = ET.SubElement(hits,"HIT")# {0}".format(count))
            ET.SubElement(hit, "frame", stime="{0}".format(stime[i]), dur="{0}".format(duration[i])).text = "{0} second".format(stime[i])
            for indw, word in enumerate(wordsli):
                if word=='':
                        continue
                word=word.strip()
                ET.SubElement(hit, "Word", stime="{0}".format(stime[i]), dur="{0}".format(duration[i])).text = "{0}".format(word)
        else: # if there is matched words to keywords
            count=1
            while len(temprec_all) > 0:# multiple matched words or word segments
                flag = 1
                wordrec = wordrec_all.pop()
                temprec = temprec_all.pop()
                begrec = begrec_all.pop()
                lenrec = lenrec_all.pop()

                hit = ET.SubElement(hits,"HIT", DUP="{0}".format(count))
                ET.SubElement(hit, "frame", stime="{0}".format(stime[i]), dur="{0}".format(duration[i])).text = "{0} second".format(stime[i])
                for indw, word in enumerate(wordsli):# if there is multiple mathced keywords in a frame, output for multiple times
                    if word=='':
                        continue
                    word=word.strip()

                    if indw==begrec and flag == 1:#np.sum([find_substring(word.lower(), s) for s in keywords]) > 0:#
                        ET.SubElement(hit, "Word", keyword="{0}".format(wordrec),  classement="{0}".format(keywords[temprec,1]), nature="{0}".format(keywords[temprec,0]), stime="{0}".format(stime[i]), dur="{0}".format(duration[i])).text = "{0}".format(wordrec)
                        flag=2
                    elif flag==2 and begrec+lenrec>indw>begrec :
                        pass
                    else:
                        ET.SubElement(hit, "Word", stime="{0}".format(stime[i]), dur="{0}".format(duration[i])).text = "{0}".format(word)

                count +=1
    tree = ET.ElementTree(hits)
    tree.write(filename, xml_declaration=True, encoding='UTF-8')

def text_recognition(img, Debug, debugPath,i):
    #Preprocessing to enhance text structure in the image
    #Image enhancement using PIL
    image = Image.fromarray(img)
    contrast = ImageEnhance.Contrast(image)
    contrasted = contrast.enhance(1)

    #contrasted.show()
    img = np.array(contrasted)
    (rows, cols) = (img.shape[0], img.shape[1])

    #sharpen the image
    kernel = np.zeros((9, 9), np.float32)
    kernel[4, 4] = 2.0  # Identity, times two!
    boxFilter = np.ones((9, 9), np.float32) / 81.0
    kernel = kernel - boxFilter
    img = cv2.filter2D(img, -1, kernel)

    # Load the dll
    mydll = cdll.LoadLibrary("TextMSER/libText.so")
    #mydll.text_recognition.restype = c_char_p
    mydll.text_recognition(img.ctypes.data_as(POINTER(c_ubyte)), rows, cols,i, Debug, debugPath, 0)

def text_detect_image(img, Debug):

    pathname = os.path.dirname(os.path.abspath(sys.argv[0]))
    # for visualization
    vis = img.copy()
    # Extract channels to be processed individually
    channels = cv2.text.computeNMChannels(img)
    # Append negative channels to detect ER- (bright regions over dark background)
    cn = len(channels) - 1
    for c in range(0, cn):
        channels.append((255 - channels[c]))

    # Apply the default cascade classifier to each independent channel (could be done in parallel)
    #print("Extracting Class Specific Extremal Regions from " + str(len(channels)) + " channels ...")
    #print("    (...) this may take a while (...)")
    for channel in channels:
        erc1 = cv2.text.loadClassifierNM1(pathname + '/trained_classifierNM1.xml')
        er1 = cv2.text.createERFilterNM1(erc1, 8, 0.00015, 0.13, 0.2, True, 0.1)#16

        erc2 = cv2.text.loadClassifierNM2(pathname + '/trained_classifierNM2.xml')
        er2 = cv2.text.createERFilterNM2(erc2, 0.5)

        regions = cv2.text.detectRegions(channel, er1, er2)
        #rects = cv2.text.erGrouping(img, channel, [r.tolist() for r in regions])
        rects, regions_groupes = cv2.text.erGrouping(img,channel,[x.tolist() for x in regions], cv2.text.ERGROUPING_ORIENTATION_ANY,pathname + '/trained_classifier_erGrouping.xml',0.5)
        #Visualization
        for r in range(0,np.shape(rects)[0]):
            rect = rects[r]
            cv2.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (0, 0, 0), 2)
            cv2.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (255, 0, 255), 1)

    #Visualization
    if Debug:
        cv2.imshow("Text detection result", vis)
        cv2.waitKey(0)

    #Text Recognition OCR
    out_img=img.copy()
    out_img_detection = img.copy()
    out_img_segmentation = np.zeros(img.shape[0], img.shape[1], np.uint8)
    scale_img = 600/float(img.shape[0])
    scale_font = (float)(2-scale_img)/1.4

    for i in xrange(0,np.shape(rects)[0]):
        rect = rects[i]
        cv2.rectangle(out_img_detection, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (255, 0, 255), 3 )
        #group_img = np.zeros(img.shape[0], img.shape[1], np.uint8)

    return rects

#the main function if used standalone
def main():
    #Read input arguments
    parser = argparse.ArgumentParser(description='Detect and recognize the text present in the input image')
    parser.add_argument("-i", "--input", help="input image file")
    parser.add_argument("-o", "--outputpath", help="the path to save the output files")
    parser.add_argument("-n", "--outputname", help="the name to save the output files")
    parser.add_argument("-d", "--debug", help="Debug mode")

    args = parser.parse_args()

    #Check the input arguments
    if not os.path.isfile(args.input):
        logging.error('---input image file does not exist---')
        sys.exit(1)

    outputpath = args.outputpath
    if outputpath != '' and not os.path.exists(outputpath):
        os.makedirs(outputpath)

    img = cv2.imread(args.input)
    (rows, cols)= (img.shape[0], img.shape[1])
    Debug = int(args.debug)

    starttime = time.time()
    text_recognition(img, Debug,0)
    #text_detect_image(img,Debug)
    endtime = time.time()

    if Debug:
        logging.debug('Text zone detection and groupping time: ' + str(endtime - starttime) +'seconds')
    sys.exit(0)

if __name__ == "__main__":

    main()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from keras.layers import *
from keras.models import Model
from keras.callbacks import EarlyStopping, TensorBoard
from keras import backend as K

import numpy as np
import matplotlib.pyplot as plt
import argparse
import os

# reparameterization trick
# instead of sampling from Q(z|X), sample eps = N(0,I)
# then z = z_mean + sqrt(var)*eps
from showcase.models import AutoencoderModel
from showcase.image_sequence import create_rgbdseq_from_files

def plot_results(models,
                 data,
                 batch_size=128,
                 model_name="vae_mnist"):
    """Plots labels and MNIST digits as function of 2-dim latent vector

    # Arguments:
        models (tuple): encoder and decoder models
        data (tuple): test data and label
        batch_size (int): prediction batch size
        model_name (string): which model is using this function
    """

    encoder, decoder = models
    x_test, y_test = data
    os.makedirs(model_name, exist_ok=True)

    filename = os.path.join(model_name, "vae_mean.png")
    # display a 2D plot of the digit classes in the latent space
    z_mean, _, _ = encoder.predict(x_test,
                                   batch_size=batch_size)
    plt.figure(figsize=(12, 10))
    plt.scatter(z_mean[:, 0], z_mean[:, 1], c=y_test)
    plt.colorbar()
    plt.xlabel("z[0]")
    plt.ylabel("z[1]")
    plt.savefig(filename)
    plt.show()

    return
    filename = os.path.join(model_name, "digits_over_latent.png")
    # display a 30x30 2D manifold of digits
    n = 30
    digit_size = 28
    figure = np.zeros((digit_size * n, digit_size * n))
    # linearly spaced coordinates corresponding to the 2D plot
    # of digit classes in the latent space
    grid_x = np.linspace(-4, 4, n)
    grid_y = np.linspace(-4, 4, n)[::-1]

    for i, yi in enumerate(grid_y):
        for j, xi in enumerate(grid_x):
            z_sample = np.array([[xi, yi]])
            x_decoded = decoder.predict(z_sample)
            digit = x_decoded[0].reshape(digit_size, digit_size)
            figure[i * digit_size: (i + 1) * digit_size,
                   j * digit_size: (j + 1) * digit_size] = digit

    plt.figure(figsize=(10, 10))
    start_range = digit_size // 2
    end_range = n * digit_size + start_range + 1
    pixel_range = np.arange(start_range, end_range, digit_size)
    sample_range_x = np.round(grid_x, 1)
    sample_range_y = np.round(grid_y, 1)
    plt.xticks(pixel_range, sample_range_x)
    plt.yticks(pixel_range, sample_range_y)
    plt.xlabel("z[0]")
    plt.ylabel("z[1]")
    plt.imshow(figure, cmap='Greys_r')
    plt.savefig(filename)
    plt.show()

def plot_history(history):
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()

# data set
image_height = 96
image_width = 128
n_chans = 4
batch_size = 128
input_shape = (image_height, image_width, n_chans)

train_gen = create_rgbdseq_from_files(glob_pattern="data/*.npz", batch_size=batch_size, is_validation=False)
val_gen = create_rgbdseq_from_files(glob_pattern="data/*.npz", batch_size=batch_size, is_validation=True)

x_train = np.vstack(filter(lambda x: isinstance(x, bool)==False, [train_gen[i] for i in range(600)]))
x_test = np.vstack(filter(lambda x: isinstance(x, bool)==False, [val_gen[i] for i in range(80)]))

model = AutoencoderModel(input_shape)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-w", "--weights", help="Load h5 model trained weights")
    parser.add_argument("-e", "--epochs", default=25)

    args = parser.parse_args()

    if args.weights:
        model.load_model(args.weights)
    else:
        # train the autoencoder
        hist = model.fit(x_train, x_test)
        model.save_model('autoenc_cnn_rgbd_180526.h5')
        plot_history(hist)

    #plot_results(models, data, batch_size=batch_size, model_name="vae_cnn")


"""
    fabrication du dictionnaire "dico_contrepet.txt" à partir du fichier "lexique-grammalecte-fr-v7.0.txt"
    https://grammalecte.net/
    2 colonnes : mot et phonétique
"""

tab_lignes = []

nom_fichier_entree = "lexique-grammalecte-fr-v7.0.txt"
nom_fichier_sortie = "dico_contrepet.txt"

with open(nom_fichier_entree,mode = 'r' ,encoding = 'utf-8') as f :

    datas = f.readlines()		# !!! bonne pratique : le close() est fait automatiquement à la fin du with
    for data in datas:
        tab_data = data.split("\t")
        # élimination des enregistrements contenant des lettres pourries
        flag = True
        mot = tab_data[2]

        lettres_pourries = '₀₁₂₃₄₅₆₇₈₉0123456789$(*,.:?Δαεηικμνξπυφχ‰€ℓØ¿ᵉˢΩω'

        for car in lettres_pourries:
                if car in mot:
                    flag = False
                    continue

        if flag :
            if len(tab_data) < 6:
                continue
            mot = mot.replace("œ","oe")         # pour faciliter le tri et les traitements ultérieurs
            mot = mot.replace("Œ","OE")
            #æ
            phonetique = tab_data[5]   #.replace("2","eu")   # pour faciliter la recherche de contrepèteries
            if phonetique.strip():
                ligne = mot+" "+phonetique
                tab_lignes.append(ligne)

# tri du tableau de ligne par ordre alphabétique
tab_lignes.sort()

# écriture du fichier
f_w = open(nom_fichier_sortie,mode = 'w' ,encoding = 'utf-8')
for ligne in tab_lignes :
    f_w.write(ligne+"\n")
f_w.close()


import qibo
from qibo import gates
from qibo.models import Circuit
import numpy as np
from src.config import logger
from qilimanjaroq.circuit import RemoteCircuit

class TestQibo():

    def execute_circuit(self) -> dict:
        qibo.set_backend('numpy')
        # Construct the circuit
        c = Circuit(2)
        # Add some gates
        c.add(gates.H(0))
        c.add(gates.H(1))
        # Define an initial state (optional - default initial state is |00>)
        initial_state = np.ones(4) / 2.0
        # Execute the circuit and obtain the final state
        # result = c(initial_state)  # c.execute(initial_state) also works
        result = c.execute(initial_state)
        print(result)
        print(result.state())
        logger.info(f"Result state: {result.state()}")
        # print(c.summary())
        ts = result.state().tostring()
        print(np.fromstring(ts, dtype=int))
        return {"success": "OK"}

    def execute_qili_circuit(self) -> dict:
        qibo.set_backend('qilimanjaroq')
        # Construct the circuit
        c = RemoteCircuit(2)
        # Add some gates
        c.add(gates.H(0))
        c.add(gates.H(1))
        result = c.local_execute()

        print(result)
        print(result.state())
        logger.info(f"Result state: {result.state()}")
        # print(c.summary())
        ts = result.state().tostring()
        print(np.fromstring(ts, dtype=int))
        return {"success": "QILI OK!!!"}


import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host="0.0.0.0"
port=1234
s.connect((host,port))
#server, addr=s.accept()
key=''

import keyboard

while True:
    if keyboard.is_pressed('a'):
        print("You pressed a arrow")
        key='a'
        s.send(bytes("{}\n".format(key), "utf-8"))
        break
    if keyboard.is_pressed('s'):
        print("You pressed s arrow")
        key = 's'
        s.send(bytes("{}\n".format(key), "utf-8"))
        break
    if keyboard.is_pressed('w'):
        print("You pressed w arrow")
        key = 'w'
        s.send(bytes("{}\n".format(key), "utf-8"))
        break
    if keyboard.is_pressed('d'):
        print("You pressed d arrow")
        key = 'd'
        s.send(bytes("{}\n".format(key), "utf-8"))
        break

# display client address

# send message to the client after
# encoding into binary string

#server.send(key)
#server.close()
#if pyautogui.click()=='left':
   # print("got here")

#if pyautogui.keyDown('left'):
    #print("here")

#print(ord('a'))
s.close()


# -*- coding: utf-8 -*-
"""
Created on Sat May  8 16:30:02 2021

@author: USER
"""

from tkinter import *

tk=Tk()#用tk當視窗Tk物件
canvas=Canvas(tk,width=404,height=404)
canvas.pack()
canvas.create_rectangle(5, 5, 404, 404, outline='blue',width=3)#建立一個正方形
x1,y1,x2,y2=[],[],[],[]#建立空串列
x3,y3,x4,y4=[],[],[],[]
for i in range(21):#形成21個新座標
    i=i*20
    x1.append(5)
    y1.append(5+i)
    x2.append(400)
    y2.append(5+i)
    x3.append(5+i)
    y3.append(5)
    x4.append(5+i)
    y4.append(400)
for i in range(21):#形成21個長方形
    canvas.create_rectangle(x1[i], y1[i], x2[i], y2[i], outline='blue',width=3)
    canvas.create_rectangle(x3[i], y3[i], x4[i], y4[i], outline='blue',width=3)
mainloop()


"""

    Nearly identical to Google challenge yesterday.

    Strategy: split string by \n
    Count tabs to determine depth.
    Use stack to maintain current prefix size
    If len(current file) + current prefix > max_len, max_len is updated
    return max_len

"""

# def test():
#     test_is_file()
#     test_depth()
#     # test_longest_path()

# def test_is_file():
#     s = Solution()
#     assert s.is_file('abc.def')
#     assert not s.is_file('\t\t\t\tabc')

# def test_depth():
#     s = Solution()
#     assert s.depth('abc.jpeg') == 0
#     assert s.depth('\t\t\t\tabc') == 4

class Solution(object):
    def is_file(self,item): return '.' in item

    def depth(self, item):
        curr_depth = 0
        for c in item:
            if c != '\t': return curr_depth
            curr_depth += 1

    def lengthLongestPath(self, input):
        """
        :type input: str
        :rtype: int
        """
        prefix_stack = [0]
        current_depth = 0
        max_length = 0
        file_tree = input.split('\n')
        for item in file_tree:
            item_depth = self.depth(item)

            while item_depth < current_depth:
                current_depth -= 1
                prefix_stack.pop()

            path_length = len(item) - item_depth + prefix_stack[-1]

            if self.is_file(item) and path_length > max_length:
                max_length = path_length
            else:
                prefix_stack.append(path_length + 1)
                current_depth += 1

        return max_length

# test()


import docker
import subprocess
import logging

client = docker.from_env()

IMAGE_CACHE = {}

logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)

class Container(object):

    def __init__(self, attrs):
        self.name = attrs["Name"].lstrip("/")
        self.image_sha256 = attrs["Image"]
        self.image = attrs["Config"]["Image"]

        self.auto_update_enabled = False
        if "patcher/auto-update" in attrs["Config"]["Labels"]:
            self.auto_update_enabled = attrs["Config"]["Labels"]["patcher/auto-update"].lower() in ["true"]

        self.systemd_name = self.name
        if "patcher/systemd-name" in attrs["Config"]["Labels"]:
            self.systemd_name = attrs["Config"]["Labels"]["patcher/systemd-name"]

        # the actual repo image digest is buried inside an image reference that we have to
        # dig out from the running container image
        repo_image = client.images.get(self.image_sha256)
        self.repo_image_sha256 = repo_image.attrs["RepoDigests"][0].split("@")[1]

    def should_update(self):
        if not self.auto_update_enabled:
            return False
        if self.image not in IMAGE_CACHE:
            d = client.images.get_registry_data(self.image)
            d.pull()
            d.reload()
            IMAGE_CACHE[self.image] = d.attrs["Descriptor"]["digest"]
        return IMAGE_CACHE[self.image] != self.repo_image_sha256

    def restart(self):
        p = subprocess.Popen(['systemctl', 'restart', self.systemd_name], stderr=subprocess.PIPE,
                             stdout=subprocess.PIPE, shell=False)
        output = p.communicate()
        if p.returncode != 0:
            LOGGER.error("Failed to restart {0}, output from systemd: {1}".format(self.systemd_name, output[1]))

    def __repr__(self):
        return "<Container (name={0}, image_sha256={1}, image={2}, repo_image_sha256={3}, auto_update_enabled={4}, systemd_name={5})>".\
            format(self.name, self.image_sha256, self.image, self.repo_image_sha256, self.auto_update_enabled, self.systemd_name)

def get_containers():
    rv = client.containers.list(all=False)
    return [Container(x.attrs) for x in rv]

if __name__ == '__main__':
    for container in get_containers():
        try:
            LOGGER.info("Acting on {0}".format(container))
            if container.should_update():
                LOGGER.info("Restarting {0}".format(container))
                container.restart()
        except Exception as e:
            LOGGER.exception("Error occurred for {0}".format(container))


# CSCI3180 Principles of Programming Languages
#
# --- Declaration ---
#
# I declare that the assignment here submitted is original except for source
# material explicitly acknowledged. I also acknowledge that I am aware of
# University policy and regulations on honesty in academic work, and of the
# disciplinary guidelines and procedures applicable to breaches of such policy
# and regulations, as contained in the website
# http://www.cuhk.edu.hk/policy/academichonesty/
#
# Assignment 2
# Name : Wong Tsz Yin
# Student ID : 1155093245
# Email Addr : 1155093245@link.cuhk.edu.hk
import random
from NPC import NPC

class Elf(NPC):

    _MAGIC_CAP = 20
    def __init__(self,pos_x,pos_y,index,Map):
        super().__init__(pos_x,pos_y,index,Map)
        self.Name="E" +str(index)
        self.Power=random.randint(0,Elf._MAGIC_CAP-6)+5

    def action_On_Warrior(self,warrior):
        super().talk("My name is %s.  Welcome to my home. My magic power is %d."%(self.Name,self.Power))
        super().talk("Your magic crystal is %d."%warrior.Magic_crystal)
        super().talk("Do you need my help?")
        super().talk("You now have following options: ")
        print("1. Yes")
        print("2. No",end=' ')
        a=input()
        if a=='1':
            value=(random.randint(0,self.Power-3)+2)
            if (warrior.Magic_crystal>value):
                warrior.decrease_Crystal(value)
                warrior.increase_Health(value)
                warrior.talk("Thanks for your help! %s."%self.Name)
            else:
                warrior.talk("Very embarrassing, I don't have enough crystals.")
        return False


"""Perform fitting with a range of regularizers"""
from pyhdx.batch_processing import yaml_to_hdxmset, StateParser
from pathlib import Path
from pyhdx.fitting import fit_gibbs_global_batch
import yaml
import time
from datetime import datetime
from pyhdx.local_cluster import default_client
from pyhdx.fileIO import csv_to_protein
from pyhdx.models import HDXMeasurementSet
from pyhdx.support import pprint_df_to_file
from pyhdx import VERSION_STRING

current_dir = Path(__file__).parent
test_data_dir = current_dir.parent / "tests" / "test_data"
input_dir = test_data_dir / "input"
yaml_stream = Path(current_dir / "yaml_files" / "SecB.yaml").read_text()
data_dict = yaml.safe_load(yaml_stream)

output_dir = current_dir / "fit"
output_dir.mkdir(exist_ok=True)

parser = StateParser(data_dict, data_src=input_dir)
hdx_set = parser.load_hdxmset()

rates_list = [
    csv_to_protein(current_dir / "guesses" / f"{name}_rates_guess.csv")["rate"]
    for name in data_dict.keys()
]
gibbs_guess = hdx_set.guess_deltaG(rates_list)

log_file = output_dir / f"fitting_log.txt"
now = datetime.now()
date = f'# {now.strftime("%Y/%m/%d %H:%M:%S")} ({int(now.timestamp())})'

lines = [VERSION_STRING, date]

r2 = 0.5
for r1 in [0, 0.01, 0.25, 0.5, 1]:
    t0 = time.time()
    result = fit_gibbs_global_batch(hdx_set, gibbs_guess, epochs=1000, r1=r1, r2=r2)
    t1 = time.time()

    block = "--------------------------"
    regularizers = f"Regualizer 1: {r1}  Regualizer 2: {r2}"
    loss = (
        f"Total_loss {result.total_loss:.2f}, mse_loss {result.mse_loss:.2f}, reg_loss {result.reg_loss:.2f}"
        f"({result.regularization_percentage:.2f}%)"
    )
    time_elapsed = f"Time elapsed: {(t1 - t0):.2f} s"
    epochs = f"Number of epochs: {result.metadata['epochs_run']}"

    result.output.to_csv(output_dir / f"fit_output_r1_{r1}_r2_{r2}.csv")  # , na_rep='NaN')
    result.output.to_file(
        output_dir / f"fit_output_r1_{r1}_r2_{r2}.txt", fmt="pprint", na_rep="NaN"
    )

    lines += ["", block, regularizers, loss, epochs, time_elapsed, block]

log_file.write_text("\n".join(lines))


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

from django.db import models, migrations
import django.utils.timezone

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Year',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('start_year', models.IntegerField(unique=True, db_index=True)),
                ('added_time', models.DateTimeField(default=django.utils.timezone.now, auto_now_add=True)),
                ('update_time', models.DateTimeField(default=django.utils.timezone.now, auto_now=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
    ]


import numpy as np

u= np.zeros((5,2))
a = np.random.randn(5,1)#产生高斯随机变量
u=u+2  #2自动扩展
x=np.log(u).T

print(np.sum(u))#求矩阵元素的所有项的和
assert (a.shape==(5,1)) #断言错误不会继续执行
print(x.sum(axis=0))#按行或列来求和
print (np.dot(u,x))#矩阵相乘


import io
from PIL import Image as PilImage
from typing import Tuple, Type, Union

from .attachments import Thumbnail
from .descriptors import StreamDescriptor
from .helpers import validate_width_height_ratio
from .typing_ import FileLike

def generate_thumbnail(
    original_image: Union[FileLike, StreamDescriptor],
    width: int = None,
    height: int = None,
    ratio: float = None,
    ratio_precision: int = 5,
    thumbnail_type: Type[Thumbnail] = Thumbnail,
) -> Tuple[int, int, float, Thumbnail]:
    width, height, ratio = validate_width_height_ratio(
        width, height, ratio
    )
    thumbnail_buffer = io.BytesIO()
    format_ = 'jpeg'
    extension = '.jpg'

    # generating thumbnail and storing in buffer
    img = PilImage.open(original_image)

    # JPEG does not handle Alpha channel, switch to PNG
    if img.mode == 'RGBA':
        format_ = 'png'
        extension = '.png'

    with img:
        original_size = img.size

        if callable(width):
            width = width(original_size)
        if callable(height):
            height = height(original_size)

        width = int(width)
        height = int(height)

        thumbnail_image = img.resize((width, height))
        thumbnail_image.save(thumbnail_buffer, format_)

    thumbnail_buffer.seek(0)

    ratio = round(width / original_size[0], ratio_precision)
    thumbnail = thumbnail_type.create_from(
        thumbnail_buffer,
        content_type=f'image/{format_}',
        extension=extension,
        dimension=(width, height)
    )

    return width, height, ratio, thumbnail


# -*- coding: utf-8 -*-
from pandas_ta.utils import get_offset, verify_series

def rma(close, length=None, offset=None, **kwargs):
    """Indicator: wildeR's Moving Average (RMA)"""
    # Validate Arguments
    length = int(length) if length and length > 0 else 10
    alpha = (1.0 / length) if length > 0 else 0.5
    close = verify_series(close, length)
    offset = get_offset(offset)

    if close is None: return

    # Calculate Result
    rma = close.ewm(alpha=alpha, min_periods=length).mean()

    # Offset
    if offset != 0:
        rma = rma.shift(offset)

    # Handle fills
    if "fillna" in kwargs:
        rma.fillna(kwargs["fillna"], inplace=True)
    if "fill_method" in kwargs:
        rma.fillna(method=kwargs["fill_method"], inplace=True)

    # Name & Category
    rma.name = f"RMA_{length}"
    rma.category = "overlap"

    return rma

rma.__doc__ = \
"""wildeR's Moving Average (RMA)

The WildeR's Moving Average is simply an Exponential Moving Average (EMA) with
a modified alpha = 1 / length.

Sources:
    https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/V-Z/WildersSmoothing
    https://www.incrediblecharts.com/indicators/wilder_moving_average.php

Calculation:
    Default Inputs:
        length=10
    EMA = Exponential Moving Average
    alpha = 1 / length
    RMA = EMA(close, alpha=alpha)

Args:
    close (pd.Series): Series of 'close's
    length (int): It's period. Default: 10
    offset (int): How many periods to offset the result. Default: 0

Kwargs:
    fillna (value, optional): pd.DataFrame.fillna(value)
    fill_method (value, optional): Type of fill method

Returns:
    pd.Series: New feature generated.
"""


import serial
import time
import win32com.client
import numpy as np
from matplotlib import pyplot as plt

class connect:
    # COM-poort zoeken waar Arduino op aangesloten is
    connected = False
    wmi = win32com.client.GetObject("winmgmts:")
    for port in wmi.InstancesOf("Win32_SerialPort"):
        if "Arduino" in port.Name:
            comPort = port.DeviceID
            ser = serial.Serial(comPort, 19200, timeout=0)
            connected = True
            if ser != 0:
                print("Arduino connected to", comPort)
        else:
            print("Arduino is not connected")
    intvalue = 1
    plt.ion()  # set plot to animated

    ydata = [0] * 50
    ax1 = plt.axes()

    # make plot
    line, = plt.plot(ydata)
    plt.ylim([0, 400])  # set the y-range to 0 to 400

    # Deze functie zet een waarde om naar een int
    def to_int(s):
        try:
            return int(s)
        except ValueError:
            return float(s)

    def light(self):
        while True:
            data = connect.ser.readline().rstrip()
            data = int.from_bytes(data, byteorder = 'big')  # read data from serial
            # port and strip line endings
            ymin = float(min(connect.ydata)) - 10
            ymax = float(max(connect.ydata)) + 10
            plt.ylim([ymin, ymax])
            connect.ydata.append(data)
            del connect.ydata[0]
            connect.line.set_xdata(np.arange(len(connect.ydata)))
            connect.line.set_ydata(connect.ydata)  # update the data
            plt.draw()  # update the plot

        """ while True:
            for x in connect.ser:
                # data = connect.ser.readline().rstrip().decode('utf-8')
                data = (int.from_bytes(x, byteorder = 'big'))
                connect.ydata.append(data)
                del connect.ydata[0]
                connect.line.set_xdata(np.arange(len(connect.ydata)))
                connect.line.set_ydata(connect.ydata)  # update the data
                plt.draw()  # update the plot
                time.sleep(1) """

connect.light(connect)


import numpy as np
from scipy.optimize import minimize

import utils

len_x = 2
len_y = 4
len_z = 3

r = [None] * len_x

for i in range(len_x):
    r[i] = [None] * len_y
    for j in range(len_y):
        r[i][j] = [None] * len_z
        for k in range(len_z):
            r[i][j][k] = i * j * k

# print(r)

vec = utils.toVector(r)
multiDim = utils.to3D(vec, len_x, len_y, len_z)

print(r)
print(len(vec))
print(multiDim)

# arr_1d = np.arange(12)
# arr_3d = arr_1d.reshape(2, 3, 2)

# print(arr_3d)


"""
URL configuration for social_app project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include

from drf_yasg import openapi
from drf_yasg.views import get_schema_view

from rest_framework.permissions import AllowAny

# Swagger-UI.
schema_view = get_schema_view(
    openapi.Info(
        title="MLSA SOCIAL IMPACT - GREEN AIR API ",
        default_version='v1',
        description="Django Backend API",
        terms_of_service="",
        contact=openapi.Contact(email="rooseveltabandy@gmail.com"),
        license=openapi.License(name="MIT License"),
    ),
    url="https://backendgreenair.azurewebsites.net/",
    public=True,
    permission_classes=[AllowAny, ],
)
admin.site.site_header = "Green Air"
urlpatterns = [
    path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    path('admin/', admin.site.urls),
    path('auth/', include('core.urls')),
    path('air/', include('air_quality_app.urls')),
    path('tinymce/', include('tinymce.urls')),
    path("docs/", schema_view.with_ui("redoc", cache_timeout=0), name="schema-redoc"),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


import tensorflow as tf
from tensorflow import keras
from keras.models import load_model
import os
import matplotlib.pyplot as plt
import cv2 as cv
import numpy as np
from p1_utils import resize_image

os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
new_model = load_model('C:/Niranjan/Ashoka/Research/DCV/dcv_src/P1_Edges/Results/my_model.h5')

test_data_path = "C:/Niranjan/Ashoka/Research/DCV/Datasets/BIPED/edges/imgs/test/rgbr/"
results_path = "C:/Niranjan/Ashoka/Research/DCV/dcv_src/P1_Edges/Results/Test_Output/"

test_imgs = os.listdir(test_data_path)

fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
for img in test_imgs:
    img_arr = cv.imread(test_data_path+img)
    img_x = resize_image(img_arr, 256, 256)

    pred = new_model.predict(np.expand_dims(img_x, axis=0),batch_size=1)

    axs[0].imshow(img_arr)
    axs[0].axis('off')
    axs[0].set_title('Input Image')
    axs[1].imshow(pred[0], cmap='Greys')
    axs[1].axis('off')
    axs[1].set_title('Predicted Edge Map')

    plt.savefig(results_path+img)
    print("Image saved: ", img)

print("Inference Complete! Images saved in: ", results_path)

# img_x = cv.imread("C:/Niranjan/Ashoka/Research/DCV/Datasets/BIPED/edges/imgs/test/rgbr/RGB_017.jpg")

# pred = new_model.predict(np.expand_dims(img_x, axis=0),batch_size=1)

# fig = plt.imshow(pred[0], cmap='Greys')

# plt.savefig("C:/Niranjan/Ashoka/Research/DCV/dcv_src/P1_Edges/Results/pred.png")
# plt.close(fig)


import torch
import torch.nn.functional as F

train_num = 200000

class Net(torch.nn.Module):

    def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(n_feature, n_hidden)
        self.predict = torch.nn.Linear(n_hidden, n_output)

    def forward(self, x):
        inter = F.relu(self.hidden(x))
        y = self.predict(inter)
        return y

net = Net(n_feature=4, n_hidden=10, n_output=1)

optimizer = torch.optim.SGD(net.parameters(), lr=0.5)
loss_func = torch.nn.MSELoss()

file_train = open('dataset/train_data.csv','r')
line = file_train.readline()

x = torch.FloatTensor(4).zero_()
y = torch.FloatTensor(1).zero_()

for t in range(train_num):
    line = file_train.readline()
    if line == '':
        break
    line = line.split(',')

    x[0] = float(line[6])
    x[1] = float(line[7])/10000
    x[2] = float(line[8])
    x[3] = float(line[9])/10000
    y[0] = float(line[3])

    prediction = net(x)
    loss = loss_func(prediction, y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

file_train.close()
torch.save(net.state_dict(),'net_data_single.pkl')
print('train complete: ',train_num)


#!/usr/bin/env python

import os, sys, psutil, time

# THIS SCRIPT SHOULD BE RUN AS ROOT

def is_flask(proc):
  try:
    return "LZAPI_MODE" in proc.environ() and len(proc.children()) > 0 and proc.name() == "gunicorn"
  except:
    return False

def get_process(pid):
  try:
    return psutil.Process(pid=pid)
  except:
    return None

class FlaskServerInfo(object):
  def __init__(self,mode,pid,ppid,cwd,cons):
    self.mode = mode
    self.pid = pid
    self.ppid = ppid
    self.cwd = cwd
    self.cons = cons

  def __str__(self):
    constrings = []
    for c in self.cons:
      if c.status == "LISTEN":
        constrings.append("Listening on {} to port {}".format(*(c.laddr)))

    template = """
      Server: {0.mode}
      Working directory: {0.cwd}
      PID: {0.pid}
      {1}
    """

    template = "\n".join([x.lstrip() for x in template.strip().split("\n")])
    return template.format(self,"\n".join(constrings))

def find_flask_servers():
  servers = []
  for p in psutil.process_iter():
    if is_flask(p):
      s = FlaskServerInfo(
        p.environ()["LZAPI_MODE"],
        p.pid,
        p.ppid(),
        p.cwd(),
        p.connections()
      )

      servers.append(s)

  return servers

if __name__ == "__main__":
  servers = find_flask_servers()
  for s in servers:
    print(s)
    print()


from numpy import median

lines = []

with open(r"C:\Source\Whales.txt") as file:
    lines = file.read().split(",")

lines = [int(number) for number in lines]

mediana = median(lines)

outcome = [int(abs(item-mediana)) for item in lines]

print(sum(outcome))


#Tugas TA PDKP
#Muhammad Fathan Mubiina

#List Modul yang digunakan
#1. Modul 1 : Variable
#2. Modul 2 : if-else
#3. Modul 3 : Perulangan (loop)
#4. Modul 4 : Function & method
#5. Modul 5 : OOP 1 (class, constructor)
#6. Modul 8 : GUI

from tkinter import *
from solve import solve

#inisialisasi gui tkinter
class root():

    def __init__(self, root):
        self.root = root

root = Tk()
root.title("Fat's Sudoku Solver")
root.geometry("324x550")

#label keterangan program
label = Label(root,text="Fill in the numbers and click solve").grid(row=0, column=1, columnspan=10)

errLabel = Label(root, text="", fg="red")
errLabel.grid(row=15, column=1, columnspan=10, pady=5)

solvedLabel = Label(root, text="", fg="green")
solvedLabel.grid(row=15, column=1, columnspan=10, pady=5)

cells = {}

#validasi panjang angka (satu grid hanya berisi 1 angka)
def ValidateNumber(P):

    out = (P.isdigit() or P == "") and len(P) < 2
    return out

reg = root.register(ValidateNumber)

#inisialisasi grid sudoku 3x3
def draw3x3Grid(row, column, bgcolor):
    for i in range(3):
        for j in range(3):
            e = Entry(root, width=5, bg=bgcolor, justify="center", validate="key", validatecommand=(reg, "%P"))
            e.grid(row=row+i+1, column=column+j+1, sticky="nsew", padx=1, pady=1, ipady=5)
            cells[(row+i+1, column+j+1)] = e

#inisialisasi grid sudoku 9x9
def draw9x9Grid():
    color="#D0ffff"
    for rowNo in range(1, 10, 3):
        for colNo in range(0, 9, 3):
            draw3x3Grid(rowNo, colNo, color)
            if color == "#D0ffff":
                color = "#ffffd0"
            else:
                color = "#D0ffff"

#fungsi untuk mereset papan sudoku
def clearValues ():
    errLabel.configure(text="")
    solvedLabel.configure(text="")
    for row in range(2, 11):
        for col in range(1, 10):
            cell = cells[(row, col)]
            cell.delete(0, "end")

#fungsi untuk mem fetch data dari papan
def getValues():
    board = []
    errLabel.configure(text="")
    solvedLabel.configure(text="")
    for row in range(2, 11):
        rows = []
        for col in range(1, 10):
            val = cells[(row, col)].get()
            if val == "":
                rows.append(0)
            else:
                rows.append(int(val))

        board.append(rows)
    updateValues(board)

#tombol untuk solve, dan reset papan
btn = Button(root, command=getValues, text="Solve", width=10)
btn.grid(row=20, column=1, columnspan=5, pady=20)

btn = Button(root, command=clearValues, text="Clear", width=10)
btn.grid(row=20, column=5, columnspan=5, pady=20)

#fungsi untuk mengupdate tampilan papan setelah solusi ditemukan
def updateValues(s):
    sol = solve(s)
    if sol != "no":
        for rows in range(2, 11):
            for col in range(1, 10):
                cells[(rows, col)].delete(0, "end")
                cells[(rows, col)].insert(0, sol[rows-2][col-1])
            solvedLabel.configure(text="Sudoku solved!")
    else:
        errLabel.configure(text="No solution exist for this sudoku")

draw9x9Grid()
root.mainloop()


import os
import requests
from logging import StreamHandler, Formatter, INFO, getLogger
from requests.exceptions import Timeout, ConnectionError, ConnectTimeout
import json
"""requestsモジュールサンプル

    aiohttp,asyncioとの比較

"""

def init_logger():
    handler = StreamHandler()
    handler.setLevel(INFO)
    handler.setFormatter(
        Formatter("[%(asctime)s] [%(threadName)s] [%(levelname)s] %(message)s"))
    logger = getLogger()
    logger.addHandler(handler)
    logger.setLevel(INFO)

def get():
    # proxyを使う場合はget(url, proxies=proxies)の形で使う
    # proxies = {
    #     "http": "http://xxxxx",
    #     "https": "http://xxxxx"
    # }
    url = 'https://api.github.com/events'
    with requests.get(url) as r:
        getLogger().info(f'url: {r.url}')
        getLogger().info(f'encoding: {r.encoding}')
        # getLogger().info(f'text:{r.text}') # データ量多いので注意

def post():
    # proxyを使う場合はpost(url, data=payload, proxies=proxies)の形で使う
    # proxies = {
    #     "http": "http://xxxxx",
    #     "https": "http://xxxxx"
    # }
    # クエリを使う場合はpost(url, params=payload)の形で使う
    url = 'http://hoge.com/post/abcde'
    payload = {'key': 'value'}
    headers = {'content-type': 'application/json'}
    with requests.post(url, data=json.dumps(payload), headers=headers) as r:
        getLogger().info(f'url: {r.url} text:{r.text}')

def file_download(url: str, path: str = './'):
    """URLからファイルをダウンロード

    Parameters
    ----------
    url : str
        ダウンロードURL
    path : str, optional
        格納先パス, by default './'

    Returns
    -------
    str
        ダウンロードファイル名
    """
    filename = url.split('/')[-1]
    filepath = os.path.join(path, filename)
    # connect timeout, read timeoutは必要
    connect_timeout = 1.0001
    read_timeout = 1.0001
    try:
        with requests.get(url, timeout=(connect_timeout, read_timeout), stream=True) as r:
            with open(filepath, 'wb') as f:
                for chunk in r.iter_content(chunk_size=1024):
                    if chunk:
                        f.write(chunk)
                        f.flush()
        getLogger().info(f'filename: {filename}')
    except ConnectionError as e:
        # [connect timeout]
        # HTTPSConnectionPool(host='hira-tla2-test.s3.ap-northeast-1.amazonaws.com', port=443): Max retries exceeded with url: /helloworld.zip (Caused by ProxyError('Cannot connect to proxy.', timeout('timed out')))
        print('ConnectionError')
        getLogger().error(e)
    except ConnectTimeout as e:
        # 先にConnectionErrorが発生してしまう為、発生方法不明
        print('ConnectTimeout')
        getLogger().error(e)
    except Timeout as e:
        # [read timeout]
        # HTTPSConnectionPool(host='hira-tla2-test.s3.ap-northeast-1.amazonaws.com', port=443): Read timed out. (read timeout=0.001)
        print('Timeout')
        getLogger().error(e)
    except Exception as e:
        print('Exception')
        getLogger().error(e)

    return filename

if __name__ == "__main__":
    init_logger()

    getLogger().info('start')
    # get()
    post()
    file_url = 'https://aaa/helloworld.zip'
    # file_download(file_url)

    getLogger().info('end')


import unittest
from ..snapshot.weak_dict import SIDWeakKeyDictionary

class ValueRef(object):
  def __init__(self, value):
    self.value = value
  def __hash__(self):
    return hash(self.value)
  def __eq__(self, other):
    if not isinstance(other, ValueRef):
      return False
    return self.value == other.value

class SIDWeakKeyDictionaryTestCase(unittest.TestCase):
  def test_builtin(self):
    d = SIDWeakKeyDictionary()
    a = ValueRef(1)
    b = ValueRef(1)

    self.assertEqual(hash(a), hash(b))
    self.assertEqual(a, b)
    self.assertNotEqual(id(a), id(b))
    d2 = {}
    d2[a] = 3
    d2[b] = 4
    self.assertEqual(d2[a], 4)
    del d2
    d[a] = 3
    d[b] = 4
    self.assertEqual(d[a], 3)
    self.assertEqual(d[b], 4)
    self.assertEqual(len(d), 2)
    del a
    self.assertEqual(len(d), 1)

if __name__ == '__main__':
  unittest.main()


import xml.etree.ElementTree as e_tree
import re

from main.aho_korasick.pattern import Pattern
from main.model.enzyme_entity import EnzymeEntity
from main.utils.transformation_helper import TransformationHelper

class EnzymesReader:
    # SIB_ENZYMES_XML_PATH = '../../resources/static/xml/syb_enzymes.xml'
    # NEB_ENZYMES_XML_PATH = '../../resources/static/xml/neb_enzymes.xml'
    SIB_ENZYMES_XML_PATH = 'resources/static/xml/syb_enzymes.xml'
    NEB_ENZYMES_XML_PATH = 'resources/static/xml/neb_enzymes.xml'

    all_sib_enzymes_data = None
    all_neb_enzymes_data = None

    def __init__(self):
        self.all_sib_enzymes_data = self.get_enzymes_data(self.SIB_ENZYMES_XML_PATH)
        self.all_neb_enzymes_data = self.get_enzymes_data(self.NEB_ENZYMES_XML_PATH)

    def get_sib_enzymes_data(self):
        return self.all_sib_enzymes_data

    def get_neb_enzymes_data(self):
        return self.all_neb_enzymes_data

    def get_enzymes_data(self, enzyme_file_path):
        enzymes_data = []
        tree = e_tree.parse(enzyme_file_path)
        root_element = tree.getroot()
        enzyme_objs = root_element.findall('enzyme')
        for enzyme_obj in enzyme_objs:
            name = str(enzyme_obj.find('name').text)
            top_site = str(enzyme_obj.find('recognition_site_1').text)
            bottom_site = str(enzyme_obj.find('recognition_site_2').text)
            # TODO: fill link property of enzyme object
            link = ''
            enzyme = EnzymeEntity(name, top_site, bottom_site, link)
            enzymes_data.append(enzyme)
        # print(enzymes_data)
        return enzymes_data

    def get_sib_simple_patterns(self):
        return self.get_simple_patterns(self.all_sib_enzymes_data)

    def get_neb_simple_pattens(self):
        return self.get_simple_patterns(self.all_neb_enzymes_data)

    def get_simple_patterns(self, enzymes_list):
        enzymes_seq_names = {}
        for enzyme in enzymes_list:
            if enzyme.get_top_site().find('N') == -1:
                sequence = enzyme.get_top_site().replace('↑', '')
                if sequence not in enzymes_seq_names:
                    enzymes_seq_names[sequence] = list()
                enzymes_seq_names[sequence].append(enzyme.get_e_name())
        simple_patterns = self.process_multi_nucleotides(enzymes_seq_names)
        return simple_patterns

    def get_sib_wildcard_patterns(self):
        return self.get_wildcard_patterns(self.all_sib_enzymes_data)

    def get_neb_wildcard_patterns(self):
        return self.get_wildcard_patterns(self.all_neb_enzymes_data)

    def get_wildcard_patterns(self, enzymes_list):
        enzymes_seq_names = {}
        for enzyme in enzymes_list:
            sequence = enzyme.get_top_site()
            if sequence.find('N') != -1:
                sequence = sequence.replace('(N)', '')
                start_digit_pos = 0
                digits = []
                while start_digit_pos < len(sequence):
                    if sequence[start_digit_pos].isdigit():
                        end_digit_pos = start_digit_pos + 1
                        while sequence[end_digit_pos].isdigit():
                            end_digit_pos += 1
                        digits.append(sequence[start_digit_pos:end_digit_pos])
                        start_digit_pos = end_digit_pos
                    else:
                        start_digit_pos += 1
                sequence = self.replace_all_digits_by_n(sequence, digits)
                sequence = sequence.replace('↑', '')
                if sequence not in enzymes_seq_names:
                    enzymes_seq_names[sequence] = list()
                enzymes_seq_names[sequence].append(enzyme.get_e_name())
        wildcard_patterns = self.process_multi_nucleotides(enzymes_seq_names)
        return wildcard_patterns

    def get_all_sib_patterns(self):
        return self.get_all_patterns(self.all_sib_enzymes_data)

    def get_all_neb_patterns(self):
        return self.get_all_patterns(self.all_neb_enzymes_data)

    def get_all_patterns(self, enzymes_list):
        patterns = {}
        for enzyme in enzymes_list:
            sequence = enzyme.get_top_site()
            e_name = enzyme.get_e_name()
            if sequence.find('N') != -1:
                sequence = sequence.replace('(N)', '')
                start_digit_pos = 0
                digits = []
                while start_digit_pos < len(sequence):
                    if sequence[start_digit_pos].isdigit():
                        end_digit_pos = start_digit_pos + 1
                        while sequence[end_digit_pos].isdigit():
                            end_digit_pos += 1
                        digits.append(sequence[start_digit_pos:end_digit_pos])
                        start_digit_pos = end_digit_pos
                    else:
                        start_digit_pos += 1
                sequence = self.replace_all_digits_by_n(sequence, digits)
            sequence = sequence.replace('↑', '')
            if sequence in patterns:
                patterns[sequence].add_name(e_name)
            else:
                p = Pattern(sequence, [e_name])
                patterns[sequence] = p
        return list(patterns.values())

    def replace_all_digits_by_n(self, sequence, digits):
        for digit in digits:
            n_seq = 'N' * int(digit)
            sequence = sequence.replace(digit, n_seq)
        return sequence

    def process_multi_nucleotides(self, enzymes_seq_names):
        transformer = TransformationHelper()
        patterns = []
        for seq, names in enzymes_seq_names.items():
            if transformer.check_multi_nucleotides(seq):
                formatted_seqs = transformer.get_formatted_patterns(seq)
                for formatted_seq in formatted_seqs:
                    pattern = Pattern(seq, names, formatted_seq)
                    patterns.append(pattern)
            else:
                pattern = Pattern(seq, names)
                patterns.append(pattern)
        return patterns

if __name__ == "__main__":
    pass
    # patts = EnzymesReader.get_sib_enzymes_seq_names_data()
    # enzymes_seq_names_test = {'aRggY': ['name1', 'name2'], 'cgggWWttt': ['name3']}
    # patts = EnzymesReader.process_multi_nucleotides(enzymes_seq_names_test)
    # for p in patts:
    #    print(str(p))
    er = EnzymesReader()
    patts = er.get_all_sib_patterns()
    for p in patts:
        print(str(p))


# -*- coding: utf-8 -*-
import argparse
import csv
import h5py
import re
import os

import numpy as np
from os.path import join
from sklearn.model_selection import train_test_split
import math
from keras.utils.np_utils import to_categorical

from helper import Indexer, load_bin_vec, parse_input_csv, clean_str
import torch
import torch.utils.data as data_utils

def preprocess(args, emb_size, word2vec):

    # FIELDNAMES IN CSV FILE
    textfield = 'text'
    id_field = "Hospital.Admission.ID"
    subj_field = "subject.id"
    chart_field = "chart.time"
    conditions = ['cohort', #1
                  'Obesity', #2
                  'Non.Adherence', #3
                  'Developmental.Delay.Retardation', #4
                  'Advanced.Heart.Disease', #5
                  'Advanced.Lung.Disease', #6
                  'Schizophrenia.and.other.Psychiatric.Disorders', #7
                  'Alcohol.Abuse', #8
                  'Other.Substance.Abuse', #9
                  'Chronic.Pain.Fibromyalgia', #10
                  'Chronic.Neurological.Dystrophies', #11
                  'Advanced.Cancer', #12
                  'Depression', #13
                  'Dementia', #14
                  'Unsure']

    # LOAD ALL THE DATA INTO ARRAY
    inputs, targets, ids, subj, time = parse_input_csv("./clean_summaries0209.csv", textfield, conditions, id_field, subj_field, chart_field, args)

    print("FOUND {} DATA POINTS".format(len(inputs)))

    # CONVERT ALL THE TEXT  把 1610个inputs中的 text区域的 字符 全都变成 数字index
    lbl = []
    tokenizer = Indexer()  # Indexer 是开头定义的一个Class，tokenizer是一个object
    tokenizer.convert('padding')  # Indexer 是开头定义的一个Class，tokenizer是一个object
    max_len_sent = 0

    for i, t in enumerate(inputs): # 遍历 inputs list， i 是 index，t 是 elements
        current_convert = [tokenizer.convert(w) for w in clean_str(t).split()]  # 字符串转化成数字
        max_len_sent = max(max_len_sent, len(current_convert)) # 找出最长的 text 里 包含了多少个split出来的单词
        lbl.append(current_convert)
        if i % 100 == 0:
            print ("CONVERTING ROW {}".format(i))
    print ("MAXIMUM TEXT LENGTH IS {}".format(max_len_sent))
    print ("WORD AMOUNTS IS {}".format(tokenizer.counter))

    # ADD PADDING TO GET TEXT INTO EQUAL LENGTH  让 全部1610个的病例中的字符向量长度相同, 使它们均有 5572 个字符index
    for sent in lbl:
        if len(sent) < max_len_sent:
            sent.extend([2] * (max_len_sent - len(sent)))

    # CUT OFF NOTE IF CUTOFF > 0.
    if args.max_note_len > 0:
        print("SHORTENING NOTES FROM {} TO {}".format(max_len_sent, args.max_note_len))
        max_len_sent = min(args.max_note_len, len(sent))
        lbl = [sent[:max_len_sent] for sent in lbl]

    # TAKING CARE OF DATA TYPE, PUT IDS WITH TEXT!  lbl 由 list 转化成 DataFrame, 并在最后 3 列添加 ids, subj, time 1610 X 5575
    # lbl 和 targets 都对应于 inputs （1610）
    lbl = np.column_stack([lbl, ids, subj, time])
    targets = np.array(targets, dtype=int)  # 标签也要转化成 DataFrame  1610 X 15
    print ("VOCAB SIZE {}".format(len(tokenizer.d))) # labele数据 1610个数据

    # remove the ids from the training texts
    # def split_input_id(data):
        #return data[:, :-3], data[:, -3], data[:, -2], data[:, -1]

   # CONSTRUCT CORRECT EMBEDDING TABLE
    embed = np.random.uniform(-0.25, 0.25, (len(tokenizer.d) + 1, emb_size))   # 44848 X 50 ; tokenizer 存储了 44848 个 来自 inputs 中的有效单词
    #embed = np.row_stack((np.zeros(50),embed))  #  for padding

    unks = 0

    for key, value in tokenizer.d.items():  #  字典 tokenizer.d:  '<unk>': 1, 'padding': 2, 'Admission': 3, 'Date': 4 ...
        try:
             #-1 because of 1 indexing of word2idx (easier with torch)
            embed[value] = word2vec[key]   # 例如： embed[2] <-- word2vec['Admission']
        except:
            unks += 1
            pass
    print ("{} UNKNOWN WORDS".format(unks))

    embed[2,:] = 0

    # STORE ALL THE DATA
    #tokenizer.write("words.dict")

    filename = args.filename

    # STORE ALL THE DATA
    #tokenizer.write("words.dict")

    return lbl, targets, ids, subj, time, embed

def cross_validation(lbl, targets, ids, subj, time, topred, phenotypedict, phenotypedictsamples):

    lbl_ = np.array(lbl, dtype=int)
    targets_ = np.array(targets, dtype=int)

    dataset_concatenate = np.concatenate((lbl_, targets_),axis=1)

    disease_id = phenotypedict[topred]  # 第6种病， 论文中的 Psychiatric disorders

    pred = phenotypedict[topred]  #12

    pred_pos = phenotypedictsamples[topred]

    targets_pred = targets[:,pred]
    targets_pred =  np.column_stack([targets_pred, ids, subj, time])
    targets_pred = targets_pred[targets_pred[:,0].argsort()]
    pos_sam = targets_pred[-pred_pos:,:]
    neg_sam = targets_pred[:-pred_pos,:]

    insert_arr = np.array((np.floor(np.linspace(0,len(lbl_),pred_pos))),dtype = int)
    insert_arr[-1] = -1
    base_arr = np.array(neg_sam[:,1],dtype = int)
    for index,item in enumerate(insert_arr):
        base_arr = np.insert(base_arr,item,pos_sam[index,1],0)

    lbl_resample = lbl_[base_arr]  #lbl_trim2 1610 x 5575 的全部数据库
    lbl_resample_target = targets[:,pred]

    lbl_resample_target = np.column_stack([targets, ids, subj, time])
    lbl_resample_target = lbl_resample_target[np.array(lbl_resample[:,-3],dtype = int)]

    lbl_resample_target =  lbl_resample_target.astype(np.int)
    lbl_resample_target =  lbl_resample_target[:,0:15]

    train_time = 10

    lbl_biassam = lbl_resample[:,4:5572]    # 插完
    lbl_biastar = lbl_resample_target       # 插完

    fold = 10

    foldidx = int(len(lbl_resample)/fold)

    lbl_train = list()
    lbl_train_target = list()
    lbl_test = list()
    lbl_test_target = list()

    for i in range(0,fold):
        lbl_test.append(lbl_biassam[i*foldidx:foldidx*(i+1)][:])
        lbl_test_target.append(lbl_biastar[i*foldidx:foldidx*(i+1)][:])

        lbl_train.append(np.concatenate((lbl_biassam[:i*foldidx][:],lbl_biassam[foldidx*(i+1):][:])))
        lbl_train_target.append(np.concatenate((lbl_biastar[:i*foldidx][:],lbl_biastar[foldidx*(i+1):][:])))

    return lbl_train, lbl_train_target, lbl_test, lbl_test_target, phenotypedict

def readh5todata(args,path):
    myFile = h5py.File(path, 'r')
    x_train = myFile['train'][...]
    seq_lengths_xtrain = []
    for item in x_train:
        current_sample_removedpad = [movepad for movepad in item if movepad!=2]
        seq_lengths_xtrain.append(len(current_sample_removedpad))

    y_train = myFile['train_label'][...]
    # y_train = y_train[:,0]
	#seq_lengths.sort(reverse = True)
    x_train = torch.LongTensor(x_train)
    y_train = torch.LongTensor(y_train)
    seq_lengths_xtrain = torch.LongTensor(seq_lengths_xtrain)
    train = data_utils.TensorDataset(x_train,seq_lengths_xtrain, y_train)
    #y_train = torch.tensor(y_train)
    x_test = myFile['test'][...]
    seq_lengths_xtest = []
    for item in x_test:
        current_sample_removedpad = [movepad for movepad in item if movepad!=2]
        seq_lengths_xtest.append(len(current_sample_removedpad))

    y_test = myFile['test_label'][...]
  #  y_test = y_test[:,0]
    x_test = torch.LongTensor(x_test)
    y_test = torch.LongTensor(y_test)
    seq_lengths_xtest = torch.LongTensor(seq_lengths_xtest)
    test = data_utils.TensorDataset(x_test,seq_lengths_xtest, y_test)
    #y_test = y_test
    w2v = myFile['w2v'][...]
    #w2v = np.row_stack((np.zeros(50),w2v))
    w2v[2,:] = 0
    return train,test,y_test,w2v


import torch
import numpy as np
import random
from PIL import ImageFilter

class GaussianBlur(object):

    def __init__(self, p, radius):
        super(GaussianBlur, self).__init__()
        self.p = p
        self.radius = radius

    def __call__(self,img):
        if random.random() < self.p:
            blurFilters =[ImageFilter.GaussianBlur, ImageFilter.BoxBlur]
            return img.filter(blurFilters[random.randint(0,1)](radius = self.radius))
        else:
            return img

class Cutout(object):
    """Randomly mask out one or more patches from an image.
    Args:
        n_holes (int): Number of patches to cut out of each image.
        length (int): The length (in pixels) of each square patch.
    """
    def __init__(self, n_holes, length):
        self.n_holes = n_holes
        self.length = length

    def __call__(self, img):
        """
        Args:
            img (Tensor): Tensor image of size (C, H, W).
        Returns:
            Tensor: Image with n_holes of dimension length x length cut out of it.
        """

        h = img.size(1)
        w = img.size(2)

        mask = np.ones((h, w), np.float32)

        for n in range(self.n_holes):
            y = np.random.randint(h)
            x = np.random.randint(w)

            y1 = np.clip(y - self.length // 2, 0, h)
            y2 = np.clip(y + self.length // 2, 0, h)
            x1 = np.clip(x - self.length // 2, 0, w)
            x2 = np.clip(x + self.length // 2, 0, w)

            mask[y1: y2, x1: x2] = 0.

        mask = torch.from_numpy(mask)
        mask = mask.expand_as(img)
        img = img * mask

        return img


from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session
from .database import session
from .authorizator import verify_token, verify_admin
from . import crud, schemas, models

router = APIRouter()

@router.get('/list')
async def get_airdrops(
    page: int = 1,
    sort: int = 1,
    count: int = 20,
    db: Session = Depends(session)
):
    airdrops, total = crud.list_airdrop(db, page, sort, count)
    airdrops = [
        {
            "id": p.id,
            "name": p.name,
            "description": p.description,
            "amount": p.amount,
            "interval": p.interval
        }
        for p in airdrops
    ]
    pages, extra = divmod(total, count)
    if extra:
        pages += 1
    return {
        "text": "ok",
        "airdrops": airdrops,
        "pages": pages,
        "current": page,
        "total": total
    }

@router.get('/list_with_status')
async def get_airdrop_status(
    page: int = 1,
    sort: int = 1,
    count: int = 20,
    user: dict = Depends(verify_token),
    db: Session = Depends(session)
):
    airdrops, total = crud.list_airdrop_with_stat(
        db,
        page,
        sort,
        count,
        user["id"]
    )
    pages, extra = divmod(total, count)
    if extra:
        pages += 1
    return {
        "text": "ok",
        "airdrops": airdrops,
        "pages": pages,
        "current": page,
        "total": total
    }

@router.post('/create')
async def create_airdrop_as_admin(
    airdrop: schemas.AirdropCreateRequest,
    admin: dict = Depends(verify_admin),
    db: Session = Depends(session)
):
    crud.create_airdrop(
        db,
        airdrop.name,
        airdrop.description,
        airdrop.amount,
        airdrop.interval,
        airdrop.mode
    )
    return {"text": "ok"}

@router.get('/{airdrop_id}')
async def get_airdrop(
    airdrop_id: int,
    db: Session = Depends(session)
):
    airdrop = crud.get_airdrop(db, airdrop_id)
    return {
        "text": "ok",
        "id": airdrop.id,
        "name": airdrop.name,
        "description": airdrop.description,
        "amount": airdrop.amount,
        "interval": airdrop.interval,
        "mode": airdrop.mode
    }

@router.get('/{airdrop_id}/status')
async def get_airdrop_with_stat(
    airdrop_id: int,
    user: dict = Depends(verify_token),
    db: Session = Depends(session)
):
    airdrop_data = crud.get_airdrop_with_stat(db, airdrop_id, user["id"])
    return airdrop_data

@router.put('/{airdrop_id}')
async def set_airdrop_info_as_admin(
    airdrop: schemas.AirdropEditRequest,
    admin: dict = Depends(verify_admin),
    db: Session = Depends(session)
):
    crud.put_airdrop(
        db,
        airdrop.id,
        airdrop.name,
        airdrop.description,
        airdrop.amount,
        airdrop.interval,
        airdrop.mode
    )
    return {"text": "ok"}

@router.delete('/{airdrop_id}')
async def delete_airdrop_as_admin(
    airdrop_id: int,
    admin: dict = Depends(verify_admin),
    db: Session = Depends(session)
):
    crud.delete_airdrop(db, airdrop_id)
    return {"text": "ok"}

@router.post('/{airdrop_id}/claim')
async def claim_airdrop(
    airdrop_id: int,
    user: dict = Depends(verify_token),
    db: Session = Depends(session)
):
    _, before, after = crud.claim_airdrop(db, airdrop_id, user['id'])
    return {"text": "ok", "before": before, "after": after}


# -*- coding: utf-8 -*-
import scrapy
from Tencent.items import TencentItem

#scrapy.CrawlSpider

class TencentSpider(scrapy.Spider):
    name = 'tencent'
    allowed_domains = ['hr.tencent.com']

    base_url = "http://hr.tencent.com/position.php?&start="
    offset = 0
    start_urls = [base_url + str(offset)]

    # start_urls 里的url地址不做去重（不记录请求指纹）
    #start_urls = [base_url + str(num) for num in range(0, 2200 + 1, 10)]

    def parse(self, response):
        node_list = response.xpath("//tr[@class='even'] | //tr[@class='odd']")

        for node in node_list:
            item = TencentItem()

            item['position_name'] = node.xpath("./td[1]/a/text()").extract()[0]
            item['position_link'] = "http://hr.tencent.com/" + node.xpath("./td[1]/a/@href").extract()[0]

            try:
                item['position_type'] = node.xpath("./td[2]/text()").extract()[0]
            except:
                item['position_type'] = "None"

            #position_type_list = node.xpath("./td[2]/text()").extract()
            #item['position_type'] = position_type_list[0] if position_type_list else "None"

            item['people_number'] = node.xpath("./td[3]/text()").extract()[0]
            item['work_location'] = node.xpath("./td[4]/text()").extract()[0]
            item['publish_times'] = node.xpath("./td[5]/text()").extract()[0]

            yield item

        #if self.offset < 2200:
        #    self.offset += 10
        #    # scrapy.Request() 构建一个请求，第一个参数是url，callback 指定回调函数，表示该请求返回的response由哪个方法来解析
        #    yield scrapy.Request(self.base_url + str(self.offset), callback = self.parse)

        # 如果是不同标签里判断属性, 用 | 表示或
        #"//tr[@class='even'] | //tr[@class='odd']"

        # 如果同一个标签内判断属性，用 and  表示与
        # 如果到了最后一页，则列表为空，判断条件为False，不再发送请求；not 取反表示继续发送请求
        if not response.xpath("//a[@class='noactive' and @id='next']"):
            next_link = "http://hr.tencent.com/" + response.xpath("//a[@id='next']/@href").extract()[0]
            yield scrapy.Request(next_link, callback = self.parse)


import pytimeparse
from datetime import datetime, timedelta

from PyQt5.QtCore import QAbstractTableModel, QVariant, Qt
from SCTimeUtility.log.Log import getLog

class LeaderBoardModel(QAbstractTableModel):
    @staticmethod
    def lapsCompletedKey(c):
        return c.getLapCount()

    @staticmethod
    def fastestLapKey(c):
        return c.getFastestLap()

    def __init__(self, parent, headerData, cs=None):
        super().__init__(parent)

        self.defaultColumns = len(headerData)
        self.defaultRows = 10
        # self.dataList = None
        # self.tableList = tableList
        self.header = headerData
        self.carStore = cs
        self.connectActions()
        # self.dataChanged.connect(lambda: print("hello world1"))
        # self.sort = type(self).fastestLapKey
        # self.assignStorage(tableList)

    def storageModifiedEvent(self, col, row):
        # print("storage modified: ",col)
        leftChangeIndex = self.index(col, 0)
        rightChangeIndex = self.index(col, self.columnCount())
        self.dataChanged.emit(leftChangeIndex, leftChangeIndex)
        # self.headerDataChanged.emit(Qt.Horizontal, col, col)
        self.headerDataChanged.emit(Qt.Vertical, row, row)

    def connectActions(self):
        self.carStore.dataModified.connect(self.storageModifiedEvent)

    # def setModelData(self, data):
    #     self.dataList = data

    def rowCount(self, p):
        # print(max(len(self.carStore.storageList),self.defaultRows))
        return max(len(self.carStore.storageList), self.defaultRows)

    def columnCount(self, p=None):
        return self.defaultColumns

    # def setData(self, item, value, role):
    #     if role == Qt.EditRole:

    # if role == Qt.DisplayRole:
    #     if (item.column() < self.defaultColumns) and item.row() < len(self.dataList):
    #         return str(self.getItemAt(item.row(), item.column()))
    #     else:
    #         return QVariant()
    # else:
    #     return QVariant()

    def data(self, item, role):
        # print("data called")
        if role == Qt.DisplayRole:
            if (item.column() < self.defaultColumns) and item.row() < len(self.carStore.storageList):
                # print("Item at...")
                return str(self.getDisplayItemAt(item.row(), item.column()))
            else:
                return QVariant()
        elif role == Qt.UserRole:
            if (item.column() < self.defaultColumns) and item.row() < len(self.carStore.storageList):
                return self.getSortItemAt(item.row(), item.column())
            else:
                return QVariant()
        else:
            return QVariant()

    def headerData(self, section, orientation, role):
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                if section < self.defaultColumns:
                    return self.header[section]
                else:
                    return None
        elif role == Qt.UserRole:
            if orientation == Qt.Vertical:
                if section < len(self.carStore.storageList):
                    return section + 1
                else:
                    return None
        elif role == Qt.UserRole + 1:
            if orientation == Qt.Vertical:
                if section < len(self.carStore.storageList):
                    return len(self.carStore.storageList) - section
                else:
                    return None

    # takes a time string and converts to workable format
    def formatValue(self, value):
        if (value.isdigit()):
            formString = self.valueToTimeString(value)
            return pytimeparse.parse(formString)

    def valueToTimeString(self, val):
        timeList = [val[i:i + 2] for i in range(0, len(val), 2)]
        formString = ''

        for x in range(0, len(timeList)):
            if len(timeList[x]) < 2:
                newString = timeList[x] + '0'
                newString = self.reversed_string(newString)
                timeList[x] = newString

        timeList.reverse()

        for x in range(0, len(timeList)):
            if x != 0:
                formString = formString + ':' + timeList[x]
            else:
                formString = formString + timeList[x]

        if len(formString) <= 2:
            formString = formString + "s"

        return formString

    def intergerToTimeString(self, int):
        Hours = divmod(int, 3600)
        Minutes = divmod(int, 60)
        Seconds = divmod(int, 60)

        Hours = str(Hours[0])
        Minutes = str(Minutes[0])
        Seconds = str(Seconds[1])

        if len(Hours) == 1:
            Hours = '0' + Hours

        if len(Minutes) == 1:
            Minutes = '0' + Minutes

        if len(Seconds) == 1:
            Seconds = '0' + Seconds

        return Hours + ':' + Minutes + ":" + Seconds

    # def assignStorage(self, storage):
    #     if not storage:
    #         self.dataList = []
    #     else:
    #         self.dataList = storage

    def getDisplayItemAt(self, index, subIndex):
        if subIndex == 0:
            return self.carStore.storageList[index].getCarNum()
        elif subIndex == 1:
            return self.carStore.storageList[index].getTeam()
        elif subIndex == 2:
            return max(self.carStore.storageList[index].getLapCount() - 1, 0)
        elif subIndex == 3:
            fastLap = self.carStore.storageList[index].getFastestLap()
            if fastLap is not None:
                return str(timedelta(seconds=fastLap))
            else:
                return ""
        else:
            return None

    def getSortItemAt(self, index, subIndex):
        if subIndex == 0:
            return self.carStore.storageList[index].getCarNum()
        elif subIndex == 1:
            return self.carStore.storageList[index].getTeam()
        elif subIndex == 2:
            # This is kind of a hack, but it's the easiest way to
            # ensure that getFastestLAp and getLapCount have
            # compatible orders.
            return -max(self.carStore.storageList[index].getLapCount() - 1, 0)
        elif subIndex == 3:
            return self.carStore.storageList[index].getFastestLap()
        else:
            return None