"""
Authors: Amanda Landi (alandi@simons-rock.edu) and Joshua Landi (jlandi@ncsu.edu)
As part of a research project; results are forthcoming in a paper published in ___.

References:
(1) Segaran, Toby. 2007. Programming collective intelligence (First. ed.). O'Reilly. Chapter 11: Evolving Intelligence.
"""
import warnings
from copy import deepcopy
from random import choice, random, sample
from typing import List, Tuple

import numpy as np
from typing import NDArray
from sklearn.metrics import mean_squared_error, mean_absolute_error

from classes.node_type_enum import NodeType
from classes.function_node import FunctionNode
from classes.constant_node import ConstantNode
from classes.variable_node import VariableNode
from classes.node import Node
from classes.random import Random

"""
a class to manage the genetic part of GP
"""

class GeneticManagement:

    _max_depths: List[int]
    _independent_variables: NDArray[float]
    _random: Random

    def __init__(self, random: Random) -> None:
        self._random = random
        pass

    def set_max_depths(self, value: List[int]) -> 'GeneticManagement':
        self._max_depths = value
        return self

    def set_independent_variables(self, value: NDArray[float]) -> 'GeneticManagement':
        self._independent_variables = value
        return self

    def evaluate_tree(self, center_node: FunctionNode, inputs: NDArray[float]) -> Tuple[float, str]:

        values: List[int] = []
        function_string = center_node.short_name + "(" + "\t"

        for index, child in enumerate(center_node.children):
            if index == 1:
                function_string += ", "

            if isinstance(child, ConstantNode) or isinstance(child, VariableNode):
                values.append(child.evaluate(inputs))
                function_string += child.__str__() + "\t"

            else:
                value, _function_string = self.evaluate_tree(child, inputs)
                values.append(np.array(value))
                function_string +=  _function_string + "\t"

        if center_node.arity == 1:
            value = center_node.evaluate(values[0])
        else:
            value = center_node.evaluate([values[0],values[1]])

        function_string += ")"

        return (np.nan_to_num(value), function_string)

    def fitness(self, random_program: FunctionNode, y: NDArray[float]) -> float:

        if len(self._independent_variables.shape) > 1:
            row_count, column_count = self._independent_variables.shape
        else:
            row_count = 1
            column_count = len(self._independent_variables)

        value, function_string = self.evaluate_tree(random_program, self._independent_variables)

        if np.isscalar(value):
            temp = value*np.ones(y.shape)
            fit = mean_absolute_error(temp, y)
        else:
            fit = mean_absolute_error(np.reshape(value, y.shape), y)

        return fit

    def tournament(self, population: Tuple[List[Node]], y: NDArray[float], threshold: float = 0.01, tournament_size: int = 10,
            p_mutate: float = 0.2, p_crossover: float = 0.8, train: int = 0):

        winners = []; fit = []; maxdep = []
        random_indx = sample([i for i in range(len(population))], len(population))

        for i in range(len(population)):
            f = fitness(population[i], y)
            fit.append(f)

        for i in range(0, len(population), tournament_size):

            idx = random_indx[0+i:(i+tournament_size)]
            f = np.argsort([fit[k] for k in idx])
            if f[0] < threshold:
                winners.append(population[idx[f[0]]])
                maxdep.append(self._max_depths[idx[f[0]]])
            else:
                win1 = population[idx[f[0]]]
                win2 = population[idx[f[1]]]
                w1_depth = self._max_depths[idx[f[0]]]
                w2_depth = self._max_depths[idx[f[1]]]
                winners.append(win1, win2)
                maxdep.append(w1_depth, w2_depth)

                if train == 0:
                    child, c_max_d = evolve([win1, win2], [w1_depth, w2_depth], p_mutate, p_crossover, train)
                    winners.append(child[0])
                    maxdep.append(c_max_d[0])

        return (winners, maxdep)

    def mutate(self, program: Node, max_depth: int, p_mutate: float = 0.20):

        if random() < p_mutate:
            if isinstance(program, FunctionNode):
                new_center_node = self._random.node(NodeType.Function)
                if new_center_node.arity == program.arity:
                    new_center_node.children = deepcopy(program.children)
                    return new_center_node
                elif new_center_node.arity < program.arity:
                    new_center_node.children = deepcopy([choice(program.children)])
                    return new_center_node
                else:
                    max_depth -= 1
                    new_child = self._random.node(NodeType.Any).spawn_children(max_depth, self._random)
                    old_child = deepcopy(program.children[0])
                    new_center_node.children = [old_child, new_child]
                    return new_center_node
            else:
                new_center_node = self._random.node(choice([NodeType.Variable, NodeType.Constant]))
                return new_center_node
        else:
            if isinstance(program, FunctionNode):
                new_children_node = [self.mutate(k, max_depth, p_mutate) for k in program.children]
                program.children = new_children_node
            return program

    def crossover(self, program1, program2, p_crossover: float = 0.80):

        if random() < p_crossover:

            program1 = deepcopy(program2)
            return program1

        else:

            if isinstance(program1, FunctionNode) and isinstance(program2, FunctionNode):
                c1 = program1.children
                c2 = program2.children
                change_in_children = [self.crossover(k, choice(c2)) for k in c1]
                program1.children = change_in_children

            return program1

    def evolve(self, winners, max_depths = None, p_mutate: float = 0.2, p_crossover: float = 0.8, train: int = 0):

        new_gen = []; new_gen_depths = []

        if train == 0:

            for i in range(len(winners)-1):

                new_gen.append(self.mutate(self.crossover(winners[i], winners[i+1], p_crossover = p_crossover), max_depth = min(max_depths[i], max_depths[i+1]), p_mutate = p_mutate))
                new_gen_depths.append(max(max_depths[i], max_depths[i+1]))

        else:

            for i in range(len(winners)-1):

                new_gen.append(self.mutate(self.crossover(winners[i], winners[i+1], p_crossover = p_crossover), max_depth = min(self._max_depths[i], self._max_depths[i+1]), p_mutate = p_mutate))
                new_gen_depths.append(max(self._max_depths[i], self._max_depths[i+1]))

        return (new_gen, new_gen_depths)


#!/usr/bin/env python3.2
#coding=UTF-8

'''

Google Code Jam 2015
Qualification Round
Problem B
Haircut

Instructions:
	chmod u+x process.py
	./process.py <input_file >output_file
	# stderr output is intended to display on a console

Notes:
	* seems like a matter of implemented a simulation of the described process
	* will a direct implementation be too slow?
	* problem C seems too hard unless you are well-versed in geometry
		* you'd probably have to avoid floating point calculations to avoid potential for fp error
		* better to do problem B before looking in-depth at problem C
	* I might not succeed at the large dataset, so I'll try a simple algorithm for the small dataset first
	* actually, I'm thinking of an algorithm:
		* the idea is to home in... perhaps by binary search, on the correct position on the timeline
		* each barber has a repeating cycle, and we can instantly calculate their customers per minute or minutes per customer
		* actually, what if we work in floating point units...
			* calculate a total customers per minute value for all barbers together
			* calculate the minutes to serve N-1 customers from that
			* use that as a starting estimate...
	* maybe I should go straight for the large after all
	* maybe we should group the barbers by their cycle size? - would that amount to a big-O improvement though?
	* so it seems we'd be doing binary search on the minutes... do we even have a max value for that? -- 100,000 * 1,000,000,000 (worse case of 1 barber)
	* given a number of minutes, we can write a function to work out:
		* number of customers fully served so far
	* damn it... I can't waste more time thinking about it...
	* I'll do a binary search on the full 100,000,000,000,000 time space, right?
	* that will be umm...about 47 iterations... not bad - and we probably have to iterate over all barbers every iteration
	* I'm just a little unclear on how we get to the exact right position where customer N is just starting their cut
	* once we work out the time, we just iterate over barbers, filtered by those who are exactly finishing/starting

'''

################################################################################

def read_case(id, input):
	B, N = [int(n) for n in input.readline().split()]
	barbers = [int(n) for n in input.readline().split()]
	assert len(barbers) == B
	return (N, barbers)

def solve_case(id, case):
	N, barbers = case
	B = len(barbers)

	debug('{} {}\n{}\n'.format(B, N, barbers))

	def served_at(t):
		if t < 0:
			return 0
		served = B	# includes those just starting
		for barber_time in barbers:
			served += t // barber_time

		return served

	for t in range(20):
		served = served_at(t)
		debug('t: {}, served: {}\n'.format(t, served))

	def find_time(N):
		min = -1
		max = 100000 * 1000000000
		# range covered is min to max-1, inclusive
		last = 0
		while True:
			trial = (min + max) // 2
			assert trial != last
			served = served_at(trial)
			#debug('{}..{}..{}\n'.format(min, trial, max))
			#debug('N: {}, served: {}\n'.format(N, served))

			if served < N:
				min = trial
			else:
				max = trial

			if min + 1 == max:
				# max is the lowest time at which N or greater have been or are being served
				debug('time: {}, served: {}\n'.format(max, served))
				return max, served
			last = trial

	t, served = find_time(N)

	def find_barber(t, N, served):
		served = served_at(t-1)
		debug('previous: {}\n'.format(served))
		for barber_id, barber_time in enumerate(barbers):
			cut_progress = t % barber_time
			debug('{} {} {}\n'.format(barber_id, barber_time, cut_progress))
			if cut_progress == 0:
				served += 1
				if served == N:
					debug('id: {}\n'.format(barber_id))
					return barber_id

	barber_id = find_barber(t, N, served)

	return "Case #{}: {}\n".format(id, barber_id + 1)

def prepare():
	def prepare_data():
		return None

	global prepared_data
	prepared_data = prepare_cached(prepare_data, 'prepared_data.cached')

################################################################################

from sys import stdin, stdout, stderr
import time
import math
import pickle
import io

execution_timer = time.time
#execution_timer = time.clock
debugging = 1

################################################################################

def debug(message):
	if debugging:
		stderr.write(message() if hasattr(message, '__call__') else message)

def report(message):
	stderr.write(message)

def prepare_cached(prepare_data, pickle_path='data.pickle'):
	try:
		data = pickle.load(io.open(pickle_path, 'rb'))
		report("Loaded {}.\n".format(pickle_path))
	except IOError:
		data = prepare_data()
		report("Prepared {}.\n".format(pickle_path))
		pickle.dump(data, io.open(pickle_path, 'wb'))
	return data

def main():
	t0 = execution_timer()
	prepare()
	t1 = execution_timer()
	report("Completed preparation in {:.6f} seconds.\n".format(t1 - t0))

	T = int(stdin.readline())
	for case_id in range(1,T+1):
		report("Processing test case {} of {} (output {}). {:.0f} seconds elapsed.".format(case_id, T, case_id-1, execution_timer() - t1))
		report("\n" if debugging else "\r")
		stderr.flush()
		stdout.write(solve_case(case_id, read_case(case_id, stdin)))
		stdout.flush()

	t2 = execution_timer()
	report("Processed {} test cases in {:.6f} seconds.                           \n".format(T, t2 - t1))
	report("Total time: {:.6f} seconds.\n".format(t2 - t0))

if __name__ == '__main__':
	main()


"""
有一个无限长的数字序列: 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, ...
数字k在该序列中正好出现k次
这个数字序列的第n项是多少

1 + 2 + ... + k的位置刚好是数字k出现的最后一个位置
即求解满足1+2+...+k>=n 的k的最小正整数解就可以
也就是，求解一元二次方程k(k+1)/2=n的解，然后向上取整
"""

import math
n = int(input())
res = math.ceil(math.sqrt(2*n + 0.25) - 0.5)
print(res)


def rearrange_digits(input_list):
    """
    Rearrange Array Elements so as to form two number such that their sum is maximum.

    Args:
       input_list(list): Input List
    Returns:
       (int),(int): Two maximum sums
    """
    if len(input_list) < 2:
        return None, None

    first_number = 0
    second_number = 0

    input_list = merge_sort(input_list)

    i = len(input_list) - 1
    while i >= 0:
        first_number = first_number * 10 + input_list[i]
        i -= 1
        if i >= 0:
            second_number = second_number * 10 + input_list[i]
            i -= 1

    return first_number, second_number

def merge_sort(input_list):
    if len(input_list) <= 1:
        return input_list

    mid = len(input_list) // 2
    left = input_list[:mid]
    right = input_list[mid:]

    left = merge_sort(left)
    right = merge_sort(right)

    return merge(left, right)

def merge(left, right):
    merge_list = []
    i = 0
    j = 0

    while i < len(left) and j < len(right):
        if left[i] < right[j]:
            merge_list.append(left[i])
            i += 1
        else:
            merge_list.append(right[j])
            j += 1

    merge_list += left[i:]
    merge_list += right[j:]

    return merge_list

def test_function(test_case):
    output = rearrange_digits(test_case[0])
    solution = test_case[1]
    if sum(output) == sum(solution):
        print("Pass")
    else:
        print("Fail")

test_case = [[1, 2, 3, 4, 5], [542, 31]]
test_function(test_case)

test_case = [[4, 6, 2, 5, 9, 8], [964, 852]]
test_function(test_case)

print(rearrange_digits([1, 2, 9, 4, 5, 6]))  # should print (952, 641)
print(rearrange_digits([1]))  # should print (None, None)
print(rearrange_digits([]))  # should print (None, None)


# James Davenport
# CTEC 121 / Fall 2019
# Module 4 / Problem Set 5
# Problem 2 (50 points)

"""
Using the graphics library, develop a Python program to draw the a set of LEGOS. You will find a picture of the LEGO's in a file named LEGOS.png.

Be sure to create one LEGO and then use the .clone() method whenever possible to create the other five.
"""

from graphics import *

def main():
    win = GraphWin("Legos!", 1200, 800)
    win.setCoords(-20,-20,20,20)

    #First Lego
    lego1Body1 = Rectangle(Point(-13,16), Point(-1,10))
    lego1Body1.setWidth(3)
    lego1Body1.setFill("#11187A")
    lego1Body1.draw(win)

    lego1Nub1 = Rectangle(Point(-11.5,16.5),Point(-10.5,16))
    lego1Nub1.setFill("#11187A")
    lego1Nub1.setWidth(3)
    lego1Nub1.draw(win)

    lego1Nub2=lego1Nub1.clone()
    lego1Nub2.move(2,0)
    lego1Nub2.draw(win)

    lego1Nub3=lego1Nub2.clone()
    lego1Nub3.move(2,0)
    lego1Nub3.draw(win)

    lego1Nub4=lego1Nub3.clone()
    lego1Nub4.move(2,0)
    lego1Nub4.draw(win)

    lego1Nub5=lego1Nub4.clone()
    lego1Nub5.move(2,0)
    lego1Nub5.draw(win)

# Lego number 2
    lego2Body = lego1Body1.clone()
    lego2Body.setFill("Forest Green")
    lego2Body.move(17,0)
    lego2Body.draw(win)

    lego2Nub1 = Rectangle(Point(14.5,16.5),Point(13.5,16))
    lego2Nub1.setFill("Forest Green")
    lego2Nub1.setWidth(3)
    lego2Nub1.draw(win)

    lego2Nub2=lego2Nub1.clone()
    lego2Nub2.move(-2,0)
    lego2Nub2.draw(win)

    lego2Nub3=lego2Nub2.clone()
    lego2Nub3.move(-2,0)
    lego2Nub3.draw(win)

    lego2Nub4=lego2Nub3.clone()
    lego2Nub4.move(-2,0)
    lego2Nub4.draw(win)

    lego2Nub5=lego2Nub4.clone()
    lego2Nub5.move(-2,0)
    lego2Nub5.draw(win)

# Lego number 3
    lego3Body = lego1Body1.clone()
    lego3Body.setFill("Yellow")
    lego3Body.move(0,-10)
    lego3Body.draw(win)

    lego3Nub1 = lego1Nub1.clone()
    lego3Nub1.move(-0,-10)
    lego3Nub1.setFill("Yellow")
    lego3Nub1.setWidth(3)
    lego3Nub1.draw(win)

    lego3Nub2=lego3Nub1.clone()
    lego3Nub2.move(2,0)
    lego3Nub2.draw(win)

    lego3Nub3=lego3Nub2.clone()
    lego3Nub3.move(2,0)
    lego3Nub3.draw(win)

    lego3Nub4=lego3Nub3.clone()
    lego3Nub4.move(2,0)
    lego3Nub4.draw(win)

    lego3Nub5=lego3Nub4.clone()
    lego3Nub5.move(2,0)
    lego3Nub5.draw(win)

# Lego number 4
    lego4Body = lego2Body.clone()
    lego4Body.setFill("Red")
    lego4Body.move(0,-10)
    lego4Body.draw(win)

    lego4Nub1 = lego2Nub1.clone()
    lego4Nub1.move(-0,-10)
    lego4Nub1.setFill("Red")
    lego4Nub1.setWidth(3)
    lego4Nub1.draw(win)

    lego4Nub2=lego4Nub1.clone()
    lego4Nub2.move(-2,0)
    lego4Nub2.draw(win)

    lego4Nub3=lego4Nub2.clone()
    lego4Nub3.move(-2,0)
    lego4Nub3.draw(win)

    lego4Nub4=lego4Nub3.clone()
    lego4Nub4.move(-2,0)
    lego4Nub4.draw(win)

    lego4Nub5=lego4Nub4.clone()
    lego4Nub5.move(-2,0)
    lego4Nub5.draw(win)

# Lego number 5
    lego5Body = lego3Body.clone()
    lego5Body.setFill("Sky Blue")
    lego5Body.move(0,-10)
    lego5Body.draw(win)

    lego5Nub1 = lego3Nub1.clone()
    lego5Nub1.move(0,-10)
    lego5Nub1.setFill("Sky Blue")
    lego5Nub1.setWidth(3)
    lego5Nub1.draw(win)

    lego5Nub2=lego5Nub1.clone()
    lego5Nub2.move(2,0)
    lego5Nub2.draw(win)

    lego5Nub3=lego5Nub2.clone()
    lego5Nub3.move(2,0)
    lego5Nub3.draw(win)

    lego5Nub4=lego5Nub3.clone()
    lego5Nub4.move(2,0)
    lego5Nub4.draw(win)

    lego5Nub5=lego5Nub4.clone()
    lego5Nub5.move(2,0)
    lego5Nub5.draw(win)

# Lego number 6
    lego6Body = lego4Body.clone()
    lego6Body.setFill("Black")
    lego6Body.move(0,-10)
    lego6Body.draw(win)

    lego6Nub1 = lego4Nub1.clone()
    lego6Nub1.move(-0,-10)
    lego6Nub1.setFill("Black")
    lego6Nub1.setWidth(3)
    lego6Nub1.draw(win)

    lego6Nub2=lego6Nub1.clone()
    lego6Nub2.move(-2,0)
    lego6Nub2.draw(win)

    lego6Nub3=lego6Nub2.clone()
    lego6Nub3.move(-2,0)
    lego6Nub3.draw(win)

    lego6Nub4=lego6Nub3.clone()
    lego6Nub4.move(-2,0)
    lego6Nub4.draw(win)

    lego6Nub5=lego6Nub4.clone()
    lego6Nub5.move(-2,0)
    lego6Nub5.draw(win)

    win.getMouse()
main()


from distutils.version import LooseVersion
from django import VERSION as DJ_VER
from cms import __version__ as CMS_VER

SITE_ID = 1
SECRET_KEY = 'fake-key'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    }
}

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

ROOT_URLCONF = 'test_urls'

if DJ_VER >= (1, 8):
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': ['djangocms_ical/tests'],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                    'sekizai.context_processors.sekizai'
                ],
            },
        },
    ]
else:
    TEMPLATE_DIRS = ['djangocms_ical/tests']
    TEMPLATE_CONTEXT_PROCESSORS = (
        'django.core.context_processors.request',
        'sekizai.context_processors.sekizai',
    )

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

tree = 'treebeard'

if LooseVersion(CMS_VER) < LooseVersion('3.1'):
    tree = 'mptt'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'djangocms_ical',
    'cms',
    'menus',
    tree,
    'sekizai',
)

LANGUAGE_CODE = 'en'
LANGUAGES = (('en', 'English'),)

CMS_TEMPLATES = (('bogus.html', 'Bogus'),)


# -*- coding: utf-8 -*-
import numpy as np
import csv
import logging

class MultiClassDataLoader(object):
    """
    Handles multi-class training data.  It takes predefined sets of "train_data_file" and "dev_data_file"
    of the following record format.
        <text>\t<class label>
      ex. "what a masterpiece!	Positive"

    Class labels are given as "class_data_file", which is a list of class labels.
    """
    def __init__(self, flags, data_processor):
        self.__flags = flags
        self.__data_processor = data_processor
        self.__train_data_file = None
        self.__dev_data_file = None
        self.__class_data_file = None
        self.__classes_cache = None

    # NOTE: Set the files path.
    def define_flags(self, train_data_file=None, dev_data_file=None, class_data_file=None):
        if train_data_file:
            self.__flags.DEFINE_string("train_data_file", str(train_data_file), "Data source for the training data.")
        else:
            logging.debug('train data file is not defined.')

        if dev_data_file:
            self.__flags.DEFINE_string("dev_data_file", str(dev_data_file), "Data source for the cross validation data.")
        else:
            logging.warning('verification data file is not defined.')

        if class_data_file:
            self.__flags.DEFINE_string("class_data_file", str(class_data_file), "Data source for the class list.")
        else:
            logging.debug('class category file is not defined.')

    # NOTE: Set the X, Y and dev_x, dev_y
    def prepare_data(self):
        """
        Get the corp of all input and returns the indexis of word ids.
        :return: list(word_ids_vector)
        """
        self.__resolve_params()
        x_train, y_train = self.__load_data_and_labels(self.__train_data_file)
        x_dev, y_dev = self.__load_data_and_labels(self.__dev_data_file)
        # Build vocabulary
        self.vocab_processor = self.__data_processor.vocab_processor(x_train, x_dev)
        # NOTE: self.vocab_processor.fit_transform(raw_documents)
        #       => Learn the vocabulary dictionary and return indexies of words.
        x_train = np.array(list(self.vocab_processor.fit_transform(x_train)))
        x_dev = np.array(list(self.vocab_processor.fit_transform(x_dev)))
        return [x_train, y_train, x_dev, y_dev]

    def restore_vocab_processor(self, vocab_path):
        return self.__data_processor.restore_vocab_processor(vocab_path)

    def class_labels(self, class_indexes):
        return [self.__classes()[idx] for idx in class_indexes]

    def class_count(self):
        return self.__classes().__len__()

    def load_dev_data_and_labels(self):
        self.__resolve_params()
        x_dev, y_dev = self.__load_data_and_labels(self.__dev_data_file)
        return [x_dev, y_dev]

    def load_data_and_labels(self):
        self.__resolve_params()
        x_train, y_train = self.__load_data_and_labels(self.__train_data_file)
        x_dev, y_dev = self.__load_data_and_labels(self.__dev_data_file)
        x_all = x_train + x_dev
        y_all = np.concatenate([y_train, y_dev], 0)
        return [x_all, y_all]

    # NOTE: Load data from files.
    def __load_data_and_labels(self, data_file):
        x_text = []
        y = []
        with open(data_file, 'r') as tsvin:
            classes = self.__classes()

            # NOTE: np.eye() is making an identity matrix
            one_hot_vectors = np.eye(len(classes), dtype=int)

            # NOTE: creating class vectors
            class_vectors = {}
            for i, cls in enumerate(classes):
                class_vectors[cls] = one_hot_vectors[i]
            # tsvin = csv.reader(tsvin, delimiter=',')
            for row in tsvin:
                temp = row.strip()
                temp = temp.split(',')
                data = self.__data_processor.clean_data(temp[0])
                x_text.append(data)
                y.append(class_vectors[temp[1]])
        return [x_text, np.array(y)]

    # NOTE: Read a class categories from .cls file.
    def __classes(self):
        self.__resolve_params()
        if self.__classes_cache is None:
            with open(self.__class_data_file, 'r') as catin:
                classes = list(catin.readlines())
                # NOTE: Python String.strip() => erase the empty spaces both of side.
                self.__classes_cache = [s.strip() for s in classes]
        return self.__classes_cache

    def __resolve_params(self):
        if self.__class_data_file is None:
            self.__train_data_file = self.__flags.FLAGS.train_data_file
            self.__dev_data_file = self.__flags.FLAGS.dev_data_file
            self.__class_data_file = self.__flags.FLAGS.class_data_file


# Lösung Thomas

import random

r = random.Random()

def wuerfeln():
    return r.randint(1, 6) # Augenzahl zwischen 1 und 6

def muenzwurf():
    return r.randint(0, 1)

# wuerfeln mit Augenzahl zwischen 1 und 6
d = {}
for i in range(100000):
    augenzahl = wuerfeln()
    if augenzahl in d:
        d[augenzahl] +=1
    else:
        d[augenzahl] = 1

print("Würfeln:  "+str(dict(sorted(d.items())))+'\n')

# würfeln mit Münzwurf

# Dual    Dez
# 0 0 0   0
# 1 0 0   1
# 0 1 0   2
# 1 1 0   3
# 0 0 1   4
# 1 0 1   5
# 0 1 1   6
# 1 1 1   7

def wuerfeln2():
    while True:
        i_dual = 1  # Zähler Dual-Code
        i_augenzahl = 0
        while i_dual <= 4:
            i_augenzahl += muenzwurf() * i_dual
            i_dual *= 2 # Zähler Dual-Code multipliziert mit 2 (1,2,4...)
        if 0 < i_augenzahl < 7:
            break
    return i_augenzahl

dict_mw ={}
i_schleife = 0  # Schleifenzaehler
while i_schleife < 100000:
    i_augenzahl = wuerfeln2()
    if i_augenzahl in dict_mw:
        dict_mw[i_augenzahl] += 1
    else:
        dict_mw[i_augenzahl] = 1
    i_schleife += 1

print("Münzwurf: "+str(dict(sorted(dict_mw.items()))))


#train test split

#script to split csv file into train test

import pandas as pd
import numpy as np
import os

#read csv
df = pd.read_csv(os.getcwd()+'/processed_JSON/retinanet_data_supervisely.csv')

#function to get x and y coordinates from image path
def get_x_y_coords(df):

	df['image'] = None
	df['x'] = None
	df['y'] = None
	for index, row in df.iterrows():

		#get image
		df['image'][index] = str(df['path'][index].split('/')[-1])

		#get x and y coordinates
		df['x'][index] = int(df['path'][index].split('/')[-1].split('_')[0].split('.')[0])
		df['y'][index] = int(df['path'][index].split('/')[-1].split('_')[-1].split('.')[0])

	return df

#function to create new unique dataframe that drops duplicates
def create_new_unqiue_df(df):

	#create new dataframe of only image
	new_df = pd.DataFrame()
	new_df['image'] = df['image'].astype(str)

	#drop duplicates from new_df
	new_df = new_df.drop_duplicates(subset=['image'], keep='first')

	#reindex dataframe
	new_df.index = [x for x in range(len(new_df))]

	return new_df

#create train and test
#split into train and test
def train_test_split(new_df, split):

	msk = np.random.rand(len(new_df)) < split
	train = new_df[msk]
	test = new_df[~msk]

	return train, test

#get x and y coordinates from image
df = get_x_y_coords(df)
df = df.sort_values(by=['x', 'y'])
print(df[['image', 'x', 'y', 'class_title']].head(10))

#create new df with image, x and y cols and drop duplicate columns in iamges
new_df = create_new_unqiue_df(df)

#split into training and test
train, test = train_test_split(new_df, 0.9)

#TRAINGIN SET

print('training set')
print(train.head())

#inner join with df
training_df = pd.merge(left=train,right=df, left_on='image', right_on='image')

#sort values
training_df = training_df.sort_values(by=['x', 'y'])
print('\n\ntraining set')
print(training_df[['image', 'x', 'y', 'class_title']].head(10))

#TESTING SET
#left join with df
print('\n\ntesting set')
print(test.head())

#inner join with df
testing_df = pd.merge(left=test,right=df, left_on='image', right_on='image')

#sort values
testing_df = testing_df.sort_values(by=['x', 'y'])

print('\n\ntest set')
print(testing_df[['image', 'x', 'y', 'class_title']].head(10))

#check
if(len(testing_df)+len(training_df)==len(df)):
	print("train test completed successfully")
else:
	raise "error occured"

#remove files if they exist
if os.path.exists(os.getcwd()+'/train_annotations.csv'):
	os.remove(os.getcwd()+'/train_annotations.csv')

if os.path.exists(os.getcwd()+'/test_annotations.csv'):
	os.remove(os.getcwd()+'/test_annotations.csv')

#drop cols created for this script
training_df = training_df.drop(['image', 'x', 'y'], axis = 1)
testing_df = testing_df.drop(['image', 'x', 'y'], axis = 1)

#length
print(len(testing_df))
print(len(training_df))

#write to csv
training_df.to_csv(os.getcwd()+'/train_annotations.csv', header=True, index=None, sep=',', mode='a')
testing_df.to_csv(os.getcwd()+'/test_annotations.csv', header=True, index=None, sep=',', mode='a')


import sys
import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
import _pickle as pickle
from sklearn.preprocessing import PolynomialFeatures

filename = sys.argv[1]
x = []
y = []
with open(filename, 'r') as f:
    for line in f.readlines():
        xt, yt = [float(i) for i in line.split(',')]
        x.append(xt)
        y.append(yt)
# loaded input data x and y, where x refers to data and y refers to labels

# we need a way to validate and check if the model is performing at a satisfactory level
# to do so, we need to seperate the data into 2 groups
# The training dataset will be used to build the model,
# and the testing dataset will be used to see how this trained model performs on unknown data.

# split data into testing and training datasets
num_training = int(0.8 * len(x))
num_test = len(x) - num_training

# training data
x_train = np.array(x[:num_training]).reshape((num_training, 1))
y_train = np.array(y[:num_training])

# test data
x_test = np.array(x[num_training:]).reshape((num_test, 1))
y_test = np.array(y[num_training:])

# create linear regression object
linear_regressor = linear_model.LinearRegression()

# train the model using the training sets
linear_regressor.fit(x_train, y_train)

# the fit method takes the input data and trains the model

y_train_pred = linear_regressor.predict(x_train)
plt.figure()
plt.scatter(x_train, y_train_pred, color='black', linewidth=4)
plt.title('Training Data')
plt.show()

y_test_pred = linear_regressor.predict(x_test)
plt.scatter(x_test, y_test, color='green')
plt.plot(x_test, y_test_pred, color='black', linewidth=4)
plt.title('Test Data')
plt.show()

output_model_file = 'saved_model.pkl'
with open(output_model_file, 'w') as f:
    pickle.dump(linear_regressor, f)
with open(output_model_file, 'r') as f:
    model_linregr = pickle.load(f)

y_test_pred_new = model_linregr.predict(x_test)
print("\nNew mean absolute error=", round(sm.mean_absolute_error(y_test, y_test_pred_new), 2))

ridge_regressor = linear_model.Ridge(alpha=0.01, fit_intercept=True, max_iter=10000)
# the alpha parameter controls the complexity
# As alpha gets closer to 0, the ridge regressor tends to become more like a linear regressor with ordinary least squares. So, if you want to make it robust against outliers, you need to as- sign a higher value to alpha. We considered a value of 0.01, which is moderate.
ridge_regressor.fit(x_train, y_train)
y_test_pred_ridge = ridge_regressor.predict(x_test)
print("Mean absolute error=", round(sm.mean_absolute_error(y_test, y_test_pred_ridge), 2))
print("Mean squared error=", round(sm.mean_squared_error(y_test, y_test_pred_ridge), 2))
print("Median absolute error=", round(sm.median_absolute_error(y_test, y_test_pred_ridge), 2))
print("Explained variance score=", round(sm.explained_variance_score(y_test, y_test_pred_ridge), 2))
print("R2 score=", round(sm.r2_score(y_test, y_test_pred_ridge), 2))

polynomial = PolynomialFeatures(degree=3)
x_train_transformed = polynomial.fit_transform(x_train)

datapoint = [0.39, 2.78, 7.11]
poly_datapoint = polynomial.fit_transform(datapoint)
poly_linear_model = linear_model.LinearRegression()
poly_linear_model.fit(x_train_transformed, y_train)
print("\nLinear regression=", linear_regressor.predict(datapoint)[0])


from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
import numpy
import sys
import os

libs = os.popen('ldconfig -p | grep libopencv')
libs = libs.readlines()
lib_paths = [l.split('=>')[-1].strip('\n').strip(' ') for l in libs]

# Find opencv libraries in lib_folder
cvlibs = list()
lib_folders = set()
for file in lib_paths:
    file_split = file.split('.')
    cvlibs.append(file_split[0])
    lib_folders.add(file.split('libopencv')[0])
lib_folders = list(lib_folders)
cvlibs = list(set(cvlibs))
cvlibs = ['-L{}'.format(lib_folder) for lib_folder in lib_folders] + \
         ['opencv_{}'.format(lib.split(os.path.sep)[-1].split('libopencv_')[-1]) for lib in cvlibs]

setup(
    cmdclass={'build_ext': build_ext},
    ext_modules=cythonize(Extension("opencv_mat",
                                    sources=["opencv_mat.pyx", "globalmatting.cpp", "guidedfilter.cpp"],
                                    language="c++",
                                    include_dirs=[numpy.get_include(),
                                                  os.path.join('/usr', 'include', 'opencv2'),
                                                 ],
                                    library_dirs=lib_folders,
                                    libraries=cvlibs,
                                    )
                          )
)


import torch
import numpy as np
from DDPG import DDPG
from utils import ReplayBuffer

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

class HAC:
    def __init__(self, k_level, H, state_dim, action_dim, render, endgoal_thresholds,
                 action_bounds, action_offset, state_bounds, state_offset, lr,
                 goal_dim, subgoal_bounds, subgoal_offset, subgoal_thresholds):

        # adding lowest level
        print("LOWEST LEVEL")
        self.HAC = [DDPG(state_dim, state_dim, action_dim, action_bounds, action_offset, lr, H)]
        print("FINISH BUILDING HAC for lowest level")
        self.replay_buffer = [ReplayBuffer()]

        # adding remaining levels
        for i in range(k_level - 1):
            print("building HAC for level-{}".format(i + 1))
            # self.HAC.append(DDPG(state_dim, action_dim, state_bounds, state_offset, lr, H))
            self.HAC.append(DDPG(state_dim, goal_dim, state_dim, subgoal_bounds, subgoal_offset, lr, H))
            print("finished HAC for level-{}".format(i + 1))
            self.replay_buffer.append(ReplayBuffer())

        # set some parameters
        self.k_level = k_level
        self.H = H
        self.action_dim = action_dim
        self.state_dim = state_dim
        self.goal_dim = goal_dim
        self.endgoal_thresholds = endgoal_thresholds
        self.subgoal_thresholds = subgoal_thresholds
        self.render = render

        # logging parameters
        self.goals = [None] * self.k_level
        self.reward = 0
        self.timestep = 0

    def set_parameters(self, lamda, gamma, action_clip_low, action_clip_high,
                       state_clip_low, state_clip_high, exploration_action_noise, exploration_state_noise,
                       subgoal_clip_low, subgoal_clip_high, exploration_subgoal_noise):

        self.lamda = lamda
        self.gamma = gamma
        self.action_clip_low = action_clip_low
        self.action_clip_high = action_clip_high
        self.state_clip_low = state_clip_low
        self.state_clip_high = state_clip_high
        self.exploration_action_noise = exploration_action_noise
        self.exploration_subgoal_noise = exploration_state_noise
        self.subgoal_clip_low = subgoal_clip_low
        self.subgoal_clip_high = subgoal_clip_high
        # self.exploration_subgoal_noise = exploration_subgoal_noise

    """
    """

    def check_goal(self, state, goal, threshold):
        # print("state shape {}".format(state.shape))
        # print("action shape {}".format(self.action_dim))
        # print("goal shape {}".format(goal.shape))
        # print("threshold shape {}".format(threshold.shape))
        # state here is only checking the achieved goal, in PickAndPlace is indexed [3:6]
        obj_state = state[3:6]
        for i in range(self.goal_dim):
            if abs(obj_state[i] - goal[i]) > threshold[i]:
                return False
        return True

    """
    for level>0
    """

    def check_goal_up(self, state, goal, threshold):
        # state here is only checking the subgoal and goal
        # print("====================   LEVEL-{}  ===========================".format(self.k_level-1))
        # print("UPPER LVL state shape {}".format(state.shape))
        # print("UPPER LVL action shape {}".format(self.action_dim))
        # print("UPPER LVL goal shape {}".format(goal.shape))
        # print("UPPER LVL threshold shape {}".format(threshold.shape))
        for i in range(self.goal_dim):
            if abs(state[i] - goal[i]) > threshold[i]:
                return False
        return True

    """
    """

    def run_HAC(self, env, i_level, state, goal, subgoal_test):
        next_state = None
        done = None
        goal_transitions = []

        # logging updates
        self.goals[i_level] = goal
        print("<<<<<<<<<<<<<<<<<<<<<<<<<< GOALS in run_HAC {}".format(self.goals))

        # H attempts
        for _ in range(self.H):
            next_subgoal_test = subgoal_test

            action = self.HAC[i_level].select_action(state, goal)
            # print("ACTION in run HAC for level-{} >>>>> {}".format(i_level, action))
            #
            #   <================ high level policy ================>
            if i_level > 0:
                # print("action from nn for level-1 >>>>> {}".format(action))
                # action should be subgoal
                # add noise or take random action if not subgoal testing
                if not subgoal_test:
                    if np.random.random_sample() > 0.2:
                        action = action + np.random.normal(0, self.exploration_subgoal_noise)
                        action = action.clip(self.subgoal_clip_low, self.subgoal_clip_high)
                    else:
                        action = np.random.uniform(self.subgoal_clip_low, self.subgoal_clip_high)

                # Determine whether to test subgoal (action)
                if np.random.random_sample() < self.lamda:
                    next_subgoal_test = True

                # Pass subgoal to lower level
                # subgoal is action
                next_state, done = self.run_HAC(env, i_level - 1, state, action, next_subgoal_test)

                # only take achieved goal
                achieved_goal = next_state

                # if subgoal was tested but not achieved, add subgoal testing transition
                if next_subgoal_test and not self.check_goal_up(action, achieved_goal, self.endgoal_thresholds):
                    # print("ACTION inserted to replay buffer @ 129 {} at level-{}".format(action.shape, i_level))
                    self.replay_buffer[i_level].add((state, action, -self.H, next_state, goal, 0.0, float(done)))

                # for hindsight action transition
                # TODO:
                action = achieved_goal
                # print("HINDSIGHT ACTION action shape {}".format(action.shape))

            #   <================ low level policy ================>
            else:
                # add noise or take random action if not subgoal testing
                if not subgoal_test:
                    if np.random.random_sample() > 0.2:
                        action = action + np.random.normal(0, self.exploration_action_noise)
                        action = action.clip(self.action_clip_low, self.action_clip_high)
                    else:
                        print("======================== SUBGOAL TESTING ============================")
                        action = np.random.uniform(self.action_clip_low, self.action_clip_high)

                # take primitive action
                next_state, rew, done, _ = env.step(action)
                # print("=============================================")
                # print("Observation {}".format(np.around(next_state, decimals=3)))

                if self.render:
                    env.render()

                    if self.k_level == 2:
                        env.render_goal(self.goals[0], self.goals[1])
                    elif self.k_level == 3:
                        env.render_goal_2(self.goals[0], self.goals[1], self.goals[2])

                    for _ in range(1000000):
                        continue

                # this is for logging
                self.reward += rew
                self.timestep += 1

            # check if goal is achieved
            goal_achieved = self.check_goal(next_state, goal, self.endgoal_thresholds)

            # hindsight action transition
            if goal_achieved:
                # print("ACTION inserted to replay buffer @ 172 {} at level-{}".format(action.shape, i_level))
                self.replay_buffer[i_level].add((state, action, 0.0, next_state, goal, 0.0, float(done)))
            else:
                # print("ACTION inserted to replay buffer @ 175 {} at level-{}".format(action.shape, i_level))
                self.replay_buffer[i_level].add((state, action, -1.0, next_state, goal, self.gamma, float(done)))

            # copy for goal transition
            goal_transitions.append([state, action, -1.0, next_state, None, self.gamma, float(done)])

            state = next_state

            if done or goal_achieved:
                break

        # hindsight goal transition
        # last transition reward and discount is 0
        goal_transitions[-1][2] = 0.0
        goal_transitions[-1][5] = 0.0
        for transition in goal_transitions:
            # last state is goal for all transitions
            if i_level > 0:
                transition[4] = next_state[3:6]
            else:
                transition[4] = next_state
            # print("ACTION inserted to replay buffer @ 196 {} at level-{}".format(transition[1].shape, i_level))
            self.replay_buffer[i_level].add(tuple(transition))

        return next_state, done

    """
    """

    def update(self, n_iter, batch_size):
        for i in range(self.k_level):
            self.HAC[i].update(self.replay_buffer[i], n_iter, batch_size)

    """
    """

    def save(self, directory, name):
        for i in range(self.k_level):
            self.HAC[i].save(directory, name + '_level_{}'.format(i))

    """
    """

    def load(self, directory, name):
        for i in range(self.k_level):
            self.HAC[i].load(directory, name + '_level_{}'.format(i))


from websocket.utilities import get_server_id

MAIN_SERVER_WEBSOCKET_URL = 'wss://lightningpowerusers.com:8765'

CHANNEL_OPENING_SERVER_WEBSOCKET_URL = 'ws://localhost:8710'

PUBKEY_LENGTH = 66

INVOICES_SERVER_ID = get_server_id('invoices')
CHANNELS_SERVER_ID = get_server_id('channels')


import numpy as np
from numba import njit, prange

def train(doc_word_mat, n_topics, max_iter=100):
    """
    wrapper function for training a corpus using pLSA. To save computation time, training
    uses maximum number of iterations as the stopping criterion.
    Args:
        doc_word_mat: a document word matrix stored as a sparse matrix
        n_topics: number of topics to be trained
        max_iter: Maximum number of iteration before the EM algorithm stops
    Returns:
        likelihood function value when the training stops
        word_given_topic matrix
        topic_given_word matrix
    """
    doc = doc_word_mat.row
    word = doc_word_mat.col
    freq = doc_word_mat.data

    # Random initialization
    word_given_topic = np.random.rand(doc_word_mat.shape[1], n_topics)
    topic_given_doc = np.random.rand(n_topics, doc_word_mat.shape[0])

    return plsa_sparse(doc, word, freq, word_given_topic, topic_given_doc, max_iter)

@njit(parallel=True)
def plsa_sparse(doc, word, freq, word_given_topic, topic_given_doc, max_iter):
    """
    runs the EM algorithm for pLSA.
    Args:
        doc: row indices of the document word matrix
        word: col indices of the document word matrix
        freq: entries of the document word matrix
        word_given_topic: initialized word_given_topic matrix
        topic_given_doc: initialized topic_given_doc matrix
        max_iter: the maximum number of iterations before the EM algorithm stops
    Returns:
        word_given_topic matrix
        topic_given_word matrix
    """
    n_topics, n_docs = topic_given_doc.shape
    n_words = word_given_topic.shape[0]
    nnz = freq.size

    # Auxiliary variables used in the loop
    topic_given_doc_word = np.zeros((n_topics, nnz))
    nnz_sum = np.zeros((nnz))
    topic_sum = np.zeros((n_topics))
    doc_sum = np.zeros((n_docs))

    # Compute document probabilities
    doc_prob = np.zeros((n_docs))

    for i in range(nnz):
        doc_prob[doc[i]] += freq[i]
    doc_prob = doc_prob / np.sum(doc_prob)

    iter = 0
    while True:
        # Expectation step
        nnz_sum[:] = 0
        for i in prange(nnz):
            for t in range(n_topics):
                topic_given_doc_word[t, i] = topic_given_doc[t, doc[i]] * word_given_topic[word[i], t]
                nnz_sum[i] += topic_given_doc_word[t, i]

        # Normalize topic_given_doc_word
        for t in range(n_topics):
            for i in range(nnz):
                topic_given_doc_word[t, i] /= 1e-20 + nnz_sum[i]

        # Maximization step
        word_given_topic[:] = 0
        topic_sum[:] = 0
        topic_given_doc[:] = 0
        doc_sum[:] = 0
        for t in prange(n_topics):
            for i in range(nnz):
                update = freq[i] * topic_given_doc_word[t, i]
                word_given_topic[word[i], t] += update
                topic_sum[t] += update
                topic_given_doc[t, doc[i]] += update
                doc_sum[doc[i]] += update

        # Normalize word_given_topic
        for w in range(n_words):
            for t in range(n_topics):
                word_given_topic[w, t] /= 1e-20 + topic_sum[t]

        # Normalize topic_given_doc
        for t in range(n_topics):
            for d in range(n_docs):
                topic_given_doc[t, d] /= 1e-20 + doc_sum[d]

        if iter == max_iter:
            break
        iter += 1

    likelihood = 0
    for i in range(nnz):
        update = 0
        for t in range(n_topics):
            update += word_given_topic[word[i], t] * topic_given_doc[t, doc[i]]
        likelihood += freq[i] * update * doc_prob[doc[i]]

    return likelihood, word_given_topic, topic_given_doc


from django.urls import path, re_path
from . import views

urlpatterns = [
	path('', views.index, name = 'index'),
	path('timeline-<int:year>/', views.timeline, name = 'timeline'),
	path('month-detail-<int:year>-<int:month>', views.timeline_detail, name = 'timeline_detail'),
	path('category-detail-<str:target>/', views.category_detail, name = 'category_detail'),
	path('category-<str:target>/', views.category, name = 'category'),
	re_path(r'(?P<Head>.+)/', views.detail, name = 'detail'),
]


# dar inja hame chiz mohaseb mishe  20 ta 20 ta joda mikone
from numpy import array
from numpy import hstack

import csv
import math
list0=[]

list=["209223000","211409100","211822000","211881000","212048000","215058000","218019000","218779000","219008000","219010252","219082000",
      "220018000","220289000","220552000","220609000","220614000","228330600","236216000","240244000","240891000","244013009","244267000",
      "244678000","244790522","246397000","246694000","247604000","248095000","248295000","248602000","255623000","255806146","256122000",
      "256757000","257519000","257742000","258764000","259360000","259888000","259890000","259936000","265581970","265724260","265734000",
      "265882000","266209000","266212000","266225000","266235000","273337270","311601000","311814000","419001223","636016188"]
list1=["10","2","26","19","5","19","16","25","33","48","39","7","20","43","20","5","5","1","1","12","7","6","30","1","2","42","6",
       "32","19","30","26","10","12","36","3","37","10","36","15","1","22","5","8","34","14","24","32","2","15","2","2","1","22","37"]
temp="# Timestamp,MMSI,season,day_time,Latitude,Longitude,rain,htsgwsfc_2,perpwsfc_2,precsno_3,qlml_3,speedmax_3,taugwx_3,taugwy_3,tlml_3,ulml_3,vlml_3,dirpwsfc_4,tcc_5,u_6,v_6,hic_7,ui_7,vi_7,rain-month_8,wsp_9,wind_stress_9,wsp_err_9,albedo_10,albvisdf_10,Width,Length,Gross Tonnage,Summer Deadweight,Draught,Navigational status,SOG,COG,Heading,Ship type,di,Actual time"

def extract(mmis):
    list0.clear()
    with open("C:/data set/danmark/16.csv") as f2:
        for line3 in f2:
            if mmis in line3:
                list0.append(line3)

def cordinate():

    tmp=list0[0]
    tmp1=tmp.split(',')
    lat1=tmp1[4]
    lon1=tmp1[5]
    v=len(list0)-1
    tmp2 = list0[v]
    tmp3=tmp2.split(',')
    lat2 = tmp3[4]
    lon2 = tmp3[5]
    valu=distance (lat1, lon1, lat2, lon2)
    return valu,lat1, lon1, lat2, lon2
#####################################################
def distance (lat1, long1, lat2, long2):
    R = 6373.0
    lat1=float(lat1)
    long1=float(long1)
    lat2 = float(lat2)
    long2 = float(long2)
    lat1 = math.radians(lat1)
    lon1 = math.radians(long1)
    lat2 = math.radians(lat2)
    lon2 = math.radians(long2)

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    distance = R * c
    distance=float("{:.5f}".format(distance))
    print(distance)

    return distance

def printme( mmis):
    count=-1
    #field = []
    rain = 0
    htsgwsfc_2 = 0
    perpwsfc_2 = 0
    precsno = 0
    qlml = 0
    speedmax = 0
    taugwx = 0
    taugwy = 0
    tlml = 0
    ulml = 0
    vlml = 0
    dirpwsfc = 0
    tcc = 0
    u = 0
    v = 0
    hic = 0
    ui = 0
    vi = 0
    rain_month = 0
    wsp = 0
    wind_stress = 0
    wsp_err = 0
    albedo = 0
    albvisdf = 0
    SOG=COG=Heading=di=0
    kardam=0

    for line2 in list0:
        kardam=0
        #field.clear()
        field = line2.split(",")
        rain = rain + float(field[6])
        htsgwsfc_2 = htsgwsfc_2 + float(field[7])
        perpwsfc_2 = perpwsfc_2 + float(field[8])
        precsno = precsno + float(field[9])
        qlml = qlml + float(field[10])
        speedmax = speedmax + float(field[11])
        taugwx = taugwx + float(field[12])
        taugwy = taugwy + float(field[13])
        tlml = tlml + float(field[14])
        ulml = ulml + float(field[15])
        vlml = vlml + float(field[16])
        dirpwsfc = dirpwsfc + float(field[17])
        tcc = tcc + float(field[18])
        u = u + float(field[19])
        v = v + float(field[20])
        hic = hic + float(field[21])
        ui = ui + float(field[22])
        vi = vi + float(field[23])
        rain_month = rain_month + float(field[24])
        wsp = wsp + float(field[25])
        wind_stress = wind_stress + float(field[26])
        wsp_err = wsp_err + float(field[27])
        albedo = albedo + float(field[28])
        albvisdf = albvisdf + float(field[29])
        SOG = SOG + float(field[36])
        COG = COG + float(field[37])
        di = di + float(field[40])
        # ##################################################add to the field:

    with open("C:/data set/danmark/16.csv") as f2:
        for line3 in f2:
            if mmis in line3:
                field = line3.split(",")
                line1 = temp.split(",")
                field = line2.split(",")
                val, lat1, lon1, lat2, lon2=cordinate()
                tol = len(list0) - 1
                x = field[0]
                x = x + ',' + lat2
                x = x + ',' + lon2

                x=x+','+field[1]
                x = x + ',' + field[2]
                x = x + ',' + field[3]
                x = x + ',' + field[4]
                x = x + ',' + field[5]
                x= x+ ','+ str(rain / tol)
                x= x+ ','+ str(htsgwsfc_2 / tol)
                x= x+ ','+ str(perpwsfc_2 / tol)
                x= x+ ','+ str(precsno / tol)
                x= x+ ','+ str(qlml / tol)
                x= x+ ','+ str(speedmax / tol)
                x= x+ ','+ str(taugwx / tol)
                x = x + ',' + str(taugwy / tol)
                x= x+ ','+ str(tlml / tol)
                x= x+ ','+ str(ulml / tol)
                x=x+ ','+ str(vlml / tol)
                x=x+ ','+ str(dirpwsfc / tol)
                x=x+ ','+ str(tcc / tol)
                x=x+ ','+ str(u / tol)
                x=x+ ','+ str(v / tol)
                x=x+ ','+ str(hic / tol)
                x = x + ',' + str(ui / tol)
                x=x+ ','+ str(vi / tol)
                x=x+ ','+ str(rain_month / tol)
                x=x+ ','+ str(wsp / tol)
                x=x+ ','+ str(wind_stress / tol)
                x = x + ',' + str(wsp_err / tol)
                x=x+ ','+ str(albedo / tol)
                x = x + ',' + str(albvisdf / tol)
                x = x + ',' + field[30]
                x=x+ ','+ field[31]
                x=x+ ','+ field[32]
                x = x + ',' + field[33]
                x = x + ',' + field[34]
                x=x+ ','+ field[35]
                x=x+ ','+ str(SOG / tol)
                x = x + ',' + str(COG / tol)
                x = x + ',' + field[38]
                x=x+ ','+ field[39]
                x = x + ',' + str(val)
                x = x + ',' + field[41]

                #####################################
        # add = 'C:/data set/danmark/' + mmis + '.txt'
                add = 'C:/data set/danmark/' + "asad1" + '.txt'
                with open(add, "a") as file_object:
                    cont = 0
                    mak = ",".join(map(str, line1))
                    mak = mak + "\n"
                    file_object.write(x)
                    lock = 1
                    rain = 0
                    htsgwsfc_2 = 0
                    perpwsfc_2 = 0
                    precsno = 0
                    qlml = 0
                    speedmax = 0
                    taugwx = 0
                    taugwy = 0
                    tlml = 0
                    ulml = 0
                    vlml = 0
                    dirpwsfc = 0
                    tcc = 0
                    u = 0
                    v = 0
                    hic = 0
                    ui = 0
                    vi = 0
                    rain_month = 0
                    wsp = 0
                    wind_stress = 0
                    wsp_err = 0
                    albedo = 0
                    albvisdf = 0
                    SOG = 0
                    COG = 0
                    Heading = 0
                    di = 0
                    if lock == 1:
                        break
    return;

for mmis in list:
    extract(mmis)
    printme(mmis)


"""Implementation of the types in Python 3's typing.py."""

from pytype import abstract
from pytype.pytd import pytd
from pytype.pytd import utils as pytd_utils

class TypingOverlay(abstract.Module):
  """A representation of the 'typing' module that allows custom overlays."""

  is_lazy = True  # uses _convert_member

  def __init__(self, vm, node, real_module):
    super(TypingOverlay, self).__init__(vm, node, "typing", typing_overload)
    self.real_module = real_module

  def _convert_member(self, name, m):
    return m(name, self.vm, self.vm.root_cfg_node).to_variable(
        self.vm.root_cfg_node, name)

  def get_attribute(self, node, name, valself=None, valcls=None):
    if name in typing_overload:
      return super(TypingOverlay, self).get_attribute(
          node, name, valself, valcls)
    else:
      return self.real_module.get_attribute(
          node, name, valself, valcls)

class Union(abstract.ValueWithSlots):
  """Implementation of typing.Union[...]."""

  def __init__(self, name, vm, node, elements=()):
    super(Union, self).__init__(vm.convert.type_type, vm, node)
    self.name = "Union"
    self.elements = elements
    self.set_slot("__getitem__", self.getitem_slot)

  def __str__(self):
    return "Union[" + ", ".join(str(n) for n in self.elements) + "]"

  def getitem_slot(self, node, slice_var):
    slice_tuple = abstract.get_atomic_python_constant(slice_var)
    values = tuple(s.Data(node)[0] for s in slice_tuple)
    new_union = Union(self.name, self.vm, node, self.elements + values)
    return node, new_union.to_variable(node, "Union")

  def instantiate(self, node):
    n = self.vm.program.NewVariable(self.name)
    for e in self.elements:
      instance = e.instantiate(node)
      n.PasteVariable(instance, node)
    return n

  def match_var_against(self, var, subst, node, view):
    for element in self.elements:
      new_subst = abstract.match_var_against_type(
          var, element, subst, node, view)
      if new_subst is not None:
        return new_subst

  def get_instance_type(self, node, instance=None, seen=None):
    return pytd.UnionType(tuple(
        e.get_instance_type(node, seen=seen)
        for e in self.elements))

class _Container(abstract.ValueWithSlots):
  """Implementation of typing.X[...]."""

  def __init__(self, name, vm, node, inner=None):
    # TODO(kramm): type_type is wrong. Correct would be "typing.GenericMeta".
    # But in the output, we'd want this to become an alias.
    super(_Container, self).__init__(vm.convert.type_type, vm, node)
    self.name = name
    self.inner = inner
    self.set_slot("__getitem__", self.getitem_slot)

  def getitem_slot(self, node, inner):
    new_list = self.__class__(self.name, self.vm, node, inner)
    return node, new_list.to_variable(node, self.name)

  def instantiate(self, node):
    if self.inner:
      return self.vm.convert.build_list(node, [
          self.vm.instantiate(self.inner, node)])
    else:
      return self.vm.convert.build_list(node, [
          self.vm.convert.create_new_unknown(node, "inner")])

  def match_var_against(self, var, subst, node, view):
    new_subst = None
    for cls in [c for clsv in self.concrete_classes
                for c in clsv.data]:
      new_subst = abstract.match_var_against_type(var, cls, subst, node, view)
      if new_subst is not None:
        subst = new_subst
        break
    else:
      return None
    if self.inner:
      v = view[var].data
      # __builtins__.pytd always uses T as type parameter for sequence classes.
      if (isinstance(v, abstract.SimpleAbstractValue) and
          "T" in v.type_parameters):
        inner = v.type_parameters["T"]
        for formal in self.inner.data:
          new_subst = abstract.match_var_against_type(
              inner, formal, subst, node, view)
          if new_subst is not None:
            return new_subst
      elif isinstance(v, (abstract.Unknown, abstract.Unsolvable)):
        return subst
    else:
      return subst

  def get_instance_type(self, node, instance=None, seen=None):
    if self.inner:
      t = pytd_utils.JoinTypes([i.get_instance_type(node, seen=seen)
                                for i in self.inner.data])
      return pytd.GenericType(pytd.NamedType(self.pytd_name), (t,))
    else:
      return pytd.NamedType(self.pytd_name)

  def __str__(self):
    if self.inner:
      return self.name + "[" + str(self.inner.data[0]) + "]"
    else:
      return self.name

class List(_Container):
  pytd_name = "__builtin__.list"

  def __init__(self, name, vm, node, inner=None):
    super(List, self).__init__("List", vm, node, inner)
    self.concrete_classes = [self.vm.convert.list_type]

class Sequence(_Container):
  pytd_name = "typing.Sequence"

  def __init__(self, name, vm, node, inner=None):
    super(Sequence, self).__init__("Sequence", vm, node, inner)
    self.concrete_classes = [self.vm.convert.list_type,
                             self.vm.convert.tuple_type]

def build_any(name, vm, node, inner=None):
  del name
  del node
  del inner
  return abstract.Unknown(vm)

typing_overload = {
    "Union": Union,
    "List": List,
    "Sequence": Sequence,
    "Any": build_any,
}


def findHam():
    import cv2
    import numpy as np

    # read
    img_frontground = cv2.imread("my_picture.jpg", 1)
    img_background1 = cv2.imread("./pic4findHam/img_background1.jpeg", 1)
    img_background2 = cv2.imread("./pic4findHam/img_background2.jpeg", 1)
    img_background3 = cv2.imread("./pic4findHam/img_background3.jpeg", 1)
    img_background4 = cv2.imread("./pic4findHam/img_background4.jpeg", 1)
    img_background5 = cv2.imread("./pic4findHam/img_background5.jpeg", 1)

    #copy origin frontground for output
    img_origin = img_frontground.copy()

    # gray
    img_frontground_gray = cv2.cvtColor(img_frontground, cv2.COLOR_BGR2GRAY)
    img_background1_gray = cv2.cvtColor(img_background1, cv2.COLOR_BGR2GRAY)
    img_background2_gray = cv2.cvtColor(img_background2, cv2.COLOR_BGR2GRAY)
    img_background3_gray = cv2.cvtColor(img_background3, cv2.COLOR_BGR2GRAY)
    img_background4_gray = cv2.cvtColor(img_background4, cv2.COLOR_BGR2GRAY)
    img_background5_gray = cv2.cvtColor(img_background5, cv2.COLOR_BGR2GRAY)

    # find difference
    fgbg = cv2.createBackgroundSubtractorMOG2()
    fgmask = fgbg.apply(img_background5_gray)
    fgmask = fgbg.apply(img_background4_gray)
    fgmask = fgbg.apply(img_background3_gray)
    fgmask = fgbg.apply(img_background2_gray)
    fgmask = fgbg.apply(img_background1_gray)
    fgmask = fgbg.apply(img_frontground_gray)

    #erode & dilate
    ret, fgmask_bin = cv2.threshold(fgmask,126,255,cv2.THRESH_BINARY)
    kernel = np.ones((3,3),np.uint8)
    erosion = cv2.erode(fgmask_bin, kernel, iterations=1)
    dilation = cv2.dilate(erosion, kernel, iterations=10)

    #find contour
    image, contours, hierarchy = cv2.findContours(dilation, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    #img = cv2.drawContours(img_frontground, contours, -1, (0,255,0),1)

    # find max contour
    max_area=1
    max_area_contour=0
    for i, cnt in enumerate(contours):
        temp = cv2.contourArea(cnt)
        if temp>max_area:
            max_area=temp
            max_area_contour=i

    #draw HamPosi
    if max_area_contour==0:
        HamPosi=[[1,1]]
    else:
        HamPosi = sum(contours[max_area_contour])/len(contours[max_area_contour])
#    img = cv2.drawContours(img_frontground, contours, max_area_contour, (0,255,0),1)
    img_findHam=cv2.circle(img_frontground, (int(HamPosi[0][0]),int(HamPosi[0][1])), 5, (0,255,0), thickness=-1)

    # display
    #cv2.imshow('frame',fgmask)
    #cv2.imshow('frame',fgmask_bin)
    #cv2.imshow('frame',erosion)
#    cv2.imshow('frame',img_findHam)

    # output
    bg_diff_path  = './findHam.jpg'
    cv2.imwrite(bg_diff_path,img_findHam)

    cv2.imwrite('./pic4findHam/img_background5.jpeg',img_background4)
    cv2.imwrite('./pic4findHam/img_background4.jpeg',img_background3)
    cv2.imwrite('./pic4findHam/img_background3.jpeg',img_background2)
    cv2.imwrite('./pic4findHam/img_background2.jpeg',img_background1)
    cv2.imwrite('./pic4findHam/img_background1.jpeg',img_origin)

#    cv2.waitKey(0)
#    cv2.destroyAllWindows()
#findHam()


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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('main', '0003_tamgas_imagebrowse'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='tamgas',
            options={'verbose_name': '\u0442\u0430\u043c\u0433\u0430-'},
        ),
        migrations.RemoveField(
            model_name='tamgas',
            name='imagebrowse',
        ),
    ]


import pytest
try:
    from unittest import mock
except ImportError:
    import mock

from genericclient_base import BaseGenericClient, BaseEndpoint, BaseResource, ParsedResponse

def test_host():
    client = BaseGenericClient(url='http://dummy.org')
    assert client.host == 'dummy.org'

    client = BaseGenericClient(url='http://dummy.org:8000')
    assert client.host == 'dummy.org:8000'

    client = BaseGenericClient(url='http://dummy.org:8000/api')
    assert client.host == 'dummy.org:8000'

    client = BaseGenericClient(url='http://dummy.org/api')
    assert client.host == 'dummy.org'

def test_protocol(monkeypatch, client, response):
    with pytest.raises(NotImplementedError):
        client.hydrate_data(response)

    with pytest.raises(NotImplementedError):
        client.make_session()

    monkeypatch.setattr(client, 'make_session', mock.MagicMock())

    session = client.session
    assert session is not None

def test_attribute(client):
    endpoint = client.users
    assert isinstance(endpoint, BaseEndpoint)

def test_item(client):
    endpoint = client["active-users"]
    assert isinstance(endpoint, BaseEndpoint)

def test_endpoints(monkeypatch, mock):
    class PostResource(BaseResource):
        pk_name = 'slug'

    class PostEndpoint(BaseEndpoint):
        resource_class = PostResource

    response = ParsedResponse(
        status_code=200,
        data={
            'id': 1,
            'slug': 'cats-demand-longer-breaks',
            'headline': 'Cats Demand Longer Breaks, Cleaner Litter, Slower Mice',
        },
    )

    class Client(BaseGenericClient):
        endpoint_classes = {
            'posts': PostEndpoint,
        }

    client = Client(url='http://dummy.org')

    monkeypatch.setattr(client.endpoint_class, 'request', mock.MagicMock(return_value=response))

    post = client.posts.get(slug='cats-demand-longer-breaks')
    assert post.__class__ is PostResource

    post = client["posts"].get(slug='cats-demand-longer-breaks')
    assert post.__class__ is PostResource

    monkeypatch.setattr(client.endpoint_class, 'request', mock.MagicMock())

    post.save()

    client.posts.request.assert_called_with(
        'put', 'http://dummy.org/posts/cats-demand-longer-breaks', json={
            'id': 1,
            'slug': 'cats-demand-longer-breaks',
            'headline': 'Cats Demand Longer Breaks, Cleaner Litter, Slower Mice',
        }
    )


#!/usr/bin/env python

class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        # pos1: the biggest index of nums[pos1] < nums[pos1+1]
        # pos2: the lasted item of nums[pos1] < nums[pos2]
        pos1 = -1
        for i in range(len(nums)-1, 0, -1):
            if nums[i-1] < nums[i]:
                pos1 = i-1
                break

        if pos1 == -1:
            nums.sort()
            return

        pos2 = pos1+1
        for j in range(len(nums)-1, pos1, -1):
            if nums[j] > nums[pos1]:
                pos2 = j
                break

        # swap nums[pos1], nums[pos2] and then reverse nums[pos1+1:]
        nums[pos1], nums[pos2] = nums[pos2], nums[pos1]
        i = pos1 + 1
        j = len(nums) - 1
        while i < j:
            nums[i], nums[j] = nums[j], nums[i]
            i += 1
            j -= 1

        return nums

def s(n):
    print(str(n) + ' -> ' + str(Solution().nextPermutation(n)))

s([])
s([1])
s([1,1])
s([1,2])
s([2,1])
s([1,2,3])
s([1,3,2])
s([2,1,3])
s([2,3,1])
s([3,1,2])
s([3,2,1])
s([5,1,1])
s([38,2,6,5,3,2])


"""
Created by hwyncho on 2017. 08. 24.
Last updated by hwyncho on 2018. 06. 16.
Copyright © 2018 hwyncho. All rights reserved.
==================================================
Convolutional Neural Network.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os

import numpy as np
import tensorflow as tf
from PIL import Image

from dataset import Dataset

np.random.seed(777)
tf.set_random_seed(777)

class Cnn:
    def __init__(self):
        self.__load_dataset = False
        self.__load_model = False

        self._dataset = None
        self._model = None

        self._image_width = 48
        self._image_height = 48
        self._image_mode = 'RGB'

        self._dataset_size = 0
        self._image_size = 0
        self._label_size = 0

        self._device = 'cpu'
        self._epoch = 100
        self._batch_size = 100

    def __del__(self):
        del self.__load_dataset
        del self.__load_model

        del self._dataset
        del self._model

        del self._image_width
        del self._image_height
        del self._image_mode

        del self._dataset_size
        del self._image_size
        del self._label_size

        del self._device
        del self._epoch
        del self._batch_size

    def load_dataset(self, path):
        """
        Load the dataset from a specific path.

        :param dataset_path: str
            The path to the dataset to load.
        """
        if not isinstance(path, str):
            raise TypeError("type of 'path' must be str.")

        if not os.path.exists(path):
            raise FileNotFoundError()

        self.__load_dataset = True
        self._dataset = Dataset(path, one_hot=True)
        self._dataset_size = self._dataset.num_examples
        self._image_size = self._dataset.image_size
        self._label_size = self._dataset.label_size

    def load_model(self, path, sess):
        """
        Load the learned model from a specific path.

        :param model_path: str
            The path to the model to load.
        """
        if not isinstance(path, str):
            raise TypeError("type of 'path' must be str.")

        par_dir = os.path.split(path)[0]
        meta_file = '{}.meta'.format(path)
        ckpt_file = '{}/checkpoint'.format(par_dir)

        if not os.path.exists(meta_file):
            raise FileNotFoundError(
                'The meta file is not exist from: {}'.format(par_dir))

        if not os.path.exists(ckpt_file):
            raise FileNotFoundError(
                'The checkpoint file is not exist from: {}'.format(par_dir))

        self.__load_model = True
        self._model = tf.train.import_meta_graph(meta_file, clear_devices=True)
        self._model.restore(sess, tf.train.latest_checkpoint(par_dir))

    def set_device(self, device='cpu'):
        """
        Set up the device with CPU or GPU.

        :param device: str
            'cpu' or 'gpu'
        """
        if device in ['cpu', 'gpu']:
            self._device = device
        else:
            raise ValueError("'device' must be in 'cpu' or 'gpu'.")

    def set_epoch(self, epoch=100):
        """
        Set size of epoch.

        :param epoch: int
            size of epoch
        """
        self._epoch = epoch

    def set_batch_size(self, batch_size=100):
        """
        Set size of batch.

        :param batch_size: int
            size of batch
        """
        self._batch_size = batch_size

    def train(self, dataset_path, model_path='./Models/model'):
        """
        Train Neural-Networks and save model.

        :param dataset_path: str
            the path to load the dataset for train.
        :param model_path: str
            the path to save the learning model.
        """
        if not isinstance(dataset_path, str):
            raise TypeError("type of 'dataset_path' must be str.")

        if not isinstance(model_path, str):
            raise TypeError("type of 'model_path' must be str.")

        self.load_dataset(dataset_path)

        image_width = 48
        image_height = 48
        if self._image_mode == 'RGB':
            image_channel = 3
        elif self._image_mode == 'L':
            image_channel = 1

        learning_rate = 0.001

        with tf.device('/{}:0'.format(self._device)):
            # Input
            with tf.variable_scope('Input_Layer') as scope:
                x = tf.placeholder(tf.float32, shape=[
                                   None, self._image_size], name='images')
                y = tf.placeholder(tf.float32, shape=[
                                   None, self._label_size], name='labels')

            x_2d = tf.reshape(
                x, [-1, image_height, image_width, image_channel], name='images_2d')

            # Hidden Layers
            with tf.variable_scope('Hidden_Layers') as scope:
                conv_0 = tf.layers.conv2d(
                    inputs=x_2d, filters=32, kernel_size=5, padding='same', activation=tf.nn.relu
                )
                pool_0 = tf.layers.max_pooling2d(
                    inputs=conv_0, pool_size=2, strides=2, padding='same'
                )

                conv_1 = tf.layers.conv2d(
                    inputs=pool_0, filters=64, kernel_size=5, padding='same', activation=tf.nn.relu
                )
                pool_1 = tf.layers.max_pooling2d(
                    inputs=conv_1, pool_size=2, strides=2, padding='same'
                )

                conv_2 = tf.layers.conv2d(
                    inputs=pool_1, filters=128, kernel_size=5, padding='same', activation=tf.nn.relu
                )
                pool_2 = tf.layers.max_pooling2d(
                    inputs=conv_2, pool_size=2, strides=2, padding='same'
                )

                conv_3 = tf.layers.conv2d(
                    inputs=pool_2, filters=256, kernel_size=5, padding='same', activation=tf.nn.relu
                )
                pool_3 = tf.layers.max_pooling2d(
                    inputs=conv_3, pool_size=2, strides=2, padding='same'
                )

                flatten = tf.layers.flatten(inputs=pool_3)
                fc = tf.layers.dense(
                    inputs=flatten, units=1024, activation=tf.nn.relu)

            # Output
            with tf.variable_scope('Output_Layer') as scope:
                y_predict = tf.layers.dense(
                    inputs=fc, units=self._label_size, activation=None, name='y_predict')

            cross_entropy = tf.reduce_mean(
                tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_predict))
            train_step = tf.train.AdamOptimizer(
                learning_rate=learning_rate).minimize(cross_entropy)

        with tf.device('/cpu:0'):
            model_saver = tf.train.Saver()

        # set train count
        if self._dataset_size <= self._batch_size:
            train_count = 1
        else:
            if (self._dataset_size % self._batch_size) == 0:
                train_count = int(self._dataset_size / self._batch_size)
            else:
                train_count = int(self._dataset_size / self._batch_size) + 1

        # session configure
        config = tf.ConfigProto(log_device_placement=True)
        if self._device == 'gpu':
            config.gpu_options.allow_growth = True

        with tf.Session(config=config) as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())

            # optimization
            for step in range(self._epoch):
                self._dataset.reset_batch()
                for i in range(train_count):
                    batch_x, batch_y = self._dataset.next_batch(
                        self._batch_size)
                    sess.run(train_step, feed_dict={x: batch_x, y: batch_y})

            # save model
            par_dir = os.path.split(model_path)[0]
            if not os.path.exists(par_dir):
                os.makedirs(par_dir)

            model_saver.save(sess, model_path)
            self.__load_model = True
            print("Model save to '{}'".format(model_path))

            tf.reset_default_graph()

    def eval(self, dataset_path, model_path):
        """
        Evaluate saved model.

        :param dataset_path: str
            the path to load the dataset for evaluate.
        :param model_path: str
            the path to load the learned model.

        :return: dict
        """
        if not isinstance(dataset_path, str):
            raise TypeError("type of 'dataset_path' must be str.")

        if not isinstance(model_path, str):
            raise TypeError("type of 'model_path' must be str.")

        self.load_dataset(dataset_path)

        sess = tf.Session()

        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())

        self.load_model(model_path, sess)
        graph = tf.get_default_graph()

        with tf.device('/{}:0'.format(self._device)):
            # Input
            x = graph.get_tensor_by_name('Input_Layer/images:0')
            y = graph.get_tensor_by_name('Input_Layer/labels:0')

            y_predict = graph.get_tensor_by_name(
                'Output_Layer/y_predict/BiasAdd:0')

            correct_prediction = tf.equal(
                tf.argmax(y, 1), tf.argmax(y_predict, 1))
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        with tf.device('/cpu:0'):
            confusion_matrix = tf.confusion_matrix(
                labels=tf.argmax(y, 1), predictions=tf.argmax(y_predict, 1))

        if self._dataset_size <= self._batch_size:
            test_count = 1
        else:
            if (self._dataset_size % self._batch_size) == 0:
                test_count = int(self._dataset_size / self._batch_size)
            else:
                test_count = int(self._dataset_size / self._batch_size) + 1

        # test
        self._dataset.reset_batch()
        for i in range(test_count):
            batch_x, batch_y = self._dataset.next_batch(self._batch_size)
            if i == 0:
                test_accuracy = sess.run(accuracy, feed_dict={
                                         x: batch_x, y: batch_y})
                test_cm = sess.run(confusion_matrix, feed_dict={
                                   x: batch_x, y: batch_y})
            else:
                test_accuracy += sess.run(accuracy,
                                          feed_dict={x: batch_x, y: batch_y})
                test_cm += sess.run(confusion_matrix,
                                    feed_dict={x: batch_x, y: batch_y})

        test_accuracy /= test_count
        test_cm = test_cm.tolist()

        tf.reset_default_graph()
        sess.close()

        return {
            'accuracy': test_accuracy,
            'confusion_matrix': test_cm
        }

    def query(self, image_path, model_path):
        """
        Input images and predict labels.

        :param images: array
        :param model_path: str

        :return: nothing
        """
        if not isinstance(image_path, str):
            raise TypeError("type of 'image_path' must be str.")

        if not isinstance(model_path, str):
            raise TypeError("type of 'model_path' must be str.")

        image_width = 48
        image_height = 48
        if self._image_mode == 'RGB':
            image_channel = 3
        elif self._image_mode == 'L':
            image_channel = 1

        image = Image.open(image_path).convert(
            mode=self._image_mode).resize((image_width, image_height))
        w, h = image.size

        pixel_list = list()
        for y in range(h):
            for x in range(w):
                pixel = image.getpixel((x, y))
                if isinstance(pixel, tuple):
                    pixel_list += [p for p in pixel]
                else:
                    pixel_list.append(pixel)

        sess = tf.Session()

        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())

        self.load_model(model_path, sess)
        graph = tf.get_default_graph()

        with tf.device('/{}:0'.format(self._device)):
            # Input
            x = graph.get_tensor_by_name('Input_Layer/images:0')

            y_predict = graph.get_tensor_by_name(
                'Output_Layer/y_predict/BiasAdd:0')

            predict = tf.argmax(y_predict, 1)

            # run session
            result = sess.run(predict, feed_dict={x: [pixel_list]})

        sess.close()

        return result


#! /usr/bin/env python3.6

import urllib3
from bs4 import BeautifulSoup

url = 'https://finance.yahoo.com/quote/GOOG?p=GOOG'
http = urllib3.PoolManager()
response=http.request('GET', url)
soup = BeautifulSoup(response.data)
print (soup.prettify())

# print (soup.html)
# print (soup.p)
# print (soup.find_all(id="link2"))
# print (soup.find_all(["a","b"]))
# for tag in soup.find_all(True):
# 	print("TAG: tag", tag.name())

print(soup.get_text())


import pandas as pd
import pytest

def test_create_dataframe():
    try :
        data = { 'name' : ['AA', 'IBM', 'GOOG'],
            'date' : ['2001-12-01', '2012-02-10', '2010-04-09'],
            'shares' : [100, 30, 90],
            'price' : [12.3, 10.3, 32.2]
        }
        df = pd.DataFrame(data)
        print(df)

        #Add Additional Columns
        df['owner'] = 'Unknown'
        print("After Adding new Column...")
        print(df)

        #Set custom row index
        print("After Adding Setting Custom Index...")
        df.index = ['one', 'two', 'three']
        print(df)

        #data can be accessed using row and column index
        #Access using Column Index
        print("Access through Custom Index...")
        print(df['shares'])

        # access data by row-index
        print("Access data by row-index...")
        print(df.ix['one'])

    except :
        print('Something went wrong. check Log')

def test_convert_dict_to_series():
    try :
        d = {'name' : 'IBM', 'date' : '2010-09-08', 'shares' : 100, 'price' : 10.2}
        s = pd.Series(d)
        print(s)
    except :
        print('Something went wrong. check Log')


# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import collections
import re

from hamcrest import (assert_that, has_item, has_entry, is_not, empty,
                      has_property, contains_inanyorder, has_length,
                      greater_than)  # noqa: H301
import jmespath
import pycontrail.types as types
import pytest
from stepler.third_party import utils

from vapor.helpers import contrail_status
from vapor.helpers import asserts
from vapor.helpers.asserts import superset_of
from vapor import settings

def test_contrail_node_services_status(os_faults_steps):
    contrail_status.check_services_statuses(os_faults_steps)

@pytest.mark.parametrize('role', settings.CONTRAIL_ROLES_DISTRIBUTION)
def test_contrail_service_distribution(os_faults_steps, role):
    """Check that contrail services are running on correct nodes."""
    services_statuses = contrail_status.get_services_statuses(os_faults_steps)
    nodes = settings.CONTRAIL_ROLES_DISTRIBUTION[role]
    expected_services = settings.CONTRAIL_ROLES_SERVICES_MAPPING[role]
    with asserts.AssertsCollector() as collector:
        for node, services in services_statuses.items():
            if node not in nodes:
                continue
            services = [x.service for x in services]
            collector.check(services, superset_of(expected_services))

def test_ifmap_service(os_faults_steps):
    """Verify ifmap service."""
    fqdns = settings.CONTRAIL_ROLES_DISTRIBUTION[
        settings.ROLE_CONTRAIL_CONTROLLER]
    nodes = os_faults_steps.get_nodes(fqdns)
    os_faults_steps.execute_cmd(nodes, 'ifmap-view visual visual')

@pytest.mark.usefixtures('contrail_network_cleanup')
def test_add_virtual_network(contrail_api_client):
    network_name, = utils.generate_ids()
    net = types.VirtualNetwork(network_name)
    contrail_api_client.virtual_network_create(net)
    networks = contrail_api_client.virtual_networks_list()
    assert_that(networks['virtual-networks'],
                has_item(has_entry('uuid', net.uuid)))

@pytest.mark.usefixtures('contrail_network_cleanup')
def test_delete_virtual_network(contrail_api_client, contrail_network):
    contrail_api_client.virtual_network_delete(id=contrail_network.uuid)
    networks = contrail_api_client.virtual_networks_list()
    assert_that(networks['virtual-networks'],
                is_not(has_item(has_entry('uuid', contrail_network.uuid))))

@pytest.mark.usefixtures('contrail_network_cleanup')
def test_update_virtual_network(contrail_api_client, contrail_network):
    new_display_name, = utils.generate_ids()
    contrail_network.display_name = new_display_name
    contrail_api_client.virtual_network_update(contrail_network)
    network_data = contrail_api_client.virtual_network_read(
        id=contrail_network.uuid)
    assert_that(network_data, has_property('display_name', new_display_name))

@pytest.mark.usefixtures('contrail_security_groups_cleanup')
def test_add_security_group(contrail_api_client):
    security_group_name, = utils.generate_ids()
    security_group = types.SecurityGroup(security_group_name)
    contrail_api_client.security_group_create(security_group)
    groups = contrail_api_client.security_groups_list()
    assert_that(groups['security-groups'],
                has_item(has_entry('uuid', security_group.uuid)))

@pytest.mark.usefixtures('contrail_security_groups_cleanup')
def test_delete_security_group(contrail_api_client, contrail_security_group):
    contrail_api_client.security_group_delete(id=contrail_security_group.uuid)
    groups = contrail_api_client.security_groups_list()
    assert_that(
        groups['security-groups'],
        is_not(has_item(has_entry('uuid', contrail_security_group.uuid))))

@pytest.mark.usefixtures('contrail_security_groups_cleanup')
def test_update_security_group(contrail_api_client, contrail_security_group):
    new_display_name, = utils.generate_ids()
    contrail_security_group.display_name = new_display_name
    contrail_api_client.security_group_update(contrail_security_group)
    group_data = contrail_api_client.security_group_read(
        id=contrail_security_group.uuid)
    assert_that(group_data, has_property('display_name', new_display_name))

@pytest.mark.usefixtures('contrail_policies_cleanup')
def test_add_network_policy(contrail_api_client):
    network_policy_name, = utils.generate_ids()
    network_policy = types.NetworkPolicy(network_policy_name)
    contrail_api_client.network_policy_create(network_policy)
    policies = contrail_api_client.network_policys_list()
    assert_that(policies['network-policys'],
                has_item(has_entry('uuid', network_policy.uuid)))

@pytest.mark.usefixtures('contrail_policies_cleanup')
def test_delete_network_policy(contrail_api_client, contrail_network_policy):
    contrail_api_client.network_policy_delete(id=contrail_network_policy.uuid)
    policies = contrail_api_client.network_policys_list()
    assert_that(
        policies['network-policys'],
        is_not(has_item(has_entry('uuid', contrail_network_policy.uuid))))

@pytest.mark.usefixtures('contrail_policies_cleanup')
def test_update_network_policy(contrail_api_client, contrail_network_policy):
    new_display_name, = utils.generate_ids()
    contrail_network_policy.display_name = new_display_name
    contrail_api_client.network_policy_update(contrail_network_policy)
    policy_data = contrail_api_client.network_policy_read(
        id=contrail_network_policy.uuid)
    assert_that(policy_data, has_property('display_name', new_display_name))

@pytest.mark.usefixtures('contrail_ipams_cleanup')
def test_add_network_ipam(contrail_api_client):
    network_ipam_name, = utils.generate_ids()
    network_ipam = types.NetworkIpam(network_ipam_name)
    contrail_api_client.network_ipam_create(network_ipam)
    ipams = contrail_api_client.network_ipams_list()
    assert_that(ipams['network-ipams'],
                has_item(has_entry('uuid', network_ipam.uuid)))

@pytest.mark.usefixtures('contrail_ipams_cleanup')
def test_delete_network_ipam(contrail_api_client, contrail_ipam):
    contrail_api_client.network_ipam_delete(id=contrail_ipam.uuid)
    ipams = contrail_api_client.network_ipams_list()
    assert_that(
        ipams['network-ipams'],
        is_not(has_item(has_entry('uuid', contrail_ipam.uuid))))

@pytest.mark.usefixtures('contrail_ipams_cleanup')
def test_update_network_ipam(contrail_api_client, contrail_ipam):
    new_display_name, = utils.generate_ids()
    contrail_ipam.display_name = new_display_name
    contrail_api_client.network_ipam_update(contrail_ipam)
    ipam_data = contrail_api_client.network_ipam_read(
        id=contrail_ipam.uuid)
    assert_that(ipam_data, has_property('display_name', new_display_name))

def test_contrail_alarms_is_empty(client_contrail_analytics):
    alarms = client_contrail_analytics.get_alarms()
    query = ('*[?@.value.*.alarms[?ack!=`True`]][].'
             '{Node: @.name, Type: @.value.*.alarms[].type}')
    not_ack_alarms = jmespath.search(query, alarms)
    assert_that(not_ack_alarms, empty())

def test_zookeeper_status(znodes_list):
    expected_znodes_list = set(settings.ZOOKEEPER_NODES)
    znodes_list = set(znodes_list)
    min_intersection = len(znodes_list) // 2
    assert_that(znodes_list & expected_znodes_list,
                has_length(greater_than(min_intersection)))

def test_zookeeper_leader_followers(zoo_client):
    """Check that only one zookeeper host has leader mode."""
    hosts = ['{}:{}'.format(ip, port) for (ip, port) in zoo_client.hosts]
    pattern = re.compile('Mode: (follower|leader)')
    modes = collections.defaultdict(set)
    for host in hosts:
        zoo_client.set_hosts(host)
        zoo_client.start()
        result = zoo_client.command('stat')
        mode = pattern.search(result).group(1)
        modes[mode].add(host)
        zoo_client.stop()
    zoo_client.close()
    assert_that(modes['leader'], has_length(1))


import unittest
import numpy as np
from paderbox.label_handling.label_handler import LabelHandler
from paderbox.database import keys
import paderbox.testing as tc
import tempfile
from os import path

class TestLabelHandler(unittest.TestCase):
    def setUp(self):
        self.labels = ['a', 'b', 'c']
        self.lh = LabelHandler(self.labels)

    def test_add(self):
        new_labels = ['d', 'e']
        self.lh.add_labels(new_labels)
        self.assertEqual(self.lh.num_labels, 5)
        self.assertEqual(self.lh.labels, self.labels + new_labels)
        self.assertEqual(self.lh.label_to_int['e'], 4)
        self.assertEqual(self.lh.int_to_label[4], 'e')

    def test_transcription(self):
        self.assertEqual(self.lh.process_transcription(
            self.labels, toarray=False), list(range(3)))
        self.assertEqual(self.lh.recover_transcription(range(3)), self.labels)

    def test_segmentation(self):
        segmentation = [('a', 0, 3), ('c', 1, 3), ('b', 0, 1)]
        target = self.lh.prepare_n_hot(segmentation, num_frames=4)
        self.assertEqual(
            target.tolist(), [[1, 1, 0,],[1, 0, 1],[1, 0, 1],[0, 0, 0]])

    def test_tags(self):
        tags = ['b', 'a']
        target = self.lh.prepare_n_hot(tags)
        self.assertEqual(target.tolist(), [1, 1, 0,])

    def test_src_mapping(self):
        lh = LabelHandler(['a', 'b'], src_mapping={'c': 'b'})
        self.assertEqual(lh.process_transcription(
            ['a', 'b', 'c'], toarray=False), [0, 1, 1])

    def test_oov(self):
        lh = LabelHandler(['a', 'c',], oov_label='<unk>')
        self.assertEqual(lh.num_labels, 3)
        self.assertEqual(lh.process_transcription(
            ['a', 'b', 'c'], toarray=False), [0, 2, 1])

    def test_auto_add(self):
        lh = LabelHandler(oov_label='<unk>', auto_add=True)
        self.assertEqual(lh.num_labels, 1)
        self.assertEqual(lh.process_transcription(
            ['a', 'b', 'c'], toarray=False), [1, 2, 3])
        self.assertEqual(lh.num_labels, 4)
        lh.freeze()
        self.assertEqual(lh.process_transcription(
            ['c', 'd',], toarray=False), [3, 0,])
        self.assertEqual(lh.num_labels, 4)

    def test_read(self):
        lh = LabelHandler(label_key='chars')
        examples = [
            {
                'chars': ['a', 'b',],
            }
        ]
        self.assertEqual(lh.num_labels, 0)
        lh.read_labels(examples)
        self.assertEqual(lh.num_labels, 2)
        examples = [
            {
                'chars': [('c', 0., 2.), ('d', 0.5, 1.5)],
            }
        ]
        lh.read_labels(examples)
        self.assertEqual(lh.num_labels, 4)

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


from apscheduler.schedulers.blocking import BlockingScheduler
import time
import datetime
sched = BlockingScheduler()

@sched.scheduled_job('interval', seconds=10)
def timed_job():
    print('This job is run every three minutes.')
    print(time.ctime())

@sched.scheduled_job('cron', day_of_week='mon-fri', hour='16-19', minute='30-59', second='*/3')
def scheduled_job():
	print('This job is run every 3 mins')

print('before the start funciton')
sched.start()
print("let us figure out the situation")


"""
Contains all the queries needed to get rides
"""
from datetime import timedelta
from .models import Ride

MAXIMUM_RIDES = 10
DECIMAL_LIMIT_FORMAT = "{0:.3f}"

def get_rides(latitude_threshold, longitude_threshold, time_threshold,
              latitude, longitude, time):
    latitude_lb = float(DECIMAL_LIMIT_FORMAT.format(latitude - latitude_threshold))
    latitude_ub = float(DECIMAL_LIMIT_FORMAT.format(latitude + latitude_threshold))
    longitude_lb = float(DECIMAL_LIMIT_FORMAT.format(longitude - longitude_threshold))
    longitude_ub = float(DECIMAL_LIMIT_FORMAT.format(longitude + longitude_threshold))

    rides = Ride.objects.filter(pickup_latitude__gte=latitude_lb,
                                pickup_latitude__lte=latitude_ub,
                                pickup_longitude__gte=longitude_lb,
                                pickup_longitude__lte=longitude_ub,
                                pickup_datetime__gte=time,
                                pickup_datetime__lte=time + timedelta(minutes=time_threshold))\
        .order_by("pickup_datetime")

    return rides[:MAXIMUM_RIDES]


# full assembly of the sub-parts to form the complete net
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
from PIL import Image
from torch.nn.functional import sigmoid
from dynamic_model_modules import *

class CleanU_Net(nn.Module):
    def __init__(self, in_channels):
        super(CleanU_Net, self).__init__()
        self.Conv_down1 = Conv_down(in_channels, 32)
        self.Conv_down2 = Conv_down(32, 64)
        self.Conv_down3 = Conv_down(64, 128)
        self.Conv_down4 = Conv_down(128, 256)
        self.Conv_down5 = Conv_down(256, 512)
        self.Conv_up1 = Conv_up(512, 256)
        self.Conv_up2 = Conv_up(256, 128)
        self.Conv_up3 = Conv_up(128, 64)
        self.Conv_up4 = Conv_up(64, 32)

    def forward(self, x):

        x, conv1 = self.Conv_down1(x)
        print("dConv1 => down1|", x.shape)
        x, conv2 = self.Conv_down2(x)
        print("dConv2 => down2|", x.shape)
        x, conv3 = self.Conv_down3(x)
        print("dConv3 => down3|", x.shape)
        x, conv4 = self.Conv_down4(x)
        print("dConv4 => down4|", x.shape)
        _, x = self.Conv_down5(x)
        print("dConv5|", x.shape)
        x = self.Conv_up1(x, conv4)
        print("up1 => uConv1|", x.shape)
        x = self.Conv_up2(x, conv3)
        print("up2 => uConv2|", x.shape)
        x = self.Conv_up3(x, conv2)
        print("up3 => uConv3|", x.shape)
        x = self.Conv_up4(x, conv1)

        return x

if __name__ == "__main__":
    # A full forward pass
    im = torch.randn(1, 1, 572, 572)
    model = CleanU_Net(1)
    x = model(im)
    print(x.shape)
    del model
    del x
    # print(x.shape)


import math
import os
import random
import re
import sys

# Complete the cavityMap function below.
def cavityMap(grid):

    gridlist = []
    for i in range (len(grid)):
        gridlist.append(list(grid[i]))

    for i in range (len(gridlist)):
        if i == 0 or i == len(gridlist) - 1:
            continue
        for k in range (len(gridlist)):
            if k == 0 or k == len(gridlist) - 1:
                continue
            if gridlist[i][k] > gridlist[i + 1][k] and gridlist[i][k] > gridlist[i - 1][k] and gridlist[i][k] > gridlist[i][k + 1] and gridlist[i][k] > gridlist[i][k - 1]:
                gridlist[i][k] = 'X'

    result = map(''.join, gridlist)
    return result

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())

    grid = []

    for _ in range(n):
        grid_item = input()
        grid.append(grid_item)

    result = cavityMap(grid)

    fptr.write('\n'.join(result))
    fptr.write('\n')

    fptr.close()


from requests_html import HTMLSession
import json

# Insert your API_key here
API_key="XXXXXXXXXXX-XXXXX-XXXXXX-XXXXXXXX"

def parse_json(answer):
    answer=answer["data"]
    this_player={}
    this_player["name"]=answer["platformInfo"]["platformUserHandle"]
    for i in range(2,5):
        ranking={}
        ranking["rank"]=answer["segments"][i]["stats"]["tier"]["value"]
        ranking["div"]=answer["segments"][i]["stats"]["division"]["value"]+1
        ranking["MMR"]=answer["segments"][i]["stats"]["rating"]["value"]
        this_player[f'{i-1}v{i-1}']=ranking
    return this_player

#feel free to add more steam ids
steam_ids=['76561198327846028','76561199023677910','76561198446567626']
all_players={}
session =HTMLSession()
headers={'TRN-Api-Key':API_key}
for id in steam_ids:
    url= f"https://public-api.tracker.gg/v2/rocket-league/standard/profile/steam/{id}"
    response = session.get(url,headers=headers)
    all_players[id]=parse_json(json.loads(response.text))

print(json.dumps(all_players, indent=4))


"""empty message

Revision ID: c8fa5284adc
Revises: 3d509b77d059
Create Date: 2014-07-25 16:37:43.111104

"""

# revision identifiers, used by Alembic.
revision = 'c8fa5284adc'
down_revision = '3d509b77d059'

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('course_time', sa.Column('course_id', sa.Integer(), nullable=True))
    op.add_column('schedule_course', sa.Column('schedule_id', sa.Integer(), nullable=True))
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('schedule_course', 'schedule_id')
    op.drop_column('course_time', 'course_id')
    ### end Alembic commands ###