import torch.nn as nn
import torch
from myconstants import *

# RNN model
class RNNModel(nn.Module):
    def __init__(self, input_dim = 11, hidden_dim = 64, layer_dim=3, output_dim=1, dropout_prob=0.2):
        """The __init__ method that initiates an RNN instance.

        Args:
            input_dim (int): The number of nodes in the input layer
            hidden_dim (int): The number of nodes in each layer
            layer_dim (int): The number of layers in the network
            output_dim (int): The number of nodes in the output layer
            dropout_prob (float): The probability of nodes being dropped out
        """

        super(RNNModel, self).__init__()

        # Defining the number of layers and the nodes in each layer
        self.hidden_dim = hidden_dim
        self.layer_dim = layer_dim

        # RNN layers
        self.rnn = nn.RNN(
            input_dim, hidden_dim, layer_dim, batch_first=True, dropout=dropout_prob
        )
        # Fully connected layer
        self.fc = nn.Linear(hidden_dim, output_dim)

    def forward(self, x):
        """The forward method takes input tensor x and does forward propagation

        Args:
            x (torch.Tensor): The input tensor of the shape (batch size, sequence length, input_dim)

        Returns:
            torch.Tensor: The output tensor of the shape (batch size, output_dim)

        """
        # Initializing hidden state for first input with zeros
        h0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim, device=DEVICE).requires_grad_()

        # Forward propagation by passing in the input and hidden state into the model
        out, h0 = self.rnn(x, h0.detach())

        # Reshaping the outputs in the shape of (batch_size, seq_length, hidden_size)
        # so that it can fit into the fully connected layer
        out = out[:, -1, :]

        # Convert the final state to our desired output shape (batch_size, output_dim)
        out = self.fc(out)
        return out

# LSTM Model
class LSTMModel(nn.Module):
    def __init__(self, input_dim = 11, hidden_dim = 64, layer_dim = 3, output_dim = 1, dropout_prob = 0.2):

        super(LSTMModel, self).__init__()

        # Defining the number of layers and the nodes in each layer
        self.hidden_dim = hidden_dim
        self.layer_dim = layer_dim

        # LSTM layers
        self.lstm = nn.LSTM(input_dim, hidden_dim, layer_dim, batch_first=True, dropout=dropout_prob)

        # Fully connected layer
        self.fc = nn.Linear(hidden_dim, output_dim)

    def forward(self, x):
        # Initializing hidden state for first input with zeros
        h0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim,device=DEVICE).requires_grad_()

        # Initializing cell state for first input with zeros
        c0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim,device=DEVICE).requires_grad_()

        # We need to detach as we are doing truncated backpropagation through time (BPTT)
        # If we don't, we'll backprop all the way to the start even after going through another batch
        # Forward propagation by passing in the input, hidden state, and cell state into the model
        out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))

        # Reshaping the outputs in the shape of (batch_size, seq_length, hidden_size)
        # so that it can fit into the fully connected layer
        out = out[:, -1, :]

        # Convert the final state to our desired output shape (batch_size, output_dim)
        out = self.fc(out)
        return out

def get_model(model, model_params):
    models = {
        "rnn": RNNModel,
        "lstm": LSTMModel,
    }
    return models.get(model.lower())(**model_params)


import sys

def writeFile(data, filename,):
    with open(filename, 'w',  encoding='UTF8') as f:
        for element in data:
            f.write(element + '\n')

if __name__ == "__main__":
    # Som default forventes at hvert arg er et decimalt tal.
    # Hvis sys.argv[1] == -h, så opfattes alle argumenter som hextal.
    if sys.argv[1] == '-h':
        baseidx = 2
        base = 16
        subscript = chr(int('2081', 16))+chr(int('2086', 16))
    else:
        baseidx = 1
        base = 10
        subscript = chr(int('2081', 16))+chr(int('2080', 16))

    codepoints = []

    for elm in sys.argv[baseidx:]:
        codepoints.append(elm + subscript + ': ' + chr(int(elm, base)))
    writeFile(codepoints, 'utfChars.txt')


import numpy as np
import keras.models
from keras.models import model_from_json
from scipy.misc import imread, imresize,imshow
import matplotlib.pyplot as plt

json_file = open('model.json','r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
#load woeights into new model
loaded_model.load_weights("model.h5")
print("Loaded Model from disk")

loaded_model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

#img_answer = (img_resized/255.0).astype('float32')
#
#plt.imshow(np.uint8(img_tinted))
#plt.show()

x = imread('output.png',mode='L')
x = np.invert(x)
x = imresize(x,(28,28))
plt.imshow(x)
plt.show()
x = x.reshape(1,28,28,1)

out = loaded_model.predict(x)
print(out)
print(np.argmax(out,axis=1))


#!/usr/bin/env python

"""The setup script."""

from setuptools import setup, find_packages

with open('README.md') as readme_file:
    readme = readme_file.read()

with open('HISTORY.md') as history_file:
    history = history_file.read()

requirements = [ ]

setup_requirements = [ ]

test_requirements = [ ]

setup(
    author="Container Solutions",
    author_email='cre@container-solutions.com',
    python_requires='>=3.6',
    classifiers=[
        'Development Status :: 2 - Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
    ],
    description="prom-metrics-check is command line tools which help checking "
                "metric between dashboards of grafana and prometheus metrics.",
    entry_points={
        'console_scripts': [
            'prom-metrics-check=prom_metrics_check.cli:main',
        ],
    },
    install_requires=requirements,
    license="MIT license",
    long_description=readme + '\n\n' + history,
    include_package_data=True,
    keywords='prom-metrics-check',
    name='prom-metrics-check',
    packages=find_packages(include=['prom_metrics_check', 'prom_metrics_check.*']),
    setup_requires=setup_requirements,
    test_suite='tests',
    tests_require=test_requirements,
    url='https://github.com/ContainerSolutions/prom-metrics-check',
    version='0.1.0',
    zip_safe=False,
)


from PyQt5.QtGui import QPainter
from random import random
from modes import aimode
from constants import Directions as d, Types as t, StartingPositions as s, Colors as c, Options as o, TYPE_TIMER
import expections
import objects

class PowerUp(aimode.AIMode):

    def __init__(self, multiplayer):
        super().__init__(1)
        self.counter = 0
        self.multiplayer = multiplayer

    def start(self):
        self.car_list = []
        self.c1 = self.new_car(s.START_X_2P_1, s.START_Y_2P_1, d.NORTH, c.BLUE, o.PLAYER_NAME, 30)
        if self.multiplayer:
            self.c2 = self.new_car(s.START_X_2P_2, s.START_Y_2P_2, d.SOUTH, c.PINK, 'Player2', 30)
        else:
            self.c2 = self.new_ai(s.START_X_2P_2, s.START_Y_2P_2, d.SOUTH, c.RED, 'AI', 30)
        self.counter = 0
        self.powerup_1 = 0
        self.powerup_2 = 0
        self.obj_arr = [None, None]

    def move(self, car):
        """ Es wird ein Auto (car) bewegt, dabei wird geprüft ob es ein PowerUp einsammelt
        oder mit der Mauer, sich selbst oder einem anderen Auto kollidiert
        """
        try:
            car.move()
            for i in range(len(self.obj_arr)):
                if self.obj_arr[i] != None and self.obj_arr[i].collect(car):
                    self.obj_arr[i].action(car)
                    self.obj_arr[i] = None
            for i in self.car_list:
                if i != car:
                    self.test_collision(car, i)
        except(expections.OutOfMapError, expections.SelfDestruction, expections.Collision):
            car.lost = True

    def update_object(self):
        """ Es werden falls nicht vorhanden passende PowerUps auf dem Spielfeld plaziert
        """
        # Es ist immer ein PowerUp des Typen PLUS_LENGTH auf dem Spielfeld und befindet sich immer in obj_arr[0]
        if self.obj_arr[0] == None: # Falls kein solches PowerUp vorhanden, wird sofort eins erzeugt
            self.obj_arr[0] = objects.Object(self.car_list, self.obj_arr, t.PLUS_LENGTH)
        if self.counter == 0:       # Jedes PowerUp, das in obj_arr[1] liegt, hat einen Timer, wenn dieser
            self.obj_arr[1] = None  # abgelaufen ist wird das PowerUp entfernt
        else:
          self.counter = self.counter - 1
        # In obj_arr[1] liegen die anderen PowerUps, jedoch muss nicht immer eins vorhanden sein
        if self.obj_arr[1] == None:
            if random() < 0.05: # mit einer Wahrscheinlichkeit von 5% wird ein neues PowerUp erzeugt, falls keines vorhanden ist
                r = random()
                if r < 0.35:                                        # 35%
                    type = t.MINUS_LENGTH
                    self.counter = TYPE_TIMER[t.MINUS_LENGTH]
                elif r < 0.70:                                      # 35%
                    type = t.DESTROY_TAIL
                    self.counter = TYPE_TIMER[t.DESTROY_TAIL]
                elif r < 0.82 and self.powerup_1 < 2:               # 12%, wird maximal zwei mal im Spiel gespawnt
                    type = t.SELFDMG_OF
                    self.counter = TYPE_TIMER[t.SELFDMG_OF]
                    self.powerup_1 = self.powerup_1 + 1
                elif r < 0.94 and r > 0.82 and self.powerup_2 < 2:   # 12%, wird maximal zwei mal im Spiel gespawnt
                    type = t.BORDERDMG_OF
                    self.counter = TYPE_TIMER[t.BORDERDMG_OF]
                    self.powerup_2 = self.powerup_2 + 1
                else:                                                # 6% (- 30%), erhöhte Wahrscheinlichkeit, falls SELFDMG_OF und/oder                                                    # BORDER
                    type = t.LOSE                                    # BORDERDMG_OF schon zwei mal gespawnt sind
                    self.counter = TYPE_TIMER[t.LOSE]
                self.obj_arr[1] = objects.Object(self.car_list, self.obj_arr, type)

    def timerEvent(self, e):
        self.update_object()
        self.cars_move(self.car_list)
        self.update_cars(self.car_list)
        self.update()

    def paintEvent(self, e):
        painter = QPainter(self)
        for i in self.obj_arr:
            if i != None:
                self.draw_object(painter, i.x, i.y, i.color)
        self.draw_cars(painter, self.car_list)
        painter.end()


import math

def binarySearch(searchArray : list , searchTarget : float):
    startingPointer = 0
    endingPointer = len(searchArray) -1

    while(startingPointer <= endingPointer):
        midpoint = math.floor((startingPointer+endingPointer)/2)
        if searchArray[midpoint] == searchTarget:
            return midpoint
        elif searchTarget > searchArray[midpoint]:
            startingPointer = midpoint +1
        else:
            endingPointer = midpoint -1
    return 'Not Found'

print(binarySearch([1,2,3,4,5,6],5))


import cv2

def rescaleFrame(frame, scale=.75):
    # work for images, videos and live videos
    width = int(frame.shape[1] * scale)
    height = int(frame.shape[0] * scale)
    dimensions = (width, height)

    return cv2.resize(frame, dimensions, interpolation=cv2.INTER_AREA)

img = cv2.imread('Resources/Photos/cat.jpg')
# cv2.imshow('Photos', img)
img = rescaleFrame(img, 0.75)

# Gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# cv2.imshow('Gray', gray)

# Blur
blur = cv2.GaussianBlur(img, (3, 3), cv2.BORDER_DEFAULT)
# cv2.imshow('Blur', blur)

# Edge cascade
edge = cv2.Canny(blur, 125, 175)
# cv2.imshow('Edge cascade', edge)

# Dilating the image
dilated = cv2.dilate(edge, (7, 7), iterations=3)
# cv2.imshow('Dilated', dilated)

# Eroding
eroded = cv2.erode(dilated, (7, 7), iterations=3)
# cv2.imshow('Erode', eroded)

# Resize
resized = cv2.resize(img, (1200, 600), interpolation=cv2.INTER_LINEAR)
# cv2.imshow('Resized', resized)

# Cropping
cropped = img[50:200, 100:200]
cv2.imshow('Cropped', cropped)

cv2.waitKey(0)


import os
import re

from autotest.client import test, utils
import postprocessing

class iozone(test.test):

    """
    This autotest module runs the IOzone filesystem benchmark. The benchmark
    generates and measures a variety of file operations. Iozone has been ported
    to many machines and runs under many operating systems.

    Iozone is useful for performing a broad filesystem analysis of a vendor's
    computer platform. The benchmark tests file I/O performance for the
    following operations:

    Read, write, re-read, re-write, read backwards, read strided, fread, fwrite,
    random read, pread ,mmap, aio_read, aio_write

    @author: Ying Tao (yingtao@cn.ibm.com)
    @see: http://www.iozone.org
    """
    version = 6

    def initialize(self):
        self.job.require_gcc()

    def setup(self, tarball='iozone3_414.tar'):
        """
        Builds the given version of IOzone from a tarball.
        :param tarball: Tarball with IOzone
        @see: http://www.iozone.org/src/current/iozone3_414.tar
        """
        tarball = utils.unmap_url(self.bindir, tarball, self.tmpdir)
        utils.extract_tarball_to_dir(tarball, self.srcdir)
        os.chdir(os.path.join(self.srcdir, 'src/current'))
        utils.system('patch -p3 < %s' %
                     os.path.join(self.bindir, 'makefile.patch'))

        arch = utils.get_current_kernel_arch()
        if (arch == 'ppc'):
            utils.make('linux-powerpc')
        elif (arch == 'ppc64'):
            utils.make('linux-powerpc64')
        elif (arch == 'x86_64'):
            utils.make('linux-AMD64')
        else:
            utils.make('linux')

    def run_once(self, dir=None, args=None):
        """
        Runs IOzone with appropriate parameters, record raw results in a per
        iteration raw output file as well as in the results attribute

        :param dir: IOzone file generation dir.
        :param args: Arguments to the iozone program.
        """
        if not dir:
            dir = self.tmpdir
        os.chdir(dir)
        if not args:
            args = '-a'

        cmd = os.path.join(self.srcdir, 'src', 'current', 'iozone')
        self.results = utils.system_output('%s %s' % (cmd, args))
        self.auto_mode = ("-a" in args)

        self.results_path = os.path.join(self.resultsdir,
                                         'raw_output_%s' % self.iteration)
        self.analysisdir = os.path.join(self.resultsdir,
                                        'analysis_%s' % self.iteration)

        utils.open_write_close(self.results_path, self.results)

    def __get_section_name(self, desc):
        return desc.strip().replace(' ', '_')

    def generate_keyval(self):
        keylist = {}

        if self.auto_mode:
            labels = ('write', 'rewrite', 'read', 'reread', 'randread',
                      'randwrite', 'bkwdread', 'recordrewrite',
                      'strideread', 'fwrite', 'frewrite', 'fread', 'freread')
            for line in self.results.splitlines():
                fields = line.split()
                if len(fields) != 15:
                    continue
                try:
                    fields = tuple([int(i) for i in fields])
                except ValueError:
                    continue
                for l, v in zip(labels, fields[2:]):
                    key_name = "%d-%d-%s" % (fields[0], fields[1], l)
                    keylist[key_name] = v
        else:
            child_regexp = re.compile('Children see throughput for[\s]+'
                                      '([\d]+)\s+([-\w]+[-\w\s]*)\=[\s]+([\d\.]*) KB/sec')
            parent_regexp = re.compile('Parent sees throughput for[\s]+'
                                       '([\d]+)\s+([-\w]+[-\w\s]*)\=[\s]+([\d\.]*) KB/sec')

            KBsec_regexp = re.compile('\=[\s]+([\d\.]*) KB/sec')
            KBval_regexp = re.compile('\=[\s]+([\d\.]*) KB')

            section = None
            w_count = 0

            for line in self.results.splitlines():
                line = line.strip()

                # Check for the beginning of a new result section
                match = child_regexp.search(line)
                if match:
                    # Extract the section name and the worker count
                    w_count = int(match.group(1))
                    section = self.__get_section_name(match.group(2))

                    # Output the appropriate keyval pair
                    key_name = '%s-%d-kids' % (section, w_count)
                    keylist[key_name] = match.group(3)
                    continue

                # Check for any other interesting lines
                if '=' in line:
                    # Is it something we recognize? First check for parent.
                    match = parent_regexp.search(line)
                    if match:
                        # The section name and the worker count better match
                        p_count = int(match.group(1))
                        p_secnt = self.__get_section_name(match.group(2))
                        if p_secnt != section or p_count != w_count:
                            continue

                        # Set the base name for the keyval
                        basekey = 'parent'
                    else:
                        # Check for the various 'throughput' values
                        if line[3:26] == ' throughput per thread ':
                            basekey = line[0:3]
                            match_x = KBsec_regexp
                        else:
                            # The only other thing we expect is 'Min xfer'
                            if not line.startswith('Min xfer '):
                                continue
                            basekey = 'MinXfer'
                            match_x = KBval_regexp

                        match = match_x.search(line)
                        if match:
                            result = match.group(1)
                            key_name = "%s-%d-%s" % (section, w_count, basekey)
                            keylist[key_name] = result

        self.write_perf_keyval(keylist)

    def postprocess_iteration(self):
        self.generate_keyval()
        if self.auto_mode:
            a = postprocessing.IOzoneAnalyzer(list_files=[self.results_path],
                                              output_dir=self.analysisdir)
            a.analyze()
            p = postprocessing.IOzonePlotter(results_file=self.results_path,
                                             output_dir=self.analysisdir)
            p.plot_all()


# -*- coding:gb18030 -*-

import BigWorld
import csconst
import csstatus
from bwdebug import *
from SpellBase import *
from Buff_Normal import Buff_Normal
from Function import newUID

class Buff_299034( Buff_Normal ):
	"""
	参数为正数表示“增加”，负数则表示“减小”
	Buff的功能：
	移动速度增加/减小
	物理法术防御力增加/减小
	物理法术攻击力增加/减小
	"""
	def __init__( self ):
		"""
		"""
		Buff_Normal.__init__( self )
		self._p1 = 0		# 移动速度增加/减小
		self._p2 = 0		# 物理法术防御力增加/减小
		self._p3 = 0		# 物理法术攻击力增加/减小

	def init( self, dict ):
		"""
		读取技能配置
		@param dict: 配置数据
		@type  dict: python dict
		"""
		Buff_Normal.init( self, dict )

		self._p1 = int( dict[ "Param1" ] if len( dict[ "Param1" ] ) > 0 else 0 )  * 100		# 移动速度改变百分比
		self._p2 = int( dict[ "Param2" ] if len( dict[ "Param2" ] ) > 0 else 0 )  * 100		# 物理法术防御力增加/减小百分比
		self._p3 = int( dict[ "Param3" ] if len( dict[ "Param3" ] ) > 0 else 0 )  * 100		# 物理法术攻击力增加/减小百分比

	def doBegin( self, receiver, buffData ):
		"""
		Virtual method; call only by real entity.
		效果开始的处理。

		@param receiver: 效果要影响的实体
		@type  receiver: BigWorld.Entity
		@param buffData: BUFF
		@type  buffData: BUFF
		@return: None
		"""
		Buff_Normal.doBegin( self, receiver, buffData )
		receiver.move_speed_percent += self._p1								# 移动速度
		receiver.calcMoveSpeed()

		receiver.armor_percent += self._p2									# 物理防御
		receiver.calcArmor()
		receiver.magic_armor_percent += self._p2							# 法术防御
		receiver.calcMagicArmor()

		receiver.damage_min_percent += self._p3								# 物理攻击力
		receiver.calcDamageMin()
		receiver.damage_max_percent += self._p3
		receiver.calcDamageMax()
		receiver.magic_damage_percent += self._p3							# 法术攻击力
		receiver.calcMagicDamage()

	def doReload( self, receiver, buffData ):
		"""
		Virtual method; call only by real entity.
		效果重新加载的处理。

		@param receiver: 效果要影响的实体
		@type  receiver: BigWorld.Entity
		@param buffData: BUFF
		@type  buffData: BUFF
		@return: None
		"""
		Buff_Normal.doReload( self, receiver, buffData )
		receiver.move_speed_percent -= self._p1
		receiver.armor_percent += self._p2									# 物理防御
		receiver.magic_armor_percent += self._p2							# 法术防御
		receiver.damage_min_percent += self._p3
		receiver.damage_max_percent += self._p3
		receiver.magic_damage_percent += self._p3

	def doEnd( self, receiver, buffData ):
		"""
		"""
		Buff_Normal.doEnd( self, receiver, buffData )
		receiver.move_speed_percent -= self._p1
		receiver.calcMoveSpeed()

		receiver.armor_percent -= self._p2									# 物理防御
		receiver.calcArmor()
		receiver.magic_armor_percent -= self._p2							# 法术防御
		receiver.calcMagicArmor()

		receiver.damage_min_percent -= self._p3
		receiver.calcDamageMin()
		receiver.damage_max_percent -= self._p3
		receiver.calcDamageMax()
		receiver.magic_damage_percent -= self._p3
		receiver.calcMagicDamage()


import json

from gootool.youtube import YoutubeMe

if __name__ == '__main__':
    youtube = YoutubeMe(
        credential='./.credentials/youtube-credential.json',
        client_secret='./client_secret.json',
        )
    results = youtube.get_playlists()
    with open('youtube-lists.json', 'w', encoding='utf-8') as f:
        json.dump(results, f, ensure_ascii=False, indent=2)


'''
db 연결 설정 코드입니다
'''
from flask_restful import reqparse, Resource, Api
from flask import request, Flask
from flaskext.mysql import MySQL
import config

app = Flask(__name__)
api = Api(app)

# MySQL 연결 세팅
app.config['MYSQL_DATABASE_USER'] = config._DB_CONF['user']
app.config['MYSQL_DATABASE_PASSWORD'] = config._DB_CONF['passwd']
app.config['MYSQL_DATABASE_DB'] = config._DB_CONF['db']
app.config['MYSQL_DATABASE_HOST'] = config._DB_CONF['host']
app.config['MYSQL_DATABASE_PORT'] = config._DB_CONF['port']

# MySQL 연결
mysql = MySQL()
mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor()
_query = "select * from prescription"
cursor.execute(_query)
_data = cursor.fetchall()
result= []
for data in _data:
    _insurance = data[1]
    _nursesign = data[2]
    _grantnum = data[3]
    _patientname = data[4]
    _patientreginum = data[5]
    _insname = data[6]
    _insphonenum = data[7]
    _insfaxnum = data[8]
    _insemail = data[9]
    _diseasecode1 = data[10]
    _diseasecode2 = data[11]
    _doctorname = data[12]
    _doctortype = data[13]
    _doctornum = data[14]
    _mediname = data[15]
    _medidose = data[16]
    _medidailydose = data[17]
    _meditotalday = data[18]
    _mediusage = data[19]
    _mediinside = data[20]
    _usepreiod = data[21]
    _dispensename = data[22]
    _pharmacistname = data[23]
    _pharmacistseal = data[24]
    _preparationamount = data[25]
    _preparationyear = data[26]
    _changeprescription = data[27]
    tempjson= { "insurance":_insurance ,"nursesign": _nursesign,"grantnum": _grantnum , "patientname":_patientname ,"patientreginum" :_patientreginum ,"insname" : _insname ,"insphonenum": _insphonenum ,"insfaxnum": _insfaxnum , "insemail": _insemail ,"diseasecode1": _diseasecode1 ,"diseasecode2": _diseasecode2,"doctorname": _doctorname ,"doctortype": _doctortype ,"doctornum": _doctornum ,"mediname": _mediname ,"medidose": _medidose ,"medidailydose":_medidailydose ,"meditotalday" :_meditotalday , "mediusage":_mediusage ,"mediinside": _mediinside ,"usepreiod": _usepreiod ,"dispensename": _dispensename ,"pharmacistname": _pharmacistname , "pharmacistseal": _pharmacistseal ,"preparationamount": _preparationamount ,"preparationyear": _preparationyear , "changeprescription": _changeprescription }
    result.append(tempjson)
# resultjson = json.dumps(result)


import torch
import numpy as np
from sklearn.model_selection import train_test_split
import cv2
import os
from torch.utils.data import DataLoader, TensorDataset
from net import MyCNN
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt

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

data = []
labels = []

folder_cats = "dataset/training_set/cats"
folder4_dogs = "dataset/training_set/dogs"

def add_images(folder, label: int):
    for i in os.listdir(folder):

        if i.endswith(".jpg") or i.endswith(".png") or i.endswith("jpeg"):
            image = cv2.imread(os.path.join(folder, i))
            image = cv2.resize(image, (64, 64))
            data.append(image)
            labels.append(label)

add_images(folder_cats, 0)
add_images(folder4_dogs, 1)

# print(len(data))

data = np.array(data)
labels = np.array(labels)
data = data.transpose(0, 3, 1, 2)

data = torch.from_numpy(data / 255).float()
labels = torch.from_numpy(labels)

x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.1)
train_dataset = TensorDataset(x_train, y_train)
test_dataset = TensorDataset(x_test, y_test)

train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)

network = MyCNN()
network.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(network.parameters(), lr=0.001)

train_losses = []
validation_losses = []
train_accuracies = []
validation_accuracies = []
highest_accuracy = 0

def train():
    losses = []
    correct = 0
    network.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        data = data.to(device)
        target = target.to(device)
        optimizer.zero_grad()
        output = network(data)
        loss = criterion(output, target.long())
        losses.append(loss.item())
        loss.backward()
        optimizer.step()

        predictions = []

        for i in output:
            label = i.cpu().detach().numpy().argmax()
            predictions.append(label)

        for i in range(0, len(predictions)):
            if target[i] == predictions[i]:
                correct += 1
    train_accuracies.append((correct / len(train_loader.dataset)) * 100)
    train_losses.append(sum(losses) / len(losses))

    print(f"Training loss: {sum(losses) / len(losses)}")
    print(f"Training accuracy: {(correct / len(train_loader.dataset)) * 100}")

def test(epoch):
    losses = []
    correct = 0
    global highest_accuracy
    network.eval()

    with torch.no_grad():

        for batch_idx, (data, target) in enumerate(test_loader):
            data = data.to(device)
            target = target.to(device)
            output = network(data)
            loss = criterion(output, target.long())
            losses.append(loss.item())

            predictions = []
            for i in output:
                label = i.cpu().detach().numpy().argmax()
                predictions.append(label)

            for i in range(0, len(predictions)):
                if target[i] == predictions[i]:
                    correct += 1
        validation_accuracies.append((correct / len(test_loader.dataset)) * 100)
        validation_losses.append(sum(losses) / len(losses))
        print(f"Validation loss: {sum(losses) / len(losses)}")
        print(f"Validation accuracy: {(correct / len(test_loader.dataset)) * 100}")

        if (correct / len(test_loader.dataset)) * 100 > highest_accuracy:
            highest_accuracy = (correct / len(test_loader.dataset)) * 100
            torch.save(network, "models/" + str(epoch + 1) + '-' + str((correct / len(test_loader.dataset)) * 100))

epochs = 20
for epoch in range(epochs):
    print(f"Training epoch: {epoch + 1}")
    train()
    test(epoch)

fig1 = plt.figure()
plt.plot(range(1, epochs + 1), train_losses, color="blue")
plt.plot(range(1, epochs + 1), validation_losses, color="red")

plt.legend(["Training Loss", "Validation Loss"], loc="upper right")
plt.xlabel("Epoch")
plt.ylabel("Cross Entropy Loss")

fig2 = plt.figure()
plt.plot(range(1, epochs + 1), train_accuracies, color="blue")
plt.plot(range(1, epochs + 1), validation_accuracies, color="red")

plt.legend(["Training Accuracy", "Validation Accuracy"], loc="upper right")
plt.xlabel("Epoch")
plt.ylabel("Accuracy %")
plt.show()


from django.urls import path, include
from django.conf import settings

urlpatterns = [
    path('', include('apps.core.urls')),
    path('budget/', include('apps.budget.urls')),
    path('mileage/', include('apps.mileage.urls')),
    path('episodetracker/', include('apps.episodetracker.urls')),
]

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        path('__debug__/', include(debug_toolbar.urls)),
    ] + urlpatterns


from typing import Optional

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def reorderList(self, head: Optional[ListNode]) -> None:
        slow = head
        fast = head
        # find middle
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        # reverse second list
        second = slow.next
        prev = slow.next = None

        while second:
            temp = second.next
            second.next = prev
            prev = second
            second = temp

        # merge two lists
        first = head
        second = prev
        while second:
            tmp1, tmp2 = first.next, second.next
            first.next, second.next = second, tmp1
            first, second = tmp1, tmp2


import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from collections import defaultdict

gg_data = pd.read_csv('adata_24dec.csv')

gg_data.head()

gg_data.nunique()

gg_data.account_status.value_counts()

gg_data.loc[gg_data['account_status'] == 'active'];

mydict = {}
for index, row in gg_data.iterrows():
    try:
        val = mydict[row['friend_id']]
        val.append(row['user_id'])
        mydict.update({row['friend_id']:val})
    except KeyError:
        val = [row['user_id']]
        mydict.update({row['friend_id']:val})

hist_dict = {}
for key, value in mydict.iteritems():
    try:
        new_key = len(mydict[key])
        count   = hist_dict[new_key]
        count+=1
        hist_dict.update({new_key:count})
    except KeyError:
        new_key = len(mydict[key])
        hist_dict.update({new_key:1})

len(hist_dict)

hist_dict
del hist_dict[1]
del hist_dict[2]

plt.bar(hist_dict.keys(), hist_dict.values(), 0.5, color='g', align='center')
plt.ylabel('Number of users')
plt.xlabel('Number of Active Contacts')
plt.show()


from pymongo import MongoClient

client = MongoClient('localhost', 27017)
mongo_base = client.instaparser
collection = mongo_base['instagram']

for i in collection.find({"user_id": "2280876151"}):
    print(i)

print('subscribe')
for i in collection.find({"subscribe_user_id": "2280876151"}):
    print(i)


import collections
import logging
import os
import re
import string  # pylint: disable=deprecated-module

from .xmlapi import XMLAPI
from . import util

# pylint: disable=protected-access
# This whole file is calling around into non-public functions that we
# don't want regular API users to touch

_trackprops = bool("VIRTINST_TEST_SUITE" in os.environ)
_allprops = []
_seenprops = []

class _XMLPropertyCache(object):
    """
    Cache lookup tables mapping classes to their associated
    XMLProperty and XMLChildProperty classes
    """
    def __init__(self):
        self._name_to_prop = {}
        self._prop_to_name = {}

    def _get_prop_cache(self, cls, checkclass):
        cachename = str(cls) + "-" + checkclass.__name__
        if cachename not in self._name_to_prop:
            ret = {}
            for c in reversed(type.mro(cls)[:-1]):
                for key, val in c.__dict__.items():
                    if isinstance(val, checkclass):
                        ret[key] = val
                        self._prop_to_name[val] = key
            self._name_to_prop[cachename] = ret
        return self._name_to_prop[cachename]

    def get_xml_props(self, inst):
        return self._get_prop_cache(inst.__class__, XMLProperty)

    def get_child_props(self, inst):
        return self._get_prop_cache(inst.__class__, XMLChildProperty)

    def get_prop_name(self, propinst):
        return self._prop_to_name[propinst]

_PropCache = _XMLPropertyCache()

class _XMLChildList(list):
    """
    Little wrapper for a list containing XMLChildProperty output.
    This is just to insert a dynamically created add_new() function
    which instantiates and appends a new child object
    """
    def __init__(self, childclass, copylist, xmlbuilder):
        list.__init__(self)
        self._childclass = childclass
        self._xmlbuilder = xmlbuilder
        for i in copylist:
            self.append(i)

    def new(self):
        """
        Instantiate a new child object and return it
        """
        return self._childclass(self._xmlbuilder.conn)

    def add_new(self):
        """
        Instantiate a new child object, append it, and return it
        """
        obj = self.new()
        self._xmlbuilder.add_child(obj)
        return obj

class _XMLPropertyBase(property):
    def __init__(self, fget, fset):
        self._propname = None
        property.__init__(self, fget=fget, fset=fset)

    @property
    def propname(self):
        """
        The variable name associated with this XMLProperty. So with
        a definition like

            foo = XMLProperty("./@bar")

        and this will return "foo".
        """
        if not self._propname:
            self._propname = _PropCache.get_prop_name(self)
        return self._propname

class XMLChildProperty(_XMLPropertyBase):
    """
    Property that points to a class used for parsing a subsection of
    of the parent XML. For example when we deligate parsing
    /domain/cpu/feature of the /domain/cpu class.

    @child_class: XMLBuilder class this property is tracking. So for
        guest.devices.disk this is DeviceDisk
    @relative_xpath: Relative location where the class is rooted compared
        to its xmlbuilder root path. So if xmlbuilder is ./foo and we
        want to track ./foo/bar/baz instances, set relative_xpath=./bar
    @is_single: If True, this represents an XML node that is only expected
        to appear once, like <domain><cpu>
    """
    def __init__(self, child_class, relative_xpath=".", is_single=False):
        self.child_class = child_class
        self.is_single = is_single
        self.relative_xpath = relative_xpath

        _XMLPropertyBase.__init__(self, self._fget, None)

    def __repr__(self):
        return "<XMLChildProperty %s %s>" % (str(self.child_class), id(self))

    def _get(self, xmlbuilder):
        if self.propname not in xmlbuilder._propstore and not self.is_single:
            xmlbuilder._propstore[self.propname] = []
        return xmlbuilder._propstore[self.propname]

    def _fget(self, xmlbuilder):
        if self.is_single:
            return self._get(xmlbuilder)
        return _XMLChildList(self.child_class,
                             self._get(xmlbuilder),
                             xmlbuilder)

    def clear(self, xmlbuilder):
        if self.is_single:
            self._get(xmlbuilder).clear()
        else:
            for obj in self._get(xmlbuilder)[:]:
                xmlbuilder.remove_child(obj)

    def append(self, xmlbuilder, newobj):
        self._get(xmlbuilder).append(newobj)
    def remove(self, xmlbuilder, obj):
        self._get(xmlbuilder).remove(obj)
    def set(self, xmlbuilder, obj):
        xmlbuilder._propstore[self.propname] = obj

    def get_prop_xpath(self, _xmlbuilder, obj):
        return self.relative_xpath + "/" + obj.XML_NAME

class XMLProperty(_XMLPropertyBase):
    def __init__(self, xpath,
                 set_converter=None, validate_cb=None,
                 is_bool=False, is_int=False, is_yesno=False, is_onoff=False,
                 default_cb=None, default_name=None, do_abspath=False):
        """
        Set a XMLBuilder class property that maps to a value in an XML
        document, indicated by the passed xpath. For example, for a
        <domain><name> the definition may look like:

          name = XMLProperty("./name")

        When building XML from scratch (virt-install), 'name' works
        similar to a regular class property(). When parsing and editing
        existing guest XML, we  use the xpath value to get/set the value
        in the parsed XML document.

        :param xpath: xpath string which maps to the associated property
                      in a typical XML document
        :param name: Just a string to print for debugging, only needed
            if xpath isn't specified.
        :param set_converter: optional function for converting the property
            value from the virtinst API to the guest XML. For example,
            the Guest.memory API was once in MiB, but the libvirt domain
            memory API is in KiB. So, if xpath is specified, on a 'get'
            operation we convert the XML value with int(val) / 1024.
        :param validate_cb: Called once when value is set, should
            raise a RuntimeError if the value is not proper.
        :param is_bool: Whether this is a boolean property in the XML
        :param is_int: Whether this is an integer property in the XML
        :param is_yesno: Whether this is a yes/no property in the XML
        :param is_onoff: Whether this is an on/off property in the XML
        :param default_cb: If building XML from scratch, and this property
            is never explicitly altered, this function is called for setting
            a default value in the XML, and for any 'get' call before the
            first explicit 'set'.
        :param default_name: If the user does a set and passes in this
            value, instead use the value of default_cb()
        :param do_abspath: If True, run os.path.abspath on the passed value
        """
        self._xpath = xpath
        if not self._xpath:
            raise RuntimeError("XMLProperty: xpath must be passed.")

        self._is_bool = is_bool
        self._is_int = is_int
        self._is_yesno = is_yesno
        self._is_onoff = is_onoff
        self._do_abspath = do_abspath

        self._validate_cb = validate_cb
        self._convert_value_for_setter_cb = set_converter
        self._default_cb = default_cb
        self._default_name = default_name

        if sum([int(bool(i)) for i in
                [self._is_bool, self._is_int,
                 self._is_yesno, self._is_onoff]]) > 1:
            raise RuntimeError("Conflict property converter options.")

        if self._default_name and not self._default_cb:
            raise RuntimeError("default_name requires default_cb.")

        self._is_tracked = False
        if _trackprops:
            _allprops.append(self)

        _XMLPropertyBase.__init__(self, self.getter, self.setter)

    def __repr__(self):
        return "<XMLProperty %s %s>" % (str(self._xpath), id(self))

    ####################
    # Internal helpers #
    ####################

    def _convert_get_value(self, val):
        # pylint: disable=redefined-variable-type
        if self._default_name and val == self._default_name:
            ret = val
        elif self._is_bool:
            ret = bool(val)
        elif self._is_int and val is not None:
            intkwargs = {}
            if "0x" in str(val):
                intkwargs["base"] = 16
            ret = int(val, **intkwargs)
        elif self._is_yesno and val is not None:
            ret = bool(val == "yes")
        elif self._is_onoff and val is not None:
            ret = bool(val == "on")
        else:
            ret = val
        return ret

    def _convert_set_value(self, xmlbuilder, val):
        if self._default_name and val == self._default_name:
            val = self._default_cb(xmlbuilder)
        elif self._do_abspath and val is not None:
            val = os.path.abspath(val)
        elif self._is_onoff and val is not None:
            val = bool(val) and "on" or "off"
        elif self._is_yesno and val is not None:
            val = bool(val) and "yes" or "no"
        elif self._is_int and val is not None:
            intkwargs = {}
            if "0x" in str(val):
                intkwargs["base"] = 16
            val = int(val, **intkwargs)

        if self._convert_value_for_setter_cb:
            val = self._convert_value_for_setter_cb(xmlbuilder, val)
        return val

    def _default_get_value(self, xmlbuilder):
        """
        Return (can use default, default value)
        """
        ret = (False, -1)
        if not xmlbuilder._xmlstate.is_build:
            return ret
        if self.propname in xmlbuilder._propstore:
            return ret
        if not self._default_cb:
            return ret

        if self._default_name:
            return (True, self._default_name)
        return (True, self._default_cb(xmlbuilder))

    def _set_default(self, xmlbuilder):
        """
        Encode the property default into the XML and propstore, but
        only if a default is registered, and only if the property was
        not already explicitly set by the API user.

        This is called during the get_xml_config process and shouldn't
        be called from outside this file.
        """
        candefault, val = self._default_get_value(xmlbuilder)
        if not candefault:
            return
        self.setter(xmlbuilder, val, validate=False)

    def _nonxml_fset(self, xmlbuilder, val):
        """
        This stores the value in XMLBuilder._propstore
        dict as propname->value. This saves us from having to explicitly
        track every variable.
        """
        propstore = xmlbuilder._propstore

        if self.propname in propstore:
            del(propstore[self.propname])
        propstore[self.propname] = val

    def _nonxml_fget(self, xmlbuilder):
        """
        The flip side to nonxml_fset, fetch the value from
        XMLBuilder._propstore
        """
        candefault, val = self._default_get_value(xmlbuilder)
        if candefault:
            return val
        return xmlbuilder._propstore.get(self.propname, None)

    def clear(self, xmlbuilder):
        # We only unset the cached data, since XML will be cleared elsewhere
        propstore = xmlbuilder._propstore
        if self.propname in propstore:
            self.setter(xmlbuilder, None)

    ##################################
    # The actual getter/setter impls #
    ##################################

    def getter(self, xmlbuilder):
        """
        Fetch value at user request. If we are parsing existing XML and
        the user hasn't done a 'set' yet, return the value from the XML,
        otherwise return the value from propstore

        If this is a built from scratch object, we never pull from XML
        since it's known to the empty, and we may want to return
        a 'default' value
        """
        if _trackprops and not self._is_tracked:
            _seenprops.append(self)
            self._is_tracked = True

        if (self.propname in xmlbuilder._propstore or
            xmlbuilder._xmlstate.is_build):
            val = self._nonxml_fget(xmlbuilder)
        else:
            val = self._get_xml(xmlbuilder)
        return self._convert_get_value(val)

    def _get_xml(self, xmlbuilder):
        """
        Actually fetch the associated value from the backing XML
        """
        xpath = xmlbuilder._xmlstate.make_abs_xpath(self._xpath)
        return xmlbuilder._xmlstate.xmlapi.get_xpath_content(
                xpath, self._is_bool)

    def setter(self, xmlbuilder, val, validate=True):
        """
        Set the value at user request. This just stores the value
        in propstore. Setting the actual XML is only done at
        get_xml_config time.
        """
        if _trackprops and not self._is_tracked:
            _seenprops.append(self)
            self._is_tracked = True

        if validate and self._validate_cb:
            self._validate_cb(xmlbuilder, val)
        self._nonxml_fset(xmlbuilder,
                          self._convert_set_value(xmlbuilder, val))

    def _set_xml(self, xmlbuilder, setval):
        """
        Actually set the passed value in the XML document
        """
        xpath = xmlbuilder._xmlstate.make_abs_xpath(self._xpath)
        xmlbuilder._xmlstate.xmlapi.set_xpath_content(xpath, setval)

class _XMLState(object):
    def __init__(self, root_name, parsexml, parentxmlstate,
                 relative_object_xpath):
        self._root_name = root_name
        self._namespace = ""
        if ":" in self._root_name:
            ns = self._root_name.split(":")[0]
            self._namespace = " xmlns:%s='%s'" % (ns, XMLAPI.NAMESPACES[ns])

        # xpath of this object relative to its parent. So for a standalone
        # <disk> this is empty, but if the disk is the forth one in a <domain>
        # it will be set to ./devices/disk[4]
        self._relative_object_xpath = relative_object_xpath or ""

        # xpath of the parent. For a disk in a standalone <domain>, this
        # is empty, but if the <domain> is part of a <domainsnapshot>,
        # it will be "./domain"
        self._parent_xpath = (
            parentxmlstate and parentxmlstate.abs_xpath()) or ""

        self.xmlapi = None
        self.is_build = False
        if not parsexml and not parentxmlstate:
            self.is_build = True
        self.parse(parsexml, parentxmlstate)

    def parse(self, parsexml, parentxmlstate):
        if parentxmlstate:
            self.is_build = parentxmlstate.is_build or self.is_build
            self.xmlapi = parentxmlstate.xmlapi
            return

        # Make sure passed in XML has required xmlns inserted
        if not parsexml:
            parsexml = "<%s%s/>" % (self._root_name, self._namespace)
        elif self._namespace and "xmlns" not in parsexml:
            parsexml = parsexml.replace("<" + self._root_name,
                    "<" + self._root_name + self._namespace)

        try:
            self.xmlapi = XMLAPI(parsexml)
        except Exception:
            logging.debug("Error parsing xml=\n%s", parsexml)
            raise

    def set_relative_object_xpath(self, xpath):
        self._relative_object_xpath = xpath or ""

    def set_parent_xpath(self, xpath):
        self._parent_xpath = xpath or ""

    def _join_xpath(self, x1, x2):
        if x1.endswith("/"):
            x1 = x1[:-1]
        if x2.startswith("."):
            x2 = x2[1:]
        return x1 + x2

    def abs_xpath(self):
        return self._join_xpath(self._parent_xpath or ".",
                self._relative_object_xpath or ".")

    def make_abs_xpath(self, xpath):
        """
        Convert a relative xpath to an absolute xpath. So for DeviceDisk
        that's part of a Guest, accessing driver_name will do convert:
            ./driver/@name
        to an absolute xpath like:
            ./devices/disk[3]/driver/@name
        """
        return self._join_xpath(self.abs_xpath() or ".", xpath)

class XMLBuilder(object):
    """
    Base for all classes which build or parse domain XML
    """
    # Order that we should apply values to the XML. Keeps XML generation
    # consistent with what the test suite expects.
    _XML_PROP_ORDER = []

    # Name of the root XML element
    XML_NAME = None

    # In some cases, libvirt can incorrectly generate unparseable XML.
    # These are libvirt bugs, but this allows us to work around it in
    # for specific XML classes.
    #
    # Example: nodedev 'system' XML:
    # https://bugzilla.redhat.com/show_bug.cgi?id=1184131
    _XML_SANITIZE = False

    def __init__(self, conn, parsexml=None,
                 parentxmlstate=None, relative_object_xpath=None):
        """
        Initialize state

        :param conn: VirtinstConnection to validate device against
        :param parsexml: Optional XML string to parse

        The rest of the parameters are for internal use only
        """
        self.conn = conn

        if self._XML_SANITIZE:
            if hasattr(parsexml, 'decode'):
                parsexml = parsexml.decode("ascii", "ignore").encode("ascii")
            else:
                parsexml = parsexml.encode("ascii", "ignore").decode("ascii")

            parsexml = "".join([c for c in parsexml if c in string.printable])

        self._propstore = collections.OrderedDict()
        self._xmlstate = _XMLState(self.XML_NAME,
                                   parsexml, parentxmlstate,
                                   relative_object_xpath)

        self._validate_xmlbuilder()
        self._initial_child_parse()

    def _validate_xmlbuilder(self):
        # This is one time validation we run once per XMLBuilder class
        cachekey = self.__class__.__name__ + "_xmlbuilder_validated"
        if getattr(self.__class__, cachekey, False):
            return

        xmlprops = self._all_xml_props()
        childprops = self._all_child_props()
        for key in self._XML_PROP_ORDER:
            if key not in xmlprops and key not in childprops:
                raise RuntimeError("programming error: key '%s' must be "
                                   "xml prop or child prop" % key)

        childclasses = []
        for childprop in childprops.values():
            if childprop.child_class in childclasses:
                raise RuntimeError("programming error: can't register "
                        "duplicate child_classs=%s" % childprop.child_class)
            childclasses.append(childprop.child_class)

        setattr(self.__class__, cachekey, True)

    def _initial_child_parse(self):
        # Walk the XML tree and hand of parsing to any registered
        # child classes
        for xmlprop in list(self._all_child_props().values()):
            child_class = xmlprop.child_class
            prop_path = xmlprop.get_prop_xpath(self, child_class)

            if xmlprop.is_single:
                obj = child_class(self.conn,
                    parentxmlstate=self._xmlstate,
                    relative_object_xpath=prop_path)
                xmlprop.set(self, obj)
                continue

            if self._xmlstate.is_build:
                continue

            nodecount = self._xmlstate.xmlapi.count(
                self._xmlstate.make_abs_xpath(prop_path))
            for idx in range(nodecount):
                idxstr = "[%d]" % (idx + 1)
                obj = child_class(self.conn,
                    parentxmlstate=self._xmlstate,
                    relative_object_xpath=(prop_path + idxstr))
                xmlprop.append(self, obj)

    def __repr__(self):
        return "<%s %s %s>" % (self.__class__.__name__.split(".")[-1],
                               self.XML_NAME, id(self))

    ############################
    # Public XML managing APIs #
    ############################

    def get_xml_config(self):
        """
        Return XML string of the object
        """
        xmlapi = self._xmlstate.xmlapi
        if self._xmlstate.is_build:
            xmlapi = xmlapi.copy_api()

        self._add_parse_bits(xmlapi)
        ret = xmlapi.get_xml(self._xmlstate.make_abs_xpath("."))

        if ret and not ret.endswith("\n"):
            ret += "\n"
        return ret

    def clear(self, leave_stub=False):
        """
        Wipe out all properties of the object

        :param leave_stub: if True, don't unlink the top stub node,
            see virtinst/cli usage for an explanation
        """
        props = list(self._all_xml_props().values())
        props += list(self._all_child_props().values())
        for prop in props:
            prop.clear(self)

        is_child = bool(re.match("^.*\[\d+\]$", self._xmlstate.abs_xpath()))
        if is_child or leave_stub:
            # User requested to clear an object that is the child of
            # another object (xpath ends in [1] etc). We can't fully remove
            # the node in that case, since then the xmlbuilder object is
            # no longer valid, and all the other child xpaths will be
            # pointing to the wrong node. So just stub out the content
            self._xmlstate.xmlapi.node_clear(self._xmlstate.abs_xpath())
        else:
            self._xmlstate.xmlapi.node_force_remove(self._xmlstate.abs_xpath())

    def validate(self):
        """
        Validate any set values and raise an exception if there's
        a problem
        """
        pass

    def set_defaults(self, guest):
        """
        Encode any default values if needed
        """
        ignore = guest

    def get_xml_id(self):
        """
        Return the location of the object in the XML document. This is
        basically the absolute xpath, but the value returned should be
        treated as opaque, it's just for cross XML comparisons. Used
        in virt-manager code
        """
        return self._xmlstate.abs_xpath()

    def get_xml_idx(self):
        """
        This is basically the offset parsed out of the object's xpath,
        minus 1. So if this is the fifth <disk> in a <domain>, ret=4.
        If this is the only <cpu> in a domain, ret=0.
        """
        xpath = self._xmlstate.abs_xpath()
        if "[" not in xpath:
            return 0
        return int(xpath.rsplit("[", 1)[1].strip("]")) - 1

    ################
    # Internal API #
    ################

    def _all_xml_props(self):
        """
        Return a list of all XMLProperty instances that this class has.
        """
        return _PropCache.get_xml_props(self)

    def _all_child_props(self):
        """
        Return a list of all XMLChildProperty instances that this class has.
        """
        return _PropCache.get_child_props(self)

    def _find_child_prop(self, child_class):
        xmlprops = self._all_child_props()
        for xmlprop in list(xmlprops.values()):
            if xmlprop.is_single:
                continue
            if child_class is xmlprop.child_class:
                return xmlprop
        raise RuntimeError("programming error: "
                           "Didn't find child property for child_class=%s" %
                           child_class)

    def _set_xpaths(self, parent_xpath, relative_object_xpath=-1):
        """
        Change the object hierarchy's cached xpaths
        """
        self._xmlstate.set_parent_xpath(parent_xpath)
        if relative_object_xpath != -1:
            self._xmlstate.set_relative_object_xpath(relative_object_xpath)
        for propname in self._all_child_props():
            for p in util.listify(getattr(self, propname, [])):
                p._set_xpaths(self._xmlstate.abs_xpath())

    def _set_child_xpaths(self):
        """
        Walk the list of child properties and make sure their
        xpaths point at their particular element. Needs to be called
        whenever child objects are added or removed
        """
        typecount = {}
        for propname, xmlprop in self._all_child_props().items():
            for obj in util.listify(getattr(self, propname)):
                idxstr = ""
                if not xmlprop.is_single:
                    class_type = obj.__class__
                    if class_type not in typecount:
                        typecount[class_type] = 0
                    typecount[class_type] += 1
                    idxstr = "[%d]" % typecount[class_type]

                prop_path = xmlprop.get_prop_xpath(self, obj)
                obj._set_xpaths(self._xmlstate.abs_xpath(),
                        prop_path + idxstr)

    def _parse_with_children(self, *args, **kwargs):
        """
        Set new backing XML objects in ourselves and all our child props
        """
        self._xmlstate.parse(*args, **kwargs)
        for propname in self._all_child_props():
            for p in util.listify(getattr(self, propname, [])):
                p._parse_with_children(None, self._xmlstate)

    def add_child(self, obj):
        """
        Insert the passed XMLBuilder object into our XML document. The
        object needs to have an associated mapping via XMLChildProperty
        """
        xmlprop = self._find_child_prop(obj.__class__)
        xml = obj.get_xml_config()
        xmlprop.append(self, obj)
        self._set_child_xpaths()

        if not obj._xmlstate.is_build:
            use_xpath = obj._xmlstate.abs_xpath().rsplit("/", 1)[0]
            indent = 2 * obj._xmlstate.abs_xpath().count("/")
            self._xmlstate.xmlapi.node_add_xml(
                    util.xml_indent(xml, indent), use_xpath)
        obj._parse_with_children(None, self._xmlstate)

    def remove_child(self, obj):
        """
        Remove the passed XMLBuilder object from our XML document, but
        ensure its data isn't altered.
        """
        xmlprop = self._find_child_prop(obj.__class__)
        xmlprop.remove(self, obj)

        xpath = obj._xmlstate.abs_xpath()
        xml = obj.get_xml_config()
        obj._set_xpaths(None, None)
        obj._parse_with_children(xml, None)
        self._xmlstate.xmlapi.node_force_remove(xpath)
        self._set_child_xpaths()

    #################################
    # Private XML building routines #
    #################################

    def _add_parse_bits(self, xmlapi):
        """
        Callback that adds the implicitly tracked XML properties to
        the backing xml.
        """
        origpropstore = self._propstore.copy()
        origapi = self._xmlstate.xmlapi
        try:
            self._xmlstate.xmlapi = xmlapi
            return self._do_add_parse_bits()
        finally:
            self._xmlstate.xmlapi = origapi
            self._propstore = origpropstore

    def _do_add_parse_bits(self):
        # Set all defaults if the properties have one registered
        xmlprops = self._all_xml_props()
        childprops = self._all_child_props()

        for prop in list(xmlprops.values()):
            prop._set_default(self)

        # Set up preferred XML ordering
        do_order = [p for p in self._propstore if p not in childprops]
        for key in reversed(self._XML_PROP_ORDER):
            if key in do_order:
                do_order.remove(key)
                do_order.insert(0, key)
            elif key in childprops:
                do_order.insert(0, key)

        for key in sorted(list(childprops.keys())):
            if key not in do_order:
                do_order.append(key)

        # Alter the XML
        for key in do_order:
            if key in xmlprops:
                xmlprops[key]._set_xml(self, self._propstore[key])
            elif key in childprops:
                for obj in util.listify(getattr(self, key)):
                    obj._add_parse_bits(self._xmlstate.xmlapi)


from __future__ import print_function

import json
from unittest import TestCase

import jsonpickle
import redis

from main import sgcheck_api

class TestAdd_account(TestCase):
    def setUp(self):
        self.app = sgcheck_api.app.test_client()

        self.r = redis.StrictRedis(
            host=sgcheck_api.REDIS_HOST,
            port=6379,
            db=0)

    def test_add_account(self):
        request = json.dumps({'key': 'testkey', 'secret': 'testsecret'})
        response = self.app.post('/api/add',
                                 data=request,
                                 headers={"content-type": "application/json"}
                                 )
        print (response.data)
        frozen = self.r.get('testkey')
        dummy_account = jsonpickle.decode(frozen)
        self.assertIn('200 OK', str(response))
        self.assertEqual(dummy_account.secret, 'testsecret')
        self.assertIsNotNone(dummy_account.created)
        self.assertFalse(dummy_account.setup)

        self.r.delete('testkey')

    def test_add_account_syntax_missing(self):
        request = json.dumps({'secret': 'testsecret'})
        response = self.app.post('/api/add',
                                 data=request,
                                 headers={"content-type": "application/json"}
                                 )
        print(response.data)
        response_json = json.loads(response.data)
        self.assertIn('200 OK', str(response))
        self.assertEqual(response_json['Status'], 'Failed')
        self.assertEqual(response_json['Error'], 'Invalid use. Both key and secret must be specified.')

    def test_add_account_syntax_blank(self):
        request = json.dumps({'key': '', 'secret': 'testsecret'})
        response = self.app.post('/api/add',
                                 data=request,
                                 headers={"content-type": "application/json"}
                                 )
        print(response.data)
        response_json = json.loads(response.data)
        self.assertIn('200 OK', str(response))
        self.assertEqual(response_json['Status'], 'Failed')
        self.assertEqual(response_json['Error'], 'Invalid use. Both key and secret must have non-zero length.')


# -*- coding: utf-8 -*-

import pymysql.cursors
import difflib
import mwclient

connection = pymysql.connect(host='localhost',
                             user='vedmaka',
                             password='',
                             db='c9',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

countries = []

try:
    with connection.cursor() as cursor:
        # Read countries
        sql = "SELECT `id`, `sortname`, `name` FROM `countries`"
        cursor.execute(sql)
        result = cursor.fetchone()
        while result is not None:
            countries.append( result['name'].lower() )
            result = cursor.fetchone()
finally:
    connection.close()

matching_countries = difflib.get_close_matches('rossiya', countries)
confidence = difflib.SequenceMatcher(None, matching_countries[0], 'rossiya').quick_ratio()

print("Confidence: %s" % confidence)
print("----------------------------")
print(matching_countries)

site = mwclient.Site('testing-cloud-9-ide-1-vedmaka.c9users.io', '/')
site.login('admin','q1w2e3r4')


import numpy as np

class DropTheLoser3(object):
    def __init__(self, X, n_1, n_2, n_3, win_idx1, win_idx):
        self.X = X
        # self.X_2 = X_2
        # self.X_3 = X_3
        self.K = len(X)
        self.n_1 = n_1
        self.n_2 = n_2
        self.n_3 = n_3
        # self.X_bar_1 = np.mean(X_1, 1)
        # self.X_bar_2 = np.mean(X_2, 1)
        # self.selection = selection
        # self.uc = uc
        # self.UC = self.X_bar + uc * np.std(X, axis=1, ddof=1)
        # if selection == 'mean':
        self.win_idx1 = win_idx1 #np.argsort(self.X_bar_1)[::-1][-10:]
        self.win_idx_in_2 = list(self.win_idx1).index(win_idx)
        self.win_idx = win_idx  #self.win_idx1[self.win_idx_in_2]
        # else:
        #     self.win_idx = np.argmax(self.UC)
        self.theta_hat = X[win_idx].mean()

    def basis(self, X_b):
        """
        Compute the basis given any generated data
        :param X_b:
        :param X_2_b:
        :param basis_type:
        :return:
        """
        Z_b = np.zeros(self.K)
        for k in range(self.K):
            Z_b[k] = np.mean(X_b[k])
        # if basis_type == "complete":
        #     Z_b = np.concatenate([np.mean(X_b, 1), np.mean(X_b ** 2, 1), [np.mean(X_2_b)], [np.mean(X_2_b ** 2)]])
        # elif basis_type == "naive":
        #     Z_b = np.mean(X_b, 1)
        #     Z_b[self.win_idx] = d_M
        # elif basis_type == "withD0":
        #     Z_b = np.mean(X_b, 1)
        #     Z_b = np.concatenate([Z_b, np.array(d_M).reshape(1, )])
        # else:
        #     raise AssertionError("invalid basis_type")
        return Z_b

    def test_statistic(self, X_win_tot):
        """
        Compute the test statistic \hat{\theta}
        :param X_b:
        :param X_2_b:
        :return:
        """
        # tmp = np.hstack([X_1_b[self.win_idx], X_2_b[self.win_idx_in_2], X_3_b])
        d_M = np.mean(X_win_tot)
        # d_M = (np.sum(X_1_b[self.win_idx, :]) + np.sum(X_2_b) + np.sum(X_3_b)) / (len(X_b[self.win_idx, :]) + len(X_2_b))
        # if not return_D0:
        #     return d_M
        # d_0 = np.mean(X_b[self.win_idx, :]) - np.mean(X_2_b)
        return d_M

    def gen_train_data(self, ntrain, return_gamma=True):
        """
        bootstrap training data
        :param ntrain: number of training data
        :param return_gamma: whether return gamma = Cov(B, theta_hat) Var(theta_hat)^{-1}
        :return:
        """
        Z_train = []
        W_train = []
        theta_hat_train = []
        D0_train = []
        for i in range(ntrain):
            X_b_1 = np.zeros([self.K, self.n_1])
            for k in range(self.K):
                tmp = np.random.choice(self.X[k], size=self.n_1, replace=True)
                X_b_1[k, :] = tmp
            idx_1 = np.argsort(np.mean(X_b_1, 1))[::-1][-10:]
            # Stage 2
            X_b_2 = np.zeros([10, self.n_2])
            s = 0
            for k in self.win_idx1:
                X_b_2[s, :] = np.random.choice(self.X[k], size=self.n_2, replace=True)
                s += 1
            idx = self.win_idx1[np.argmax(np.mean(X_b_2, 1))]
            if idx == self.win_idx:
                W_train.append(1)
            else:
                W_train.append(0)
            # Stage 3
            X_b_3 = np.random.choice(self.X[idx], size=self.n_3, replace=True)

            X_b = []
            for k in range(self.K):
                if k not in self.win_idx1:
                    X_b.append(X_b_1[k, :])
                elif k == self.win_idx:
                    X_b.append(np.hstack([X_b_1[self.win_idx], X_b_2[self.win_idx_in_2], X_b_3]))
                else:
                    X_b.append(np.hstack([X_b_1[k, :], X_b_2[list(self.win_idx1).index(k), :]]))
            Z_train.append(self.basis(X_b))
            theta_hat_train.append(self.test_statistic(X_b[self.win_idx]))

        Z_train = np.array(Z_train)
        W_train = np.array(W_train)
        result = {'Z_train': Z_train, 'W_train': W_train}
        theta_hat_train = np.array(theta_hat_train)
        if return_gamma:
            cov_Z_theta = (Z_train - np.mean(Z_train, 0)).T @ (theta_hat_train - np.mean(theta_hat_train, 0)) / ntrain
            var_theta = np.cov(theta_hat_train.T)
            if var_theta.size == 1:
                var_theta = var_theta.reshape(1, 1)
                cov_Z_theta = cov_Z_theta.reshape(len(cov_Z_theta), 1)
            gamma = cov_Z_theta @ np.linalg.inv(var_theta)
            result['gamma'] = gamma
        return result


class StartRoom():
    def __init__(self):
        self.name = "Start"
        self.image = "Pixel Art/start.png"
        self.state = True  # True - cleared stage / False - not cleaed stage
        self.outcomes = ""
        self.connected = set()

class StairsRoom():
    def __init__(self):
        self.name = "Stairs"
        self.image = "Pixel Art/stairs.png"
        self.state = True  # True - cleared stage / False - not cleaed stage
        self.outcomes = ""
        self.connected = set()

class EmptyRoom():
    def __init__(self):
        self.name = "Empty"
        self.image = "Pixel Art/room.png"
        self.state = True  # True - cleared stage / False - not cleaed stage
        self.outcomes = ""
        self.connected = set()

class MoneyRoom():
    def __init__(self):
        self.name = "Money"
        self.image = "Pixel Art/money.png"
        self.state = False  # True - cleared stage / False - not cleaed stage
        self.outcomes = "outcomes/money.json"
        self.connected = set()

class MobRoom():
    def __init__(self):
        self.name = "Mob"
        self.image = "Pixel Art/mob.png"
        self.state = False  # True - cleared stage / False - not cleaed stage
        self.outcomes = "outcomes/mob.json"
        self.connected = set()

# USELESS

class VendorRoom():
    def __init__(self):
        self.name = "Vendor"
        self.image = "Pixel Art/vendor.png"
        self.state = False  # True - cleared stage / False - not cleaed stage
        self.outcomes = "outcomes/vendor.json"
        self.connected = set()

class TrapsRoom():
    def __init__(self):
        self.name = "Traps"
        self.image = "Pixel Art/trap.png"
        self.state = False  # True - cleared stage / False - not cleaed stage
        self.outcomes = "outcomes/traps.json"
        self.connected = set()

class ItemsRoom():
    def __init__(self):
        self.name = "Items"
        self.image = "Pixel Art/item.png"
        self.state = False  # True - cleared stage / False - not cleaed stage
        self.outcomes = "outcomes/items.json"
        self.connected = set()

class BossRoom():
    def __init__(self):
        self.name = "Boss"
        self.image = "Pixel Art/boss.png"
        self.state = False  # True - cleared stage / False - not cleaed stage
        self.outcomes = "outcomes/boss.json"
        self.connected = set()


import numpy as np
import matplotlib.pyplot as plt
from time import perf_counter

from mesh import Mesh
from solver import jacobi
from solver import tdma

def lista5_1(n, tol, model):
    #n = 100

    L = model['L']
    D = model['D']

    # Propriedades

    #tol = 0.001
    max_it = 1e8

    def dirichlet(model):
        """
        Aplica condição de contorno de temperatura prescrita.
        Equação do volume tem forma:
        ap Tp = ae Te + aw Tw + B + a0 T0
        """
        H = model['H']
        K = model['K']
        Alpha = model['Alpha']

        Tb = model['Tb']
        Tamb = model['Tamb']

        dx = model['dx']
        dt = model['dt']

        hh = 4*H / (K*D)

        a0 = dx / (Alpha*dt)
        ap = a0 + 3 / dx + hh*dx
        ae = 1 / dx
        b = hh*Tamb*dx + 2*Tb / dx

        return np.array([ap, 0, ae, a0, b])

    def newmann(model):
        """
        Aplica a condição de contorno de fluxo prescrito.
        Equação do volume tem forma:
        ap Tp = ae Te + aw Tw + B + a0 T0
        """

        H = model['H']
        K = model['K']
        Alpha = model['Alpha']

        Tb = model['Tb']
        Tamb = model['Tamb']

        dx = model['dx']
        dt = model['dt']
        hh = 4*H / (K*D)

        a0 = dx / (Alpha*dt)
        ap = a0 + 1 / dx + hh*dx
        aw = 1 / dx
        b = hh*Tamb*dx

        return np.array([ap, aw, 0, a0, b])

    def internal_volume(model):
        """
        Implementa um volume interno genérico.
        Equação do volume tem forma:
        ap Tp = ae Te + aw Tw + B + a0 T0
        """

        H = model['H']
        K = model['K']
        Alpha = model['Alpha']

        Tamb = model['Tamb']

        dx = model['dx']
        dt = model['dt']
        hh = 4*H / (K*D)

        a0 = dx / (Alpha*dt)
        ap = a0 + 2 / dx + hh*dx
        aw = ae = 1 / dx
        b = hh*Tamb*dx

        return np.array([ap, aw, ae, a0, b])

    # Constroi matrizes da forma:
    # [A][T] = [b]+[A0][T0]

    print('Building model')

    A = np.zeros((n,n))
    A0 = np.zeros(n)
    b = np.zeros(n)

    for i in range(n):

        if i == 0:
            # Primeiro volume
            a = dirichlet(model)
            A[i, i+1] = -a[2]

            #print(f'{i} : Dirichlet')

        elif i == n-1:
            # Ultimo volume
            a = newmann(model)
            A[i, i-1] = -a[1]

            #print(f'{i} : Newmann')

        else:
            a = internal_volume(model)
            A[i, i-1] = -a[1]
            A[i, i+1] = -a[2]
            #print(f'{i} : Interno')

        A[i, i] = a[0]

        A0[i] = a[3]
        b[i] = a[4]

    #print(A)

    # SOLVER
    diff = model['Tb']
    it = 1

    # Condição inicial
    T = model['T0'] * np.ones(n)
    #T00 = np.copy(T)
    #B = b +  np.multiply(A0, T00)

    print('Iniciando solver')
    start_time = perf_counter()
    while diff >= tol and it < max_it:

        #print(it)
        #print(T)

        T00 = np.copy(T)

        B = b + np.multiply(A0, T00)

        T = tdma(A, B, T00)
        #T, itj = jacobi(A, B, T00)
        #print(itj)

        diff = np.max(np.abs(T - T00))
        it += 1

    end_time = perf_counter()
    print(f'Solução {n} volumes')
    print(f'Tempo de execução: {end_time - start_time}')
    print(f'Número de iterações: {it}')
    #print(T)

    return T

n = 100
L = 0.05
D = 0.01

model = {'K' : 10.0,
        'H': 5.0,
        'Alpha': 1e-6,
        'Tb' : 373,
        'Tamb' :293,
        'T0' : 293,
        'dx' : L/(n),
        'dt' : 0.5,
        'L' : L,
        'D' : D}

###############################################################################
## PLOT VOLUMES DIFERENTES
###############################################################################
L = model['L']

n = 1000
tol = 1e-2

model['dx'] = L/n
x10 = np.linspace(0,L,n)
T10 = lista5_1(n, tol, model)
#print(T10)

'''
## 10 Volumes
n = 10
tol = 1e-3

model['dx'] = L/n
x10 = np.linspace(0,L,n)
T10 = lista5_1(n, tol, model)

## 20 Volumes
n = 20
tol = 1e-3

model['dx'] = L/n
x20 = np.linspace(0,L,n)
T20 = lista5_1(n, tol, model)

## 40 Volumes
n = 40
tol = 1e-3

model['dx'] = L/n
x40 = np.linspace(0,L,n)
T40 = lista5_1(n, tol, model)

## 80 Volumes
n = 80
tol = 1e-3

model['dx'] = L/n
x80 = np.linspace(0,L,n)
T80 = lista5_1(n, tol, model)

## 160 Volumes
n = 160
tol = 1e-3

model['dx'] = L/n
x160 = np.linspace(0,L,n)
T160 = lista5_1(n, tol, model)

## 100 Volumes
#n = 100
#tol = 1e-3

#model['dx'] = L/n
#x100 = np.linspace(0,L,n)
#T100 = lista5_1(n, tol, model)

## Analítico
def analitic(n, model):

    H = model['H']
    K = model['K']

    Tamb = model['Tamb']
    Tb = model['Tb']

    hh = 4*H / (K*D)
    m = hh**(1/2)
    x = np.linspace(0,L,n)

    c = np.cosh(m * (L-x)) / np.cosh(m*L)

    T = c * (Tb - Tamb) + Tamb

    return x, T
    #x = np.linspace(0,L,n)

xanl, Tanl = analitic(100, model)

fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x10, T10, label='10 Volumes', linestyle='dashed', marker='.')
ax.plot(x20, T20, label='20 Volumes', linestyle='dashed', marker='+')
ax.plot(x40, T40, label='40 Volumes', linestyle='dotted')#, marker='.')
ax.plot(x80, T80, label='80 Volumes', linestyle='dashdot')#, marker='.')
ax.plot(x160, T160, label='160 Volumes', linestyle='dashed')#, marker='.')
#ax.plot(xanl, Tanl, label='Solução analítica')
ax.set_xlabel('x [m]', fontsize=14)
ax.set_ylabel('Temperatura [K]', fontsize=14)
ax.tick_params(axis='both', which='major', labelsize=12)
#ax.set_title('Soluções para diferentes discretizações.')
ax.legend()
ax.grid()
fig.show()
'''

###############################################################################
## PLOT TOLERÂNCIAS DIFERENTES
###############################################################################
'''
L = model['L']
n = 80
x = np.linspace(0,L,n)

##
tol = 1e-3

model['dx'] = L/n
T3 = lista5_1(n, tol, model)

##
tol = 1e-4

model['dx'] = L/n
T4 = lista5_1(n, tol, model)

##
tol = 1e-5

model['dx'] = L/n
T5 = lista5_1(n, tol, model)

##
tol = 1e-6

model['dx'] = L/n
T6 = lista5_1(n, tol, model)

fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, T3, label='Tolerância : 1e-3', linestyle='dotted')#, marker='.')
ax.plot(x, T4, label='Tolerância : 1e-4', linestyle='dashdot')#, marker='.')
ax.plot(x, T5, label='Tolerância : 1e-5', linestyle='dashed')#, marker='.')
ax.plot(x, T6, label='Tolerância : 1e-6', linestyle='dashed')#, marker='.')
#ax.plot(xanl, Tanl, label='Solução analítica')
ax.set_xlabel('x [m]', fontsize=14)
ax.set_ylabel('Temperatura [K]', fontsize=14)
ax.tick_params(axis='both', which='major', labelsize=12)
#ax.set_title('Soluções para diferentes tolerâncias considerando 80 volumes finitos')
ax.legend()
ax.grid()
fig.show()

## 160 Volumes
fig, ax = plt.subplots(figsize=(10, 6))
#ax.plot(x, T3, label='Tolerância : 1e-3', linestyle='dotted')#, marker='.')
#ax.plot(x, T4, label='Solução Numérica', linestyle='dashed')#, marker='.')
#ax.plot(x, T5, label='Tolerância : 1e-5', linestyle='dashed')#, marker='.')
ax.plot(x, T6, label='Tolerância : 1e-6', linestyle='dashed')#, marker='.')
ax.plot(xanl, Tanl, label='Solução Analítica')
ax.set_xlabel('x [m]', fontsize=14)
ax.set_ylabel('Temperatura [K]', fontsize=14)
ax.tick_params(axis='both', which='major', labelsize=12)
#ax.set_title('Soluções para diferentes tolerâncias considerando 80 volumes finitos')
ax.legend()
ax.grid()
fig.show()
'''


#
#	MathExtraction_Unet.py -- メインファイル
#

import os
import numpy as np
from keras.optimizers import Adam
import keras.backend as K
import tensorflow as tf
from keras.callbacks import ModelCheckpoint, CSVLogger
import sys
import cv2
from skimage.morphology import dilation, disk

from unet_param import UNet

# python3 MathExtraction_Unet.py [input image] [output image] [win_size] [layer_num] [input_weight_file]

argv = sys.argv

scale = 0.25
IMAGE_SIZE = int(argv[3])
LAYER_NUM = int(argv[4])

FIRST_LAYER_FILTER_COUNT = 64
image_stride = int ( IMAGE_SIZE/2 )
weight_file = argv[5]

# 値を-1から1に正規化する関数
def normalize_x(image):
	image = image/127.5 - 1
	return image

# 値を0から1に正規化する関数
def normalize_y(image):
	image = image/255
	return image

# 値を0から255に戻す関数
def denormalize_y(image):
	image = image*255
	return image

def denormalize_y_max(image):
	max_pixel = image.max()
	image = image/max_pixel * 255
	return image

# インプット画像を読み込む関数
def load_X(image_path):

	image = cv2.imread ( image_path, cv2.IMREAD_GRAYSCALE )
	h, w = image.shape

	image = cv2.bitwise_not ( image )
	image = dilation ( image, selem=disk(int(1 / (scale*2))) )

	nh = int ( h * scale )
	nw = int ( w * scale )

	image = cv2.resize( image, (nw,nh) , interpolation = cv2.INTER_CUBIC)
#	image = cv2.copyMakeBorder ( image, IMAGE_SIZE, IMAGE_SIZE, IMAGE_SIZE, IMAGE_SIZE, cv2.BORDER_WRAP)

	nh += IMAGE_SIZE * 2;
	nw += IMAGE_SIZE * 2;

	num_row = nh // image_stride
	num_col = nw // image_stride

	padh = num_row * image_stride + IMAGE_SIZE
	padw = num_col * image_stride + IMAGE_SIZE
	image = cv2.copyMakeBorder ( image, IMAGE_SIZE, (padh-nh)+IMAGE_SIZE, IMAGE_SIZE, (padw-nw)+IMAGE_SIZE, cv2.BORDER_WRAP)

	images = np.zeros((num_row * num_col, IMAGE_SIZE, IMAGE_SIZE, 1), np.float32)
	#cv2.imwrite ( 'original.png', image )
	axes = []
	i = 0;
	for row in range ( num_row ):
		for col in range ( num_col ) :
			cropimg = np.copy ( image[row*image_stride:row*image_stride+IMAGE_SIZE,
									  col*image_stride:col*image_stride+IMAGE_SIZE,
									  np.newaxis] )
			images[i] = normalize_y ( cropimg )
			i+=1
			axes.append ( (row*image_stride, col*image_stride ) )

	return images, axes, (padh, padw), (nh, nw), normalize_y ( image[:,:,np.newaxis] )

# 学習後のU-Netによる予測を行う関数
def predict( Input, Output ):

	# Inputで指定された画像を読み込み sliding Window に分ける
	X_test, axes, img_shape, org_shape, org_img = load_X(Input)

	input_channel_count = 1
	output_channel_count = 1
	first_layer_filter_count = FIRST_LAYER_FILTER_COUNT
	network = UNet(input_channel_count, output_channel_count, first_layer_filter_count, IMAGE_SIZE, LAYER_NUM )
	model = network.get_model()
	model.load_weights(weight_file)

	BATCH_SIZE = 8
	Y_pred = model.predict(X_test, BATCH_SIZE, verbose=True)

	result_img = np.zeros ( (img_shape[0], img_shape[1], 1), np.float32 )

	for i, y in enumerate(Y_pred):
		z = result_img[axes[i][0]:axes[i][0]+IMAGE_SIZE, axes[i][1]:axes[i][1]+IMAGE_SIZE, :]
		result_img[axes[i][0]:axes[i][0]+IMAGE_SIZE, axes[i][1]:axes[i][1]+IMAGE_SIZE] = np.maximum ( z, y )

	result = result_img * org_img

	return result[IMAGE_SIZE:org_shape[0]-IMAGE_SIZE,IMAGE_SIZE:org_shape[1]-IMAGE_SIZE]

def postprocess ( pred_img, image_path ):
	image = cv2.imread ( image_path, cv2.IMREAD_GRAYSCALE )
	image = cv2.bitwise_not ( image )

	h, w = image.shape
	pred_resized=cv2.resize(pred_img,(w,h),interpolation=cv2.INTER_CUBIC)
	first_masked = pred_resized * image

	labelnum, labelimg, contours, GoCs = cv2.connectedComponentsWithStats(image)

	for i in range(labelnum):
		left  = contours[i][cv2.CC_STAT_LEFT]
		right = left + contours[i][cv2.CC_STAT_WIDTH]
		top   = contours[i][cv2.CC_STAT_TOP]
		bottom = top + contours[i][cv2.CC_STAT_HEIGHT]
		crop_first = first_masked[top:bottom,left:right]
		crop_label = labelimg[top:bottom,left:right]
		crop_img = image[top:bottom,left:right]

		second_masked = (crop_label == i) * crop_first
		second_area = np.sum ( second_masked/255 )

		if ( second_area / contours[i][cv2.CC_STAT_AREA] ) < 0.7:
			crop_img[ crop_label == i ] = 0

	tmp_image = dilation ( image, selem=disk(int(1 / (scale*2))) )
	nh = int ( h * scale )
	nw = int ( w * scale )
	tmp_image = cv2.resize( tmp_image, (nw,nh) , interpolation = cv2.INTER_CUBIC)
	result_image = dilation ( tmp_image, selem=disk(1) )

	return (result_image)/255

if __name__ == '__main__':

	args = sys.argv

	InputFile = args[1]
	OutputFile = args[2]

	# config = tf.ConfigProto(allow_soft_placement=True)
	# config.gpu_options.allow_growth = True
	# session = tf.Session(config=config)
	# K.set_session(session)

	pred_img = predict( InputFile, OutputFile )

	result_image = postprocess ( pred_img, InputFile )

	cv2.imwrite( OutputFile,  denormalize_y_max(result_image) )


list = []
def maxi(list):
    maxi = list[0]
    for i  in range(len(list)):
        if (maxi < list[i]):
            maxi = list[i]
    return maxi
def mini(list):
    min = list[0]
    for i  in range(len(list)):
        if (min > list[i]):
            min = list[i]
    return min
def sommi(list):
    sum = 0
    for i in range(len(list)):
        sum = sum + list[i]
    return sum
def moy(list):
    return sommi(list) / len(list)

N = int(input("list size\n"))
for i in range (N):
    elm = int(input("New list element \n"))
    list.append(elm)

print(maxi(list),mini(list), sommi(list), moy(list))