import numpy as np
import os
from parse import read_input_file, write_output_file
import networkx as nx
import copy
from networkx.exception import NetworkXNoPath
import matplotlib.pyplot as plt

def is_valid(G):
    try:
        nx.shortest_path_length(G, source=0, target=G.number_of_nodes() - 1, weight="weight")
    except NetworkXNoPath:
        return False
    return nx.is_connected(G)
def is_valid_node(G, node):
    tmp = list(G.edges(node, data="weight"))
    G.remove_node(node)
    ret = is_valid(G)
    G.add_node(node)
    for edge in tmp:
        G.add_edge(edge[0], edge[1], weight=edge[2])
    return ret
def set_cover_nodes(edges, c, size, G):
    poss_nodes = set()
    for e in edges:
        if e[0] not in poss_nodes and e[0] != 0 and e[0] != size-1:
            poss_nodes.add(e[0])
        if e[1] not in poss_nodes and e[1] != 0 and e[1] != size-1:
            poss_nodes.add(e[1])

    covered = [False] * len(edges)
    nodes = []
    while c > 0 and len(edges) > 0:
        to_remove = set()
        for n in poss_nodes:
            if not is_valid_node(G, n):
                to_remove.add(n)
        for n in to_remove:
            poss_nodes.remove(n)
        node_freq = [[]] * size
        mx_node = None
        for i in range(len(edges)):
            e = edges[i]
            if covered[i]:
                continue
            if mx_node:
                bm = len(node_freq[mx_node])
            if e[0] in poss_nodes:
                node_freq[e[0]].append(i)
                if not mx_node or len(node_freq[e[0]]) > bm:
                    mx_node = e[0]
            if mx_node:
                bm = len(node_freq[mx_node])
            if e[1] in poss_nodes:
                node_freq[e[1]].append(i)
                if not mx_node or len(node_freq[e[1]]) > bm:
                    mx_node = e[1]
        if mx_node:
            G.remove_node(mx_node)
            nodes.append(mx_node)
        else:
            break
        c -= 1
        for e in node_freq[mx_node]:
            covered[e] = True
    new_edges = []
    for i in range(len(covered)):
        if not covered[i]:
            new_edges.append(edges[i])
    return new_edges, nodes

def find_longest_path_setup(G, dpth):
    nx.draw(G, with_labels = True)
    plt.savefig("start.png")
    plt.clf()

    size = G.number_of_nodes()
    k = 15
    c = 1
    if size <= 30:
        k = 15
        c = 1
    elif size <= 50:
        k = 50
        c = 3
    elif size <= 100:
        k = 100
        c = 5
    ans_list = [[], []]
    for i in range(k+c):
        dpth = min(k+c, dpth)
        curr = find_longest_path(G, k, c, dpth, True, size)
        if not curr:
            break
        if type(curr) == tuple:
            k -= 1
            ans_list[0].append(curr)
            G.remove_edge(curr[0], curr[1])
        elif type(curr) == int:
            c -= 1
            ans_list[1].append(curr)
            G.remove_node(curr)
        else:
            print("BAD")
            return
    # G.remove_node(1)
    #shortest_path = nx.shortest_path(G, source=0, target=size- 1, weight="weight")
    nx.draw(G, with_labels = True)
    plt.savefig("end.png")
    curr_length = nx.shortest_path_length(G, source=0, target=size - 1, weight="weight")
    print("current:", curr_length)
    return ans_list

def find_longest_path(G, k, c, depth, selection, size):
    try:
        poss_ans = nx.shortest_path_length(G, source=0, target=size-1, weight="weight")
    except NetworkXNoPath:
        return
    global ans
    if depth == 0:
        return poss_ans
    shortest_path = nx.shortest_path(G, source=0, target=size-1, weight="weight")
    # try removing each edge along the path
    longest_path_length = poss_ans
    if selection:
        best_item = None
    if k > 0:
        for i in range(len(shortest_path) - 1):
            tmp = G[shortest_path[i]][shortest_path[i + 1]]["weight"]
            G.remove_edge(shortest_path[i], shortest_path[i + 1])
            curr_path_length = find_longest_path(G, k - 1, c, depth - 1, False, size)
            if curr_path_length and curr_path_length >= longest_path_length:
                longest_path_length = curr_path_length
                if selection:
                    best_item = (shortest_path[i], shortest_path[i+1])
            G.add_edge(shortest_path[i], shortest_path[i + 1], weight=tmp)
    # try removing each node along the path
    if c > 0:
        for i in range(1, len(shortest_path) - 1):
            tmp = list(G.edges(shortest_path[i], data="weight"))
            G.remove_node(shortest_path[i])
            curr_path_length = find_longest_path(G, k, c - 1, depth - 1, False, size)
            if nx.is_connected(G) and curr_path_length and curr_path_length >= longest_path_length:
                longest_path_length = curr_path_length
                if selection:
                    best_item = shortest_path[i]
            G.add_node(shortest_path[i])
            for edge in tmp:
                G.add_edge(edge[0], edge[1], weight=edge[2])
    print(poss_ans)
    if selection:
        return best_item
    return longest_path_length

def do_file(file,folder, min_size, max_size):
    print("reading:", file)
    graph = read_input_file(folder+file, min_size=min_size, max_size=max_size)
    orig = graph.copy()
    ans = find_longest_path_setup(graph, 3)
    write_output_file(orig, ans[1], ans[0], "./outputs/" + file.split(".")[0] + ".out")
    print("ANSWER:", ans)

if __name__ == "__main__":
    # for file in os.listdir("./real_inputs/small"):
    #     do_file(file, "./real_inputs/small/", 19, 30)
    # for file in os.listdir("./real_inputs/medium"):
    #     do_file(file, "./real_inputs/large/", 30, 50)
    # for file in os.listdir("./real_inputs/large"):
    #     do_file(file, "./real_inputs/large/", 50, 100)
    do_file("small-220.in","./real_inputs/small/",19,30)

#11:22:30


#!/usr/bin/env python
#-*- coding: utf-8 -*-
import tornado.web
import json
from utils import utils
from utils import KEY
from database import db
from tornado.escape import json_encode
from utils import sendHelp
from utils import xinge

title = '提示信息'
content = '此时有爱心人士由于某些原因取消对您的帮助'
header = 'ehelp_'

class CancelSupportHandler(tornado.web.RequestHandler):
    def post(self):
        params = {}
        resp = {KEY.STATUS: 500}
        params = utils.decode_params(self.request)
        if KEY.ID not in params or KEY.LAUNCHER_ID not in params or KEY.TYPE not in params or KEY.EVENT_ID not in params:
            self.write(json_encode(resp))
            return

        '''trans the term's type'''
        params[KEY.EVENT_ID] = int(params[KEY.EVENT_ID])
        params[KEY.ID] = int(params[KEY.ID])
        params[KEY.LAUNCHER_ID] = int(params[KEY.LAUNCHER_ID])
        params[KEY.TYPE] = int(params[KEY.TYPE])

        flag = db.remove_support_relation(params)
        if flag:
            '''when the event is help or emergence, it is necessary to notify the users'''
            user_info = db.get_user_information({KEY.ID: params[KEY.LAUNCHER_ID]})
            user_info = utils.trans_unicode_to_utf(user_info)
            if user_info is not None:
                if params[KEY.TYPE] == 2:
                    mess = sendHelp.buildMessage(type=1, title=title, content=content, style=xinge.Style(0, 0, 0, 1, 3))
                    sendHelp.sendEhelp(header + user_info[KEY.NICKNAME], mess)
                if params[KEY.TYPE] == 1:
                    mess = sendHelp.buildMessage(type=1, title=title, content=content, style=xinge.Style(0, 0, 0, 0, 6))
                    sendHelp.sendEhelp(header + user_info[KEY.NICKNAME], mess)
            resp[KEY.STATUS] = 200

        self.write(json_encode(resp))


from django.shortcuts import render_to_response
from art.models import art
from django.http import HttpResponse
from art.forms import ArtForm
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.contrib.auth.decorators import login_required
# Create your views here.

def arts_all(request):
	print(request.user)
	language = 'en-gb'
	session_language = 'en-gb'

	if 'lang' in request.COOKIES:
		language = request.COOKIES['lang']
	#art.objects.filter(user_name=request.DATA.user)
	args = {}
	args.update(csrf(request))
	args['arts'] = art.objects.all()
	args['language'] = language
	args['user_logged'] = request.user
	args['u_auth'] = request.user.is_authenticated

	return render_to_response('arts.html', args)

def language(request, language = 'en-gb'):
	response = HttpResponse("setting language to %s" % language)

	response.set_cookie('lang',language)
	return response

def art_i(request, art_id=1):
	return render_to_response('art.html', {'art' : art.objects.get(id=art_id), 'user_logged': request.user, 'u_auth' : request.user.is_authenticated })

def not_logged (request):
	return render_to_response('not_logged.html')

@login_required(login_url='/accounts/not_logged/')
def create(request):
	if request.POST:
		form = ArtForm(request.POST, request.FILES)
		if form.is_valid():
			form.save()

			return HttpResponseRedirect('/arts/all/')
	else:
		form = ArtForm()

	args = {}
	args.update(csrf(request))

	args['form'] = form
	args['user_logged'] = request.user
	args['u_auth']= request.user.is_authenticated
	return render_to_response('create_art.html', args)

def like_art(request, art_id):
	if art_id:
		a = art.objects.get(id=art_id)
		count = a.likes
		count+=1
		a.likes = count
		a.save()

	return HttpResponseRedirect('/arts/get/%s'% art_id)

def search_titles(request):
	if request.method == 'POST':
		search_text = request.POST['search_text']
	else:
		search_text = ''

	arts = art.objects.filter(title__contains = search_text)

	return render_to_response('ajax_search.html', {'arts': arts})


import speech_recognition as sr
import pyttsx3
import pywhatkit
import wikipedia
import pyjokes
import sys
import datetime
from datetime import date
import webbrowser

listener = sr.Recognizer()
engine = pyttsx3.init()

def talk(text):
    engine.say(text)
    engine.runAndWait()

def take_command():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'ivan' in command:
                command = command.replace('ivan', '')
                print(command)
    except:
        pass

    return command

def run_ivan():
    talk('what should i do?')
    command = take_command()
    print(command)
    if 'play' in command:
        song = command.replace('play', '')
        talk('playing ' + song)
        pywhatkit.playonyt(song)
    elif 'time' in command:
        time = datetime.datetime.now().strftime('%I:%M %p')
        talk('Current time is ' + time)
    elif 'tell me about' in command:
        person = command.replace('tell me about', '')
        info = wikipedia.summary(person, 1)
        talk('i found this from Wikipedia that ' + info)
        print('i found this from Wikipedia that ' + info)
    elif 'what is' in command:
        person = command.replace('what is', '')
        info = wikipedia.summary(person, 1)
        talk('i found this from Wikipedia that ' + info)
        print('i found this from Wikipedia that ' + info)
    elif 'who is' in command:
        person = command.replace('who is', '')
        info = wikipedia.summary(person, 1)
        talk('i found this from Wikipedia that ' + info)
        print('i found this from Wikipedia that ' + info)
    elif 'joke' in command:
        talk(pyjokes.get_joke())
        print(pyjokes.get_joke())
    elif 'shut down' in command:
        talk('as you wish, have a good day!')
        sys.exit()
    elif 'sleep' in command:
        talk('as you wish, have a good day!')
        sys.exit()
    elif 'don\'t disturb now' in command:
        talk('as you wish, have a good day!')
        sys.exit()
    elif 'date' in command:
        today = date.today()
        todate = today.strftime("%B /%d /%Y")
        talk('today\'s date is ' + todate)
    elif 'who are you' in command:
        talk('I am ivan, a virtual AI assistant made by Raunak Manna on 11th February 2021. i can do various work '
             'like searching and showing with just few commands')
    elif 'who made you' in command:
        talk('I am ivan, a virtual AI assistant made by Raunak Manna on 11th February 2021. i can do various work '
             'like searching and showing with just few commands')
    elif 'what can you do' in command:
        talk('I am ivan, a virtual AI assistant made by Raunak Manna on 11th February 2021. i can do various work '
             'like searching and showing with just few commands')
    elif 'How are you' in command:
        talk('I am fine, thank you')
    elif 'what\'s going on?' in command:
        talk('I am fine, thank you')
    elif 'search' in command:
        search1 = command.replace('search', '')
        new = 2
        try:
            from googlesearch import search
        except ImportError:
            print("No module named 'google' found")

        # to search
        query = search1

        for j in search(query, tld="co.in", num=1, stop=1, pause=2):
            k = j

        url = j
        talk('opening...')
        webbrowser.open(url, new=new)
    else:
        talk('i am sorry, i didn\'t get that. can you please repeat it again or you can ask with a different name.')

talk('i am ivan, your virtual A I assistant.')
while True:
    run_ivan()


'''
    Created on Thur September  18 2019

    @author danie

    Predict the most important sentence from a given game recap article.
'''
import sys
sys.path.insert(1, '/mnt/c/Users/danie/OneDrive/Documents/Insight/')

import joblib
from Git.nba_smart_database import features as ft
from Git.nba_smart_database.games import Game
from Git.nba_smart_database import tree_model

model_path = ('/mnt/c/Users/danie/OneDrive/Documents/Insight/Git'
    + '/nba_smart_database/tree_model_balanced.joblib')
model = joblib.load(model_path)

def summarize(game_id, raw_data):
    game = Game(game_id, df=raw_data)
    candidates = []

    for paragraph in game.article:
        X = [[ft.n_names(paragraph),
                ft.n_teams(paragraph),
                ft.n_numbers(paragraph),
                ft.n_sentences(paragraph)]]

        if model.predict(X) == 1:
            candidates.append(paragraph)

    return candidates[0] + '\n'


import connectDB
import pandas as pd
import numpy as np
import unicodecsv

connection = connectDB.mydb

Categories = {
    "Coding":1,
    "Hackathon":2,
    "Project":3,
    "Working":4,
    "Contest":5,
    "Offline Study":6,
    "Seminar":7,
    "Workshop":8,
    "Online Study":9,
    "Self Study":10,
    "Reading Writing":11,
    "Community Study":12,
    "Developing":13,
    "Public Speaking":14,
    "Movie":15,
    "Gaming":16,
    "Teaching":17
    }

def CategoriesFinder(activity):
    category = 0
    if activity in Categories:
        category = Categories[activity]
    else :
        activity = activity.strip()
        query = "SELECT Categories_ID FROM Categories_Alternative_Name WHERE Activity_Alternative_Name = \"" + activity+ "\""
        mycursor.execute(query)
        myresult = mycursor.fetchall()
        for x in myresult:
            category = x[0]
    if(category == 0):
        print(activity)
    return category

def LearningStyle(category):
    query = "SELECT Learning_Style_ID FROM Categories_Style WHERE Categories_ID = " + str(category)
    mycursor.execute(query)
    myresult = mycursor.fetchall()
    for x in myresult:
      style = x[0]
      query = "SELECT `Count` FROM `Study_Cluster` WHERE `Cluster_MBTI_ID` = "+ str(type) +" AND `Cluster_Learning_Style_ID` = "+ str(style)
      mycursor.execute(query)
      result = mycursor.fetchall()
      count = result[0][0]+1
      query = "UPDATE Study_Cluster SET Count = "+str(count)+", Update_At = CURRENT_TIMESTAMP, Admin_ID = 3 WHERE Cluster_MBTI_ID= "+ str(type) +" AND `Cluster_Learning_Style_ID` = "+ str(style)
      mycursor.execute(query)
      connection.commit()

mycursor = connection.cursor()

data = pd.read_csv("../Data/cleaning/softSkillStudyActivities.csv")

data_list = data.values.tolist()

with open("../Data/cleaning/softSkillLeftOverData.csv", 'wb') as f:
    f.write(u'\ufeff'.encode('utf8'))
    writer = unicodecsv.writer(f,encoding='utf8')
    writer.writerow(["Type", "Activity"])
    for row in data_list:
        if(row[0] is not 0):
            type = row[0]
            activities = row[1].split("/")
            for activity in activities:
                category = CategoriesFinder(activity)
                if(category is not 0):
                    LearningStyle(category)
                else:
                    writer.writerow([type,activity])


import numpy as np
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation,Flatten
from keras.optimizers import SGD
y_train = np.load("./y_train.npy")
x_train = np.load("./x_train.npy")
x_test = np.load("./x_test.npy")
y_test = np.load("./y_test.npy")
model = Sequential()
model.add(Dense(60,input_dim=24*5,activation="relu"))
model.add(Dense(19,activation="sigmoid"))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd,loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,epochs=1000,batch_size=47000)
score = model.evaluate(x_test,y_test,batch_size=850)
model.save("ANN_2")
print(score)


import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from cvxopt import matrix
from cvxopt import solvers
import time
from copy import copy

def data_split(path):

	def load_mnist(path, kind='train'):
	    import os
	    import gzip
	    import numpy as np

	    labels_path = os.path.join(path,
	                               '%s-labels-idx1-ubyte.gz'
	                               % kind)
	    images_path = os.path.join(path,
	                               '%s-images-idx3-ubyte.gz'
	                               % kind)

	    with gzip.open(labels_path, 'rb') as lbpath:
	        labels = np.frombuffer(lbpath.read(), dtype=np.uint8,
	                               offset=8)

	    with gzip.open(images_path, 'rb') as imgpath:
	        images = np.frombuffer(imgpath.read(), dtype=np.uint8,
	                               offset=16).reshape(len(labels), 784)

	    return images, labels

	X_all_labels, y_all_labels = load_mnist(path, kind='train')

	indexLabel2 = np.where((y_all_labels==2))
	xLabel2 =  X_all_labels[indexLabel2][:1000,:].astype('float64')
	yLabel2 = y_all_labels[indexLabel2][:1000].astype('float64')

	indexLabel4 = np.where((y_all_labels==4))
	xLabel4 =  X_all_labels[indexLabel4][:1000,:].astype('float64')
	yLabel4 = y_all_labels[indexLabel4][:1000].astype('float64')

	yLabel2[:] = +1
	yLabel4[:] = -1

	X = np.concatenate([xLabel2, xLabel4])
	y = np.concatenate([yLabel2, yLabel4])

	X_train, X_test, y_train, y_test = train_test_split(X, y, stratify = y,test_size=0.2, random_state=1696995)

	scaler = MinMaxScaler()
	X_train=scaler.fit_transform(X_train)
	X_test=scaler.fit_transform(X_test)

	return X_train, y_train, X_test, y_test

class Svm_dcmp:

    def __init__(self, gamma, C, kernel, q):

        self.b = 0
        self.C = C
        self.gamma = gamma
        self.kernel = kernel
        self.q = q

    def predict(self,X):

        if self.kernel == "gauss":
            z = (self.alpha*self.y) @ self.kernel_gauss(self.X, X) + self.b
        if self.kernel == "poly":
            z = (self.alpha*self.y) @ self.kernel_poly(self.X, X) + self.b
        a = np.sign(z)
        return a

    def kernel_gauss(self, X1, X2):
        return np.exp(-self.gamma*(np.sum(X1**2, axis = 1).reshape(-1,1) + np.sum(X2**2, axis = 1) - 2*np.dot(X1,X2.T)))

    def kernel_poly(self, X1, X2):
        return (X1 @ X2.T + 1)**self.gamma

    def get_working_set(self, alpha, K):

        # box constraints
        y = self.y.ravel(); C = self.C; q = self.q
        R = np.where((alpha < 1e-5) & (y == +1) | (alpha > C-1e-5) & (y == -1) | (alpha > 1e-5) & (alpha < C-1e-5))[0]
        S = np.where((alpha < 1e-5) & (y == -1) | (alpha > C-1e-5) & (y == +1) | (alpha > 1e-5) & (alpha < C-1e-5))[0]

        # negative gradient divided by y
        Q = np.outer(y,y) * K
        grad = Q @ alpha - 1
        grady = - grad/y

        # I and J definition
        grady_dict = {i:grady[i] for i in range(len(grady))}

        R_dict = dict((k, grady_dict[k]) for k in R)
        indexed_R = {k: v for k, v in sorted(R_dict.items(), key=lambda item: item[1])}
        I = list(indexed_R.keys())[-q//2:]

        S_dict = dict((k, grady_dict[k]) for k in S)
        indexed_S = {k: v for k, v in sorted(S_dict.items(), key=lambda item: item[1])}
        J = list(indexed_S.keys())[:q//2]

        W = I + J
        W_ = list(set(np.arange(self.X.shape[0])) - set(W))

        # optimality condition
        m = max(grady[R])
        M = min(grady[S])

        flag = False
        if m-M < 1e-3:
            flag = True
            self.diff = m-M

        return W, W_, Q, flag

    def objective(self,H):
        return (0.5*self.alpha @ H @ self.alpha.T) - ( np.sum(self.alpha))

    def fit(self, X, y):

        self.y = y
        self.X = X
        self.alpha = np.zeros(X.shape[0])
        self.grad = - np.ones(X.shape[0])

        start = time.time()
        y = y.reshape(-1,1)

        old_alpha = np.zeros(X.shape[0])
        if self.kernel == "gauss":
            K = self.kernel_gauss(X, X)
        if self.kernel == "poly":
            K = self.kernel_poly(X, X)

        its = 0
        for i in range(10000):

            W, W_, Q, flag = self.get_working_set(self.alpha, K)

            if flag:
                break

            # computing alpha
            H = Q
            P = matrix(Q[np.ix_(W,W)])
            q = matrix(old_alpha[W_].T @ Q[np.ix_(W_, W)] - 1)
            G = matrix(np.vstack((-np.eye(len(W)),np.eye(len(W)))))
            h = matrix(np.hstack((np.zeros(len(W)), np.ones(len(W)) * self.C)))
            A = matrix(y[W].reshape(1, -1))
            b = matrix(- y[W_].T @ old_alpha[W_])

            solvers.options['abstol'] = 1e-14
            solvers.options['feastol'] = 1e-15
            solvers.options['show_progress'] = False

            res = solvers.qp(P, q, G, h, A, b)
            alpha = np.array(res['x']).T
            its += res["iterations"]

            self.alpha[W] = alpha
            old_alpha = copy(self.alpha)

        time_elapsed = time.time() - start

        # computing b
        K = self.kernel_gauss(self.X, self.X); y = self.y.reshape(-1,1)

        alpha = self.alpha.ravel()
        idx = np.where((alpha > 1e-5) & (alpha < self.C + 1e-5))[0]

        wx = (y * self.alpha.reshape(-1,1)).T @ K[:,idx]
        b = y[idx] - wx.T
        self.b = np.mean(b)

        return  its, time_elapsed, self.diff, self.objective(H)

def confusion_matrix(y_true, y_pred):

  	result = np.zeros((2, 2))
  	y_true[y_true == 1] = 0
  	y_pred[y_pred == 1] = 0
  	for i in range(len(y_true)):
  		result[int(y_true[i])][int(y_pred[i])] += 1

  	return result

omml homework 2 OptimusPrime/omml homework 2 OptimusPrime/Q2/svm_dcmp.py


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

import cv2
import os
from time import time

import torch
import torch.nn as nn
import torch.utils.data as data
from torch.autograd import Variable as V

import torchvision.transforms.functional as F

import torchvision
from torchvision import transforms

from networks.cenet import CE_Net_
from framework import MyFrame
from loss import dice_bce_loss
from data import ImageFolder
from Visualizer import Visualizer
from PIL import Image

import Constants
import image_utils

# Please specify the ID of graphics cards that you want to use
os.environ['CUDA_VISIBLE_DEVICES'] = "0"

def CE_Net_Train():
    NAME = 'CE-Net' + Constants.ROOT.split('/')[-1]

    # run the Visdom
    viz = Visualizer(env=NAME)

    solver = MyFrame(CE_Net_, dice_bce_loss, 2e-4)
    print("count",Constants.BATCHSIZE_PER_CARD)
    batchsize = torch.cuda.device_count() * Constants.BATCHSIZE_PER_CARD
    print("batchsize",batchsize)

    # For different 2D medical image segmentation tasks, please specify the dataset which you use
    # for examples: you could specify "dataset = 'DRIVE' " for retinal vessel detection.

    dataset = ImageFolder(root_path=Constants.ROOT, datasets='DRIVE')
    data_loader = torch.utils.data.DataLoader(
        dataset,
        batch_size=batchsize,
        shuffle=True,
        num_workers=4)

    # start the logging files
    mylog = open('logs/' + NAME + '.log', 'w')
    tic = time()

    no_optim = 0
    total_epoch = Constants.TOTAL_EPOCH
    train_epoch_best_loss = Constants.INITAL_EPOCH_LOSS
    for epoch in range(1, total_epoch + 1):
        data_loader_iter = iter(data_loader)
        train_epoch_loss = 0
        index = 0

        for img, mask in data_loader_iter:
            solver.set_input(img, mask)
            train_loss, pred = solver.optimize()
            train_epoch_loss += train_loss
            index = index + 1

        # show the original images, predication and ground truth on the visdom.
        show_image = (img + 1.6) / 3.2 * 255.
        viz.img(name='images', img_=show_image[0, :, :, :])
        viz.img(name='labels', img_=mask[0, :, :, :])
        viz.img(name='prediction', img_=pred[0, :, :, :])

        torchvision.utils.save_image(img[0, :, :, :], "test_data/results2/image_"+str(epoch) + ".jpg", nrow=1, padding=2, normalize=True, range=None, scale_each=False, pad_value=0)
        torchvision.utils.save_image(mask[0, :, :, :], "test_data/results2/mask_"+str(epoch) + ".jpg", nrow=1, padding=2, normalize=True, range=None, scale_each=False, pad_value=0)
        torchvision.utils.save_image(pred[0, :, :, :], "test_data/results2/pred_"+str(epoch) + ".jpg", nrow=1, padding=2, normalize=True, range=None, scale_each=False, pad_value=0)

        # x = torch.tensor([[1,2,3],[4,5,6]], dtype = torch.uint8)
        # x = show_image[0,:,:,:]
        # print(x.shape)
        # pil_im = transforms.ToPILImage(mode = 'RGB')(x)
        # pil_im.save('/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/image_' + str(epoch) +  '.jpg')

        # x = mask[0,:,:,:]
        # print(x.shape)
        # pil_im = transforms.ToPILImage(mode = 'L')(x)
        # pil_im.save('/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/mask_' + str(epoch) +  '.jpg')

        # x = pred[0,:,:,:]
        # print(x.shape)
        # pil_im = transforms.ToPILImage(mode = 'HSV')(x.detach().cpu().numpy())
        # pil_im.save('/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/prediction_' + str(epoch) +  '.jpg')
        # (x.detach().numpy()).save("/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/image_" + str(epoch) + ".png")
        # cv2.imwrite('imagename.jpg', x.detach().numpy().astype('uint8')).transpose(2,1,0)
        # x = mask[0,:,:,:]
        # # F.to_pil_image(x.detach().numpy()).save("/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/mask_" + str(epoch) + ".png")
        # x = pred[0,:,:,:]
        # print(x.shape)
        # cv2.imwrite('imagename2.jpg', x.detach().numpy().astype('uint8'))

        # F.to_pil_image(x.detach().numpy()).save("/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/prediction_" + str(epoch) + ".png")
        print("saving images")

        train_epoch_loss = train_epoch_loss/len(data_loader_iter)
        print(mylog, '********')
        print(mylog, 'epoch:', epoch, '    time:', int(time() - tic))
        print(mylog, 'train_loss:', train_epoch_loss)
        print(mylog, 'SHAPE:', Constants.Image_size)
        print('********')
        print('epoch:', epoch, '    time:', int(time() - tic))
        print('train_loss:', train_epoch_loss)
        print('SHAPE:', Constants.Image_size)

        # if train_epoch_loss >= train_epoch_best_loss:
        #     no_optim += 1
        # else:
        #     no_optim = 0
        #     train_epoch_best_loss = train_epoch_loss
        #     solver.save('./weights/' + NAME + '.th')
        # if no_optim > Constants.NUM_EARLY_STOP:
        #     print(mylog, 'early stop at %d epoch' % epoch)
        #     print('early stop at %d epoch' % epoch)
        #     break
        if no_optim > Constants.NUM_UPDATE_LR:
            if solver.old_lr < 5e-7:
                break
            solver.load('./weights/' + NAME + '.th')
            print("loading the weights")
            solver.update_lr(2.0, factor=True, mylog=mylog)
        mylog.flush()

    print(mylog, 'Finish!')
    print('Finish!')
    mylog.close()

if __name__ == '__main__':
    print(torch.__version__)
    CE_Net_Train()


import pickle, pygame
from db import *

pygame.init()
surface = pygame.display.set_mode((200 , 500))
pika = pygame.image.load('pikavol.png').convert()

def tolist(img):
    px = []
    for y in range(103):
        px.append([])
        prevcolor = SKYBLUE
        size = 0
        for x in range(100):
            color = tuple( img.get_at( (x,y) ) )[:3]
            px[y].append(color )
    return px

def save(img):
    with open("pikavol", 'wb') as f:
        image = pickle.Pickler(f)
        image.dump( tolist(img) )

def load(surface, X, Y):
    with open("pikavol", 'rb') as f:
        img = pickle.Unpickler(f).load()
        for y in range(1):
            for x in range(1):
                img2 =  img[y][x][:3]
                pygame.draw.rect(surface, img2, (X+x,Y+y,1,1) )

save(pika)
pygame.quit()


from fastai.vision.widgets import *
from fastai.vision.all import *
from pathlib import Path
import streamlit as st
from urllib.request import urlretrieve
urll = ("http://dl.dropboxusercontent.com/s/ecl4tj6q2u8s4q3/fig-03_5.png?raw=1")
filenamee = "fig-03_5.png"
urlretrieve(urll,filenamee)
st.image(filenamee)

url = 'http://dl.dropboxusercontent.com/s/kcmjcwxjtxnpt5n/malaria.pkl?raw=1'
filename = 'malaria.pkl'
urlretrieve(url,filename)
st.markdown("Detection of malaria disease through prediction from red blood cell images")
st.write("This app is used to predict the infectious condition of the red blood cells as healthy or infected.")
st.write("The following dataset for image classification was used to train the model     https://www.kaggle.com/iarunava/cell-images-for-detecting-malaria")

class Predict:
    def __init__(self, filename):
        self.learn_inference = load_learner(filename)
        self.img = self.get_image_from_upload()
        if self.img is not None:
            self.display_output()

    @staticmethod
    def get_image_from_upload():
        uploaded_file = st.file_uploader("Upload Files",type=['png','jpeg', 'jpg'])
        if uploaded_file is not None:
            return PILImage.create((uploaded_file))
        return None

    def display_output(self):
        st.image(self.img.to_thumb(500,500), caption='Uploaded Image')
        pred, pred_idx, probs = self.learn_inference.predict(self.img)
        st.write(f'Prediction: {pred} red blood cell; Probability: {probs[pred_idx]:.04f}')

if __name__=='__main__':
    predictor = Predict(filename)


import pygame

from engine import config
from engine.block import Block

class Collidable():
    pass

class Item(Block):

    def __init__(self, x, y, image, viewport, targetable=True, hoverable=True):
        Block.__init__(self, x, y, image, viewport)

        self.targetable = targetable
        self.hoverable = targetable or hoverable

        self.collided = False

        self.selected = False

    def mouse_collide(self, pos=None, side_effect=True):
        rect = self.image.get_rect()
        rect = pygame.Rect(
                self.x * config.image_size[0],
                self.y * config.image_size[1],
                config.image_size[0],
                config.image_size[1])

        if not pos:
            pos = pygame.mouse.get_pos()

        corrected_pos = ( pos[0] + self.viewport.v_rect.x, pos[1] + self.viewport.v_rect.y )

        collided = rect.collidepoint(corrected_pos)

        previously_collided = self.collided
        self.collided = collided

        if (self.targetable or self.hoverable) and side_effect:
            if ( (not previously_collided and collided) or
                 (previously_collided and not collided) ):
                self.dirty = 1
                self.viewport.dirty = 1

        return collided

    def toggle_selected(self):
        if self.targetable:
            self.selected = not self.selected
            self.dirty = 1
            self.viewport.dirty = 1

    def clear(self):
        self.viewport.item_layer_surf.fill(pygame.Color(0, 0, 0, 1), rect=(
            self.x * config.image_size[0],
            self.y * config.image_size[1],
            config.image_size[0], config.image_size[1] ))

    def draw(self, surf, force=False):
        if self.dirty == 1 or self.dirty == 2 or force:
            if self.dirty == 1:
                self.dirty = 0

            self.clear()

            if self.collided and not self.selected:
                b = config.selection_border
                cpy = self.image.copy()
                cpy.fill(pygame.Color("#FF0000"), rect=(0, 0, b, config.image_size[1]))
                cpy.fill(pygame.Color("#FF0000"), rect=(config.image_size[0] - b, 0, b, config.image_size[1]))
                cpy.fill(pygame.Color("#FF0000"), rect=(0, 0, config.image_size[1], b))
                cpy.fill(pygame.Color("#FF0000"), rect=(0, config.image_size[0] - b, config.image_size[1], b))

                surf.blit(cpy, (self.rect.x, self.rect.y))

            elif self.selected and not self.collided:
                b = config.selection_border
                cpy = self.image.copy()
                cpy.fill(pygame.Color("#FFFFFF"), rect=(0, 0, b, config.image_size[1]))
                cpy.fill(pygame.Color("#FFFFFF"), rect=(config.image_size[0] - b, 0, b, config.image_size[1]))
                cpy.fill(pygame.Color("#FFFFFF"), rect=(0, 0, config.image_size[1], b))
                cpy.fill(pygame.Color("#FFFFFF"), rect=(0, config.image_size[0] - b, config.image_size[1], b))

                surf.blit(cpy, (self.rect.x, self.rect.y))

            elif self.selected and self.collided:
                b = config.selection_border
                cpy = self.image.copy()
                cpy.fill(pygame.Color("#888888"), rect=(0, 0, b, config.image_size[1]))
                cpy.fill(pygame.Color("#888888"), rect=(config.image_size[0] - b, 0, b, config.image_size[1]))
                cpy.fill(pygame.Color("#888888"), rect=(0, 0, config.image_size[1], b))
                cpy.fill(pygame.Color("#888888"), rect=(0, config.image_size[0] - b, config.image_size[1], b))

                surf.blit(cpy, (self.rect.x, self.rect.y))

            else:
                return surf.blit(self.image, (self.rect.x, self.rect.y))

class HoldableItem(Item):
    def __init__(self, name, x, y, image, viewport):
        Item.__init__(self, x, y, image, viewport, targetable=False)
        self.name = name

    def set_location(self, x, y):
        self.x = x
        self.y = y

        self.rect = self.image.get_rect().move(
                x * config.image_size[0],
                y * config.image_size[1])

    def __str__(self):
        return self.name

class HoldableItemSource():
    def __init__(self, quantity, item_factory, collection_rate=1, gather_time=2):
        self.quantity = quantity
        self.item_factory = item_factory
        self.exhausted = False
        self.collection_rate = collection_rate
        self.gather_time = gather_time
        self.gather_counter = 0

    def collect(self, max_collection=None):
        #TODO find some sort of scailing collection rate
        if not self.exhausted:
            if self.gather_counter < self.gather_time:
                self.gather_counter += 1
                return []
            else:
                self.gather_counter = 0

                if max_collection:
                    collect = min(max_collection, self.collection_rate)
                else:
                    collect = self.collection_rate

                if self.quantity - collect >= 0:
                    self.quantity -= collect
                    if self.quantity == 0:

                        #remove self from item layer
                        self.exhausted = True
                        self.clear()
                        self.viewport.item_layer[self.y][self.x] = None
                        self.dirty = 1
                        self.viewport.dirty = 1

                    return [ self.item_factory(0, 0, self.viewport) for _ in range(0, collect) ]
                #TODO there is a missing case here figure it out

                else:
                    self.exhausted = True
                    return None
        return None

    def remaining(self):
        return self.quantity

class Container(Item):

    def __init__(self, capacity, x, y, image, viewport, item_types=[]):
        Item.__init__(self, x, y, image, viewport, targetable=False)

        self.capacity = capacity
        self.items = []
        self.item_types = item_types

    def can_store_type(self, to_check):
            return bool( sum([ int(isinstance(to_check, check_against))
                for check_against in self.item_types ]) )

    def get_item_types(self):
        return self.item_types

    def add_item_type(self, item_type):
        if item_type not in self.item_types:
            self.item_types.append(item_type)

    def rm_item_type(self, item_type):
        if item_type in self.item_types:
            for _type in self.item_types:
                if item_type == _type:
                    self.item_types.remove(_type)
                    return

    def can_store(self):
        return len(self.items) < self.capacity

    def store(self, item):
        self.items.append(item)

    def check_for_item(self, type_of_item):
        for item in self.items:
            if isinstance(item, type_of_item):
                return True
        return False

    def retrieve(self, type_of_item):
        for item in self.items:
            if isinstance(item, type_of_item):
                self.items.remove(item)
                return item

class Buildable(Item, Collidable):
    def __init__(self, name, x, y, image, viewport):
        Item.__init__(self, x, y, image, viewport)
        self.name = name

class BuildingPlaceholder:
    def __init__(self, type_factory, requirements):
        self.requirements  = [ {"type": req[0], "needed": req[1], "collected": 0} for req in requirements ]
        self.built = False
        self.type_factory = type_factory

    def build(self, material):
        for req in self.requirements:
            if req["type"] == type(material):
                req["collected"] += 1
        if len(self.get_required()) == 0:
            # nothing left to fetch, replace with build object
            self.built = True
            self.viewport.item_layer[self.y][self.x] = self.type_factory(self.x, self.y, self.viewport)
            self.viewport.dirty = True
            self.dirty = 1

    def get_required(self):
        reqs = []
        for req in self.requirements:
            if req["collected"] < req["needed"]:
                reqs.append( ( req["type"], req["needed"] - req["collected"]) )
        return reqs


# binarizes the image and does some cropping, uses erosion for edge detection. Does not analyze the bars

from PIL import Image, ImageFilter, ImageEnhance
import numpy as np
import matplotlib.pyplot as plt
import cv2
from collections import Counter

# General Approach and Methods:
## Binarize the image, increase / decrease contrast, increase / decrease brightness
## shape detection
## use the above shape detection to 'estimate' width value

# binarizeImg takes in an image and converts it to binary using a passed in threshold
def binarizeImg(thresh, baseImg, basePix):
  newImg = Image.new('L', (baseImg.width, baseImg.height)) # make a copy as to not change our original image
  newPix = newImg.load()
  for i in range(baseImg.width):
    for j in range(baseImg.height):
      newPix[i, j] = basePix[i, j] # copy our image

  for i in range(newImg.width):
    for j in range(newImg.height):
      if newPix[i, j] < thresh: # check against the threshold value
        newPix[i, j] = 0
      else:
        newPix[i, j] = 1
  return newImg

# function that increases the contrast of an image given an intensity
def adjustContrast(baseImg, intensity):
  contrast = ImageEnhance.Contrast(baseImg)
  contrast = contrast.enhance(intensity) # set FACTOR > 1 to enhance contrast, < 1 to decrease
  return contrast

# using a modifier, returns an image with a changed brightness setting
def adjustBrightness(baseImg, modifier):
  pixels = baseImg.load()
  for i in range(baseImg.width):
    for j in range(baseImg.height):
      newValue = pixels[i, j] + modifier
      if newValue > 255:
        pixels[i, j] = 255
      elif newValue < 0:
        pixels[i, j] = 0
      else:
        pixels[i, j] = newValue

  return baseImg

# calculates the min and max pxiels in an image, uses the difference to
# determine whether the quality of the image is good or poor
def imgIsGood(img, thresh):
  npArray = np.asarray(img) # convert to numoy array for easier calculation
  min = np.amin(npArray) # get the minimum pixel value
  max = np.amax(npArray) # get the maximum pixel value

  if min == 0 and max == 255: # if both min and max is 0 and 255, probably a digital barcode image
    return True
  else: # otherwise likely a photo of a bracode
    if (max - min) > thresh:
      return False
    else:
      return True

# SMOOTH AND SHARPEN KERNELS GRABBED FROM: https://www.taylorpetrick.com/blog/post/convolution-part3
def smoothImage(img):
  kernel = np.ones((4,4),np.float32)/25 # smooth kernel
  adjusted = cv2.filter2D(np.asarray(img),-1,kernel)
  return Image.fromarray(adjusted)

def sharpenImage(img):
  kernel = np.array(([0, -1, 0],[-1, 5, -1],[0, -1, 0]), dtype="int")
  adjusted = cv2.filter2D(np.asarray(img),-1,kernel)
  return Image.fromarray(adjusted)

def rotateImage(image, angle):
  imageCenter = tuple(np.array(image.shape[1::-1]) / 2)
  rotationMatrix = cv2.getRotationMatrix2D(imageCenter, angle, 1.0)
  result = cv2.warpAffine(image, rotationMatrix, image.shape[1::-1], flags=cv2.INTER_LINEAR)
  return result

# reverses the colors of a binary image
def inverseImage(arr):
  #return np.invert(arr)
  for i in range(len(arr)):
    for j in range(len(arr[i])):
      if arr[i, j] == 0:
        arr[i, j] = 1
      else:
        arr[i, j] = 0
  return arr

def detectBarCode(origImg, binImg):
  # finds all of the contours of the image of all detected shapes
  contours, _ = cv2.findContours(binImg,1,2)
  img = binImg # copy our binImg into a new variable to not change the original binarized version
  validBar = [] # valid bar consists of the coordinates of all rectangles detected if it is assumed to be a bar in the barcode
  maxAngle = 0
  for cnt in contours:
    (x, y, w, h) = cv2.boundingRect(cnt) # gets the x, y coords and size of the contour
    approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
    if len(approx)==4: # if approx is 4, this contour is a rectangle / square

      # with x and y > 0, skip the edge of the image being considered a contour.
      # h and w calculations help assure that we only grab contours that are rectangles
      # in the barcode and not from anywhere else in the image
      if x > 0 and y > 0 and h > 30 and w < 70:
        # If all above are true, then this is probably a barcode rectangle. Add it as a valid contour.
        rotatedRect = cv2.minAreaRect(cnt)
        validBar.append((x, y, x + w, y + h))
        angle = rotatedRect[2] * -1 # stores the angle of the contour
        if angle > maxAngle and angle != -90:
          maxAngle = angle

  validNp = np.asarray(validBar) # make validBars into a numpy array
  left = np.min(validNp[:,0])
  top = np.min(validNp[:,1])
  right = np.max(validNp[:,2])
  bottom = np.max(validNp[:,3])

  drawBoundingBox(np.asarray(origImg), contours, (left, top, right, bottom))
  rotated = rotateImage(binImg, (maxAngle * -1) // 2) # use our maxAngle // 2 as the amount to rotate

  # convert to pillow image for simple cropping, then return back the numpy array value of the cropped image
  image = Image.fromarray(rotated)
  image = image.crop((left, top, right, bottom))
  return np.asarray(image)

def drawBoundingBox(image, contours, points):
  # following gets coordinates needed for cropping
  rectImage = np.asarray(image.copy())
  contImage = np.asarray(image.copy())

  cv2.drawContours(contImage, contours, -1, (255, 0, 0), 2)
  print("DETECTED CONTOURS")
  saveArrayAsImage("contours.jpg", contImage)

  cv2.rectangle(rectImage, (points[0], points[1]), (points[2], points[3]), (255, 0, 0), 3)
  print("DETECTED BARCODE REGION")
  saveArrayAsImage("region.jpg", rectImage * 255)

def saveArrayAsImage(filename, array):
  arrayAsImg = Image.fromarray(array)
  arrayAsImg.save('./results/' + filename)

# from: https://stackoverflow.com/questions/1157106/remove-all-occurrences-of-a-value-from-a-list
def remove_values_from_list(the_list, val):
   return [value for value in the_list if value != val]

def get_four_most_common_list_values(my_list):
  most_common = Counter(my_list)
  most_common = most_common.most_common(4)
  to_return = []
  for i in most_common:
    to_return.append(i[0])
  return to_return

def convert_common_to_widths(my_list, most_common):
  new_list = []
  for item in range(len(my_list)):
    for common in range(len(most_common)):
      if my_list[item] == most_common[common]:
        new_list.append(common + 1) # bar width values can be 1, 2, 3, 4.
  return new_list

# follows the standard of 4 bar width values equaling one bar code digit. Sets this idea up.
def convert_widths_to_tuple(widths):
  tuple_list = []
  single_item = []
  counter = 0
  print(len(widths))
  for item in range(len(widths)):
    if item == counter + 4:
      tuple_list.append(single_item)
      single_item = []
      counter = item + 1
    else:
      single_item.append(widths[item])

  return tuple_list

# Uses the barcode values seen here: https://www.wikihow.com/Read-12-Digit-UPC-Barcodes
# converts the width values taken in the image and assign a numeric value
# uses the margin of error to assign a value even if a width value is slightly off
def assign_values_to_widths(widths, margin):
  actual_values = []
  width_values =	{
    3211: 0,
    2221: 1,
    2122: 2,
    1411: 3,
    1132: 4,
    1231: 5,
    1114: 6,
    1312: 7,
    1213: 8,
    3112: 9,
  }

  for width in widths:
    number = ''.join(map(str, width)) # convert the array of digits into a single number as a string
    number = int(number) # use a cast to convert our number string into an integer
    for value in width_values.keys():
      if number == value:
        actual_values.append(width_values[value])
      else:
        for m in range(number, number + margin): # account for margin of error in positive direction
          if m == value:
            actual_values.append(width_values[value])
            continue
        for m in range(number - margin, number): # account for margin of error in negative direction
          if m == value:
            actual_values.append(width_values[value])
            continue

  return actual_values

# takes in a numpy array representation of an image and attempts to analyze the barcode
def analyzeBarCode(image):
  middle = len(image) // 2
  values = [] # used to store the perceived numerical value of the barcodes
  count = 0
  currentBar = 0
  for pixel in range(len(image[middle])):
    pixelVal = image[middle][pixel]
    if pixel == 0: # if this is the first iteration
      currentBar = pixelVal
    else:
      if pixelVal == currentBar:
        count += 1
      else:
        currentBar = pixelVal
        values.append(count)
        count = 0

  values = remove_values_from_list(values, 0)
  common = get_four_most_common_list_values(values)
  print("common:", common)
  only_most_common = []
  #for ctr in range(len(values)):
   # if values[ctr] in common:
    #  only_most_common.append(values[ctr])

  widths = convert_common_to_widths(values, common)
  tuples = convert_widths_to_tuple(widths)
  print("tuples:", tuples)

  new_values = assign_values_to_widths(tuples, 20)
  print("new values:", new_values)

  return values

#### BEGIN MAIN ####

# load the barcode
imOrig = Image.open('barcode.png')
im = imOrig.convert('L') # convert to grayscale
im.save("./results/original.jpg")

erosionFactor = 0 # will change based on whether it is a photo or digital scan
# if image is not a digital scan of a barcode, meaning it is probably a photo, perform some cleanup.
if not imgIsGood(im, 100):
  print("This image seems to be a photo of a barcode, attempting to clean up and binarize.")
  im = adjustBrightness(im, -40) # increasing brightness seems to make fabrizzio's picture worse
  #im = adjustContrast(im, -100)
  #im = smoothImage(im)
  im = sharpenImage(im)
  saveArrayAsImage("enhanced.jpg", np.asarray(im))
  erosionFactor = 6
else:
  print("Image seems to be a digital scan. Going to binarize without cleaning up")
  erosionFactor = 3

# convert the image to binary, im will now hold the binarized pixels
im = binarizeImg(127, im, im.load())

# store im as a numpy array, makes it easier to perform operations
npBin = np.asarray(im)
# attempt to draw and crop the image to where the barcode is
npBin = detectBarCode(im, npBin)

# build a convolution kernel, and use it to generate the difference between the image and its eroded version
kernel = np.ones((erosionFactor, erosionFactor),np.uint8)
erosion = cv2.erode(npBin, kernel)

difference = npBin - erosion # use erosion for edge detection

## Save our final image to be used for analysis

difference = inverseImage(difference)

saveArrayAsImage("final.jpg", difference * 255)

data = analyzeBarCode(difference)
print("data:", data)


import os
import sys
import time
import logging
import datetime
import asyncore

def get_compact_traceback(e=''):
    except_list = [asyncore.compact_traceback()]
    return "Error: %s Traceback: %s" % (str(e), str(except_list))

def create_logger_obj(source):
    cur_dt = str(datetime.datetime.now().date())
    LOGS_DIR = os.path.join(os.path.dirname(os.getcwd()), 'logs')
    log_file_name = "spider_%s_%s.log" % (source, cur_dt)
    log = initialize_logger(os.path.join(LOGS_DIR, log_file_name))
    return log

def initialize_logger(file_name, log_level_list=[]):
    logger = logging.getLogger()
    try:
        add_logger_handler(logger, file_name, log_level_list)
    except (KeyboardInterrupt, SystemExit):
        raise
    except Exception as e:
        e = sys.exc_info()[2]
        time_str = time.strftime("%Y%m%dT%H%M%S", time.localtime())
        exception_str = "%s: %s: Exception: %s" % (time_str, sys.argv, get_compact_traceback(e))
        #print exception_str

    return logger

def add_logger_handler(logger, file_name, log_level_list=[]):
    success_cnt, handler = 3, None

    for i in xrange(success_cnt):
        try:
            handler = logging.FileHandler(file_name)
            break
        except (KeyboardInterrupt, SystemExit):
            raise
        except: pass

    if not handler: return

    formatter = logging.Formatter('%(asctime)s.%(msecs)d: %(filename)s: %(lineno)d: %(funcName)s: %(levelname)s: %(message)s', "%Y%m%d%H%M%S")
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    set_logger_log_level(logger, log_level_list)

    if handler.stream:
        set_close_on_exec(handler.stream)


with open('input.txt') as f:
    lines = f.read().strip().splitlines()
lines.sort()

from collections import defaultdict
import re
import numpy
guard_log = defaultdict(numpy.zeros(60).copy)

current_guard = None
sleep_start = None

for line in lines:
    time, event = line.split('] ')
    minute = int(time[-2:])
    if '#' in event:
        gid = int(re.findall('\d+', event)[0])
        current_guard = guard_log[gid]
    elif event == "falls asleep":
        sleep_start = minute
    elif event == "wakes up":
        current_guard[sleep_start:minute] += 1

def max_on_vals(d, key=None):
    def new_key(x):
        if key is None:
            return d[x]
        else:
            return key(d[x])
    return max(d, key=new_key)

print("Part 1")
gid = max_on_vals(guard_log, key=numpy.sum)
minute = numpy.argmax(guard_log[gid])
print(gid*minute)

print("Part 2")
gid = max_on_vals(guard_log, key=numpy.max)
minute = numpy.argmax(guard_log[gid])
print(gid*minute)


# -*- coding: UTF-8 -*-
# Authorized by Vlon Jang
# Created on 2017-04-20
# Blog: www.wangqingbaidu.cn
# Email: wangqingbaidu@gmail.com
# From kwai, www.kuaishou.com
# ©2015-2017 All Rights Reserved.
#
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
from utils.Common import CommonUtiler
from sklearn import preprocessing
import os
cu = CommonUtiler()
from model.inception.slim import slim

from sklearn.externals import joblib
import traceback
import numpy as np
BATCHNORM_MOVING_AVERAGE_DECAY = 0.9997
# The decay to use for the moving average.
MOVING_AVERAGE_DECAY = 0.9999

class ImageFeatureExtractor_InceptionV3(object):
    def __init__(self, num_classes=None, visual_model=None, decompression_model=None, cluster_model=None,
                 batch_size=256, per_process_gpu_memory_fraction=0.1, growth=False, device_id=None):
        self.visual_model_path = visual_model
        self.decompression_model_path = decompression_model
        self.cluster_model_path = cluster_model
        self.visual_loaded = False
        self.decompression_loaded = False
        self.cluster_loaded = False
        self.batch_size = batch_size
        self.num_classes = num_classes
        self.memory_fraction = per_process_gpu_memory_fraction
        self.growth = growth
        self.device_id = device_id

        if self.visual_model_path:
            self.load_visual_model(self.visual_model_path)
        if self.decompression_model_path:
            self.load_decompression_model(self.decompression_model_path)
        if self.cluster_model_path:
            self.load_cluster_model(self.cluster_model_path)

    # Define computing graph on specific device_id
    def _define_graph_gpu(self, device_id):
        # Define graph
        with tf.Graph().as_default(), tf.device('/gpu:%d' %device_id):
            # A tensor of size [batch_size, height, width, channels].
            self.images = tf.placeholder(tf.float32, [self.batch_size, 299, 299, 3])
            batch_norm_params = {
                # Decay for the moving averages.
                'decay': BATCHNORM_MOVING_AVERAGE_DECAY,
                # epsilon to prevent 0s in variance.
                'epsilon': 0.001,
            }
            # Set weight_decay for weights in Conv and FC layers.
            with slim.arg_scope([slim.ops.conv2d, slim.ops.fc], weight_decay=0.00004):
                with slim.arg_scope([slim.ops.conv2d], stddev=0.1, activation=tf.nn.relu,
                                    batch_norm_params=batch_norm_params):
                    logits, _ = slim.inception.inception_v3(
                        self.images,
                        num_classes=self.num_classes,
                        is_training=False,
                        restore_logits=True)
            # Unused predict and probably op
            self.pred_op = tf.argmax(logits, axis=1)
            self.prob_op = tf.reduce_max(tf.nn.softmax(logits), axis=1)
            self.logits_tensor = logits
            # Restore the moving average version of the learned variables for eval.
            variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY)
            self.variables_to_restore = variable_averages.variables_to_restore()
            # Generate sessions and get features op
            tf_config = tf.ConfigProto()
            if self.growth:
                tf_config.gpu_options.allow_growth = True
            else:
                tf_config.gpu_options.per_process_gpu_memory_fraction = self.memory_fraction
            with tf.Session(config=tf_config) as sess:
                self.sess = sess
                self.features_tensor = sess.graph.get_tensor_by_name('fc6')

    # Define graph automatically on cpu or on gpu.
    def _define_graph_auto(self):
        with tf.Graph().as_default():
            # A tensor of size [batch_size, height, width, channels].
            self.images = tf.placeholder(tf.float32, [self.batch_size, 299, 299, 3])
            batch_norm_params = {
                # Decay for the moving averages.
                'decay': BATCHNORM_MOVING_AVERAGE_DECAY,
                # epsilon to prevent 0s in variance.
                'epsilon': 0.001,
            }
            # Set weight_decay for weights in Conv and FC layers.
            with slim.arg_scope([slim.ops.conv2d, slim.ops.fc], weight_decay=0.00004):
                with slim.arg_scope([slim.ops.conv2d], stddev=0.1, activation=tf.nn.relu,
                                    batch_norm_params=batch_norm_params):
                    logits, _ = slim.inception.inception_v3(
                        self.images,
                        num_classes=self.num_classes,
                        is_training=False,
                        restore_logits=True,
                        scope='')
#             logits, _ = vgg.vgg_16(
#                 self.images,
#                 num_classes=self.num_classes,
#                 is_training=False,
#                 spatial_squeeze=True,
#                 scope=None)
            # Unused predict and probably op
            self.pred_op = tf.argmax(logits, axis=1)
            self.prob_op = tf.reduce_max(tf.nn.softmax(logits), axis=1)
            self.logits_tensor = tf.nn.softmax(logits)
            # Restore the moving average version of the learned variables for eval.

            variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY)
            self.variables_to_restore = variable_averages.variables_to_restore()

#             variables_to_restore = []
#             for var in slim2.get_model_variables():
#                 variables_to_restore.append(var)
#             self.variables_to_restore = variables_to_restore
            # Generate sessions and get features op
            tf_config = tf.ConfigProto()
            if self.growth:
                tf_config.gpu_options.allow_growth = True
            else:
                tf_config.gpu_options.per_process_gpu_memory_fraction = self.memory_fraction
            with tf.Session(config=tf_config) as sess:
                self.sess = sess
#                 self.dump_graph_def('./vgg')
                self.features_tensor = sess.graph.get_tensor_by_name('logits/pool/AvgPool:0')
#                 self.features_tensor = sess.graph.get_tensor_by_name('vgg_16/fc6/convolution:0')

    def load_visual_model(self, model_path):
        try:
            if self.device_id:
                self._define_graph_gpu(self.device_id)
            else:
                self._define_graph_auto()

#             print (os.path.exists(model_path))
            self.saver = tf.train.Saver(self.variables_to_restore)
            self.saver.restore(self.sess, model_path)
#             variables_to_restore = []
#             for var in slim2.get_model_variables():
#                 variables_to_restore.append(var)
#             print (variables_to_restore)
#             slim2.assign_from_checkpoint_fn(
#                   model_path,
#                   self.variables_to_restore,
#                   ignore_missing_vars=True)
            self.visual_loaded = True
            self.visual_model_path = model_path
            print ('Successfully loaded visual model from %s.' %model_path)
        except:
            traceback.print_exc()
            print ('No checkpoint file found in %s' %model_path)

    # Load PCA model.
    def load_decompression_model(self, model_path):
        try:
            self.decompression_model = joblib.load(model_path)
            self.decompression_loaded = True
            self.decompression_model_path = model_path
            print ('Successfully loaded decompression model from %s.' %model_path)
        except:
            print ('No decompression model file found in %s' %model_path)

    def load_cluster_model(self, model_path):
        try:
            self.cluster_model = joblib.load(model_path)
            self.cluster_model.n_jobs = 1
            self.cluster_loaded = True
            self.cluster_model_path = model_path
            print ('Successfully loaded cluster model from %s.' %model_path)
        except:
            print ('No cluster model file found in %s' %model_path)

    # Softmax cluster distance, small distance high probably.
    def __softmax_distance(self, distance, topk):
        prob = np.exp(np.max(distance) - distance)
        prob_topk_idx = np.argsort(prob)[-topk:][::-1]
        prob_sum = np.reshape(np.repeat(np.sum(prob[prob_topk_idx], axis=-1), topk), (-1, topk))
        return [(cluster_id, cluster_prob)
                for cluster_id, cluster_prob in zip(prob_topk_idx.tolist(),
                                                    np.reshape(prob[prob_topk_idx] / prob_sum,
                                                               (-1)).tolist())]

    # Softmax cluster logit, large logit high probably.
    def __softmax_logit(self, logit, topk):
        prob = np.exp(logit)
        prob_topk_idx = np.argsort(prob)[-topk:][::-1]
        prob_sum = np.reshape(np.repeat(np.sum(prob[prob_topk_idx], axis=-1), topk), (-1, topk))
        return [(cluster_id, cluster_prob)
                for cluster_id, cluster_prob in zip(prob_topk_idx.tolist(),
                                                    np.reshape(prob[prob_topk_idx] / prob_sum,
                                                               (-1)).tolist())]

    # Get avg_pool3 features from model.
    def get_feature(self, images, decompression = False):
        if self.visual_loaded:
            features = self.sess.run([self.features_tensor], {self.images:images})

            features = np.reshape(features, (self.batch_size, -1))
            if decompression and self.decompression_loaded:
                try:
                    features = self.decompression_model.transform(features)
                except:
                    print ('Error while decompressing features. Use original features')
            return features
        else:
            print ('Firstly please load pre-trained model.')

    # Get dl feature on shape [batch_size, num_classes]
    def get_feature_on_class(self, images, decompression = False):
        if self.visual_loaded:
            features = np.reshape(self.sess.run([self.logits_tensor], {self.images:images}),
                                  (self.batch_size, -1))
            if decompression and self.decompression_loaded:
                try:
                    features = self.decompression_model.transform(features)
                except:
                    print ('Error while decompressing features. Use original features')
            return features
        else:
            print ('Firstly please load pre-trained model.')

    # Get top k predictions.
    def get_prediction(self, images, topk=3):
        results = []
        if self.visual_loaded:
            distances = self.get_feature_on_class(images)
            for distance in distances:
                results.append(self.__softmax_logit(distance, topk))
            return results
        else:
            print ('Firstly please load pre-trained model.')

    # Get pred, prob pair.
    def get_labels(self, images):
        if self.visual_loaded:
            pred, prob = self.sess.run([self.pred_op, self.prob_op], {self.images:images})
            pred = np.squeeze(pred)
            prob = np.squeeze(prob)
            return pred, prob
        else:
            print ('Firstly please load pre-trained model.')

    def get_cluster(self, features, topk=3, decompression=False, normalize=True):
        results = []
        if decompression and self.decompression_loaded:
            try:
                features = self.decompression_model.transform(features)
            except:
                print ('Error while decompressing features. Use original features')

        if normalize:
            features = preprocessing.normalize(features, norm='l2')

        if self.cluster_loaded:
            distances = self.cluster_model.transform(features)
            for distance in distances:
                results.append(self.__softmax_distance(distance, topk))
            return results
        else:
            print ('Firstly please load cluster model.')

    def get_cluster_from_image(self, images, topk = 3, normalize=True):
        return self.get_cluster(self.get_feature(images, True), topk=topk, normalize=normalize)

    def get_version(self):
        return 'DL-20170503'

    def dump_graph_def(self, log_dir):
        self.writer = tf.summary.FileWriter(log_dir, self.sess.graph)


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# File Name: Interface.py
# Author: Steven Adema
# Date: Dec 2, 2018
#
# A python GUI to instnatiate a CSVParser object and read it's data frame to a table so that
# the user can interact with the data
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import sqlite3
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget, QTableWidgetItem, QVBoxLayout
import pandas
from CSVParser import CSVParser

# class to hold the GUI that will load the .csv data and allow the user to interact with it
class Ui_MainWindow(object):
    '''Ui_MainWindow is a class that creates a skelton to hold the UI elements passed as a QMainWindow object
        This allows users to see and interact with the data from the .csv file.

    Arguments:
        object QMAainWindow - A main window that can contain widgets

    Author:
        Steven Adema
    '''

    # a list of the headers for the table widget
    headers = ["REF_DATE","GEO","DGUID","Food categories","Commodity","UOM","UOM_ID",
    "SCALAR_FACTOR","SCALAR_ID","VECTOR","COORDINATE","VALUE","STATUS","SYMBOL","TERMINATED","DECIMALS"]

    # instantiate a CSV Parser
    p = CSVParser()
    # read the csv file to a dataframe
    p.parseCSVtoDict()

    def setupUi(self, MainWindow):
        '''Method to instantiate the window elements and set their parameters, actions, size, name

        Arguments:
           MainWindow -- A MainWindow Object belonging to the PyQt5 library

        Author:
            Steven Adema
        '''

        # create the main window
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(938, 564)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        # load button to allow the user to load the table with data from the .csv
        self.loadBTN = QtWidgets.QPushButton(self.centralwidget)
        self.loadBTN.setGeometry(QtCore.QRect(360, 30, 171, 31))
        self.loadBTN.setObjectName("btn_load")
        self.loadBTN.clicked.connect(self.load_btn_click)

        # table is populated from the .csv file
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(50, 70, 811, 391))
        self.tableWidget.setRowCount(498)
        self.tableWidget.setColumnCount(16)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setHorizontalHeaderLabels(self.headers)

        # button to clear the table of all data
        self.clearBTN = QtWidgets.QPushButton(self.centralwidget)
        self.clearBTN.setGeometry(QtCore.QRect(220, 480, 141, 31))
        self.clearBTN.setObjectName("clearBTN")
        self.clearBTN.clicked.connect(self.clear_button_click)

        # button to undo changes made by the user
        self.undoBTN = QtWidgets.QPushButton(self.centralwidget)
        self.undoBTN.setGeometry(QtCore.QRect(380, 480, 151, 31))
        self.undoBTN.setObjectName("undoBTN")
        self.undoBTN.clicked.connect(self.load_btn_click)

        # button to save changes made by the user
        self.saveBTN = QtWidgets.QPushButton(self.centralwidget)
        self.saveBTN.setGeometry(QtCore.QRect(550, 480, 151, 31))
        self.saveBTN.setObjectName("saveBTN")
        self.saveBTN.clicked.connect(self.save_sheet)

        # set the GUID window menu and status bars
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 938, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def save_sheet(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        fileName, _ = QFileDialog.getSaveFileName(self,"QFileDialog.getSaveFileName()","","All Files (*);;Text Files (*.txt)", options=options)
        if fileName:
            print(fileName)

    def clear_button_click(self):
        '''Method to clear the table on click by "undo changes" button

        Author:
            Steven Adema
        '''

        self.tableWidget.clearContents()

    def load_btn_click(self):
        '''Method to lead the table with data on click of "Load CSV data" button

        Author:
            Steven Adema
        '''

        # iterators for use in while loops
        i = 0
        j = 0
        # while there are still rows in the data frame....
        while i < len(self.p.df):
            # add the value from row j to column i from the respective row and column in the dataframe
            while j < 16:
                self.tableWidget.setItem(i,j, QTableWidgetItem(str(self.p.df.at[i, self.p.df.columns[j]])))
                j += 1
            # set j to 0, so that the next row starts at the first column
            j = 0
            # iterate i so that the loop moves to the next row
            i += 1

    def retranslateUi(self, MainWindow):
        '''Method to load the buttons and their text to the UI MainWindow

        Arguments:
            MainWindow -- A MainWindow Object belonging to the PyQt5 library
        Author:
            Steven Adema
        '''

        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Steven Adema - Python - Final Project"))
        self.loadBTN.setText(_translate("MainWindow", "Load CSV Data"))
        self.clearBTN.setText(_translate("MainWindow", "Clear Table"))
        self.undoBTN.setText(_translate("MainWindow", "Undo Changes"))
        self.saveBTN.setText(_translate("MainWindow", "Save Changes"))

    def iterate_through_DF(year):
        '''iterate through the DataFrame to print commodities that are recorded after the passed year

        Arguments:
            year -- lower bound filter for which years to slice the print of rows on

        Author:
            Steven Adema
        '''

        for index, row in p.df.iterrows():
                if row['REF_DATE'] >= year:
                        print(row['Commodity'] + " commodity was recorded beyond year. Row from year: " + year)

# main method to luanch the Python program, start the app and build the UI Main Window
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())


"""
Let's try to visualize what we predict...
"""

from tqdm import tqdm
import os
import h5py
import numpy as np
import pandas as pd
from sklearn.metrics import log_loss
import cv2
import tifffile as tiff
from pylab import *

if __name__ == '__main__':
    data_path = '../data'
    model_name = 'resnet50'

    result_folder = os.path.join(data_path, 'predictions', model_name, 'visualize')

    try:
        os.mkdir(result_folder)
    except:
        pass

    num_folds = 10
    num_classes = 17

    r_val_prediction_aug = []

    r_val_labels = []

    for fold in tqdm(range(num_folds)):
        f = h5py.File(os.path.join(data_path, 'predictions', model_name, 'val_pred_{fold}.hdf5'.format(fold=fold)))

        val_prediction_aug = np.array(f['val_prediction_aug'])
        r_val_prediction_aug += [val_prediction_aug]

        r_val_labels += [pd.read_csv('../data/fold{fold}/val.csv'.format(fold=fold)).sort_values(by='path')]

        f.close()

    val_true = pd.concat(r_val_labels)
    y_true = val_true.drop('path', 1)

    val_true = val_true.set_index('path')

    val_pred_aug = np.vstack(r_val_prediction_aug)

    print('log_loss_aug = ', log_loss(y_true.values.ravel(), val_pred_aug.ravel(), eps=1e-7))

    for i, path in tqdm(enumerate(val_true.index)):
        file_name = path.split('/')[-1]

        jpg = cv2.imread(path)
        jpg = cv2.cvtColor(jpg, cv2.COLOR_BGR2RGB)

        tiff_path = path.replace('train-jpg', 'train-tif-v2').replace('.jpg', '.tif')
        tf = tiff.imread(tiff_path)

        objects = val_true.columns
        y_pos = np.arange(len(objects))
        performance = val_true.loc[path].values

        fig, ax = plt.subplots(2, 5, figsize=(30, 20))

        G = tf[:, :, 1].astype(np.float64)
        NIR = tf[:, :, 3].astype(np.float64)

        NDVI = 1.0 * (G - NIR) / (G + NIR)

        ax[0, 1].set_title('R', fontsize=30)
        ax[0, 1].imshow(jpg[:, :, 0])
        ax[0, 2].set_title('G', fontsize=30)
        ax[0, 2].imshow(jpg[:, :, 1])
        ax[0, 3].set_title('B', fontsize=30)
        ax[0, 3].imshow(jpg[:, :, 2])
        ax[0, 4].set_title('RGB', fontsize=30)
        ax[0, 4].imshow(jpg)

        ax[1, 1].imshow(tf[:, :, 2])
        ax[1, 1].set_title('R', fontsize=30)
        ax[1, 2].imshow(tf[:, :, 1])
        ax[1, 2].set_title('G', fontsize=30)
        ax[1, 3].imshow(tf[:, :, 0])
        ax[1, 3].set_title('B', fontsize=30)

        ax[1, 4].set_title('NIR', fontsize=30)
        ax[1, 4].imshow(tf[:, :, 3])

        ax[1, 0].set_title('NDVI', fontsize=30)
        ax[1, 0].imshow(NDVI)

        ax[0, 0].barh(y_pos, performance, align='center', alpha=0.5, color='blue')
        ax[0, 0].barh(y_pos, val_pred_aug[i], align='center', alpha=0.5, color='red')

        ax[0, 0].set_yticks(y_pos)
        ax[0, 0].set_yticklabels(objects, fontsize=30)

        plt.tight_layout()

        savefig(os.path.join(result_folder, file_name))
        close()


# -*- coding: utf-8 -*-
import shutil
import locm, routem, mapm, dropboxm, gmaps, tools, bokehm, tspm
import logging.config, os, yaml, inspect
import time, math, random, sys, copy
import numpy as np
import abstract_optimizer, anneal_optimizer
import haversine
# haversine((45.7597, 4.8422),(48.8567, 2.3508),miles = True)243.71209416020253
logger = logging.getLogger(__name__)

class SplitterOptimizer(abstract_optimizer.AbstractOptimizer):
    """SplitterOptimizer class provides
    splitter
    based on AbstractOptimizer."""

    def __init__(self, start = None, finish = None, **kwarg):
        super(SplitterOptimizer, self).__init__()
        self.start = start
        self.finish = finish
        self.center = None
        self.knot = None
        # self.node_dtype = np.dtype([('name',np.str_, 32),  ('lat', np.float64, 1),  ('lng', np.float64, 1)])

    def get_score(self,a_set_):
        super(SplitterOptimizer, self).get_score()

    def sort(self,a_set_):
        super(SplitterOptimizer, self).sort(a_set_)

    def find_knot(self):
        node4sorting_dtype = [(name,self.node_dtype[name]) for name in self.node_dtype.names] + [('r_cm', np.float64, 1), ('r_s', np.float64, 1)]
        nodes4sort = np.zeros(self.nodes.shape[0],dtype = node4sorting_dtype)
        for node,n4s in zip(self.nodes,nodes4sort):
            n4s['name'] = node['name']
            n4s['lat'] = node['lat']
            n4s['lng'] = node['lng']
            n4s['r_cm'] = tools.r([self.center['lat'],self.center['lng']],[node['lat'],node['lng']])
            n4s['r_s'] = 0.
        nodes4sort = np.sort(nodes4sort,order=('r_cm'))
        self.knot = self.nodes[self.nodes['name']==nodes4sort[0]['name']][0]

    def split_nodes(self):
        # поиск центра масс точек
        n = self.nodes.shape[0]
        self.center = np.array( ('center', np.sum(self.nodes['lat'])/n, np.sum(self.nodes['lng'])/n), dtype = self.node_dtype)
        logger.debug("s.center\n%s" % (self.center,))
        self.find_knot()
        logger.debug("nearest_node_to_center\n%s" % (self.knot,))
        xc = tools.lng_to_km(self.knot)
        yc = tools.lat_to_km(self.knot)

        xs = tools.lng_to_km(self.start)
        ys = tools.lat_to_km(self.start)
        xf = tools.lng_to_km(self.finish)
        yf = tools.lat_to_km(self.finish)
        if xs!=xf and ys!=yf:
            A = xf-xs
            B = yf-ys
            C = -A*xc - B*yc
            # # line: A*x + B*y + C = 0    -- прямая-нормаль к start--finish, проходящая через узел, ближайший к  центру масс точке
            def part_by_sign(x,y):
                return A*x + B*y + C
        else:
            # line: (x-xs)/(xc-xs) - (y-ys)/(yc-ys)   -- прямая start--центр_масс   для случай, когда начало и конец маршрута в одной точке
            def part_by_sign(x,y):
                return (x-xs)/(xc-xs) - (y-ys)/(yc-ys)
        # разбиение на две группы по принадлежности к полуплоскостям
        part_left = []
        part_right = []
        for node in self.nodes:
            x,y = tools.lng_to_km(node['lng']), tools.lat_to_km(node['lat'])
            in_part_of_surface = part_by_sign(x,y)
            if in_part_of_surface<0:
                logger.debug("node to the left:\n%s" % (node['name'],))
                part_left.append(node)
            if in_part_of_surface>0:
                logger.debug("node to the right:\n%s" % (node['name'],))
                part_right.append(node)
        part_right_np_nodes = np.array(part_right,dtype = self.node_dtype)
        part_left_np_nodes = np.array(part_left,dtype = self.node_dtype)
        return part_right_np_nodes, part_left_np_nodes, self.knot

    def recursive_split(self,parts,nodes,fins,Lmax):
        if all([len(part)<=Lmax for part in parts]):
            return parts, fins
        for part in parts:
            if len(part)<=Lmax:
                continue
            else:
                i = parts.index(part)

                logger.debug("part\n%s" % (part,))
                logger.debug("i\n%s" % (i,))
                logger.debug("self.nodes before extracting\n%s" % (self.nodes,))
                self.nodes = nodes[part]
                logger.debug("self.nodes after extracting\n%s" % (self.nodes,))
                logging.disable(logging.DEBUG)
                rp,lp,new_finish = self.split_nodes()

                logger.debug("len(self.nodes)\n%s" % (len(self.nodes),))
                rp_idxs = tools.find_indeces_of_subarray(nodes.tolist(),rp.tolist())
                lp_idxs = tools.find_indeces_of_subarray(nodes.tolist(),lp.tolist())
                self.finish = new_finish

                parts[i:i+1] = rp_idxs,lp_idxs
                fins[i] = tools.find_indeces_of_subarray(nodes.tolist(),[new_finish])[0]
                fins[i+1] = tools.find_indeces_of_subarray(nodes.tolist(),[new_finish])[0]
                return self.recursive_split(parts,nodes,fins,Lmax)

    def split_anneal_a_route(self,max_nodes_in_part):
        s = self
        list_of_lists_of_indeces = [range(len(s.nodes))]
        all_nodes_list = copy.deepcopy(s.nodes)
        # max_nodes_in_part = 30
        knots_indices = [None]*len(s.nodes)
        logging.disable(logging.DEBUG)
        list_of_lists_of_indeces, knots_indices = s.recursive_split(list_of_lists_of_indeces,all_nodes_list,knots_indices,Lmax=max_nodes_in_part)
        logging.disable(logging.NOTSET)
        logger.debug("knots_indices\n%s" % (knots_indices,))
        logger.debug("list_of_lists_of_indeces\n%s" % (list_of_lists_of_indeces,))

        # отжиг каждой части маршрута по отдельности
        tver_coords = {u'lat':56.8583600,u'lng':35.9005700}
        ryazan_coords = {u'lat':54.6269000,u'lng':39.6916000}
        tver_np_arr = np.array(('Tver',tver_coords['lat'],tver_coords['lng']),dtype = s.node_dtype)
        ryazan_np_arr = np.array(('Tver',ryazan_coords['lat'],ryazan_coords['lng']),dtype = s.node_dtype)
        # silent anneal
        annealed_index_sets = []
        logging.disable(logging.DEBUG)

        def anneal_a_sequence(part_idxs,finish):
            if len(part_idxs)<=1:
                return part_idxs
            a = anneal_optimizer.AnnealOptimizer()
            a.nodes = all_nodes_list[part_idxs]
            a.start = tver_np_arr
            a.finish = finish
            logger.debug("a.start\n%s" % (a.start,))
            logger.debug("a.finish\n%s" % (a.finish,))

            a.set = list(np.arange(a.nodes.shape[0]))
            a.SORT_ON = True
            a.set = a.sort(a.set)
            a.new_set = copy.deepcopy(a.set)  # глубокая
            # инициализация констант
            max_loops = 100
            # max_temp = max_loops/1e1
            max_temp = 50
            stop_temperature = 0.1
            alpha = (1 - 1./max_loops)
            logger.debug("alpha\n%s" % (alpha,))
            # alpha = 1-(1/max_temp)/2.
            a.SWAP_NEAREST = False
            a.SWAP_ORDER = True
            a.SWAP_ONLY_NEIGHBOUR = False

            # инициализация генератора температур в оптимизаторе
            a.init_linear_cooling_schedule(max_temp,max_loops,stop_temp = stop_temperature)
            # a.init_kirkpatrick_cooling_schedule(max_temp,alpha,stop_temp = stop_temperature)

            t, float_value, bool_val, score_list = [], [], [], []
            # a.update_stats()
            # logger.debug("before run\n%s" % a)

            for j in range(max_loops):
                new_set_chosen = a.loop()
                # logger.info("a.current_temp\n%s" % (a.current_temp,))
                t.append(a.current_temp)
                # a.new_score = 1000.
                # math.exp(1. -abs(self.new_score)/float(self.current_temp ))
                fl = math.exp(1. - abs(a.new_score)/float(a.current_temp))
                float_value.append(fl)
                bool_val.append(new_set_chosen)
                score_list.append(a.score)
                if j%1000==0:
                    logger.info("%d of %d" % (j,max_loops))

            for i in range(0,max_loops,100):
                logger.debug("i t - f - b   %d %.02f - %.02f - %s - %.3f" % (i*10,t[i],float_value[i],bool_val[i],score_list[i]))
            annealed_nodes_sequence = tools.find_indeces_of_subarray(all_nodes_list,a.nodes[a.set])
            # annealed_nodes = all_nodes_list[annealed_nodes_sequence]
            # a.plot_stats(fname=str(annealed_nodes_sequence))
            return annealed_nodes_sequence

        for part_idxs,finish_idx in zip(list_of_lists_of_indeces,knots_indices):
            if finish_idx!=None:
                finish_node = all_nodes_list[finish_idx]
            else:
                finish_node = s.finish
            annealed_nodes_sequence = anneal_a_sequence(part_idxs,finish_node)
            annealed_index_sets.append(annealed_nodes_sequence)

        logging.disable(logging.NOTSET)
        logger.debug("annealed_index_sets\n%s" % (annealed_index_sets,))

        full_route_indices = []
        for part,fin in zip(annealed_index_sets,knots_indices):
            full_route_indices += part
            if fin!=None:
                full_route_indices.append(fin)
        logger.debug("full_route_indices\n%s" % (full_route_indices,))

        logging.disable(logging.DEBUG)
        final_full_route_indices = anneal_a_sequence(full_route_indices,ryazan_np_arr)
        logging.disable(logging.NOTSET)

        # рисование групп узлов и промежуточных финишей
        # moscow = locm.Location(address='Moscow')
        # plot = bokehm.Figure(output_fname='annealed_splitter.html',center_coords=moscow.coords,use_gmap=True,)
        # plot.add_line(all_nodes_list, circle_size=10,circles_color='blue',alpha= 0.1,no_line = True)
        # colors_list = ['red','green','blue','orange','yellow']*(len(annealed_index_sets)//5+1)
        # for i,part in enumerate(annealed_index_sets):
        #     logger.debug("len(part)\n%s" % (len(part),))
        #     logger.debug("part\n%s" % (part,))
        #     plot.add_line(all_nodes_list[part], circle_size=5,circles_color=colors_list[i],alpha= 0.5,no_line = False)
        #     if knots_indices[i]!=None:
        #         plot.add_line([s.start,all_nodes_list[knots_indices[i]]], circle_size=10,circles_color='red',alpha= 1.,no_line = False)
        # plot.save2html()

        # moscow = locm.Location(address='Moscow')
        # plot = bokehm.Figure(output_fname='a_route_annealed_splitted.html',center_coords=moscow.coords,use_gmap=True,)
        # step_grid = np.arange(len(full_route_indices))
        # plot.add_line( all_nodes_list[full_route_indices], circle_size=step_grid,circles_color='red',alpha= 0.5,no_line = False)
        # plot.save2html()

        return final_full_route_indices

def init_split_optimizer(nodes_data):
    # создание сплиттера-оптимизатора
    s = SplitterOptimizer()
    node_dtype = np.dtype([('name',np.str_, 32),  ('lat', np.float64, 1),  ('lng', np.float64, 1)])
    # node_dtype = np.dtype([('name',np.str_, 32),  ('lat', np.float64, 1),  ('lng', np.float64, 1)])
    tver_coords = {u'lat':56.8583600,u'lng':35.9005700}
    ryazan_coords = {u'lat':54.6269000,u'lng':39.6916000}
    s.start = np.array(('Tver',tver_coords['lat'],tver_coords['lng']),dtype = node_dtype)
    s.finish = np.array(('Tver',tver_coords['lat'],tver_coords['lng']),dtype = node_dtype)
    # s.finish = np.array(('Ryazan',ryazan_coords['lat'],ryazan_coords['lng']),dtype = node_dtype)

    # инициализация узлов
    logger.debug("nodes_data\n%s" % (nodes_data))
    s.nodes = nodes_data
    return s

def splitter_optimizer_test():
    logger.debug('splitter optimizer  started')

    # создание оптимизатора
    s = SplitterOptimizer()
    node_dtype = np.dtype([('name',np.str_, 32),  ('lat', np.float64, 1),  ('lng', np.float64, 1)])
    # node_dtype = np.dtype([('name',np.str_, 32),  ('lat', np.float64, 1),  ('lng', np.float64, 1)])
    tver_coords = {u'lat':56.8583600,u'lng':35.9005700}
    ryazan_coords = {u'lat':54.6269000,u'lng':39.6916000}
    s.start = np.array(('Tver',tver_coords['lat'],tver_coords['lng']),dtype = node_dtype)
    # s.finish = np.array(('Tver',tver_coords['lat'],tver_coords['lng']),dtype = node_dtype)
    s.finish = np.array(('Ryazan',ryazan_coords['lat'],ryazan_coords['lng']),dtype = node_dtype)
    # инициализация узлов
    n_cities = 20
    nodes_data = tools.get_nodes_data(nodes_num = n_cities, recreate_nodes_data=False)
    logger.debug("nodes_data\n%s" % (nodes_data))
    s.nodes = nodes_data

    # parts = [s.nodes]

    list_of_lists_of_indeces = [range(len(nodes_data))]
    all_nodes_list = nodes_data#.tolist()
    max_nodes_in_part = 5
    knots = [None]*len(nodes_data)
    list_of_lists_of_indeces, knots = s.recursive_split(list_of_lists_of_indeces,all_nodes_list,knots,Lmax=max_nodes_in_part)
    logger.debug("knots\n%s" % (knots,))

    # рисование групп узлов и промежуточных финишей
    moscow = locm.Location(address='Moscow')
    plot = bokehm.Figure(output_fname='splitter.html',center_coords=moscow.coords,use_gmap=True,)
    plot.add_line(s.nodes, circle_size=10,circles_color='blue',alpha= 0.1,no_line = True)
    colors_list = ['red','green','blue','orange','yellow']*(len(list_of_lists_of_indeces)//5+1)
    for i,part in enumerate(list_of_lists_of_indeces):
        logger.debug("len(part)\n%s" % (len(part),))
        logger.debug("part\n%s" % (part,))
        plot.add_line(nodes_data[part], circle_size=5,circles_color=colors_list[i],alpha= 0.5,no_line = True)
        if knots[i]!=None:
            plot.add_line([s.start,nodes_data[knots[i]]], circle_size=10,circles_color='red',alpha= 1.,no_line = False)

    plot.save2html()
    logger.debug('splitter optimizer  finished')
    return s

def main():
    pass

if __name__ == "__main__":
    tools.setup_logging()
    logger = logging.getLogger(__name__)
    logger.info("splitter optimizer script run directly")
    splitter_optimizer_test()
    logger.info("splitter optimizer script finished running")


# Written by Nicholas Gibson on 2/1/18

import pygame
import numpy as np
import serial
import time

# Initialise motor Control message
motControl = "+000+000+000+000"

# Initialise serial port
ser = serial.Serial('/dev/ttyACM0', 9600) # For the boat
time.sleep(0.5)

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# This is a simple class that will help us print to the screen
# It has nothing to do with the joysticks, just outputting the
# information.
class TextPrint:
    def __init__(self):
        self.reset()
        self.font = pygame.font.Font(None, 20)

    def Print(self, screen, textString):
        textBitmap = self.font.render(textString, True, BLACK)
        screen.blit(textBitmap, [self.x, self.y])
        self.y += self.line_height

    def reset(self):
        self.x = 10
        self.y = 10
        self.line_height = 15

    def indent(self):
        self.x += 10

    def unindent(self):
        self.x -= 10

    def blankline(self):
        self.y += self.line_height

pygame.init()

# Set the width and height of the screen [width,height]
size = [500, 300]
screen = pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# Initialize the joysticks
pygame.joystick.init()

# Get ready to print
textPrint = TextPrint()

# -------- Main Program Loop -----------
while done == False:
    # EVENT PROCESSING STEP
    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True  # Flag that we are done so we exit this loop

        # Possible joystick actions: JOYAXISMOTION JOYBALLMOTION JOYBUTTONDOWN JOYBUTTONUP JOYHATMOTION
        if event.type == pygame.JOYBUTTONDOWN:
            print("Joystick button pressed.")
        if event.type == pygame.JOYBUTTONUP:
            print("Joystick button released.")

    # DRAWING STEP
    # First, clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.
    screen.fill(WHITE)
    textPrint.reset()

    # Get count of joysticks
    joystick_count = pygame.joystick.get_count()

    #textPrint.Print(screen, "Number of joysticks: {}".format(joystick_count))
    #textPrint.indent()

    joystick = pygame.joystick.Joystick(0)
    joystick.init()

    textPrint.Print(screen, "RoboBoat Wireless Controller: Manual Mode".format(0))
    name = joystick.get_name()
    textPrint.Print(screen, "Joystick name: {}".format(name))

    textPrint.indent()

    axisLY = joystick.get_axis(1)
    axisRY = joystick.get_axis(4)

    textPrint.Print(screen, "Axis LY value: {:>6.3f}".format(axisLY))
    textPrint.Print(screen, "Axis RY value: {:>6.3f}".format(axisRY))
    textPrint.blankline()

    buttonB = joystick.get_button(1)
    textPrint.Print(screen, "Emergency Stop: {}".format(buttonB))

    textPrint.blankline()

    rightMot = int(axisLY*-255)
    leftMot = int(axisRY*-255)

    textPrint.Print(screen, "Motor 1: {}".format(leftMot))

    textPrint.Print(screen, "Motor 2: {}".format(leftMot))

    textPrint.Print(screen, "Motor 3: {}".format(rightMot))

    textPrint.Print(screen, "Motor 4: {}".format(rightMot))

    # Making motor message

    """
    # Left Motor sign
    if np.sign(leftMot) == -1:
        motControl = '-' + motControl[1:16]
        motControl = motControl[0:4] + '-' + motControl[5:16]
    else:
        motControl = '+' + motControl[1:16]
        motControl = motControl[0:4] + '+' + motControl[5:16]

    # Right Motor sign
    if np.sign(rightMot) == -1:
        motControl = motControl[0:8] + '-' + motControl[9:16]
        motControl = motControl[0:12] + '-' + motControl[13:16]
    else:
        motControl = motControl[0:8] + '+' + motControl[9:16]
        motControl = motControl[0:12] + '+' + motControl[13:16]

    # Left Motor value

    if leftMot > 255:
        leftMot = 255
    elif leftMot < -255:
        leftMot = -255
    """

    if leftMot > 99:
        leftMot = '+' + str(int(leftMot))
    elif leftMot > 9:
        leftMot = '+0' + str(int(leftMot))
    elif leftMot > -1:
        leftMot = '+00' + str(int(leftMot))
    elif leftMot > -10:
        leftMot = '-00' + str(int(abs(leftMot)))
    elif leftMot > -100:
        leftMot = '-0' + str(int(abs(leftMot)))
    elif leftMot > -256:
        leftMot = str(int(leftMot))

    # Right mot value
    if rightMot > 99:
        rightMot = '+' + str(int(rightMot))
    elif rightMot > 9:
        rightMot = '+0' + str(int(rightMot))
    elif rightMot > -1:
        rightMot = '+00' + str(int(rightMot))
    elif rightMot > -10:
        rightMot = '-00' + str(int(abs(rightMot)))
    elif rightMot > -100:
        rightMot = '-0' + str(int(abs(rightMot)))
    elif rightMot > -256:
        rightMot = str(int(rightMot))

    motControl = '<' + leftMot + leftMot + rightMot + rightMot + '>'

    textPrint.blankline()
    textPrint.Print(screen, "{}".format(motControl))
    ser.write(motControl)

    # Display Screen
    pygame.display.flip()

    # Limit to 20 frames per second
    clock.tick(20)

# Close the window and quit.
pygame.quit()


from django.shortcuts import render
from user.models import UserSensorDetail
from sensor_owner.models import SensorDetail

def is_sensor_present(request, sensor_data):
    if UserSensorDetail.objects.filter(user_name=request.user, sensor_id=sensor_data).exists():
        return True
    else:
        return False

def manage_sensors(request):
    user_sensor_data = UserSensorDetail.objects.filter(user_name=request.user)
    available_sensor_data = SensorDetail.objects.all()
    return render(request, 'manage_sensors.html', {'user_sensors': user_sensor_data, 'available_sensors': available_sensor_data})

def add_sensor(request, pk):
    available_sensor_data = SensorDetail.objects.all()
    sensor_data = available_sensor_data.get(id=pk)
    current_user = request.user
    error_message = ''
    success_message = ''
    if is_sensor_present(request, sensor_data):
        error_message = 'Sensor already subscribed'
    else:
        new_sensor = UserSensorDetail()
        new_sensor.user_name = current_user
        new_sensor.sensor_id = sensor_data
        new_sensor.save()
        success_message = 'Sensor with ID '+ str(sensor_data.sensor_id) + ' subscribed'

    user_sensor_data = UserSensorDetail.objects.filter(user_name=request.user)
    return render(request, 'manage_sensors.html', {'user_sensors': user_sensor_data, 'available_sensors': available_sensor_data,
                                                   'error_message': 'Sensor already subscribed', 'error_message': error_message,
                                                   'success_message': success_message})

def delete_sensor(request, pk):
    sensor = UserSensorDetail.objects.get(id=pk)
    sensor.delete()

    user_sensor_data = UserSensorDetail.objects.filter(user_name=request.user)
    available_sensor_data = SensorDetail.objects.all()
    success_message = 'Sensor deleted'

    return render(request, 'manage_sensors.html', {'user_sensors': user_sensor_data, 'available_sensors': available_sensor_data,
                                                   'success_message': success_message})


"""
You are given an array of arrays a. Your task is to group the arrays a[i] by their mean values,
 so that arrays with equal mean values are in the same group, and arrays with different mean
 values are in different groups.

Each group should contain a set of indices (i, j, etc), such that the corresponding
arrays (a[i], a[j], etc) all have the same mean. Return the set of groups as an array
of arrays, where the indices within each group are sorted in ascending order, and the
groups are sorted in ascending order of their minimum element.

"""
from collections import defaultdict

def meanGroups(a):
    grouped = defaultdict(list)

    for i in range(len(a)):
        grouped[sum(a[i]) / max(1, len(a[i]))].append(i)

    res = list(grouped.values())
    [x.sort() for x in res]
    return sorted(res, key=min)