#PORT = 80

#URL = input('Please enter URL: ')
#if URL[0] == 'w' or URL[0] == 'W':
#        URL = 'http://'+ URL
#elif URL[0:3] != 'www.':
  #  URL = 'http://www.'+URL
#
#try:
 #   urlSplit = URL.split('/')
#except:
#    quit('Error: Improper URL formatting')

#try:
 #   HOST = urlSplit[2]
#except:
  #  quit('Error: Improper URL formatting')
#print(HOST)
#socket.getaddrinfo('localhost', 8080)

#mySock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#mySock.connect((URL, PORT))

#while True:
   # data = mySock.recv(5120)
  #  if len(data) < 1:
    #    break
    #time.sleep(0.1)
   # count = count + len(data)
   # print(len(data), count)
   # picture = picture+data

#mySock.close()

#URL = input('Please enter URL: ')
#mySock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#HOST = URL.split('/')[2]
#mySock.connect((HOST, 80))
#toSend = ('GET' + ' ' + URL + ' ' + 'HTTP/1.0\r\n\r\n').encode()
#mySock.send(toSend)

#count = 0
#while True:
 #   data = mySock.recv(512)
 #   count += len(data)
  #  if (len(data) < 1) or (count >= 3000): break
  #  print(data.decode(), end='')
#mySock.close()
#print(count)
#import socket
#import urllib.request

#count = 0
#fhand = urllib.request.urlopen(input('Open URL: '))
#for line in fhand:
 #   count = count + len(line)
 #   if count >= 9000:
  #      print("Maximum value")
    #    quit()
  #  print(line.decode().strip())

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = input('Enter - ')
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
# Retrieve all of the paragraph tags
tags = soup('p')
print(len(tags))
#for tag in tags:
   # print(tag)


#!/usr/bin/env python2
# -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import re
import config
import commands
import datetime
import logging

# 传入的result_ips是一个list：[ip1:port1 , ip2:port2 , ....]
def generate_nginx_content(result_ips):
    update_content = ""
    for proxy in result_ips:
        if proxy:
            ip_port = proxy.split(",")[0]
            update_content += "server {ip}:{port} weight=1 max_fails=2 fail_timeout=500s;\n".format(
                ip=ip_port.split(":")[0],
                port=ip_port.split(":")[1]
            )

    return update_content

def update_nginx_conf(result_ips, path):
    proxy_path = path
    with open(proxy_path) as f:
        content = f.read()
        f.close()
    new_ip = generate_nginx_content(result_ips)
    content = "upstream  proxy_upstream {\n" + new_ip + "}"
    with open(proxy_path, "wb") as f:
        f.write(content)
        f.close()

#写入配置文件并重启nginx
def reload_nginx(result_ips):
    nginx_proxy_upstream_file_path = config.nginx_proxy_upstream_file_path
    # 像在/etc下的路径还需要root的权限去执行，应先调整好相关的权限
    update_nginx_conf(result_ips, nginx_proxy_upstream_file_path)

    # reload nginx
    # 注：odp的nginx在reload之前必须要start
    cmd = "/home/company_gitlab/odp/webserver/loadnginx.sh reload"
    #cmd = "/usr/sbin/nginx -s reload"
    res = commands.getstatusoutput(cmd)
    Date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    if res[0] == 0:
        # logging.info("odp nginx重启成功.")
        logging.info("%s:tingyun nginx重启成功." % Date)
    else:
        # logging.warning("odp nginx重启失败.")
        logging.info("%s:tingyun nginx重启失败." % Date)


#
# @lc app=leetcode.cn id=122 lang=python3
#
# [122] 买卖股票的最佳时机 II
#
# https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/
#
# algorithms
# Easy (68.45%)
# Likes:    1336
# Dislikes: 0
# Total Accepted:    435.4K
# Total Submissions: 634K
# Testcase Example:  '[7,1,5,3,6,4]'
#
# 给定一个数组 prices ，其中 prices[i] 是一支给定股票第 i 天的价格。
#
# 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易（多次买卖一支股票）。
#
# 注意：你不能同时参与多笔交易（你必须在再次购买前出售掉之前的股票）。
#
#
#
# 示例 1:
#
#
# 输入: prices = [7,1,5,3,6,4]
# 输出: 7
# 解释: 在第 2 天（股票价格 = 1）的时候买入，在第 3 天（股票价格 = 5）的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
# 随后，在第 4 天（股票价格 = 3）的时候买入，在第 5 天（股票价格 = 6）的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
#
#
# 示例 2:
#
#
# 输入: prices = [1,2,3,4,5]
# 输出: 4
# 解释: 在第 1 天（股票价格 = 1）的时候买入，在第 5 天 （股票价格 = 5）的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
# 注意你不能在第 1 天和第 2 天接连购买股票，之后再将它们卖出。因为这样属于同时参与了多笔交易，你必须在再次购买前出售掉之前的股票。
#
#
# 示例 3:
#
#
# 输入: prices = [7,6,4,3,1]
# 输出: 0
# 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
#
#
#
# 提示：
#
#
# 1
# 0
#
#
#

# @lc code=start
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        '''
            dp[i][k][0/1] 代表在第i天交易至多k次的情况下，持有或不持有能获得的利润（0代表不持有，1代表持有）
            dp[i][k][0] = max(dp[i-1][k][0], dp[i-1][k][1] + prices[i])
            dp[i][k][1] = max(dp[i-1][k][1], dp[i-1][k-1][0] - prices[i])
            因为 k为 无穷，所以k == k-1
            dp[i][k][0] = max(dp[i-1][k][0], dp[i-1][k][1] + prices[i])
            dp[i][k][1] = max(dp[i-1][k][1], dp[i-1][k][0] - prices[i])
            则 无需记录k状态
            dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i])
            dp[i][1] = max(dp[i-1][1], dp[i-1][0] - prices[i])
        '''
        n = len(prices)
        # base case: dp[-1][0] = 0, dp[-1][1] = -infinity
        dp_i_0 = 0
        dp_i_1 = float('-inf')
        for i in range(n):
            temp = dp_i_0
            # dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i])
            dp_i_0 = max(dp_i_0, dp_i_1 + prices[i])
            # dp[i][1] = max(dp[i-1][1], dp[i-1][0] - prices[i])
            dp_i_1 = max(dp_i_1, temp - prices[i])
        return dp_i_0
# @lc code=end


# Python RegExp Syntax to Javascript RegExp Syntax Translator
# This code was pulled from the repository at:
#   https://github.com/GULPF/rescrypt
# Original license was MIT but was converted to Apache v2 for
# ease of integrating with the Transcrypt project
#

# XXX: don't redefine those here

T = (1<<0)
TEMPLATE = T

I = (1<<1)
IGNORECASE = I

# Deprecated
L = (1<<2)
LOCALE = L

M = (1<<3)
MULTILINE = M

S = (1 << 4)
DOTALL = S
# Legacy - Unicode by default in Python 3
U = (1 << 5)
UNICODE = U
X = (1 << 6)
VERBOSE = X
DEBUG = (1<<7)

A = (1<<8)
ASCII = A

# This is a javascript specific flag
Y = (1 << 16)
STICKY = Y
G = (1 << 17)
GLOBAL = G
# This flag is used to indicate that re module should use
# the javascript regex engine directly and not attempt to
# translate the regex string into a python regex
J = (1<<19)
JSSTRICT = J

__pragma__ ('tconv')

def _read_escape(rgx, idx, append_to):
    # XXX: This must handle the case in `if c == '\\'`?:
    if rgx[idx] == '\\':
        if idx + 1 >= len(rgx):
            pass # XXX: exception
        # These three are regex escape codes
        # that doesn't exist in JS.
        if rgx[idx + 1] == 'A':
            append_to.append('^')
        elif rgx[idx + 1] == 'a':
            append_to.append('\\07')
        elif rgx[idx + 1] == 'Z':
            append_to.append('$')
        # Otherwise just leave as is.
        else:
            append_to.append('\\' + rgx[idx + 1])
        idx += 2
    else:
        append_to.append(rgx[idx])
        idx += 1
    return idx

def _read_until(rgx, start, char, append_to):
    idx = start
    while idx < len(rgx) and rgx[idx] != char:
        idx = _read_escape(rgx, idx, append_to)
    if idx >= len(rgx):
        raise Exception("Unexpected end of input")
    # append_to.append(rgx[idx])
    # idx += 1
    return idx

def _safe_char_at(s, idx):
    if idx >= len(s):
        return ''
    return s[idx]

def translate(rgx, flags=0):
    # import re

    idx = 0

    flagdict = {
        'i': IGNORECASE,
        'L': LOCALE,
        'm': MULTILINE,
        's': DOTALL,
        'u': UNICODE,
        'x': VERBOSE,
        'a': ASCII
    }

    tokens = []
    groupindex = {}
    n_capturings = 1 # Capturing indices start at 1

    while idx < len(rgx):
        # The current and next character
        c = rgx[idx]
        n = _safe_char_at(rgx, idx + 1)

        # TODO: use read_escape instead
        if c == '\\':
            # These three are regex escape codes
            # that doesn't exist in JS.
            if n == 'A':
                value = '^'
            elif n == 'a':
                value = '\\07'
            elif n == 'Z':
                value = '$'
            # Otherwise just leave as is.
            else:
                value = '\\' + n

            tokens.append(value)
            idx += 2

        elif c == '$':
            # '$' has slightly different semantics in Python and JS.
            # Note that /\Z/ in Python is equal to /$/ in JS.
            tokens.append('(?=\\n?$)')
            idx += 1

        elif c == '{':
            # Rewrite `{,b}` to `{0,b}`.
            # Leave others as is.
            if n == ',':
                itr = idx + 2
                b_nbrs = []

                while rgx[itr] in '0123456789' and itr < len(rgx):
                    b_nbrs.append(rgx[itr])
                    itr += 1

                if rgx[itr] == '}':
                    tokens.extend(["{", "0", ","] + b_nbrs)
                    idx += 2 + len(b_nbrs)
                else:
                    tokens.extend(["{", ","])
                    idx += 2
            else:
                tokens.append(rgx[idx])
                idx += 1

        elif c == '[':
            # This requires no rewriting, but we need
            # to consume everything until the next
            # unescaped ']' to make sure that what's
            # inside of the set isn't interpreted as something
            # special (e.g /[(]/ is valid but /(/ isn't)
            tokens.append(rgx[idx])
            idx += 1

            if rgx[idx] == '^':
                tokens.append(rgx[idx])
                idx += 1

            # As a special rule, Python allows a literal ']' as the first
            # member of a set. E.g  /[]]/ is a set containing ']',
            # and /[^]]/ is an inverted set containing ']'.
            if n == ']':
                tokens.append('\\' + n)
                idx += 1

            idx = _read_until(rgx, idx, ']', tokens)
            tokens.append(']')
            idx += 1

        elif c == '(' and n == '?':
            # Extension notation.
            n2 = _safe_char_at(rgx, idx + 2)
            n3 = _safe_char_at(rgx, idx + 3)

            # Named group def.
            # XXX: For simplicity this allows any chars in group name
            #      but Python only allows valid identfiers.
            if n2 == 'P' and n3 == '<':
                namearr = []
                idx += 4 # skip (?P<
                idx = _read_until(rgx, idx, '>', namearr)
                idx += 1 # skip '>'
                groupindex["".join(namearr)] = n_capturings
                n_capturings += 1
                tokens.append('(')

            # Named group ref.
            elif n2 == 'P' and n3 == '=':
                namearr = []
                idx += 4 # skip (?P=
                idx = _read_until(rgx, idx + 4, ')', namearr)
                idx += 1 # skip '>'
                name = "".join(namearr)
                if name not in groupindex:
                    raise error("Unknown named capturing group: " + name)
                tokens.append('\\' + groupindex[name])

            # Comment
            elif n2 == '#':
                idx = _read_until(rgx, idx, ')', [])
                idx += 1 # Skip )

            # Flag
            elif n2 in flagdict:
                idx += 2
                # NOTE: No reason to care about escape
                #       sequences here since the only
                #       valid letters are 'iLmsux'.
                while idx < len(rgx) and rgx[idx] != ')':
                    if rgx[idx] not in flagdict:
                        break
                    flags |= flagdict[rgx[idx]]
                    idx += 1

                if idx == len(rgx):
                    raise error("Expected '(' but found " + rgx[idx])

                if rgx[idx] != ')':
                    if rgx[idx] in ["-", ":"]:
                        raise error("The '(?imsx-imsx:...)' regex syntax " +
                                    "is not supported by Transcrypt.")
                    raise error("Unknown regex flag '" + rgx[idx] + "'")

                idx += 1

            elif (n2 == '<' and n3 == '=') or (n2 == '<' and n3 == '!'):
                raise Exception("Regex lookbehinds are not supported by Transcrypt")

            elif n2 == ':':
                tokens.append(rgx[idx])
                idx += 1

            # XXX: implement
            # One of:
            #   - lookahead
            #   - neg lookahead
            #   - if then else
            # Note that we are probably not able to implement
            # lookbehinds.
            else:
                raise Exception("Unknown regex extension '" + n2 + "'")
        else:
            if c == '(':
                n_capturings += 1
            tokens.append(rgx[idx])
            idx += 1

    if flags & DOTALL:
        for idx, token in enumerate(tokens):
            if token == '.':
                tokens[idx] = r'[\s\S]'

    return "".join(tokens), flags, groupindex, n_capturings - 1

print("input", r"(?P<prefix>[a-zA-Z]+)://(?P<suffix>[^/]*)")
print("output", translate(r"(?P<prefix>[a-zA-Z]+)://(?P<suffix>[^/]*)"))


import datetime
import io
import typing
import uuid

import fastavro

DATETIME_STR_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
DATE_STR_FORMAT = "%Y-%m-%d"
TIME_STR_FORMAT = "%H:%M:%S"

def serialize(payload: typing.Dict, schema: typing.Dict, serialization_type: str = "avro") -> bytes:
    if serialization_type == "avro":
        file_like_output: typing.Union[io.BytesIO, io.StringIO] = io.BytesIO()

        fastavro.schemaless_writer(file_like_output, schema, payload)

        value = file_like_output.getvalue()
    elif serialization_type == "avro-json":
        file_like_output = io.StringIO()
        fastavro.json_writer(file_like_output, schema, [payload])
        value = file_like_output.getvalue().encode("utf-8")
    else:
        raise ValueError(f"Serialization type should be `avro` or `avro-json`, not {serialization_type}")

    file_like_output.flush()

    return value  # type: ignore

def deserialize(data: bytes, schema: typing.Dict, serialization_type: str = "avro") -> typing.Dict:
    if serialization_type == "avro":
        input_stream: typing.Union[io.BytesIO, io.StringIO] = io.BytesIO(data)

        payload = fastavro.schemaless_reader(input_stream, schema)

    elif serialization_type == "avro-json":
        input_stream = io.StringIO(data.decode())
        # This is an iterator, but not a container
        records = fastavro.json_reader(input_stream, schema)
        # records can have multiple payloads, but in this case we return the first one
        payload = list(records)[0]
    else:
        raise ValueError(f"Serialization type should be `avro` or `avro-json`, not {serialization_type}")

    input_stream.flush()

    return payload

def datetime_to_str(value: datetime.datetime) -> str:
    return value.strftime(DATETIME_STR_FORMAT)

def date_to_str(value: datetime.date) -> str:
    return value.strftime(DATE_STR_FORMAT)

def time_to_str(value: datetime.time) -> str:
    return value.strftime(TIME_STR_FORMAT)

def to_json(data: typing.Dict[str, typing.Any]) -> typing.Dict:
    json_data = {}

    for field, value in data.items():
        if isinstance(value, bytes):
            value = value.decode()
        elif isinstance(value, datetime.datetime):
            value = datetime_to_str(value)
        elif isinstance(value, datetime.date):
            value = date_to_str(value)
        elif isinstance(value, datetime.time):
            value = time_to_str(value)
        elif isinstance(value, uuid.UUID):
            value = str(value)

        json_data[field] = value

    return json_data


import urllib.parse as parse
import pyodbc
import sqlalchemy
import pandas as pd
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import LabelBinarizer, StandardScaler, LabelEncoder
from sklearn.model_selection import StratifiedShuffleSplit
import string
import re
from bert import bert_tokenization
from sklearn.pipeline import Pipeline
import tqdm
import pickle as pkl
import os
import tfpipeline.v2 as tfp
from tensorflow import io

def eri_db(db, server='SNADSSQ3', driver='{SQL Server}'):
    params = ('Driver={driver};'
              'Server={server};'
              'Database={db};'
              'Trusted_Connection=yes;')
    params = params.format(driver=driver, server=server, db=db)
    params = parse.quote(params)
    uri = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
    return sqlalchemy.create_engine(uri)

class DataFrameTransformer(BaseEstimator, TransformerMixin):
    def __init__(self, attribute_names):
        self.attribute_names = attribute_names
    def fit(self, X):
        return self
    def transform(self, X):
        return X[self.attribute_names].values

class DescriptionCleaner(BaseEstimator, TransformerMixin):
    def __init__(self):
        return None
    def fit(self, X):
        return self
    def transform(self, X):
        def clean(x):
            sep = ['*', '-', '>', '<', '~']
            other_punc = ''.join([s for s in string.punctuation if s not in sep])
            ## Normalize sentence separators
            pat = '[\\.\\?!;]{1,5}\s'
            x = re.sub(pat, '. ', x)

            ## Ensures that there is punctuation at the end of a line
            pat = '([^\\.])\s*(?:\r\n|\n)'
            x = re.sub(pat, '\\1. ', x)

            ## Strips out any bullet-like formatting
            pat = '\\.\s*[^\w\s{}]\s*(\w)'.format(other_punc)
            x = re.sub(pat, '. \\1', x)
            pat = ':\s*[^\w\s{}]\s*(\w)'.format(other_punc)
            x = re.sub(pat, '. \\1', x)
            pat = '(\w)\s*[^\w\s{}]\s*(\w)'.format(other_punc)
            x = re.sub(pat, '\\1. \\2', x)
            pat = '^\s*[^\w\s{}]\s*(\w)'.format(other_punc)
            x = re.sub(pat, '\\1', x)
            return x
        clean_desc = []
        for x in tqdm.tqdm(X, desc='DescriptionCleaner'):
            clean_desc.append(clean(x))
        return np.array(clean_desc)

class BERTTokenizer(BaseEstimator, TransformerMixin):
    def __init__(self, vocab_file, max_seq_len):
        self.vocab_file = vocab_file
        self.max_seq_len = max_seq_len
        self.tokenizer = bert_tokenization.FullTokenizer(vocab_file)
    def fit(self, X):
        return self
    def transform(self, X):
        def tokenize(x):
            x = self.tokenizer.tokenize(x)
            x = ['[CLS]'] + x + ['[SEP]']
            x = self.tokenizer.convert_tokens_to_ids(x)
            if len(x) > self.max_seq_len:
                x = x[:self.max_seq_len]
            while len(x) < self.max_seq_len:
                x.append(0)
            return x
        token_ids = []
        for x in tqdm.tqdm(X, desc='BERTTokenizer'):
            token_ids.append(tokenize(x))
        return np.array(token_ids)

if __name__ == '__main__':
    assessor_work = eri_db('AssessorWork')

    with open('get_exp_data.sql', 'r') as f:
        sql = f.read()
    exp_df = pd.read_sql(sql, assessor_work, index_col='job_id')
    with open('get_rev_data.sql', 'r') as f:
        sql = f.read()
    rev_df = pd.read_sql(sql, assessor_work)
    rev_df = rev_df.pivot('job_id', 'cut_point', 'comp')
    rev_df.columns = ['low_comp', 'high_comp']
    comp_df = pd.concat([exp_df, rev_df])

    with open('get_desc_data.sql', 'r') as f:
        sql = f.read()
    desc_df = pd.read_sql(sql, assessor_work, index_col='job_id').sort_index()
    desc_df = desc_df[desc_df.index.isin(comp_df.index)]
    desc_job_ids = desc_df.index.values

    lb = LabelBinarizer()
    desc_y = lb.fit_transform(desc_df.flsa.values)
    classes = lb.classes_
    file = os.path.join(os.getcwd(), 'data', 'classes.npy')
    with open(file, 'wb') as f:
        np.save(f, classes)

    desc_pipe = Pipeline([
        ('df_extract', DataFrameTransformer('title_desc')),
        ('cleaner', DescriptionCleaner()),
        ('tokenizer', BERTTokenizer(os.path.join(os.getcwd(), 'data', 'uncased_base', 'vocab.txt'),
                                    max_seq_len=256))
    ])
    desc_X = desc_pipe.fit_transform(desc_df)

    bert_input = []
    comp_input = []
    y = []
    for i, id in enumerate(desc_job_ids):
        label = desc_y[i]
        input_ids = desc_X[i]
        low_comp = comp_df.loc[id, 'low_comp']
        high_comp = comp_df.loc[id, 'high_comp']
        rand_comps = np.random.randint(low_comp, high_comp, 10)
        comp_samples = [low_comp, high_comp, *rand_comps]
        for comp in comp_samples:
            bert_input.append(input_ids)
            comp_input.append(comp)
            y.append(label)
    bert_input = np.array(bert_input).reshape(-1, 256)
    comp_input = np.array(comp_input).reshape(-1, 1)
    y = np.array(y, dtype='int64')
    shuf_idx = np.random.permutation(len(y))
    bert_input = bert_input[shuf_idx]
    comp_input = comp_input[shuf_idx]
    y = y[shuf_idx]
    scaler = StandardScaler()
    comp_input = scaler.fit_transform(comp_input)

    assert len(bert_input) == len(comp_input) == len(y)
    splitter = StratifiedShuffleSplit(n_splits=2, test_size=0.25, random_state=43)
    for train_index, test_index in splitter.split(comp_input, y):
        comp_input_tmp, comp_input_test = comp_input[train_index], comp_input[test_index]
        bert_input_tmp, bert_input_test = bert_input[train_index], bert_input[test_index]
        y_tmp, y_test = y[train_index], y[test_index]
    splitter = StratifiedShuffleSplit(n_splits=2, test_size=0.1, random_state=12)
    for train_index, valid_index in splitter.split(comp_input_tmp, y_tmp):
        comp_input_train, comp_input_valid = comp_input_tmp[train_index], comp_input_tmp[valid_index]
        bert_input_train, bert_input_valid = bert_input_tmp[train_index], bert_input_tmp[valid_index]
        y_train, y_valid = y_tmp[train_index], y_tmp[valid_index]
    ds_lookup = {
        'comp_input_train': comp_input_train,
        'comp_input_test': comp_input_test,
        'comp_input_valid': comp_input_valid,
        'bert_input_train': bert_input_train,
        'bert_input_test': bert_input_test,
        'bert_input_valid': bert_input_valid,
        'y_train': y_train,
        'y_test': y_test,
        'y_valid': y_valid
    }
    for file, obj in ds_lookup.items():
        path = os.path.join('data', '{}.npy'.format(file))
        with open(path, 'wb') as f:
            np.save(f, obj)

    io.gfile.makedirs('saved_models')
    file = os.path.join('saved_models', 'scaler.pkl')
    with open(file, 'wb') as f:
        pkl.dump(scaler, f)


__author__ = 'monangi.kumar'

import xlrd
import xlutils.copy

inBook = xlrd.open_workbook('F:/Reports/PERF_RESULTS.xls', formatting_info=True)
outBook = xlutils.copy.copy(inBook)

def _getOutCell(outSheet, colIndex, rowIndex):
    """ HACK: Extract the internal xlwt cell representation. """
    row = outSheet._Worksheet__rows.get(rowIndex)
    if not row: return None

    cell = row._Row__cells.get(colIndex)
    return cell

def setOutCell(outSheet, col, row, value):
    """ Change cell value without changing formatting. """
    # HACK to retain cell style.
    previousCell = _getOutCell(outSheet, col, row)
    # END HACK, PART I

    outSheet.write(row, col, value)

    # HACK, PART II
    if previousCell:
        newCell = _getOutCell(outSheet, col, row)
        if newCell:
            newCell.xf_idx = previousCell.xf_idx
    # END HACK

outSheet = outBook.get_sheet(0)
setOutCell(outSheet, 2, 2, test)
outBook.save('F:/Reports/PERF_RESULTS.xls')


"""
Usage:
    python zip_util.py zip
    python zip_util.py unzip

    Assumes the following directories exist with the following arguments:

    - responses (zip)
        Write the compressed CSV files as ZIP files to this directory
    - responses-unzipped (unzip)
        Write the uncompressed ZIP files as CSV files to this directory

"""
import os
import sys
from subprocess import DEVNULL, call

USAGE = 'USAGE: python zip_util.py [zip | unzip]'

def cli(operation):
    if call('git lfs env', shell=True,
                       stdout=DEVNULL, stderr=DEVNULL) != 0:
        print('''Git LFS is not installed.
Install Git LFS and run `git lfs pull` in this repository to pull the contents of the data files.''')
        return

    if operation == 'zip':
        in_dir = 'responses-unzipped'
        out_dir = 'responses'
        for file in os.listdir(in_dir):
            if 'DS_Store' in file:
                continue
            os.system(f'cd {in_dir}; zip {file}.zip {file}; mv {file}.zip ../{out_dir}')

    elif operation == 'unzip':
        in_dir = 'responses'
        out_dir = 'responses-unzipped'
        for file in os.listdir(in_dir):
            if 'DS_Store' in file:
                continue
            assert file[-4:] == '.zip'
            filename = file[:-4]
            os.system(f'cd {in_dir}; unzip {file}; mv {filename} ../{out_dir}')

    else:
        print(USAGE)
        sys.exit(2)

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print(USAGE)
        sys.exit(2)

    cli(sys.argv[1])


#  Copyright 2019 Elasticsearch BV
#
#      Licensed under the Apache License, Version 2.0 (the "License");
#      you may not use this file except in compliance with the License.
#      You may obtain a copy of the License at
#
#          http://www.apache.org/licenses/LICENSE-2.0
#
#      Unless required by applicable law or agreed to in writing, software
#      distributed under the License is distributed on an "AS IS" BASIS,
#      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#      See the License for the specific language governing permissions and
#      limitations under the License.

# File called _pytest for PyCharm compatability

from pandas.util.testing import assert_almost_equal

from eland.tests.common import TestData

class TestSeriesMetrics(TestData):
    funcs = ["max", "min", "mean", "sum"]
    timestamp_funcs = ["max", "min", "mean"]

    def test_flights_metrics(self):
        pd_flights = self.pd_flights()["AvgTicketPrice"]
        ed_flights = self.ed_flights()["AvgTicketPrice"]

        for func in self.funcs:
            pd_metric = getattr(pd_flights, func)()
            ed_metric = getattr(ed_flights, func)()
            assert_almost_equal(pd_metric, ed_metric, check_less_precise=True)

    def test_flights_timestamp(self):
        pd_flights = self.pd_flights()["timestamp"]
        ed_flights = self.ed_flights()["timestamp"]

        for func in self.timestamp_funcs:
            pd_metric = getattr(pd_flights, func)()
            ed_metric = getattr(ed_flights, func)()
            pd_metric = pd_metric.floor("S")  # floor or pandas mean with have ns
            assert_almost_equal(pd_metric, ed_metric, check_less_precise=True)

    def test_ecommerce_selected_non_numeric_source_fields(self):
        # None of these are numeric
        column = "category"

        ed_ecommerce = self.ed_ecommerce()[column]

        for func in self.funcs:
            ed_metric = getattr(ed_ecommerce, func)()
            assert ed_metric.empty

    def test_ecommerce_selected_all_numeric_source_fields(self):
        # All of these are numeric
        columns = ["total_quantity", "taxful_total_price", "taxless_total_price"]

        for column in columns:
            pd_ecommerce = self.pd_ecommerce()[column]
            ed_ecommerce = self.ed_ecommerce()[column]

            for func in self.funcs:
                assert_almost_equal(
                    getattr(pd_ecommerce, func)(),
                    getattr(ed_ecommerce, func)(),
                    check_less_precise=True,
                )


import os
import configparser

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DAYS = int(os.getenv('GOOGLE_DAYS', 20))
LIMIT = 2000

AUTH_ROOT = os.path.abspath(os.path.join(BASE_DIR))
config_auth = configparser.RawConfigParser()
config_auth.read(AUTH_ROOT + '/auth.conf')

TESTING = {
    'running': False
}


import datetime
import json
import requests
import pandas as pd
import streamlit as st
from copy import deepcopy
from twilio.rest import Client
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

st.set_page_config(layout='wide', initial_sidebar_state='collapsed')

@st.cache(allow_output_mutation=True, suppress_st_warning=True)
def load_mapping():
    df = pd.read_csv("district_mapping.csv")
    return df

def filter_column(df, col, value):
    df_temp = deepcopy(df.loc[df[col] == value, :])
    return df_temp

def filter_in_stock(df, col):
    df_temp = deepcopy(df.loc[df[col] > 0, :])
    return df_temp

def get_location(df, col):
    df_temp = deepcopy(df.loc[df[col], :])
    return df_temp

mapping_df = load_mapping()

mapping_dict = pd.Series(mapping_df["district id"].values,
                         index = mapping_df["district name"].values).to_dict()

rename_mapping = {
    'date': 'Date',
    'min_age_limit': 'Minimum Age Limit',
    'available_capacity': 'Available Capacity',
    'pincode': 'Pincode',
    'name': 'Hospital Name',
    'state_name' : 'State',
    'district_name' : 'District',
    'block_name': 'Block Name',
    'fee_type' : 'Fees'
    }

st.title('Manasa says lets get vaccinated!')
st.info('The CoWIN APIs are geo-fenced so sometimes you may not see an output! Please try after sometime ')

# numdays = st.sidebar.slider('Select Date Range', 0, 100, 10)
unique_districts = list(mapping_df["district name"].unique())
unique_districts.sort()

left_column_1, right_column_1 = st.beta_columns(2)
with left_column_1:
    numdays = st.slider('Select Date Range', 0, 100, 5)

with right_column_1:
    default_dist = unique_districts.index('BBMP')
    dist_inp = st.selectbox('Select District', unique_districts, index=default_dist)

DIST_ID = mapping_dict[dist_inp]

base = datetime.datetime.today()
date_list = [base + datetime.timedelta(days=x) for x in range(numdays)]
date_str = [x.strftime("%d-%m-%Y") for x in date_list]

final_df = None
for INP_DATE in date_str:
    URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id={}&date={}".format(DIST_ID, INP_DATE)
    response = requests.get(URL)
    if (response.ok) and ('centers' in json.loads(response.text)):
        resp_json = json.loads(response.text)['centers']
        if resp_json is not None:
            df = pd.DataFrame(resp_json)
            if len(df):
                df = df.explode("sessions")
                df['min_age_limit'] = df.sessions.apply(lambda x: x['min_age_limit'])
                df['available_capacity'] = df.sessions.apply(lambda x: x['available_capacity'])
                df['date'] = df.sessions.apply(lambda x: x['date'])
                df = df[["date", "available_capacity", "min_age_limit", "pincode", "name", "state_name", "district_name", "block_name", "fee_type"]]
                if final_df is not None:
                    final_df = pd.concat([final_df, df])
                else:
                    final_df = deepcopy(df)
            else:
                st.error("No rows in the data Extracted from the API")

if len(final_df):
    final_df.drop_duplicates(inplace=True)
    final_df.rename(columns=rename_mapping, inplace=True)

    final_df = filter_column(final_df, "Minimum Age Limit", 18)
    final_df = filter_in_stock(final_df, "Available Capacity")
    table = deepcopy(final_df)
    table.reset_index(inplace=True, drop=True)
    st.table(table)
else:
    st.error("No Data Found")

if final_df is not None:
    if (len(final_df) > 0):
        hospitals = []
        [hospitals.append(x) for x in final_df["Hospital Name"] if x not in hospitals]
        sms_text = str("Cowin notification : Run for vaccine at {0}".format(hospitals))

        # To send SMS via Twilio
        account_sid = 'YOUR_TWILIO_ACCOUNT_SID'
        auth_token = 'YOUR_TWILIO_AUTH_TOKEN'
        client = Client(account_sid, auth_token)
        message = client.messages \
            .create(
            body=sms_text,
            from_='+1123456789', #Twilio  configured mobile number
            to='+919912345678' #Mobile number that needs to be notified, your personal number
        )

        #Email notify
        fromaddr = "sender@gmail.com"
        toaddr = ["xyz@gmail.com", "abc@gmail.com", "def@yahoo.com"]

        for dest in toaddr:
            msg = MIMEMultipart()
            msg['From'] = fromaddr
            msg['To'] = dest
            msg['Subject'] = "CoWin Notification By Manasa"
            body = str(hospitals)
            msg.attach(MIMEText(body, 'plain'))
            s = smtplib.SMTP('smtp.gmail.com', 587)
            s.starttls()
            s.login(fromaddr, "YOUR_GMAIL_PASSWORD")
            text = msg.as_string()
            s.sendmail(fromaddr, dest, text)
            s.quit()
    else:
        print("Do nothing")
else:
    print("Do nothing")

st.markdown("Manasa Kaleyanda Lokesh")


#!/usr/bin/env python3

import tabs as t

""" This module may be used to create apple scripts that control
    GP7 note entering """

def getScriptHeader():
    return "\n".join(map(lambda x: x.strip(), """
        activate application "Guitar Pro 7"
        delay 0.1

        set keycode_right to 124
        set keycode_left to 123
        set keycode_up to 126
        set keycode_down to 125
        set keycode_enter to 36

        set keydelay to 0.025
        """.splitlines()))+"\n"

def selectCurrentChord():
    return "\n".join(map(lambda x: x.strip(), """
    tell application "System Events" to tell process "Guitar Pro 7"
	    key code keycode_up using {shift down}
    end tell
    delay keydelay
    """.splitlines()))+"\n"

def togglePalmMute():
    return "\n".join(map(lambda x: x.strip(), """
    tell application "System Events" to tell process "Guitar Pro 7"
	    keystroke "p"
    end tell
    delay keydelay
    """.splitlines()))+"\n"

def nextNote():
    return "\n".join(map(lambda x: x.strip(), """
    tell application "System Events" to tell process "Guitar Pro 7"
	    key code keycode_right
    end tell
    delay keydelay
    """.splitlines()))+"\n"

def stringUp():
    return "\n".join(map(lambda x: x.strip(), """
    tell application "System Events" to tell process "Guitar Pro 7"
	    key code keycode_up
    end tell
    delay keydelay
    """.splitlines()))+"\n"

def stringDown():
    return "\n".join(map(lambda x: x.strip(), """
    tell application "System Events" to tell process "Guitar Pro 7"
	    key code keycode_down
    end tell
    delay keydelay
    """.splitlines()))+"\n"

def putKeystrokes(keys):
    return "\n".join(map(lambda x: x.strip(), """
    tell application "System Events" to tell process "Guitar Pro 7"
	    keystroke "KEYSTROKES"
    end tell
    delay keydelay
    """.splitlines())).replace("KEYSTROKES",keys)+"\n"

def setAltText(text):
    return putKeystrokes("t") + putKeystrokes(text) + "\n".join(map(lambda x: x.strip(), """
    tell application "System Events" to tell process "Guitar Pro 7"
	    key code keycode_enter
    end tell
    delay keydelay
    """.splitlines()))+"\n"

def enterTabScript(columnwise,auxinfo={},title=""):
    current_string = 0
    collate = [getScriptHeader()]

    if title:
        collate.append(setAltText(title))

    for i,c in enumerate(columnwise):
        if i in auxinfo:
            aux = auxinfo[i]
        else:
            aux = {}
        is_a_rest = True
        for j,f in enumerate(c):
            if f == t.unplayedString:
                continue
            if (current_string != j):
                if current_string > j:
                    for k in range(j,current_string):
                        collate.append(stringDown())
                else:
                    for k in range(current_string,j):
                        collate.append(stringUp())
                current_string = j
            is_a_rest = False
            collate.append(putKeystrokes(str(f)))
        if is_a_rest:
            collate.append(putKeystrokes("r"))

        if "pm" in aux:
            collate.append(selectCurrentChord())
            collate.append(putKeystrokes("p"))
        collate.append(nextNote())
    return "\n".join(collate)


#!/usr/bin/env python3
import random
from ponysay import ponysay

for i in range(0, 1000):
	pony = random.choice(ponysay.list_ponies_with_quotes())
	print(ponysay.render_pony(pony, ponysay.random_quote(pony),
					  balloonstyle=ponysay.balloonstyles['cowsay'],
					  width=ponysay.termwidth,
					  center=True,
					  centertext=False))


from sklearn.linear_model import LinearRegression, RidgeCV
import numpy as np
from statsmodels.tsa.arima_model import ARIMA

class myARIMA:
    """
    Simple ARIMA model
    """

    def __init__(self, data):
        """
        Parameters
        ----------
        data: timeseries
            data to fit

        """
        self.X = data
        self.model = ARIMA(self.X, order=(1,1,0))

    def fit(self, **kwargs):
        """
        Model fitting
        Parameters
        ----------
        **kwargs: dict
            Key-words parameters for fitting.
        """

        self.results = self.model.fit(disp=-1, trend='nc')

    def predict(self, steps=1):
        """
        Model prediciton
        Parameters
        ----------
        steps: int
            Number of prediction steps
        """

        y_pred = np.empty((steps, ), dtype=np.float64)
        for step in range(steps):
            y_pred[step] = self.results.predict(len(self.X) + step, len(self.X) + step)[-1]
            self.X[:-1] = self.X[1:]
            self.X[-1] = y_pred[step]
            self.model = ARIMA(self.X, order=(1,1,0))
            self.results = self.model.fit(disp=-1, trend='nc')
        return y_pred


import argparse
import os

import tensorflowjs as tfjs
from kito import reduce_keras_model

from utils.anchors import anchors_for_shape
from model import efficientdet

import argparse
import time
import os

from model import efficientdet
from utils import preprocess_image
from utils.anchors import anchors_for_shape

parser = argparse.ArgumentParser(description="Convert to tfjs")
parser.add_argument("h5file", type=str, help="path to h5 file")
parser.add_argument("out_dir", help="will create this directory if doesn't exist")
parser.add_argument("--summary", action="store_true", default=False)
parser.add_argument(
    "--kito", action="store_true", default=False, help="Reduce a model using kito",
)
parser.add_argument(
    "--weighted",
    action="store_true",
    default=False,
    help="use a model model with weighted biFPN layers. currently not supported",
)

parser.add_argument(
    "--phi", type=int, default=0,
    required=True, help="phi constant between 0 and 7 inclusive"
)

parser.add_argument("--quantization_dtype", default=None)

args = parser.parse_args()

# Disable cuda for this becuase it slows down startup
os.environ["CUDA_VISIBLE_DEVICES"] = ""

model_path = args.h5file  #'pascal_10_0.6209_1.3075_0.7892.h5'
tfjs_target_dir = args.out_dir

phi = args.phi
weighted_bifpn = args.weighted

VOC_CLASSES = 20
image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
image_size = image_sizes[phi]

num_classes = VOC_CLASSES
score_threshold = 0.5

image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
image_size = image_sizes[phi]

anchors = anchors_for_shape((image_size, image_size))

model, prediction_model = efficientdet(
    phi=phi,
    weighted_bifpn=weighted_bifpn,
    num_classes=num_classes,
    score_threshold=score_threshold,
    no_filter=True,
    drop_connect_rate=0,  # Remove dropout layers
    anchors=anchors   # Include anchor boxes in the output
)

prediction_model.load_weights(model_path, by_name=True)

if args.kito:
    print("running Keras Inference Time Optimization")
    final_model = reduce_keras_model(prediction_model)
else:
    final_model = prediction_model

if args.summary:
    final_model.summary()

tfjs.converters.save_keras_model(final_model, tfjs_target_dir, args.quantization_dtype)


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

# -*- coding:utf-8 -*-
# 矩形覆盖 就是斐波那契数列
class Solution:
    def rectCover(self, number):
        # write code here
        if number <= 0:
            return 0
        if number == 1:
            return 1
        if number == 2:
            return 2
        front = 1
        res = 2
        for i in range(3, number+1):
            tmp = res
            res += front
            front = tmp
        return res


import os
from typing import List

import cv2
import numpy as np
from mtcnn.mtcnn import MTCNN

# running this more than once in same console throws an exception, hence this guard:
try:
    detector = MTCNN()
except ValueError as ve:
    # doing the job of this silly library:
    if not ve.args[0].startswith("Variable pnet/conv1/weights already exists"):
        raise ve

PADDING = 32

def cropped_face(image: np.ndarray, bounding_box: List[int]) -> np.ndarray:
    x, y, w, h = bounding_box
    top = max(0, y - h)
    bottom = y + 2 * h
    left = max(0, x - w)
    right = x + 2 * w
    return image[top:bottom, left:right]
#
#
#
# img_array = cv2.imread("datasets/videos/manual_zuck_screenshot.png")
# faces = detector.detect_faces(img_array)
# cv2.imwrite("datasets/videos/uncropped_face.jpg", img_array)
# cv2.imwrite("datasets/videos/cropped_face.jpg", cropped_face(img_array, faces[0]['box']))

videos_dir = "datasets/videos/"
filename = "how_to_build_the_future.mkv"

video_path = videos_dir + filename
filename_w_o_extension = filename[:-4]
faces_dir = "datasets/videos/faces/%s_better_crop/MZ/" % filename_w_o_extension
os.makedirs(faces_dir, exist_ok=True)

capture = cv2.VideoCapture(video_path)

while capture.isOpened():
    frame_number = capture.get(cv2.CAP_PROP_POS_FRAMES)

    ret, frame = capture.read()

    # proxy for end of video:
    if not ret:
        break

    # only detect faces every FPS frames (i.e. every second):
    if not (0 == int(frame_number % capture.get(cv2.CAP_PROP_FPS))):
        continue

    # just for testing purposes, don't want to do entire video
    # if frame_number > 30 * 10:
    #     break

    faces = detector.detect_faces(frame)

    for face_num, face in enumerate(faces):
        cropped = cropped_face(frame, face['box'])
        filename = faces_dir + "frame_%s_face_%s.jpg" % (frame_number, face_num)
        cv2.imwrite(filename, cropped)

# releasing for now, because it seems to prevent me from opening the file in VLC (?!)
capture.release()


import numpy as np
# hidden contains number of nodes in each hidden layer
# n: inputs
# m: number of nodes in the first hidden layer
# You must make sure that Ws and bs contain the appropriate dimensions
# Ws: List or dictionary containing matrices representing parameters in each layer
# bs: the corresponding biases

def build_mlp_structure(nin, mlp_hidden):
    mlp_structure = [nin] + mlp_hidden + [1]
    wb_shapes = []
    for i in range(len(mlp_hidden) + 1):
        wb_shapes.append((mlp_structure[i], mlp_structure[i + 1]))
        wb_shapes.append((1, mlp_structure[i + 1]))
    wb_sizes = [h * w for h, w in wb_shapes]
    neurons_cnt = np.sum(np.array(wb_sizes))
    print('Total trainable parameters is like', neurons_cnt)
    return neurons_cnt, wb_shapes, wb_sizes

def func_format_weights(theta_params, wb_sizes, wb_shapes):
    wb_size_new = []
    sum_sizes = 0
    wb_sizes_array = np.asarray(wb_sizes)
    # Format the size entries using number python package
    for k in range(len(wb_sizes_array)):
        sum_sizes += wb_sizes_array[k]
        wb_size_new.append(sum_sizes)
    # ##### calculate length of input and bias params
    l_params_values = np.split(theta_params, wb_size_new)
    for i in range(len(l_params_values) - 1):
        l_params_values[i] = np.reshape(l_params_values[i], wb_shapes[i])
    ws_classify = l_params_values[0:][::2]
    bs_classify = l_params_values[1:][::2]
    return ws_classify, bs_classify

def process_ws_bs(ws0, bs0):
    ws_red = ws0[0:-1]
    lst_ws_bs = []
    for k in range(len(bs0)):
        reshaped_ws = np.r_[ws_red[k], bs0[k]]
        # print(reshaped_ws.shape)
        lst_ws_bs.append(reshaped_ws)
    return lst_ws_bs

def func_full_matrices(lst_weights):
    # empty lists
    full_flow_matrix = lst_weights.copy()
    # set index from forward flow from first layer

    # Check weights and determine flow coming from backward to front
    for w_idx in range(len(full_flow_matrix)-1):
        k0_index = w_idx + 1
        lst_new_matrices = [full_flow_matrix[0]]
        # print(full_flow_matrix[w_idx].shape)
        # print(full_flow_matrix[k0_index].shape)
        # original shape of matrix
        if k0_index <= len(full_flow_matrix):
            for col_idx in range(full_flow_matrix[w_idx].shape[1]):
                if np.sum(full_flow_matrix[w_idx][:, col_idx]) == 0.0:
                    if col_idx < full_flow_matrix[w_idx].shape[1]:
                        full_flow_matrix[k0_index][col_idx, :] = 0.0
                    else:
                        continue
    # reverse mode
    mat_arrange = full_flow_matrix.copy()
    mat_arrange.reverse()
    # print(len(mat_arrange))
    # from back to front
    for mat_in_m in range(len(mat_arrange)-1):
        k = mat_in_m + 1
        if k <= len(mat_arrange):
            for mat_idx in range(mat_arrange[mat_in_m].shape[0]-1):
                if np.sum(mat_arrange[mat_in_m][mat_idx, :]) == 0.0:
                    mat_arrange[k][:, mat_idx] = 0.0
        # print(k, len(mat_arrange))
    mat_arrange.reverse()
    return mat_arrange

def func_revers_wsbs(ws_bs_list):
    ws_list = []
    bs_list = []
    for mat_idx in range(len(ws_bs_list)):

        if np.mod(mat_idx, 2) == 0:
            bs_list.append(ws_bs_list[mat_idx])
        else:
            ws_list.append(ws_bs_list[mat_idx])
    return ws_list, bs_list

def func_all_params_concatenate(ws_mat, bs_mat):
    conc_matrix = []  # create an empty list to store concatenated matrix
    # ws_new = ws_mat[0:-1]
    for j_idx in range(len(ws_mat)):
        # reshape the ws matrix each
        reshape_ws_mat = np.reshape(ws_mat[j_idx], ws_mat[j_idx].shape[0] * ws_mat[j_idx].shape[1])
        # append to list
        conc_matrix.append(reshape_ws_mat)
        # concatenate bs and have x rows and 1 column
        re_bs0 = bs_mat[j_idx].reshape(-1)
        # re_bs0 = np.reshape(bs_mat[j_idx], bs_mat[j_idx].shape[0] * bs_mat[j_idx].shape[1])
        conc_matrix.append(re_bs0)  # append bs
    conc_arrays = np.concatenate(conc_matrix, axis=0)
    return np.array(conc_arrays)  # return the array of new matrix

def func_collect_allparams(allparams, sizes1, shapes1):
    # first pass the vector of parameters from program and format weight vector
    w, b = func_format_weights(allparams, sizes1, shapes1)
    params_in_list = process_ws_bs(w, b)
    # print(len(params_in_list))
    p_in_full = func_full_matrices(params_in_list)
    all_ps = func_organize(p_in_full)
    return all_ps

def func_set_to_zero(ws_and_bs):
    ws_and_bs.reverse()
    new_ws_bs = []
    for l in range(1, len(ws_and_bs)):
        # print(ws_and_bs[l])
        earlier_mat = ws_and_bs[l-1].copy()
        l_mat = ws_and_bs[l]
        for n_idx in range(earlier_mat.shape[0]):
            if np.sum(earlier_mat[n_idx, :]) == 0.0:
                l_mat[n_idx, :] = 0.0
        new_ws_bs.append(earlier_mat)
        new_ws_bs.append(l_mat)
    print(len(new_ws_bs))
    return new_ws_bs

def func_organize(new_ws_bs0):
    lsting_wsbs = []
    for k in new_ws_bs0:
        shaped_k = k[0:-1, :].shape
        ws_reshaped = np.reshape(k[0:-1, :], shaped_k[0]*shaped_k[1])
        bs_reshaped = np.reshape(k[-1, :], shaped_k[1])
        #
        lsting_wsbs.append(ws_reshaped)
        lsting_wsbs.append(bs_reshaped)
    return np.array(np.concatenate(lsting_wsbs))


def main():
    W,H,x,y = [int(x) for x in input().split()]
    area = W*H/2
    if x == W/2 and y == H/2:
        print(area,1)
    else:
        print(area,0)

if __name__ == '__main__':
    main()


from cmind import utils
import os

def preprocess(i):

    print ('')
    print ('Preprocessing ...')

    os_info = i['os_info']

    env = i['env']

    meta = i['meta']

    automation = i['automation']

    quiet = (env.get('CM_QUIET', False) == 'yes')

    print ('  ENV CM_VAR1: {}'.format(env.get('CM_VAR1','')))

    return {'return':0}

def postprocess(i):

    print ('')
    print ('Postprocessing ...')

    env = i['env']

    return {'return':0}


# Authors:
# PJ Solomon November 27 2017
# Joseph Zieg November 29 2017

from Adafruit_MotorHAT import Adafruit_MotorHAT as HAT
import RPi.GPIO as GPIO
import time

# Globals for direction control
RIGHT = 0
STRAIGHT = 1
LEFT = 2

#############
# CAR CLASS #
#############

class Car(object):
    # Controls for the rover. Uses input from sensors to determine optimal course
    # Assumes Steering = Motor1, Drive = Motor4
    # Uses 3 HR-S04 ultrasonic sensors for obstacle detection

    def __init__(self, addr=0x60, steering_id=1, drive_id=4):
        # Initialize HAT and motors
        self.mh = HAT(addr)
        self.steering = self.mh.getMotor(steering_id)
        self.motor = self.mh.getMotor(drive_id)
        self.MAX_SPEED = 255

        # Initialize sensors
        # Defaults to decision made at 30 cm
        GPIO.setmode(GPIO.BCM)
        self.usm = Sensor(12, 13) # Middle sensor
        self.usr = Sensor(5, 6)   # Right side sensor
        self.usl = Sensor(8, 7)   # Left side sensor
        self.usTriggered = None

        self.sensors = [self.usl, self.usm, self.usr]

    def drive(self, sec):
        # Decision should update frequently, currently 1/100th of a sec
        # Testing should see if this is enough
        # May want to add functionality to back up if dist to wall is ~0
        for i in range(sec * 1000):  # Runs for sec amount of seconds
            if self.collisionWarning():
                print("Collision Warning")
                for us in self.sensors:
                    print(self.sensors.index(us), us.getDistance())
                self.turn(self.MAX_SPEED / 2)
            else:
                self.steering.setSpeed(0)
                self.forward(self.MAX_SPEED)
            time.sleep(.01)

    def forward(self, speed):
        self.motor.setSpeed(speed)
        self.motor.run(HAT.BACKWARD)

    def backward(self, speed):
        self.motor.setSpeed(speed)
        self.motor.run(HAT.FORWARD)

    def right(self, speed):
        self.steering.setSpeed(self.MAX_SPEED)
        self.steering.run(HAT.BACKWARD)

        self.motor.setSpeed(speed)
        self.motor.run(HAT.FORWARD)

    def left(self, speed):
        # idk if these are the correct directions
        self.steering.setSpeed(self.MAX_SPEED)
        self.steering.run(HAT.FORWARD)

        self.motor.setSpeed(speed)
        self.motor.run(HAT.FORWARD)

    def turn(self, speed):
        if self.usTriggered == STRAIGHT:
            direction = self.directionDecision()
            if direction is RIGHT:
                self.left(speed)
            elif direction is LEFT:
                self.right(speed)

        elif self.usTriggered == LEFT:
            self.right(speed)

        elif self.usTriggered == RIGHT:
            self.left(speed)

    def directionDecision(self):
        if self.sensors[0].getDistance() < self.sensors[2].getDistance():
            return RIGHT
        else:
            return LEFT

    def collisionWarning(self):
        # Iterates over the sensor array, determines if one sensor is triggered
        # returns True if an object is within minimum safe distance for any sensor
        for us in self.sensors:
            if us.collisonWarning():
                self.usTriggered = self.sensors.index(us)
                return True
        return False

    def turnOff(self):
        self.steering.run(HAT.RELEASE)
        self.motor.run(HAT.RELEASE)
        GPIO.cleanup()

    def test(self):
        # Test sensor output
        print("Initializing Sensors")

        print("Testing Middle")
        self.usm.distanceTest()

        print("Testing Left")
        self.usl.distanceTest()

        print("Testing Right")
        self.usr.distanceTest()

        print("Testing Driving Capabilities")
        # Test obstacle detection and motor response
        self.drive(30)

        # turn off motor
        self.turnOff()

################
# SENSOR CLASS #
################

class Sensor(object):
    # Sensor object for use with Car class
    # GPIO code structure for getDistance() function taken from:
    # https://www.modmypi.com/download/range_sensor.py

    def __init__(self, echo, trigger, min_distance=.2):
        self.MIN_DISTANCE = min_distance * 100
        # set GPIO Pins
        self.GPIO_TRIGGER = trigger
        self.GPIO_ECHO = echo
        GPIO.setwarnings(False)
        GPIO.setup(self.GPIO_TRIGGER, GPIO.OUT)
        GPIO.setup(self.GPIO_ECHO, GPIO.IN)

        GPIO.output(self.GPIO_TRIGGER, False)
        time.sleep(2)

    def collisonWarning(self):
        if self.getDistance() <= self.MIN_DISTANCE:
            return True
        return False

    def getDistance(self):
        # Returns the distance in cm given by the sensor

        GPIO.output(self.GPIO_TRIGGER, True)
        time.sleep(0.00001)
        GPIO.output(self.GPIO_TRIGGER, False)

        while GPIO.input(self.GPIO_ECHO) == 0:
            pulse_start = time.time()

        while GPIO.input(self.GPIO_ECHO) == 1:
            pulse_end = time.time()

        pulse_duration = pulse_end - pulse_start

        distance = pulse_duration * 17150

        distance = round(distance, 2)
        return distance

    def distanceTest(self):
        # Tests the sensor input, outputs to console for 10s
        for i in range(3):
            dist = self.getDistance()
            print ("Measured Distance = %.1f cm" % dist)
            if self.collisonWarning():
                print("!!Min Distance Reached!!")
            time.sleep(.1)

####################
# CAR RUNNING CODE #
####################
try:
    car = Car()

    car.test()
    print("Program closed")
except KeyboardInterrupt:
    car.turnOff()
    print("Program closed")


# Import all packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.io

# Create table using df of treatment dict
treatment = {'name': ['Daniel', 'John', 'Jane'],
 			'sex': ['male', 'male', 'female'],
 			'treat_a': ['18', 12, 24],
 			'treat_b': [42, 31, 27]}

treat_df = pd.DataFrame(data=treatment)
print(treat_df)

# Check the type of data in treat_df
# print(treat_df.dtypes)
treat_df['sex'] = treat_df['sex'].astype('category')
treat_df['treat_a'] = pd.to_numeric(treat_df['treat_a'], downcast = 'integer', errors='coerce')
treat_df.set_index('name')
# print(treat_df.dtypes)
# print(treat_df)

# Apply .apply method to df
new_treat_df = treat_df[['name', 'treat_a', 'treat_b']].set_index('name')
print(new_treat_df)
print(new_treat_df.apply(np.mean, axis=0)) # mean of col
print(new_treat_df.apply(np.mean, axis=1)) # mean of row


import cv2
import numpy as np
import os
import dlib

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

detector = dlib.get_frontal_face_detector()

name = input("Enter your name: ")

frames = []
outputs = []

while True:

    ret, frame = cap.read()
    gray = []

    if ret:

        faces = detector(frame)

        for face in faces:
            x = face.left()
            y = face.top()
            w = face.right() - x
            h = face.bottom() - y

            cut = frame[y:y+h, x:x+w]
            fix = cv2.resize(cut, (100, 100))
            gray = cv2.cvtColor(fix, cv2.COLOR_BGR2GRAY)
            cv2.imshow("My Face", gray)

        cv2.imshow("My Screen", frame)

    key = cv2.waitKey(1)
    if key == ord("q"):
        break
    if key == ord("c"):
        frames.append(np.array(gray).flatten())
        outputs.append([name])

X = np.array(frames)
y = np.array(outputs)

data = np.hstack([y, X])

f_name = "face_data.npy"

if os.path.exists(f_name):
    old = np.load(f_name)
    data = np.vstack([old, data])

np.save(f_name, data)

cap.release()
cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
'''
plot
figure
label
legend

中文字体
特殊符号

'''

x = np.linspace(-3, 5, 50)
y1 = x * 2 + 1
y2 = x ** 2

'''
用不同的图绘制
plt.figure()
plt.plot(x, y1)

plt.figure()
plt.plot(x, y2, color="red", linewidth=.4, linestyle='--')
plt.show()
'''
plt.figure()
l1, = plt.plot(x, y1, label="line1")
l2, = plt.plot(x, y2, color="red", linewidth=.4, linestyle='--', label="line2")

'''
设置图中值的取值范围
'''
# plt.xlim(-2, 2)
# plt.ylim(-2, 2)
'''
设置x, y label
'''
plt.xlabel("I am x")
plt.ylabel("I am y")
#x_ticks = np.linspace(1, 9, 1)
#plt.xticks(x_ticks)
'''
斜体用$$包括, 空格需要转义
r"$\alpha$"  数学alpha符号
'''
#plt.yticks([-1, 0, 1, 2, 3], [r"$\alpha$", "bad", "normal", "good", "$very\ good$"])

ax = plt.gca() # get current axis
#ax.spines['right'].set_color("red")
'''
将坐标轴交叉点设置在(0, 0)
'''
'''
ax.spines['right'].set_color("none")
ax.spines['top'].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
'''

ax.legend(loc="best")
'''
loc
    best
    upper right
'''
plt.show()


from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from . import views

app_name = 'budget'
urlpatterns = [
    # login view
    url(r'^login/$', views.LoginView.as_view(), name='login'),
    # new user account redirect
    url(r'^login/new/$', views.NewLoginView.as_view(), name='new_user'),
    # logout view
    url(r'^logout/$', login_required(views.LogoutView.as_view()), name='logout'),
    # home page
    url(r'^$', login_required(views.IndexView.as_view()), name='index'),
    # create budget redirect
    url(r'^budgets/$', login_required(views.CreateBudgetView.as_view()), name='budget_create'),
    # budget view
    url(r'^budgets/(?P<pk>[0-9]+)/$', login_required(views.BudgetView.as_view()), name='budget'),
    # create budget-item redirect
    url(r'^budgets/(?P<budget_id>[0-9]+)/item/$', login_required(views.ItemCreateView.as_view()), name='item_create'),
    # delete budget-item redirect
    url(r'^budgets/(?P<budget_id>[0-9]+)/delete/$', login_required(views.ItemDeleteView.as_view()), name='item_delete'),
    # budget-item detail view may be used later
    #url(r'^budgets/(?P<budget_id>[0-9]+)/item/(?P<pk>[0-9]+)/$', login_required(views.ItemView.as_view()), name='item'),
    # create account redirect
    url(r'^accounts/$', login_required(views.CreateAccountView.as_view()), name='account_create'),
    # account view
    url(r'^accounts/(?P<pk>[0-9]+)/$', login_required(views.AccountView.as_view()), name='account'),
    # link budget redirect
    url(r'^accounts/(?P<account_id>[0-9]+)/link/$', login_required(views.BudgetLinkView.as_view()), name='link_budget'),
    # create transaction redirect
    url(r'^accounts/(?P<account_id>[0-9]+)/transaction/$', login_required(views.TransactionCreateView.as_view()), name='transaction_create'),
    # delete transaction redirect
    url(r'^accounts/(?P<account_id>[0-9]+)/delete/$', login_required(views.TransactionDeleteView.as_view()), name='transaction_delete'),
    # transaction view may be used later
    # url(r'^accounts/(?P<account_id>[0-9]+)/transaction/(?P<pk>[0-9]+)/$', login_required(views.TransactionView.as_view()), name='transaction')
]


import os
import random
import threading

# '''ClusterServer.exe -p 8061 -n qqq -d 3000 -a'''

word = 'qqq'
threads = []

# for e in range(8060, 8080):
for e in (8010, 8020, 8060):
    threads.append(
        threading.Thread(
            target=os.system,
            args=(f'ClusterServer.exe -p {e} -n {word} -d {random.randint(100, 1500)} -a',)
		)
	)

for e in threads:
    e.start()


def merge_arrays(A,B):
    i=0
    j=0
    m=len(A)
    n=len(B)
    ans=[]
    while i<m and j<n:
        if A[i]<B[j]:
            ans.append(A[i])
            i=i+1
        elif A[i]>B[j]:
            ans.append(B[j])
            j=j+1
    while i<m:
        ans.append(A[i])
        i=i+1
    while j<n:
        ans.append(B[j])
        j=j+1
    return ans

def merge_k(ar):
    ans=ar[0]
    i=1
    while i<len(ar):
        ans=merge_arrays(ans,ar[i])
        i=i+1
    return ans

ar=[[1,4,7],[2,5,8],[3,6,9]]
res=merge_k(ar)
print(res)


from django.contrib.gis.geos import Point
from rest_framework.serializers import ModelSerializer, Field, ValidationError

from .models import Bookmark

class SimplifiedGeometryField(Field):
    """
    don't deal w/ the WKT serialization of the GeoDJango field
    just deal w/ a simple array
    """

    def __init__(self, *args, **kwargs):
        self.precision = kwargs.pop("precision", 12)
        self.geometry_class = kwargs.pop("geometry_class")
        super().__init__(*args, **kwargs)

    def to_representation(self, value):
        return map(lambda x: round(x, self.precision), value.coords)

    def to_internal_value(self, data):
        try:
            return self.geometry_class(data)
        except TypeError as e:
            raise ValidationError(str(e))

class BookmarkGeoSerializer(ModelSerializer):
    # _not_ using GeoFeatureModelSerializer b/c I do not want to convert the whole queryset to GeoJSON
    class Meta:
        model = Bookmark
        fields = ("id", "owner", "title", "zoom", "center", "feature_collection")

    center = SimplifiedGeometryField(geometry_class=Point, precision=Bookmark.PRECISION)


__version__ = '0.2'
__author__ = 'Sergii Naumov'

import os
import sys
import hashlib
import argparse
import ntpath
import json
import re
import datetime
from multiprocessing.dummy import Pool as ThreadPool

from virus_total import VirusTotal
from vt_report import VTReport
from vt_report_generator import VTReportOutputGenerator

parser = argparse.ArgumentParser()
parser.add_argument(
    '-get', action='store_true', help='Get existing report from Virus Total.'
    )
parser.add_argument(
    '-submit',
    action='store_true',
    help='Submit files on Virus Total. Not supported yet.'
    )
parser.add_argument('-hash', metavar='', help='File hash.')
parser.add_argument('-f', metavar='', help='File name.')
parser.add_argument('-d', metavar='', help='Directory with files.')
parser.add_argument(
    '-hash_file', metavar='', help='File with sha1/sha256/md5 hashes.'
    )
parser.add_argument('-log', metavar='', help='Store results to log file.')
parser.add_argument(
    '-v_off',
    action='store_true',
    help='Turn off verbose mode. Works only if log on.'
    )

valid_hash = re.compile('([a-fA-F\d]{32}|[a-fA-F\d]{40}|[a-fA-F\d]{64})$')
re_hash_file = re.compile(
    '([a-fA-F\d]{32}|[a-fA-F\d]{40}|[a-fA-F\d]{64})(?:\s|,|$)'
    )

def create_requests_pool(instance, data):
    vt = VirusTotal(instance)
    pool = ThreadPool(4)
    pool.map(vt.execute, data)
    pool.close()
    pool.join()
    return vt.Reports

def submit_files_to_vt(data):
    results = create_requests_pool('submit', TEST_DATA)

def seconds_to_h_m_s(seconds):
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    return [int(h), int(m), int(s)]

def file_sha1sum(fname):
    hash_sha1 = hashlib.sha1()
    with open(fname, 'rb') as f:
        for chunk in iter(lambda: f.read(4096), b''):
            hash_sha1.update(chunk)
    return hash_sha1.hexdigest()

def is_valid_hash(file_hash):
    return valid_hash.match(file_hash) is not None

def check_files_on_vt(data, log_file=None):
    if len(data) > 0:
        vt_generator = VTReportOutputGenerator()
        estimated_time_s = 4  # Emperical value
        estimated_time = [0, 0, estimated_time_s]

        if len(data) > VirusTotal.REQUEST_LIMIT:
            estimated_time_s = (len(data)
                                / VirusTotal.REQUEST_LIMIT
                                * VirusTotal.TIME_INTERVAL)
            estimated_time = seconds_to_h_m_s(estimated_time_s)
        t = (datetime.datetime.now()
             + datetime.timedelta(seconds=estimated_time_s))

        print('{0} file(s) will be checked on VT.'.format(len(data)))
        print('Current VT limit: {0} requests per {1} seconds.\n'.format(
            VirusTotal.REQUEST_LIMIT, VirusTotal.TIME_INTERVAL)
        )
        print('Current time: {}'.format(
            datetime.datetime.now().strftime("%H:%M:%S"))
        )
        print('Estimated delta time: {0:02}:{1:02}:{2:02}'.format(
            *estimated_time)
        )
        print('Estimated completion time: {}\n'.format(
            t.time().strftime("%H:%M:%S"))
        )

        results = create_requests_pool('check', list(data.keys()))
        vt_report = VTReport(results)
        vt_generator.print_results(vt_report, data)

        if log_file:
            vt_generator.save_results(vt_report, data, log_file)

def is_valid_arguments(args):
    if len(sys.argv) < 3:
        return False
    if not (args.get ^ args.submit):
        return False
    if args.submit and args.hash:
        return False
    if ((args.log is None) and (args.v_off is True)):
        return False
    return (int(args.hash is not None)
            + int(args.f is not None)
            + int(args.d is not None)
            + int(args.hash_file is not None)) == 1

if __name__ == '__main__':
    with open('logos/logo.txt', 'r') as f:
        print(f.read())
    print('Just Another Virus Total Submitter.')
    print('version = {0}\n'.format(__version__))

    with open('config.json', 'r') as f:
        config = json.load(f)
        if not is_valid_hash(config['VirusTotalKey']):
            print(
                'Invalid key. '
                'Please add valid VirusTotal key in config.json file.\n'
                )
            sys.exit(0)

        VirusTotal.API_KEY = config['VirusTotalKey']

    args = parser.parse_args()
    entities = {}

    if not is_valid_arguments(args):
        parser.print_help()
        sys.exit(0)

    if args.v_off:
        VTReportOutputGenerator.VERBOSE_MODE = False

    if args.submit:
        print('Submit method not yet implemented.')
    else:
        if args.hash is not None:
            if is_valid_hash(args.hash):
                entities[args.hash] = args.hash

        elif args.f is not None:
            entities[file_sha1sum(args.f)] = args.f

        elif args.d is not None:
            if os.path.isdir(args.d):
                for root, sub_folders, files in os.walk(args.d):
                    for f in files:
                        fpath = os.path.join(root, f)
                        entities[file_sha1sum(fpath)] = fpath

        elif args.hash_file is not None:
            with open(args.hash_file, 'r') as f:
                result = re_hash_file.findall(f.read())
                print('{} hashes was read from {}'.format(
                    len(result), args.hash_file)
                )
                result = set(result)
                print('({} unique hashes.)\n'.format(len(result)))
                for h in result:
                    entities[h] = h

        check_files_on_vt(entities, args.log)

    print('\n')
    print('°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸')


# very rough bounds!
typemap = { (-180,0, 90, 180) : "BR", (-150, -30, -60, 60) : "AR", (0, 180, 90, 180) : "BL", (30, 150, -60, 60) : "AL" }

def determinetype(phipsi):
    phi, psi = phipsi
    for bound in typemap:
        if bound[0] < phi < bound[1] and bound[2] < psi < bound[3]:
            return typemap[bound]
    return "?"

def my_phi_psi(selection):
    r = cmd.get_phipsi(selection)

    if r is not None:
        keys = r.keys()
        keys.sort()

        cmd.feedback('push')
        cmd.feedback('disable','executive','actions')
        for key in keys:
            phipsiType = determinetype(r[key])
            argtuple = r[key] + (phipsiType,)
            cmd.iterate("(%s`%d)" % key, "print ' %-5s " + ("( %4.0f, %4.0f ) %s" % argtuple) + "'%(resn+'-'+resi+' '+chain+':')")
        cmd.feedback('pop')

def motif(residueRange, chain=None):
    """
    Use like "motif 10-12" to show a backbone segment from residues 10 to 12.
    """
    name = residueRange
    if chain is None:
        selection = "name ca+n+c+o+h and not hetatm and resi %s" % residueRange
    else:
        selection = "name ca+n+c+o+h and not hetatm and resi %s and chain %s" % (residueRange, chain)
    cmd.select(name, selection)
    cmd.show("sticks", name)
    cmd.zoom(name)
    cmd.disable(name)
    cmd.label("name ca and %s" % selection, "resi")
    my_phi_psi(name)

cmd.extend("motif", motif)
cmd.extend("my_phi_psi", my_phi_psi)


import re
import pandas as pd

#makes a data frame out of .results file

# (1) run .parseResults(.results)
# (2) run .makeDataFrame(output of 1)
# (3) run .calculateTotal(output of 2, "WER")

#parse .results file into dictionary (that can be converted to data frame
    #key is field
    #value is list of values for fields (each item in list is a row in dataframe)
def parseResults(f):
    data = open(f, "rb")

    #initialize dict
    dict = {}
    #initialize value lists
    dict["title"] = []
    dict["WER"] = []
    dict["inc_words"] = []
    dict["total_words"] = []
    dict["insertions"] = []
    dict["deletions"] = []
    dict["substitutions"] = []
    dict["SER"] = []
    dict["inc_utterances"] = []
    dict["total_utterances"] = []

    #regexes
    #nan
    nanRegex = r'.*nan.*'
    #SER
        #group 1 = SER
        #group 2 = incorrect utterances
        #group 3 = total utterances
    serRegex = r'\%SER ([0-9]+\.[0-9]{2}) \[ ([0-9]+) \/ ([0-9]+) \]'
    #WER
        #group 1 = WER
        #group 2 = incorrect words
        #group 3 = total words
        #group 4 = insertions
        #group 5 = deletions
        #group 6 = insertions
        #group 7 = deletions
        #group 8 = substitutions
    werRegex = r'\%WER ([0-9]+\.[0-9]{2}) \[ ([0-9]+) \/ ([0-9]+), ([0-9]+) ins, ([0-9]+) del, ([0-9]+) sub \]'

    #iterate through lines
    for line in data:
        if line.startswith("%SER"):
            #is SER
            matched = re.match(serRegex, line.rstrip())
            if re.match(nanRegex, line):
                dict["SER"].append("NaN")
                dict["inc_utterances"].append(0.0)
                dict["total_utterances"].append(0.0)
            else:
                dict["SER"].append(float(matched.group(1)))
                dict["inc_utterances"].append(float(matched.group(2)))
                dict["total_utterances"].append(float(matched.group(3)))
        elif line.startswith("%WER"):
            #is WER
            matched = re.match(werRegex, line.rstrip())
            if re.match(nanRegex, line):
                dict["WER"].append("NaN")
                dict["inc_words"].append(0.0)
                dict["total_words"].append(0.0)
                dict["insertions"].append(0.0)
                dict["deletions"].append(0.0)
                dict["substitutions"].append(0.0)
            else:
                dict["WER"].append(float(matched.group(1)))
                dict["inc_words"].append(float(matched.group(2)))
                dict["total_words"].append(float(matched.group(3)))
                dict["insertions"].append(float(matched.group(4)))
                dict["deletions"].append(float(matched.group(5)))
                dict["substitutions"].append(float(matched.group(6)))
        elif not line.startswith("=") and not line.startswith("Scored"):
            #is title
            dict["title"].append(line.rstrip())

    data.close()
    return dict

#make dataframe
def makeDataFrame(dict):
    df = pd.DataFrame(dict)

    return df

#df.describe()

#calculate total WER
    #df = DataFrame
    #errorType = "WER" or "SER"
def calculateTotal(df, errorType):
    if errorType == "WER":
        all = df["total_words"].sum()
        all_inc = df["inc_words"].sum()
    elif errorType == "SER":
        all = df["total_utterances"].sum()
        all_inc = df["inc_utterances"].sum()
    else:
        all = df["total_words"].sum()
        all_inc = df["inc_words"].sum()

    return (all_inc / all) * 100


# *###################
# * IMPORTS
# *###################

from values.value_list import List
from values.value_number import Number
from runtime_results import RTResult

from errors import RTError

# *###################
# * POP
# *###################

def execute_pop_func(self, exec_ctx):
    list_ = exec_ctx.symbol_table.get("list")
    index = exec_ctx.symbol_table.get("index")

    if not isinstance(list_, List):
        return RTResult().failure(RTError(
            self.pos_start, self.pos_end,
            "First argument must be a list",
            exec_ctx
        ))

    if not isinstance(index, Number):
        return RTResult().failure(RTError(
            self.pos_start, self.pos_end,
            "Second argument must be an integer",
            exec_ctx
        ))

    try:
        element = list_.elements.pop(index.value)
    except IndexError:
        return RTResult().failure(RTError(
            self.pos_start, self.pos_end,
            f"Element at index '{index.value}' could not be removed as it is out of range of the list.",
            exec_ctx
        ))
    return RTResult().success(element)


# TO-DO: complete the helper function below to merge 2 sorted arrays
def merge(left, right):
    # init the combined list
    # that will hold the sorted elements from left and right
    combined = []

    #init the pointers that start at the beginning of each list
    a = 0
    b = 0

    # traverse the lists
    while a < len(left) and b < len(right):
        # compare elements from pointers
        if left[a] < right[b]:
            combined.append(left[a])
            a += 1
        else:
            combined.append(right[b])
            b += 1

    # at this point, we have finished traversing one list completely
    # we need to add all of the elements from the other list to the combined list
    while a < len(left):
        combined.append(left[a])
        a += 1
    while b < len(right):
        combined.append(right[b])
        b += 1

    return combined

# TO-DO: implement the Merge Sort function below USING RECURSION
def merge_sort(arr):
    # break the array down recursively
    # base case: when the list gets down to single elements
    if len(arr) > 1:
        # break down array halves until they reach the point
        # where we have arrays of single elements
        left = merge_sort(arr[:len(arr) // 2])
        right = merge_sort(arr[len(arr) // 2:])

        # merge them back up
        arr = merge(left, right)

    return arr

# implement an in-place merge sort algorithm
def merge_in_place(arr, start, mid, end):
    # make copies of both arrays that we are trying to merge
    # second parameter is non-inclusive so we need to add 1
    left_array = arr[start: mid + 1]
    right_array = arr[mid + 1: end + 1]

    # set up initial values to keep track of progress through array
    left_index = 0
    right_index = 0
    sorted_index = start

    # iterate through both copies until we have no elements left in one of them
    while left_index < len(left_array) and right_index < len(right_array):
        # if the left side has the smaller element
        # place it in the sorted part and move left_array forward
        if left_array[left_index] <= right_array[right_index]:
            arr[sorted_index] = left_array[left_index]
            left_index += 1
        else:
            arr[sorted_index] = right_array[right_index]
            right_index += 1
        sorted_index += 1

    # here we have no elements in left or right
    # go through remaining elements and add them
    while left_index < len(left_array):
        arr[sorted_index] = left_array[left_index]
        left_index += 1
        sorted_index += 1

    while right_index < len(right_array):
        arr[sorted_index] = right_array[right_index]
        right_index += 1
        sorted_index += 1
    return arr

def merge_sort_in_place(arr, left, right):
    if left >= right:
        return arr

    mid = (left + right) // 2
    merge_sort_in_place(arr, left, mid)
    merge_sort_in_place(arr, mid + 1, right)
    merge_in_place(arr, left, mid, right)
    return arr

# STRETCH: implement the Timsort function below
# hint: check out https://github.com/python/cpython/blob/master/Objects/listsort.txt
def timsort(arr):
    # Your code here

    return arr


from pyface.qt import QtGui, QtCore
#%matplotlib qt
import mne
import glob
import sys
import numpy as np
import os.path as op
import pandas as pd
# import matplotlib
# matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
from mayavi import mlab
from scipy.spatial.distance import euclidean
from intra_extra_fx import find_stim_coords, get_stim_params, plot_source_space, save_results
from intra_extra_info import study_path, subjects_dir
import re
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.patches as mpatches
import nibabel as nib
pd.set_option('display.expand_frame_repr', False)

def make_fwd_solution(cond_fname, fs_subj, study_path):
    eeg_epo = mne.read_epochs(cond_fname)
    img_type = 'anony' if fs_subj.find('_an') > 0 else 'orig'
    subj = fs_subj.strip('_an')

    trans_fname = op.join(study_path, 'source_stim', subj, 'source_files', img_type,'%s_fid-trans.fif' % fs_subj)
    src_fname = op.join(study_path, 'source_stim', subj, 'source_files', img_type, '%s-oct5-src.fif' % fs_subj)
    bem_fname = op.join(subjects_dir, fs_subj, 'bem', '%s-bem-sol.fif' % fs_subj)

    fwd = mne.make_forward_solution(eeg_epo.info, trans_fname, src_fname, bem_fname)

    trans = mne.read_trans(trans_fname)
    mne.viz.plot_alignment(eeg_epo.info, trans, subject=subj, surfaces=['head', 'brain'], subjects_dir=subjects_dir)
    # mlab.show()

    mne.write_forward_solution(op.join(study_path, 'source_stim', subj, 'source_files', img_type,
                                       '%s-fwd.fif' % fs_subj), fwd, overwrite=True)
    return fwd

def source_loc(evoked, cov, fwd, subj, img_type, study_path, plot=False):
    cond = evoked.info['description']
    stim_coords = find_stim_coords(cond, subj, study_path)
    stim_params = get_stim_params(cond)

    inv = mne.minimum_norm.make_inverse_operator(evoked.info, fwd, cov, loose=0.2, depth=0.8)
    method = "dSPM"
    snr = 300.
    lambda2 = 1. / snr ** 2
    stc = mne.minimum_norm.apply_inverse(evoked, inv, lambda2, method=method, pick_ori=None)
    stc.save(op.join(study_path, 'source_stim', subj, 'source_files', img_type,'stc', '%s_%s' %(subj, cond)))

    # plot_source_space(subj, study_path, subjects_dir)

    hemi = 'lh' if stim_params['is_left'] else 'rh'
    contact_nr = int(re.findall('\d+', stim_params['ch'])[0])
    view = 'medial' if contact_nr < 5 else 'lateral'  # improve view selection

    vertno_max, time_max = stc.get_peak(hemi=hemi)
    stc_max = np.max(stc.data)

    if plot:
        brain_inf = stc.plot(surface='inflated', hemi=hemi, subjects_dir=subjects_dir, subject=fs_subj,
                             clim=dict(kind='value', lims=[0, stc_max*0.75, stc_max]),
                             initial_time=time_max, time_unit='s', alpha=1, background='w', foreground='k')

        brain_inf.add_foci(vertno_max, coords_as_verts=True, hemi=hemi, color='blue',
                           scale_factor=0.8)

        brain_inf.add_foci(stim_coords['surf_ori'], map_surface='pial', hemi=hemi, color='green', scale_factor=0.8)

        brain_inf.show_view(view)  # mlab.view(130, 90)
        fig_fname = op.join(study_path, 'source_stim', subj, 'figures', 'distributed', '%s_inf_foci.eps' % cond)
        #brain_inf.save_image(fig_fname)
        mlab.show()
        #mlab.clf()

    max_mni = mne.vertex_to_mni(vertno_max, hemis=0, subject=subj, subjects_dir=subjects_dir)  # mni coords of vertex (ojo vertex resolution ico-5)
    stim_mni = stim_coords['mni']  # mni coords of stimulation
    dist_mni = euclidean(max_mni, stim_mni)

    print(dist_mni)
    return dist_mni

    # brain_pial = stc.plot(surface='pial', hemi=hemi, subjects_dir=subjects_dir,
    #                      clim=dict(kind='value', lims=[0, stc_max*0.75, stc_max]),
    #                      initial_time=time_max, time_unit='s', alpha=1, background='w', foreground='k')
    #
    # brain_pial.add_foci(vertno_max, coords_as_verts=True, hemi=hemi, color='blue',
    #                    scale_factor=1)
    #
    # brain_pial.add_foci(stim_coords['surf'], hemi='lh', color='green', scale_factor=1)
    #
    # brain_pial.show_view('medial')  # mlab.view(130, 90)
    # fig_fname = op.join(study_path, 'source_stim', subj, 'figures', 'distributed', '%s_pial_foci.eps' % cond)
    # brain_pial.save_image(fig_fname)
    # mlab.show()

def vol_source_loc(evoked, cov, fs_subj, study_path, plot=True):
    cond = evoked.info['description']

    bem_fname = op.join(subjects_dir, fs_subj, 'bem', '%s-bem-sol.fif' % fs_subj)
    trans_fname = op.join(study_path, 'source_stim', subj, 'source_files', img_type, '%s_static-trans.fif' % fs_subj)

    mri_file = op.join(subjects_dir, subj, 'mri', 'T1.mgz')
    v_source = mne.setup_volume_source_space(subject=fs_subj, mri=mri_file, bem=bem_fname, subjects_dir=subjects_dir,
                                             pos=2.)

    fwd = mne.make_forward_solution(evoked.info, trans_fname, v_source, bem_fname,
                                mindist=5.0,  # ignore sources<=5mm from innerskull
                                meg=False, eeg=True,
                                n_jobs=2)

    mne.write_forward_solution(op.join(study_path, 'source_stim', subj, 'source_files', '%s_vol_source_space_2mm'),
                               fwd)

    inv = mne.minimum_norm.make_inverse_operator(evoked.info, fwd, cov, loose=1, depth=1)
    method = "sLORETA"
    snr = 30.
    lambda2 = 1. / snr ** 2
    stc = mne.minimum_norm.apply_inverse(evoked, inv, lambda2, method=method, pick_ori=None)

    stc.crop(-0.005, 0.005)

    from nilearn.plotting import plot_stat_map
    from nilearn.image import index_img
    import nilearn as ni

    nii_fname = op.join(study_path, 'source_stim', subj, 'source_files', img_type, 'stc', '%s_%s_stc.nii' % (subj, cond))
    img = mne.save_stc_as_volume(nii_fname, stc.copy().crop(-0.003, 0.003), fwd['src'], dest='mri')

    t0_img = index_img(img, 3)
    act_coord = ni.plotting.find_xyz_cut_coords(t0_img)
    #act_coord = [57, 122, 87]
    thr = np.percentile(t0_img.get_data(), 99.9)
    st_map = plot_stat_map(t0_img, mri_file, threshold=thr, display_mode='ortho', cmap=plt.cm.plasma)
    st_map.add_markers(np.reshape(act_coord, (-1, 3)), marker_color='g', marker_size=50)
    fname_fig = op.join(study_path, 'source_stim', subj, 'figures', 'volume', '%s_%s_st_map.pdf' % (subj, cond))
    st_map.savefig(fname_fig)

    dist_vol = euclidean()

def dipole_source_loc(evoked, cov,  fs_subj, study_path, plot=False):
    evo_crop = evoked.copy().crop(-0.005, 0.005)
    subj = fs_subj.strip('_an') if fs_subj.find('_an') > 0 else fs_subj
    img_type = 'anony' if fs_subj.find('_an') > 0 else 'orig'

    seeg_ch_info = pd.read_csv(op.join(study_path, 'physio_data', subj, 'chan_info', '%s_seeg_ch_info.csv' % subj))

    # ori_to_an_fname = op.join(study_path, 'freesurfer_subjects', subj, 'mri', 'ori_to_an_trans.lta')  # mri to mri
    # ori_to_an_trans = np.genfromtxt(ori_to_an_fname, skip_header=8, skip_footer=18)

    trans_fname = op.join(study_path, 'source_stim', subj, 'source_files', img_type, '%s_static-trans.fif' % fs_subj)
    bem_fname = op.join(subjects_dir, fs_subj, 'bem', '%s-bem-sol.fif' % fs_subj)

    cond = eeg_epo.info['description']
    stim_coords = find_stim_coords(cond, subj, study_path)

    # Fit a dipole
    dip = mne.fit_dipole(evo_crop, cov, bem_fname, trans_fname, min_dist=5, n_jobs=2)[0]

    from mne.beamformer import rap_music
    dip = rap_music(evo_crop, fwd, cov, n_dipoles=1, return_residual=True, verbose=True)[0][0]

    import mne.transforms as tra
    from scipy import linalg
    trans = mne.read_trans(trans_fname)

    surf_to_head = linalg.inv(trans['trans'])

    # stim_point = tra.apply_trans(surf_to_head, stim_coords['surf']/1e3)
    stim_point = stim_coords['surf_ori']  # get point for plot in mm
    #dip.pos[np.argmax(dip.gof)] = tra.apply_trans(surf_to_head, stim_coords['surf_ori']/1e3)  # check stim loc (apply affine in m)

    #stim_point = tra.apply_trans(ori_to_an_trans, stim_point)

    dip_surf = tra.apply_trans(trans['trans'], dip.pos[np.argmax(dip.gof)]) * 1e3  # transform from head to surface
    dist_surf = euclidean(dip_surf, stim_point)  # compute distance in surface space
    print(dist_surf)

    if plot:
        # Plot the result in 3D brain with the MRI image.
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        dip.plot_locations(trans_fname, fs_subj, subjects_dir, mode='orthoview', coord_frame='mri', ax=ax, show_all=True,
                           idx='gof')
        ax.scatter(stim_point[0], stim_point[1], stim_point[2])
        ax.plot([stim_point[0], -128], [stim_point[1], stim_point[1]], [stim_point[2], stim_point[2]], color='g')
        ax.plot([stim_point[0], stim_point[0]], [stim_point[1], -128], [stim_point[2], stim_point[2]], color='g')
        ax.plot([stim_point[0], stim_point[0]], [stim_point[1], stim_point[1]], [stim_point[2], -128], color='g')
        ax.text2D(0.05, 0.90, 'distance: %i mm \nstim coords = %0.1f %0.1f %0.1f' % (dist_surf, stim_point[0], stim_point[1], stim_point[2]),
                  transform=ax.transAxes)
        red_patch = mpatches.Patch(color='red')
        green_patch = mpatches.Patch(color='green')
        fig.legend(handles=[red_patch, green_patch], labels=['dipole', 'electrode'])

        fig.savefig(op.join(study_path, 'source_stim', subj, 'figures', 'dipole', '%s_dip_15mm.png' % cond))
        plt.close()

        from mne.viz import plot_dipole_locations, plot_dipole_amplitudes
        plot_dipole_amplitudes(dip)
        plot_dipole_locations(dip, trans, subj, subjects_dir=subjects_dir)

    return dist_surf

if __name__ == '__main__':
    subj = 'S5'
    img_type = 'orig'
    fs_subj = subj + '_an' if img_type == 'anony' else subj

    epo_path = op.join(study_path, 'source_stim', subj, 'epochs', 'fif')
    conds = glob.glob(epo_path + '/*-epo.fif')

    fwd_fname = op.join(study_path, 'source_stim', subj, 'source_files', img_type, '%s-fwd.fif' % fs_subj)

    if not op.isfile(fwd_fname):
        fwd = make_fwd_solution(conds[0], fs_subj, study_path)
    else:
        fwd = mne.read_forward_solution(fwd_fname)

    for cond_fname in conds:
        eeg_epo = mne.read_epochs(cond_fname)
        eeg_epo.interpolate_bads(reset_bads=True)
        # eeg_epo.filter(None, 40)
        cov = mne.compute_covariance(eeg_epo, method='shrunk', tmin=-0.5, tmax=-0.3)  # use method='auto' for final computation
        evoked = eeg_epo.average()

        evoked.plot(spatial_colors=True)
        mne.viz.plot_evoked_topo(evoked)
        evoked.plot_topomap(np.linspace(-0.005, 0.005, 11))

        dist_dis = source_loc(evoked, cov, fwd, subj, img_type, study_path, plot=False)
        dist_dip = dipole_source_loc(evoked, cov,  fs_subj, study_path, plot=True)

        cond = eeg_epo.info['description']
        stim_params = get_stim_params(cond)

        save_results(subj, study_path, stim_params, evoked, dist_dis, dist_dip, img_type)


import numpy as np
import tensorflow as tf
from tensorflow.python.keras.layers import Dense, Embedding, Lambda, Layer

from odin import backend as bk
from odin.networks.sequential_networks import SequentialNetwork

__all__ = [
    'get_conditional_embedding', 'RepeaterEmbedding', 'ConditionalEmbedding',
    'ConditionalProjection'
]

class Embedder:

  @property
  def embedding_shape(self):
    raise NotImplementedError

def get_conditional_embedding(method) -> Embedder:
  r""" Three support method for conditional embedding:

      - 'repeat': repeat the labels to match the input image
      - 'project': embed then project (using Dense layer)
      - 'embed': only embed to the given output shape
  """
  method = str(method).strip().lower()
  classes = dict(repeat=RepeaterEmbedding,
                 project=ConditionalProjection,
                 embed=ConditionalEmbedding)
  for name, cls in classes.items():
    if method == name or method in name:
      return cls
  raise KeyError(
      "Cannot find conditional embedding method for key: %s, all support methods are: %s"
      % method, str(classes))

class RepeaterEmbedding(Layer, Embedder):
  r""" Expand and repeat the inputs so that it is concatenate-able to the
  output_shape """

  def __init__(self, num_classes, output_shape, name='RepeaterEmbedding'):
    super().__init__(name=name)
    self._shape = [int(i) for i in tf.nest.flatten(output_shape)]
    self._ndim = len(self._shape) + 1  # add batch_dim
    self.num_classes = int(num_classes)

  @property
  def embedding_shape(self):
    return tuple([None] + self._shape[:-1] + [self.num_classes])

  def call(self, inputs, **kwargs):
    shape = inputs.shape
    ndim = len(shape)
    if ndim > self._ndim:
      raise RuntimeError("Cannot broadcast inputs shape=%s to output shape=%s" %
                         (shape[1:], self._shape))
    elif ndim < self._ndim:
      n = abs(self._ndim - ndim)
      # first expand
      for _ in range(n):
        inputs = tf.expand_dims(inputs, axis=1)
      # now repeat
      for i, s in enumerate(inputs.shape[1:]):
        if s == 1 and self._shape[i] != 1:
          inputs = tf.repeat(inputs, self._shape[i], axis=i + 1)
    return inputs

class ConditionalEmbedding(Embedding, Embedder):
  r""" Turns positive integers (indexes) (or one-hot encoded vector)
  into dense vectors of fixed size, then reshape the vector to desire
  output shape.
  """

  def __init__(self,
               num_classes,
               output_shape,
               embeddings_initializer='uniform',
               embeddings_regularizer=None,
               activity_regularizer=None,
               embeddings_constraint=None,
               **kwargs):
    output_shape = [int(i) for i in tf.nest.flatten(output_shape)]
    super().__init__(input_dim=int(num_classes),
                     output_dim=int(np.prod(output_shape)),
                     embeddings_initializer=embeddings_initializer,
                     embeddings_regularizer=embeddings_regularizer,
                     activity_regularizer=activity_regularizer,
                     embeddings_constraint=embeddings_constraint,
                     mask_zero=False,
                     input_length=1,
                     **kwargs)
    self._output_shape = output_shape

  @property
  def embedding_shape(self):
    return tuple([None] + self._output_shape)

  def call(self, inputs, **kwargs):
    if inputs.shape[-1] > 1:
      inputs = tf.expand_dims(tf.argmax(inputs, axis=-1, output_type=tf.int32),
                              axis=-1)
    outputs = super().call(inputs)
    outputs = tf.squeeze(outputs, axis=1)
    outputs = tf.reshape(outputs, [-1] + self._output_shape)
    return outputs

class ConditionalProjection(SequentialNetwork, Embedder):
  r""" A combination of both embedding and projection to transform the labels
  into the image space for concatenation.

  This approach is used in ConditionalGAN
  """

  def __init__(self,
               num_classes,
               output_shape,
               embedding_dim=50,
               activation='linear',
               use_bias=True,
               embeddings_initializer='uniform',
               embeddings_regularizer=None,
               activity_regularizer=None,
               embeddings_constraint=None,
               kernel_initializer='glorot_uniform',
               bias_initializer='zeros',
               kernel_regularizer=None,
               bias_regularizer=None,
               kernel_constraint=None,
               bias_constraint=None,
               name='ConditionalProjection'):
    output_shape = [int(i) for i in tf.nest.flatten(output_shape)]
    self._output_shape = output_shape
    layers = [
        Embedding(input_dim=int(num_classes),
                  output_dim=int(embedding_dim),
                  embeddings_initializer=embeddings_initializer,
                  embeddings_regularizer=embeddings_regularizer,
                  activity_regularizer=activity_regularizer,
                  embeddings_constraint=embeddings_constraint,
                  mask_zero=False,
                  input_length=1),
        Lambda(lambda x: tf.squeeze(x, axis=1)),
        Dense(int(np.prod(output_shape)),
              activation=activation,
              use_bias=use_bias,
              kernel_initializer=kernel_initializer,
              bias_initializer=bias_initializer,
              kernel_regularizer=kernel_regularizer,
              bias_regularizer=bias_regularizer,
              activity_regularizer=activity_regularizer,
              kernel_constraint=kernel_constraint,
              bias_constraint=bias_constraint)
    ]
    super().__init__(layers=layers, name=name)

  @property
  def embedding_shape(self):
    return tuple([None] + self._output_shape)

  def call(self, inputs, **kwargs):
    if inputs.shape[-1] > 1:
      inputs = tf.expand_dims(tf.argmax(inputs, axis=-1, output_type=tf.int32),
                              axis=-1)
    outputs = super().call(inputs, **kwargs)
    outputs = tf.reshape(outputs, [-1] + self._output_shape)
    return outputs


import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

from keras.layers import Dense, Input
from keras.layers import Convolution2D, Flatten, Lambda
from keras.layers import Reshape, Conv2DTranspose
from keras.models import Model, load_model
from keras import backend as K
from keras.datasets import mnist
import os

os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = "0"

(x_train, y_train_), (x_test, y_test_) = mnist.load_data('mnist.npz')
image_size = x_train.shape[1]
x_train = np.reshape(x_train, [-1, image_size, image_size, 1])
x_test = np.reshape(x_test, [-1, image_size, image_size, 1])
x_train = x_train.astype('float32')/255.
x_test = x_test.astype('float32')/255.

input_shape = (image_size, image_size, 1)
batch_size = 256
kernel_size = 3
filters = 16
latent_dim = 2
epochs = 30

x_in = Input(shape=input_shape)
x = x_in
for i in range(2):
    filters *=2
    x = Convolution2D(
        filters=filters,
        kernel_size=kernel_size,
        activation='relu',
        strides=2,
        padding='same',
    )(x)

shape = K.int_shape(x)

x = Flatten()(x)  # 把多维的数据变为一维
x = Dense(16, activation='relu')(x)
z_mean = Dense(latent_dim)(x)
z_log_var = Dense(latent_dim)(x)

def sampling(args):
    z_mean, z_log_var = args
    epsilon = K.random_normal(shape=K.shape(z_mean))
    return z_mean + epsilon * K.exp(z_log_var/2)

z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])

##### 定义模型解码部分（生成器） #####

latent_inputs = Input(shape=(latent_dim,))
x = Dense(shape[1]*shape[2]*shape[3], activation='relu')(latent_inputs)
x = Reshape((shape[1], shape[2], shape[3]))(x)

for i in range(2):
    x = Conv2DTranspose(
        filters=filters,
        kernel_size=kernel_size,
        activation='relu',
        strides=2,
        padding='same'
    )(x)
    filters //=2

outputs = Conv2DTranspose(
    filters=1,
    kernel_size=kernel_size,
    activation='sigmoid',
    padding='same'
)(x)

encoder = Model(x_in, z_mean)
decoder = Model(latent_inputs, outputs)
x_out = decoder(z)
vae = Model(x_in, x_out)

xent_loss = K.sum(K.binary_crossentropy(x_in, x_out), axis=[1, 2, 3])
kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
vae_loss = K.mean(xent_loss + kl_loss)

vae.add_loss(vae_loss)

vae.compile(optimizer='rmsprop')
vae.summary()

vae.fit(
    x_train,
    shuffle=True,
    epochs=epochs,
    batch_size=batch_size,
    validation_data=(x_test, None)
)
vae.save('./model/model_vae_cnn')
encoder.save('./model/model_vae_cnn_encoder')
decoder.save('./model/model_vae_cnn_decoder')

encoder_model = load_model('./model/model_vae_cnn_encoder', custom_objects={'sampling':sampling})
x_test_encoded = encoder_model.predict(x_test, batch_size=batch_size)
plt.figure(figsize=(6,6))
plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test_)
plt.colorbar()
plt.show()

decoder_model = load_model('./model/model_vae_generator', custom_objects={'sampling':sampling})
n = 15
digit_size = 28
figure = np.zeros((digit_size * n, digit_size * n))

grid_x = norm.ppf(np.linspace(0.05, 0.95, n))
grid_y = norm.ppf(np.linspace(0.05, 0.95, n))

for i, yi in enumerate(grid_x):
    for j, xi in enumerate(grid_y):
        z_sample = np.array([[xi, yi]])
        x_decoded = decoder_model.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))
plt.imshow(figure, cmap='Greys_r')
plt.show()