#!/usr/bin/env python
# -*- coding:utf-8 -*-

import argparse

def parse_args():
    parser = argparse.ArgumentParser(usage='Identify related words in tmt based on the '
                                           '1. LCS between source and tms'
                                           '2. word alignment between tms and tmt')

    parser.add_argument('-s', '--src', required=True,
                        help='Path to the plain source file.')
    parser.add_argument('-tms', required=True)
    parser.add_argument('-tmt', required=True)
    parser.add_argument('--match-file', required=True,
                        help='Match file indicating the relationship between source and TMS.')
    parser.add_argument('-a', '--alignment', required=True,
                        help='Alignment file indicating the relationship between TMS and TMT.')
    parser.add_argument('-o', '--output', required=True)

    parser.add_argument('--source-tag', default='0',
                        help='DEFAULT: 0')
    parser.add_argument('--relate-tag', default='1',
                        help='DEFAULT: 1')
    parser.add_argument('--unrelate-tag', default='2',
                        help='DEFAULT: 2')
    parser.add_argument('--concat-tag', default=None,
                        help='DEFAULT: None. If set to None, will use the unrelate_tag.')

    args = parser.parse_args()

    return args

def find_longest_common_subsequences(seq_1, seq_2, sep=' '):
    # Ensure the smaller of the two sequences is used to create the columns for
    # the DP table.
    flag = 'col'
    if len(seq_1) < len(seq_2):
        new_seq_1 = seq_2
        seq_2 = seq_1
        seq_1 = new_seq_1
        flag = 'row'

    seq_1_len = len(seq_1)
    seq_2_len = len(seq_2)
    seq_1_len_plus_1 = seq_1_len + 1
    seq_2_len_plus_1 = seq_2_len + 1

    subseq_last_row = [''] * seq_2_len_plus_1
    subseq_current_row = [''] + [''] * seq_2_len

    for row in range(1, seq_1_len_plus_1):

        for col in range(1, seq_2_len_plus_1):

            if seq_1[row - 1] == seq_2[col - 1]:
                diagonal_cell_value = subseq_last_row[col - 1]
                # matched_element = seq_1[row-1]
                if flag == 'col':  # seq_2 corresponds to tms
                    rec = col - 1
                else:  # seq_1 corresponds to tms
                    rec = row - 1
                new_cell_value = f'{diagonal_cell_value}{sep}{rec}'
            else:
                above_set = subseq_last_row[col]
                left_set = subseq_current_row[col - 1]
                if len(above_set) >= len(left_set):
                    new_cell_value = above_set
                else:
                    new_cell_value = left_set
            subseq_current_row[col] = new_cell_value

        subseq_last_row = subseq_current_row
        subseq_current_row = [''] + [''] * seq_2_len

    return [int(i) for i in subseq_last_row[-1].split()]

def process_one_line(src_tokens, tms_tokens, tmt_tokens, aligns, opt):
    # find the LCS of src_line and tms_line
    # record the indices of LCS's tokens in tms_line
    lcs_indices = find_longest_common_subsequences(src_tokens, tms_tokens)

    # re-arrange the alignment information into a list of tuple and from the "target" perspective
    align_info = []
    for p in aligns:
        s_i, t_i = p.split('-')
        s_i, t_i = int(s_i), int(t_i)
        align_info.append((t_i, s_i))

    # iterate every possible pair of t-s alignment and filter out the indices of the related words
    # referring to paper: Boosting Neural Machine Translation with Similar Translations(ACL2020)
    set_pos = set([])
    set_neg = set([])

    for t in range(len(tmt_tokens)):
        neg_flag = True
        for s in range(len(tms_tokens)):
            if (t, s) in align_info and s in lcs_indices:
                set_pos.add(t)
            elif (t, s) in align_info and s not in lcs_indices:
                neg_flag = False
        if neg_flag:
            set_neg.add(t)

    final_set = set_pos.intersection(set_neg)

    # 1: T  2: R
    map_table = [opt.relate_tag if t in final_set else opt.unrelate_tag for t in range(len(tmt_tokens))]
    concat_tag = opt.concat_tag if opt.concat_tag is not None else opt.unrelate_tag
    map_table = [opt.source_tag] * len(src_tokens) + [concat_tag] + map_table

    return map_table

def main():
    opt = parse_args()

    print('Reading source lines...')
    with open(opt.src, 'r') as f:
        src_lines = [l.strip() for l in f]

    print('Reading TMS lines...')
    with open(opt.tms, 'r') as f:
        tms_lines = [l.strip() for l in f]

    print('Reading TMT lines...')
    with open(opt.tmt, 'r') as f:
        tmt_lines = [l.strip() for l in f]

    print('Reading match file lines...')
    with open(opt.match_file, 'r') as f:
        match_lines = [l.strip() for l in f]

    print('Reading alignment lines...')
    with open(opt.alignment, 'r') as f:
        align_lines = [l.strip() for l in f]

    assert len(src_lines) == len(match_lines), 'Every source line should and have to have one row of match ' \
                                               'information(if no match, then leave an empty row)'

    assert len(tms_lines) == len(tmt_lines)
    assert len(tms_lines) == len(align_lines), 'TMS and TMT alignment information should be integrated.'

    wf = open(opt.output, 'w')
    for i, src_line in enumerate(src_lines):
        match_line = match_lines[i]
        src_tokens = src_line.strip().split()
        # only one-best is supported now.
        if match_line.strip() != '':
            tm_i = int(match_line.strip().split('|||')[0].split()[0])
            tms_line = tms_lines[tm_i]
            tmt_line = tmt_lines[tm_i]
            align_line = align_lines[tm_i]

            tms_tokens = tms_line.strip().split()
            tmt_tokens = tmt_line.strip().split()
            aligns = align_line.strip().split()
            line_res = process_one_line(src_tokens, tms_tokens, tmt_tokens, aligns, opt)
        else:
            line_res = [opt.source_tag] * len(src_tokens)

        wf.write(' '.join([f'{i}' for i in line_res]) + '\n')

if __name__ == '__main__':
    main()


from setuptools import setup, find_packages
import requests
import os
from ppickle import dump, load
package_info_filename = "package_info.ppickle"
package_info = load(package_info_filename)
USERNAME = package_info["USERNAME"]
REPONAME = package_info["REPONAME"] or os.path.split(os.path.dirname(os.path.abspath(__file__)))[-1]
GITHUB_API_URL = package_info["GITHUB_API_URL"] or f"https://api.github.com/repos/{USERNAME}/{REPONAME}"
URL = package_info["URL"] or f'https://github.com/{USERNAME}/{REPONAME}'
AUTHOR_EMAIL = package_info["AUTHOR_EMAIL"] or f'{USERNAME}@users.noreply.github.com'
README_PATH = package_info["README_PATH"] or 'README.md'
requirements = [
    "requests",
]

def get_description():
    description = requests.get(GITHUB_API_URL).json()['description']
    return description

def get_topic():
    topics = requests.get(GITHUB_API_URL + "/topics", headers={
        "Accept": "application/vnd.github.mercy-preview+json", }).json()['names']
    return ' '.join(topics)

def increment_version():
    raw_version = package_info['version'] or '0.0.0'
    int_version = int(raw_version.replace('.', ''))  # 3
    int_version += 1
    new_version = '.'.join(str(int_version).zfill(3))  # 0.0.4
    package_info['version'] = new_version
    dump(package_info_filename, package_info)
    with open('version.txt', 'w') as f:
        f.write(new_version)
    return new_version

def get_long_description():
    with open(README_PATH, 'r', encoding='utf-8') as f:
        long_description = f.read()

description = get_description()
keywords = get_topic()
version = increment_version()
long_description = get_long_description()
setup(
    name=REPONAME,
    version=version,
    description=description,
    long_description=long_description,
    long_description_content_type='text/markdown',
    url=URL,
    author=USERNAME,
    author_email=AUTHOR_EMAIL,
    license='MIT',
    keywords=keywords,
    packages=find_packages(),
    install_requires=requirements,
    classifiers=[
        'Programming Language :: Python :: 3.6',
    ],
)


from PIL import Image
import os
from pathlib import Path
from PIL.ExifTags import TAGS

#### Custom user file path ####
folder_path = Path("/Users/alexchapple/Mine/Watermark/")

def watermark_photo(input_image_path, output_image_path, watermark_image_path):

    # Opens the images
    base_image = Image.open(folder_path / str('photos' '/' + str(photo)))
    watermark = Image.open(watermark_image_path)

    # Gets the initial width and height for portrait mode
    init_width, init_height = base_image.size

    # Looks into exif code to see whether the photo is portrait or landscape
    base_image = get_exif(base_image)
    width, height = base_image.size

    # Resize water mark to fit the photo
    watermark = watermark.resize((int(init_width*0.2), int(init_height*0.2)), Image.ANTIALIAS)
    watermark.save('watermark_white.png')

    # Position of the watermark
    width2, height2 = watermark.size
    position = (width-width2, height-height2)

    # Add watermark to an image
    transparent = Image.new('RGB', (width, height))
    transparent.paste(base_image, (0, 0))
    transparent.paste(watermark, position, mask=watermark)
    transparent.save(folder_path / str('watermarked_photos/' + output_image_path))

# Function that looks into the exif code to correctly orientate the image
def get_exif(image):
    ret = {}
    info = image._getexif()
    for tag, value in info.items():
        decoded = TAGS.get(tag, tag)
        ret[decoded] = value
    print(ret["Orientation"])
    orientation = ret["Orientation"]
    if orientation == 3:
        image = image.rotate(180, expand=True)
    elif orientation == 6:
        image = image.rotate(-90, expand=True)
    elif orientation == 8:
        image = image.rotate(90, expand=True)
    return image

# Script code that runs through the 'photos' folder and performs the water marking
photo_folder = os.listdir(folder_path / 'photos')
i = 1

for photo in photo_folder:
    if photo.endswith('.JPG') | photo.endswith('.jpg'):
        watermark_photo(photo, str(i) + '.JPG', 'watermark.png')
        i += 1


# # library
import unittest
import os
import json

# # modules
from config import path_to_dir

class TestForJenkins(unittest.TestCase):

    def test_existence_of_files(self):
        paths = ('src/intense.json', 'main.py', 'bot.py', 'chatbot.py', 'config.py', 'logger.py', 'training.py')

        for name in paths:
            path = f'{path_to_dir}/{name}'
            exist = os.path.exists(path)
            if not exist:
                print(f'FILE NOT EXIST: {name} ({path})')
            self.assertTrue(exist)

    def test_structure_intense_json(self):
        with open('src/intense.json', 'r') as file:
            full_intents = json.load(file)

        languages = full_intents.keys()
        for language in languages:
            intense = full_intents[language]
            self.assertTrue('intents' in intense.keys())

            intense = intense['intents']
            for element in intense:
                keys = element.keys()
                headers = ('tag', 'patterns', 'responses')
                for header in headers:
                    self.assertTrue(header in keys)

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


from django.http import JsonResponse
from django.shortcuts import render, HttpResponse
from pacientes.models import Paciente
from personal.models import Doctores
from citas.models import Cita
from django.core import serializers
from pacientes.functpacientes import buscaPaciente

import json

# Create your views here.
def index(request):
    doctores = Doctores.objects.all()

    context = {
        'tmedicos': doctores,
    }
    return render(request, "citas/cita.html",context)

# http://www.lalicode.com/post/5/

def guardacitas(request):
    resp = ""

    paciente = Paciente()
    if request.method == "POST" and request.is_ajax():
        json_data = json.loads(request.body)

        pacid = buscaPaciente(json_data['nombre'],json_data['apellidos'],json_data["correo"])
        cita =Cita.objects.filter(id_paciente=pacid, fecha=json_data["fecha"]).count()
        doc = Doctores.objects.get(id=int(json_data['medico']))
        if cita>0:
            resp = "Ya existe una cita igual..."
        else:
            if pacid < 1:
                paciente.nombre = json_data['nombre']
                paciente.apellidos = json_data['apellidos']
                paciente.correo = json_data["correo"]
                paciente.celular = json_data['celular']
                paciente.estatus = "IP"  # paciente con información parcial cuando se atienda en la cita se hace el compelemento
                paciente.telefono = json_data['telefono']
                paciente.observaciones = json_data["motivo"]
                paciente.doctor=doc
                paciente.save()

            pacid = buscaPaciente(json_data['nombre'],json_data['apellidos'],json_data["correo"])
            cita = Cita()
            cita.nombre = json_data['nombre']
            cita.apellidos = json_data['apellidos']
            cita.telefono = json_data['telefono']
            cita.celular = json_data['celular']
            cita.correo = json_data["correo"]
            cita.fecha = json_data["fecha"]
            cita.hora = json_data["hora"]
            cita.motivo = json_data["motivo"]
            cita.id_paciente = Paciente.objects.get(id = pacid)
            cita.save()
            resp = "La cita ha sido Guardada..."

    else:
        resp = "Error al intentar guardar la cita "

    return JsonResponse({
        'respuesta': {
            'mensaje': resp,
        }
    })

def buscarcitas(request):
    return render(request, "citas/buscar.html")

def ajax_obtenDatosPacienteCitas(request):
    id = request.GET.get('id')
    if request.is_ajax():
        try:
            paciente = Paciente.objects.filter(id=id)
            data = serializers.serialize('json', paciente,
                                         fields=('nombre', 'apellidos', 'correo', 'telefono', 'celular'))
            return HttpResponse(data, content_type='application/json')
        except:
            return HttpResponse("Error al intentar recuperar datos")

def ajax_BuscaPacientes(request):
    busca = request.GET.get('empieza')
    if request.is_ajax():
        # print(busca)
        pacientes = Paciente.objects.filter(nombre__icontains=busca)
        paciente = []
        for p in pacientes:
            p_json = {}

            p_json['id'] = p.id
            p_json['nombre'] = p.nombre
            p_json['apellidos'] = p.apellidos
            paciente.append(p_json)
        # data_json = json.dumps(paciente)
    else:
        data_json = "Busqueda falló"
    mimetype = 'application/json'

    return JsonResponse({
        'respuesta': {
            'paciente': paciente
        }
    })


from __future__ import print_function
import sys, os, math
import numpy as np
from numpy import float32, int32, uint8, dtype
from PIL import Image
import glob

# Load PyGreentea
# Relative path to where PyGreentea resides
pygt_path = '../..'
sys.path.append(pygt_path)
import pygreentea.pygreentea as pygt
from pygreentea.pygreentea import malis

# Load the datasets - individual tiff files in a directory
raw_dir = '../../../project_data/dataset_01/train/raw'
label_dir = '../../../project_data/dataset_01/train/labels'

raw_path = sorted(glob.glob(raw_dir+'/*.tif'))
label_path = sorted(glob.glob(label_dir+'/*.png'))
num_images = len(raw_path)

raw_ds = [np.array(Image.open(raw_path[i]).convert('L'), 'f') for i in range(0, num_images)]
gt_ds = [np.array(Image.open(label_path[i]).convert('L'), 'f') for i in range(0,num_images)]
gt_ds_scaled = [np.floor(label/31) for label in gt_ds]

datasets = []
test_datasets = []
for i in range(0,1):
    dataset = {}
    dataset['data'] = np.expand_dims(pygt.normalize(np.asarray(raw_ds, float32)), 0)
    dataset['label'] = np.expand_dims(np.asarray(gt_ds_scaled, float32), 0)
    datasets += [dataset]

# Custom callback function to generate slices
def data_slice_callback(input_specs, batch_size, dataset_indexes, offsets, dataset_combined_sizes, data_arrays, slices):
    pass

def test_data_slice_callback(dataset_idx, offsets, datasets, slices):
    pass

# Set train options
class TrainOptions:
    loss_snapshot = 100
    test_interval = 4000
    train_device = 0
    test_device = 0
    test_net = None
    test_level = 0
    test_stages = None

options = TrainOptions()

# Set solver options
solver_config = pygt.caffe.SolverParameter()
solver_config.train_net = 'net.prototxt'
solver_config.base_lr = 0.00001
solver_config.momentum = 0.99
solver_config.weight_decay = 0.000005
solver_config.lr_policy = 'inv'
solver_config.gamma = 0.0001
solver_config.power = 0.75
solver_config.max_iter = 6000
solver_config.snapshot = 2000
solver_config.snapshot_prefix = 'net'
solver_config.type = 'Adam'
solver_config.display = 1

# Set devices
pygt.caffe.enumerate_devices(False)
pygt.caffe.set_devices((options.train_device,))

solverstates = pygt.get_solver_states(solver_config.snapshot_prefix)

# First training stage (softmax + euclid)
if (len(solverstates) == 0 or solverstates[-1][0] < solver_config.max_iter):
    solver, test_net = pygt.init_solver(solver_config, options)
    if (len(solverstates) > 0):
        solver.restore(solverstates[-1][1])
    pygt.train(solver, options, datasets, data_slice_callback,
               test_net, test_datasets, test_data_slice_callback)


import random
import numpy as np
import pandas as pd
import re
import uuid
import json
import hashlib
import io
from zipfile import ZipFile, ZIP_DEFLATED
from concurrent.futures import as_completed, ProcessPoolExecutor, wait

from os import listdir

file_regex = r"(\d+)_([A-Za-z_\d]+)_(\d*)_state_(\d+)\.txt"

def progressBar(iterable):
    total = len(iterable)
    largest = 0

    # Progress Bar Printing Function
    def printProgressBar(iteration, suf):
        percent = ("{0:.2f}").format(100 * (iteration / float(total)))

        filledLength = int(20 * iteration // total)
        bar = "█" * filledLength + "_" * (20 - filledLength)

        content = f"\rProgress |{bar}| {percent}% {suf}"
        lg = max(largest, len(content))
        missing = lg - len(content)
        content += " " * missing

        print(content, end="")

        return lg

    # Initial Call
    printProgressBar(0, "")
    # Update Progress Bar
    for i, item in enumerate(iterable):
        yield item
        largest = max(largest, printProgressBar(i + 1, item))
    # Print New Line on Complete
    print()

file_id = 0

def parse_file(file, content):
    global file_id
    matches = re.findall(file_regex, file)
    seed, alg, load, it = matches[0]

    tlc = 86
    tfc = 86 * 320

    data = pd.read_csv(io.BytesIO(content), delimiter=";")
    data = data.drop(["Unnamed: 320"], axis=1)

    data = data[0:tlc].values
    data = data.reshape(1, -1)

    actual_data = [[]]
    for i in range(0, tfc, 1):
        if data[0][i] != 0:
            data[0][i] = 1
        actual_data[0].append(int(data[0][i]))

    expected_output = {
        "MAdapSPV": 2,
        "K5SP_CS": 2,
        "K2SP_LastFit": 1,
        "K5SP_Random": 0,
    }

    payload = {
        "id": file_id,
        "seed": seed,
        "alg": alg,
        "load": load,
        "it": it,
        "data": actual_data,
        "expected_output": expected_output.setdefault(alg, 3),
    }
    file_id += 1

    return payload

input_zip = ZipFile("dataset.zip")
output_zip = ZipFile("workload.zip", "w", ZIP_DEFLATED)

batch = 500
cur = []
cur_i = 0

workload_id = 0

def process():
    global cur_i
    global cur
    global output_zip
    global workload_id

    if not cur:
        return
    workload = {
        "id": workload_id,
        "count": len(cur),
        "inputs": cur,
    }

    workload_id += 1

    content = json.dumps(workload, separators=(",", ":"))
    output_zip.writestr(f"{cur_i}.json", content)
    cur = []
    cur_i += 1

namelist = input_zip.namelist()
random.shuffle(namelist)

for filename in progressBar(namelist):
    content = input_zip.read(filename)
    parsed = parse_file(filename, content)

    cur.append(parsed)
    if len(cur) >= batch:
        process()

process()
output_zip.close()

# files = []
# batch = 500
# for i in range(0, len(filenames), batch):
#     files.append(filenames[i : i + batch])

# for i in range(len(files)):
#     print(f"{i} starting...")
#     workload = {
#         "id": uuid.uuid4().hex,
#         "count": 0,
#         "inputs": [],
#     }

#     with ProcessPoolExecutor(max_workers=7) as e:
#         futuresLst = [e.submit(parse_file, f) for f in files[i]]
#         for future in as_completed(futuresLst):
#             try:
#                 workload["inputs"].append(future.result())
#             except Exception as exc:
#                 print("%r generated an exception: %s" % (future, exc))

#         wait(futuresLst)
#         e.shutdown(True)

#     with open(f"workload/{i}.json", "w") as json_file:
#         json.dump(workload, json_file, separators=(",", ":"))

#     print(f"{i} ended")


import socket
from tkinter import *
from tkinter.ttk import Progressbar
from multiprocessing import Queue
import threading
from queue import Empty
import time

#iniciando socket, variaveis global e listas de pareamento
UDP_IP = socket.gethostbyname(socket.gethostname())
UDP_PORT = 5000
ender = (UDP_IP,UDP_PORT)
running1=1
running2=0
addrList = []
ipList = []
q = Queue()
#sock.settimeout(None)
#caixa de mensagens
def mensagem_pareamento(ip, estado):
        win = Toplevel()
        win.geometry('200x200')
        message = str(ip)
        message = message+estado
        Label(win, text=message, fg='red',width=200).pack()
        Button(win, text='Fechar', command=win.destroy).pack(side=BOTTOM)

def pareamento(lista1,lista2):
            global running1#,sock
            running1 = 1
            ipList = lista1
            addrList = lista2

            if(running1):
                sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                sock.bind(ender)
                q.put(sock)
                data, addr = sock.recvfrom(1024)
                #printando conexão (dados e endereço com portas)
                print ("Mensagem recebida:", data)
                print("Endereço: ",addr)
                ip = addr[0]

                if(ip in ipList):
                    mensagem_pareamento(ip,' Já foi pareado antes')
                    print('Foi pareado antes\n')

                else:
                    mensagem_pareamento(ip,' pareado')
                    sock.sendto(b"ok", addr)
                    print('Pareado')
                    ipList.append(ip)
                    addrList.append(addr)

                #printando listas de dispositivos pareados
                print('##listas##')
                print(ipList)
                print(addrList)

#janela do cronometro (classe)
class cronometro:
    def __init__(self, master):
        self.master = master
        # self.frame = Frame(self.master)
        self._start = 0.00
        self._tempo_gasto = 0.00
        self._running = 0
        self.timestr = StringVar()
        self.valor_a = ''
        self.master.geometry('500x250')
        self.desenhar_widgets()
        self.flag = 0

        #botões
        self.botao1 = Button(self.master,text='Start', command=self.Start, state=NORMAL)
        self.botao1.pack(side=LEFT)
        self.botao2 = Button(self.master, text='Parar', command=self.Stop, state=NORMAL)
        self.botao2.pack(side=LEFT)
        self.botao3 = Button(self.master, text='Resetar', command=self.Reset).pack(side=LEFT)
        self.botao4 = Button(self.master, text='Fechar', command=self.master.destroy).pack(side=LEFT)

    #métodos do cronômetro
    def desenhar_widgets(self):
        l = Label(self.master, textvariable=self.timestr,font=(None,35))
        self._setTime(self._tempo_gasto)
        l.pack(fill=X, expand=NO, pady=2, padx=2)

    def _update(self):
        self._tempo_gasto = time.time() - self._start
        self._setTime(self._tempo_gasto)
        self._timer = self.master.after(5, self._update)

    def _setTime(self, elap):
        minutos = int(elap/60)
        segundos = int(elap - minutos*60.0)
        msegundos = int((elap - minutos*60.0 - segundos)*1000)
        self.timestr.set('%02d:%02d:%03d' % (minutos, segundos, msegundos))

    def Start(self):
        global running2
        if not self._running:
            self.botao1.config(state=DISABLED)
            self.botao2.config(state=NORMAL)
            self._start = time.time() - self._tempo_gasto
            self._update()
            self._running = 1
            self.flag = 0
            running2 = 1
            self.t2 = threading.Thread(target=self.Rec_dados)
            self.t2.start()

    def Stop(self):
        if self._running:
            self.botao2.config(state=DISABLED)
            #self.botao2.config(state=NORMAL)
            self.master.after_cancel(self._timer)
            self._tempo_gasto = time.time() - self._start
            self._setTime(self._tempo_gasto)
            self._running = 0

    def Reset(self):
        self._start = time.time()
        self._tempo_gasto = 0.0
        self._setTime(self._tempo_gasto)

    def Rec_dados(self):
        global addrList,ipList#, sock
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind(ender)
        #if(not self.flag):
        for j in addrList:
            sock.sendto(b"start", j)
        for i in ipList:
            data, addr = sock.recvfrom(1024)
            ip = addr[0]
            print (data)
            if(((data == b'ok') and (ip in ipList))is True):
                tempo_atual= self.timestr.get()
                l2 = Label(self.master, text=tempo_atual, font=(None,12)).pack(side=BOTTOM)
                print ("Mensagem recebida:", data)
                print("Endereço: ",addr)
            else:
                print('nao pareado ou mensagem incorreta')

        self.botao1.config(state=NORMAL)
        self.master.after_cancel(self._timer)
        self._tempo_gasto = time.time() - self._start
        self._setTime(self._tempo_gasto)
        self._running = 0
        sock.close()
#aplicação de GUI
class Application:
    def __init__(self, master):
        self.master = master

        #frames da tela
        self.bottomFrame = Frame()
        self.bottomFrame.pack(side=BOTTOM)

        #tamanho da tela
        master.geometry('500x250')

        #botões
        self.buttom2 = Button(self.bottomFrame,fg='red',text='parar servidor', command=self.parar)
        self.buttom2.pack()
        self.teste = Button(self.bottomFrame,text='Cronometro', command=self.abrir_janela).pack(side=LEFT)
        self.buttom1 = Button(self.bottomFrame,fg='blue',text='Parear', command=self.chamar_pareamento)
        self.buttom1.pack()
        #queue variable e barra de progresso
        self.pbar = Progressbar(mode='indeterminate')
        self.pbar.pack(side=BOTTOM)

        #labels
        self.label1 = Label(text ='SpeedRun',font=(None,40))
        self.label1.pack(side=TOP)

    #métodos da classe de aplicação
    def parar(self):
        print("Setado para falso")
        global running1
        running1 = 0

    def onGetValue(self):
        global running1
        if(self.t1.is_alive()):
            print("Esperando conexão")
            self.master.after(100, self.onGetValue)
            self.buttom1.config(state=DISABLED)

        else:
            try:
                print('FIM', q.get(0))
                self.buttom1.config(state=NORMAL)
                self.pbar.stop()

            except Empty:
                print("queue está vazia")

    def chamar_pareamento(self):
        self.t1 = threading.Thread(target=pareamento,args=(ipList, addrList))
        self.t1.daemon = True
        self.t1.start()
        self.pbar.start(20)
        self.master.after(100, self.onGetValue)

    def abrir_janela(self):
        self.janela = Toplevel(self.master)
        self.nova_janela = cronometro(self.janela)

#iniciando a aplicação e rodando o loop
if __name__ == '__main__':
    root = Tk()
    app = Application(root)
    root.mainloop()


# _*_coding:utf-8_*_
# Name:Brian
# Create_time:2021/2/3 13:47
# file: utils2.py
# location:chengdu
# number:610000
import os
import logging
import time
from metrics import *
import matplotlib.pyplot as plt

def get_logger(root, name=None, debug=True):
    time_lag = time.time()
    # when debug is true, show DEBUG and INFO in screen
    # when debug is false, show DEBUG in file and info in both screen&file
    # INFO will always be in screen
    # create a logger
    logger = logging.getLogger(name)
    # critical > error > warning > info > debug > notset
    logger.setLevel(logging.DEBUG)

    # define the formate
    formatter = logging.Formatter('%(asctime)s: %(message)s', "%Y-%m-%d %H:%M:%S")
    # create another handler for output log to console
    console_handler = logging.StreamHandler()
    if debug:
        console_handler.setLevel(logging.DEBUG)
    else:
        console_handler.setLevel(logging.INFO)
        # create a handler for write log to file
        logfile = os.path.join(root, 'run_%s.log' % time_lag)
        print('Creat Log File in: ', logfile)
        file_handler = logging.FileHandler(logfile, mode='w')
        file_handler.setLevel(logging.DEBUG)
        file_handler.setFormatter(formatter)
    console_handler.setFormatter(formatter)
    # add Handler to logger
    logger.addHandler(console_handler)
    if not debug:
        logger.addHandler(file_handler)
    return logger

def train_epoch(train_loader, adj_mx, net, optimizer, epoch, logger, loss_criterion, device="cuda", log_step=10):
    """
    Trains one epoch with the given data.
    :param training_input: Training inputs of shape (num_samples, num_nodes,
    num_timesteps_train, num_features).
    :param training_target: Training targets of shape (num_samples, num_nodes,
    num_timesteps_predict).
    :param batch_size: Batch size to use during training.
    :return: Average loss for this epoch.
    """
    epoch_training_losses = []
    for batch_idx, (X_data, y_batch) in enumerate(train_loader):
        net.cuda()
        net.train()
        optimizer.zero_grad()
        y_batch = y_batch.to(device)
        X_batch = X_data[:, 0:1, :, :].permute(2, 0, 3, 1).to(device)  # T B  N C z
        mask_missing = X_data[:, 1:2, :, :].permute(2, 0, 3, 1).to(device)
        out = net(X_batch, adj_mx, mask_missing)
        del X_batch
        loss = loss_criterion(out, y_batch)
        # loss = loss_criterion(out, y_batch.to(device))
        loss.backward()
        optimizer.step()
        del out
        del y_batch
        if batch_idx % log_step == 0:
            logger.info('Train Epoch {}: {}/{} Loss: {:.6f}'.format(
                epoch, batch_idx, len(train_loader), loss.item()))
        epoch_training_losses.append(loss.detach().cpu().numpy())
    train_epoch_loss = sum(epoch_training_losses) / len(epoch_training_losses)
    logger.info('**********Train Epoch {}: averaged Loss: {:.6f}'.format(epoch, train_epoch_loss))
    del train_loader
    return train_epoch_loss

def val_epoch(val_loader, net, adj_mx, epoch, logger, loss_criterion, device="cuda", log_step=20):
    epoch_val_losses = []
    with torch.no_grad():
        for batch_idx, (X_data, y_batch) in enumerate(val_loader):
            y_batch = y_batch.to(device)
            X_batch = X_data[:, 0:1, :, :].permute(2, 0, 3, 1).to(device)  # T B  N C z
            mask_missing = X_data[:, 1:2, :, :].permute(2, 0, 3, 1).to(device)
            out = net(X_batch, adj_mx, mask_missing)
            # y_batch=y_batch.to(device)
            val_loss = loss_criterion(out, y_batch)
            del y_batch
            del out
            if batch_idx % log_step == 0:
                logger.info('val Epoch {}: {}/{} Loss: {:.6f}'.format(
                    epoch, batch_idx, len(val_loader), val_loss.item()))
            epoch_val_losses.append(val_loss.detach().cpu().numpy())
        train_epoch_loss = sum(epoch_val_losses) / len(epoch_val_losses)
        logger.info('**********val Epoch {}: averaged Loss: {:.6f}'.format(epoch, train_epoch_loss))
        del val_loader
        return train_epoch_loss

def test_all(adj_mx, test_loader, model, max_speed, logger, epochs, device="cuda"):
    model.to(device)
    model.eval()
    mae_list = []
    mape_list = []
    rmse_list = []
    with torch.no_grad():
        for batch_idx, (X_data, y_batch) in enumerate(test_loader):
            y_batch = y_batch.to(device)
            y_batch = y_batch * max_speed
            mask_missing = X_data[:, 1:2, :, :].permute(2, 0, 3, 1).to(device)
            X_batch = X_data[:, 0:1, :, :].permute(2, 0, 3, 1).to(device)  # T B  N C z
            output = model(X_batch, adj_mx, mask_missing) * max_speed

            # target_unnormalized = y_batch.detach().cpu().numpy()
            mae, rmse, mape, _, _ = All_Metrics(output, y_batch, None, 0.)
            logger.info("test_eopch_old:{}/{}:, MAE: {:.2f}, RMSE: {:.2f}, MAPE: {:.2f}%".format(
                batch_idx, len(test_loader), mae, rmse, mape * 100))

            mape_list.append(mape)
            mae_list.append(mae)
            rmse_list.append(rmse)
            # logger.info("test_eopch_all index:{}/{}, MAE: {:.2f}, RMSE: {:.2f}, MAPE: {:.2f}%".format(
            #     batch_idx, len(test_loader), mae, rmse, mape * 100))
        fig = plt.figure()
        ax1 = fig.add_subplot(111)
        plt.plot(mae_list, "b", label="mae_list")
        plt.plot(rmse_list, "g", label="rmse_list")
        ax1.set_ylabel('MSE and RMSE values')
        ax1.set_title("Result of test epochs-> %s " % epochs)
        ax1.set_ylim(0, 20)
        ax2 = ax1.twinx()  # this is the important function
        plt.plot(mape_list, "r", label="MAPE")
        ax2.set_ylabel('MAPE values')
        ax2.set_ylim(0, 0.25)
        ax1.legend()
        ax2.legend()
        plt.show()
        mae_ave = sum(mae_list) / len(mae_list)
        mape_ave = sum(mape_list) / len(mape_list)
        rmse_ave = sum(rmse_list) / len(rmse_list)
        logger.info("test_eopch average： MAE: {:.2f}, RMSE: {:.2f}, MAPE: {:.4f}%".format(
            mae_ave, rmse_ave, mape_ave * 100))


from django.urls import path
from . import views

urlpatterns=[
    path('',views.index,name='index'),
    path('register/', views.register, name="register"),
	path('login/', views.loginUser, name="login"),
    path('logout/',views.logoutUser,name="logout") ,
    path('profile/', views.profile,name='profile'),
    path('edit/',views.profile_update,name='edit'),
    path('neighbourhood/', views.create_neighbourhood,name='newneighbourhood'),
    path('businesses/<id>', views.businesses, name='hoodbusiness'),
    path('singlehood/<id>', views.singlehood, name='singlehood'),
    path('new_business/', views.newBusiness, name='newbusiness'),
    path('joinhood/<id>', views.joinhood, name='joinhood'),
    path('leavehood/<id>', views.leavehood, name='leavehood'),
    path('post', views.post, name='post'),
    path('hoodpost/<id>', views.neighbourhood_post, name='hoodpost'),

]


import sklearn.neural_network
import numpy as np

x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])

model = sklearn.neural_network.MLPClassifier(
    activation="relu", max_iter=10000, hidden_layer_sizes=(8, 2)
)
model.fit(x, y)

print("score:", model.score(x, y))
print("predictions:", model.predict(x))
print("expected:", np.array([0, 1, 1, 0]))


#!/usr/bin/env python

# Italy EPG is a script that will communicate with remote EPG provider
# and  parse through all the channels and format into XML so that it can be used
# in xTeve or Plex

from bs4 import BeautifulSoup   ### html parsing
import re                       ### text manipulation
import requests                 ### for making json requests
import json                     ### for json formatting and manipulation
import datetime as dt           ### to format the dates and times properly into xml file
import time                     ### for the sleeps

print("Starting EPG script...")

### Define variables

print("Setting variables...")
network_url = "https://remote_epg_url/"
network_image_url = "https://remote_image_url"
italy_channels = [ 'channel1', 'channel2', 'channel3' ] ### define channels
local_image_url = "http://local.url/italy/"

#######################################################################
### writexmlchannel - write channel headers to xml file
#######################################################################

def writexmlchannel(write_channel):

    f.write('    <channel id="{}">\n'.format(write_channel))
    f.write('      <display-name>{}</display-name>\n'.format(write_channel))
    f.write('      <icon src="{}{}" />\n'.format(local_image_url,write_channel))  ### channel icon located on local server
    f.write('    </channel>\n')

###############################################################################################################################
### writexmlprogramme - writes the input data into xml format into the xml epgtv file
##############################################################################################################################

def writexmlprogramme(requested_channel,iso_start_date,iso_end_date,title,stitle,desc,imagelink,ns_epnum,air_date):

    f.write('    <programme channel="{}" start="{}" stop="{}">\n'.format(requested_channel,iso_start_date,iso_end_date))
    f.write('      <title lang="it">{}</title>\n'.format(title))
    f.write('      <sub-title lang="it">{}</sub-title>\n'.format(stitle))
    if desc:
      f.write('      <desc lang="it">{}</desc>\n'.format(desc))
    if imagelink:
      f.write('      <icon src="{}"/>\n'.format(imagelink))
    if ns_epnum:
      f.write('      <episode-num system="xmltv_ns">{}</episode-num>\n'.format(ns_epnum))
    if air_date:
      f.write('      <episode-num system="original-air-date">{}</episode-num>\n'.format(air_date))
    f.write('    </programme>\n')

##########################################################################################
### dateincrementer - increment date and put into format required for xmltv
##########################################################################################

def dateincrementer(date_increment_requested):

    today = dt.datetime.now()
    today_format = today.strftime('%d-%m-%Y')
    #print("today is: ",today_format)
    date_increment = dt.datetime.now() + dt.timedelta(days=date_increment_requested)
    date_increment_format = date_increment.strftime('%d-%m-%Y')
    #print("Incremented by: {} Day is: {}".format(date_increment_requested,date_increment_format))
    return date_increment_format

#############################################################################################
### Main Loop
#############################################################################################

eventcount = 0  ### Reset counter

### Write XML Header at top of xml epgtv file
print("Opening/creating xml file...")
f = open("italy_epg.xmltv","w")
print("Writing XML header")
f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
f.write('  <tv source-info-url="" generator-info-name="Italy EPG Generator : on {}" source-info-name="Big Vs generator">\n'.format(dt.datetime.now()))

### write all the channel headers
print("Writing channel headers")
for channel in italy_channels:
  writexmlchannel(channel)

### Main program loop

print("Starting main programme parser")
for i in range(6):

  epg_date_requested = dateincrementer(i)
  print("Requested EPG date is: ",epg_date_requested)
  request_date = str(epg_date_requested)

  for channel in italy_channels:

    time.sleep(5)
    request_url = "{}{}/{}.json".format(network_url,channel,request_date)
    request_epg = requests.get(request_url)                                                                ### Grab the json data from provider
    reqjson = json.loads(request_epg.text)                                                                      ### Put json data into variable/json list
    events = reqjson['events']                     ### events are the tv programmes

    for event in events:                           ### parse individual programmes

      eventcount = eventcount + 1                  ### reset counters
      info_grab_errors = 0

      ### Grab program name
      namegrab = event['program']
      namegrab = namegrab['name']
      if namegrab:  ### if program name exists
        namegrab = re.sub(r'&',r'and',namegrab)
        title=namegrab
      else:
        title=""                               ### no program name provided

      episode = event['episode_title']
      if episode:
        episode = re.sub(r'&',r'and',episode)
        stitle=episode
      else:
        stitle=""

      startdate = event['date']
      if not startdate:
        info_grab_errors = info_grab_errors + 1     ### error out if no start time exists

      starttime = event['hour']
      if not starttime:
        info_grab_errors = info_grab_errors + 1      ### error if no start hour exists
      duration = event['duration']
      if not duration:
        info_grab_errors = info_grab_errors + 1      ### error if no duration exists

      ### Calculate start time ######################################

      mytime = dt.datetime.strptime(starttime,'%H:%M').time() # Convert time string to time object
      mydate = dt.datetime.strptime(startdate,'%d/%m/%Y') #convert date string to date object
      combinedstarttime = dt.datetime.combine(mydate,mytime) #combine date and time together

      timezone="+0200" ###Italy Time Zone
      isodt=combinedstarttime.strftime('%Y%m%d%H%M%S') ### Convert date string to ISO for XMLTV

      ### Convert datetime object to string to paste into XMLTV file
      strdate=str(isodt) #Convert back to string and add timezone
      iso_start_time=strdate+" "+ timezone

      ### Figure out end time #########################################

      duration_time = dt.datetime.strptime(duration,'%H:%M:%S').time() #Convert duration time string to time object

      duration_add_hour = int(duration_time.strftime('%H')) #Strip out Hour and convert to int to use with timedelta
      duration_add_minutes = int(duration_time.strftime('%M'))

      duration_add = combinedstarttime + dt.timedelta(hours=duration_add_hour)
      duration_add = duration_add + dt.timedelta(minutes=duration_add_minutes)

      iso_end_time=duration_add.strftime('%Y%m%d%H%M%S')
      iso_end_time=str(iso_end_time)
      iso_end_time=iso_end_time+" "+timezone   ### Final end time, formatted to ISO and in proper time zone

      ################################
      ### Parse program description
      ################################

      description = str(event['description'])
      if description:
        description = re.sub(r'&',r'and',description)
      else:
        pass      ### No description provided

      sforcheck = False
      episodeforcheck = False

      ### Parse episode number

      episodenumber = event['episode']
      if episodenumber:
        if str.isdecimal(episodenumber):
          ns_episode = int(episodenumber)
          ns_episode = ns_episode - 1
          episodeforcheck = True

      ### Parse season number

      season = event['season']
      if season:
        if str.isdecimal(season):
            sforcheck = True
            ns_season = int(season)
            ns_season = ns_season - 1

      ns_epnum = ""
      air_date = ""

      if sforcheck == True and episodeforcheck == True:

        ns_epnum = "{}.{}.".format(ns_season,ns_episode)

      elif "Puntata del" in stitle:  ### If exists an original air date, use that for season or episode number

        punt = re.sub(r' ','',stitle)
        punt = re.sub(r'Puntatadel','',punt)

        puntsearch = re.search('[0-9]{2}/[0-9]{2}/[0-9]{4}',punt)

        if puntsearch is not None:  ### Found an air date, start to parse
          punt = puntsearch.group()

          punt_date = dt.datetime.strptime(punt,'%d/%m/%Y')
          comb_punt_time = dt.datetime.combine(punt_date,mytime)
          comb_fpunt_time = comb_punt_time.strftime('%Y-%m-%d %H:%M')
          air_date = comb_fpunt_time
        else:
          air_date = ""

      else:
        pass        ### No series or episode information provided

      ### Check for a show thumbnail

      image = event['image']
      if image:
          imagelink = str(network_image_url+image)
      else:
          imagelink = ""

      requested_channel = channel ### Use already provided channel number/name

      ### If there were no errors above, proceed to write data to XML file

      if info_grab_errors == 0:
        writexmlprogramme(requested_channel,iso_start_time,iso_end_time,title,stitle,description,imagelink,ns_epnum,air_date)
      else:
        pass ### Errors parsing program
        #print("Failed to find critical information, not writing to xml") ### Program found errors, not writing program to xml

      ### Finished writing programme

#############################################
### Finished writing channels and programs
#############################################
print("Finished parsing. Programmes added: ",eventcount)          ### Give number of found programs

### Close the output xml file

f.write('  </tv>\n')
f.close()

### Push Results via MQTT ###

network = "Italy"

### If using MQTT, uncomment the below 2 lines

### from pushresults import PushResultsMqtt
###PushResultsMqtt(network,eventcount)

### If using xTeve, Force XTeve to update M3U file, which is pointed at localhost, otherwise comment out below lines
apiurl="http://xteve.lan:34400/api/"
headers = {'Content-type': 'application/json'}

print("Pushing JSON POST update to XTeve")
r = requests.post(apiurl, json={"cmd": "login"}, headers=headers)
t = requests.post(apiurl, json={"cmd": "update.xmltv"}, headers=headers)
datares=json.loads(t.text)

if datares['status'] == True:
     print("POST Successful")

### Done


__author__ = 'Harsh'
import pandas as pd
import numpy as np
import operator
from sklearn.feature_extraction import DictVectorizer

def category_to_dictionary(cats):
    final_dict = {}
    for x in cats:
        if x in frequent_categories:
            final_dict.setdefault(x, 1)
    return final_dict

def processBusiness(business_frame):

    columns_to_drop = ['full_address','latitude','longitude','name','neighborhoods','state','type']
    business_frame = business_frame.drop(columns_to_drop, axis=1)

    #Identify the categories in each business
    category_list = business_frame.categories.fillna("").map(lambda x: str.split(x, ",")).values

    #Identifying frequent categories by checking how many times it occurs in each review
    ##We can take first 20 or 40 such categories as our features
    frequent_categories = pd.Series([category for categories in category_list for category in categories]).value_counts().ix[1:40].index
    dictionary_vector = DictVectorizer()

    category_features = dictionary_vector.fit_transform(business_frame.categories.fillna("").map(lambda x: category_to_dictionary(str.split(x, ","))).values).toarray()
    category_frame = pd.DataFrame(category_features, index = business_frame.index, columns = dictionary_vector.feature_names_)

    # Combining the extracted features
    business_frame = business_frame.combine_first(category_frame)

    # Finding the which city occurs more time by finding the value counts of it
    city_frequency = business_frame.city.value_counts()
    city_frequency_dictionary = dict(city_frequency)

    # finding the most frequent city by identifying the cities that have count > 200 (200 was decided by experimenting with different values)
    frequent_city_200 =  [(k,v) for k,v in city_frequency_dictionary.items() if v > 200]
    frequent_city_200 = dict(frequent_city_200)
    sorted_frequency_city_200 = sorted(frequent_city_200.items(), key = operator.itemgetter(1), reverse= True)
    sorted_frequency_city_200 = [str(city[0]) for city in sorted_frequency_city_200]

    # Sorted cities based on the count
    business_frame['city'] = business_frame.city.map(lambda test: test if test in sorted_frequency_city_200 else 'Other' )
    business_frame = business_frame.drop(['categories'], axis= 1)
    return business_frame

def main():

    # Load the business from python pandas
    business_frame_train = pd.read_csv('../yelp_training_set/yelp_training_set_business.csv', header = 0, index_col = 'business_id')
    business_frame_test = pd.read_csv('../yelp_test_set/yelp_test_set_business.csv', header = 0, index_col = 'business_id')

    business_frame_final = business_frame_train.combine_first(business_frame_test)
    business_frame_final = business_frame_final.drop_duplicates()

    business_frame_processed = processBusiness(business_frame_final)

    # Save the returned final business frame into csv
    business_frame_processed.to_csv('E:\Fall 2014\Social Media Mining\Processed Features\Business_features.csv')

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
# Copyright (c) 2018-2021, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
import numpy as np
import pandas as pd
import pytz
from pandas._testing import assert_frame_equal

from wetterdienst.provider.eccc.observation import EcccObservationRequest

def test_eccc_api_stations():
    request = EcccObservationRequest(
        parameter="DAILY",
        resolution="DAILY",
        start_date="1990-01-01",
        end_date="1990-01-02",
        humanize=True,
        tidy=True,
        si_units=False,
    ).filter_by_station_id(station_id=(14,))

    expected = pd.DataFrame(
        {
            "station_id": ["14"],
            "from_date": [pd.Timestamp("1984-01-01", tz=pytz.UTC)],
            "to_date": [pd.Timestamp("1996-01-01", tz=pytz.UTC)],
            "height": [4.0],
            "latitude": [48.87],
            "longitude": [-123.28],
            "name": ["ACTIVE PASS"],
            "state": ["BRITISH COLUMBIA"],
        }
    )

    assert_frame_equal(request.df, expected)

def test_eccc_api_values():
    request = EcccObservationRequest(
        parameter="DAILY",
        resolution="DAILY",
        start_date="1980-01-01",
        end_date="1980-01-02",
        humanize=True,
        tidy=True,
        si_units=False,
    ).filter_by_station_id(station_id=(1652,))

    values = request.values.all().df

    expected_df = pd.DataFrame(
        {
            "station_id": pd.Categorical(["1652"] * 22),
            "dataset": pd.Categorical(["daily"] * 22),
            "parameter": pd.Categorical(
                [
                    "temperature_air_max_200",
                    "temperature_air_max_200",
                    "temperature_air_min_200",
                    "temperature_air_min_200",
                    "temperature_air_mean_200",
                    "temperature_air_mean_200",
                    "ndays_heating_degree",
                    "ndays_heating_degree",
                    "ndays_cooling_degree",
                    "ndays_cooling_degree",
                    "precipitation_height_liquid",
                    "precipitation_height_liquid",
                    "snow_depth_new",
                    "snow_depth_new",
                    "precipitation_height",
                    "precipitation_height",
                    "snow_depth",
                    "snow_depth",
                    "wind_direction_gust_max",
                    "wind_direction_gust_max",
                    "wind_gust_max",
                    "wind_gust_max",
                ]
            ),
            "date": [
                pd.Timestamp("1980-01-01", tz=pytz.UTC),
                pd.Timestamp("1980-01-02", tz=pytz.UTC),
            ]
            * 11,
            "value": [
                -16.3,
                -16.4,
                -29.1,
                -28.3,
                -22.7,
                -22.4,
                40.7,
                40.4,
                0.0,
                0.0,
                0.0,
                0.0,
                1.8,
                0.0,
                0.8,
                0.0,
                19.0,
                20.0,
                np.NaN,
                np.NaN,
                np.NaN,
                np.NaN,
            ],
            "quality": pd.Series([np.NaN] * 22, dtype=float),
        }
    )

    assert_frame_equal(
        values.reset_index(drop=True), expected_df, check_categorical=False
    )


from flask import session, render_template, Blueprint, g, request
from models.blog import Blog
from models.comment import Comment
from models.user import User
from models.role import Permission
from routes import *
from routes.decorators import render, permission_required,admin_required,login_required

main = Blueprint('blog', __name__)

Model = Blog

@main.route('/', methods=['GET', 'POST'])
@main.route('/index/<int:page>', methods=['GET', 'POST'])
def index(page=1):
    blog_list = Model.query.order_by(Model.timestamp.desc()).paginate(page, per_page=10, error_out=False)
    # cur_user = User.query.get(session['user_id'])
    # print(cur_user)
    return render('blog/blog_index.html', paginaties = blog_list.items)

@main.route('/article/<blog_id>')
def article(blog_id):
    blog = Model.query.get(blog_id)
    return render('blog/blog_content.html', blog = blog)

@main.route('/board')
@main.route('/board/<category>')
def board(category=''):
    if category:
        blog_all = Model.query.filter_by(category=category).all()
    else:
        blog_all = Model.query.order_by(Model.timestamp.desc())
    return render('blog/board.html', blogs = blog_all )


from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import GradientBoostingClassifier

cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = \
train_test_split(cancer.data, cancer.target, \
                 stratify=cancer.target, random_state=0)

gbrt = GradientBoostingClassifier(random_state=0)
gbrt.fit(X_train, y_train)

print('Правильность на обучающем наборе: {:.3f}'.\
format(gbrt.score(X_train, y_train)))
print('Правильность на тестовом наборе: {:.3f}'.\
format(gbrt.score(X_test, y_test)))

gbrt_depth = GradientBoostingClassifier(random_state=0, max_depth=1)
gbrt_depth.fit(X_train, y_train)

print('Правильность на обучающем наборе (max_depth=1): {:.3f}'.\
format(gbrt_depth.score(X_train, y_train)))
print('Правильность на тестовом наборе (max_depth=1): {:.3f}'.\
format(gbrt_depth.score(X_test, y_test)))

gbrt_rate = GradientBoostingClassifier(random_state=0, learning_rate=0.01)
gbrt_rate.fit(X_train, y_train)

print('Правильность на обучающем наборе (learning_rate=0.01): {:.3f}'.\
format(gbrt_rate.score(X_train, y_train)))
print('Правильность на тестовом наборе (learning_rate=0.01): {:.3f}'.\
format(gbrt_rate.score(X_test, y_test)))


# Copyright 2020 Daniel Williams.
# Contains code contributions by the Google AI Language Team, HuggingFace Inc.,
# NVIDIA CORPORATION, authors from the University of Illinois at Chicago, and
# authors from the University of Parma and Adidas AG.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from transformers.modeling_albert import AlbertPreTrainedModel, AlbertModel
import torch
from torch.autograd import grad
import math

class AlbertForABSA(AlbertModel):
    def __init__(self, config, num_labels=3, dropout=None, epsilon=None, od=1,use_relu=0):
        super(AlbertForABSA, self).__init__(config)
        self.num_labels = num_labels
        self.epsilon = epsilon
        # self.dropout = torch.nn.Dropout(dropout)
        # self.relu = torch.nn.ReLU()
        # self.od1 = math.floor(2.5*config.hidden_size)
        # self.od2 = math.floor(2*config.hidden_size)
        # self.use_relu = use_relu
        # self.preclass = torch.nn.Linear(config.hidden_size, self.od1)
        # self.preclass2 = torch.nn.Linear(self.od1, self.od2)
        self.classifier = torch.nn.Linear(config.hidden_size, num_labels)
        self.loss_fct = torch.nn.CrossEntropyLoss(ignore_index=-1)
        self.init_weights()

    def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None):
        sequence_output, albert_emb = self.albert_forward(input_ids,
                                                token_type_ids,
                                                attention_mask,
                                                output_hidden_states=False)
        # sequence_output = self.dropout(sequence_output)

        # sequence_output = self.preclass(sequence_output) ########
        # if self.use_relu:
        #     sequence_output = self.relu(sequence_output)
        # sequence_output = self.dropout(sequence_output)

        # sequence_output = self.preclass2(sequence_output) ########
        # if self.use_relu:
        #     sequence_output = self.relu(sequence_output)
        # sequence_output = self.dropout(sequence_output)

        logits = self.classifier(sequence_output)
        if labels is not None:
            _loss = self.loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
            if sequence_output.requires_grad: #if training mode
                perturbed_sentence = self.adv_attack(albert_emb, _loss, self.epsilon)
                # perturbed_sentence = self.replace_cls_token(albert_emb, perturbed_sentence) #
                adv_loss = self.adversarial_loss(perturbed_sentence, attention_mask, labels)
                return _loss, adv_loss
            return _loss
        else:
            return logits

    def adv_attack(self, emb, loss, epsilon):
        loss_grad = grad(loss, emb, retain_graph=True)[0]
        loss_grad_norm = torch.sqrt(torch.sum(loss_grad**2, (1,2)))
        perturbed_sentence = emb + epsilon * (loss_grad/(loss_grad_norm.reshape(-1,1,1)))
        return perturbed_sentence

    def adversarial_loss(self, perturbed, attention_mask, labels):

        extended_attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)
        extended_attention_mask = extended_attention_mask.to(dtype=next(self.parameters()).dtype) # fp16 compatibility
        extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0
        encoded_layers = self.encoder(perturbed, extended_attention_mask)
        encoded_layers_last = encoded_layers[-1]
        # encoded_layers_last = self.dropout(encoded_layers_last)

        # encoded_layers_last = self.preclass(encoded_layers_last) ########
        # if self.use_relu:
        #     encoded_layers_last = self.relu(encoded_layers_last)
        # encoded_layers_last = self.dropout(encoded_layers_last)

        # encoded_layers_last = self.preclass2(encoded_layers_last) ########
        # if self.use_relu:
        #     encoded_layers_last = self.relu(encoded_layers_last)
        # encoded_layers_last = self.dropout(encoded_layers_last)

        logits = self.classifier(encoded_layers_last)
        loss_fct = torch.nn.CrossEntropyLoss(ignore_index=-1)
        adv_loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
        return adv_loss

    def albert_forward(self, input_ids, token_type_ids=None, attention_mask=None, output_hidden_states=False):
        if attention_mask is None:
            attention_mask = torch.ones_like(input_ids)
        if token_type_ids is None:
            token_type_ids = torch.zeros_like(input_ids)
        extended_attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)
        extended_attention_mask = extended_attention_mask.to(dtype=next(self.parameters()).dtype) # fp16 compatibility
        extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0
        embedding_output = self.embeddings(input_ids, token_type_ids)
        encoded_layers = self.encoder(hidden_states=embedding_output, attention_mask=extended_attention_mask)
        sequence_output = encoded_layers[-1]

        return sequence_output, embedding_output


import sys

from lib.parsers.instagram import instagram
from lib.parsers.facebook import facebook
from lib.parsers.twitter import twitter
from lib.parsers.snapchat import snapchat
from lib.parsers.linkedIn import linkedIn
from lib.parsers.gmail import gmail
from lib.parsers.discord import discord
from lib.parsers.web_host_app import web_host_app
from lib.colors import style

# Generate email templates based on users option
def generate_email():
    print (u"{}[2J{}[;H".format(chr(27), chr(27)))     # Clear the terminal
    template_id = 0
    templates = ['Facebook', 'Instagram', 'Twitter',
                'Snapchat', 'LinkedIn', 'Gmail',
                'Discord', '000WebHost']
    print(style.RESET("        -- Choose your phishing page --\n"))
    for template in templates:
        template_id += 1
        print(style.GREEN(f'[{template_id}]') + style.RESET(f' {template} Template.'))
    try:
        template_option = int(input(style.YELLOW('\n[+]') + style.RESET(' Enter template ID: ')))
    except:
        print(style.RED('\n[!]') + style.RESET(' Wrong input, exiting...'))
        sys.exit()

    if template_option == "9":
        print (u"{}[2J{}[;H".format(chr(27), chr(27)))     # Clear the terminal
        sys.exit()
    else:
        templates = [facebook, instagram, twitter,
                    snapchat, linkedIn, gmail,
                    discord, web_host_app]

        template = int(template_option) - 1
        templates[template]()
        print (u"{}[2J{}[;H".format(chr(27), chr(27)))     # Clear the terminal
        print(style.GREEN('[+]') + style.RESET(' The page has been generated and saved in Templates/Generated_Emails.'))
        input(style.YELLOW('[*]') + style.RESET(' Press anything to continue.'))


import copy

from aiohttp import web
from apispec import APISpec, Path

from .utils import get_path, get_path_keys

PATHS = {'get', 'put', 'post', 'delete', 'patch'}

class AiohttpApiSpec:
    def __init__(self, url='/api/docs/api-docs', **kwargs):
        self.spec = APISpec(**kwargs)
        if 'apispec.ext.marshmallow' not in self.spec.plugins:
            self.spec.setup_plugin('apispec.ext.marshmallow')
        self.url = url

    def swagger_dict(self):
        return self.spec.to_dict()

    def register(self, app: web.Application):
        for route in app.router.routes():
            view = route.handler
            method = route.method.lower()
            if hasattr(view, '__apispec__'
                       ) and view.__apispec__['docked'].get(method) is not True:
                url_path = get_path(route)
                if url_path:
                    if not view.__apispec__['docked'].get('parameters'):
                        view.__apispec__['parameters'].extend({"in": "path",
                                                               "name": path_key,
                                                               "required": True,
                                                               "type": "string"}
                                                              for path_key in
                                                              get_path_keys(url_path) if path_key)
                        view.__apispec__['docked']['parameters'] = True
                    self._update_paths(view.__apispec__, method, url_path)
                view.__apispec__['docked'][method] = True
        app['swagger_dict'] = self.spec.to_dict()

        def swagger_handler(request):
            return web.json_response(request.app['swagger_dict'])

        app.router.add_routes([web.get(self.url, swagger_handler)])

    def _update_paths(self, data: dict, method, url_path):
        operations = copy.deepcopy(data)
        operations.pop('docked', None)

        if method in PATHS:
            self.spec.add_path(Path(path=url_path, operations={method: operations}))


# q7.py
#
# ===============================================
# Problem
# ===============================================
# A babysitter charges $2.50 an hour until 9:00 PM when the rate drops to
# $1.75 an hour (the children are in bed). Write a program that accepts a
# starting time and ending time in hours and minutes and calculates the total
# babysitting bill. You may assume that the starting and ending times are in
# a single 24-hour period. Partial hours should be appropriately prorated.

from datetime import datetime, time, timedelta

def returnHours(start, end):
  return (start.hour - end.hour) + (1 - ((end.minute - start.minute) / 60)) - 1

def main():
  # Program description
  print("Babysitter Time Calculator")

  startTime = input("Enter the starting time (HH:mm): ")
  endTime = input("Enther the ending time (HH:mm): ")

  normalRate = 2.5
  rateDrop = 1.75

  rateDropTime = datetime.strptime("21:00", "%H:%M")

  formattedStartTime = datetime.strptime(startTime, "%H:%M")
  formattedEndTime = datetime.strptime(endTime, "%H:%M")

  if (formattedEndTime > rateDropTime):
    bill = (returnHours(formattedEndTime, rateDropTime) * rateDrop) + (returnHours(rateDropTime, formattedStartTime) * normalRate)
  else:
    bill = returnHours(formattedEndTime, formattedStartTime) * normalRate

  print("The total bill is ${0:0.2f}".format(bill))

main()


from django.urls import get_resolver
from rest_framework import serializers
from rest_framework.utils.field_mapping import ClassLookupDict

default_style = ClassLookupDict({
    serializers.Field: {
        'tag': 'input',
        'input_type': 'text'
    },
    serializers.EmailField: {
        'tag': 'input',
        'input_type': 'email'
    },
    serializers.URLField: {
        'tag': 'input',
        'input_type': 'url'
    },
    serializers.IntegerField: {
        'tag': 'input',
        'input_type': 'number'
    },
    serializers.FloatField: {
        'tag': 'input',
        'input_type': 'number'
    },
    serializers.DateTimeField: {
        'tag': 'input',
        'input_type': 'datetime-local'
    },
    serializers.DateField: {
        'tag': 'input',
        'input_type': 'date'
    },
    serializers.TimeField: {
        'tag': 'input',
        'input_type': 'time'
    },
    serializers.FileField: {
        'tag': 'input',
        'input_type': 'file'
    },
    serializers.BooleanField: {
        'tag': 'checkbox'
    },
    serializers.ChoiceField: {
        'tag': 'select',  # Also valid: 'radio'
    },
    serializers.MultipleChoiceField: {
        'tag': 'select_multiple',  # Also valid: 'checkbox_multiple'
    },
    serializers.RelatedField: {
        'tag': 'select',  # Also valid: 'radio'
    },
    serializers.ManyRelatedField: {
        'tag': 'select multiple',  # Also valid: 'checkbox_multiple'
    },
    serializers.Serializer: {
        'tag': 'fieldset'
    },
    serializers.ListSerializer: {
        'tag': 'list_fieldset'
    },
    serializers.ListField: {
        'tag': 'list_field'
    },
    serializers.DictField: {
        'tag': 'dict_field'
    },
    serializers.FilePathField: {
        'tag': 'input',
        'input_type': 'file',
    },
    serializers.JSONField: {
        'tag': 'textarea',
    },
})

def _vue_form_generator(viewset):
    if isinstance(viewset, serializers.Serializer):
        serializer = viewset
        list_url = None
        retrieve_url = None
    else:
        list_url = [url for callback, url in get_resolver().reverse_dict.items() if
                    getattr(callback, 'cls', None) == viewset and 'create' in getattr(callback, 'actions', {}).values()]
        list_url = list_url and list_url[0][0][0][0]
        retrieve_url = [url for callback, url in get_resolver().reverse_dict.items() if
                        getattr(callback, 'cls', None) == viewset and 'update' in getattr(callback, 'actions',
                                                                                {}).values()]
        retrieve_url = retrieve_url and retrieve_url[0][0][0][0].rsplit('/', 2)[0]
        serializer = viewset().get_serializer_class()
    model_name = serializer.Meta.model._meta.model_name
    component_name = f'{model_name.title()}Form'
    yield """<template>

      Form has errors

    <form @submit.prevent="submit">

    """
    for name, field in serializer().fields.items():
        style = default_style[field]
        tag = style['tag']
        input_type = ('input_type' in style) and f' type="{style["input_type"]}"' or ''
        if field.read_only:
            tag = 'input'
            input_type = ' type="hidden"'
        else:
            yield f"""

              {field.label}"""
        yield f"""<{tag}{input_type} name="{name}" v-model="form.{name}"{'' if hasattr(field, 'iter_options') else '/'}>"""
        if hasattr(field, 'iter_options'):
            yield f"""<option :value="k" :text="v" v-for="(v, k) in form.{name}_options" :key="k"></option>"""
            yield f"""</{tag.split()[0]}>"""
        if not field.read_only:
            yield """\n
"""

    yield f"""

        <button type="submit" class="button">
          Submit
        </button>

    </form>

  </template>
    """
#     yield """"""

def generate_vue_form(viewset):
    return '\n'.join(_vue_form_generator(viewset))


import torch
import torch.nn as nn
from torch.utils import model_zoo
from torchsummary import summary
import torch.optim as optim
import torchvision.models as models

__all__ = ['FCResNet', 'FCResnet50']

model_urls = {
    'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
}  #

class BilinearConvTranspose2d(nn.ConvTranspose2d):
    """A conv transpose initialized to bilinear interpolation."""

    def __init__(self, channels, stride, groups=1):
        """Set up the layer.
        Parameters
        ----------
        channels: int
            The number of input and output channels
        stride: int or tuple
            The amount of upsampling to do
        groups: int
            Set to 1 for a standard convolution. Set equal to channels to
            make sure there is no cross-talk between channels.
        """
        if isinstance(stride, int):
            stride = (stride, stride)

        assert groups in (1, channels), "Must use no grouping, " + \
            "or one group per channel"

        kernel_size = (2 * stride[0] - 1, 2 * stride[1] - 1)
        padding = (stride[0] - 1, stride[1] - 1)
        super().__init__(
            channels, channels,
            kernel_size=kernel_size,
            stride=stride,
            padding=padding,
            groups=groups,
            output_padding=1)

    def reset_parameters(self):
        """Reset the weight and bias."""
        nn.init.constant(self.bias, 0)
        nn.init.constant(self.weight, 0)
        bilinear_kernel = self.bilinear_kernel(self.stride)
        for i in range(self.in_channels):
            if self.groups == 1:
                j = i
            else:
                j = 0
            self.weight.data[i, j] = bilinear_kernel

    @staticmethod
    def bilinear_kernel(stride):
        """Generate a bilinear upsampling kernel."""
        num_dims = len(stride)

        shape = (1,) * num_dims
        bilinear_kernel = torch.ones(*shape)

        # The bilinear kernel is separable in its spatial dimensions
        # Build up the kernel channel by channel
        for channel in range(num_dims):
            channel_stride = stride[channel]
            kernel_size = 2 * channel_stride - 1
            # e.g. with stride = 4
            # delta = [-3, -2, -1, 0, 1, 2, 3]
            # channel_filter = [0.25, 0.5, 0.75, 1.0, 0.75, 0.5, 0.25]
            delta = torch.arange(1 - channel_stride, channel_stride)
            channel_filter = (1 - torch.abs(delta / channel_stride))
            # Apply the channel filter to the current channel
            shape = [1] * num_dims
            shape[channel] = kernel_size
            bilinear_kernel = bilinear_kernel * channel_filter.view(shape)
        return bilinear_kernel

# define 3x3 convolutional layer
def conv3x3(in_planes, out_planes, stride=1):
    """3x3 convolution with padding"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)

# define residual block
class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, inplanes, outplanes, stride=1, downsample=None):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(inplanes, outplanes, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(outplanes)
        self.conv2 = nn.Conv2d(outplanes, outplanes, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(outplanes)
        self.conv3 = nn.Conv2d(outplanes, outplanes * self.expansion, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(outplanes * self.expansion)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

# ResNet
class FCResNet(nn.Module):
    def __init__(self, block, layers, num_classes=2):
        self.inplanes = 64
        super(FCResNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.fusion1 = nn.Conv2d(128 * block.expansion, 2, kernel_size=1, bias=False)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.fusion2 = nn.Conv2d(256 * block.expansion, 2, kernel_size=1, bias=False)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.fusion3 = nn.Conv2d(512 * block.expansion, 2, kernel_size=1, bias=False)
        self.deconv1 = BilinearConvTranspose2d(2, 2)
        self.deconv2 = BilinearConvTranspose2d(2, 2)
        self.deconv3 = BilinearConvTranspose2d(2, 8)

        for m in self.modules():  # modules: store all the layers in self
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')  # initializa the weight/kernel
            elif isinstance(m, nn.BatchNorm2d):  # mean->0, variance->1
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

    def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(planes * block.expansion)
            )
        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        fuse_1 = self.fusion1(x)
        x = self.layer3(x)
        fuse_2 = self.fusion2(x)
        x = self.layer4(x)
        x = self.fusion3(x)
        x = self.deconv1(x)
        x = self.deconv2(x + fuse_2)
        x = self.deconv3(x + fuse_1)
        return x

def FCResnet50(pretrained=False, **kwargs):
    """Constructs a ResNet-50 model.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = FCResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
    if pretrained:
        resnet50 = models.resnet50(pretrained=True)
        pretrained_dict = resnet50.state_dict()
        model_dict = model.state_dict()
        pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
        model_dict.update(pretrained_dict)
        model.load_state_dict(model_dict)
    return model


import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
import pandas as pd
import os
import sys
from torch.autograd import Variable
from numpy import array

device = ''
predict_data = []
predict_x = []
predict_z = []
doTraining = True
doPrediction = True
if __name__ == "__main__":
    if len(sys.argv) >= 4:
        device = str(sys.argv[1])
        doTraining = sys.argv[2].lower() == 'true'
        doPrediction = sys.argv[3].lower() == 'true'

        if doPrediction and len(sys.argv) >= 5:
            args = sys.argv[4].strip('[]').split(',')
            if len(args) == 2:
                weather = int(args[0])
                time = int(args[1])

                predict_time = array([(time)], dtype='int64')
                predict_weather = array([(weather)], dtype='int64')

                predict_x.append(predict_time)
                predict_z.append(predict_weather)
            elif doPrediction and len(sys.argv) < 5:
                print('Lenght of PredictionData must be 2. Yours is ', len(args))
                quit()
        elif doPrediction:
            print('USAGE: <DeviceName> <DoTraining> <DoPrediction> <PredictionData>')
            quit()
    else:
        print('USAGE: <DeviceName> <DoTraining> <DoPrediction> [<PredictionData>]')
        quit()

x_list = []
z_list = []
target_list = []
x_data = []
y_data = []
z_data = []
train_data_path = 'AI/data/' + device + '.csv'
modelPath = 'AI/models/' + device + '.pt'

if doTraining | doPrediction:
    for weather, time in zip(
        pd.read_csv(train_data_path, sep=',', chunksize=1, usecols=['Weather']),
        pd.read_csv(train_data_path, sep=',', chunksize=1, usecols=['Time'])
    ):
        weather = weather.values
        time = time.values

        x_list.append(time)
        z_list.append(weather)
    for target in pd.read_csv(train_data_path, sep=',', chunksize=1, usecols=['State']):
        target = target.values
        target_list.append(target)

    for x, z, target in zip(x_list, z_list, target_list):
        x_data.append(x)
        y_data.append(target)
        z_data.append(z)

x, y, z = Variable(torch.Tensor(x_data)), Variable(torch.Tensor(y_data)), Variable(torch.Tensor(z_data))

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.hidden1 = torch.nn.Linear(1, 200)
        self.relu1 = torch.nn.ReLU()
        self.hidden2 = torch.nn.Linear(200, 400)
        self.relu2 = torch.nn.ReLU()
        self.hidden3 = torch.nn.Linear(400, 150)
        self.relu3 = torch.nn.ReLU()
        self.prediction = torch.nn.Linear(150, 1)

    def forward(self, x):
        res = self.hidden1(x)
        res = self.relu1(res)
        res = self.hidden2(res)
        res = self.relu2(res)
        res = self.hidden3(res)
        res = self.relu3(res)
        res = torch.sigmoid(self.prediction(res))
        return res

model = Model()
if os.path.isfile(modelPath):
    checkpoint = torch.load(modelPath)
    model.load_state_dict(checkpoint['state_dict'])

optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = torch.nn.BCELoss(reduction='mean')

def train(epoch):
    model.train()
    data = x + z
    target = y

    data = (data - data.mean()) / (data.std(unbiased=False) + 1)

    prediction = model(data)
    loss = criterion(prediction, target)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    torch.save({'state_dict': model.state_dict()}, modelPath)
    print('EPOCH: ', epoch, ' LOSS:', loss.data.numpy())

if doTraining:
    for epoch in range(5000):
        train(epoch + 1)

if doPrediction:
    model.eval()
    traindata = x + z
    data = Variable(torch.Tensor(predict_x) + torch.Tensor(predict_z))
    data = (data - traindata.mean()) / (traindata.std(unbiased=False) + 1)
    out = model(data)
    print(out.view(-1).data.numpy())
    result = int(torch.round(out.view(-1).data).numpy())

    if result < 0:
        result = 0

    print(result)
    sys.exit(result)

quit()


from math import floor

from .config import Config

def reverse_range(object):
  """Yields reverse range of object: list(reverse_range([1, 2, 3])) -> [2, 1, 0]."""
  return range(len(object) - 1, -1, -1)

def dotted_name(names):
  """Returns a dotted name of a list of strings, integers, lists, and tuples."""

  # It will just return the value instead of "b.a.d" if input was "bad"!
  if isinstance(names, str):
    return names
  elif isinstance(names, int):
    return str(names)

  resolved = []
  for name in names:
    if isinstance(name, str):
      resolved.append(name)
    elif isinstance(name, int):
      resolved.append(str(name))
    elif isinstance(name, list) or isinstance(name, tuple):
      resolved += name
    else:
      assert False
  return ".".join(resolved)

class InvalidVersionException(BaseException):
  pass

def float_version(f):
  """Converts a float X.Y into (X, Y)."""
  assert(not f < 0)
  major = floor(f)
  minor = int((f - major) * 10)
  return (major, minor)

def combine_versions(list1, list2):
  assert len(list1) == len(list2)
  assert len(list1) == 2
  if not Config.get().ignore_incomp() and\
    ((list1[0] is None and list1[1] is not None and list2[0] is not None and list2[1] is None) or
     (list1[0] is not None and list1[1] is None and list2[0] is None and list2[1] is not None)):
    raise InvalidVersionException("Versions could not be combined: {} and {}".format(list1, list2))
  res = []

  # Convert integers and floats into version tuples.
  def fixup(v):
    if isinstance(v, int):
      return (v, 0)
    elif isinstance(v, float):
      return float_version(v)
    return v

  for i in range(len(list1)):
    v1 = fixup(list1[i])
    v2 = fixup(list2[i])
    if v1 == 0 and v2 == 0:
      res.append(0)
    elif v1 == 0:
      res.append(v2)
    elif v2 == 0:
      res.append(v1)
    elif v1 is None or v2 is None:
      res.append(None)
    else:
      res.append(max(v1, v2))
  return res

def version_strings(vers):
  res = []
  for i in range(len(vers)):
    ver = vers[i]
    # When versions aren't known, show something instead of nothing. It might run with any
    # version.
    if ver == 0 or ver == (0, 0):
      res.append("~{}".format(i + 2))
    elif ver is None:
      res.append("!{}".format(i + 2))
    else:
      res.append(dotted_name(ver))
  return ", ".join(res)


#!/usr/bin/python3
"""
Collection of tests for Base class.
"""
import contextlib
from io import StringIO
import unittest
from models.base import Base
from models.rectangle import Rectangle
from models.square import Square

class BaseTest(unittest.TestCase):
    """Test Base methods."""

    def setUp(self):
        """Set up Base class tests."""
        Base._Base__nb_objects = 0

    def tearDown(self):
        """Tidy up after test methods."""
        pass

    def test_type(self):
        """Test type."""
        b1 = Base()
        self.assertTrue(type(b1) == Base)

    def test_id(self):
        """Test id."""
        b1 = Base()
        b2 = Base()
        b3 = Base()
        b4 = Base(12)
        b5 = Base(-1)
        self.assertEqual(b1.id, 1)
        self.assertEqual(b2.id, 2)
        self.assertEqual(b3.id, 3)
        self.assertEqual(b4.id, 12)
        self.assertEqual(b5.id, -1)

    def test_unknown(self):
        """Test name error."""
        with self.assertRaises(NameError):
            Base(a)

    def test_to_json(self):
        """Test to_json_string method."""
        r1 = Rectangle(10, 7, 2, 8)
        dictionary = r1.to_dictionary()
        json_dictionary = Base.to_json_string([dictionary])
        self.assertEqual(str(type(json_dictionary)), "<class 'str'>")

    def test_to_json_rectangle(self):
        """Test to_json_string length for rectangle."""
        r1 = Rectangle(10, 7, 2, 8)
        dictionary = r1.to_dictionary()
        json_dictionary = Base.to_json_string([dictionary])
        self.assertEqual(len(json_dictionary),
                         len(str([{"x": 2, "width": 10, "id": 1, "height": 7,
                                   "y": 8}])))
        self.assertTrue(type(json_dictionary), dict)
        self.assertTrue(type(json_dictionary) is str)

    def test_to_json_square(self):
        """Test to_json_string length for square."""
        s1 = Square(10, 2, 8)
        dictionary = s1.to_dictionary()
        json_dictionary = Base.to_json_string([dictionary])
        self.assertEqual(len(json_dictionary),
                         len(str([{"x": 2, "size": 10, "id": 1, "y": 8}])))
        self.assertTrue(type(json_dictionary), dict)

    def test_to_json_empty(self):
        """Test to_json_string method with empty and None."""
        json_dictionary = Base.to_json_string(None)
        self.assertEqual(json_dictionary, '[]')
        json_dictionary = Base.to_json_string([])
        self.assertEqual(json_dictionary, '[]')

    def test_to_json_empty(self):
        """Test to_json_string method with empty string."""
        json_dictionary = Base.to_json_string('')
        self.assertEqual(json_dictionary, '[]')

    def test_save_to_file_rectangle_len(self):
        """Test length of json string rectangle."""
        r1 = Rectangle(10, 7, 2, 8)
        r2 = Rectangle(2, 4)
        Rectangle.save_to_file([r1, r2])
        with open("Rectangle.json") as file:
            self.assertEqual(len(file.read()), len(str(
                [{"x": 2, "id": 6, "width": 10, "y": 8, "height": 7},
                 {"x": 0, "id": 7, "width": 2, "y": 0, "height": 4}])))

    def test_save_to_file_square_len(self):
        """Test length of json string square."""
        s1 = Square(10, 7, 2, 8)
        s2 = Square(2, 4)
        Square.save_to_file([s1, s2])
        with open("Square.json") as file:
            self.assertEqual(len(file.read()), len(str(
                    [{"x": 7, "id": 8, "size": 10, "y": 2},
                     {"x": 4, "id": 7, "size": 2, "y": 0}])))

    def test_save_to_file_rectangle(self):
        """Test save_to_file rectangle empty."""
        Rectangle.save_to_file([])
        with open("Rectangle.json") as file:
            self.assertEqual(file.read(), '[]')

    def test_save_to_file_square(self):
        """Test save_to_file square empty."""
        Square.save_to_file([])
        with open("Square.json") as file:
            self.assertEqual(file.read(), '[]')

    def test_save_to_file_rectangle_none(self):
        """Test save_to_file square empty."""
        Rectangle.save_to_file(None)
        with open("Rectangle.json") as file:
            self.assertEqual(file.read(), '[]')

    def test_save_to_file_square_none(self):
        """Test save_to_file with None."""
        Square.save_to_file(None)
        with open("Square.json") as file:
            self.assertEqual(file.read(), '[]')

    def test_from_json_string_three(self):
        """Test from_json_string three inputs."""
        list_input = [{'id': 89, 'width': 10, 'height': 4},
                      {'id': 7, 'width': 1, 'height': 7}]
        json_list_input = Rectangle.to_json_string(list_input)
        list_output = Rectangle.from_json_string(json_list_input)
        self.assertEqual(list_output, [{'id': 89, 'width': 10, 'height': 4},
                                       {'id': 7, 'width': 1, 'height': 7}])
        self.assertTrue(type(list_output), list)

    def test_from_json_string_two(self):
        """Test from_json_string two inputs."""
        list_input = [{'id': 89, 'size': 10}, {'id': 7, 'size': 1}]
        json_list_input = Square.to_json_string(list_input)
        list_output = Square.from_json_string(json_list_input)
        self.assertEqual(list_output, [{'id': 89, 'size': 10},
                                       {'id': 7, 'size': 1}])
        self.assertTrue(type(list_output), list)

    def test_from_json_string_none(self):
        """Test from_json_string with None."""
        list_input = [{'id': 89, 'width': 10, 'height': 4},
                      {'id': 7, 'width': 1, 'height': 7}]
        json_list_input = Rectangle.to_json_string(list_input)
        list_output = Rectangle.from_json_string(None)
        self.assertEqual(list_output, [])

    def test_from_json_string_empty(self):
        """Test from_json_string with empty string."""
        list_input = [{'id': 89, 'width': 10, 'height': 4},
                      {'id': 7, 'width': 1, 'height': 7}]
        json_list_input = Rectangle.to_json_string(list_input)
        list_output = Rectangle.from_json_string('')
        self.assertEqual(list_output, [])
        self.assertTrue(type(list_output), list)

    def test_create(self):
        """Test create."""
        temp_stdout = StringIO()
        with contextlib.redirect_stdout(temp_stdout):
            r1 = Rectangle(3, 5, 1)
            r1_dictionary = r1.to_dictionary()
            r2 = Rectangle.create(**r1_dictionary)
            print(r1)
        output = temp_stdout.getvalue()
        self.assertEqual(output, '[Rectangle] (1) 1/0 - 3/5\n')
        self.assertTrue(type(r1) == Rectangle)

        temp_stdout = StringIO()
        with contextlib.redirect_stdout(temp_stdout):
            s1 = Square(3)
            s1_dictionary = s1.to_dictionary()
            s2 = Square.create(**s1_dictionary)
            print(s1)
        output = temp_stdout.getvalue()
        self.assertEqual(output, '[Square] (3) 0/0 - 3\n')
        self.assertTrue(type(s1) == Square)

    def test_create2(self):
        """Test create, check for new instance."""
        temp_stdout = StringIO()
        with contextlib.redirect_stdout(temp_stdout):
            r1 = Rectangle(3, 5, 1)
            r1_dictionary = r1.to_dictionary()
            r2 = Rectangle.create(**r1_dictionary)
            print(r2)
        output = temp_stdout.getvalue()
        self.assertEqual(output, '[Rectangle] (1) 1/0 - 3/5\n')
        self.assertTrue(type(r2) == Rectangle)
        self.assertFalse(r1 is r2)
        self.assertFalse(r1 == r2)

    def test_create_none(self):
        """Test create with None."""
        r1 = Rectangle(3, 5, 1)
        r1_dictionary = r1.to_dictionary()
        with self.assertRaises(TypeError):
            r2 = Rectangle.create(None)

    def test_create_int(self):
        """Test create with int."""
        r1 = Rectangle(3, 5, 1)
        r1_dictionary = r1.to_dictionary()
        with self.assertRaises(TypeError):
            r2 = Rectangle.create(1, 2, 3)

    def test_create_string(self):
        """Test create with string."""
        r1 = Rectangle(3, 5, 1)
        r1_dictionary = r1.to_dictionary()
        with self.assertRaises(TypeError):
            r2 = Rectangle.create('string')

    def test_create_name_error(self):
        """Test create with name error."""
        r1 = Rectangle(3, 5, 1)
        r1_dictionary = r1.to_dictionary()
        with self.assertRaises(NameError):
            r2 = Rectangle.create(**betty)

    def test_load_from_file_rectangle(self):
        """Test load from file rectangle."""
        temp_stdout = StringIO()
        with contextlib.redirect_stdout(temp_stdout):
            r1 = Rectangle(10, 7, 2, 8)
            r2 = Rectangle(2, 4)
            list_rectangles_input = [r1, r2]
            Rectangle.save_to_file(list_rectangles_input)
            list_rectangles_output = Rectangle.load_from_file()
            print(list_rectangles_output[0])
        output = temp_stdout.getvalue()
        self.assertEqual(output, '[Rectangle] (1) 2/8 - 10/7\n')

        temp_stdout = StringIO()
        with contextlib.redirect_stdout(temp_stdout):
            print(list_rectangles_output[1])
        output = temp_stdout.getvalue()
        self.assertEqual(output, '[Rectangle] (2) 0/0 - 2/4\n')
        self.assertTrue(type(list_rectangles_output), list)

    def test_load_from_file_square(self):
        """Test load from file square."""
        temp_stdout = StringIO()
        with contextlib.redirect_stdout(temp_stdout):
            s1 = Square(5)
            s2 = Square(7, 9, 1)
            list_squares_input = [s1, s2]
            Square.save_to_file(list_squares_input)
            list_squares_output = Square.load_from_file()
            print(list_squares_output[0])
        output = temp_stdout.getvalue()
        self.assertEqual(output, '[Square] (1) 0/0 - 5\n')

        temp_stdout = StringIO()
        with contextlib.redirect_stdout(temp_stdout):
            print(list_squares_output[1])
        output = temp_stdout.getvalue()
        self.assertEqual(output, '[Square] (2) 9/1 - 7\n')
        self.assertTrue(type(list_squares_output), list)

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


"""
Denoising variational autoencoder.
Code originally based on https://github.com/L1aoXingyu/pytorch-beginner/tree/master/08-AutoEncoder.
"""

import os
import torch
import modules
import argparse
import numpy as np
from utils import to_img, zero_mask, add_gaussian, salt_and_pepper, \
    save_image_wrapper, init_model, init_loss, init_data_loader

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--batch_size', type=int, default=32)
    parser.add_argument('--learning_rate', type=float, default=1e-2)
    parser.add_argument('--num_epochs', type=int, default=100)
    parser.add_argument('--model_class', type=str, default='CVAE')
    parser.add_argument('--dataset_key', type=str, default='resisc')
    parser.add_argument('--noise_type', type=str, default='gs')
    parser.add_argument('--zero_frac', type=float, default=0.3)
    parser.add_argument('--gaussian_stdev', type=float, default=0.4)
    parser.add_argument('--sp_frac', type=float, default=0.1)
    parser.add_argument('--restore_path', type=str, default=None)
    parser.add_argument('--save_path', type=str, default='./model.pth')
    parser.add_argument('--log_freq', type=int, default=10)
    parser.add_argument('--dataset_path', type=str, default='data/NWPU-RESISC45')
    parser.add_argument('--weight_decay', type=float, default=0)

    args = parser.parse_args()
    print(args)
    print('----------')

    batch_size     = args.batch_size
    learning_rate  = args.learning_rate
    num_epochs     = args.num_epochs
    model_class    = args.model_class
    dataset_key    = args.dataset_key
    noise_type     = args.noise_type
    zero_frac      = args.zero_frac
    gaussian_stdev = args.gaussian_stdev
    sp_frac        = args.sp_frac
    restore_path   = args.restore_path
    save_path      = args.save_path
    log_freq       = args.log_freq
    dataset_path   = args.dataset_path
    weight_decay   = args.weight_decay

    # set up log folders
    if not os.path.exists('./01_original'):
        os.makedirs('./01_original')
    if not os.path.exists('./02_noisy'):
        os.makedirs('./02_noisy')
    if not os.path.exists('./03_output'):
        os.makedirs('./03_output')

    save_dir = os.path.dirname(save_path)
    if save_dir and not os.path.exists(save_dir):
        os.makedirs(save_dir)

    # set up model and criterion
    model = init_model(model_class, restore_path, restore_required=False, latent_dim=512)
    criterion = init_loss('vae', reconstruction_loss_type='mse')

    # load data
    data_loader, _, _, _, data_minval, data_maxval = \
        init_data_loader(dataset_key, batch_size, dataset_path)

    # training loop
    warning_displayed = False
    original, noisy, output = None, None, None
    model_optimizer = torch.optim.Adam(
        model.parameters(), lr=learning_rate, weight_decay=weight_decay)

    for epoch in range(num_epochs):
        mean_loss, total_num_examples = 0, 0
        for data in data_loader:
            original = data.float()
            if not model.is_convolutional:
                original = original.view(original.size(0), -1)
            if torch.cuda.is_available():
                original = original.cuda()

            # apply noise
            if noise_type == 'mn':
                noisy, _ = zero_mask(original, zero_frac)
            elif noise_type == 'gs':
                noisy, _ = add_gaussian(original, gaussian_stdev)
            elif noise_type == 'sp':
                noisy, _ = salt_and_pepper(original, sp_frac, data_minval, data_maxval)
            else:
                if not warning_displayed:
                    print('unrecognized noise type: %r' % (noise_type,))
                    print('using clean image as input')
                    warning_displayed = True
                noisy = original
            if torch.cuda.is_available():
                noisy = noisy.cuda()

            # =============== forward ===============
            output, mean, log_var = model(noisy)
            loss = criterion(output, original, mean, log_var)
            batch_size_ = original.size(0)  # might be undersized last batch
            total_num_examples += batch_size_
            # assumes `loss` is sum for batch
            mean_loss += (loss - mean_loss * batch_size_) / total_num_examples

            # =============== backward ==============
            model_optimizer.zero_grad()
            loss.backward()
            model_optimizer.step()

        # =================== log ===================
        print('epoch {}/{}, loss={:.6f}'.format(epoch + 1, num_epochs, mean_loss.item()))
        if epoch % log_freq == 0 or epoch == num_epochs - 1:
            # save images
            to_save = [
                (to_img(original.data.cpu()), './01_original', 'original'),
                (to_img(noisy.data.cpu()), './02_noisy', 'noisy'),
                (to_img(output.data.cpu()), './03_output', 'output'),
            ]
            for img, folder, desc in to_save:
                save_image_wrapper(img, os.path.join(folder, '{}_{}.png'.format(desc, epoch + 1)))

            # save model(s)
            torch.save(model.state_dict(), save_path)
            print('[o] saved model to %s' % save_path)