#! /opt/conda/bin/python3
""" Training script for tucana model """

# Copyright 2018 FAU-iPAT (http://ipat.uni-erlangen.de/)
#
# 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.

import argparse

parser = argparse.ArgumentParser(description='Tucana v5 training script')
# Data source parameters
parser.add_argument('--databasepath', type=str)
parser.add_argument('--datapath', action='append', type=str)
parser.add_argument('--answerpath', action='append', type=str)
parser.add_argument('--configpath', action='append', type=str)
parser.add_argument('--modelfile', type=str)
parser.add_argument('--fileformat', type=str)
parser.add_argument('--maxfilecount', type=int)
parser.add_argument('--nocache', type=int)
# Resume parameters
parser.add_argument('--resumefile', type=str)
parser.add_argument('--initialepoch', type=int)
parser.add_argument('--checkpoint', type=int)
# Solver parameters
parser.add_argument('--learningrate', type=float)
parser.add_argument('--decay', type=float)
# Batch parameters
parser.add_argument('--batchsize', type=int)
# Results parameters
parser.add_argument('--resultpath', type=str)
parser.add_argument('--bestfile', type=str)
parser.add_argument('--runstatsfile', type=str)
parser.add_argument('--checkpointfile', type=str)
parser.add_argument('--cprunstatsfile', type=str)
parser.add_argument('--weightfile', type=str)
parser.add_argument('--tensorboard', type=str)
parser.add_argument('--epochs', type=int)
parser.add_argument('--verbose', type=int)
parser.add_argument('--histogramfreq', type=int)
# Data filter parameters
parser.add_argument('--mindist', type=float)
# Data enhancement parameters
# Specific parameters
parser.add_argument('--userectangle', type=int)
parser.add_argument('--usebartlett', type=int)
parser.add_argument('--usehanning', type=int)
parser.add_argument('--usemeyer', type=int)
parser.add_argument('--usepoisson', type=int)
parser_args = parser.parse_args()
cmdargs = dict(vars(parser_args))

def getarg(name, default):
    global cmdargs
    return cmdargs[name] if name in cmdargs and cmdargs[name] is not None else default

print('')
print('#################################')
print('###   Start training script   ###')
print('#################################')
print('')

for key in cmdargs.keys():
    print('{:16s} = {}'.format(key, cmdargs[key]))

# Import the libraries

print('')
print('###############################')
print('###   Importing libraries   ###')
print('###############################')
print('')

from keras.callbacks import ModelCheckpoint, TensorBoard
from keras.optimizers import Adam
from keras.callbacks import Callback
from dieFFT.toolbox.models import Model, model_from_json
from dieFFT.toolbox.callbacks import StopFileInterrupt, RunStatsCallback
from dieFFT.toolbox import c2f, DataGenerator, DataGeneratorSelection, DataGeneratorEnhancement
import numpy as np
import os

# Check optional arguments

print('')
print('#########################################')
print('###   Checking setup and parameters   ###')
print('#########################################')
print('')

if getarg('nocache', 0) > 0:
    DataGenerator.clear_cache()

data_path = getarg('databasepath', './')
result_path = getarg('resultpath', './')
model_file = getarg('modelfile', './lyra.json')
resume_file = getarg('resumefile', None)
file_format = getarg('fileformat', 'batch_{0:05d}.npy')

paths = []
if getarg('userectangle', 1) > 0:
    paths.append(data_path+'rectangle/')
if getarg('usebartlett', 1) > 0:
    paths.append(data_path+'bartlett/')
if getarg('usehanning', 0) > 0:
    paths.append(data_path+'hanning/')
if getarg('usemeyer', 1) > 0:
    paths.append(data_path+'meyer/')
if getarg('usepoisson', 0) > 0:
    paths.append(data_path+'poisson/')
path_data = []
for path in getarg('datapath', paths):
    if os.path.isdir(path):
        path_data.append(path)

path_answer = []
for path in getarg('answerpath', [data_path+'answer/']):
    if os.path.isdir(path):
        path_answer.append(path)

path_config = []
for path in getarg('configpath', [data_path+'config/']):
    if os.path.isdir(path):
        path_config.append(path)

file_best = getarg('bestfile', result_path + 'best.hdf5')
file_final = getarg('weightfile', result_path+'final.hdf5')
file_checkpoint = getarg('checkpointfile', result_path + 'checkpoint.hdf5')
path_tensorboard = getarg('tensorboard', result_path)
file_runstats = getarg('runstatsfile', result_path+'runstats.npy')
file_cp_runstats = getarg('cprunstatsfile', result_path+'checkpoint_runstats.npy')

config_batchsize = getarg('batchsize', 128)
config_epochs = getarg('epochs', 150)
config_verbose = getarg('verbose', 1)
config_checkpoint = bool(getarg('checkpoint', 0) > 0)
config_histogram_freq = getarg('histogramfreq', 0)
config_initial_epoch = getarg('initialepoch', 0)

config_min_dist = getarg('mindist', None)

# Print the resulting setup

print('Model File = '+model_file)
if isinstance(path_data, (tuple,list)):
    for path in path_data:
        print('Data Path = '+path)
else:
    print('Data Path = '+path_data)
if isinstance(path_answer, (tuple,list)):
    for path in path_answer:
        print('Answer Path = '+path)
else:
    print('Answer Path = '+path_answer)
if isinstance(path_config, (tuple,list)):
    for path in path_config:
        print('Config Path = '+path)
else:
    print('Config Path = '+path_config)
print('File Format = '+file_format)
if resume_file is not None:
    print('Resuming from file = '+resume_file)
print('')
print('Best File = ' + file_best)
print('Final Weights File = '+file_final)
print('Tensorboard Path = '+path_tensorboard)
print('Runtime Statistic File = '+file_runstats)
print('')
print('Batch Size = {0}'.format(config_batchsize))
print('')
print('Minimal Distance = {0:5.3f}'.format(config_min_dist if config_min_dist is not None else 0.0))

# Build the result directory

def mkdir(path):
    if not os.path.exists(path):
        try:
            os.makedirs(path)
            pass
        except:
            pass
mkdir(result_path)

# Load the model and compile it

print('')
print('####################################')
print('###   Load and compiling model   ###')
print('####################################')
print('')

optimizer = Adam(
    lr = getarg('learningrate', 0.001),
    # beta_1=0.9,
    # beta_2=0.999,
    # epsilon=1e-8,
    decay = getarg('decay', 0.0)
)

with open(model_file) as file:
    json = file.read()
model = model_from_json(json)
model.compile(optimizer=optimizer, loss="binary_crossentropy", metrics=["binary_accuracy"])
model.summary()
if resume_file is not None:
    model.load_weights(resume_file)

# Define the data preparation class

class DataPrepare(DataGeneratorEnhancement):

    def __init__(self):
        super(DataPrepare, self).__init__()

    def enhance(self, data_in, answer_in, config):
        data = c2f(data_in, normalize=0, real=True, imaginary=True, absolute=True)
        config.append(answer_in[0])
        answer = np.minimum(answer_in[0], np.ones(answer_in[0].shape))
        return [data], [answer], config

prepare = DataPrepare()

# Define the data enhancement class

class DataEnhance(DataGeneratorEnhancement):

    def __init__(self):
        super(DataEnhance, self).__init__()

    def enhance(self, data, answer, config):
        return data, answer, config

enhance = DataEnhance()

# Define the data selection class

class DataSelect(DataGeneratorSelection):

    def __init__(self, mindist=None):
        super(DataSelect, self).__init__()
        self.mindist = mindist

    def select(self, idx, data, answer, config):
        valid = np.equal(idx, idx)
        if self.mindist is not None:
            valid = np.logical_and(valid, np.asarray(config[0]['mindist']) >= self.mindist)
        return valid

select = DataSelect(
    mindist=config_min_dist,
)

# Define the generators

print('')
print('###############################')
print('###   Building generators   ###')
print('###############################')
print('')

dg = DataGenerator(
    path_data=path_data,
    path_answer=path_answer,
    path_config=path_config,
    file_format=file_format,
    file_limit=getarg('maxfilecount', None),  # Automatic counting
    validation=0.2,
    testing=0.1,
    data_loader='numpy_dict',
    file_size=None,  # Get automatically from first data file
    batch_size=config_batchsize,
    verbose=1,
    nocache=(getarg('nocache', 0) > 0),
)

gen = dg.generator(
    dataset='training',
    batch_size=None,
    shuffle=True,
    prepend_idx=False,
    append_config=False,
    preparation=(prepare),
    selection=(select),
    enhancement=(enhance),
)

gen_count = dg.batches(dataset='training', batch_size=None)

val_gen = dg.generator(
    dataset='validation',
    batch_size=None,
    shuffle=True,
    prepend_idx=False,
    append_config=False,
    preparation=(prepare),
    selection=(select),
    enhancement=(enhance),
)

val_gen_count = dg.batches(dataset='validation', batch_size=None)

test_gen = dg.generator(
    dataset='testing',
    batch_size=None,
    shuffle=True,
    prepend_idx=False,
    append_config=False,
    preparation=(prepare),
    selection=(select),
    enhancement=(enhance),
)

test_gen_count = dg.batches(dataset='testing', batch_size=None)

# Counting data sets remaining after filtering

print('')
print('#################################')
print('###   Counting data samples   ###')
print('#################################')
print('')

if gen_count > 0 :
    count_samples = dg.count(dataset='training', preparation=(prepare), selection=(select))
    print('Valid Training Samples = {0}   ({1:5.2f}%)'.format(
        count_samples,
        100 * count_samples / max(1, dg.batches(dataset='training', batch_size=1))
    ))

if val_gen_count > 0:
    val_count_samples = dg.count(dataset='validation', preparation=(prepare), selection=(select))
    print('Valid Validation Samples = {0}   ({1:5.2f}%)'.format(
        val_count_samples,
        100 * val_count_samples / max(1, dg.batches(dataset='validation', batch_size=1))
    ))

if test_gen_count > 0:
    test_count_samples = dg.count(dataset='testing', preparation=(prepare), selection=(select))
    print('Valid Testing Samples = {0}   ({1:5.2f}%)'.format(
        test_count_samples,
        100 * test_count_samples / max(1, dg.batches(dataset='testing', batch_size=1))
    ))

# Setup the fitting, start training and save final model

print('')
print('#################################')
print('###   Run training of model   ###')
print('#################################')
print('')

class CPRunStats(Callback):
    def __init__(self, runstats, filename):
        super(CPRunStats, self).__init__()
        self._runstats = runstats
        self._filename = filename
    def on_epoch_end(self, epoch, logs):
        runstats = self._runstats.runstats
        np.save(self._filename, runstats)

rsCallback = RunStatsCallback()
cbstack = [
    StopFileInterrupt(),
    ModelCheckpoint(file_best, save_best_only=True),
    TensorBoard(log_dir=path_tensorboard, histogram_freq=config_histogram_freq, write_graph=True, write_images=True),
    rsCallback
]
if config_checkpoint is True:
    cbstack.append(ModelCheckpoint(file_checkpoint, save_best_only=False))
    cbstack.append(CPRunStats(rsCallback, file_cp_runstats))

model.fit_generator(
    gen,
    steps_per_epoch=gen_count,
    epochs=config_epochs,
    callbacks=cbstack,
    max_q_size=15,
    validation_data=val_gen,
    validation_steps=val_gen_count,
    verbose=config_verbose,
    initial_epoch=config_initial_epoch,
)
runstats = rsCallback.runstats
model.save(file_final)

# Assemble best metrices and save the runstats

print('')
print('################################################')
print('###   Assembling stats and saving runstats   ###')
print('################################################')
print('')

print('Statistics of best model (best validation accuracy):')
best_stats = {}
names = model.metrics_names
model.load_weights(file_best)

best_list = [
    {'dataset': 'training', 'key': 'training', 'label': 'Training', 'listkey': 'traininglist'},
    {'dataset': 'validation', 'key': 'validation', 'label': 'Validation', 'listkey': 'validationlist'},
    {'dataset': 'testing', 'key': 'testing', 'label': 'Testing', 'listkey': 'testinglist'},
]
for best_entry in best_list:
    eval_dict = {key: [] for key in names}
    best = dg.generator(
        dataset=best_entry['dataset'],
        batch_size=None,
        shuffle=False,
        prepend_idx=False,
        append_config=False,
        preparation=(prepare),
        selection=(select),
        enhancement=(enhance),
    )
    best_count = dg.batches(dataset=best_entry['dataset'], batch_size=None)
    if best_count > 0:
        batch_sizes = []
        for _ in range(best_count):
            data, answer = next(best)
            batch_sizes.append(data[0].shape[0])
            stats = model.evaluate(data, answer, verbose=0)
            for key, value in zip(names, stats):
                eval_dict[key].append(value)
        eval_avg = {}
        for key in names:
            eval_avg[key] = np.average(np.asarray(eval_dict[key]), weights=batch_sizes)
        best_stats[best_entry['key']] = eval_avg
        best_stats[best_entry['listkey']] = eval_dict
        print('{0:25s} = {1:6.2f}%'.format(best_entry['label']+' accuracy', 100.0*eval_avg['binary_accuracy']))

runstats['best'] = best_stats
np.save(file_runstats, runstats)

print('')
print('###############################')
print('###   End training script   ###')
print('###############################')
print('')


#!/usr/bin/env python3

__author__ = 'Jake Miller (@LaconicWolf)'
__date__ = '20180710'
__version__ = '0.01'
__description__ = """Attempts reverse DNS lookups of a specified IP range."""

import string
import itertools
import sys
import re
import argparse
if sys.version.startswith('3'):
    import ipaddress
try:
    import dns.resolver, dns.reversename, dns.query, dns.zone
except ImportError as error:
    missing_module = str(error).split(' ')[-1]
    print('[*] Try running "pip install dnspython", or do an Internet search for installation instructions.')
    exit()

def ip_range(input_string):
    """Accepts a dash specified range and returns a list of ip addresses
    within that range. Adapted from:
    https://stackoverflow.com/questions/20525330/python-generate-a-list-of-ip-addresses-from-user-input
    """
    octets = input_string.split('.')
    chunks = [list(map(int, octet.split('-'))) for octet in octets]
    ranges = [range(c[0], c[1] + 1) if len(list(c)) == 2 else c for c in chunks]
    addrs = ['.'.join(list(map(str, address))) for address in itertools.product(*ranges)]
    return addrs

def cidr_ip_range(input_string):
    """Accepts a CIDR range and returns a list of ip addresses
    within the CIDR range.
    """
    addr_obj = ipaddress.ip_network(input_string)
    addrs = [addr for addr in addr_obj.hosts()]
    return addrs

def ptr_lookup(address):
    """Performs a reverse DNS lookup and returns the result."""
    addr = dns.reversename.from_address(address)
    try:
        resolved_name = str(dns.resolver.query(addr, "PTR")[0])
    except Exception as e:
        if args.verbose:
            print(e)
        return
    return resolved_name

def main():
    results = [((ptr_lookup(str(address)), address)) for address in ip_addresses]
    print('\n{:35}{:35}'.format("DOMAIN NAME", "IP ADDRESS"))
    for item in results:
        domain_name = item[0]
        ip_address = item[1]
        if domain_name is not None:
            if args.domain:
                if args.domain not in domain_name:
                    continue
            print('{:35}{:35}'.format(domain_name.strip('.'), ip_address))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-v", "--verbose",
                        help="Increase output verbosity.",
                        action="store_true")
    parser.add_argument("-r", "--range",
                        help="Specify the IP range to test. Example: ./{0} -r 192.168.0.0/24 or ./{0} -r 192.168.0.0-255".format(sys.argv[0]))
    parser.add_argument("-d", "--domain",
                        help="Limit results to those that contain the specified domain.")
    parser.add_argument("-f", "--file",
                        help="Specify file containing IP addresses to resolve.")
    parser.add_argument("-a", "--address",
                        help="Specify a single IP address to resolve.")
    args = parser.parse_args()
    if not args.file and not args.range and not args.address:
        parser.print_help()
        print()
        print('[-] Please specify IP addresses to resolve.')
        print('[*] Example: ./{} -r 192.168.0.0/24'.format(sys.argv[0]))
        print('[*] Example: ./{} -f /path/to/addr_file'.format(sys.argv[0]))
        print('[*] Example: ./{} -a 192.168.0.1'.format(sys.argv[0]))
        print()
        exit()

    if args.address:
        if '/' in args.address or '-' in args.address or string.ascii_letters in args.address:
            print('\n[-] Invalid characters detected in IP address.\n')
            exit()
        ip_addrs = [args.address]
    if args.file:
        if not os.path.exists(args.file):
            parser.print_help()
            print("\n[-]  The file cannot be found or you do not have permission to open the file. Please check the path and try again\n")
            exit()
        ip_addrs = open(args.file).read().splitlines()
        for ip in ip_addrs:
            if '/' in ip or '-' in ip or string.ascii_letters in ip:
                print('\n[-] Invalid characters detected in IP address in file. Please \
                    inspect the contents of the file and try again.\n')
                exit()
    if args.range:
        if not '-' in args.range and not '/' in args.range:
            if sys.version.startswith('3'):
                parser.print_help()
                print("\n[-] Please either specify a CIDR range or an octet range with a dash ('-').\n")
                exit()
            else:
                parser.print_help()
                print("\n[-] Please specify an octet range with a dash ('-').\n")
                exit()

        # https://www.regextester.com/93987
        cidr_regex = r'^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$'

        # adapted from https://stackoverflow.com/questions/10086572/ip-address-validation-in-python-using-regex
        dash_regex = r'^[\d+-?]{1,7}\.[\d+-?]{1,7}\.[\d+-?]{1,7}\.[\d+-?]{1,7}$'

        if '-' in args.range:
            if '/' in args.range:
                if sys.version.startswith('3'):
                    parser.print_help()
                    print("\n[-] Please either use CIDR notation or specify octet range with a dash ('-'), not both.\n")
                    exit()
                else:
                    parser.print_help()
                    print("\n[-] CIDR notation not supported with Python2. For CIDR notation, please use Python3.\n")
                    exit()
            if not re.findall(dash_regex, args.range):
                parser.print_help()
                print('\n[-] Invalid IP range detected. Please try again.\n')
                exit()
            ip_addrs = ip_range(args.range)

        elif '/' in args.range:
            if sys.version.startswith('2'):
                parser.print_help()
                print("\n[-] CIDR notation not supported when runnng this script with Python2. For CIDR notation, please use Python3.\n")
                exit()
            try:
                if not re.findall(cidr_regex, args.range):
                    parser.print_help()
                    print('\n[-] Invalid CIDR range detected. Please try again.\n')
                    exit()
                ip_addrs = cidr_ip_range(args.range)
            except ValueError as error:
                parser.print_help()
                print('\n[-] Invalid CIDR range detected. Please try again.')
                print('[-] {}\n'.format(error))
                exit()

    # Additional validation to dump any octet larger than 255
    ip_addresses = []
    for addr in ip_addrs:
        octets = str(addr).split('.')
        invalid_addr = [octet for octet in octets if int(octet) > 255]
        if invalid_addr:
            continue
        ip_addresses.append(addr)
    main()


import Env_New.StochasticJSSP_Env as StochasticJSSP_Env
import Env_New.Env_utils as Env_utils
import matplotlib.pyplot as plt
import networkx as nx
import random
import time
import torch
import numpy as np

Env = StochasticJSSP_Env.StochasticJSSP()

Env.reset(Env_utils.to_numpy(StochasticJSSP_Env.G.graph))

t = 0
show = False
timestamp1 = time.time()
while not Env.done():
    # print(Env.feasible_candidate)
    node = random.choice(Env.feasible_candidate)
    # print(node)
    t += 1
    # print(t)
    timestamp3 = time.time()
    Env.step(node)
    timestamp4 = time.time()
timestamp2 = time.time()
if Env.done() and show:
    g = Env.g_list[-1]
    pos = nx.shell_layout(g)
    nx.draw(g, pos, with_labels=True, edge_color='#80b3ff', node_color='#ff6666')
    print('There is:', g.number_of_nodes(), ' number of nodes and ', g.number_of_edges(),
          ' edges.')
    plt.draw()
    plt.show()
print('This rollout take: % seconds' % (timestamp2 - timestamp1))
# print(Env.feature_dis)
# print(len(Env.prev_step_list))
print(Env.mask)
'''
t = torch.tensor([[1, 2], [3, 4], [5, 6]])
r = torch.gather(t, 0, torch.tensor([[0, 1], [1, 0]]))
print(t)
print(r)
idx = torch.tensor([0, 2])
print(t[idx])
'''


#-*- coding:utf-8 -*-
import urllib2
import re #正则
from bs4 import BeautifulSoup
import csv
import sys
import database
import send_email
def getImage(url,i):
    imagelist=[]
    req = urllib2.Request(url)  # 请求页面 # 加一个键值对，针对服务器的反爬机制
    req.add_header("User-Agent",
                   "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
    html = urllib2.urlopen(req).read()
    soup = BeautifulSoup(html, 'lxml')
    images_content = soup.find_all('a', class_=re.compile('TypeBigPics'))
    for image in images_content:
        images=[]
        image_url = image.attrs['href'].replace(u'\xa0', u' ')
        image_src = image.img['src'].replace(u'\xa0', u' ')
        image_text = image.span.get_text().replace(u'\xa0', u' ')
        print(u"图片网址： " + image_url)
        print(u"图片地址：" + image_src)
        print(u"图片标题:" + image_text)
        images.append(image_text)
        images.append(image_src)
        images.append(image_url)
        # writer.writerow((i, images[0], images[1], images[2]))
        imagelist.append(images)
    return imagelist

def getTopImages():#只得到无图片型资讯
    reload(sys)
    list=[]
    sys.setdefaultencoding("gbk")
    url="http://www.umei.cc/bizhitupian/fengjingbizhi/"
    images=[]
    i=1
    # csvfile=open("D:csv_top_images.csv","wb")
    # writer=csv.writer(csvfile)
    # writer.writerow(('ID',u'图片标题',u'图片链接地址',u'图片网址'))
    getImage(url, i)
    for page in range(2,4):
        i+=1
        url = "http://www.umei.cc/bizhitupian/fengjingbizhi/"
        print("正在解析第"+str(page)+"页新闻数据")
        url=url+str(page)+".htm"
        print(url)
        images=getImage(url,i)
        list.append(images)
        insertImagedb(images)
        send_email.send(str(list).replace('u\'','\'').decode("unicode-escape"))
def insertImagedb(images):
    mysql_s = database.Mysql()
    for image in images:
        print(image)
        mydict = {}
        mydict['title'] = image[0]
        mydict['image_url'] = image[1]
        mydict['url'] = image[2]
        print(mydict)
        mysql_s.insertData("tb_images", mydict)

getTopImages()


from django.shortcuts import render
from django.http import HttpResponse
import json
from package.jiaowu.timetable import getTimetable

# Create your views here.

def timetable(req):
    if req.method == 'GET':
        clas = req.GET.get('clas', -1)
        week = req.GET.get('week', -1)
        weekday = req.GET.get('weekday', -1)
        timeblock = req.GET.get('timeblock', -1)
        if not clas == -1:
            tt = getTimetable(clas, week, weekday, timeblock)
            r = {'status': 'success', 'code': 3601, 'body': tt}
            return HttpResponse(json.dumps(r, ensure_ascii=False))
        else:
            r = {'status': 'success', 'code': 3602, 'body':'请输入班级参数'}
            return HttpResponse(json.dumps(r, ensure_ascii=False))
    else:
        r = {'status': 'error', 'code': 1502, 'body': '请求方法应该是 GET。'}
        return HttpResponse(json.dumps(r, ensure_ascii=False))


import pandas as pd
import numpy as np
from collections import Counter
import joblib
from text_preprocessing import text_process

df = pd.read_csv('sample30.csv') # reading the original dataset

vectorizer = joblib.load('tfidf_vectorizer.pkl') # loading the tf_idf vectorizer model
lr_model = joblib.load('lr_model.pkl') # loading the logistic regression model

user_based_recommender = pd.read_csv('user_final_rating.csv').set_index('reviews_username') # loading the User-based Recommender Model

# taking the username as user input
user_input = input("Enter your username: ")
if user_input not in list(user_based_recommender.index):
    print('Sorry !!! The username is not registered !!!')
else:
    # getting top 20 recommended products
    recommended_user_based_products = pd.DataFrame(user_based_recommender.loc[user_input].sort_values(ascending=False))[0:20]

    # fine-tuning the recommender model outputs (products) to select top 5 products with mostly positive reviews
    positive_products = []
    for i in range(20):
        reviews = list(df.loc[df.name == recommended_user_based_products.index[i], 'reviews_text'])
        processed_reviews = np.array([text_process(review) for review in reviews]).astype('U')
        sentiment = Counter(lr_model.predict(vectorizer.transform(processed_reviews))).most_common()[0][0]
        if sentiment == 1:
            positive_products.append(recommended_user_based_products.index[i])
        if len(positive_products) == 5:
            break

    # getting the 5 final recommended product details like brand, categories and manufacturer along with the name
    recommended_user_based_product_details = {}
    recommended_user_based_product_details['name'] = []
    recommended_user_based_product_details['brand'] = []
    recommended_user_based_product_details['categories'] = []
    recommended_user_based_product_details['manufacturer'] = []

    for product in positive_products:
        recommended_user_based_product_details['brand'].append(df.loc[df.name == product, 'brand'].iloc[0])
        recommended_user_based_product_details['categories'].append(df.loc[df.name == product, 'categories'].iloc[0])
        recommended_user_based_product_details['manufacturer'].append(df.loc[df.name == product, 'manufacturer'].iloc[0])
        recommended_user_based_product_details['name'].append(product)

    print('Recommended Product Details are as follows .... ')
    print(pd.DataFrame(recommended_user_based_product_details).head())


from fractions import gcd, Fraction

def get_divisors(n):
    return [i for i in range(1, n+1) if n % i == 0]

def find_roots(exp):
    divs1 = get_divisors(abs(exp[0].numerator))
    divs2 = [0] + get_divisors(abs(exp[-1].numerator))

    for d1 in divs1:
        for d2 in divs2:
            ans = None
            if solve(exp, Fraction(d2, d1)) == 0:
                ans = Fraction(d2, d1)
            elif solve(exp, Fraction(-d2,d1)) == 0:
                ans = Fraction(-d2, d1)

            if ans is not None:
                zvysok = horner(exp, ans)[:-1]
                return [ans] + find_roots(zvysok) if zvysok else []

    return []

def horner(exp, val):
    ans = [None] * len(exp)
    tmp = [Fraction(0)] * (len(exp)+1)

    for i in range(len(exp)):
        ans[i] = Fraction(exp[i] + tmp[i])
        tmp[i+1] = val * ans[i]

    return ans

def solve(exp, val):
    return horner(exp, val)[-1]

if __name__ == '__main__':
    for _ in range(int(input())):
        _ = input()
        exp = [Fraction(x) for x in input().split()]
        print(' '.join(map(str, sorted(find_roots(exp)))))


"""main.py"""
import argparse
from file_handling.file_handling import FileHandling
from generation_report.generation_report import GenerationReport
from linear_SVC.linear_SVC import LinearSVCClass
from settings.settings import (INPUT_DATASET,
                               INPUT_NEGATIVE_DATA,
                               INPUT_POSITIVE_DATA)
from test_result import test_result
from text_handling.text_handling import TextHandling
from text_vectorizer.text_vectorizer import TextVectorizer

parser = argparse.ArgumentParser()
parser.add_argument('-run', type=int)

if __name__ == "__main__":
    args = parser.parse_args()
    run = args.run

    if run:
        print('-----: RUN TEXT CLASSIFICATION:')

        try:
            # step1: file processing
            print("---RUN STEP1---")
            file_handling = FileHandling()
            file_handling.combine_input_data(file_negative_data=INPUT_NEGATIVE_DATA,
                                             file_positive_data=INPUT_POSITIVE_DATA)
            file_handling.get_dataset_info(dataset=INPUT_DATASET)

            # step2: split the dataset into training and test samples.
            print("---RUN STEP2---")
            result_split_dataset = file_handling.split_dataset(dataset=INPUT_DATASET)

            # step3: cleaning the text.
            print("---RUN STEP3---")
            text_handling = TextHandling()

            print("---clear dataset train result---")
            clear_dataset_train_result = text_handling.apply_text_cleaning(dataset=result_split_dataset['dataset_train'])

            print("---clear dataset test result---")
            clear_dataset_test_result = text_handling.apply_text_cleaning(dataset=result_split_dataset['dataset_test'])

            # step4: TfidfVectorizer.
            text_vectorizer = TextVectorizer()
            result_vec = text_vectorizer.apply_tfidf_vectorizer(dataset_train=clear_dataset_train_result,
                                                                dataset_test=clear_dataset_test_result)

            # step5: LinearSVC.
            linearSVC = LinearSVCClass()
            predict = linearSVC.apply_linear_svc(tfidf_vectorizer_train=result_vec["tfidf_vectorizer_train"],
                                                 tfidf_vectorizer_test=result_vec["tfidf_vectorizer_test"],
                                                 train_data_label=result_split_dataset["train_data_label"])

            # step6: generating a classification report
            create_report = GenerationReport.create_report(test_data_label=result_split_dataset["test_data_label"],
                                                           predict=predict)

            # step7: test result
            test_result(dataset_test=result_split_dataset['dataset_test'],
                        test_data_label=result_split_dataset['test_data_label'],
                        predict=predict)

        except Exception as e:
            print(e)


# coding: utf8

import requests

from flask import current_app as app
from .utils import SpiderJob

def run_job(job):
    """
    爬虫运行调度任务
    :param job: 当前任务对象
    :return:
        ：success:
            0
    """
    sj = SpiderJob(job)
    state = sj.status_check()
    if state == 'RUNNING':
        raise ValueError("当前任务已在运行,任务id: {}".format(job.id))

    if state == 'PAUSED':
        sj.resume()
    if state == 'STOPPED':
        # no spider process
        sj.start_crawlers(1)
        state_new = sj.status()
        if state_new == 'RUNNING':
            raise ValueError("任务已经是运行状态，需要等待其结束,任务id: {}".format(job.id))

    response = requests.post("http://{}:{}/feed".format(
        app.config['SCC_SERVICE_HOST'], app.config['SCC_SERVICE_PORT']), json=sj.spec)

    if response.status_code < 200 or response.status_code >= 300:
        raise ValueError("爬虫服务异常返回：{}".format(response.text))
    resp_json = response.json()
    if resp_json.get('status') != "SUCCESS":
        raise ValueError("爬虫服务提交失败：{}".format(resp_json.get('error')))

    if job.status != "RUNNING":
        job.update(set__status="RUNNING")

    return 0


import os
from decimal import Decimal

from celery import Celery

from panoptes_client import Panoptes

from django.conf import settings
from django.db.models import Q

# set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "vespa.settings")

app = Celery("vespa")

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object("django.conf:settings", namespace="CELERY")

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
    sender.add_periodic_task(
        settings.PERIODIC_TASK_INTERVAL,
        queue_image_generations.s(),
    )
    sender.add_periodic_task(
        settings.PERIODIC_TASK_INTERVAL,
        queue_json_generations.s(),
    )
    sender.add_periodic_task(settings.PERIODIC_TASK_INTERVAL, calculate_magnitudes.s())
    sender.add_periodic_task(settings.PERIODIC_TASK_INTERVAL, set_locations.s())
    sender.add_periodic_task(
        settings.PERIODIC_TASK_INTERVAL,
        set_zooniverse_metadata.s(),
    )

@app.on_after_fork.connect
@app.on_after_finalize.connect
def setup_zooniverse(sender, **kwargs):
    if settings.ZOONIVERSE_CLIENT_ID and settings.ZOONIVERSE_CLIENT_SECRET:
        Panoptes.connect(
            client_id=settings.ZOONIVERSE_CLIENT_ID,
            client_secret=settings.ZOONIVERSE_CLIENT_SECRET,
        )

@app.task
def queue_image_generations():
    from starcatalogue.models import Star, FoldedLightcurve

    for star in Star.objects.filter(
        fits_error_count__lt=settings.FITS_DOWNLOAD_ATTEMPTS
    ).filter(Q(image_version=None) | Q(image_version__lt=Star.CURRENT_IMAGE_VERSION))[
        :1000
    ]:
        star.get_image_location()

    for lightcurve in (
        FoldedLightcurve.objects.filter(
            star__fits_error_count__lt=settings.FITS_DOWNLOAD_ATTEMPTS
        )
        .filter(
            Q(image_version=None)
            | Q(image_version__lt=FoldedLightcurve.CURRENT_IMAGE_VERSION)
        )
        .filter(
            ~Q(sigma=Decimal("NaN"))
            & ~Q(chi_squared=Decimal("NaN"))
            & ~Q(period_length=Decimal("NaN"))
        )[:1000]
    ):
        lightcurve.get_image_location()

@app.task
def queue_json_generations():
    from starcatalogue.models import Star, FoldedLightcurve

    for star in Star.objects.filter(
        fits_error_count__lt=settings.FITS_DOWNLOAD_ATTEMPTS
    ).filter(Q(json_version=None) | Q(json_version__lt=Star.CURRENT_JSON_VERSION))[
        :1000
    ]:
        star.get_json_location()

@app.task
def calculate_magnitudes():
    from starcatalogue.models import Star

    for star in Star.objects.filter(
        fits_error_count__lt=settings.FITS_DOWNLOAD_ATTEMPTS
    ).filter(
        Q(fits_file__isnull=False)
        & (
            Q(_min_magnitude__isnull=True)
            | Q(_max_magnitude__isnull=True)
            | Q(_mean_magnitude__isnull=True)
            | Q(stats_version__lt=Star.CURRENT_STATS_VERSION)
            | Q(stats_version__isnull=True)
        )
    )[
        :1000
    ]:
        star.calculate_magnitudes()

@app.task
def set_locations():
    from starcatalogue.models import Star

    for star in Star.objects.filter(location=None)[:1000]:
        star.set_location()

@app.task
def set_zooniverse_metadata():
    from starcatalogue.models import ZooniverseSubject

    for subject in ZooniverseSubject.objects.filter(
        Q(metadata_version=None)
        | Q(metadata_version__lt=ZooniverseSubject.CURRENT_METADATA_VERSION)
    )[:1000]:
        subject.save_metadata()


import numpy as np
import tensorflow as tf
import cv2
import time

from imutils.video import FPS

import UtilsIO
import config
from pyimagesearch.centroidtracker import CentroidTracker
from pyimagesearch.trackableobject import TrackableObject
import dlib

from roi_elements import RoiElements

class DetectorAPI:
    def __init__(self, path_to_ckpt):
        self.path_to_ckpt = path_to_ckpt

        self.detection_graph = tf.Graph()
        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(self.path_to_ckpt, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

        self.default_graph = self.detection_graph.as_default()
        self.sess = tf.Session(graph=self.detection_graph)

        # Definite input and output Tensors for detection_graph
        self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
        # Each box represents a part of the image where a particular object was detected.
        self.detection_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        self.detection_scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
        self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
        self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')

    def processFrame(self, image):
        # Expand dimensions since the trained_model expects images to have shape: [1, None, None, 3]
        image_np_expanded = np.expand_dims(image, axis=0)
        # Actual detection.
        start_time = time.time()
        (boxes, scores, classes, num) = self.sess.run(
            [self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections],
            feed_dict={self.image_tensor: image_np_expanded})
        end_time = time.time()

        # print("Elapsed Time:", end_time - start_time)

        im_height, im_width, _ = image.shape
        boxes_list = [None for i in range(boxes.shape[1])]
        for i in range(boxes.shape[1]):
            boxes_list[i] = (int(boxes[0, i, 0] * im_height),
                             int(boxes[0, i, 1] * im_width),
                             int(boxes[0, i, 2] * im_height),
                             int(boxes[0, i, 3] * im_width))

        return boxes_list, scores[0].tolist(), [int(x) for x in classes[0].tolist()], int(num[0])

    def close(self):
        self.sess.close()
        self.default_graph.close()

line = (0, 0, 0, 0)
startPoint = False
endPoint = False

lineFlag = False

startCountFlag = False

def on_mouse(event, x, y, flags, params):
    global line, startPoint, endPoint

    # get mouse click
    if event == cv2.EVENT_LBUTTONDOWN:

        if startPoint == True and endPoint == True:
            startPoint = False
            endPoint = False
            line = []

        if startPoint == False:
            line = (x, y, 0, 0)
            startPoint = True
        elif endPoint == False:
            endPoint = True
            line = (line[0], line[1], x, y)

if __name__ == '__main__':
    ct = CentroidTracker(maxDisappeared=40, maxDistance=50)
    trackers = []
    trackableObjects = {}

    totalFrames = 0
    totalDown = 0
    W = None
    H = None
    roi = 250
    initBB = None
    roi_area = None
    roi_elements = None

    frame_size_w = 500
    frame_size_h = 400

    model_path = 'faster_rcnn_inception_v2/frozen_inference_graph.pb'
    odapi = DetectorAPI(path_to_ckpt=model_path)
    threshold = 0.7

    #cap = cv2.VideoCapture(UtilsIO.SAMPLE_FILE_NAME_2)
    cap = cv2.VideoCapture(config.CONFIG_IP_CAM_REAL)

    # start the frames per second throughput estimator
    fps = FPS().start()
    cv2.namedWindow('preview')

    cv2.setMouseCallback('preview', on_mouse)

    while True:
        r, img = cap.read()
        img = cv2.resize(img, (frame_size_w, frame_size_h))
        rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        if lineFlag:
            if startPoint == True and endPoint == True:
                try:
                    cv2.line(img, (line[0], line[1]), (line[2], line[3]), (255, 0, 255), 2)
                except:
                    pass

        if initBB is not None and roi_elements is None or (initBB is not None and initBB != roi_elements.box):
            roi_elements = RoiElements(initBB)

        if initBB is not None:
            roi_area = roi_elements.getRoiArea(img)

        if W is None or H is None:
            (H, W) = img.shape[:2]

        status = "Waiting"
        rects = []

        if totalFrames % 7 == 0:
            status = "Detecting"
            trackers = []
            if roi_area is not None:
                boxes, scores, classes, num = odapi.processFrame(roi_area)

            else:
                boxes, scores, classes, num = odapi.processFrame(img)

            # Visualization of the results of a detection.
            for i in range(len(boxes)):
                # Class 1 represents human
                if classes[i] == 1 and scores[i] > threshold:
                    box = boxes[i]

                    tracker = dlib.correlation_tracker()
                    if roi_elements is not None:

                        left = box[1] + roi_elements.roi_area.coord_right
                        top = box[0] + roi_elements.roi_area.coord_bottom
                        right = box[3] + roi_elements.roi_area.coord_right
                        bottom = box[2] + roi_elements.roi_area.coord_bottom
                        rect = dlib.rectangle(int(left), int(top),
                                              int(right), int(bottom))
                    else:
                        left = box[1]
                        top = box[0]
                        right = box[3]
                        bottom = box[2]
                        rect = dlib.rectangle(int(left), int(top),
                                              int(right), int(bottom))
                    tracker.start_track(rgb, rect)

                    trackers.append(tracker)

        else:
            for tracker in trackers:
                status = "Tracking"

                tracker.update(rgb)
                pos = tracker.get_position()

                startX = int(pos.left())
                startY = int(pos.top())
                endX = int(pos.right())
                endY = int(pos.bottom())

                rects.append((startX, startY, endX, endY))
                # cv2.rectangle(img, (startX, startY), (endX, endY), (0, 255, 0), 2)

            #        cv2.line(img, (0, roi), (W, roi), (0, 255, 255), 2)

            objects = ct.update(rects)

            if startCountFlag and roi_elements.line is not None:
                dy = roi_elements.calcMeanYDistance()
                dx = roi_elements.calcMeanXDistance()
                angle = roi_elements.calculateAngle()

            # loop over the tracked objects
            for (objectID, centroid) in objects.items():

                if roi_elements is not None and roi_elements.line is not None:
                    if roi_elements.checkCentroidInsideLine(centroid) == False:
                        pass
                # check to see if a trackable object exists for the current
                # object ID
                to = trackableObjects.get(objectID, None)

                # if there is no existing trackable object, create one
                if to is None:
                    to = TrackableObject(objectID, centroid)

                # otherwise, there is a trackable object so we can utilize it
                # to determine direction
                else:
                    # the difference between the y-coordinate of the *current*
                    # centroid and the mean of *previous* centroids will tell
                    # us in which direction the object is moving (negative for
                    # 'up' and positive for 'down')
                    y = [c[1] for c in to.centroids]
                    directionY = centroid[1] - np.mean(y)
                    x = [c[0] for c in to.centroids]
                    directionX = centroid[0] - np.mean(x)

                    if not to.counted:

                        if roi_elements is not None and roi_elements.line is not None:

                            if angle > 45 and directionX > 0 and centroid[0] > dx:
                                totalDown += 1
                                to.counted = True

                            elif angle < 45 and directionY > 0 and centroid[1] > dy:
                                totalDown += 1
                                to.counted = True

                        elif (directionY > 0 and centroid[1] > frame_size_h // 2):
                            totalDown += 1
                            to.counted = True

                # store the trackable object in our dictionary
                trackableObjects[objectID] = to

                # draw both the ID of the object and the centroid of the
                # object on the output frame
                text = "ID {}".format(objectID)
                cv2.putText(img, text, (centroid[0] - 10, centroid[1] - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                cv2.circle(img, (centroid[0], centroid[1]), 4, (0, 255, 0), -1)

            # construct a tuple of information we will be displaying on the
            # frame
            info = [
                ("Down", totalDown),
                ("Status", status),
            ]

            # loop over the info tuples and draw them on our frame
            for (i, (k, v)) in enumerate(info):
                text = "{}: {}".format(k, v)
                cv2.putText(img, text, (10, H - ((i * 20) + 20)),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)

            if roi_elements != None:
                # roi area
                cv2.rectangle(img, (roi_elements.roi_area.coord_left, roi_elements.roi_area.coord_top),
                              (roi_elements.roi_area.coord_right, roi_elements.roi_area.coord_bottom),
                              (0, 255, 0), 2)
            if roi_elements != None and roi_elements.line != None:
                cv2.line(img, (roi_elements.line[0], roi_elements.line[1]),
                         (roi_elements.line[2], roi_elements.line[3]),
                         (255, 0, 255), 2)

            # show the output frame
            cv2.imshow("preview", img)
            key = cv2.waitKey(1) & 0xFF

            # if the `q` key was pressed, break from the loop
            if key == ord("q"):
                break

            if key == ord("s"):
                initBB = None
                initBB = cv2.selectROI("ROI_FRAME", img, fromCenter=False,
                                       showCrosshair=False)
            #            cv2.imshow("tmp", tmp)
            #            cv2.namedWindow('real image')
            #            a = cv.SetMouseCallback('real image', on_mouse, 0)
            #            cv2.imshow('real image', img)

            if key == ord("x"):
                # select the bounding box of the object we want to track (make
                # sure you press ENTER or SPACE after selecting the ROI)
                if lineFlag == False:
                    lineFlag = True
                else:
                    lineFlag = False

            if key == ord("b"):
                # select the bounding box of the object we want to track (make
                # sure you press ENTER or SPACE after selecting the ROI)
                if startCountFlag is False:
                    startCountFlag = True
                    if roi_elements is not None and roi_elements.line is None:
                        roi_elements.setLine(line)
                        totalDown = 0
                else:
                    startCountFlag = False

            # increment the total number of frames processed thus far and
            # then update the FPS counter
        totalFrames += 1
        fps.update()

        # stop the timer and display FPS information
    fps.stop()
    print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
    print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

    cv2.destroyAllWindows()


from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from airflow.operators.python_operator import BranchPythonOperator
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.bash_operator import BashOperator
from airflow.utils.dates import days_ago
from scripts.data_ingestion import ingest_data
from scripts.data_preparing import prepare_data
from scripts.data_drift import drift_detection
from scripts.model_training import train
from scripts.model_inference import inference
from scripts.model_evaluation import evaluate

# Define some arguments for our DAG
default_args = {
    'owner': 'arocketman',
    'depends_on_past': False,
    'start_date': days_ago(1),
    'catchup': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=5)
}

# Instantiate our DAG
dag = DAG(
    'orchestrated_experiment',
    default_args=default_args,
    description='A ML orchestrated experiment',
    schedule_interval=timedelta(days=1),
)

def validate_data():
	print('show data quality...')

with dag:
	drift_detection_task = BranchPythonOperator(
        task_id='drift_detection',
        provide_context=True,
        python_callable=drift_detection
	)

	data_ingestion_task = PythonOperator(
        task_id='data_ingestion',
        python_callable=ingest_data
	)

	data_validation_task = PythonOperator(
        task_id='data_validation',
        python_callable=validate_data
	)

	data_preparation_task = PythonOperator(
        task_id='data_preparation',
        python_callable=prepare_data
	)

	model_training_task = PythonOperator(
        task_id='model_training',
        provide_context=True,
        python_callable=train
	)

	model_evaluation_task = PythonOperator(
        task_id='model_evaluation',
        provide_context=True,
        python_callable=evaluate
	)

	inference_task_1 = PythonOperator(
        task_id='inference',
        provide_context=True,
        python_callable=inference
	)

	inference_task_2 = PythonOperator(
        task_id='retrained_inference',
        provide_context=True,
        python_callable=inference
	)

	git_push_task = BashOperator(
        task_id='git_push_task',
        # "scripts" folder is under "/usr/local/airflow/dags"
        bash_command="scripts/git_push.sh",
        trigger_rule='one_success',
        )

	data_ingestion_task >> data_validation_task >> data_preparation_task >> drift_detection_task >> [model_training_task, inference_task_1]
	model_training_task >> model_evaluation_task >> inference_task_2
	[inference_task_1, inference_task_2] >> git_push_task


import numpy as np
import pandas as pd
import keras
import cv2 as cv
from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator

import matplotlib.pyplot as plt

# get the dataset that was created on convert_to_csv.py
data_set = pd.read_csv(r'/home/greg/Documents/uni/AI/course_files/shuffledCK.csv', dtype='a')

# create two numpy arrays that will contain the data for emotion labels and image pixels separately
emotion_labels = np.array(data_set['emotion'])
image_pixels = np.array(data_set['imgPixels'])

# convert numpy array to list

image_pixels = image_pixels.tolist()

# convert image_pixels array to 2D array, each array index can be displayed as an image using matplotlib
faces = []

for pixels in image_pixels:
    face = [int(pixel) for pixel in pixels.split(' ')]
    face = np.asarray(face).reshape(48, 48)
    face = cv.resize(face.astype('uint8'), (48, 48))
    faces.append(face.astype('float32'))

faces = np.asarray(faces)

# normalise the data to values between 0 and 1
faces /=255

# convert label array to one hot vector ie 4:'Sad' will become [0. 0. 0. 0. 1. 0. 0.]

emotion = keras.utils.to_categorical(emotion_labels, 7)

# split emotion data and appropriate labels to a train and test set equal to the number of images

train_size = 608

# prepare the image and label data to be used for the training phase

train_X = faces[0:train_size]
train_Y = emotion[0:train_size]
train_X = train_X.reshape(-1, 48, 48, 1)

# define the type of model as sequence of layers

model = Sequential()

##############################FEATURE_LEARNING##################################

# first layer
model.add(Conv2D(64, (5, 5), activation='relu', input_shape=(48,48,1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

# second layer
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Conv2D(512, (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

##############################FEATURE_LEARNING##################################

###############################CLASSIFICATION###################################

model.add(Flatten())

model.add(Dense(512, activation='relu'))
model.add(Dropout(0.4))

model.add(Dense(256, activation='relu'))
model.add(Dropout(0.4))

model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(7, activation='softmax'))

###############################CLASSIFICATION###################################

model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

history=model.fit(train_X, train_Y, validation_split=0.17, batch_size=10, epochs=50)

model.save('modelB.h5')

model.summary()

print(history.history.keys())

#plot model history data
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

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


import argparse
import os
# http://www.gabrielserafini.com/blog/2008/08/19/mac-os-x-voices-for-using-with-the-say-command/
from random import shuffle, randint

PUNCT = ["'", ",", ".", "\"", "?"]

parser = argparse.ArgumentParser(description="Reads a string like you're having a stroke.")
parser.add_argument('-str', type=str, help='String to stroke.')
parser.add_argument('-file', help='File to stroke.')
parser.add_argument('--wild', type=int, help='Induce vomiting.')
parser.add_argument('--talk2me', type=bool, default=True, help='For when you need a shoulder to lean on.')

args = parser.parse_args()

def have(thing): print(thing)

if args.str:
    s = args.str
    s = s.lower()

elif args.file:
    with open(args.file, 'rb') as sober_thought:
        s = sober_thought.read()
        s = str(s).lower()
else:
    raise KeyError("There's nothing here!")

split = s.split(' ')
shuffle(split)

stroke_pregame = ' '.join(split)

if args.wild:
    enhanced_pregame = list(stroke_pregame)
    for drip in range(args.wild):
        victim = randint(0, len(enhanced_pregame) - 1)
        enhanced_pregame[victim] = enhanced_pregame[victim].upper()

    for e in range(len(enhanced_pregame)):
        if enhanced_pregame[e] in PUNCT:
            enhanced_pregame[e] == '!!!'

    # keep it low key that there's a good time going on in here
    stroke_pregame = enhanced_pregame

stroke = (''.join(stroke_pregame)) + '.'
stroke = stroke.replace(';', '!')

if args.talk2me:
    stroke = "say -v \"Bad News\" sa{}".format(stroke)
    os.system(stroke)
else:
    have(stroke)


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

from scapy.all import IP, ICMP, sr1
import ipaddress

myip = '172.17.0.1'
netmask = '255.255.0.0'

def gen_iplist(ip, mask):
    ipv4 = ipaddress.ip_address(ip)
    netmask = ipaddress.ip_address(netmask)
    netaddr = ipaddress.ip_address(int(ipv4) & int(netmask))
    netaddr = str(netaddr).split('/')[0]

    cidr = bin(int(netmask)).count('1')
    print(str(netaddr) + '/' + str(cidr))
    ip_network = ipaddress.ip_network(str(netaddr) + '/' + str(cidr))

    return ip_network.hosts()

ip_list = gen_iplist(myip, netmask)

for ip in ip_list:
    pkt = IP(dst=str(ip), ttl=64)/ICMP()
    reply = sr1(pkt, timeout=3)

    if reply is not None:
        print(str(ip) + ' is up.')


###############################################################
# pip install .; pytest -v --capture=no -v --nocapture tests/test_box.py:Test_box.test_001
# pytest -v --capture=no tests/test_boxr.py
# pytest -v  tests/test_installer.py
###############################################################
from pprint import pprint
from cloudmesh.management.configuration.config import Config
from cloudmesh.storage.provider.box.Provider import Provider
from cloudmesh.common.util import HEADING
from cloudmesh.common.util import path_expand
from cloudmesh.common.util import writefile
import os
import pytest
from pathlib import Path

@pytest.mark.incremental
class Test_box:

    def setup(self):
        self.p = Provider(service="box")
        self.source = location = path_expand('~/.cloudmesh/storage/test/box')
        self.source_file = source = path_expand(f'{location}/test.txt')

        os.makedirs(self.box_location, exist_ok=True)
        writefile(Path(source, 'Test content'))
        self.destination = "/"
        self.destination_file = '/test.txt'
        self.filename = 'test.txt'

    @pytest.fixture(scope='class')
    def dummy_dir(self):
        self.p = Provider(service="box")
        destination = self.destination = "/"
        test_dir = self.p.create_dir(service=self.p.service,
                                     directory=f"destination/testdir")
        return test_dir

    def test_config(self):
        config = Config()
        storage = config['cloudmesh.storage']
        print(storage)

    def test_provider(self):
        file = open(self.source_file)

        assert file.read() == 'Test content'

    def test_put(self):
        HEADING()
        test_file = self.p.put(service=self.p.service,
                               source=self.source_file,
                               destination=self.destination,
                               recursive=False)
        pprint(test_file)

        assert test_file is not None

    def test_get(self):
        HEADING()
        file = self.p.get(service=self.p.service,
                          source=self.destination_file,
                          destination=self.source,
                          recursive=False)
        pprint(file)

        assert file is not None

    def test_list(self):
        HEADING()
        contents = self.p.list(service=self.p.service,
                               source=self.destination,
                               recursive=False)
        for c in contents:
            pprint(c)

        assert len(contents) > 0

    def test_search(self):
        HEADING()
        search_files = self.p.search(service=self.p.service,
                                     directory=self.destination,
                                     filename=self.filename,
                                     recursive=False)
        pprint(search_files)

        assert len(search_files) > 0

    def test_create_dir(self, test_dir):
        HEADING()

        assert len(test_dir) > 0

    def test_delete(self, dummy_dir):
        HEADING()
        self.p.client.folder(dummy_dir[0]['id']).delete()


import wx

class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, -1, "My Frame", size=(3000, 3000))
        self.panel = wx.Panel(self,-1)
        #self.panel.Bind(wx.EVT_MOTION,  self.OnMove)
        wx.StaticText(self.panel, -1, "What are the values of X", pos=(10, 12))
        #self.posCtrl = wx.TextCtrl(self.panel, -1, "", pos=(100, 10))
        wx.CheckBox(self.panel, -1, "Apples", (20,100), (160,-1))
        wx.CheckBox(self.panel, -1, "Mango", (20,150), (160,-1))
        wx.CheckBox(self.panel, -1, "Banana", (20,200), (160,-1))
        wx.CheckBox(self.panel, -1, "Orange", (20,250), (160,-1))
        button=wx.Button(self.panel,label="OK",pos=(800, 400), size = (50,50))
        self.Bind(wx.EVT_BUTTON, self.newwindow, button)

    # def OnMove(self, event):
        # pos = event.GetPosition()
        # self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))

    def newwindow(self, event):
        secondWindow = window2(parent=self.panel)
        secondWindow.Show()

class window2(wx.Frame):

    title = "new Window"

    def __init__(self,parent):
        wx.Frame.__init__(self,parent, -1,'Window2', size=(1000,700))
        panel=wx.Panel(self, -1)

        self.SetBackgroundColour(wx.Colour(100,100,100))
        self.Centre()
        self.Show()

app = wx.App(False)
frame = MyFrame()
frame.Show(True)
app.MainLoop()


import os, json, time

from iconsdk.builder.transaction_builder import DeployTransactionBuilder
from iconsdk.builder.call_builder import CallBuilder
from iconsdk.icon_service import IconService
from iconsdk.libs.in_memory_zip import gen_deploy_data_content
from iconsdk.providers.http_provider import HTTPProvider
from iconsdk.signed_transaction import SignedTransaction

from tbears.libs.icon_integrate_test import IconIntegrateTestBase, SCORE_INSTALL_ADDRESS

from BattleBombRoyale.tests.utils import *
from BattleBombRoyale.gamestate.gamestate import GameState
from BattleBombRoyale.player.player import Player
from BattleBombRoyale.bomb.bomb import Bomb

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

class TestBattleBombRoyale(IconIntegrateTestBase):
    TEST_HTTP_ENDPOINT_URI_V3 = "http://127.0.0.1:9000/api/v3"
    SCORE_PROJECT= os.path.abspath(os.path.join(DIR_PATH, '..'))

    _PARTICIPATION_COST = 1 * 10**18

    def get_gamestate(self, token):
        # OK
        result = icx_call(super(),
            from_=self._j1.get_address(),
            to_=self._score_address,
            method="get_gamestate",
            params={'token': token},
            icon_service=self.icon_service,
        )
        result = json.loads(result)
        return GameState.deserialize(result)

    def generate_blocks(self, count):
        # Generates 10 blocks
        for _ in range(count):
            result = icx_transfer_call(super(),
                from_=self._j1,
                to_=self._j1.get_address(),
                icon_service=self.icon_service,
                value=0
            )

    def wait(self):
        time.sleep(Bomb._BOMB_TIME_FOR_AFK_EXPLODING / 1000)

    def loot(self):
        result = transaction_call_success(super(),
            from_=self._wallets_no_bomb[0],
            to_=self._score_address,
            method="loot_player",
            icon_service=self.icon_service,
            params={'looted_address' : self._has_bomb.address}
        )

    def wait_and_loot(self):
        self.wait()
        self.loot()

    def get_balance(self, player):
        return get_icx_balance(super(), address=player.address, icon_service=self.icon_service)

    def refresh_state(self):
        self.gamestate = self.get_gamestate(self._token)
        self._has_bomb = list(filter(lambda player: player.has_bomb(), self.gamestate.get_all_players()))[0]
        self._has_bomb_wallet = list(filter(lambda wallet: wallet.address == self._has_bomb.address, self._wallet_array))[0]

        self._players_no_bomb = list(filter(lambda player: not player.has_bomb(), self.gamestate.get_all_players()))
        self._wallets_no_bomb = []
        for player in self._players_no_bomb:
            for wallet in self._wallet_array:
                if player.address == wallet.get_address():
                    self._wallets_no_bomb.append(wallet)

    def start_game(self):
        # OK
        result = transaction_call_success(super(),
            from_=self._j1,
            to_=self._score_address,
            method="start_game",
            icon_service=self.icon_service
        )
        self.refresh_state()

    def set_ready(self):
        # OK
        result = transaction_call_success(super(),
            from_=self._j1,
            to_=self._score_address,
            method="ready_ask",
            icon_service=self.icon_service
        )

        # OK
        result = transaction_call_success(super(),
            from_=self._j2,
            to_=self._score_address,
            method="ready_ok",
            icon_service=self.icon_service
        )

    def setUp(self):
        super().setUp()

        self.icon_service = None
        # if you want to send request to network, uncomment next line and set self.TEST_HTTP_ENDPOINT_URI_V3
        # self.icon_service = IconService(HTTPProvider(self.TEST_HTTP_ENDPOINT_URI_V3))

        # install SCORE
        self._score_address = self._deploy_score()['scoreAddress']

        self._j1 = self._wallet_array[0]
        self._j2 = self._wallet_array[1]
        self._j3 = self._wallet_array[2]
        self._j4 = self._wallet_array[3]
        self._spectator = self._wallet_array[9]

        for wallet in self._wallet_array:
            icx_transfer_call(super(), self._test1, wallet.get_address(), 100 * 10**18, self.icon_service)

        # OK
        result = transaction_call_success(super(),
            from_=self._j1,
            to_=self._score_address,
            method="create_game",
            icon_service=self.icon_service,
            value=self._PARTICIPATION_COST
        )
        self._token = result['txHash']

        # OK
        result = transaction_call_success(super(),
            from_=self._j2,
            to_=self._score_address,
            method="join_game",
            params={'token': self._token},
            icon_service=self.icon_service,
            value=self._PARTICIPATION_COST
        )

    def _deploy_score(self, to: str = SCORE_INSTALL_ADDRESS) -> dict:
        # Generates an instance of transaction for deploying SCORE.
        transaction = DeployTransactionBuilder() \
            .from_(self._test1.get_address()) \
            .to(to) \
            .step_limit(100_000_000_000) \
            .nid(3) \
            .nonce(100) \
            .content_type("application/zip") \
            .content(gen_deploy_data_content(self.SCORE_PROJECT)) \
            .build()

        # Returns the signed transaction object having a signature
        signed_transaction = SignedTransaction(transaction, self._test1)

        # process the transaction in local
        result = self.process_transaction(signed_transaction, self.icon_service)

        self.assertTrue('status' in result)
        self.assertEqual(1, result['status'])
        self.assertTrue('scoreAddress' in result)

        return result

    # ===============================================================
    def test_win_game_ok(self):
        self.set_ready()
        self.start_game()
        old_balance = self.get_balance(self._players_no_bomb[0])
        self.wait_and_loot()

        # OK
        result = transaction_call_success(super(),
            from_=self._wallets_no_bomb[0],
            to_=self._score_address,
            method="win_game",
            icon_service=self.icon_service
        )

        # Check reward
        new_balance = self.get_balance(self._players_no_bomb[0])
        self.assertTrue(new_balance > old_balance)

    def test_win_game_METHOD_NOT_PAYABLE(self):
        self.set_ready()
        self.start_game()
        self.wait_and_loot()
        # Fail
        result = transaction_call_error(super(),
            from_=self._wallets_no_bomb[0],
            to_=self._score_address,
            method="win_game",
            icon_service=self.icon_service,
            value=123
        )
        self.assertTrue('Method not payable' in result['failure']['message'])

    def test_win_game_GAME_ISNT_VICTORY(self):
        self.set_ready()
        self.start_game()
        # Fail
        result = transaction_call_error(super(),
            from_=self._wallets_no_bomb[0],
            to_=self._score_address,
            method="win_game",
            icon_service=self.icon_service
        )
        self.assertEqual(result['failure']['message'], 'GAME_ISNT_VICTORY')

    def test_win_game_PLAYER_IS_NOT_WINNER(self):
        self.set_ready()
        self.start_game()
        self.wait_and_loot()
        # Fail
        result = transaction_call_error(super(),
            from_=self._has_bomb_wallet,
            to_=self._score_address,
            method="win_game",
            icon_service=self.icon_service
        )
        self.assertEqual(result['failure']['message'], 'PLAYER_IS_NOT_WINNER')

    def test_win_game_before_looting_player_last_turn(self):
        self.set_ready()
        self.start_game()
        self.wait()
        # Fail
        result = transaction_call_error(super(),
            from_=self._wallets_no_bomb[0],
            to_=self._score_address,
            method="win_game",
            icon_service=self.icon_service
        )
        self.assertEqual(result['failure']['message'], 'GAME_ISNT_VICTORY')

    def test_win_game_GAME_NOT_STARTED(self):
        # Fail
        result = transaction_call_error(super(),
            from_=self._j1,
            to_=self._score_address,
            method="win_game",
            icon_service=self.icon_service
        )
        self.assertEqual(result['failure']['message'], 'GAME_NOT_STARTED')


import cv2
import numpy as np

# Load an color image in grayscale
img = cv2.imread('fisdom.jpg',0)
ret, im_thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV )
cv2.imshow("image",im_thresh)
cv2.waitKey(0)

kernel = np.ones((3,3),np.uint8)
dilate = cv2.dilate(im_thresh,kernel,iterations = 1)
cv2.imshow("dialte",dilate)
cv2.waitKey(0)
# cv2.imwrite("result1.jpg",im_thresh_dil)


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

import time

import compas
import compas_rhino

from compas_rhino.artists.mixins import VertexArtist
from compas_rhino.artists.mixins import EdgeArtist
from compas_rhino.artists.mixins import FaceArtist

try:
    import rhinoscriptsyntax as rs

except ImportError:
    compas.raise_if_ironpython()

__all__ = ['VolMeshArtist']

class VolMeshArtist(FaceArtist, EdgeArtist, VertexArtist):
    """A volmesh artist defines functionality for visualising COMPAS volmeshes in Rhino.

    Parameters
    ----------
    volmesh : compas.datastructures.VolMesh
        A COMPAS volmesh.
    layer : str, optional
        The name of the layer that will contain the volmesh.

    Attributes
    ----------
    defaults : dict
        Default settings for color, scale, tolerance, ...

    """

    def __init__(self, volmesh, layer=None):
        self.volmesh = volmesh
        self.layer = layer

    @property
    def layer(self):
        """str: The layer that contains the volmesh."""
        return self.volmesh.attributes.get('layer')

    @layer.setter
    def layer(self, value):
        self.volmesh.attributes['layer'] = value

    @property
    def volmesh(self):
        """compas.datastructures.VolMesh: The volmesh that should be painted."""
        return self.datastructure

    @volmesh.setter
    def volmesh(self, volmesh):
        self.datastructure = volmesh

    def redraw(self, timeout=None):
        """Redraw the Rhino view.

        Parameters
        ----------
        timeout : float, optional
            The amount of time the artist waits before updating the Rhino view.
            The time should be specified in seconds.
            Default is ``None``.

        """
        if timeout:
            time.sleep(timeout)
        rs.EnableRedraw(True)
        rs.Redraw()

    def clear_layer(self):
        """Clear the main layer of the artist."""
        if self.layer:
            compas_rhino.clear_layer(self.layer)

    def clear(self):
        """Clear the vertices, faces and edges of the volmesh, without clearing the
        other elements in the layer."""
        self.clear_vertices()
        self.clear_faces()
        self.clear_edges()

# ==============================================================================
# Main
# ==============================================================================

if __name__ == "__main__":

    import compas

    from compas.datastructures import VolMesh
    from compas_rhino.artists.volmeshartist import VolMeshArtist

    volmesh = VolMesh.from_obj(compas.get('boxes.obj'))

    artist = VolMeshArtist(volmesh, layer='VolMeshArtist')

    artist.clear_layer()

    artist.draw_vertices()
    artist.redraw(0.0)

    artist.draw_vertexlabels()
    artist.redraw(1.0)

    artist.draw_faces()
    artist.redraw(1.0)

    artist.draw_facelabels()
    artist.redraw(1.0)

    artist.draw_edges()
    artist.redraw(1.0)

    artist.draw_edgelabels()
    artist.redraw(1.0)


import numpy as np
import random
import sys
import os
from PIL import Image, ImageOps
from skimage.io import imsave
import threading
import keras
import math
from shutil import copyfile

from keras import backend as K
import tensorflow as tf

class Data_manager:

    def get_train_x_y_bymodel(self, image_names, image_folder, img_height, img_length, model_dict, models_count):
        self.threads = []
        self.images_array = np.ndarray(shape=(len(image_names), img_height, img_length, 3), dtype = np.float32)
        self.data_array = np.zeros(shape=(len(image_names), models_count), dtype = np.float32)

        image_count = 0
        for image_name in image_names:
            if(image_name.endswith(('.jpg', '.png'))):

                thread = threading.Thread(target= self.read_picture_by_model, args=(image_count, image_folder, image_name,
                                                                                img_height, img_length, model_dict))
                thread.daemon = True
                thread.start()
                self.threads.append(thread)

                print('Sample: {} of {}'.format(image_count, len(image_names)), end = '\r')
                image_count = image_count + 1

        for thread in self.threads:
            thread.join()

        return self.images_array, self.data_array

    def read_picture_by_model(self, index, image_folder, image_name, img_height, img_length, model_dict):
        filename = os.path.join(image_folder, image_name)

        image = Image.open(filename)

        image = image.resize(size =(img_height, img_length))

        try:
            klase = self.class_from_name(image_name)

            if(klase in model_dict):
                hot_encode_index = model_dict[klase]
                self.data_array[index, hot_encode_index] = 1
                self.images_array[index] = np.expand_dims(np.array(image).astype(float), axis = 0) / 255.
        except:
            print("failed to input: " +filename)

    def get_train_x_y_by_model_and_year(self, image_names, image_folder, img_height, img_length, model_dict, models_count, first_year, last_year):
        self.threads = []
        self.images_array = np.ndarray(shape=(len(image_names), img_height, img_length, 3), dtype = np.float32)
        self.model_array = np.zeros(shape=(len(image_names), models_count), dtype = np.float32)
        self.year_array = np.zeros(shape=(len(image_names), last_year - first_year + 1), dtype = np.float32)

        image_count = 0
        for image_name in image_names:
            if(image_name.endswith(('.jpg', '.png'))):

                thread = threading.Thread(target= self.read_picture_by_model_and_year, args=(image_count, image_folder, image_name,
                                                                                img_height, img_length, model_dict, first_year, last_year))
                thread.daemon = True
                thread.start()
                self.threads.append(thread)

                print('Sample: {} of {}'.format(image_count, len(image_names)), end = '\r')
                image_count = image_count + 1

        for thread in self.threads:
            thread.join()

        return self.images_array, self.model_array, self.year_array

    def read_picture_by_model_and_year(self, index, image_folder, image_name, img_height, img_length, model_dict, first_year, last_year):
        filename = os.path.join(image_folder, image_name)

        image = Image.open(filename)

        image = image.resize(size =(img_height, img_length))

        try:
            klase = self.class_from_name(image_name)
            metai = self.year_from_name(image_name)

            if(klase in model_dict):
                hot_encode_index = model_dict[klase]
                self.model_array[index, hot_encode_index] = 1

                hot_encode_index = metai - first_year
                self.year_array[index, hot_encode_index] = 1

                self.images_array[index] = np.expand_dims(np.array(image).astype(float), axis = 0) / 255.
        except:
            print("failed to input: " +filename)

    def get_train_x_y_byprice(self, image_names, image_folder, img_height, img_length, price_interval_len, price_interval_count):
        self.threads = []
        self.images_array = np.ndarray(shape=(len(image_names), img_height, img_length, 3), dtype = np.float32)
        #self.data_array = np.zeros(shape=(len(image_names), price_interval_count), dtype = np.float32)
        self.data_array = np.zeros(shape=(len(image_names), 1), dtype = np.float32)

        image_count = 0
        for image_name in image_names:
            if(image_name.endswith(('.jpg', '.png'))):

                #thread = threading.Thread(target= self.read_picture_by_price_by_class, args=(image_count, image_folder, image_name, img_height, img_length, price_interval_len))
                thread = threading.Thread(target= self.read_picture_by_price_one_float, args=(image_count, image_folder, image_name, img_height, img_length, price_interval_len))
                thread.daemon = True
                thread.start()
                self.threads.append(thread)

                print('Sample: {} of {}'.format(image_count, len(image_names)), end = '\r')
                image_count = image_count + 1

        for thread in self.threads:
            thread.join()

        return self.images_array, self.data_array

    def read_picture_by_price_one_float(self, index, image_folder, image_name, img_height, img_length, price_interval_len):
        filename = os.path.join(image_folder, image_name)

        image = Image.open(filename)

        image = image.resize(size =(img_height, img_length))###########################################RESIZE

        try:
            data = image_name.split()
            price = int(data[2])

            self.data_array[index, 0] = float(price)
            self.images_array[index] = np.expand_dims(np.array(image), axis = 0) / 255.
        except:
            print("failed to input: " +filename)

    def read_picture_by_price_by_class(self, index, image_folder, image_name, img_height, img_length, price_interval_len):
        filename = os.path.join(image_folder, image_name)

        image = Image.open(filename)

        image = image.resize(size =(img_height, img_length))###########################################RESIZE

        try:
            data = image_name.split()
            price = int(data[2])

            hot_encode_index = int(math.floor(price/price_interval_len))
            self.data_array[index, hot_encode_index] = 1
            self.images_array[index] = np.expand_dims(np.array(image), axis = 0) / 255.
        except:
            print("failed to input: " +filename)

    def deal_with_one_picture(self, input_path, output_path, img_height, img_length):
        image = Image.open(input_path)

        img_box = image.getbbox()
        temp_lenght = img_box[3]
        temp_height = img_box[2]

        #try:
        background = Image.new('RGB', (img_height, img_length), (0, 0, 0))

        original_height = temp_lenght
        original_width = temp_height
        designer_height = img_length
        designer_width = img_height

        if (original_width * designer_height > designer_width * original_height):
            designer_height = int((designer_width * original_height) / original_width)
        else:
            designer_width = int((designer_height * original_width) / original_height)

        new_image = image.resize((int(designer_width), int(designer_height)), Image.NEAREST)

        #upper left corner
        x = int(abs(img_length - designer_width) / 2)
        y = int(abs(img_height - designer_height) / 2)

        background.paste(new_image, (x, y, x+designer_width, y+designer_height))

        #new_image  = image.crop((x, y, x+new_height, y+new_length))
        #new_image = image.resize((img_height, img_length), Image.NEAREST)

        imsave(output_path,np.array(background))

        #####MAKE MORE IMAGE VARIATIONS#####
        ####################################
        extension = output_path[-4:]
        output_path = output_path[:-4]

        background = Image.new('RGB', (img_height, img_length), (0, 0, 0))
        background.paste(ImageOps.mirror(new_image), (x, y, x+designer_width, y+designer_height))
        imsave(output_path+"(1)"+extension,np.array(background))

        if(img_height / img_length != designer_width/designer_height):
            x = 0
            y = 0

            background = Image.new('RGB', (img_height, img_length), (0, 0, 0))
            background.paste(new_image, (x, y, x+designer_width, y+designer_height))
            imsave(output_path+"(2)"+extension,np.array(background))

            background.paste(ImageOps.mirror(new_image), (x, y, x+designer_width, y+designer_height))
            imsave(output_path+"(3)"+extension,np.array(background))

            x = int(abs(img_length - designer_width))
            y = int(abs(img_height - designer_height))

            background = Image.new('RGB', (img_height, img_length), (0, 0, 0))
            background.paste(new_image, (x, y, x+designer_width, y+designer_height))
            imsave(output_path+"(4)"+extension,np.array(background))

            background.paste(ImageOps.mirror(new_image), (x, y, x+designer_width, y+designer_height))
            imsave(output_path+"(5)"+extension,np.array(background))
        #####MAKE MORE IMAGE VARIATIONS#####
        ####################################

        #except:
        #    print("Failed to transform: "+input_path)
        #    return None

    def prepare_images(self, source_folder, destination_folder, img_height, img_length):
        names = os.listdir(source_folder)

        threads = []

        for n in names:
            if(n.endswith(('.jpg', '.png'))):
                input_path = os.path.join(source_folder, n)
                output_path = os.path.join(destination_folder, n)

                print('{}                                    '.format(input_path), end = '\r')
                #print('{}                                    '.format(input_path))
                new_thread = threading.Thread(target = self.deal_with_one_picture, args=(input_path, output_path, img_height, img_length))
                new_thread.daemon = True
                threads.append(new_thread)
                new_thread.start()

        for thread in threads:
            thread.join()

    def create_class_dict(self, image_names, good_samples):
        class_dict = {}
        classes_count = 0

        year_min = 3000
        year_max = 1000

        for name in image_names:
            klase = self.class_from_name(name)

            if(klase not in class_dict
            and name.split()[0] in good_samples):
                class_dict[klase] = classes_count
                classes_count = classes_count + 1

            year = self.year_from_name(name)
            year_max = max(year_max, int(year))
            year_min = min(year_min, int(year))

        return class_dict, classes_count, int(year_min), int(year_max)

    def year_from_name(self, image_name):
        return int(image_name.split()[3])

    def class_from_name(self, image_name):
        data = image_name.split()
        manufactor = data[0]
        model = data[1]

        if(manufactor == "bmw"):
            if(len(model) == 3):
                model = model[0]+" serija"
            if(model.endswith("gran_turismo")):
                model = model[0]+" gran turismo"
            if(model.endswith("gran_coupe")):
                model = model[0]+" gran coupe"
            if(model.endswith("active_tourer")):
                model = model[0]+" active tourer"
            if(model.endswith("gran_tourer")):
                model = model[0]+" gran tourer"

        if(manufactor == "mercedes_benz"):
            if(len(model) == 4):
                model = model[0]+" klase"
            if(model.startswith("cla")):
                model = "cla klase"
            if(model.startswith("clc")):
                model = "clc klase"
            if(model.startswith("clk")):
                model = "clk klase"
            if(model.startswith("cls")):
                model = "cls klase"
            if(model.startswith("gl")):
                model = "gl klase"
            if(model.startswith("gla")):
                model = "gla klase"
            if(model.startswith("glc")):
                model = "glc klase"
            if(model.startswith("gle")):
                model = "gle klase"
            if(model.startswith("glk")):
                model = "glk klase"
            if(model.startswith("gls")):
                model = "gls klase"
            if(model.startswith("ml")):
                model = "ml klase"
            if(model.startswith("slk")):
                model = "slk klase"
            if(model.startswith("sl")):
                model = "sl klase"

        return manufactor + " " + model

    def save_classes(self, classes_dict, first_year, last_year, file_name):
        text_file = open(file_name, "w+")

        for model in classes_dict:
            #text_file.write(model+" "+str(classes_dict[model])+"\n")
            text_file.write(model+"\n")

        text_file.write("Years: {}-{}".format(first_year, last_year))

        text_file.close()

        print("Classes dictionary saved!")

def separate_validation_data(source_folder, destination_folder, image_names):
    print("From: {}. To: {}".format(source_folder, destination_folder))

    for name in image_names:
        os.rename(os.path.join(source_folder, name), os.path.join(destination_folder, name))
        #copyfile(os.path.join(source_folder, name), os.path.join(destination_folder, name))

############################################################################################################
#TESTING HELPER FUNCTIONS
############################################################################################################

if __name__ == "__main__":
    source_folder = sys.argv[1]
    destination_folder = sys.argv[2]

    try:
        count = int(sys.argv[3])
    except:
        count = 0

    #source_folder = 'C:/Users/Jstanke/Downloads/test'
    #destination_folder = 'C:/Users/Jstanke/Downloads/new'

    img_height = 300
    img_length = 300

    print("From: {}. To: {}".format(source_folder, destination_folder))

    if(count > 0):
        image_names = np.array([f for f in os.listdir(source_folder) if
                    os.path.isfile(os.path.join(source_folder, f)) and f.endswith(('.png', '.jpg'))])

        shuffled_image_indexes = random.sample(range(0, len(image_names)), count)

        separate_validation_data(source_folder, destination_folder, image_names[shuffled_image_indexes])
    else:
        Data_manager().prepare_images(source_folder, destination_folder, img_height, img_length)

    """
    #source_folder = 'C:\\git_repos\\DATA FOR TRAINING\\AutoPliusData(Clean + formated)'
    #destination_folder = 'C:\\git_repos\\DATA FOR TRAINING\\test_folder'
    image_names = np.array([f for f in os.listdir(source_folder) if
                os.path.isfile(os.path.join(source_folder, f)) and f.endswith(('.png', '.jpg'))])

    data = Data_manager()
    image_array, data_array = data.get_train_x_y(image_names[:200], source_folder, img_height, img_length)

    cnt = 0
    for image in image_array:
        output_path = os.path.join(destination_folder, image_names[cnt])

        imsave(output_path, image)

        cnt = cnt + 1
    """


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

from data import *
from geometry import *
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, \
    NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import filedialog
from matplotlib import pyplot as plt
import matplotlib
matplotlib.use("TkAgg")
LARGE_FONT = ("Verdana", 12)

# Variables globales
global group
group = import_data("villes.csv")
group.name = "Test"

class MainPage(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Travelling salesman problem")

        # Définition des frames
        self.commandes = tk.Frame(self)
        self.commandes.grid(row=0, column=0, sticky=W+E+N+S)
        self.affichage = tk.Frame(self)
        self.affichage.grid(row=2, column=0, sticky=W+E)
        self.fig = plt.figure(1)
        canvas = FigureCanvasTkAgg(self.fig, self.affichage)
        canvas.get_tk_widget().pack(side="top", fill=BOTH, expand=True)
        toolbar = NavigationToolbar2TkAgg(canvas, self.affichage)
        toolbar.update()
        canvas._tkcanvas.pack(side="top", fill=BOTH, expand=True)

        # Commandes
        button = ttk.Button(self.commandes, text="Charger groupe",
                            command=lambda: self.charger())
        button.grid(row=0, column=0)
        button2 = ttk.Button(self.commandes, text="Ajouter noeud",
                             command=lambda: self.ajouter_noeud())
        button2.grid(row=0, column=1)
        button3 = ttk.Button(self.commandes, text="Calcul",
                             command=lambda: self.calcul(group))
        button3.grid(row=0, column=2)

        # Nouveau noeud
        nv_noeud = tk.Frame(self)
        nv_noeud.grid(row=1, column=0, sticky=W+E)
        Label(nv_noeud, text="Nom du noeud :").grid(row=1, column=0)
        self.nv_noeud_nom = Entry(nv_noeud, textvariable=StringVar())
        self.nv_noeud_nom.grid(row=1, column=1)
        Label(nv_noeud, text="Index :").grid(row=1, column=2)
        self.nv_noeud_index = Entry(nv_noeud, textvariable=StringVar())
        self.nv_noeud_index.grid(row=1, column=3)
        Label(nv_noeud, text="X = ").grid(row=1, column=4)
        self.nv_noeud_X = Entry(nv_noeud, textvariable=StringVar())
        self.nv_noeud_X.grid(row=1, column=5)
        Label(nv_noeud, text="Y = ").grid(row=1, column=6)
        self.nv_noeud_Y = Entry(nv_noeud, textvariable=StringVar())
        self.nv_noeud_Y.grid(row=1, column=7)

    def charger(self):
        global group
        filename = filedialog.askopenfilename()
        group = import_data(filename)
        group.name = filename
        print("Chargement du fichier {}".format(filename))
        print(group)
        return group

    def ajouter_noeud(self):
        global group
        # group += Node(index=14, name="Barcelone", xy=[2.17340,41.38506])
        group += Node(index=int(self.nv_noeud_index.get()),
                      name=self.nv_noeud_nom.get(),
                      xy=[float(self.nv_noeud_X.get()),
                          float(self.nv_noeud_Y.get())])
        print(group)
        return group

    def calcul(self, group):
        self.fig.clear()
        self.fig.add_axes([0.1, 0.1, 0.8, 0.8])
        path1, cost1 = travelling_salesman_problem(group, CostMatrix(group),
                                                   ["Paris", "Moscou"],
                                                   self.fig.axes[0])
        self.fig.canvas.draw()


"""empty message

Revision ID: 28d2f8286594
Revises: b414acd761f1
Create Date: 2019-08-18 18:53:16.086031

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '28d2f8286594'
down_revision = 'b414acd761f1'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('game_data', sa.Column('venue', sa.String(length=64), nullable=True))
    op.create_index(op.f('ix_game_data_venue'), 'game_data', ['venue'], unique=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_game_data_venue'), table_name='game_data')
    op.drop_column('game_data', 'venue')
    # ### end Alembic commands ###


# encoding: utf-8

from opendatatools import futures
import datetime
import os
import pandas as pd
import math
import tushare as ts
import datetime
pd.set_option('display.width', None)  # 设置字符显示宽度
pd.set_option('display.max_rows', None)  # 设置显示最大行
pd.set_option('display.max_columns', None)  # 设置显示最大行

if __name__ == '__main__':

    if os.path.exists(r"c:\price.csv"):
        os.remove(r"c:\price.csv")

    begin = datetime.date(2018, 10, 25)
    end = datetime.date(2018, 10, 25)
    for i in range((end - begin).days + 1):
        day = begin + datetime.timedelta(days=i)
        days = day.strftime('%Y-%m-%d')
        dce = ts.get_dce_daily(days)
        shf = ts.get_shfe_daily(days)
        zce = ts.get_czce_daily(days)
        cff = ts.get_cffex_daily(days)
        frames = [dce, shf, zce, cff]
        try:
            df2 = pd.concat(frames)
            #print(df2)
            df2 = df2.dropna(axis=0, how='any')
            df2['close'] = df2['close'].astype(float)
            df2['test'] = df2['close'] * df2['volume']
            print(df2.head())
            df2 = df2.groupby('variety')['volume', 'test'].sum()
            df2['set_close'] = round(df2['test'] / df2['volume'])
            df2['date'] = days
            df2 = df2.dropna(axis=0, how='any')
            df2 = df2.reset_index()
            df2 = df2[['date', 'variety', 'set_close']]
        except:
            print(days, frames, '数据异常')
            continue
        if os.path.exists(r"c:\price.csv"):
            df2.to_csv(r"c:\price.csv", mode='a', encoding='ANSI', header=False)
        else:
            df2.to_csv(r"c:\price.csv", encoding='ANSI')


#!/usr/bin/env python

import six

import agate

def csv2csv(f, **kwargs):
    """
    "Convert" a CSV into a new CSV by normalizing types and correcting for other anomalies.
    """
    table = agate.Table.from_csv(f, **kwargs)

    output = six.StringIO()
    table.to_csv(output)
    result = output.getvalue()
    output.close()

    return result


from typing import Dict
import pandas as pd
import datetime as dt
from src.typeDefs.iexDamRecord import IIexDamDataRecord
from typing import List

def getIexDamData(targetFilePath: str) -> List[IIexDamDataRecord]:
    iexDamRecords: List[IIexDamDataRecord] = []

    iexDamDf = pd.read_excel(
        targetFilePath, sheet_name="MarketMinute", skiprows= 5, nrows= 96)
    iexDamDf = iexDamDf.rename(columns={
        'None.1': 'time_block', 'Date | Hour | Time Block': 'Date'})
    # for itr in range(iexDamDf['None.1']):
    #     iexDamDf[]
    # del iexDamDf[None]
    iexDamDf = iexDamDf.drop([None, 'Unnamed: 4'], axis=1)
    iexDamDf = iexDamDf.loc[:, ~iexDamDf.columns.duplicated()]
    iexDamDf[['first_block','First','last_block']] = iexDamDf.time_block.str.split(" ",expand=True)
    # iexDamDf[['First','Last']] = iexDamDf.time_block.str.split(expand=True)
    iexDamDf = iexDamDf.drop(['First', 'last_block', 'time_block'], axis=1)
    iexDamDf[['hour','minute']] = iexDamDf.first_block.str.split(":",expand=True)
    iexDamDf['Date'] = iexDamDf['Date'][0]
    iexDamDf[['day','month','year']] = iexDamDf.Date.str.split("-",expand=True)
    iexDamDf['Date'] = pd.to_datetime(iexDamDf[['year', 'month', 'day', 'hour', 'minute']])
    iexDamDf = iexDamDf.drop(['year', 'month', 'day', 'first_block','hour','minute'], axis=1)
    iexDamDf = pd.melt(iexDamDf, id_vars=['Date'])
    # iexDamDf = iexDamDf.rename(columns={
    #     'variable': 'metric_name', 'value': 'data_val',
    #     'Date': 'data_time'})
    iexDamRecords = iexDamDf.to_dict('records')

    return iexDamRecords


# Yet to be finished

import requests
from bs4 import BeautifulSoup

comics_url = "https://www.smbc-comics.com/"

req = requests.get(comics_url)
soup = BeautifulSoup(req.content, 'html5lib')

def downloadImage(url):
    imagesrc = soup.find(id="cc-comic").get('src')

prev_comic = soup.findAll("a", {"class": "cc-prev"})[0].get('href')


import numpy as np
import torch

class ReplayBuffer:
    """
    https://github.com/openai/spinningup/blob/master/spinup/algos/pytorch/ddpg/ddpg.py
    """
    def __init__(self, obs_shape, act_shape, buffer_size):
        buffer_obs_shape = tuple([buffer_size]) + obs_shape
        buffer_act_shape = tuple([buffer_size]) + act_shape
        self.obs_buf = np.zeros(buffer_obs_shape, dtype=np.float32)
        self.obs2_buf = np.zeros(buffer_obs_shape, dtype=np.float32)
        self.act_buf = np.zeros(buffer_act_shape, dtype=np.float32)
        self.rew_buf = np.zeros(buffer_size, dtype=np.float32)
        self.done_buf = np.zeros(buffer_size, dtype=np.float32)
        self.pointer, self.size, self.buffer_size = 0, 0, buffer_size

    def store(self, obs, act, rew, next_obs, done):
        self.obs_buf[self.pointer] = obs
        self.obs2_buf[self.pointer] = next_obs
        self.act_buf[self.pointer] = act
        self.rew_buf[self.pointer] = rew
        self.done_buf[self.pointer] = done
        self.pointer = (self.pointer+1) % self.buffer_size
        self.size = min(self.size+1, self.buffer_size)

    def sample_batch(self, batch_size=32):
        idxs = np.random.randint(0, self.size, size=batch_size)
        batch = dict(obs=self.obs_buf[idxs],
                     obs2=self.obs2_buf[idxs],
                     act=self.act_buf[idxs],
                     rew=self.rew_buf[idxs],
                     done=self.done_buf[idxs])
        return {k: torch.as_tensor(v, dtype=torch.float32) for k,v in batch.items()}

    def __len__(self):
        return self.size