from multiprocessing import Process, Queue
import multiprocessing
import threading
import time
import uuid
import ctypes
import json
import traceback
import copy
import sys

class _threading(threading.Thread):
    def __init__(self, *args, **keywords):
        threading.Thread.__init__(self, *args, **keywords)

    def get_id(self):
        for id, thread in threading._active.items():
            if thread is self:
                return id

    def kill(self):
        thread_id = self.get_id()
        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(thread_id), ctypes.py_object(SystemExit))
        if res == 0:
            logging.debug("Exception raise failure - invalid thread ID")
        if res > 1:
            ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(thread_id), 0)

class workerHandler:
    class _worker:
        def __init__(self, name, call, args, delete, maxDuration, multiprocessing):
            self.name = name
            self.call = call
            self.id = str(uuid.uuid4())
            self.createdTime = int(time.time())
            self.startTime = 0
            self.endTime = 0
            self.duration = 0
            self.result = None
            self.running = None
            self.args = args
            self.multiprocessing = multiprocessing
            if not self.multiprocessing:
                self.thread = _threading(target=self.threadCall)
            else:
                self.thread = _threading(target=self.multiprocessingThreadCall)
            self.maxDuration = maxDuration
            self.delete = delete

        def start(self):
            self.thread.start()

        def multiprocessingThreadCall(self):
            self.startTime = int(time.time())
            self.running = True
            logging.debug("Threaded process worker started, workerID={0}".format(self.id))

            Q = Queue()
            p = Process(target=multiprocessingThreadStart, args=(Q,self.call,self.args)) # Taking an entire copy of cache is not effient review bug
            try:
                p.start()
                try:
                    rc, e = Q.get(timeout=self.maxDuration)
                    p.join(timeout=self.maxDuration)
                except:
                    raise SystemExit

                if rc != 0:
                    logging.debug("Threaded process worker crashed, workerID={0}".format(self.id))
                    systemTrigger.failedTrigger(self.id,"triggerCrashed",''.join(traceback.format_exception(etype=type(e), value=e, tb=e.__traceback__)))

                # Ensure cache is updated with any new items
                #cache.globalCache.sync(globalCacheObjects)
            except SystemExit:
                logging.debug("Threaded process worker killed, workerID={0}".format(self.id))
                systemTrigger.failedTrigger(self.id,"triggerKilled")
            except Exception as e:
                logging.debug("Threaded worker crashed, workerID={0}".format(self.id))
                systemTrigger.failedTrigger(self.id,"triggerCrashed",''.join(traceback.format_exception(etype=type(e), value=e, tb=e.__traceback__)))
            finally:
                if p.exitcode == None:
                    p.terminate()
                #Q.close()

            logging.debug("Threaded process worker completed, workerID={0}".format(self.id))
            self.running = False
            self.endTime = int(time.time())
            self.duration = (self.endTime - self.startTime)

        def threadCall(self):
            self.startTime = int(time.time())
            self.running = True
            logging.debug("Threaded worker started, workerID={0}".format(self.id))
            # Handle thread raise exception kill
            try:
                if self.args:
                    self.result = self.call(*self.args)
                else:
                    self.result = self.call()
            except SystemExit:
                logging.debug("Threaded worker killed, workerID={0}".format(self.id))
                systemTrigger.failedTrigger(self.id,"triggerKilled")
            except Exception as e:
                logging.debug("Threaded worker crashed, workerID={0}".format(self.id))
                systemTrigger.failedTrigger(self.id,"triggerCrashed",''.join(traceback.format_exception(etype=type(e), value=e, tb=e.__traceback__)))
            logging.debug("Threaded worker completed, workerID={0}".format(self.id))
            self.running = False
            self.endTime = int(time.time())
            self.duration = (self.endTime - self.startTime)

    def __init__(self,concurrent=15,autoStart=True):
        self.concurrent = concurrent
        self.workerList = []
        self.stopped = False

        # Autostarting worker handler thread
        workerThread = self._worker("workerThread",self.handler,None,True,0,False)
        workerThread.start()
        self.workerList.append(workerThread)
        self.workerID = workerThread.id

    def handler(self):
        tick = 0
        loops = 0
        underConcurrent = self.concurrent # Used to limit list looping to find active workers
        workersStillWaiting = [] # Cache waiting workers to limit list looping to find waiting workers
        while not self.stopped:
            now = int(time.time())
            self.lastHandle = now

            # Any room to start another worker?
            if underConcurrent < 1:
                activeWorkerCount = len([ x for x in self.workerList if x.running == True ])
                underConcurrent = ( self.concurrent - activeWorkerCount )
            if underConcurrent > 0:
                if len(workersStillWaiting) == 0:
                    workersStillWaiting = [ x for x in self.workerList if x.running == None ]
                if len(workersStillWaiting) > 0:
                    # Check if number of workersWaiting is above the number of available concurrent threads and select mx available
                    workersWaiting = workersStillWaiting
                    if len(workersWaiting) > underConcurrent:
                        workersWaiting = workersWaiting[0:underConcurrent]
                    # Start all workers possible up to the concurrent limit
                    for workerWaiting in workersWaiting:
                        logging.debug("Starting threaded worker, workerID={0}".format(workerWaiting.id))
                        workerWaiting.start()
                        underConcurrent-=1
                        del workersStillWaiting[workersStillWaiting.index(workerWaiting)]

            # Execute worker cleanup every 5ish seconds
            if (tick + 5) < now:
                # Any workers need clearning up due to overrun or stopped?
                cleanupWorkers = [ x for x in self.workerList if (x.running == False and x.delete) or (x.startTime > 0 and x.maxDuration > 0 and (now - x.startTime ) > x.maxDuration) ]
                for worker in cleanupWorkers:
                    if worker.running != False:
                        worker.thread.kill()
                    del self.workerList[self.workerList.index(worker)]
                tick = now

            # CPU saver
            loops+=1
            if ((underConcurrent == 0) or (underConcurrent > 0 and len(workersStillWaiting) == 0)):
                loops = 0
                time.sleep(workerSettings["loopT1"])
            elif (loops > workerSettings["loopL"] and len(workersStillWaiting) != 0):
                loops = 0
                time.sleep(workerSettings["loopT"])

    def new(self, name, call, args=None, delete=True, maxDuration=60, multiprocessing=False):
        workerThread = self._worker(name, call, args, delete, maxDuration, multiprocessing)
        self.workerList.append(workerThread)
        logging.debug("Created new worker, workerID={0}".format(workerThread.id))
        return workerThread.id

    def get(self, id):
        worker = [x for x in self.workerList if x.id == id]
        if worker:
            worker = worker[0]
        logging.debug("Got data for worker, workerID={0}".format(id))
        return worker

    def getAll(self):
        result = []
        for worker in self.workerList:
            result.append(worker)
        return result

    def getActive(self):
        result = []
        workersRunning = [x for x in self.workerList if x.running == True]
        for worker in workersRunning:
            result.append(worker)
        return result

    def delete(self, id):
        worker = [x for x in self.workerList if x.id == id]
        if worker:
            worker = worker[0]
            logging.debug("Deleted worker, workerID={0}".format(id))
            del worker
        else:
            logging.debug("Unable to locate worker, workerID={0}".format(id))

    def kill(self, id):
        worker = [x for x in self.workerList if x.id == id]
        if worker:
            worker = worker[0]
            worker.thread.kill()
            logging.debug("Killed worker, workerID={0}".format(id))
        else:
            logging.debug("Unable to locate worker, workerID={0}".format(id))

    def wait(self, jid):
        worker = [x for x in self.workerList if x.id == id][0]
        logging.debug("Waiting for worker, workerID={0}".format(id))
        while (worker.running != False ):
            time.sleep(0.1)

    def waitAll(self):
        while (self.queue() > 0 or len(self.active()) > 0):
            time.sleep(0.5)

    def activeCount(self):
        workersRunning = [x for x in self.workerList if x.id != self.workerID and x.running == True]
        return len(workersRunning)

    def active(self):
        result = []
        workersRunning = [x for x in self.workerList if x.id != self.workerID and x.running == True]
        for workerRunning in workersRunning:
            result.append(workerRunning.name)
        return result

    def count(self):
        return len(self.workerList)

    def queue(self):
        workersWaiting = [x for x in self.workerList if x.running == None]
        return len(workersWaiting)

    def stop(self):
        self.stopped = True
        for runningJob in self.getActive():
            self.kill(runningJob.id)
        for job in self.getAll():
            self.delete(job.id)

    # API Calls
    def api_get(self,id=None,action=None):
        result = { "results" : []}
        if not id and not action:
            workers = self.getAll()
        elif id and not action:
            workers = [self.get(id)]
        elif not id and action == "active":
            workers = self.getActive()

        for worker in workers:
            if worker:
                result["results"].append({ "id" : worker.id, "nane": worker.name, "startTime" : worker.startTime, "createdTime" : worker.createdTime })

        return result

    def api_delete(self,id=None):
        if not id:
            workers = self.getAll()
        else:
            workers = [self.get(id)]

        for worker in workers:
            worker.thread.kill()

        return { "result" : True }

from core import api, logging, settings, model, cache
from system.models import trigger as systemTrigger

workerSettings = settings.config["workers"]

multiprocessing.set_start_method("spawn",force=True)

def start():
    global workers
    # Creating instance of workers
    try:
        if workers:
            workers.kill(workers.workerID)
            logging.debug("Workers start requested, Existing thread kill attempted, workerID='{0}'".format(workers.workerID),6)
            workers = None
    except NameError:
        pass
    workers = workerHandler(workerSettings["concurrent"])
    logging.debug("Workers started, workerID='{0}'".format(workers.workerID),6)
    return True

def multiprocessingThreadStart(Q,threadCall,args):
    #cache.globalCache.sync(globalCache)
    rc = 0
    error = None
    try:
        threadCall(*args)
    except Exception as e:
        error = e
        rc = 1
    Q.put((rc,error))

######### --------- API --------- #########
if api.webServer:
    if not api.webServer.got_first_request:
        @api.webServer.route(api.base+"workers/", methods=["GET"])
        def getWorkers():
            result = workers.api_get()
            if result["results"]:
                return result, 200
            else:
                return {}, 404

        @api.webServer.route(api.base+"workers/", methods=["DELETE"])
        def deleteWorkers():
            result = workers.api_delete()
            if result["result"]:
                return result, 200
            else:
                return {}, 404

        @api.webServer.route(api.base+"workers/", methods=["POST"])
        def updateWorkers():
            data = json.loads(api.request.data)
            if data["action"] == "start":
                result = start()
                return { "result" : result }, 200
            elif data["action"] == "settings":
                if "concurrent" in data:
                    workerSettings["concurrent"] = int(data["concurrent"])
                    workers.concurrent = workerSettings["concurrent"]
                if "loopT" in data:
                    workerSettings["loopT"] = float(data["loopT"])
                if "loopL" in data:
                    workerSettings["loopL"] = float(data["loopL"])
                return { }, 200
            else:
                return { }, 404

        @api.webServer.route(api.base+"workers/<workerID>/", methods=["GET"])
        def getWorker(workerID):
            if workerID == "0":
                result = workers.api_get(workers.workerID)
                result["results"][0]["lastHandle"] = workers.lastHandle
                result["results"][0]["workerID"] = workers.workerID
            else:
                result = workers.api_get(workerID)

            if result["results"]:
                return result, 200
            else:
                return {}, 404

        @api.webServer.route(api.base+"workers/<workerID>/", methods=["DELETE"])
        def deleteWorker(workerID):
            result = workers.api_delete(workerID)
            if result["result"]:
                return result, 200
            else:
                return {}, 404

        @api.webServer.route(api.base+"workers/stats/", methods=["GET"])
        def getWorkerStats():
            result = {}
            result["results"] = []
            result["results"].append({ "activeCount" : workers.activeCount(), "queueLength" : workers.queue(), "workers" : workers.active() })
            return result, 200

        @api.webServer.route(api.base+"workers/settings/", methods=["GET"])
        def getWorkerSettings():
            result = {}
            result["results"] = []
            result["results"].append(workerSettings)
            return result, 200


"""tools for ipython notebook"""

import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
import itertools
import numpy as np
from sklearn.model_selection import cross_val_score

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=cm.Blues,
                          figsize=(15,12)):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        #print("Normalized confusion matrix")
    else:
        pass
        #print('Confusion matrix, without normalization')

    #print(cm)
    plt.figure(figsize=figsize)
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title, fontsize=22)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.ylabel('True label', fontsize=22)
    plt.xlabel('Predicted label', fontsize=22)
    plt.tight_layout()

def plot_correlation_matrix(cm, attributes, title='Correlation Matrix', cmap='RdBu', figsize=(12,12),savename=None):
    fig=plt.figure(figsize=figsize)
    #plt.matshow(eucorr.as_matrix(), cmap=plt.cm.Blues)
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    plt.clim(-1,1)
    tick_marks = np.arange(len(attributes))
    plt.xticks(tick_marks, attributes, rotation=45)
    plt.yticks(tick_marks, attributes)
    fmt = 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], '.2f'),
                  horizontalalignment="center",
                  verticalalignment="center",
                  color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    if savename != None:
        fig.savefig(savename,bbox_inches='tight')

def gen_log_space(limit, n):
    """ generate integers in logaritmic space"""
    result = [1]
    if n>1:  # just a check to avoid ZeroDivisionError
        ratio = (float(limit)/result[-1]) ** (1.0/(n-len(result)))
    while len(result)<n:
        next_value = result[-1]*ratio
        if next_value - result[-1] >= 1:
            # safe zone. next_value will be a different integer
            result.append(next_value)
        else:
            # problem! same integer. we need to find next_value by artificially incrementing previous value
            result.append(result[-1]+1)
            # recalculate the ratio so that the remaining values will scale correctly
            ratio = (float(limit)/result[-1]) ** (1.0/(n-len(result)))
    # round, re-adjust to 0 indexing (i.e. minus 1) and return np.uint64 array
    return np.array(list(map(lambda x: round(x)-1, result)), dtype=int)

def cross_validate_model(model, x_train, y_train):
    m = cross_val_score(model, x_train, y_train)

    test_score, train_score = m[0], m[1]

    return (np.mean(test_score), np.std(test_score)),(np.mean(train_score), np.std(train_score))


name = input("What is your name?: ")
accepted = False

while accepted == False:

    if any(i.isdigit() for i in name) or name =="":
        print("Error in the name you entered. Try again.")
        name = input("What is your name?: ")
    else:
        accepted = True

print("Every second letter of your name is {}".format(name[::2]))


from __future__ import print_function
import pytest

from landlab.layers import MaterialLayers, MaterialLayersMixIn
from landlab import RasterModelGrid

def test_MaterialLayersMixIn():
    mg = RasterModelGrid(4,4)
    ml = mg.material_layers
    assert hasattr(mg, 'material_layers') == True
    assert ml.number_of_layers == 0
    assert ml.number_of_stacks == 4

def test_adding_untracked_layer():
    layers = MaterialLayers(3)
    layers.add(1., type=3., size='sand')
    layers.add([0., 0., 1.], type=3., size='sand')
    with pytest.raises(ValueError):
        layers.add([1.], type=3., size='sand', spam='eggs')


#!/usr/bin/env python
# coding=utf-8
from time import sleep
from test.common.aems_case import AeMSCase
from test.page.SmallCellMgtPage.file_mgt_page import FileManagementPage
from utils.common.utx import Tag, tag

driver = AeMSCase().driver
file_management_page = FileManagementPage(driver=driver)

class TestFileManagement(AeMSCase):

    def setUp(self):
        AeMSCase.setUp(self)
        file_management_page.act_open_tab("Small Cell Management", "File Management")
        if not AeMSCase.column_names:
            AeMSCase.column_names = file_management_page.get_column_names()

    @tag(Tag.HIGH)
    def test_upload(self):
        files = file_management_page.get_upload_files(self.version)
        # to upload all profile which format is .csv (end with csv)
        for file in files:
            if file.endswith(".csv"):
                file_management_page.act_upload_file(file)
                sleep(0.25)
                file_management_page.find_xpath_by_text('button', 'Confirm').click()
                file_management_page.get_alert_text_and_dismiss()
                sleep(0.25)

    def tearDown(self):
        AeMSCase.tearDown(self)


class Graph:
    def __init__(self, numVertices):
        self.V = numVertices
        self.vertices = [i for i in range(self.V)]
        self.edges = [set() for _ in range(self.V)]
        self.edgeList = {vertices : edges for (vertices, edges) in zip(self.vertices, self.edges)}

    def addEdges(self, src, dest):
        if src > self.V - 1 or dest > self.V or src < 0 or dest < 0:
            print("Invalid Sources and Destination")
            return
        if src != dest:
            self.edgeList.get(src).add(dest)
            self.edgeList.get(dest).add(src)
        else:
            print("Source and Destination can't be same")

    def removeEdges(self, src, dest):
        if src > self.V - 1 or dest > self.V or src < 0 or dest < 0:
            print("Invalid Sources and Destination")
            return
        if src != dest:
            self.edgeList.get(src).remove(dest)
            self.edgeList.get(dest).remove(src)
        else:
            print("Source and Destination can't be same")

    def displayGraph(self):
        print(self.edgeList)

def main():
    print("Enter 1 to create a graph")
    print("Enter 2 to exit")
    flag = int(input("Enter your Choice\n"))
    if flag > 2 or flag < 0:
        print("Invalid Choice")
        return
    elif flag == 2:
        return
    else:
        N = int(input("Enter the no. of vertices in the graph"))
        G = Graph(N)
        while True:
            print("Enter 1 for adding an edge")
            print("Enter 2 for removing an edge")
            print("Enter 3 for dislpaying the graph")
            print("Enter 4 for exit")
            flag = int(input("Enter your Choice"))
            if flag < 0 or flag > 4:
                print("Invalid Input")
                return
            elif flag == 1:
                G.addEdges(int(input("Enter the source node. ")), int(input("Enter the destination node. ")))
            elif flag == 2:
                G.removeEdges(int(input("Enter the source node. ")), int(input("Enter the destination node. ")))
            elif flag == 3:
                G.displayGraph()
            else:
                return

main()


from odoo import api, fields, models, tools, _
from odoo.modules import get_module_resource
from odoo.exceptions import ValidationError
from odoo.exceptions import UserError, AccessError
import logging

class Liquidacion(models.Model):
    _name = "account_gt.liquidacion"
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _description = 'Liquidacion'
    _order = 'id desc'

    name = fields.Char('Nombre', required=True, copy=False, readonly=True, index=True, default=lambda self: _('New'))
    descripcion = fields.Char('Descripcion',tracking=True)
    fecha = fields.Date('Fecha',tracking=True)
    factura_ids = fields.One2many('account_gt.liquidacion_factura','liquidacion_id','Facturas')
    pago_ids = fields.One2many('account_gt.liquidacion_pago','liquidacion_id','Pagos')
    company_id = fields.Many2one('res.company', 'Company', required=True, default=lambda self: self.env.company)
    currency_id = fields.Many2one('res.currency','Moneda',default=lambda self: self.env.company.currency_id.id)
    diario_id = fields.Many2one('account.journal', 'Diario', required=True,tracking=True)
    cuenta_id = fields.Many2one('account.account', 'Cuenta de desajuste',tracking=True)
    move_id = fields.Many2one('account.move', 'Movimiento',tracking=True)
    state = fields.Selection([
        ('borrador', 'Borrador'),
        ('conciliado', 'Conciliado'),
        ('cancelado', 'Cancelado'),], string='Estado', readonly=True, copy=False, index=True, tracking=3, default='borrador')

    @api.model
    def create(self, vals):
        if vals.get('name', _('New')) == _('New'):
            seq_date = None
            if 'company_id' in vals:
                vals['name'] = self.env['ir.sequence'].with_context(force_company=vals['company_id']).next_by_code(
                    'account_gt.liquidacion', sequence_date=seq_date) or _('New')
            else:
                vals['name'] = self.env['ir.sequence'].next_by_code('account_gt.liquidacion', sequence_date=seq_date) or _('New')

        result = super(Liquidacion, self).create(vals)
        return result

    def conciliar_liquidacion(self):
        move = False
        moneda_factura = False
        moneda_pago= False
        for dato in self:
            lineas = []

            total = 0
            if dato.factura_ids:
                moneda_factura = dato.factura_ids[0].currency_id
                for linea in dato.factura_ids:
                    # logging.warn(f.number)
                    # logging.warn(f.amount_total)
                    for l in linea.factura_id.line_ids:
                        if l.account_id.reconcile:
                            if not l.reconciled:
                                total += l.credit - l.debit
                                lineas.append(l)
                                logging.warn(l.credit - l.debit)
                            else:
                                raise UserError('La factura %s ya esta conciliada' % (linea.factura_id.name))
            logging.warn('PASA FACTURA')
            logging.warn(lineas)

            if dato.pago_ids:
                moneda_pago = dato.pago_ids[0].currency_id
                for linea in dato.pago_ids:
                    # logging.warn(c.name)
                    # logging.warn(c.amount)
                    for l in linea.pago_id.move_line_ids:
                        if l.account_id.reconcile:
                            if not l.reconciled :
                                total -= l.debit - l.credit
                                lineas.append(l)
                                logging.warn(l.debit - l.credit)
                            else:
                                raise UserError('El Pago %s ya esta conciliado' % (linea.pago_id.name))

            logging.warn('PASA PAGO')
            logging.warn(lineas)

            if (moneda_pago.name=="GTQ" and moneda_factura.name=="GTQ") and moneda_factura.id == moneda_pago.id and round(total) != 0:
                logging.warn('TOTAL')
                logging.warn(total)
                break

            lineas_conciliares = []
            nuevas_lineas = []
            for linea in lineas:
                nuevas_lineas.append((0, 0, {
                    'name': linea.name,
                    'debit': linea.credit,
                    'credit': linea.debit,
                    'account_id': linea.account_id.id,
                    'partner_id': linea.partner_id.id,
                    'journal_id': dato.diario_id.id,
                    'date_maturity': dato.fecha,
                }))

            if total != 0 and moneda_factura.id != moneda_pago.id:
                nuevas_lineas.append((0, 0, {
                    'name': 'Diferencia de ' + dato.name,
                    'debit': -1 * total if total < 0 else 0,
                    'credit': total if total > 0 else 0,
                    'account_id': dato.cuenta_id.id,
                    'date_maturity': dato.fecha,
                }))

            if total != 0 and moneda_factura.name== 'USD' and moneda_pago.name=='USD':
                logging.warn('DOLAR')
                nuevas_lineas.append((0, 0, {
                    'name': 'Diferencia de ' + dato.name,
                    'debit': -1 * total if total < 0 else 0,
                    'credit': total if total > 0 else 0,
                    'account_id': dato.cuenta_id.id,
                    'date_maturity': dato.fecha,
                }))

            move = self.env['account.move'].create({
                'line_ids': nuevas_lineas,
                'ref': dato.name,
                'date': dato.fecha,
                'journal_id': dato.diario_id.id,
            });
            #
            indice = 0
            for linea in lineas:
                lineas_conciliar = linea | move.line_ids[indice]
                lineas_conciliar.reconcile()
                indice += 1

            move.post()
            self.write({'move_id': move.id})

        if move:
            for linea in dato.factura_ids:
                linea.factura_id.write({'liquidacion_id': dato.id})
            for linea in dato.pago_ids:
                linea.pago_id.write({'liquidacion_id': dato.id})
            self.write({'state': 'conciliado'})

        return True

    def cancelar_liquidacion(self):
        for dato in self:
            for l in dato.move_id.line_ids:
                if l.reconciled:
                    l.remove_move_reconcile()
            dato.move_id.button_draft()
            dato.move_id.button_cancel()
            # dato.move_id.unlink()

            if dato.move_id:
                for linea in dato.factura_ids:
                    linea.factura_id.write({'liquidacion_id': False})
                for linea in dato.pago_ids:
                    linea.pago_id.write({'liquidacion_id': False})
                self.write({'state': 'cancelado'})

        return True

    def cambiar_borrador(self):
        self.write({'state': 'borrador'})
        return True

class LiquidacionFactura(models.Model):
    _name = "account_gt.liquidacion_factura"
    _rec_name = "liquidacion_id"

    liquidacion_id = fields.Many2one('account_gt.liquidacion','Liquidacion')
    factura_id = fields.Many2one('account.move','Factura')
    currency_id = fields.Many2one('res.currency',string='moneda', related='factura_id.currency_id')
    total = fields.Monetary('Total',related='factura_id.amount_total')

class LiquidacionPago(models.Model):
    _name = "account_gt.liquidacion_pago"
    _rec_name = "liquidacion_id"

    liquidacion_id = fields.Many2one('account_gt.liquidacion','Liquidacion')
    pago_id = fields.Many2one('account.payment','Pago')
    currency_id = fields.Many2one('res.currency',string='moneda', related='pago_id.currency_id')
    total = fields.Monetary('Total',related='pago_id.amount')


import re
from os.path import join

import click
import untangle
import pypandoc
from slugify import slugify

@click.command()
@click.argument('xmlfile', type=click.File('rb'))
@click.option('--output-dir', type=click.Path(exists=True,
              dir_okay=True, file_okay=False))
def main(xmlfile, output_dir):
  obj = untangle.parse(xmlfile)
  for page in obj.mediawiki.page:
    title = page.title.cdata
    filename = slugify(title) + '.md'
    permalink = '/' + wikimedia_slugify(title)
    author = page.revision.contributor.username.cdata
    date = page.revision.timestamp.cdata
    body = page.revision.text.cdata
    body_without_category = re.sub(r"\[\[Category:[^\]]+\]\]", '', body)
    category_match = re.search(r"\[\[Category:([^\]]+)\]\]", body)
    category = '' if category_match is None else category_match[1]

    try:
      body_md = pypandoc.convert_text(body_without_category, 'gfm',
                 format='mediawiki')

      contents = """---
title: {}
permalink: {}
layout: page
author: {}
date: {}
category: {}
---
{}
""".format(title, permalink, author, date, category, body_md)

      click.echo(title)

      filepath = join(output_dir, filename)
      with open(filepath, 'w') as file:
        file.write(contents)

    except Exception as e:
      click.echo(e, err=True)

def wikimedia_slugify(input):
  return input.replace(' ', '_')

if __name__ == '__main__':
  main()


import os
from hydra.experimental import compose, initialize
initialize(config_path="./conf")
import util
util.config._config = compose("config.yaml")

import torch
from torchvision.utils import save_image
from evolution.generator import Generator, GeneratorDataset
from tqdm import tqdm
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

def store_from_dataset(dataset, path, batch_size=50):
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=False, drop_last=False)
    i = 0
    for batch, _ in tqdm(dataloader):
        for image in batch:
            save_image((image + 1) / 2, os.path.join(path, f"image_{i}.png"))
            i += 1

def main(samples=100, output="output"):
    path = os.path.join(os.path.dirname(__file__), "generator.pkl")
    generator = Generator.load(path)
    generator_dataset = GeneratorDataset(generator, size=samples)
    os.makedirs(output, exist_ok=True)
    store_from_dataset(generator_dataset, output)

if __name__ == "__main__":
    parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument("-o", "--output", type=str, default="output", help='Output Path')
    parser.add_argument("-s", "--samples", default=100, type=int, help='Number of samples to generate')
    args = parser.parse_args()
    print(args)
    main(samples=args.samples, output=args.output)


from django.shortcuts import render
from blog.models import BlogPost,Blogger,Category

def index(request):
    blogposts = BlogPost.objects.order_by('-blog_date')
    featured_blogposts = BlogPost.objects.order_by('-blog_date')
    categories = Category.objects.all()
    context = {
        'blogposts':blogposts,
        'featured_blogposts':featured_blogposts,
        'categories':categories
    }
    return render(request,'main/index.html',context)

def about(request):
    return render(request,'main/about.html')

def search(request):
    query = request.GET['query']
    queryset_title = BlogPost.objects.order_by('-blog_date').filter(title__icontains=query)
    context = {
        'blogposts':queryset_title,
    }

    return render(request,'main/search.html',context)


"""All constants in project"""

# consts for work
ADMIN_NICKNAME = 'mezgoodle'
NO_ASSOCIATION = 'NONE'
LABEL_NAME = 'needs review'
BOT_NAME = 'mezgoodle-bot'
# consts for tests
TEST_REF = 'backport-17ab8f0-3.7'
TEST_REPO_NAME = 'some_name'
TEST_NUMBER = 5772
ISSUE_URL = 'https://api.github.com/issue/123'
TEST_DATA_PR = {
    'action': 'opened',
    'pull_request': {
        'number': TEST_NUMBER,
        'author_association': 'owner',
        'issue_url': ISSUE_URL,
        'state': 'closed',
        'labels': 'labels'
    },
    'sender': {'login': ADMIN_NICKNAME}
}
TEST_DATA_PR_1 = {
    'action': 'closed',
    'pull_request': {
        'number': TEST_NUMBER,
        'user': {'login': BOT_NAME},
        'merged': True,
        'merged_by': {'login': ADMIN_NICKNAME},
        'head': {
            'ref': TEST_REF, 'user': {'login': ADMIN_NICKNAME},
            'repo': {'name': TEST_REPO_NAME}
        },
        'issue_url': ISSUE_URL,
        'state': 'closed',
    },
}
TEST_DATA_PR_2 = {
    'action': 'closed',
    'pull_request': {
        'number': TEST_NUMBER,
        'user': {'login': BOT_NAME},
        'merged': False,
        'merged_by': {'login': None},
        'head': {
            'ref': TEST_REF, 'user': {'login': ADMIN_NICKNAME},
            'repo': {'name': TEST_REPO_NAME}
        },
        'issue_url': ISSUE_URL,
        'state': 'closed',
    },
}
TEST_DATA_ISSUES = {
    'action': 'created',
    'comment': {
        'url': ISSUE_URL,
    },
    'sender': {'login': ADMIN_NICKNAME}
}
TEST_DATA_INSTALLATION = {
    'action': 'created',
    'repositories': [{'full_name': BOT_NAME}],
    'sender': {'login': ADMIN_NICKNAME}
}
HEADERS = {'x-github-event': 'ping', 'x-github-delivery': '1234'}


# encoding: utf-8
"""
@version: ??
@author: chenyitao
@license: Apache Licence
@software: PyCharm
@file: kline_protocol.py
@time: 2018/4/16 10:23
"""
import logging
from string import lower

from .binance_protocol_base import BinanceWSProtocolBase

log = logging.getLogger(__name__)

class BinanceWSKLineProtocol(BinanceWSProtocolBase):

    platform = 'binance'

    feature = 'binance.kline'

    def start(self):
        log.info('Binance KLine Started...')

    def message(self, data):
        if data.get('data'):
            self.update_market(data)

    def update_market(self, data):
        data = data.get('data')
        tick = data.get('k')
        date = data.get('E')
        currency_pair = lower(data.get('s'))
        index = self.req_currency_pairs.index(currency_pair)
        currency_pair = self.src_currency_pairs[index]
        market_info = {'platform': self.platform,
                       'currencyPair': currency_pair,
                       'high': tick.get('h'),
                       'low': tick.get('l'),
                       'open': tick.get('o'),
                       'last': tick.get('c'),
                       'volume': tick.get('v'),
                       'date': date}
        self.storage(market_info)


import json
import requests
from time import sleep

# Constants

API_KEY          = "ThingsBoard API Key"
THINGSBOARD_HOST = "Linode Public IP Address"

thingsboard_url  = "http://{0}/api/v1/{1}/telemetry".format(THINGSBOARD_HOST, API_KEY)

sense = SenseHat()

data = {}

while True:
    data['temperature'] = 25
    data['pressure']    = 65
    data['humidity']    = 96030

    #r = requests.post(thingsboard_url, data=json.dumps(data))
    print(str(data))
    sleep(5)


# Insert items into a list in sorted order
# https://app.commit.live/lesson/fsdse-python-assignment-58

# Define a function that takes two parameters: a list of numbers, a number
#The function should first sort the list and then insert the number into the list
#so that the list still remains sorted. Function should return the updated list.

a = [1, 10, 2, 20, 3, 30, 4, 40]

def solution(li_of_num, num):
    li_of_num.append(num)
    li_of_num.sort()
    return li_of_num

numb = 99

solution(a, numb)

#DOne, Not Posted =================================================================================


#!/usr/bin/python
#coding:utf8

__author__ = ['fuyadong']

from setuptools import setup, find_packages

setup(
    name='mymoduel_fuyd',
    version="1.0.0",
    url='http://www.osforce.cn',
    license='MIT',
    author='fuyadong',
    author_email='fuyadong2011@163.com',
    description='a distributed test framework',
    classifiers=[
        "Programming Language :: Python",
    ],
    platforms='any',
    keywords='framework nose testing',
    packages=find_packages(exclude=['test']),
    install_requires=[]
)


import time

class MemberObj:
    def __init__(self, name):

        # users name
        self.name = name

        # users total time spent in voice channel
        self.time = 0

        # users total time spent i respective channel
        self.channeltime = {}

        # boolean representing if they currently are connected to
        # a channel
        self.current_channel = None

        self.connect_time = 0

        print('member', self.name, 'initialized')

    def state_change(self, channel):
        if self.current_channel is not channel:
            if self.current_channel is not None:
                self.time = self.time + (int(time.time()) - self.connect_time)
                if channel in self.channeltime:
                    self.channeltime[channel] = self.channeltime[channel] + (int(round(time.time())) - self.connect_time)
                else:
                    self.channeltime[channel] = (int(time.time()) - self.connect_time)

            self.connect_time = int(time.time())
            self.current_channel = channel

    def get_active_time(self):
        return self.time

    def get_active_time_channel(self, channel):
        return self.channeltime.get(channel)


import threading
from importlib import import_module
from typing import Type, Dict, Any

from dbt.exceptions import RuntimeException
from dbt.include.global_project import PACKAGES
from dbt.logger import GLOBAL_LOGGER as logger
from dbt.contracts.connection import Credentials, AdapterRequiredConfig

from dbt.adapters.base.impl import BaseAdapter
from dbt.adapters.base.plugin import AdapterPlugin

# TODO: we can't import these because they cause an import cycle.
# Profile has to call into load_plugin to get credentials, so adapter/relation
# don't work
BaseRelation = Any

Adapter = BaseAdapter

class AdpaterContainer:
    def __init__(self):
        self.lock = threading.Lock()
        self.adapters: Dict[str, Adapter] = {}
        self.adapter_types: Dict[str, Type[Adapter]] = {}

    def get_adapter_class_by_name(self, name: str) -> Type[Adapter]:
        with self.lock:
            if name in self.adapter_types:
                return self.adapter_types[name]

            names = ", ".join(self.adapter_types.keys())

        message = f"Invalid adapter type {name}! Must be one of {names}"
        raise RuntimeException(message)

    def get_relation_class_by_name(self, name: str) -> Type[BaseRelation]:
        adapter = self.get_adapter_class_by_name(name)
        return adapter.Relation

    def load_plugin(self, name: str) -> Type[Credentials]:
        # this doesn't need a lock: in the worst case we'll overwrite PACKAGES
        # and adapter_type entries with the same value, as they're all
        # singletons
        try:
            mod = import_module('.' + name, 'dbt.adapters')
        except ImportError as e:
            logger.info("Error importing adapter: {}".format(e))
            raise RuntimeException(
                "Could not find adapter type {}!".format(name)
            )
        if not hasattr(mod, 'Plugin'):
            raise RuntimeException(
                f'Could not find plugin in {name} plugin module'
            )
        plugin: AdapterPlugin = mod.Plugin  # type: ignore
        plugin_type = plugin.adapter.type()

        if plugin_type != name:
            raise RuntimeException(
                f'Expected to find adapter with type named {name}, got '
                f'adapter with type {plugin_type}'
            )

        with self.lock:
            # things do hold the lock to iterate over it so we need it to add
            self.adapter_types[name] = plugin.adapter

        PACKAGES[plugin.project_name] = plugin.include_path

        for dep in plugin.dependencies:
            self.load_plugin(dep)

        return plugin.credentials

    def register_adapter(self, config: AdapterRequiredConfig) -> None:
        adapter_name = config.credentials.type
        adapter_type = self.get_adapter_class_by_name(adapter_name)

        with self.lock:
            if adapter_name in self.adapters:
                # this shouldn't really happen...
                return

            adapter: Adapter = adapter_type(config)  # type: ignore
            self.adapters[adapter_name] = adapter

    def lookup_adapter(self, adapter_name: str) -> Adapter:
        return self.adapters[adapter_name]

    def reset_adapters(self):
        """Clear the adapters. This is useful for tests, which change configs.
        """
        with self.lock:
            for adapter in self.adapters.values():
                adapter.cleanup_connections()
            self.adapters.clear()

    def cleanup_connections(self):
        """Only clean up the adapter connections list without resetting the
        actual adapters.
        """
        with self.lock:
            for adapter in self.adapters.values():
                adapter.cleanup_connections()

FACTORY: AdpaterContainer = AdpaterContainer()

def register_adapter(config: AdapterRequiredConfig) -> None:
    FACTORY.register_adapter(config)

def get_adapter(config: AdapterRequiredConfig):
    return FACTORY.lookup_adapter(config.credentials.type)

def reset_adapters():
    """Clear the adapters. This is useful for tests, which change configs.
    """
    FACTORY.reset_adapters()

def cleanup_connections():
    """Only clean up the adapter connections list without resetting the actual
    adapters.
    """
    FACTORY.cleanup_connections()

def get_adapter_class_by_name(name: str) -> Type[BaseAdapter]:
    return FACTORY.get_adapter_class_by_name(name)

def get_relation_class_by_name(name: str) -> Type[BaseRelation]:
    return FACTORY.get_relation_class_by_name(name)

def load_plugin(name: str) -> Type[Credentials]:
    return FACTORY.load_plugin(name)


import threading
import socket
import time
phone = socket.socket()
port = input("What port do you want to connect to?")
addr = (socket.gethostname(), int(port))
phone.bind(addr)
phone.listen(16)
def server():
	while True:
		try:
			conn, callid = phone.accept()
			message = bytes.decode(conn.recv(1024))
			conn.send("r".encode("UTF-8"))
			conn.close()
			print("Call from {0}: {1}".format(callid, message))
		except KeyboardInterrupt:
			conn.close()
			phone.close()
			break
		except:
			conn.close()
			phone.close()
			break
def client():
	cpu = input("What computer do you want to connect to? 2-17\n")
	alias = input("What do you want your alias to be?")
	while True:
		try:
			msg = (alias + ": " + input("What is the message that you want to send?\n"))
			data = msg.encode("UTF-8")
			phone = socket.socket()
			address = ("vampy-cs-" + str(cpu), int(port))
			phone.connect(address)
			phone.sendall(data)
			phone.close()
			time.sleep(0.5)
		except ConnectionRefusedError:
			print("yo u cant connect")
		except KeyboardInterrupt:
			phone.close()
			break
t1 = threading.Thread(target = server)
t2 = threading.Thread(target = client)
t1.start()
t2.start()
t1.join()
t2.join()
print("Shutting down.")


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

from odoo import models, api
from odoo.tools import float_is_zero

class PosReconciliation(models.Model):
    _inherit = "pos.order"

    @api.model
    def _process_order(self, pos_order):
        prec_acc = self.env['decimal.precision'].precision_get('Account')
        pos_session = self.env['pos.session'].browse(pos_order['pos_session_id'])
        if pos_session.state == 'closing_control' or pos_session.state == 'closed':
            pos_order['pos_session_id'] = self._get_valid_session(pos_order).id
        order = self.create(self._order_fields(pos_order))
        journal_ids = set()
        payment_sum = 0
        for payments in pos_order['statement_ids']:
            if not float_is_zero(payments[2]['amount'], precision_digits=prec_acc):
                payment_sum = payment_sum + payments[2]['amount']
                if payment_sum > pos_order['amount_total']:
                    payments[2]['amount'] = payments[2]['amount'] - pos_order['amount_return']
                order.add_payment(self._payment_fields(payments[2]))
            journal_ids.add(payments[2]['journal_id'])
        if pos_session.sequence_number <= pos_order['sequence_number']:
            pos_session.write({'sequence_number': pos_order['sequence_number'] + 1})
            pos_session.refresh()
        return order


import os
import pandas as pd
import numpy as np

csv_directory = r'C:\Users\aarus\Documents\STEM2SHTEM\CSV data\toy_setting_k31.csv'
all_data = pd.read_csv(csv_directory, low_memory=False)
print("read csv")

listOfDFRows = all_data.to_numpy().tolist()
print("converted to numpy")

cols = ["File Name"]

for row in listOfDFRows:
    cols.append(row[1])
    del row[0:2]

print(len(cols))

used = 0
data = []

for i in range(len(listOfDFRows)):
    print(used)
    my_row = []
    my_row.append(cols[i + 1])
    curr_row = listOfDFRows[i]
    for u in range(used):
        my_row.append('')
    for j in range(i, len(listOfDFRows)): #previously i + 1
        if i == j:
            my_row.append(0)
            continue
        resultant = []
        next_row = listOfDFRows[j]
        for ind in range(len(curr_row)):
            resultant.append(abs(curr_row[ind] - next_row[ind]))
        my_row.append(sum(resultant))
        print(sum(resultant))
    data.append(my_row)
    used += 1
    print(len(my_row))

df = pd.DataFrame(data=data, columns=cols)
#df.to_csv("kmer_diff_toy_setting_k31.csv")


import argparse
import sys
from typing import Dict, List, Tuple

from OpenSSL import SSL, crypto
from OpenSSL.crypto import TYPE_DH, TYPE_DSA, TYPE_EC, TYPE_RSA

class Cert:
    """
    A class representing a certificate. It would be equivalent to the following command:
        $openssl req -x509 -out localhost.crt -keyout localhost.key \
            -newkey rsa:2048 -nodes -sha256 \
            -subj '/CN=localhost' -extensions EXT -config <( \
            printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
    """
    type_map: Dict = {
        "RSA": TYPE_RSA,
        "DSA": TYPE_DSA,
        "DH": TYPE_DH,
        "EC": TYPE_EC
    }
    def __init__(
        self,
        cn: str = "localhost",
        c: str = None,
        l: str = None,
        st: str = None,
        o: str = None,
        ou: str = None,
        emailAddress: str = None,
        serialNumber: str = 0,
        validityStartInSeconds: str = 0,
        validityEndInSeconds: str = 10*365*24*60*60,
        distinguishedName: str = "localhost",
        subjectAltName: str = "DNS:localhost",
        keyUsage: str = "digitalSignature",
        extendedKeyUsage: str = "serverAuth",
        cryptoType: str = "RSA",
        bits: str = 4096,
        digest: str = "sha512",
        **kwargs
    ) -> None:

        # create a key pair
        self.key = crypto.PKey()
        self.key.generate_key(self.type_map.get(cryptoType, TYPE_RSA), bits)
        # create the certificate
        self.cert = crypto.X509()
        subj = self.cert.get_subject()
        subj.CN = cn
        if c is not None:
            subj.C = c
        if st is not None:
            subj.ST = st
        if l is not None:
            subj.L = l
        if o is not None:
            subj.O = o
        if ou is not None:
            subj.OU = ou
        if emailAddress is not None:
            subj.emailAddress = emailAddress
        extensions = [
            crypto.X509Extension(b"subjectAltName", False, subjectAltName.encode()),
            crypto.X509Extension(b"keyUsage", False, keyUsage.encode()),
            crypto.X509Extension(b"extendedKeyUsage", False, extendedKeyUsage.encode())
        ]
        self.cert.add_extensions(extensions)
        self.cert.set_serial_number(serialNumber)
        self.cert.gmtime_adj_notBefore(0)
        self.cert.gmtime_adj_notAfter(validityEndInSeconds)
        self.cert.set_issuer(subj)
        self.cert.set_pubkey(self.key)
        self.cert.sign(self.key, digest)

    def save(
        self,
        key_file: str,
        cert_file: str
    ) -> None:
        """
        Method to save the certificate and key.
        Can be checked afterwards with:
            $openssl x509 -in selfsigned.crt -text
        """
        with open(cert_file, "w") as f:
            f.write(crypto.dump_certificate(crypto.FILETYPE_PEM, self.cert).decode("utf-8"))
        with open(key_file, "w") as f:
            f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, self.key).decode("utf-8"))

class ArgParser(argparse.ArgumentParser):
    def __init__(self):
        super().__init__()
        self.add_argument("-cn", type=str, help="CN (Common Name)", default=argparse.SUPPRESS)
        self.add_argument("-c", type=str, help="C (Country Name)", default=argparse.SUPPRESS)
        self.add_argument("-l", type=str, help="L (Locality)", default=argparse.SUPPRESS)
        self.add_argument("-st", type=str, help="ST (State or Province)", default=argparse.SUPPRESS)
        self.add_argument("-o", type=str, help="O (Organization)", default=argparse.SUPPRESS)
        self.add_argument("-ou", type=str, help="OU (Organization Unit)", default=argparse.SUPPRESS)
        self.add_argument("-emailAddress", "-e", type=str, help="emailAddres", default=argparse.SUPPRESS)
        self.add_argument("-serialNumber", "-sn", type=int, help="serialNumber", default=argparse.SUPPRESS)
        self.add_argument("-validityStartInSeconds", "-vs", type=int, help="validityStart in seconds", default=argparse.SUPPRESS)
        self.add_argument("-validityEndInSeconds", "-ve", type=int, help="validityEnd in seconds", default=argparse.SUPPRESS)
        self.add_argument("-distinguishedName", "-dn", type=str, help="DN (Distinguished Name)", default=argparse.SUPPRESS)
        self.add_argument("-subjectAltName", "-san", type=str, help="SAN (Subject Alt Name)", default=argparse.SUPPRESS)
        self.add_argument("-keyUsage", "-ku", type=str, help="KU (Key Usage)", default=argparse.SUPPRESS)
        self.add_argument("-extendedKeyUsage", "-eku", type=str, help="EKU (Extended Key Usage)", default=argparse.SUPPRESS)
        self.add_argument("-cryptoType", "-t",type=str, help="Crypto Type", default=argparse.SUPPRESS)
        self.add_argument("-bits", "-b", type=int, help="Crypto number of bits", default=argparse.SUPPRESS)
        self.add_argument("-digest", "-dig", type=str, help="Digest", default=argparse.SUPPRESS)
        self.add_argument("-key", "-key-file", type=str, help="Key filename to be created", default="private.key")
        self.add_argument("-cert", "-cert-file", type=str, help="Cert filename to be creted", default="selfsigned.crt")

    def parse_args(self) -> argparse.Namespace:
        if len(sys.argv)==1:
            self.print_help(sys.stderr)
            sys.exit(1)
        return super().parse_args()

if __name__ == "__main__":
    parser = ArgParser()
    args = parser.parse_args()
    args_dict = vars(args)
    print("Create certificate with the following arguments:")
    for k, v in args_dict.items():
        print(f"{k} = {v}")
    cert = Cert(**args_dict)
    cert.save(args.key, args.cert)


'''
MAIN
everything that is needed to train something.  includes flags for models, datasets, learning rates and more.
'''

import torch
from DataWrapper import *
from torch.utils.data import DataLoader, SubsetRandomSampler
import random
import numpy as np
from architectures import *
import torch.nn as nn
import torch.optim
from plotter_helper import *
from tensorboardX import SummaryWriter
import sys
from skimage import io
import json
import argparse
import time
from evaluation import *

from sklearn.metrics import f1_score
from losses import *
import utils
import os
import constants
import datetime
from tqdm import trange, tqdm

# Parse training parameters
args = utils.parse_training_params().parse_args()

# Fix for slow convergence https://github.com/pytorch/pytorch/issues/15054
if args.dilate_first or args.dilate_second:
    torch.backends.cudnn.benchmark = True

# Fix seed for reproducible results
utils.fix_seed()

# Create run_dir the first time
if not os.path.isdir(constants.run_dir):
    os.mkdir(constants.run_dir)

# Here everything is saved
timestamp = datetime.datetime.now().strftime("%b_%d_%H%M%S")
LOG_NAME = args.log_dir + "_" + timestamp
save_dir = os.path.join(constants.run_dir, LOG_NAME)
os.mkdir(save_dir)
model_dir = os.path.join(save_dir, 'model')
os.mkdir(model_dir)
results_dir = os.path.join(save_dir, 'results')
os.mkdir(results_dir)
print(f"Saving to {save_dir}")

os.mkdir(os.path.join(results_dir, "prediction"))
os.mkdir(os.path.join(results_dir, "overlay"))
inter_dir = os.path.join(save_dir, "inter")
os.mkdir(inter_dir)

log_dir = os.path.join('logdir', LOG_NAME)
writer = SummaryWriter(log_dir)
print(f"Writing log files to {log_dir}")

# Initialize JSON Saver
json_saver = {'train_loss': dict(),  # Total batch training loss every save_iter_freq
              'train_f1': dict(),  # Batch F1 score every save_iter_freq
              'train_accuracy': dict(),  # Batch accuracy every save_iter_freq
              'train_timestamps': dict(),  # Timestamps ever save_iter_freq
              'val_loss': dict(),  # Total loss every val_iter_freq averaged over val_count many samples
              'val_f1': dict(),  # F1 score every val_iter_freq averaged over val_count many samples
              'val_accuracy': dict(),  # Accuracy every val_iter_freq averaged over val_count many samples
              'val_timestamps': dict(),  # Timestamps every val_iter_freq
              'start_time': timestamp,
              'name': args.log_dir,
              'dataset': args.dataset,
              'number_epochs': args.nr_epochs,
              'optimizer': args.optimizer,
              'learning_rate': args.learning_rate,
              'train_set': args.dataset,
              'batch_size': args.batch_size}
with open(save_dir + '/data.json', 'w') as fp:
    json.dump(json_saver, fp, indent=2)

if args.dataset is 0:
    data_save_dir = os.path.join(constants.data_dir, "static")
    train_dir = os.path.join(data_save_dir, "split_train")
    val_dir = os.path.join(constants.data_dir+'/static/', "split_validate")
    if not os.path.isdir(data_save_dir):
        print("no static data directory found. Run create_dataset.py first.")
        exit(1)
else:
    if args.dataset is 1:
        data_save_dir = os.path.join(constants.data_dir, "original")
        train_dir = os.path.join(data_save_dir, "split_train")
        val_dir = os.path.join(constants.data_dir + '/static/', "split_validate")
    elif args.dataset is 2:
        data_save_dir = os.path.join(constants.data_dir, "augmented")
        train_dir = os.path.join(data_save_dir, "split_train")
        val_dir = os.path.join(constants.data_dir + '/static/', "split_validate")

    # elif args.dataset is 3:
    #     create_data(train_set=['data/chicago', 'data/original/split_train'], train_set_amount=[1000, 1000],
    #                 eval_set=['data/original/split_validation'], eval_set_amount=[500],
    #                 augmentations=['rotate', 'flip', 'both'],
    #                 fraction_augmented_train=0.9, fraction_augmented_validation=0.5,
    #                 patched=True,
    #                 train_dir=train_dir, val_dir=val_dir)
    #
    # elif args.dataset is 4:
    #     create_data(train_set=['data/scaled'], train_set_amount=[1000],
    #                 eval_set=['data/scaled'], eval_set_amount=[200],
    #                 augmentations=[],
    #                 fraction_augmented_train=0.5, fraction_augmented_validation=0.5,
    #                 train_dir=train_dir, val_dir=val_dir)
    else:
        print("Not a training set")
        exit(1)

print(train_dir)
train_data = DataWrapper(os.path.join(train_dir, "input/"), os.path.join(train_dir, "target/"),
                         torch.cuda.is_available(), args.model)
val_data = DataWrapper(os.path.join(val_dir, "input/"), os.path.join(val_dir, "target/"),
                       torch.cuda.is_available(), args.model)

nr_training_samples = train_data.__len__()
nr_validation_samples = val_data.__len__()
nr_training_iterations = nr_training_samples // args.batch_size

# Size of validation images
VAL_IMAGE_SIZE = val_data.image_size()
BATCH_VAL_IMAGE_SIZE = (args.batch_size, 1, 400, 400)
if args.model == 18:
    BATCH_VAL_IMAGE_SIZE = (args.batch_size, 1, 608, 608)

# Select model based on arguments
intermediate_supervision, model = select_model(modelNames[args.model], args)

# Transfer model to GPU if possible
if torch.cuda.is_available():
    torch.cuda.empty_cache()
    model.cuda()
else:
    print("CUDA unavailable, using CPU!")

# Declare loss
criterion = nn.BCEWithLogitsLoss().cuda()

# Select optimizer
if args.optimizer is 1:
    optimizer = torch.optim.SGD(model.parameters(),
                                lr=args.learning_rate,
                                momentum=0.9,
                                weight_decay=0.0005)

elif args.optimizer is 2:
    optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate)

elif args.optimizer is 3:
    optimizer = torch.optim.Adadelta(model.parameters(), lr=1.0, rho=0.9, eps=1e-06, weight_decay=0.0005)

elif args.optimizer is 4:
    optimizer = torch.optim.RMSprop(model.parameters(), lr=args.learning_rate, alpha=0.99, eps=1e-08,
                                    weight_decay=0.0005,
                                    momentum=0.9,
                                    centered=False)
else:
    print("Not a valid optimizer")
    exit(1)

# Count number of trainable parameters
model_parameters = filter(lambda p: p.requires_grad, model.parameters())
params = sum([np.prod(p.size()) for p in model_parameters])
print("Number of trainable paramters in model:", params)
writer.add_text("Trainable Parameters", str(params))
json_saver['n_parameters'] = int(params)

# Variables for model selection
best_f1, best_epoch, best_loss = 0.0, 0, 1.

# Thresholding
threshold1 = nn.Threshold(args.threshold, 0.0).cuda()
threshold2 = nn.Threshold(-args.threshold, -1.0).cuda()

# Infinite validation set to validate while training
static_validation_data = create_batches(val_data, batch_size=1)
infinite_validation_data = utils.infinite_data_loader(static_validation_data)

# Timestamp before training start
time_before = datetime.datetime.now()

# Training loop
with trange(args.nr_epochs) as t:
    for current_epoch in t:

        # Progress bar
        t.set_description("Epochs")

        # Create data batches
        training_data = create_batches(train_data, batch_size=args.batch_size)

        # Enable training mode
        model.train()

        epoch_val_f1 = 0.0
        epoch_val_loss =0.0
        epoch_val_count = 0

        with tqdm(enumerate(training_data), total=nr_training_iterations) as tb:
            for i_batch, batch in tb:
                # Training iteration count
                iter_count = current_epoch * (nr_training_samples / args.batch_size) + i_batch

                # For progress bar
                tb.set_description("Training Iterations")

                # Reset gradients
                optimizer.zero_grad()

                # Forward pass
                print(batch['input'].size())
                inputs = batch['input'].cuda()
                targets = batch['target'].cuda()
                outputs = model(inputs)

                # Backward pass
                # Intermediate supervision for W-Net
                if intermediate_supervision:
                    loss = criterion(outputs[:, 0, :, :].unsqueeze(1), targets) + \
                           criterion(outputs[:, 1, :, :].unsqueeze(1), targets)
                    jac = jaccard(utils.flatten(targets),
                                  torch.sigmoid(utils.flatten(outputs[:, -1, :, :].unsqueeze(1))))
                else:
                    loss = criterion(outputs, targets)
                    jac = jaccard(utils.flatten(targets),
                                  torch.sigmoid(utils.flatten(outputs)))

                total_loss = loss + args.jaccard * jac
                total_loss.backward()
                optimizer.step()

                # Save progress
                if i_batch % args.save_iter_freq == 0 and i_batch != 0:

                    # Apply sigmoid to output
                    sigmoided_out = torch.sigmoid(outputs)

                    # Convert to binary labels
                    thresholded_out = -threshold2(-threshold1(sigmoided_out))

                    # Calculate accuracy
                    accuracy = torch.mean((thresholded_out[:, -1, :, :].unsqueeze(1) - targets) ** 2)
                    accuracy = accuracy.cpu().detach().numpy()

                    # Calculate f1 score (on the CPU)
                    ground_truth = batch['target'].cpu().view(BATCH_VAL_IMAGE_SIZE).detach().numpy()
                    predictions = thresholded_out[:, -1, :, :].unsqueeze(1).cpu().view(BATCH_VAL_IMAGE_SIZE).detach().numpy()
                    f1 = f1_score(y_true=ground_truth.flatten().astype(int),
                                  y_pred=predictions.flatten().astype(int))

                    # Get losses on the CPU
                    iter_loss = loss.cpu().detach().numpy()
                    iter_jac_loss = jac.cpu().detach().numpy()
                    iter_total_loss = total_loss.cpu().detach().numpy()

                    # Write losses to tensorboard
                    writer.add_scalar('train/Total Loss', float(iter_total_loss), iter_count)
                    writer.add_scalar('train/BCE Loss', float(iter_loss), iter_count)
                    writer.add_scalar('train/Jaccard Loss', float(iter_jac_loss), iter_count)
                    writer.add_scalar('train/Accuracy', float(accuracy), iter_count)
                    writer.add_scalar('train/F1 Score', float(f1), iter_count)

                    # Add images to tensorboard
                    writer.add_image('train/input', batch['input'][0], iter_count)
                    # Special case for intemediate supervision
                    if intermediate_supervision:
                        writer.add_image('train/intermediate_out', sigmoided_out[0][0].unsqueeze(0), iter_count)
                        writer.add_image('train/intermediate_prediction', thresholded_out[0][0].unsqueeze(0),
                                         iter_count)
                        writer.add_image('train/output', sigmoided_out[0][1].unsqueeze(0), iter_count)
                        writer.add_image('train/prediction', thresholded_out[0][1].unsqueeze(0), iter_count)

                    else:
                        writer.add_image('train/output', sigmoided_out[0], iter_count)
                        writer.add_image('train/prediction', thresholded_out[0], iter_count)
                    writer.add_image('train/target', batch['target'][0], iter_count)

                    # Add iter values to JSON
                    json_saver['train_loss'][str(iter_count)] = float(iter_total_loss)
                    json_saver['train_f1'][str(iter_count)] = float(f1)
                    json_saver['train_accuracy'][str(iter_count)] = float(accuracy)
                    duration = (datetime.datetime.now() - time_before).total_seconds()
                    json_saver['train_timestamps'][str(current_epoch)] = duration

                # Validate
                if i_batch % args.val_iter_freq == 0:

                    time_after = datetime.datetime.now()
                    epoch_val_count += 1

                    # Switch to evaluation mode
                    model.eval()
                    with torch.no_grad():

                        # Store average values
                        val_avg_loss, val_avg_f1, val_avg_acc = 0.0, 0.0, 0.0

                        for val_step in range(args.val_count):
                            val_batch = next(iter(infinite_validation_data))

                            # Forward pass
                            val_inputs = val_batch['input'].cuda()
                            val_targets = val_batch['target'].cuda()
                            val_outputs = model(val_inputs)

                            # Calculate loss
                            if intermediate_supervision:
                                val_loss = criterion(val_outputs[:, 0, :, :].unsqueeze(1), val_targets) + \
                                           criterion(val_outputs[:, 1, :, :].unsqueeze(1), val_targets)
                                val_jac = jaccard(utils.flatten(val_targets),
                                                  torch.sigmoid(utils.flatten(val_outputs[:, -1, :, :].unsqueeze(1))))

                            else:
                                val_loss = criterion(val_outputs, val_targets)
                                val_jac = jaccard(utils.flatten(val_targets),
                                                  torch.sigmoid(utils.flatten(val_outputs)))
                            val_total_loss = val_loss + args.jaccard * val_jac
                            val_avg_loss += val_total_loss

                            # Special case for intermediate supervision
                            if intermediate_supervision:
                                full_outputs = val_outputs
                                full_sigmoided_out = torch.sigmoid(full_outputs)
                                outputs = full_sigmoided_out[0][0].unsqueeze(0).detach().cpu().numpy()
                                out_image = Image.fromarray(np.uint8(outputs[0] * 255), 'L')
                                out_image.convert('RGB').save(inter_dir + "/intermediate_" + str(val_step) + ".png")
                                outputs = full_sigmoided_out[0][1].unsqueeze(0).detach().cpu().numpy()
                                out_image = Image.fromarray(np.uint8(outputs[0] * 255), 'L')
                                out_image.convert('RGB').save(inter_dir + "/final_" + str(val_step) + ".png")

                                val_outputs = val_outputs[:, -1, :, :].unsqueeze(1)

                            # Apply sigmoid
                            val_sigmoided_out = torch.sigmoid(val_outputs)

                            # Threshold predictions
                            val_thresholded_out = -threshold2(-threshold1(val_sigmoided_out))

                            # Calculate accuracy
                            accuracy = torch.mean((val_thresholded_out - val_targets) ** 2).cpu().detach().numpy()
                            val_avg_acc += accuracy

                            # Calculate f1 score (on the CPU)
                            val_ground_truth = val_batch['target'].cpu().view(VAL_IMAGE_SIZE).detach().numpy()
                            val_predictions = val_thresholded_out.cpu().view(VAL_IMAGE_SIZE).detach().numpy()
                            val_f1 = f1_score(y_true=val_ground_truth.flatten().astype(int),
                                              y_pred=val_predictions.flatten().astype(int))
                            val_avg_f1 += val_f1

                            # Save first image during validation
                            if val_step == 0:
                                writer.add_image('valid/input', val_batch['input'][0], iter_count)
                                writer.add_image('valid/output', val_sigmoided_out[0], iter_count)
                                writer.add_image('valid/prediction', val_thresholded_out[0], iter_count)
                                writer.add_image('valid/target', val_batch['target'][0], iter_count)

                        val_avg_loss /= args.val_count
                        val_avg_acc /= args.val_count
                        val_avg_f1 /= args.val_count

                        epoch_val_f1 += val_avg_f1
                        epoch_val_loss += val_avg_loss
                        # Write average validation score to tensorboard
                        writer.add_scalar('valid/Total Loss', val_avg_loss, iter_count)
                        writer.add_scalar('valid/Accuracy', val_avg_acc, iter_count)
                        writer.add_scalar('valid/F1 Score', val_avg_f1, iter_count)

                        # Write average validation score to JSON
                        json_saver['val_loss'][str(iter_count)] = float(val_avg_loss)
                        json_saver['val_accuracy'][str(iter_count)] = float(val_avg_acc)
                        json_saver['val_f1'][str(iter_count)] = float(val_avg_f1)
                        json_saver['val_timestamps'][str(iter_count)] = (time_after - time_before).total_seconds()

                    # Switch back to training mode
                    model.train()

        epoch_val_f1 /= epoch_val_count
        epoch_val_loss /= epoch_val_count
        # Save model if better
        if epoch_val_loss < best_loss:
            torch.save(model.state_dict(), os.path.join(model_dir, 'model.pt'))
            json_saver['model_save_iter'] = current_epoch
            best_loss = epoch_val_loss
            best_epoch = current_epoch

        # Save json to file
        with open(save_dir + '/data.json', 'w') as fp:
            json.dump(json_saver, fp, indent=2)

        # Early stopping
        if current_epoch - best_epoch > args.early_stopping:
            print("Early stopping. Best iter was {}, now at {}".format(best_epoch, current_epoch))
            break

# Save
time_after_all = datetime.datetime.now()
json_saver['end_time'] = time_after_all.strftime("%b_%d_%H%M%S")
json_saver['run_time'] = (time_after_all - time_before).total_seconds()
json_saver['last_iter'] = iter_count

# Save json file
with open(save_dir + '/data.json', 'w') as fp:
    json.dump(json_saver, fp, indent=2)

print("Starting evaluation")

# Load model, put it on the GPU and set it to evaluation mode
model.load_state_dict(torch.load(os.path.join(model_dir, 'model.pt')))
model.cuda()
model.eval()

# Evaluate
predictions = evaluate(save_dir, model, args.threshold, intermediate_supervision)
# Create overlays
create_overlays(save_dir)
# Create submission file
mask2submission(save_dir + "/" + LOG_NAME + ".csv", predictions)


import os
from flask import Flask , render_template, request
app=Flask(__name__)

def form_string(r):
    temp=""
    for key, value in r.items():
        temp=temp+"{},{} <br>".format(key, value)
    return temp

@app.route('/', methods=["GET", "POST"])
def main_index():
    if request.method =="GET":
        return render_template("form_basic.html")
    elif request.method == "POST":
        result=request.form
        output = form_string(result)
        return output

if __name__ == '__main__':
    app.run(debug=True)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

"""
Dataset obsahuje nasledujici promenne:
 'Age' - vek v rocich
 'Fare' - cena jizdenky
 'Name' - jmeno cestujiciho
 'Parch' - # rodicu/deti daneho cloveka na palube
 'PassengerId' - Id
 'Pclass' - Trida, 1 = 1. trida, 2 = 2.trida, 3 = 3.trida
 'Sex' - pohlavi
 'SibSp' - # sourozencu/manzelu daneho cloveka na ppalube
 'Survived' - 0 = Neprezil, 1 = Prezil
 'Embarked' - Pristav, kde se dany clovek nalodil. C = Cherbourg, Q = Queenstown, S = Southampton
 'Cabin' - Cislo kabiny
 'Ticket' - Cislo tiketu
"""

def load_dataset(train_file_path, test_file_path):
    """
    Napiste funkci, ktera nacte soubory se souboru zadanych parametrem a vytvori dva separatni DataFrame. Pro testovani vyuzijte data 'data/train.csv' a 'data/test.csv'
    Ke kazdemu dataframe pridejte sloupecek pojmenovaný jako "Label", ktery bude obsahovat hodnoty "Train" pro train.csv a "Test" pro test.csv.

    1. Pote slucte oba dataframy.
    2. Z vysledneho slouceneho DataFramu odstraňte sloupce  "Ticket", "Embarked", "Cabin".
    3. Sloučený DataDrame bude mít index od 0 do do počtu řádků.
    4. Vratte slouceny DataDrame.
    """

    train = pd.read_csv(train_file_path)
    test = pd.read_csv(test_file_path)
    train['Label'] = "Train"
    test['Label'] = "Test"

    return train.append(test, ignore_index=True, sort=True).drop(columns=["Ticket", "Embarked", "Cabin"])

def get_missing_values(df : pd.DataFrame) -> pd.DataFrame:
    """
    Ze zadaneho dataframu zjistete chybejici hodnoty. Vyvorte DataFrame, ktery bude obsahovat v indexu jednotlive promenne
    a ve prvnim sloupci bude promenna 'Total' obsahujici celkovy pocet chybejicich hodnot a ve druhem sloupci promenna 'Percent',
    ve ktere bude procentualni vyjadreni chybejicich hodnot vuci celkovemu poctu radku v tabulce.
    DataFrame seradte od nejvetsich po nejmensi hodnoty.
    Vrattre DataFrame chybejicich hodnot a celkovy pocet chybejicich hodnot.

    Priklad:

               |  Total  |  Percent
    "Column1"  |   34    |    76
    "Column2"  |   0     |    0

    """
    total = df.isna().sum().to_numpy(dtype = int)
    percent = total / df.shape[0]
    output = pd.DataFrame(columns = ['Total', 'Percent'], index = df.columns.values, data = np.column_stack((total, percent * 100)))

    return output.sort_values(list(output.columns.values), ascending=False)

def substitute_missing_values(df: pd.DataFrame) -> pd.DataFrame:
    """
    Chybejici hodnoty ve sloupecku "Age" nahradte meanem hodnot z "Age".
    Chybejici hodnoty ve sloupecku "Fare" nahradte meadianem hodnot z "Fare".
    V jednom pripade pouzijte "loc" a ve druhem "fillna".
    Zadany DataFrame neupravujte, ale vytvorte si kopii.
    Vratte upraveny DataFrame.
    """

    output = df.fillna({'Age': df['Age'].mean()})
    output.loc[np.isnan(output['Fare'])] = output['Fare'].median()

    return output

def get_correlation(df: pd.DataFrame) -> float:
    """
    Spocitejte korelaci pro "Age" a "Fare" a vratte korelaci mezi "Age" a "Fare".
    """

    return df['Age'].corr(df['Fare'])

def get_survived_per_class(df : pd.DataFrame, group_by_column_name : str) ->pd.DataFrame:
    """
    Spocitejte prumer z promenne "Survived" pro kazdou skupinu zadanou parametrem "group_by_column_name".
    Hodnoty seradte od nejvetsich po mejmensi.
    Hodnoty "Survived" zaokhroulete na 2 desetinna mista.
    Vratte pd.DataFrame.

    Priklad:

    get_survived_per_class(df, "Sex")

                 Survived
    Male     |      0.32
    Female   |      0.82

    """

    group_name_df = df.groupby(group_by_column_name).mean()['Survived']
    output = pd.DataFrame(columns = [group_by_column_name], data = group_name_df.index.to_numpy())
    output['Survived'] = np.round(group_name_df.values, 2)

    return output.sort_values('Survived', ascending = False)

def get_outliers(df: pd.DataFrame) -> (int, str):
    """
    Vyfiltrujte odlehle hodnoty (outliers) ve sloupecku "Fare" pomoci metody IRQ.
    Tedy spocitejte rozdil 3. a 1. kvantilu, tj. IQR = Q3 - Q1.
    Pote odfiltrujte vsechny hodnoty nesplnujici: Q1 - 1.5*IQR < "Fare" < Q3 + 1.5*IQR.
    Namalujte box plot pro sloupec "Fare" pred a po vyfiltrovani outlieru.
    Vratte tuple obsahujici pocet outlieru a jmeno cestujiciho pro nejvetsi outlier.
    """

    Q1 = df['Fare'].quantile(0.25)
    Q3 = df['Fare'].quantile(0.75)
    IQR = Q3 - Q1

    low = Q1 - 1.5 * IQR
    high = Q3 + 1.5 * IQR
    output = df.loc[(df['Fare'] <= low) | (df['Fare'] >= high)]

    plt.boxplot([df['Fare'].dropna().tolist(), output['Fare'].dropna().tolist()])

    return output.shape[0], output.loc[output['Fare'].idxmax()]['Name']

def normalise(df: pd.DataFrame, col: str) -> pd.DataFrame:
    """
    Naskalujte sloupec "col" zadany parametrem pro kazdou "Pclass" hodnotu z dataframu "df" zvlast.
    Pouzijte vzorec: scaled_x_i = (x_i - min(x)) / (max(x) - min(x)), kde "x_i" prestavuje konkretni hodnotu ve sloupeci "col".
    Vratte naskalovany dataframe.
    """

    output = df

    for i in df['Pclass'].unique():
        column = df.loc[df['Pclass'] == i, [col]]
        output.loc[df['Pclass'] == i, [col]] = (column - column.min())/(column.max() - column.min())

    return output

def create_new_features(df: pd.DataFrame) -> pd.DataFrame:
    """
    Vytvorte 3 nove promenne:
    1. "Fare_scaled" - vytvorte z "Fare" tak, aby mela nulovy prumer a jednotkovou standartni odchylku.
    2. "Age_log" - vytvorte z "Age" tak, aby nova promenna byla logaritmem puvodni "Age".
    3. "Sex" - Sloupec "Sex" nahradte: "female" -> 1, "male" -> 0, kde 0 a 1 jsou integery.

    Nemodifikujte predany DataFrame, ale vytvorte si novy, upravte ho a vratte jej.
    """
    output = df

    output['Fare_scaled'] = (output['Fare'] - output['Fare'].mean()) / np.std(output['Fare'])
    output['Age_log'] = np.log(output['Age'])
    output['Sex'] = (output['Sex'] == 'female')

    return output

def determine_survival(df: pd.DataFrame, n_interval: int, age: float, sex: str) -> float:
    """
    Na zaklade statistickeho zpracovani dat zjistete pravdepodobnost preziti Vami zadaneho cloveka (zadava se vek a pohlavi pomoci parametru "age" a "sex")

    Vsechny chybejici hodnoty ve vstupnim DataFramu ve sloupci "Age" nahradte prumerem.
    Rozdelte "Age" do n intervalu zadanych parametrem "n_interval". Napr. pokud bude Age mit hodnoty [2, 13, 18, 25] a mame jej rozdelit do 2 intervalu,
    tak bude vysledek:

    0    (1.977, 13.5]
    1    (1.977, 13.5]
    2     (13.5, 25.0]
    3     (13.5, 25.0]

    Pridejte k rozdeleni jeste pohlavi. Tj. pro kazdou kombinaci pohlavi a intervalu veku zjistete prumernou
    pravdepodobnost preziti ze sloupce "Survival" a tu i vratte.

    Vysledny DataFrame:

    "AgeInterval"   |    "Sex"    |   "Survival Probability"
       (0-10)       | "male"      |            0.21
       (0-10)       | "female"    |            0.28
       (10-20)      | "male"      |            0.10
       (10-20)      | "female"    |            0.15
       atd...

    Takze vystup funkce determine_survival(df, n_interval=20, age = 5, sex = "male") bude 0.21. Tato hodnota bude navratovou hodnotou funkce.

    """

    df = df.fillna({'Age': df['Age'].mean()})
    intervals = np.linspace(df['Age'].min(), df['Age'].max(), n_interval + 1)

    AgeIntervals = [(intervals[i], intervals[i + 1])
                    for i in range(0, intervals.shape[0] - 1)
                        for j in (0, 1)] #male ande female intervals
    sexes = []
    for i in range(len(AgeIntervals)) :
        if i % 2 == 0 : sexes.append('male')
        else : sexes.append('female')

    output = 0.0
    survival_probabilities = []
    for interval, it_sex in zip(AgeIntervals, sexes):
        probability = df.loc[(df['Age'] > interval[0]) & (df['Age'] <= interval[1]) & (df['Sex'] == it_sex), ['Survived']].mean()['Survived']
        survival_probabilities.append(probability)

        if output == 0.0 and interval[0] < age <= interval[1] and it_sex == sex:
            output = probability

    output_df = pd.DataFrame()

    output_df['AgeInterval'] = AgeIntervals
    output_df['Sex'] = sexes
    output_df['Survival Probability'] = survival_probabilities

    return output


from tkinter import *
from tkinter import ttk

#class Application:

#	def __init__(self, master):
#		label = ttk.Label(master, text="What's up world?")
		#label.pack()
		#label.config(wraplength = 150)
		#label.config(justify = CENTER)
		#label.config(foreground = 'blue', background = 'black')
#		self.logo = PhotoImage(file = 'C:\\Users\\Leon\\Desktop\\SummerThings\\Images\\jhene.pgm')
#		label.image = logo

#		label.pack()

#def main():
#	root = Tk()
#	app = Application(root)
#	root.mainloop()

#if __name__ == "__main__": main()

root = Tk()
label = ttk.Label(text="What's up world?")
logo = PhotoImage(file = 'C:\\Users\\Leon\\Desktop\\SummerThings\\Images\\jhene.pgm')
label.config(image = logo)
label.pack()

root.mainloop()


import json
import time

import adafruit_dht
import board

from logger.Logger import Logger, LOG_LEVEL
from sensors.linux.sensor import Sensor

class HumiditySensor(Sensor):
    sensor = adafruit_dht.DHT11

    def __init__(self, pin, name=None, key=None, model='11', redis_conn=None):
        super().__init__(pin, name=name, key=key, redis_conn=redis_conn)
        self.pin_obj = getattr(board, pin)
        self.type = model
        return

    def init_sensor(self):
        """
        Initialize the sensor here (i.e. set pin mode, get addresses, etc)
        this gets called by the worker
        """
        sensor_types = {
            '11': adafruit_dht.DHT11,
            '22': adafruit_dht.DHT22,
            '2302': adafruit_dht.DHT22
        }  # AM2302 = DHT22

        if self.type in sensor_types:
            self.sensor = sensor_types[self.type]

        else:
            Logger.log(
                LOG_LEVEL["warning"],
                'Sensor Model Error: Defaulting to DHT11'
            )
        return

    def read(self):
        """
        Read the sensor(s), parse the data and store it in redis if redis
        is configured
        """
        # Set values just in case we never set them up.

        humidity = None
        temperature_c = None

        # read_retry() not implemented in new lib
        for i in range(15):
            dht_device = self.sensor(self.pin_obj)
            time.sleep(2)

            try:
                dht_device.measure()
                temperature_c = dht_device.temperature
                humidity = dht_device.humidity
                if humidity is not None and temperature_c is not None:
                    break

            except RuntimeError:
                # Errors happen fairly often, DHT's are hard to read,
                # just keep going:
                continue

            finally:
                dht_device.exit()

        if humidity is not None and temperature_c is not None:
            self.r.set(
                self.key + '_temperature',
                round(temperature_c * 1.8 + 32, 2)
            )
            self.r.set(
                self.key + '_humidity', humidity
            )
            readings = {
                'temperature': round(temperature_c * 1.8 + 32, 2),
                'humidity': round(humidity, 2)
            }
            self.r.set(self.key, json.dumps(readings))
            return readings

        else:
            Logger.log(
                LOG_LEVEL["error"],
                'DHT Reading was Invalid. Trying again next cycle.'
            )
            return None

    def read_raw(self):
        """
        Read the sensor(s) but return the raw data, useful for debugging

        Returns:

        """
        return self.read()


from flask import request, Response, render_template, make_response, jsonify
import simplejson as json

class JSONResponse(Response):
    @classmethod
    def force_type(cls, rv, environ=None):
        if isinstance(rv, dict):
            best = request.accept_mimetypes.best_match(['application/json', 'text/html'])
            if best == 'text/html' and request.accept_mimetypes[best] > request.accept_mimetypes['application/json']:
                rv = make_response(render_template('api/json.html', response=json.dumps(rv, sort_keys=True, indent=2)))
            else:
                rv = jsonify(rv)
        return super(JSONResponse, cls).force_type(rv, environ)


# coding: utf-8
import os
import numpy as np
import pandas as pd
import keras
from keras.models import load_model
from keras.models import Sequential,Model
from keras.layers import Conv2D
from keras.layers import Conv2DTranspose,Concatenate,Input
import matplotlib.pyplot as plt
import warnings
import json
import cv2
import zipfile
warnings.filterwarnings("ignore")
train_path1 = "../raw_data/round_train/part1/OK_Images/"

test_path1 =  "../raw_data/round_test/part1/TC_Images/" #产品1的测试集路径

model_path1 =  '../model/model1/'

temp_path = '../temp_data/'

save_path1 = '../temp_data/result/data/focusight1_round2_train_part1/TC_Images/'

result_path = '../result/'

mask_save = '../temp_data/'

'''构建模型'''
model = Sequential([
    Conv2D(filters=30, kernel_size=[3, 3], strides=[2, 2], padding='same', input_shape=[128, 128, 1],
           activation='relu'),
    Conv2D(filters=60, kernel_size=[3, 3], strides=[2, 2], padding='same', activation='relu'),  # 64*64*30
    Conv2D(filters=90, kernel_size=[3, 3], strides=[2, 2], padding='same', activation='relu'),  # 32*32*60
    Conv2D(filters=120, kernel_size=[3, 3], strides=[2, 2], padding='same', activation='relu'),  # 16*16*90
    Conv2DTranspose(90, kernel_size=[3, 3], strides=[2, 2], padding='same', activation='relu'),  # 32x32x120
    Conv2DTranspose(60, kernel_size=[3, 3], strides=[2, 2], padding='same', activation='relu'),  ## # 32x32x60
    Conv2DTranspose(30, kernel_size=[3, 3], strides=[2, 2], padding='same', activation='relu'),  # # 32x32x30
    Conv2DTranspose(1, kernel_size=[3, 3], strides=[2, 2], padding='same', activation='relu'),  # # 32x32x1
])
model.summary()
model.compile(loss=keras.losses.mean_squared_error,
              optimizer=keras.optimizers.Adam(),
              metrics=['accuracy'])
''''''''''''''''''''''''''''''''

def load_data(test_path):
    testfiles = os.listdir(test_path)
    testfile_names = []
    test = np.empty([len(testfiles),128,128,1])
    for i in range(len(testfiles)):
        testfile_names.append(os.path.splitext(testfiles[i])[0])
        img = cv2.imread(test_path + testfiles[i],cv2.IMREAD_GRAYSCALE).reshape([128,128,1])
        test[i] = img
    return test, testfile_names

def Dection(X, Y,file_name):
    if X.ndim==4:
        X = X[:,:,:,0]
        Y = Y[:,:,:,0]
    img_diff = np.abs(X-Y)
    res = np.copy(img_diff)
    for i in range(0,len(X)):
        img1 = img_diff[i] # 阈值大概在10左右
        img3 = np.copy(img1)
        img3[img3<18] = 0
        img3[img3>=18] = 255
        res[i] = img3
        x_origin = np.zeros(shape=[*X[i].shape,3],dtype=np.uint8)
        x_origin[:,:,0]=X[i]
        x_origin[:,:,1]=X[i]
        x_origin[:,:,2]=X[i]
        x_mask = np.copy(x_origin)
        x_mask[img3>0,:]=(255,0,0)
        plt.imsave(temp_path+'mask/{}.jpg'.format(file_name[i]),np.concatenate([x_mask,x_origin],axis=0),vmin = 0,vmax = 255)
    return res
def get_segment_result(test, testfile_names, model_path):
    '''首先加载模型'''
    model.load_weights(model_path + 'model.h5')
    test = test.astype('float32')
    '''再对模型进行预测'''
    y_test = model.predict(test)
    '''对预测的结果进行处理'''
    mask_imgs = Dection(test, y_test,testfile_names)
    regions = []
    for i in range(len(mask_imgs)):
        img = mask_imgs[i]
        index = np.argwhere(img>0)
        region = []
        for j in range(len(index)):
            x, y = index[j]
            region.append("{}, {}".format(x,y))
        regions.append(region)
    region_df = pd.DataFrame({'regions': regions,
                            'testfile_names': testfile_names})
    return region_df

#本示例未进行像素级的检测，为了保证result.zip的完整性，示例中人为写入一对像素坐标
def save_result(tc_images,save_path):
    for row_index,row in tc_images.iterrows():
        result = {}
        result['Height'] = 128
        result['Width'] = 128
        result['name'] = row['testfile_names'] + '.bmp'
        regions = []
        points = {}
        points['points'] = row['regions'] #请注意，两像素值中间有一个空格
        regions.append(points)
        result['regions'] = regions
        if len(row['regions']) < 10 or len(row['regions']) > 3000:
            continue
        savefile = save_path + row['testfile_names'] + '.json'
        with open(savefile,'w') as f:
            f.write(json.dumps(result,ensure_ascii=False,indent=2))

def zipdir(path,file):
    z = zipfile.ZipFile(file,'w',zipfile.ZIP_DEFLATED)
    for dirpath, dirnames, filenames in os.walk(path):
        fpath = dirpath.replace(path,'')
        fpath = fpath and fpath + os.sep or ''
        for filename in filenames:
            z.write(os.path.join(dirpath, filename),fpath+filename)
    z.close()

test1, testfile_names1 = load_data(test_path1)

region_df1 = get_segment_result(test1, testfile_names1, model_path1)

save_result(region_df1,save_path1)

zipdir(temp_path + 'result/', result_path + 'data.zip')