import pandas as pd
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
from os import listdir
from os.path import isfile, join, isdir
import argparse
from  sklearn.metrics import accuracy_score
#Get files from exp directory
import numpy as np
import matplotlib.ticker as ticker
from matplotlib.ticker import FuncFormatter

from matplotlib.ticker import PercentFormatter
current_palette_7 = sns.color_palette("hls", 7)
sns.set_palette("Set2")

def get_acc(input_path_log ):
    df1= pd.read_csv(input_path_log, sep=";")
    return accuracy_score(df1["Pred"],df1["Golden"])

def main(args):

    datasets = ["AMAZON" , "NEWS" , "STANFORD"]

    batch_size_inc=[]
    #tools = ["CHAR" , "GLOVE" , "GOOGLE_NEWS" , "BERT" , "TXT_CNN" , "VADER"]
    #dirs=[join(args.log_path,d) for d in listdir(args.log_path)]
    #embbedings=[d for d in listdir(args.log_path)]
    fig = plt.figure (figsize=(1, 2))
    nb_dir=0
    j=0
    #dfi = pd.DataFrame (columns=["AMAZON" , "NEWS" , "STANFORD"], index=embbedings)
    #for dir in dirs:
    #    means = []
    #    nb_dir=nb_dir +1
    onlyfiles=[join(args.log_path,f) for f in listdir(args.log_path) if isfile(join(args.log_path,f)) ]
    i = 0
    for file in onlyfiles:

            markers = ["^" , "o" , "s"]
            df=pd.read_csv(file, sep=";")
            sizes=df["size"].values
            #accs=df["acc"].values
            #print(accs)
            incs=df["inc"].values
            ax = fig.add_subplot (1 ,1 , 1)
            sns.lineplot (sizes,incs  , ax=ax , markersize=6,marker=markers[i],linewidth=1,markerfacecolor='white',
         markeredgecolor='black',
         markeredgewidth=0.5)
            ax.spines['right'].set_visible (False)
            ax.spines['top'].set_visible (False)
            #ax = fig.add_subplot (1 , 2 , 2)
            #sns.lineplot (sizes,accs  , ax=ax , markersize=6,marker=markers[i], linewidth=1,markerfacecolor='white', markeredgecolor='black', markeredgewidth=0.5)
            ax.set_xticks (sizes)
            ax.spines['right'].set_visible (False)
            ax.spines['top'].set_visible (False)

            plt.xticks ( rotation=45,fontsize=8)
            plt.yticks (rotation=45,fontsize=6)

            i=i+1
            #means.append (get_inc_mean_inc(join(dir, file)))
            #ets[dataset] , df[column].mean () , color="black")

        #ax = fig.add_subplot (2 , 2 , nb_dir)
        #plt.bar (datasets , means , color="black")
        #ax.set_title (embbedings[nb_dir - 1] , fontsize=10)

    #ax.text (10 , 1  , str (mean_1) , color='blue' , fontweight='bold')
    #ax.get_legend ().remove ()
    import matplotlib.ticker as mtick
    ax = fig.add_subplot (1 , 1, 1)
    ax.set_ylabel ("Inconsistency_mean" , fontsize=8)
    ax.set_xlabel ("batch_size" , fontsize=8)
    ax.set_title("Inc & batch size",fontsize=8)
    ax.xaxis.set_major_formatter (mtick.FormatStrFormatter ('%.1e'))
    #ax = fig.add_subplot (1 , 2 , 2)
    #ax.set_ylabel ("Accuracy" , fontsize=8)
    #ax.set_xlabel ("batch_size" , fontsize=8)
    #plt.figlegend (datasets , loc='center left' , ncol=3 , labelspacing=0., prop={'size': 5})
    #ax.set_title("Acc and batch size",fontsize=8)

    #ax.legend (loc="lower left" ,  prop={'size': 6})
    #ax = fig.add_subplot (1 , 2 , 2)
    #plt.axis ('off')
    plt.show ()
if __name__ =='__main__':
    parser = argparse.ArgumentParser (description='Syntactically Controlled Paraphrase Transformer')

    parser.add_argument ('--log_path' , type=str ,
                         default="D:/Users/wissam/Documents/These/these/papers_material/VLDB_submission/public_folder/experiments/logs/intool_inc_polar_fact" ,
                         help='input path of logs')
    args = parser.parse_args ()
    main(args)


# -*- coding: utf-8 -*-
# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.utils import get_first_day, get_last_day, date_diff, flt, getdate
from frappe.model.document import Document
from verp.utilities.product import get_price

class SubscriptionPlan(Document):
	def validate(self):
		self.validate_interval_count()

	def validate_interval_count(self):
		if self.billing_interval_count < 1:
			frappe.throw(_('Billing Interval Count cannot be less than 1'))

@frappe.whitelist()
def get_plan_rate(plan, quantity=1, customer=None, start_date=None, end_date=None, prorate_factor=1):
	plan = frappe.get_doc("Subscription Plan", plan)
	if plan.price_determination == "Fixed Rate":
		return plan.cost * prorate_factor

	elif plan.price_determination == "Based On Price List":
		if customer:
			customer_group = frappe.db.get_value("Customer", customer, "customer_group")
		else:
			customer_group = None

		price = get_price(item_code=plan.item, price_list=plan.price_list, customer_group=customer_group, company=None, qty=quantity)
		if not price:
			return 0
		else:
			return price.price_list_rate * prorate_factor

	elif plan.price_determination == 'Monthly Rate':
		start_date = getdate(start_date)
		end_date = getdate(end_date)

		no_of_months = (end_date.year - start_date.year) * 12 + (end_date.month - start_date.month) + 1
		cost = plan.cost * no_of_months

		# Adjust cost if start or end date is not month start or end
		prorate = frappe.db.get_single_value('Subscription Settings', 'prorate')

		if prorate:
			prorate_factor = flt(date_diff(start_date, get_first_day(start_date)) / date_diff(
				get_last_day(start_date), get_first_day(start_date)), 1)

			prorate_factor += flt(date_diff(get_last_day(end_date), end_date) / date_diff(
				get_last_day(end_date), get_first_day(end_date)), 1)

			cost -= (plan.cost * prorate_factor)

		return cost


from task1 import PriorProb
import numpy as np
import math
from matplotlib import pyplot as plt

"""
Given one observation, X = {(1.5, 0.5)},
compute the posterior P(H|X) with 𝜎 = 12.
You will get one probability for each possible hypothesis.
Display your result either as a bar graph or a list of probabilities.
Use Tenenbaum's Size Principle as the likelihood function.
"""

class LikeLihood(object):
    def __init__(self,observation,sigma1,sigma2):
        self.observation = observation
        self.sigma1 = sigma1
        self.sigma2 = sigma2

    def makeLikelihood(self,l1,l2,s1,s2):
        count = 0
        for ob in self.observation:
            x = ob[0]
            y = ob[1]
            if l1 <= x <= s1 and l2<= y <= s2:
                count +=1
        if len(self.observation) == count:
            return 1
        else:
            return 0

class Posterior(object):
    def __init__(self,obs,sigma1,sigma2):
        self.observation = obs
        self.sigma1 = sigma1
        self.sigma2 = sigma2

    def generatePosterior(self,l1,l2,s1,s2):
        likelihood = LikeLihood(self.observation, self.sigma1, self.sigma2)
        probability_likelihood = likelihood.makeLikelihood(l1,l2,s1,s2)
        prob = PriorProb(l1, l2, s1, s2, self.sigma1, self.sigma2)
        probability_prior = prob.makePrior()
        return probability_prior*probability_likelihood

class PosteriorDistribution(object):
    def __init__(self,h,sigma1,sigma2,obs):
        self.h= h
        self.sigma1 = sigma1
        self.sigma2 = sigma2
        self.observation = obs
        self.posterior_probability = {}

    def generatePosterior(self):
        posterior = Posterior(self.observation,self.sigma1,self.sigma2)
        for i in range(1, self.h + 1):
            self.posterior_probability[i] = posterior.generatePosterior(-i,-i,i,i)
        return self.posterior_probability

    def normalize_posterior(self):
        return { k: v/sum(self.posterior_probability.values()) for k,v  in self.posterior_probability.items()}

    def plotDistribution(self,prior_dist):
        label = prior_dist.keys()
        index = np.arange(len(label))
        plt.bar(index, prior_dist.values())
        plt.xlabel('hypothesis - i', fontsize=5)
        plt.ylabel('prior probability', fontsize=5)
        plt.xticks(index, label, fontsize=5, rotation=30)
        title = 'Normalized Posterior distribution without size principle for sigma1 = '+str(self.sigma1)+" and sigma2 = "+str(self.sigma2)
        plt.title(title)
        plt.show()

if __name__ == '__main__':
    obs = [(1.5,0.5)]
    h = 10
    sigma1 = 20
    sigma2 = sigma1
    posteriorDistribution = PosteriorDistribution(h,sigma1,sigma2,obs)
    probs = posteriorDistribution.generatePosterior()
    normalized_probs = posteriorDistribution.normalize_posterior()
    posteriorDistribution.plotDistribution(normalized_probs)
    print(sum(normalized_probs.values()))


from MirrorDownloader import MirrorDownloader
from sqlitehelper import SQLiteHelper
from distromirrors.Helpers import Helpers

from concurrent.futures import ThreadPoolExecutor as TPE
import socket
from urllib import parse
import logging

class MirrorDatabase:
    def __init__(self, databasefile='mirrors.db'):
        self.downloader = MirrorDownloader()
        self.conn = SQLiteHelper(databasefile)
        self.cachefile = 'mirrors.pkl'
        self.log = logging.getLogger('MirrorDatabase')

    def resolve_mirrors(self):
        mirrors = self.get_mirrors_from_database()
        threadpool = TPE(4)

        def not_a_lambda_function(mirror):
            mirror_hostname = parse.urlparse(mirror['mirror']).hostname
            address_list = []
            for addrinfo in socket.getaddrinfo(mirror_hostname,80):
                address_list.append(addrinfo[-1][0])
            return (mirror['ID'], address_list)

        results = threadpool.map(not_a_lambda_function,mirrors)
        return list(results)

    def get_mirror_by_id(self, id):
        return self.conn.execute("SELECT mirrors.Distro, mirrors.Mirror, mirror_ip.IP, mirror_ip.City, mirror_ip.Area, mirror_ip.Country FROM mirrors INNER JOIN mirror_ip ON mirrors.ID = mirror_ip.MirrorID WHERE mirrors.ID = ?", [id]).fetchall()

    def get_mirror_by_ip(self, ip):
        return self.conn.execute("SELECT mirrors.Distro, mirrors.Mirror, mirror_ip.IP, mirror_ip.City, mirror_ip.Area, mirror_ip.Country FROM mirrors INNER JOIN mirror_ip ON mirrors.ID = mirror_ip.MirrorID WHERE mirror_ip.IP = ?", [ip]).fetchall()

    def create_database(self):
        if not self.conn.table_exists('mirrors'):
            self.conn.execute('''
            CREATE TABLE mirrors
            (
              ID INTEGER PRIMARY KEY,
              Distro TEXT,
              Mirror TEXT,
              Hostname TEXT
            )''')
            self.log.info('Created mirrors db')

        if not self.conn.table_exists('mirror_ip'):
            self.conn.execute('''
            CREATE TABLE mirror_ip
            (
              MirrorID INTEGER,
              IP TEXT,
              Country TEXT,
              Area TEXT,
              City TEXT,
              FOREIGN KEY(MirrorID) REFERENCES mirrors(ID)
            )
            ''')
            self.log.info('Created mirror_ip db')

    def get_mirrors_from_database(self):
        return self.conn.execute("SELECT ID, Mirror FROM mirrors").fetchall()

    def fill_database(self):
        logging.basicConfig(level=logging.INFO)
        database = MirrorDatabase()
        helper = Helpers()
        database.create_database()
        downloader = MirrorDownloader()
        if not (database.conn.number_of_rows('mirrors')):
            mirrors = downloader.download_mirrors()
            ip, location = helper.get_location()
            for distro in mirrors:
                database.conn.executemany('INSERT INTO mirrors (Distro, Mirror)  VALUES (?, ?)', [(distro['name'], m) for m in distro['mirrors']])
                database.conn.commit()
        else:
            database.log.warn('No mirrors were added')

        hosts = database.resolve_mirrors()
        for mirror_id, addresses in hosts:
                for address in addresses:
                    if not database.get_mirror_by_ip(address):
                        database.log.info("IP %s was new", address)
                        location = helper.get_mirror_location(address)
                        country = location['country']['names']['en']
                        city = location['city']['names']['en'] if 'city' in location else ""
                        area = ", ".join([s['names']['en'] for s in location['subdivisions']]) if 'subdivisions' in location else ""
                        database.conn.execute("INSERT INTO mirror_ip (MirrorID, IP, Country, Area, City) VALUES (?, ?, ?, ?, ?)", [mirror_id, address, country, area, city])
                    else:
                        database.log.info("IP %s was already in the database", address)
        database.conn.commit()
        database.conn.close()


import unittest

from Monkey import Monkey

class MonkeyTest(unittest.TestCase):
    """ Basic unit tests to check Monkey class is behaving as expected

    """
    __monkey = None

    def setUp(self):
        self.__monkey = Monkey()

    def test_check_alive(self):
        """
            Test that _alive set to true when health is set to a value >=30
        """
        self.__monkey.set_health(50)
        self.__monkey.check_alive()
        self.assertTrue(self.__monkey.get_alive())

    def test_check_dead(self):
        """
            Test that _alive is set to false when health is set to a value <30
        """
        self.__monkey.set_health(29)
        self.__monkey.check_alive()
        self.assertFalse(self.__monkey.get_alive())

    def test_zero_health_when_dead(self):
        """
            Tests health goes to 0 when dead
        """
        if __name__ == '__main__':
            self.__monkey.set_health(25)
            self.__monkey.check_alive()
            self.assertEqual(0, self.__monkey.get_health())

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


import requests as RQ

BASE_URL = 'http://127.0.0.1:5000/'

def _url(url):
    return BASE_URL+url

def getTerm(term):
    payload = {'term' : term}
    r = RQ.get(_url('hwlib/terms'), data=payload)
    if r.status_code == 200: return r.json()
    return term

def getLibUnits(lib='', parent='', addr=''):
    payload = dict()
    if lib != '':
        payload.update({'id' : lib})
    if parent != '':
        payload.update({'unit' : parent, 'addr' : addr})

    r = RQ.get(_url('hwlib/units'), data=payload)
    if r.status_code == 200: return r.json()
    raise AssertionError('Server error ' + str(r.status_code))

def getPrjUnits(unit='', lib='', mode='full'):
    payload = {'mode' : mode}
    if unit != '': payload.update ({'unit' : unit})
    if lib !='': payload.update ({'id' : lib})
    r = RQ.get(_url('cfg/units'), data=payload)
    if r.status_code == 200: return r.json()
    raise AssertionError('Server error ' + str(r.status_code))

def getStatus():
    r = RQ.get(_url('cfg/stat'))
    if r.status_code == 200: return r.json()
    raise AssertionError('Server error ' + str(r.status_code))

def get_can_move_up(unit):
    payload = {'unit' : unit,
               'cmd' : 'up'}
    r = RQ.get(_url('cfg/units'), data=payload)
    if r.status_code == 200: return r.json()
    raise AssertionError('Server error ' + str(r.status_code))

def get_can_move_down(unit):
    payload = {'unit' : unit,
               'cmd' : 'down'}
    r = RQ.get(_url('cfg/units'), data=payload)
    if r.status_code == 200: return r.json()
    raise AssertionError('Server error ' + str(r.status_code))

def del_unit(unit):
    payload = {'unit' : unit}
    r = RQ.delete(_url('cfg/units'), data=payload)
    if r.status_code != 204:
        raise AssertionError('Server error ' + str(r.status_code))

def move_up_unit(unit):
    payload = {'unit' : unit,
               'cmd' : 'up'}
    r = RQ.put(_url('cfg/units'), data=payload)
    if r.status_code != 201:
        raise AssertionError('Server error ' + str(r.status_code))

def move_dn_unit(unit):
    payload = {'unit' : unit,
               'cmd' : 'down'}
    r = RQ.put(_url('cfg/units'), data=payload)
    if r.status_code != 201:
        raise AssertionError('Server error ' + str(r.status_code))

def ins_new_unit(parent, addr, lib):
    payload = {'unit' : parent,
               'addr' : addr,
               'id' : lib}
    r = RQ.post(_url('cfg/units'), data=payload)
    if r.status_code != 201:
        raise AssertionError('Server error ' + str(r.status_code))
    return r.json()

def set_unit_alias(unit, alias):
    payload = {'unit' : unit,
               'alias' : alias}
    r = RQ.put(_url('cfg/units'), data=payload)
    if r.status_code != 201:
        raise AssertionError('Server error ' + str(r.status_code))

def main():
    print (getTerm('Power'))

if __name__ == '__main__':
    main()


import statistics

newCalculation = True;
while newCalculation:

    #sum = 0;
    #count = 0;
    fileName = str(input("What is the name of the file you want to open: "));
    try:
        f = open(fileName, "r");
        #print("list of random numbers in random.txt: ");
        #max = 0;
        #min = 10;
        numbers = []
        for x in f:
            #print(x)
            #num = int(x)

            numbers.append(int(x));
            count2 = len(numbers)
            max2 = max(numbers)
            min2 = min(numbers)
            sum2= sum(numbers)
            #print("numbers ", numbers);
            #sum += int(x);
            #count += 1;
            average = sum2 / count2;
            range = max2 - min2;

        numbers.sort();

        length = len(numbers)
        if (length % 2 == 0):
            median = (numbers[(length)//2] + numbers[(length)//2-1]) / 2
        else:
            median = numbers[(length-1)//2]
        mode2 = max(numbers, key=lambda n: numbers.count(n))
        print("FileName: ", fileName);
        print("Sum: ", sum2);
        print("count: ",count2);
        print("Average: ", average);
        print("Max: ", max2);
        print("Min: ", min2);
        print("Range: ", range);
        print("Median: ", median);
        print("Mode: ", mode2);

        again = input("If you want to play again type y: ");
        if again != "y":
            newCalculation = False;

    except IOError:
        print("There is no file named randomnum.txt");

        if (len(numbers) < 1):
            print("the file is empty");


from urllib import parse

from scrapy.http import Request
from scrapy_redis.spiders import RedisSpider

from ScrapyRedisTest.items import JobboleArticleItem, ArticleItemLoader
from ScrapyRedisTest.utils.common import md5

class JobboleSpider(RedisSpider):
    name = 'jobbole'
    allowed_domains = ['blog.jobbole.com']
    start_urls = ['http://blog.jobbole.com/all-posts/']
    # redis:6379>
    # lpush jobbole:start_urls http://blog.jobbole.com/all-posts/
    redis_key = 'jobbole:start_urls'

    def parse(self, response):
        """
            1、获取文章列表页中的文章url并进行解析
            2、获取下一页的url并交给downloader进行下载
        """
        # 获取列表页中所有文章url
        post_nodes = response.css('#archive .floated-thumb .post-thumb a')
        for node in post_nodes:
            img_url = node.css('img::attr(src)').extract_first('')
            url = node.css('::attr(href)').extract_first('')
            yield Request(url=parse.urljoin(response.url, url), meta={'front_image_url': img_url}, callback=self.extract_article)

        # 获取下一页的url
        next_page_url = response.css('.next.page-numbers::attr(href)').extract_first('')
        if next_page_url:
            yield Request(url=parse.urljoin(response.url, next_page_url), callback=self.parse)

    def extract_article(self, response):
        """提取文章具体字段(标题 日期 内容等)"""

        # 通过item loader加载item
        item_loader = ArticleItemLoader(item=JobboleArticleItem(), response=response)
        item_loader.add_css('title', '.entry-header h1::text')
        item_loader.add_css('create_date', '.entry-meta-hide-on-mobile::text')
        item_loader.add_value('url', response.url)
        item_loader.add_value('url_object_id', md5(response.url))
        item_loader.add_value('front_image_url', [response.meta.get('front_image_url', '')])
        item_loader.add_css('praise_nums', 'span.vote-post-up h10::text')
        item_loader.add_css('fav_nums', 'span.bookmark-btn::text')
        item_loader.add_css('comment_nums', 'a[href="#article-comment"] span::text')
        item_loader.add_css('tags', 'p.entry-meta-hide-on-mobile a::text')
        item_loader.add_css('content', 'div.entry')
        item = item_loader.load_item()

        yield item


# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import absolute_import

import tensorflow as tf
from tensorflow.python.client import device_lib
import argparse
import sys
reload(sys)
sys.setdefaultencoding('utf8')
sys.path.append('..')
import os
import time

from model.wnet_forInferEval import WNet as WNET

model_root = '/Data_HDD/Harric/ChineseCharacterExp/'
data_root = '/DataA/Harric/ChineseCharacterExp/'
save_path = '/Data_HDD/Harric/ChineseCharacterExp/GeneratedResult/Generated_201904/TrainStyle/'

flags = tf.flags
flags.DEFINE_integer("style_input_number","-1","StyleReferenceNum")
flags.DEFINE_string("evaluating_generator_dir","-1","ModelDirLoad")

FLAGS = flags.FLAGS
style_input_number=FLAGS.style_input_number
evaluating_generator_dir=FLAGS.evaluating_generator_dir

# content prototype setting
content_data_dir=list()
content_data_dir.append('CASIA_Dataset/StandardChars/GB2312_L1/')
content_data_dir.append('CASIA_Dataset/StandardChars/GB2312_L1/')
file_list_txt_content=list()
file_list_txt_content.append('../TrainTestFileList/StandardChars/Char_0_3754_GB2312L1_Train.txt')
file_list_txt_content.append('../TrainTestFileList/StandardChars/Char_0_3754_GB2312L1_Test.txt')

# content to be generated setting
targeted_content_input_txt='../ContentTxt/ContentChars_BlancaPython_32.txt'

# style data setting
style_data_dir=list()
style_data_dir.append('CASIA_Dataset/HandWritingData_240Binarized/CASIA-HWDB2.1/')
style_data_dir.append('CASIA_Dataset/HandWritingData_240Binarized/CASIA-HWDB1.1/')

# true style setting
file_list_txt_true_style=list()
file_list_txt_true_style.append('../EvaluationDataFileLists/HandWritingData/ContentChar_BlancaPython_Writer_1101_1150_Cursive.txt')
file_list_txt_true_style.append('../EvaluationDataFileLists/HandWritingData/ContentChar_BlancaPython_Writer_1101_1150_Isolated.txt')

# input style setting
targeted_style_input_txt='../ContentTxt/StyleChars_Paintings_20.txt'
file_list_txt_input_style=list()
file_list_txt_input_style.append('../EvaluationDataFileLists/HandWritingData/StyleChars_Paintings_Writer_1101_1150_Cursive.txt')
file_list_txt_input_style.append('../EvaluationDataFileLists/HandWritingData/StyleChars_Paintings_Writer_1101_1150_Isolated.txt')

print_separater = "#################################################################"
input_args = ['--generator_residual_at_layer','3',
              '--generator_residual_blocks','1',
              '--generator_device','/device:GPU:0']
parser = argparse.ArgumentParser(description='Eval')

# network settings
parser.add_argument('--generator_residual_at_layer', dest='generator_residual_at_layer', type=int, required=True)
parser.add_argument('--generator_residual_blocks', dest='generator_residual_blocks', type=int, required=True)
parser.add_argument('--generator_device', dest='generator_device',type=str,required=True, help='Devices for generator')

def get_available_gpus():
    local_device_protos = device_lib.list_local_devices()
    cpu_device=[x.name for x in local_device_protos if x.device_type == 'CPU']
    gpu_device=[x.name for x in local_device_protos if x.device_type == 'GPU']
    print("Available CPU:%s with number:%d" % (cpu_device, len(cpu_device)))
    print("Available GPU:%s with number:%d" % (gpu_device, len(gpu_device)))
    return cpu_device, gpu_device,len(cpu_device),len(gpu_device)

def main(_):
    print(print_separater)
    avalialbe_cpu, available_gpu, available_cpu_num, available_gpu_num = get_available_gpus()

    if available_gpu_num==0:
        args.generator_device = '/device:CPU:0'

    for ii in range(len(content_data_dir)):
        content_data_dir[ii] = os.path.join(data_root, content_data_dir[ii])
    for ii in range(len(style_data_dir)):
        style_data_dir[ii] = os.path.join(data_root, style_data_dir[ii])

    global evaluating_generator_dir
    experiment_id_list = evaluating_generator_dir.split('/')
    for experiment_id in experiment_id_list:
        if 'Exp' in experiment_id:
            break

    if 'Style4' in experiment_id:
        style_input_number=4
    elif 'Style1' in experiment_id:
        style_input_number=1
    else:
        global style_input_number
        experiment_id = experiment_id+'-Style%d' % style_input_number

    global save_path
    save_path = os.path.join(save_path, experiment_id)

    model = WNET(style_input_number=style_input_number,
                 experiment_id=experiment_id,

                 targeted_content_input_txt=targeted_content_input_txt,
                 targeted_style_input_txt=targeted_style_input_txt,
                 save_path=save_path,

                 content_data_dir=content_data_dir,
                 file_list_txt_content=file_list_txt_content,
                 style_data_dir=style_data_dir,
                 file_list_txt_true_style=file_list_txt_true_style,
                 file_list_txt_input_style=file_list_txt_input_style,

                 generator_residual_at_layer=args.generator_residual_at_layer,
                 generator_residual_blocks=args.generator_residual_blocks,
                 generator_devices=args.generator_device,

                 model_dir=os.path.join(model_root,evaluating_generator_dir))

    model.character_generation()

#input_args = []
args = parser.parse_args(input_args)

if __name__ == '__main__':
    tf.app.run()


#loads discord and other assets
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
from random import randint

Client = discord.Client() #so the word client is able to be used instead of writing the whole thing
bot_prefix= "/" #set the bots prefix, what ever symbol is used here will need to be put in front of commands
client = commands.Bot(command_prefix=bot_prefix) #so the word client is able to be used instead of writing the whole thing

@client.event #As soon as the bot comes online all this will be ran
async def on_ready():
    print("Bot Online!")
    print("Name: {}".format(client.user.name))
    print("ID: {}".format(client.user.id))
    rnd = randint(1, 30)
    if rnd is 1:
        await client.send_message(discord.Object(id='283181037243072512'), 'k den')

    elif rnd is 2:
        await client.send_message(discord.Object(id='283181037243072512'), 'Hello')

    elif rnd is 3:
        await client.send_message(discord.Object(id='283181037243072512'), 'woh')

    elif rnd is 4:
        await client.send_message(discord.Object(id='283181037243072512'), '"I remember when I was 17" - Chris Imhoff')

    elif rnd is 5:
        await client.send_message(discord.Object(id='283181037243072512'), 'Epic!')

    elif rnd is 6:
        await client.send_message(discord.Object(id='283181037243072512'), "Jed you're still kicked out of the spooksters")

    elif rnd is 7:
        await client.send_message(discord.Object(id='283181037243072512'), 'k den')

    elif rnd is 8:
        await client.send_message(discord.Object(id='283181037243072512'), "Creamed Cabbage and Toilah are raging homosexuals, don't get too close!")

    elif rnd is 9:
        await client.send_message(discord.Object(id='283181037243072512'), '42 Maxwell St, Monavale')

    elif rnd is 10:
        await client.send_message(discord.Object(id='283181037243072512'), 'Send LEWDS!')

    elif rnd is 11:
        await client.send_message(discord.Object(id='283181037243072512'), 'xd')

    elif rnd is 12:
        await client.send_message(discord.Object(id='283181037243072512'), 'yeet')

    elif rnd is 13:
        await client.send_message(discord.Object(id='283181037243072512'), "It's 6ft at the green tides today!")

    elif rnd is 14:
        await client.send_message(discord.Object(id='283181037243072512'), 'Frantic Gecko has small man syndrome')

    elif rnd is 15:
        await client.send_message(discord.Object(id='283181037243072512'), 'Daily reminder that harlem shake videos are still being uploaded to youtube')

    elif rnd is 16:
        await client.send_message(discord.Object(id='283181037243072512'), 'rawr xd')

    elif rnd is 17:
        await client.send_message(discord.Object(id='283181037243072512'), 'I love Hayley')

    elif rnd is 18:
        await client.send_message(discord.Object(id='283181037243072512'), 'Flat is justice!')

    elif rnd is 19:
        await client.send_message(discord.Object(id='283181037243072512'), 'Illya is best loli!')

    elif rnd is 20:
        await client.send_message(discord.Object(id='283181037243072512'), 'Hikikomori was a mod+ on raidforums, be careful or he will HACK you')

    elif rnd is 21:
        await client.send_message(discord.Object(id='283181037243072512'), 'Sneaky Squid enjoys being fem dommed by his gf')

    elif rnd is 22:
        await client.send_message(discord.Object(id='283181037243072512'), 'Spacial is dating a man bangs table')

    elif rnd is 23:
        await client.send_message(discord.Object(id='283181037243072512'), 'James™ is da bong lord')

    elif rnd is 24:
        await client.send_message(discord.Object(id='283181037243072512'), 'haha weed!')

    elif rnd is 25:
        await client.send_message(discord.Object(id='283181037243072512'), 'Traps are not gay!')

    elif rnd is 26:
        await client.send_message(discord.Object(id='283181037243072512'), "RIP Charlie's sister!")

    elif rnd is 27:
        await client.send_message(discord.Object(id='283181037243072512'), 'Anyone at the mall?')

    elif rnd is 28:
        await client.send_message(discord.Object(id='283181037243072512'), 'Peter Griffin finds weed in black ops 2')

    elif rnd is 29:
        await client.send_message(discord.Object(id='283181037243072512'), "HELP! I'm stuck in your draw!!!")

    elif rnd is 30:
        await client.send_message(discord.Object(id='283181037243072512'), 'owo')

@client.command(pass_context=True) #/gay command - name's the gayest member of the spooksters discord
async def gay(ctx):
    print("running /gay command...")
    rnd = randint(1, 2)
    if rnd is 1:
        await client.say("Bryce!")
        print("/gay command was run")

    elif rnd is 2:
        await client.say("Toilah!")
        print("/gay command was run")

@client.command(pass_context=True) #/sex Allows you to have le sexy time
async def sex(ctx,args):
    print("Running /sex...")
    rnd = randint(1, 10)
    if rnd is 1:
        await client.say('Your attempt to sex {} was an epic fail https://images.discordapp.net/attachments/270523219214204938/336812256866336780/18403246_1398304556931039_8835722781557344382_n.jpg'.format(args))
        print("/sex option #1 was run")

    elif rnd is 2:
        await client.say('Your attempt to sex {} was an epic WIN!!! https://cdn.discordapp.com/attachments/270523219214204938/336812897999126528/1827564-Overlook-Tracer-Winstonboiii.jpg '.format(args))
        print("/sex option #2 was run")

    elif rnd is 3:
        await client.say('Your attempt to sex {} was an epic fail https://images.discordapp.net/attachments/270523219214204938/336812994543616011/18275019_1712761285418928_21504952677890610_n.jpg'.format(args))
        print("/sex option #3 was run")

    elif rnd is 4:
        await client.say('Your attempt to sex {} was an epic WIN!!! https://images.discordapp.net/attachments/270523219214204938/336813286655918081/ngbbs4495f6d906fd9.gif'.format(args))
        print("/sex option #4 was run")

    elif rnd is 5:
        await client.say('Your attempt to sex {} was an epic WIN!!! https://images.discordapp.net/attachments/270523219214204938/336814181472796672/big_1473484418_image.jpg'.format(args))
        print("/sex option #5 was run")

    elif rnd is 6:
        await client.say('Your attempt to sex {} was an epic fail!!! https://i.ytimg.com/vi/hoETcc7HPYw/maxresdefault.jpg '.format(args))
        print("/sex option #6 was run")

    elif rnd is 7:
        await client.say('Your attempt to sex {} was an epic fail!!! https://images.discordapp.net/attachments/270523219214204938/336814705999872012/17362034_1834954790100000_3559162889712655566_n.jpg?width=442&height=590 '.format(args))
        print("/sex option #7 was run")

    elif rnd is 8:
        await client.say('Your attempt to sex {} was an epic WIN!!! http://nerdreactor.com/wp-content/uploads/2011/07/CDBABY-Dirty-Single-Cover.jpg '.format(args))
        print("/sex option #8 was run")

    elif rnd is 9:
        await client.say('Your attempt to sex {} was an epic fail!!! https://images.discordapp.net/attachments/270523219214204938/336815116848988160/qqcvc5q1sssy.jpg?width=589&height=589 '.format(args))
        print("/sex option #9 was run")

    elif rnd is 10:
        await client.say('Your attempt to sex {} was an epic WIN!!! http://i1.ytimg.com/vi/X3ihcfJdAMs/hqdefault.jpg  '.format(args))
        print("/sex option #10 was run")

@client.command(pass_context=True) #posts the best loli
async def illya(ctx):
    channelid = str
    channelid = ctx.message.channel
    print("/illya command was run")
    path = r"\app\Discord Bot\illya"
    random_filename = random.choice([
        x for x in os.listdir(path)
        if os.path.isfile(os.path.join(path, x))
    ])
    await client.send_file(channelid, '\app\Discord Bot\illya\random_filename')

@client.command(pass_context=True) #allows you to warn people doesn't actually mean anything
async def warning(ctx, args):
    rnd = randint(1, 10)
    if rnd is 1:
        await client.say('{} has been warned, reason: Being an absolute dickhead!'.format(args))

    elif rnd is 2:
        await client.say('{} has been warned, reason: Acting like a real Chris Imhoff!'.format(args))

    elif rnd is 3:
        await client.say('{} has been warned, reason: Spending a Saturday with the mrs!'.format(args))

    elif rnd is 4:
        await client.say("{} has been warned, reason: You're really just pissing me off!".format(args))

    elif rnd is 5:
        await client.say("{} has been warned, reason: Here I was just quietly enjoying my time on The Spookster discord and you just logged in and fucking ruined my day, I'm actually just so fucking pissed at you right now you should probably just leave before I decide too make my way down to your house and beat your lord farquad lookin ass".format(args))

    elif rnd is 6:
        await client.say('{} has been warned, reason: Caught them watching MLP porn!'.format(args))

    elif rnd is 7:
        await client.say("{} has been warned, reason: You think your words hurt? I'm currently on 20 medications for depression. My life is shit. You think your insults can drag me down? I've already hit rock bottom. Look at these cuts. Yeah, I cut the long way. I've been hospitalized twice for blood loss, and each time I come back, I'm cursed for bringing home another hospital bill. You think your words sting? They means nothing. You think you're the only one to insult me? My mom curses me for existing. My dad beats me every day and my older brother, the pervert he is, keeps touching me. They curse me every day for existing. I'm an annoyance. I shouldn't be alive, I wish I could just end it, but there isn't even pain anymore. No reason to end it. You think your words hurt? They mean nothing to me. I don't feel anything anymore. Now please just leave me alone before I ban you".format(args))

    elif rnd is 8:
        await client.say('{} has been warned, reason: fuckin elmo from sesame street lookin ass'.format(args))

    elif rnd is 9:
        await client.say('{} has been warned, reason: okaaaayyyyyy Robbie'.format(args))

    elif rnd is 10:
        await client.say('{} has been warned, reason: Honestly there was no real reason I just really fucking hate you'.format(args))

@client.event
async def on_message(owo):
    if owo.content == 'owo' :
        print('owo is running...')
        channelid = owo.channel
        await client.send_message(channelid, "What's this?")
        print('owo command was run!')
    await client.process_commands(owo)

client.run("MzM2NDMxMjU3NTgxOTEyMDY0.DE82AA.PZeFLPjS1nhzZYvGjCCs9eidrGU") #This is the bot's token, tells the client what bot it is controlling


import torch
from all.environments import State

class NStepBuffer:
    """A rolling n-step buffer, such that every sample is an n-step rollout."""

    def __init__(self, n, discount_factor=1):
        self.n = n
        self.gamma = discount_factor
        self.i = 0
        self._states = []
        self._actions = []
        self._rewards = []
        self._next_states = []
        self._lengths = []
        self._temp = []

    def __len__(self):
        return len(self._states)

    def store(self, states, actions, rewards):
        # move states that are ready out of temp buffer
        if len(self._temp) == self.n:
            discount = self.gamma ** (self.n - 1)
            for i, v in enumerate(self._temp[0]):
                state, action, reward, last_state, length = v
                if last_state.mask == 0:
                    self._store(state, action, reward, last_state, length)
                else:
                    reward += discount * rewards[i]
                    last_state = states[i]
                    self._store(state, action, reward, last_state, length + 1)
            self._temp = self._temp[1:]

        for t, _temp in enumerate(self._temp):
            discount = self.gamma ** (len(self._temp) - 1 - t)
            for i, v in enumerate(_temp):
                state, action, reward, last_state, length = v
                if last_state.mask == 1:
                    reward += discount * rewards[i]
                    last_state = states[i]
                    length += 1
                _temp[i] = (state, action, reward, last_state, length)

        self._temp.append(
            [
                (state, action, reward, next_state, 0)
                for state, action, reward, next_state in zip(
                    states, actions, rewards * 0, states
                )
            ]
        )

        return self

    def sample(self, batch_size):
        if batch_size > len(self):
            raise Exception("Not enough states for batch size!")

        states = self._states[0:batch_size]
        actions = self._actions[0:batch_size]
        actions = torch.tensor(actions, device=actions[0].device)
        next_states = self._next_states[0:batch_size]
        rewards = self._rewards[0:batch_size]
        rewards = torch.tensor(rewards, device=rewards[0].device, dtype=torch.float)
        lengths = self._lengths[0:batch_size]
        lengths = torch.tensor(lengths, device=rewards[0].device, dtype=torch.float)

        self._states = self._states[batch_size:]
        self._actions = self._actions[batch_size:]
        self._next_states = self._next_states[batch_size:]
        self._rewards = self._rewards[batch_size:]
        self._lengths = self._lengths[batch_size:]

        states = State.from_list(states)
        next_states = State.from_list(next_states)
        return states, actions, rewards, next_states, lengths

    def _store(self, state, action, reward, next_state, length):
        self._states.append(state)
        self._actions.append(action)
        self._rewards.append(reward)
        self._next_states.append(next_state)
        self._lengths.append(length)

class NStepBatchBuffer:
    """
    A batch version of the n-step buffer,
    such that the first sample is n-step, second is n-1-step, etc.
    This may be better for on-policy methods.
    """

    def __init__(self, n_steps, n_envs, discount_factor=1):
        self.n_steps = n_steps
        self.n_envs = n_envs
        self.gamma = discount_factor
        self._states = []
        self._actions = []
        self._rewards = []

    def __len__(self):
        if not self._states:
            return 0
        return (len(self._states) - 1) * self.n_envs

    def store(self, states, actions, rewards):
        if not self._states:
            self._states = [states]
            self._actions = [actions]
            self._rewards = [rewards]
        elif len(self._states) <= self.n_steps:
            self._states.append(states)
            self._actions.append(actions)
            self._rewards.append(rewards)
        else:
            raise Exception("Buffer length exceeded: " + str(self.n_steps))

    def sample(self, _):
        if len(self) < self.n_steps * self.n_envs:
            raise Exception("Not enough states received!")

        sample_n = self.n_envs * self.n_steps
        sample_states = [None] * sample_n
        sample_actions = [None] * sample_n
        sample_returns = [0] * sample_n
        sample_next_states = [None] * sample_n
        sample_lengths = [0] * sample_n

        # compute the N-step returns the slow way
        sample_returns = torch.zeros(
            (self.n_steps, self.n_envs),
            device=self._rewards[0].device
        )
        sample_lengths = torch.zeros(
            (self.n_steps, self.n_envs),
            device=self._rewards[0].device
        )
        current_returns = self._rewards[0] * 0
        current_lengths = current_returns.clone()
        for i in range(self.n_steps):
            t = self.n_steps - 1 - i
            mask = self._states[t + 1].mask.float()
            current_returns = (
                self._rewards[t] + self.gamma * current_returns * mask
            )
            current_lengths = (
                1 + current_lengths * mask
            )
            sample_returns[t] = current_returns
            sample_lengths[t] = current_lengths

        for e in range(self.n_envs):
            next_state = self._states[self.n_steps][e]
            for i in range(self.n_steps):
                t = self.n_steps - 1 - i
                idx = t * self.n_envs + e
                state = self._states[t][e]
                action = self._actions[t][e]

                sample_states[idx] = state
                sample_actions[idx] = action
                sample_next_states[idx] = next_state

                if not state.mask:
                    next_state = state

        self._states = self._states[self.n_steps:]
        self._actions = self._actions[self.n_steps:]
        self._rewards = self._rewards[self.n_steps:]

        return (
            State.from_list(sample_states),
            sample_actions,
            sample_returns.view(-1),
            State.from_list(sample_next_states),
            sample_lengths.view(-1),
        )


"""
Demo: Detect symbol via HOG-SVM detector
"""

from symbolDetector import DetectorOption, SymbolDetector
from pdfReader import PdfReader

if "__main__" == __name__:
    data_dir = '../data/'

    ####read pdf (test data)
    pr = PdfReader(data_dir + "test.pdf")

    option = DetectorOption("hog_svm")
    sd = SymbolDetector(option)
    sd.detect_all(pr.page(0)[100:500, 1:500], "show")


# -*- coding: utf-8 -*-
"""
Created on Mon Apr  8 15:26:55 2019

@author: agupta466
"""
import pandas as pd
import numpy as np
import fnmatch
import datetime
import sys
from pathlib import Path
import os
import colorama
from colorama import init
import termcolor
from termcolor import colored

here = Path(__file__).resolve()
path = here.parents[2]

sys.path.append(str(path)+"/Export/Python Codes")
sys.path.append(str(path)+"/Production/Python Codes")

import Forecasting_Export as export
from Forecasting_Export import add_state_code, read_all_files, change_date_to_month, repeat_fun, calculating_total_zones, get_ratio_file, retrieving_zones, prev_years_ratios, valid_years, preprocessing_state_to_zone, calculate_state_to_zone, retrieving_confidence, add_confidence, add_confidence2, merge_all_states, try_cutoff, get_date, Replace

import Forecasting_Production as production
from Forecasting_Production import year_preprocessing, get_state_file, get_harvest_plantation_file, data_preprocessing_yearly

encoding='ISO-8859-15'
prefix='20'

def non_states_monthly(actual_data,pred_data,year_var,crop_type):
    prod_states=actual_data['Estado'].unique()
    model_states=pred_data['UF'].unique()
    non_model_state_list=list(set(prod_states).difference(model_states))

    production_subset=actual_data[(actual_data['Type']=='Producao')&(actual_data['Produto']==crop_type)]
    print(production_subset)
    production_subset=production_subset[production_subset['Estado'].isin(non_model_state_list)]
    print(production_subset)

    production_subset['Years'] = production_subset['Years'].apply(lambda x: x.split('-')[1])
    production_subset['Years'] = production_subset['Years'].astype(int)

    all_prod_list=production_subset['Estado'].unique()
    no_prod_states_list=list(set(non_model_state_list).difference(all_prod_list))
    print(no_prod_states_list)

    month_list=list(range(1,13))
    year_list=list(range(year_var[1][1],(int(year_var[1][4])+1)))
    print(year_list)
    columns=['Produto','UF','Type','Years','Month','Percentage','Yearly_value','Monthly_Value']
    df1=pd.DataFrame(0,index=np.arange(len(month_list)),columns=columns)
    df=pd.DataFrame(columns=columns)

    if(crop_type=='Milho 1 Safra'):
        percentage_list=[0,0.25,0.25,0.25,0.25,0,0,0,0,0,0,0]
    if(crop_type=='Milho 2 Safra'):
        percentage_list=[0,0,0,0,0.25,0.25,0.25,0.25,0,0,0,0]
    if(crop_type=='Soja'):
        percentage_list=[0,0,0.25,0.25,0.25,0.25,0,0,0,0,0,0]

    for i in range(len(no_prod_states_list)):
        for j in range(len(year_list)):
            df1['Years']=year_list[j]
            df1['Month']=month_list
            df1['Type']='Producao'
            df1['Produto']=crop_type
            df1['UF']=no_prod_states_list[i]
            df1['Percentage']=percentage_list
            df1['Yearly_value']=0
            df1['Monthly_Value']=0
            df=df.append(df1)

    for index,row in production_subset.iloc[:,:].iterrows():
        df1['Years']=production_subset.loc[index,'Years']
        df1['Month']=month_list
        df1['Type']='Producao'
        df1['Produto']=crop_type
        df1['UF']=production_subset.loc[index,'Estado']
        df1['Percentage']=percentage_list
        df1['Yearly_value']=production_subset.loc[index,'Value']
        df1['Monthly_Value']=0
        df=df.append(df1)

    df['Monthly_Value']=df['Yearly_value']*df['Percentage']
    df=df.reset_index(drop=True)
    return(df)

def getting_all_zones(vli_agro,year_var):
    year=valid_years(year_var)
    len_year=len(year)
    vli_agro=vli_agro.iloc[0:57]
    vli_agro=vli_agro.reset_index(level=1)
    vli_agro_list=[0,1,2,4,6,7,8,9]
    vli_agro_list=vli_agro_list[:(len_year)]
    vli_agro=vli_agro.iloc[:, lambda vli_agro: vli_agro_list]
    vli_agro.columns = year
    vli_agro.index.name='Zonas'
    return(vli_agro)

def retrieving_production_predictions(p):
    # Getting Results from Production Predictions
    all_files=os.listdir(p)
    export_predict_files = fnmatch.filter(all_files, "*Predicted-Production*")
    df = read_all_files(export_predict_files,p)

    df.rename(columns={'Year': 'Date'}, inplace=True)
    df=change_date_to_month(df)
    complete_list_code=add_state_code(df,export_predict_files,'predicted_production')
    df.insert(2,'UF',complete_list_code)
    return(df)

def retrieving_actual_production(actual_data,year_var):
    max_year=year_var[1][0]
    min_year=year_var[1][1]
    max_month=year_var[1][2]
    print(actual_data)
    actual_data1 = actual_data[(actual_data['Years']>=min_year)&(actual_data['Years']<max_year)]
    actual_data2 = actual_data[(actual_data['Years']==max_year)&(actual_data['Month']<=max_month)]
    final_actual = pd.concat([actual_data1.reset_index(drop=True),actual_data2.reset_index(drop=True)],axis=0)
    final_actual = final_actual.reset_index(drop=True)
    return(final_actual)

def retrieving_predicted_production(pred_data,year_var):
    max_year=year_var[1][0]
    max_month=year_var[1][2]
    pred_data1 = pred_data[(pred_data['Year']==max_year)&(pred_data['Month']>max_month)]
    pred_data2 = pred_data[(pred_data['Year']>max_year)]
    pred_full = pd.concat([pred_data1.reset_index(drop=True),pred_data2.reset_index(drop=True)],axis=0)
    pred_full = pred_full.reset_index(drop=True)
    return(pred_full)

def calculating_zones_ratio(vli_agro,year_var):
    year_list=valid_years(year_var)
    len_year_list=len(year_list)
    for index,row in vli_agro.iloc[:,:].iterrows():
        for i in range(len(year_list)):
            if (row[3+len_year_list+i]!=0) and (row[3+i]!=0):
                vli_agro.loc[index,'Ratio-'+str(year_list[i])] = row[3+i]/row[3+len_year_list+i]
            if (row[3+len_year_list+i]==0):
                vli_agro.loc[index,'Ratio-'+str(year_list[i])] = 0
            if (row[3+len_year_list+i]==0) and (row[3+i]==0):
                vli_agro.loc[index,'Ratio-'+str(year_list[i])] = 1

    vli_agro=prev_years_ratios(vli_agro,year_var)
    return(vli_agro)

def main():

    init()
    print(colored('\nProduction Forecast Results - Format Building Starts\n','green'))

    ## Initialize state file
    state1=get_state_file('../Input Files/')

    ## Reading harvest plantation monthly division file
    harvest_plantation=get_harvest_plantation_file('../Input Files/',state1)

    ## Years & Months Pre-processing (monthly file)
    years=harvest_plantation.iloc[:,5:]
    years=year_preprocessing(years)
    year_list_monthly=years.columns

    harvest_plantation_yearly=pd.read_excel("../Input Files/Detalhe_Safra.xls",encoding=encoding)
    combined_yearly=data_preprocessing_yearly(harvest_plantation_yearly,year_list_monthly)

    year_var=pd.read_csv(str(path)+'/Production/Data Directory/year_variables.csv',header=None)
# =============================================================================
#     if(year_var[1][2]==12):
#         year_var[1][0]=year_var[1][0]+1
#         year_var[1][2]=0
# =============================================================================

    print(colored('\nCalculating Brazil State to Zone Ratios...\n','green'))
    print(colored('\nReading Zone Wise Production File for Soya and Milho...\n','green'))

    ## calculating State to Zone Ratio - Soya
    vli_agro=pd.ExcelFile(str(path)+"/Production/Input Files/Prod_longo_prazo.xlsx",encoding=encoding)

    print(colored('\nCalculating State to Zones Ratio for Soya...\n','green'))

    vli_agro_soya = pd.read_excel(vli_agro, 'Resultados soja e farelo')
    vli_agro_soya=vli_agro_soya.iloc[11:68]

    soya_zones=getting_all_zones(vli_agro_soya,year_var)
    all_zones_soya=retrieving_zones(soya_zones)

    final_zones_soya=calculating_total_zones(all_zones_soya,year_var)
    final_zones_soya.to_csv(str(path)+'/Production/Data Directory/Soya_Zones_values.csv',encoding=encoding,index=False)

    zones_ratio_soya = calculating_zones_ratio(final_zones_soya,year_var)
    zones_ratio_soya.to_csv(str(path)+'/Production/Data Directory/Soya_State_to_Zone_Ratio.csv',encoding=encoding,index=False)

    print(colored('\nSoya - State to Zones Ratio Completed!...\n','green'))

    print(colored('\nCalculating State to Zones Ratio for Milho...\n','green'))
    ## Calculating State to Zone Ratio - Milho
    vli_agro_milho = pd.read_excel(vli_agro, 'Resultados milho')
    vli_agro_milho=vli_agro_milho.iloc[8:65]

    milho_zones=getting_all_zones(vli_agro_milho,year_var)
    all_zones_milho=retrieving_zones(milho_zones)

    final_zones_milho=calculating_total_zones(all_zones_milho,year_var)
    final_zones_milho.to_csv(str(path)+'/Production/Data Directory/Milho_Zones_values.csv',encoding=encoding,index=False)

    zones_ratio_milho=calculating_zones_ratio(final_zones_milho,year_var)
    zones_ratio_milho.to_csv(str(path)+'/Production/Data Directory/Milho_State_to_Zone_Ratio.csv',encoding=encoding,index=False)

    print(colored('\nMilho - State to Zones Ratio Completed!...\n','green'))

    print(colored('\nRetrieving Predictions for All Crops - Milho 1ª Safra, Milho 2ª Safra & Soya...\n','green'))

    ## Retrieving Prediction Values - All Crops
    milho1_path=str(path)+'/Production/Data Directory/Jan2019/Milho1_-_Production_Model_Re-Run_Results/Milho1 Predicted Production New Results/'
    milho1_predict=retrieving_production_predictions(milho1_path)

    milho2_path=str(path)+'/Production/Data Directory/Jan2019/Milho2_Production_Model_Re-Run_Results/Milho2 Predicted Production New Results/'
    milho2_predict=retrieving_production_predictions(milho2_path)

    soya_path=str(path)+'/Production/Data Directory/Jan2019/Production_Re-Run_Results_-_Soya/Soya_Predicted_Production_New_Results/'
    soya_predict=retrieving_production_predictions(soya_path)

    print(colored('\nMilho 1ª Safra - Converting Non Model States Agroconsult Yearly Estimated into Monthly...\n','green'))

    ## Converting Non Model States Agroconsult Yearly Estimates into Monthly - All Crops
    milho1_non_model=non_states_monthly(combined_yearly,milho1_predict,year_var,"Milho 1 Safra")
    milho1_non_model.to_csv(str(path)+'/Production/Data Directory/Milho1_Non_Model.csv',index=False,encoding=encoding)

    print(colored('\nMilho 1ª Safra - Conversion Completed!...\n','green'))
    print(colored('\nMilho 2ª Safra - Converting Non Model States Agroconsult Yearly Estimated into Monthly...\n','green'))

    milho2_non_model=non_states_monthly(combined_yearly,milho2_predict,year_var,"Milho 2 Safra")
    milho2_non_model.to_csv(str(path)+'/Production/Data Directory/Milho2_Non_Model.csv',index=False,encoding=encoding)

    print(colored('\nMilho 2ª Safra - Conversion Completed!...\n','green'))
    print(colored('\nSoya - Converting Non Model States Agroconsult Yearly Estimated into Monthly...\n','green'))

    soya_non_model=non_states_monthly(combined_yearly,soya_predict,year_var,"Soja")
    soya_non_model.to_csv(str(path)+'/Production/Data Directory/Soya_Non_Model.csv',index=False,encoding=encoding)

    print(colored('\nSoya - Conversion Completed!...\n','green'))
    print(colored('\nRetrieving Actual Values for all Model States - All Crops...\n','green'))

    ## Reading Actual States for which Models developed
    monthly_final=pd.read_csv(str(path)+'/Production/Data Directory/Monthly_Final.csv',encoding=encoding)
    monthly_final['Years']=monthly_final['Years'].astype(int)
    monthly_final['Month']=monthly_final['Month'].astype(int)

    del monthly_final['Cumulative_Prod']

    soya_actual=monthly_final[(monthly_final['Type']=='Producao')&(monthly_final['Produto']=='Soja')]
    soya_actual=retrieving_actual_production(soya_actual,year_var)

    milho1_actual=monthly_final[(monthly_final['Type']=='Producao')&(monthly_final['Produto']=='Milho 1 Safra')]
    milho1_actual=retrieving_actual_production(milho1_actual,year_var)

    milho2_actual=monthly_final[(monthly_final['Type']=='Producao')&(monthly_final['Produto']=='Milho 2 Safra')]
    milho2_actual=retrieving_actual_production(milho2_actual,year_var)

    ## ---------------------------------------------- Soya -------------------------------------------------- ##

    print(colored('\nCalculating Zone-wise Production Values for all Model States - Soya...\n','green'))

    ## Calculating Zone-wise Actuals for Actual States
    soya_actual=soya_actual[['Years','Month','UF','Monthly_Value']]
    soya_state_to_zone=pd.read_csv(str(path)+'/Production/Data Directory/Soya_State_to_Zone_Ratio.csv',encoding=encoding)
    soya_state_to_zone=preprocessing_state_to_zone(soya_state_to_zone)

    zonewise_soya_model_actual=calculate_state_to_zone(soya_actual,soya_state_to_zone,'pred')
    zonewise_soya_model_actual.columns=['Year','Month','UF','Zonas','Production']

    print(colored('\nCalculating Zone-wise Production Values for all Non - Model States - Soya...\n','green'))

    soya_non_model=pd.read_csv(str(path)+'/Production/Data Directory/Soya_Non_Model.csv',encoding=encoding)

    soya_non_model_actual=retrieving_actual_production(soya_non_model,year_var)
    soya_non_model_actual=soya_non_model_actual[['Years','Month','UF','Monthly_Value']]

    zonewise_soya_nonmodel_actual=calculate_state_to_zone(soya_non_model_actual,soya_state_to_zone,'pred')
    zonewise_soya_nonmodel_actual.columns=['Year','Month','UF','Zonas','Production']

    ## Combining Model and Non-Model States
    all_states_soya_zonewise=pd.concat([zonewise_soya_model_actual,zonewise_soya_nonmodel_actual],axis=0)
    all_states_soya_zonewise=all_states_soya_zonewise.reset_index(drop=True)
    all_states_soya_zonewise=add_confidence(all_states_soya_zonewise)
    all_states_soya_zonewise['Tipo']='Real'
    all_states_soya_zonewise['Tipo Ajustado']='Real / Calculada'
    all_states_soya_zonewise.to_csv(str(path)+'/Production/Data Directory/Soya_Actual_Production.csv',index=False,encoding=encoding)

    ## Getting Confidence Results for Predicted States
    p=str(path)+'/Production/Data Directory/Jan2019/Production_Re-Run_Results_-_Soya/Soya_Confidence_New_Results/'
    soya_confidence_result=retrieving_confidence(p,'prod')
    soya_confidence_result.rename(columns={'Month_Num': 'Month','Ano': 'Year'}, inplace=True)
    predicted_soya_prod=retrieving_predicted_production(soya_predict,year_var)
    predicted_soya_prod=pd.merge(predicted_soya_prod[['Year','Month','UF']],soya_confidence_result[['Year','Month','UF','Point.Forecast']],on=['Year','Month','UF'],how='outer',right_index=False,sort=True,indicator=True).query("_merge == 'both'").drop('_merge',axis=1)
    predicted_soya_zonewise=calculate_state_to_zone(predicted_soya_prod,soya_state_to_zone,'pred')

    #soya_confidence_result=soya_confidence_result.drop(soya_confidence_result.columns[15],axis=1)
    del soya_confidence_result['Point.Forecast']
    soya_confidence_result=calculate_state_to_zone(soya_confidence_result,soya_state_to_zone,'confidence')
    soya_confidence_result.to_csv(str(path)+'/Production/Data Directory/Soya_Confidence_Production.csv',encoding=encoding,index=False)

    ## Merging the Confidence Interval Results with Export Predictions
    soya_zonewise_predicted=pd.merge(predicted_soya_zonewise,soya_confidence_result,on=['Year','Month','UF','Zonas'],how='outer',right_index=False,sort=True,indicator=True).query("_merge != 'right_only'").drop('_merge',axis=1)
    soya_zonewise_predicted.fillna(0)
    soya_zonewise_predicted['Tipo']='Prevista'
    soya_zonewise_predicted['Tipo Ajustado']='Prevista'
    soya_zonewise_predicted.rename(columns={'Zones_Export': 'Production'}, inplace=True)
    soya_zonewise_predicted.to_csv(str(path)+'/Production/Data Directory/Soya_Predicted_Production.csv',encoding=encoding,index=False)

    ## Getting all Fitted Values - Zones
    soya_predict.rename(columns={'Year': 'Years'}, inplace=True)
    fitted_production_soya=retrieving_actual_production(soya_predict,year_var)
    fitted_zonewise_production_soya=calculate_state_to_zone(fitted_production_soya,soya_state_to_zone,'pred')
    fitted_zonewise_production_soya=add_confidence(fitted_zonewise_production_soya)
    fitted_zonewise_production_soya.rename(columns={'Zones_Export': 'Production'}, inplace=True)
    fitted_zonewise_production_soya['Tipo']='Calculada'
    fitted_zonewise_production_soya['Tipo Ajustado']='Real / Calculada'
    fitted_zonewise_production_soya.to_csv(str(path)+'/Production/Data Directory/Soya_ModelFitted_Production.csv',encoding=encoding,index=False)

    ## Getting Non Model Predicted Values - Zones
    soya_non_model.rename(columns={'Years': 'Year'}, inplace=True)
    soya_non_model_pred=retrieving_predicted_production(soya_non_model,year_var)
    soya_non_model_pred=soya_non_model_pred[['Year','Month','UF','Monthly_Value']]
    soya_zonewise_non_model_pred=calculate_state_to_zone(soya_non_model_pred,soya_state_to_zone,'pred')
    soya_zonewise_non_model_pred=add_confidence2(soya_zonewise_non_model_pred)
    soya_zonewise_non_model_pred.rename(columns={'Zones_Export': 'Production'}, inplace=True)
    soya_zonewise_non_model_pred['Tipo']='Prevista'
    soya_zonewise_non_model_pred['Tipo Ajustado']='Prevista'
    soya_zonewise_non_model_pred.to_csv(str(path)+'/Production/Data Directory/Soya_NonModel_Predicted_Production.csv',encoding=encoding,index=False)

    ## Getting Non Model Fitted Values - Zones
    zonewise_soya_nonmodel_fitted=zonewise_soya_nonmodel_actual
    zonewise_soya_nonmodel_fitted['Production']=0
    zonewise_soya_nonmodel_fitted=add_confidence(zonewise_soya_nonmodel_fitted)
    zonewise_soya_nonmodel_fitted['Tipo']='Calculada'
    zonewise_soya_nonmodel_fitted['Tipo Ajustado']='Real / Calculada'
    zonewise_soya_nonmodel_fitted.to_csv(str(path)+'/Production/Data Directory/Soya_NonModel_Fitted_Production.csv',encoding=encoding,index=False)

    print(colored('\nCombining Zone-wise Model and Non-Model States Production Values - Soya...\n','green'))

    soya_prod=merge_all_states(all_states_soya_zonewise,fitted_zonewise_production_soya,soya_zonewise_predicted,soya_zonewise_non_model_pred,zonewise_soya_nonmodel_fitted)

    soya_prod.insert(0,'Produto','Soja')
    soya_prod=soya_prod[['Produto','Month','Year','UF','Zonas','Low','Production','High','Tipo','Tipo Ajustado','Conf']]
    soya_prod.columns=['Produto',u'Mês','Ano','UF',u'Microrregião',u'Produção Mín (Kt)',u'Produção Med (Kt)',u'Produção Max (Kt)','Tipo','Tipo Ajustado',u'% Confiança']

    print(colored('\nZone-Wise Production Results - Soya Completed!...\n','green'))

    ## --------------------------------------------- Milho 1 -------------------------------------------- ##

    print(colored('\nCalculating Zone-wise Production Values for all Model States - Milho 1ª Safra...\n','green'))

    milho1_actual=milho1_actual[['Years','Month','UF','Monthly_Value']]
    milho_state_to_zone=pd.read_csv(str(path)+'/Production/Data Directory/Milho_State_to_Zone_Ratio.csv',encoding=encoding)
    milho_state_to_zone=preprocessing_state_to_zone(milho_state_to_zone)

    zonewise_milho1_model_actual=calculate_state_to_zone(milho1_actual,milho_state_to_zone,'pred')
    zonewise_milho1_model_actual.columns=['Year','Month','UF','Zonas','Production']

    print(colored('\nCalculating Zone-wise Production Values for all Non Model States - Milho 1ª Safra...\n','green'))

    milho1_non_model=pd.read_csv(str(path)+'/Production/Data Directory/Milho1_Non_Model.csv',encoding=encoding)

    milho1_non_model_actual=retrieving_actual_production(milho1_non_model,year_var)
    milho1_non_model_actual=milho1_non_model_actual[['Years','Month','UF','Monthly_Value']]

    zonewise_milho1_nonmodel_actual=calculate_state_to_zone(milho1_non_model_actual,milho_state_to_zone,'pred')
    zonewise_milho1_nonmodel_actual.columns=['Year','Month','UF','Zonas','Production']

    ## Combining Model and Non-Model States - Soya
    all_states_milho1_zonewise=pd.concat([zonewise_milho1_model_actual,zonewise_milho1_nonmodel_actual],axis=0)
    all_states_milho1_zonewise=all_states_milho1_zonewise.reset_index(drop=True)
    all_states_milho1_zonewise=add_confidence(all_states_milho1_zonewise)
    all_states_milho1_zonewise['Tipo']='Real'
    all_states_milho1_zonewise['Tipo Ajustado']='Real / Calculada'
    all_states_milho1_zonewise.to_csv(str(path)+'/Production/Data Directory/Milho1_Actual_Production.csv',index=False,encoding=encoding)

    ## Getting Confidence Results for Predicted States
    p=str(path)+'/Production/Data Directory/Jan2019/Milho1_-_Production_Model_Re-Run_Results/Milho1 Confidence New Results/'
    milho1_confidence_result=retrieving_confidence(p,'prod')
    milho1_confidence_result.rename(columns={'Month_Num': 'Month','Ano': 'Year'}, inplace=True)

    predicted_milho1_prod=retrieving_predicted_production(milho1_predict,year_var)
    predicted_milho1_prod=pd.merge(predicted_milho1_prod[['Year','Month','UF']],milho1_confidence_result[['Year','Month','UF','Point.Forecast']],on=['Year','Month','UF'],how='outer',right_index=False,sort=True,indicator=True).query("_merge != 'right_only'").drop('_merge',axis=1)
    predicted_milho1_zonewise=calculate_state_to_zone(predicted_milho1_prod,milho_state_to_zone,'pred')

    del milho1_confidence_result['Point.Forecast']
    milho1_confidence_result=calculate_state_to_zone(milho1_confidence_result,milho_state_to_zone,'confidence')
    milho1_confidence_result.to_csv(str(path)+'/Production/Data Directory/Milho1_Confidence_Production.csv',encoding=encoding,index=False)

    ## Merging the Confidence Interval Results with Export Predictions
    milho1_zonewise_predicted=pd.merge(predicted_milho1_zonewise,milho1_confidence_result,on=['Year','Month','UF','Zonas'],how='outer',right_index=False,sort=True,indicator=True).query("_merge != 'right_only'").drop('_merge',axis=1)
    milho1_zonewise_predicted.fillna(0)
    milho1_zonewise_predicted['Tipo']='Prevista'
    milho1_zonewise_predicted['Tipo Ajustado']='Prevista'
    milho1_zonewise_predicted.rename(columns={'Zones_Export': 'Production'}, inplace=True)
    milho1_zonewise_predicted.to_csv(str(path)+'/Production/Data Directory/Milho1_Predicted_Production.csv',encoding=encoding,index=False)

    ## Getting all Fitted Values - Zones
    milho1_predict.rename(columns={'Year': 'Years'}, inplace=True)
    fitted_production_milho1=retrieving_actual_production(milho1_predict,year_var)
    fitted_zonewise_production_milho1=calculate_state_to_zone(fitted_production_milho1,milho_state_to_zone,'pred')
    fitted_zonewise_production_milho1=add_confidence(fitted_zonewise_production_milho1)
    fitted_zonewise_production_milho1.rename(columns={'Zones_Export': 'Production'}, inplace=True)
    fitted_zonewise_production_milho1['Tipo']='Calculada'
    fitted_zonewise_production_milho1['Tipo Ajustado']='Real / Calculada'
    fitted_zonewise_production_milho1.to_csv(str(path)+'/Production/Data Directory/Milho1_ModelFitted_Production.csv',encoding=encoding,index=False)

    ## Getting Non Model Predicted Values - Zones
    milho1_non_model.rename(columns={'Years': 'Year'}, inplace=True)
    milho1_non_model_pred=retrieving_predicted_production(milho1_non_model,year_var)
    milho1_non_model_pred=milho1_non_model_pred[['Year','Month','UF','Monthly_Value']]
    milho1_zonewise_non_model_pred=calculate_state_to_zone(milho1_non_model_pred,milho_state_to_zone,'pred')
    milho1_zonewise_non_model_pred=add_confidence2(milho1_zonewise_non_model_pred)
    milho1_zonewise_non_model_pred.rename(columns={'Zones_Export': 'Production'}, inplace=True)
    milho1_zonewise_non_model_pred['Tipo']='Prevista'
    milho1_zonewise_non_model_pred['Tipo Ajustado']='Prevista'
    milho1_zonewise_non_model_pred.to_csv(str(path)+'/Production/Data Directory/Milho1_NonModel_Predicted_Production.csv',encoding=encoding,index=False)

    ## Getting Non Model Fitted Values - Zones
    zonewise_milho1_nonmodel_fitted=zonewise_milho1_nonmodel_actual
    zonewise_milho1_nonmodel_fitted['Production']=0
    zonewise_milho1_nonmodel_fitted=add_confidence(zonewise_milho1_nonmodel_fitted)
    zonewise_milho1_nonmodel_fitted['Tipo']='Calculada'
    zonewise_milho1_nonmodel_fitted['Tipo Ajustado']='Real / Calculada'
    zonewise_milho1_nonmodel_fitted.to_csv(str(path)+'/Production/Data Directory/Milho1_NonModel_Fitted_Production.csv',encoding=encoding,index=False)

    print(colored('\nCombining Zone-wise Model and Non-Model States Production Values - Milho 1ª Safra...\n','green'))

    milho1_prod=merge_all_states(all_states_milho1_zonewise,fitted_zonewise_production_milho1,milho1_zonewise_predicted,milho1_zonewise_non_model_pred,zonewise_milho1_nonmodel_fitted)

    milho1_prod.insert(0,'Produto','Milho 1ª Safra')
    milho1_prod=milho1_prod[['Produto','Month','Year','UF','Zonas','Low','Production','High','Tipo','Tipo Ajustado','Conf']]
    milho1_prod.columns=['Produto',u'Mês','Ano','UF',u'Microrregião',u'Produção Mín (Kt)',u'Produção Med (Kt)',u'Produção Max (Kt)','Tipo','Tipo Ajustado',u'% Confiança']

    print(colored('\nZone-Wise Production Results - Milho 1ª Safra Completed!...\n','green'))

    ## --------------------------------------------- Milho 2 ------------------------------------------------ ##

    print(colored('\nCalculating Zone-wise Production Values for all Model States - Milho 2ª Safra...\n','green'))

    milho2_actual=milho2_actual[['Years','Month','UF','Monthly_Value']]
    milho_state_to_zone=pd.read_csv(str(path)+'/Production/Data Directory/Milho_State_to_Zone_Ratio.csv',encoding=encoding)
    milho_state_to_zone=preprocessing_state_to_zone(milho_state_to_zone)

    zonewise_milho2_model_actual=calculate_state_to_zone(milho2_actual,milho_state_to_zone,'pred')
    zonewise_milho2_model_actual.columns=['Year','Month','UF','Zonas','Production']

    print(colored('\nCalculating Zone-wise Production Values for all Non Model States - Milho 2ª Safra...\n','green'))

    milho2_non_model=pd.read_csv(str(path)+'/Production/Data Directory/Milho2_Non_Model.csv',encoding=encoding)

    milho2_non_model_actual=retrieving_actual_production(milho2_non_model,year_var)
    milho2_non_model_actual=milho2_non_model_actual[['Years','Month','UF','Monthly_Value']]

    zonewise_milho2_nonmodel_actual=calculate_state_to_zone(milho2_non_model_actual,milho_state_to_zone,'pred')
    zonewise_milho2_nonmodel_actual.columns=['Year','Month','UF','Zonas','Production']

    ## Combining Model and Non-Model States - Soya
    all_states_milho2_zonewise=pd.concat([zonewise_milho2_model_actual,zonewise_milho2_nonmodel_actual],axis=0)
    all_states_milho2_zonewise=all_states_milho2_zonewise.reset_index(drop=True)
    all_states_milho2_zonewise=add_confidence(all_states_milho2_zonewise)
    all_states_milho2_zonewise['Tipo']='Real'
    all_states_milho2_zonewise['Tipo Ajustado']='Real / Calculada'
    all_states_milho2_zonewise.to_csv(str(path)+'/Production/Data Directory/Milho2_Actual_Production.csv',index=False,encoding=encoding)

    ## Getting Confidence Results for Predicted States
    p=str(path)+'/Production/Data Directory/Jan2019/Milho2_Production_Model_Re-Run_Results/Milho2 Confidence New Results/'
    milho2_confidence_result=retrieving_confidence(p,'prod')
    milho2_confidence_result.rename(columns={'Month_Num': 'Month','Ano': 'Year'}, inplace=True)

    predicted_milho2_prod=retrieving_predicted_production(milho2_predict,year_var)
    predicted_milho2_prod=pd.merge(predicted_milho2_prod[['Year','Month','UF']],milho2_confidence_result[['Year','Month','UF','Point.Forecast']],on=['Year','Month','UF'],how='outer',right_index=False,sort=True,indicator=True).query("_merge != 'right_only'").drop('_merge',axis=1)
    predicted_milho2_zonewise=calculate_state_to_zone(predicted_milho2_prod,milho_state_to_zone,'pred')

    del milho2_confidence_result['Point.Forecast']
    milho2_confidence_result=calculate_state_to_zone(milho2_confidence_result,milho_state_to_zone,'confidence')
    milho2_confidence_result.to_csv(str(path)+'/Production/Data Directory/Milho2_Confidence_Production.csv',encoding=encoding,index=False)

    ## Merging the Confidence Interval Results with Export Predictions
    milho2_zonewise_predicted=pd.merge(predicted_milho2_zonewise,milho2_confidence_result,on=['Year','Month','UF','Zonas'],how='outer',right_index=False,sort=True,indicator=True).query("_merge != 'right_only'").drop('_merge',axis=1)
    milho2_zonewise_predicted.fillna(0)
    milho2_zonewise_predicted['Tipo']='Prevista'
    milho2_zonewise_predicted['Tipo Ajustado']='Prevista'
    milho2_zonewise_predicted.rename(columns={'Zones_Export': 'Production'}, inplace=True)
    milho2_zonewise_predicted.to_csv(str(path)+'/Production/Data Directory/Milho2_Predicted_Production.csv',encoding=encoding,index=False)

    ## Getting all Fitted Values - Zones
    milho2_predict.rename(columns={'Year': 'Years'}, inplace=True)
    fitted_production_milho2=retrieving_actual_production(milho2_predict,year_var)
    fitted_zonewise_production_milho2=calculate_state_to_zone(fitted_production_milho2,milho_state_to_zone,'pred')
    fitted_zonewise_production_milho2=add_confidence(fitted_zonewise_production_milho2)
    fitted_zonewise_production_milho2.rename(columns={'Zones_Export': 'Production'}, inplace=True)
    fitted_zonewise_production_milho2['Tipo']='Calculada'
    fitted_zonewise_production_milho2['Tipo Ajustado']='Real / Calculada'
    fitted_zonewise_production_milho2.to_csv(str(path)+'/Production/Data Directory/Milho2_ModelFitted_Production.csv',encoding=encoding,index=False)

    ## Getting Non Model Predicted Values - Zones
    milho2_non_model.rename(columns={'Years': 'Year'}, inplace=True)
    milho2_non_model_pred=retrieving_predicted_production(milho2_non_model,year_var)
    milho2_non_model_pred=milho2_non_model_pred[['Year','Month','UF','Monthly_Value']]
    milho2_zonewise_non_model_pred=calculate_state_to_zone(milho2_non_model_pred,milho_state_to_zone,'pred')
    milho2_zonewise_non_model_pred=add_confidence2(milho2_zonewise_non_model_pred)
    milho2_zonewise_non_model_pred.rename(columns={'Zones_Export': 'Production'}, inplace=True)
    milho2_zonewise_non_model_pred['Tipo']='Prevista'
    milho2_zonewise_non_model_pred['Tipo Ajustado']='Prevista'
    milho2_zonewise_non_model_pred.to_csv(str(path)+'/Production/Data Directory/Milho2_NonModel_Predicted_Production.csv',encoding=encoding,index=False)

    ## Getting Non Model Fitted Values - Zones
    zonewise_milho2_nonmodel_fitted=zonewise_milho2_nonmodel_actual
    zonewise_milho2_nonmodel_fitted['Production']=0
    zonewise_milho2_nonmodel_fitted=add_confidence(zonewise_milho2_nonmodel_fitted)
    zonewise_milho2_nonmodel_fitted['Tipo']='Calculada'
    zonewise_milho2_nonmodel_fitted['Tipo Ajustado']='Real / Calculada'
    zonewise_milho2_nonmodel_fitted.to_csv(str(path)+'/Production/Data Directory/Milho2_NonModel_Fitted_Production.csv',encoding=encoding,index=False)

    print(colored('\nCombining Zone-wise Model and Non-Model States Production Values - Milho 2ª Safra...\n','green'))

    milho2_prod=merge_all_states(all_states_milho2_zonewise,fitted_zonewise_production_milho2,milho2_zonewise_predicted,milho2_zonewise_non_model_pred,zonewise_milho2_nonmodel_fitted)

    milho2_prod.insert(0,'Produto','Milho 2ª Safra')
    milho2_prod=milho2_prod[['Produto','Month','Year','UF','Zonas','Low','Production','High','Tipo','Tipo Ajustado','Conf']]
    milho2_prod.columns=['Produto',u'Mês','Ano','UF',u'Microrregião',u'Produção Mín (Kt)',u'Produção Med (Kt)',u'Produção Max (Kt)','Tipo','Tipo Ajustado',u'% Confiança']

    print(colored('\nZone-Wise Production Results - Milho 2ª Safra Completed!...\n','green'))
    print(colored('\nStarted Combining Zone-Wise Production Results - All Crops...\n','green'))

    ## Combining all crops together
    all_crops=pd.concat([soya_prod.reset_index(drop=True),milho1_prod.reset_index(drop=True)],axis=0)
    all_crops=pd.concat([all_crops.reset_index(drop=True),milho2_prod.reset_index(drop=True)],axis=0)
    all_crops=all_crops.reset_index(drop=True)

    state1=get_state_file(str(path)+"/Production/Input Files/")

    all_crops=pd.merge(all_crops,state1,left_on=['UF'],right_on=['State_Code'],how='left',right_index=False,sort=True,indicator=True).query("_merge != 'left_only'").drop('_merge',axis=1)
    del all_crops['State_Code']

    all_crops=all_crops[['Produto',u'Mês','Ano','State_Name','UF',u'Microrregião',u'Produção Mín (Kt)',u'Produção Med (Kt)',u'Produção Max (Kt)','Tipo','Tipo Ajustado',u'% Confiança']]
    all_crops['State_Name']=all_crops['State_Name'].str.upper()
    all_crops.rename(columns={'State_Name': 'Estado'}, inplace=True)

    all_crops.iloc[:,11] = all_crops.iloc[:,11].apply(pd.to_numeric, errors = 'coerce')

    for field in all_crops.iloc[:,6:9]:
        all_crops[field] = all_crops[field].map(try_cutoff)

    print(colored('\nCombining Zone-Wise Production Results - All Crops Completed!...\n','green'))

    ## Saving in Indian Settings
    date=get_date()
    all_crops.to_csv(str(path)+'/Production/Output Files/'+date+u'_Produção.csv',index=False,encoding=encoding)
    all_crops.to_csv(str(path)+'/Export/Input Files/'+date+u'_Produção.csv',index=False,encoding=encoding)
    all_crops.to_csv(str(path)+'/Excess Exportable/Input Files/'+date+u'_Produção.csv',index=False,encoding=encoding)
    all_crops.to_csv(str(path)+'/Production/Output Files/Produção.csv',index=False,encoding=encoding)

    print(colored('\nZone-Wise Production Prediction Results Saving in Database !!!...\n','green'))

    ## Saving in Brazilian Settings
    all_crops[[u'Produção Mín (Kt)',u'Produção Med (Kt)',u'Produção Max (Kt)']] = all_crops[[u'Produção Mín (Kt)',u'Produção Med (Kt)',u'Produção Max (Kt)']].astype(str)
    for field in all_crops.iloc[:,6:9]:
        all_crops[field] = all_crops[field].map(Replace)

    all_crops.to_excel(str(path)+'/Production/Output Files/'+date+u'_Produção.xlsx',index=False,encoding=encoding)
    all_crops.to_excel(str(path)+'/Excess Exportable/Input Files/'+date+u'_Produção.xlsx',index=False,encoding=encoding)

    print(colored('\nProduction Forecasting & Output Results Process Completed !!!...\n','green'))

if __name__ == '__main__':
    main()


from flask import jsonify, render_template, session, url_for, request, g, abort
from app import app#, db
import json
import os
import shutil
import socket
import pickle
import numpy as np
import random
from werkzeug import secure_filename
from server.baseClient import dbClient

client = dbClient()

ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif', 'PNG', 'JPG', 'JPEG', 'GIF'])
EMOTIONS = ['amusement', 'awe', 'contentment', 'anger', 'disgust', 'excitement', 'fear', 'sadness']
EMOTIONSLABELS = {'amusement': 1, 'awe': 2, 'contentment': 3, 'anger': 4, 'disgust': 5, 'excitement': 6, 'fear': 7, 'sadness': 8}

DATABASE_IMAGE_PATH = os.path.abspath('./') + "/server/data/images/other/"
WEB_IMAGE_PATH = os.path.abspath('.') + "/frontend/app/static/reqImg/"

WEB_UPLOAD_PATH = os.path.abspath('.') + "/frontend/upload/"
DATABASE_INSERT_PATH = os.path.abspath('.') + "/server/"

def moveImge(imgName, originPath, desPath):
    # print(originPath + imgName)
    # print(desPath)
    shutil.copy(originPath + imgName, desPath)
    return

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html', title='Home')

@app.route('/')
@app.route('/predict')
def predict():
    # if request.method == 'GET':
    return render_template('predict.html', title='predict')

@app.route('/')
@app.route('/labelling')
def labelling():
    # if request.method == 'GET':
    return render_template('labelling.html', title='Labelling')

def allowedFile(filename):
    return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS

@app.route('/imgClassify', methods=['POST', 'GET'])
def imgClassify():
    if request.method == 'POST':
        # take the upload picture as an input
        # carry out emotion classification
        # return emotion category
        if 'image-up' not in request.files:  # no image
            print("no image!")
            return jsonify(message='Network problem, no image...')

        # TODO:
        # too complicated, need to be simplified
        fileMeta = request.files["image-up"]
        filename = fileMeta.filename
        if fileMeta and allowedFile(filename):
            filename = secure_filename(filename)
            # fake Data test:
            # fileMeta.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            # emotionCategory = 'guess what?'
            # print("")
            # print(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            # print("")

            fileMeta.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

            #moveImge(filename, WEB_UPLOAD_PATH, DATABASE_INSERT_PATH)

            # client.insertImage(filename)

            emotionCategory = client.predict_and_insert(filename)
            # emotionCategory.sort(key=lambda x: -x[0])
            predictEmotion = []
            for emotion in emotionCategory:
                predictEmotion.append("{:.1f}".format(emotion[0]*100))
                predictEmotion.append(emotion[1])
            # print(predictEmotion)

            ################
            # add prediction logic
            ################
            # -->call Classification Model get EMOTION_CATEGORY
            # -->call DB Model store image

            # image store folder path: UPLOAD_FOLDER in __init__.py
            # predict result: emotionCategory(string)

            return jsonify(message='success', predictResult=predictEmotion)
        else:
            print("File format error!")
            return jsonify(message='File format error!')
    else:
        # take the choosen emotion category as an input
        # return example picture of this emotion
        return

# IMGID = ""

@app.route('/imageLable', methods=['POST', 'GET'])
def imageLable():

    if request.method == 'POST':
        # add a new label of given picture into the database
        newLable = request.form.getlist("label[]")
        targetImgID = request.form.get("imgId")
        # print("New label: ", newLable)
        # print("targetImgID: ", targetImgID)

        if targetImgID == "EMPTY":
            return jsonify(message='noImg')

        if len(newLable) > 0:
            # print("New label: ", newLable)
            ################
            # add store new label logic
            ################
            # newLable = ['NEW_EMOTION']
            # TODO:
            # too complicated, need to be simplified

            emotion = newLable[0]
            client.insertModelLabel(targetImgID, EMOTIONSLABELS[emotion])
            # print(emotion)
            # print(EMOTIONSLABELS[emotion])
            return jsonify(message='success')

        else:
            return jsonify(message='empty')
    else:
        # send back un-labeled/week-labeled picture
        # as well as the week-labeled

        # fake Data test:
        # imageName = random.randint(0, 4)
        # reqImage = app.config['REQUEST_FOLDER'] + "/" + str(imageName) + ".jpg"
        # weekLabel = ['awe', 'contentment', 'anger']

        ################
        # add request image and label logic
        ################
        # -->call DB server get img file path and weeklabel

        # TODO:
        # too complicated, need to be simplified

        imgData = client.getRandomImageWithWeakLabel()
        # print("==========start printing important !!!!!!!!!!============")
        # print(imgData)
        # print("==========end printing important !!!!!!!!!!============")

        imgPath = imgData['path']
        imgName = imgPath.split('/')[-1]

        if not os.path.isdir(WEB_IMAGE_PATH):
            os.makedirs(WEB_IMAGE_PATH)

        moveImge(imgName, DATABASE_IMAGE_PATH, WEB_IMAGE_PATH)
        reqImage = app.config['REQUEST_FOLDER'] + "/" + imgName

        weekLabelNum = imgData['labels']
        imgID = imgData['id']
        # IMGID = imgID
        weekLabel = []
        for i in range(len(weekLabelNum)):
            weekLabel.append(EMOTIONS[weekLabelNum[i] - 1])
        # print(weekLabel)
        # print(imgPath)
        return jsonify(img=reqImage, label=weekLabel, id=imgID)

# print("test: ")
# print(client.getRandomImageWithWeakLabel())
# print("Finish test.")


from selenium import webdriver
import pytest

@pytest.fixture(scope='class',params=["chrome","firefox"])
def setup_method(request):
    if request.param == "chrome":
        driver = webdriver.Chrome("./chromedriver")

    if request.param == "firefox":
        driver = webdriver.Chrome("./chromedriver")
    request.cls.driver = driver
    yield
    driver.quit()
@pytest.mark.usefixtures('setup_method')
class Base_class:
    pass
class Test_google(Base_class):
    def test_google(self):
        self.driver.get("https://www.google.com")
        assert self.driver.title == "Google"

class Test_Facebook(Base_class):
    def test_facebook(self):
        self.driver.get("https://www.facebook.com")
        assert self.driver.title == "Facebook"


import astropy.table as t
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import glob
from matplotlib.ticker import ScalarFormatter
from astropy.cosmology import Planck15 as planck
import scipy.optimize as sciopt
import astropy.table as tab
import os

def make_many_plots(den, met, outpath, ion_num = [3, 8], uvb_true ='KS18', q_true = 18):
    # setting the figure
    font = {'family': 'serif', 'weight': 'normal', 'size': 14}
    plt.rc('font', **font)
    mpl.rcParams['axes.linewidth'] = 1.5

    final_path  = outpath
    if not os.path.isdir(final_path):
        os.mkdir(final_path)

    out_fig_name = outpath + '/true_UVB_{}_Q{}_{:.0f}ions_logZ{}_logN{}_3_8combined.pdf'.format(uvb_true, q_true, ion_num[0], met, np.log10(den))
    print('creating file ', out_fig_name)
    #figure_size = [14, 4]
    #fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(figure_size[0], figure_size[1]))

    figure_size = [5, 10]
    fig, ((ax1), (ax2), (ax3)) = plt.subplots(3, 1, figsize=(figure_size[0], figure_size[1]))

    plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0.2, wspace=0.0)

    path = '/home/vikram/cloudy_run/diff_op/photo_NH15'

    #uvb = ['KS18', 'HM12', 'P19', 'FG20']
    uvb = ['KS18',  'P19', 'FG20']

    uvb_Q = [14, 15, 16, 17, 18, 19, 20]

    uvb_models = []
    the_Q_values = []
    for background in uvb:
        if background == 'KS18':
            for q in uvb_Q:
                uvb_models.append(background)
                the_Q_values.append(q)
        else:
            q = 18
            uvb_models.append(background)
            the_Q_values.append(q)

    # ks_array = ['14', '15', '16', '17', '18', '19', '20']
    # all_uvb = ks_array + ['FG20', 'P19']
    # n_H_array = [1e-3, 1e-4, 1e-5]

    """
    for num in ion_num:
        d = tab.Table.read(path + '/diff_res_{}ions_new.txt'.format(num), format='ascii')
        print(num,  len(d))
        for m, n, Z, in zip(d['Q'], d['Max_diff_nH'], d['Max_diff_Z']):
            for uvb in all_uvb:
                if uvb == m:
                    n2_all.append(n)
                    z2_all.append(Z)

    print(len(n2_all))
    ax.scatter(n2_all, z2_all, alpha = 0.5, color = 'b')
    """

    binwidth = 0.01

    d = tab.Table.read(path + '/all_combined.fits')
    d = d [d['true_uvb'] == uvb_true]
    d = d [d['true_Q'] == q_true]

    color_list = ['dodgerblue', 'orange', 'green', 'magenta']

    num=ion_num[0]
    dnum0 = d[d['n_ions'] == num]
    sort_d0 = dnum0[dnum0['true_nH'] == den]
    sort_d0 = sort_d0[sort_d0['true_logZ'] == met]

    num=ion_num[1]
    dnum = d[d['n_ions'] == num]
    sort_d = dnum[dnum['true_nH'] == den]
    sort_d = sort_d[sort_d['true_logZ'] == met]

    for the_uvb, q_val in zip(uvb_models, the_Q_values):

        col_name_nH = 'nH_' + the_uvb + '_Q{}'.format(q_val)
        col_name_Z = 'Z_' + the_uvb + '_Q{}'.format(q_val)
        n_max_array = sort_d[col_name_nH]
        z_max_array = sort_d[col_name_Z]

        n_max_array0 = sort_d0[col_name_nH]
        z_max_array0 = sort_d0[col_name_Z]

        if the_uvb =='KS18':
            label0 = 'KS19 Q{}'.format(the_uvb, q_val)
        else:
            label0 = '{} '.format(the_uvb)

        sc= ax1.scatter(n_max_array, z_max_array, alpha=0.3, s=13)
        plt.draw()
        color_draw = sc.get_edgecolor()[0]
        new_colr = [color_draw[0], color_draw[1], color_draw[2], 1]
        new_colr_alp = [color_draw[0], color_draw[1], color_draw[2], 0.7]
        ax1.scatter(n_max_array, z_max_array, alpha=0.3, facecolor = 'None', s=13, edgecolor  = new_colr_alp, linewidth = 0.6)

        #ax1.scatter(n_max_array, z_max_array, color = 'k', alpha = 0.8, s = 13, facecolor ='None')
        #ax1.scatter(n_max_array0, z_max_array0, marker ='x', s=10, color = new_colr)
        ax1.scatter(n_max_array0, z_max_array0, marker ='x', s=7, color  = new_colr, linewidth = 0.1)

        #print(line[0].get_color())

        ax2.hist(n_max_array, bins=np.arange(min(n_max_array)-0.5*binwidth, max(n_max_array) + 1.5*binwidth, binwidth),
                 alpha=0.75,
                 histtype='stepfilled', linewidth=1.2, edgecolor = 'k', facecolor = new_colr_alp)

        ax3.hist(z_max_array, bins=np.arange(min(z_max_array)-0.5*binwidth, max(z_max_array) + 1.5*binwidth, binwidth),
                 alpha=0.75,
                 histtype='stepfilled', linewidth=1.2, edgecolor = 'k', facecolor = new_colr_alp)

        #--------------------

        ax2.hist(n_max_array0, bins=np.arange(min(n_max_array0)-0.5*binwidth, max(n_max_array0) + 1.5*binwidth, binwidth),
                 histtype='step', linewidth=1.2, edgecolor = new_colr)

        ax3.hist(z_max_array0, bins=np.arange(min(z_max_array0)-0.5*binwidth, max(z_max_array0) + 1.5*binwidth, binwidth),
                 histtype='step', linewidth=1.2, edgecolor = new_colr)

        n_med = np.median(n_max_array)
        z_med = np.median(z_max_array)

        label_name = r'log n$_{\rm H}$) =' + '{:.2f} ->{} Q{}'.format(n_med, the_uvb, q_val)
        ax2.axvline(n_med, label=label_name, linewidth = 0.8, dashes=[5, 7], color = new_colr, zorder =50)
        txt = '{:.2f}'.format(n_med)
        if the_uvb != 'P19':
            ax2.annotate(txt, (n_med+0.008, 150), fontsize=8, color = new_colr)
        else:
            ax2.annotate(txt, (n_med+0.008, 135), fontsize=8, color = new_colr)

        label_name = r' log Z =' + '{:.2f} ->{} Q{}'.format(z_med, the_uvb, q_val)
        ax3.axvline(z_med, label=label_name, linewidth = 0.8, dashes=[5, 7], color = new_colr, zorder =50)
        txt = '{:.2f}'.format(z_med)
        if the_uvb != 'P19' and q_val !=20:
            ax3.annotate(txt, (z_med+0.008, 150), fontsize=8, color = new_colr)
        else:
            ax3.annotate(txt, (z_med+0.008, 135), fontsize=8, color = new_colr)

        if the_uvb != 'P19' and the_uvb !='FG20':
            txt = 'Q{}'.format(q_val)
            if q_val <=15:
                ax1.annotate(txt, (n_med- 0.03, z_med + 0.082), fontsize=8, color = new_colr)
            else:
                ax1.annotate(txt, (n_med - 0.03, z_med + 0.055), fontsize=8, color=new_colr)
        else:
            txt = '{}'.format(the_uvb)
            if the_uvb == 'FG20':
                ax1.annotate(txt, (n_med -0.03, z_med + 0.12), fontsize=8, color = new_colr)
            else:
                ax1.annotate(txt, (n_med -0.03, z_med - 0.1), fontsize=8, color = new_colr)

        print(the_uvb)

    ax1.annotate('Photoionized absorber\n' +  'True UVB KS19 (Q18)', xy=(0.06, 0.83), xycoords='axes fraction', fontsize=11)

    ax1.annotate(r'True (log Z, log n$_{\rm H}$) = ' + '({:.0f}, {:.0f})'.format(met, np.log10(den)),
                 xy=(0.06, 0.76), xycoords='axes fraction', fontsize=11)

    x = [-1000, -1000]
    ax1.scatter(x, x, alpha=0.5, s=18, label='Using 8 ions', color = 'b')
    ax1.scatter(x, x, alpha=0.5, s=15, label='Using 3 ions', color = 'b', marker  = 'x')

    lg = ax1.legend(fontsize=11, loc=4)
    lg.get_frame().set_facecolor('none')

    """
    n_med = np.median(n_max_array)
    z_med = np.median(z_max_array)

    ax2.axvline(n_med, linestyle = '--', color = 'cyan')
    ax3.axvline (z_med, linestyle = '--', color = 'cyan')

    ax2.annotate (r'Median' , xy=(0.6, 0.9), xycoords='axes fraction', fontsize=12)
    ax2.annotate (r'$\Delta_{\rm max}$ (log n$_{\rm H}$) =' + '{:.2f}'.format(n_med) , xy=(0.6, 0.83), xycoords='axes fraction', fontsize=11)

    ax3.annotate (r'Median' , xy=(0.6, 0.9), xycoords='axes fraction', fontsize=12)
    ax3.annotate (r'$\Delta_{\rm max}$ (log Z) =' + '{:.2f}'.format(z_med) , xy=(0.6, 0.83), xycoords='axes fraction', fontsize=11)

    print(n_med, z_med)
    """

    #ax1.legend(loc='best', fontsize=9, ncol=2)
    #ax2.legend(loc='best', fontsize=9)
    #ax3.legend(loc='best', fontsize=9)

    #ax1.set_xlabel(r'log n$_{\rm H}$ (cm $^{-3}$)')
    ax1.set_ylabel(r'log Z(Z$_{\odot}$)')

    add_num = 0.49
    ax1.set_xlim(-4.4, np.log10(den)+add_num)
    ax1.set_ylim(-1.4, met+add_num)
    ax2.set_xlim(-4.4, np.log10(den)+add_num)
    ax3.set_xlim(-1.4, met+add_num)
    ax2.set_ylim(0, 174)
    ax3.set_ylim(0, 174)

    ax2.set_xlabel(r'log n$_{\rm H}$ (cm $^{-3}$)')
    ax3.set_xlabel(r'log Z(Z$_{\odot}$)')
    ax2.set_ylabel('Frequency')
    ax3.set_ylabel('Frequency')

    # ax.set_xscale('log')
    # ax.set_yscale('log')

    # ax.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))

    for ax in (ax1, ax2, ax3):
        # deco
        ax.tick_params(direction='in', length=7, width=1.7)
        ax.tick_params(direction='in', which='minor', length=4, width=1.7)
        ax.yaxis.set_ticks_position('both')
        ax.xaxis.set_ticks_position('both')
        # decorating the plot
        for axis in ['top', 'bottom', 'left', 'right']:
            ax.spines[axis].set_linewidth(1.7)
            ax.spines[axis].set_color('k')

    fig.savefig(out_fig_name, bbox_inches='tight')

    plt.close()

    return

"""
Zarray = [-2, -1, 0]
Narray = [1e-5, 1e-4, 1e-3]

for n in Narray:
    for z in Zarray:
        make_many_plots(den=n, met=z, outpath= '/home/vikram/cloudy_run/more_fig/photo')
"""

Zarray = [-1]
Narray = [1e-4]

for n in Narray:
    for z in Zarray:
        make_many_plots(den=n, met=z, outpath= '/home/vikram/cloudy_run/more_fig/photo')


from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from sitepages.views import home, site_page
from contact.views import contact_page

urlpatterns = [
                  url(r'^admin/', admin.site.urls),
                  url(r'^about-us/$', site_page, {'slug': 'about-us'},
                      name='about_us'),
                  url(r'^faqs/$', site_page, {'slug': 'faqs'}, name='faqs'),
                  url(r'^contact/$', contact_page, name='contact'),
                  url(r'^$', home, name='home'),
                  url(r'^accounts/',
                      include('accounts.urls', namespace='accounts')),
                  url(r'^projects/',
                      include('projects.urls', namespace='projects')),
                  url(r'^generate/',
                      include('generate.urls', namespace='generate')),
              ] + static(settings.STATIC_URL,
                         document_root=settings.STATIC_ROOT)

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import numpy as np
from Tools import Tools
from sklearn.cluster import MiniBatchKMeans

class Cluster:
    def __init__(self, dataSet, centroid):
        self.dataSet = dataSet
        self.centroid = centroid

    def getCentroid(self):
        return self.centroid

    def getCluster(self):
        return self.dataSet

class KMeans:
    def __init__(self):
        self.centroids = np.array([])
        self.cluster_0 = []
        self.cluster_1 = []
        self.c_0 = None
        self.c_1 = None

    def split(self, dataSet):
        kM = MiniBatchKMeans(n_clusters=2)
        result = kM.fit(dataSet)

        self.c_0 = result.cluster_centers_[0]
        self.c_1 = result.cluster_centers_[1]

        index = 0
        for label in result.labels_:
            if label == 0:
                self.cluster_0.append(dataSet[index])
            else:
                self.cluster_1.append(dataSet[index])
            index += 1
        self.cluster_0 = np.array(self.cluster_0)
        self.cluster_1 = np.array(self.cluster_1)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('shop', '0005_image_for_main'),
    ]

    operations = [
        migrations.CreateModel(
            name='CheckoutOrder',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('checkout_id', models.PositiveIntegerField(max_length=255, verbose_name='order id at Checkout platform')),
                ('delivery_id', models.PositiveIntegerField(max_length=255, verbose_name='id of delivery type')),
                ('delivery_type', models.CharField(max_length=63, verbose_name='Delivery type')),
                ('cost', models.DecimalField(verbose_name='Cost', max_digits=10, decimal_places=2)),
                ('_status', models.CharField(max_length=63, null=True, blank=True)),
                ('_status_update_time', models.DateTimeField(auto_now_add=True, verbose_name='Update date')),
                ('order', models.OneToOneField(to='shop.Order')),
            ],
            options={
                'verbose_name': 'Order at "Checkout" platform',
                'verbose_name_plural': 'Orders at Checkout platform',
            },
            bases=(models.Model,),
        ),
    ]


from bs4 import BeautifulSoup
import urllib
import re
import gmailpython as mail
import sys

def nextLevel(addrs):
    for addr in addrs:
        html_page = urllib.request.urlopen(addr)
        #with urllib.request.urlopen("http://dl.funsaber.net/serial/Narcos/season%201/720/") as url:
        #    s = url.read()
        #html_page = s
        soup = BeautifulSoup(html_page,"html.parser" )
        for link in soup.findAll('a'):
            url_part = link.get('href')
            if not '../' in url_part :
                links.append(addr+url_part)

addr=['http://dl.funsaber.net/serial/Narcos/season%201/720/',
'http://dl.funsaber.net/serial/Narcos/season%202/720/',
'http://dl.funsaber.net/serial/Narcos/season%203/720/']
links=["#rdmStart"]

if(len(sys.argv)>1):
    addr = sys.argv[1:]

nextLevel(addr)
links.append("\n#rdmEnd")
links = '\n'.join(links)
print (links)
details=mail.getUserDetails()
if 'zebra' in details[2]:
    details[2]='vaibhavfury@gmail.com'
mail.notifyKappe(details[0],details[1],details[2],"Narcos links #rdm",[],links)