import matplotlib.pyplot as plt
import pytest
import torch
from astropy.io import fits

from mpol import images, utils
from mpol.constants import *

def test_odd_npix():
    expected_error_message = "Image must have an even number of pixels."

    with pytest.raises(ValueError, match=expected_error_message):
        images.BaseCube.from_image_properties(npix=853, nchan=30, cell_size=0.015)

    with pytest.raises(ValueError, match=expected_error_message):
        images.ImageCube.from_image_properties(npix=853, nchan=30, cell_size=0.015)

def test_negative_cell_size():
    expected_error_message = "cell_size must be a positive real number."

    with pytest.raises(ValueError, match=expected_error_message):
        images.BaseCube.from_image_properties(npix=800, nchan=30, cell_size=-0.015)

    with pytest.raises(ValueError, match=expected_error_message):
        images.ImageCube.from_image_properties(npix=800, nchan=30, cell_size=-0.015)

def test_single_chan():
    im = images.ImageCube.from_image_properties(cell_size=0.015, npix=800)
    assert im.nchan == 1

def test_basecube_grad():
    bcube = images.BaseCube.from_image_properties(npix=800, cell_size=0.015)
    loss = torch.sum(bcube())
    loss.backward()

def test_imagecube_grad(coords):
    bcube = images.BaseCube(coords=coords)
    # try passing through ImageLayer
    imagecube = images.ImageCube(coords=coords, passthrough=True)

    # send things through this layer
    loss = torch.sum(imagecube(bcube()))

    loss.backward()

# test for proper fits scale
def test_imagecube_tofits(coords, tmp_path):
    # creating base cube
    bcube = images.BaseCube(coords=coords)

    # try passing through ImageLayer
    imagecube = images.ImageCube(coords=coords, passthrough=True)

    # sending the basecube through the imagecube
    imagecube(bcube())

    # creating output fits file with name 'test_cube_fits_file39.fits'
    # file will be deleted after testing
    imagecube.to_FITS(fname=tmp_path / "test_cube_fits_file39.fits", overwrite=True)

    # inputting the header from the previously created fits file
    fits_header = fits.open(tmp_path / "test_cube_fits_file39.fits")[0].header
    assert (fits_header["CDELT1"] and fits_header["CDELT2"]) == pytest.approx(
        coords.cell_size / 3600
    )

def test_basecube_imagecube(coords, tmp_path):
    # create a mock cube that includes negative values
    nchan = 1
    mean = torch.full(
        (nchan, coords.npix, coords.npix), fill_value=-0.5, dtype=torch.double
    )
    std = torch.full(
        (nchan, coords.npix, coords.npix), fill_value=0.5, dtype=torch.double
    )

    # tensor
    base_cube = torch.normal(mean=mean, std=std)

    # layer
    basecube = images.BaseCube(coords=coords, nchan=nchan, base_cube=base_cube)

    # the default softplus function should map everything to positive values
    output = basecube()

    fig, ax = plt.subplots(ncols=2, nrows=1)

    im = ax[0].imshow(
        np.squeeze(base_cube.detach().numpy()), origin="lower", interpolation="none"
    )
    plt.colorbar(im, ax=ax[0])
    ax[0].set_title("input")

    im = ax[1].imshow(
        np.squeeze(output.detach().numpy()), origin="lower", interpolation="none"
    )
    plt.colorbar(im, ax=ax[1])
    ax[1].set_title("mapped")

    fig.savefig(tmp_path / "basecube_mapped.png", dpi=300)

    # try passing through ImageLayer
    imagecube = images.ImageCube(coords=coords, nchan=nchan, passthrough=True)

    # send things through this layer
    imagecube(basecube())

    fig, ax = plt.subplots(ncols=1)
    im = ax.imshow(
        np.squeeze(imagecube.sky_cube.detach().numpy()),
        extent=imagecube.coords.img_ext,
        origin="lower",
        interpolation="none",
    )
    fig.savefig(tmp_path / "imagecube.png", dpi=300)

    plt.close("all")

def test_base_cube_conv_cube(coords, tmp_path):
    # test whether the HannConvCube functions appropriately

    # create a mock cube that includes negative values
    nchan = 1
    mean = torch.full(
        (nchan, coords.npix, coords.npix), fill_value=-0.5, dtype=torch.double
    )
    std = torch.full(
        (nchan, coords.npix, coords.npix), fill_value=0.5, dtype=torch.double
    )

    # The HannConvCube expects to function on a pre-packed ImageCube,
    # so in order to get the plots looking correct on this test image,
    # we need to faff around with packing

    # tensor
    test_cube = torch.normal(mean=mean, std=std)
    test_cube_packed = utils.sky_cube_to_packed_cube(test_cube)

    # layer
    conv_layer = images.HannConvCube(nchan=nchan)

    conv_output_packed = conv_layer(test_cube_packed)
    conv_output = utils.packed_cube_to_sky_cube(conv_output_packed)

    fig, ax = plt.subplots(ncols=2, nrows=1)

    im = ax[0].imshow(
        np.squeeze(test_cube.detach().numpy()), origin="lower", interpolation="none"
    )
    plt.colorbar(im, ax=ax[0])
    ax[0].set_title("input")

    im = ax[1].imshow(
        np.squeeze(conv_output.detach().numpy()), origin="lower", interpolation="none"
    )
    plt.colorbar(im, ax=ax[1])
    ax[1].set_title("convolved")

    fig.savefig(tmp_path / "convcube.png", dpi=300)

    plt.close("all")

def test_multi_chan_conv(coords, tmp_path):
    # create a mock channel cube that includes negative values
    # and make sure that the HannConvCube works across channels

    nchan = 10
    mean = torch.full(
        (nchan, coords.npix, coords.npix), fill_value=-0.5, dtype=torch.double
    )
    std = torch.full(
        (nchan, coords.npix, coords.npix), fill_value=0.5, dtype=torch.double
    )

    # tensor
    test_cube = torch.normal(mean=mean, std=std)

    # layer
    conv_layer = images.HannConvCube(nchan=nchan)

    conv_layer(test_cube)

def test_image_flux(coords):
    nchan = 20
    im = images.ImageCube(coords=coords, nchan=nchan)
    assert im.flux.size()[0] == nchan


# -*- encoding: utf-8 -*-
"""
sqlalchemy是python的orm程序
数据库却都是关系型的，为了保证一致的使用习惯，通过orm将编程语言的对象模型和数据库的关系模型建立映射关系，这样我们在使用编程语言对数据库进行操作的时候可以直接使用编程语言的对象模型进行操作就可以了
http://www.cnblogs.com/pangguoping/p/5720322.html3
http://www.cnblogs.com/pangguoping/p/5744619.html
http://blog.csdn.net/u013636377/article/details/51331940
"""

import os
import sys
import requests
import psutil
from pprint import pprint as pp
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, CHAR, Integer, String, ForeignKey, UniqueConstraint, Index
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import create_engine
from sqlalchemy import func, or_, not_

DB_CONNECT_STRING = 'mysql+pymysql://root:123@localhost:3306/test?charset=utf8'
engine = create_engine(DB_CONNECT_STRING, echo=False)
DB_Session = sessionmaker(bind=engine)
session = DB_Session()

BaseModel = declarative_base()

def init_db():
    BaseModel.metadata.create_all(engine)

def drop_db():
    BaseModel.metadata.drop_all(engine)

class User(BaseModel):
    __tablename__ = 't1'

    id = Column(Integer, primary_key=True)
    name = Column(CHAR(30)) # or Column(String(30))

user = User(name='a')
session.add(user)
user = User(name='b')
session.add(user)

user = User()
session.add(user)

#向user表中添加数据
session.add_all([
    User(name='user1'),
    User(name='user2'),
    User(name='user3'),
]
)

#session.commit()

query = session.query(User)
print (query) # 显示SQL 语句
print (query.statement) # 同上

print ( '---------------------------------------' );

for user in query: # 遍历时查询
    print (user.name)

print ( '---------------------------------------' );
print (query.first().name)      # 记录不存在时，first() 会返回 None


from django.shortcuts import render
from .models import Alumni
# Create your views here.
def alumni(request):
    obj_alumni = Alumni.objects.all()
    obj_year = []
    for i in obj_alumni:obj_year.append(i.year)
    new_lst = list(set(obj_year))
    new_lst.sort()
    dict = {'alum':obj_alumni,'year':new_lst}
    return render(request,'alumni.html',context=dict)


import sys
import csv
import json
from http.server import HTTPServer, SimpleHTTPRequestHandler, test as test_orig

def leArquivo( arquivo ):
	with open( arquivo, newline='' ) as csvfile:
		listaProdutos = csv.reader( csvfile, delimiter=',' )
		novaLista=[]
		for row in listaProdutos:
			novaLista.append( row )

	return novaLista

def arquivoJSON( arquivo ):
	return json.dumps( leArquivo( arquivo ))

def test (*args):
   test_orig(*args, port=8000)

class CORSRequestHandler( SimpleHTTPRequestHandler ):
    def end_headers (self):
        self.send_header( 'Access-Control-Allow-Origin', '*' )
        self.send_header( 'Content-Type', 'application/json' )
        SimpleHTTPRequestHandler.end_headers(self)

if __name__ == '__main__':
	arq = open( 'index.html','w+' )
	arq.write( arquivoJSON( 'bancodedados.csv' ))
	arq.close()
	test( CORSRequestHandler, HTTPServer )


import h5py
import cv2
import numpy as np
import glob
import math
import string
import random
import matplotlib.pyplot as plt

from utils import *

class Player:
    data = Data()
    path = ''
    path_1 = ''
    path_2 = ''
    colDict = {}

    def createColDict(self, identities):
        self.colDict = {}
        for e in identities:
            if e == 11:
                self.colDict[e] = (165,194,102)
            elif e == 19:
                self.colDict[e] = (98,141,252)
            elif e == 22:
                self.colDict[e] = (203,160,141)
            else:
                self.colDict[e] = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))

    def play(self, toVideo):
        files = sorted(glob.glob(self.path + '*.mov'))
        if files == []:
            files = sorted(glob.glob(self.path + '*.mp4'))
            if files == []:
                print('Error reading video files')
                quit()
        else:
            print('playing video from files ', [f.replace(self.path, '') for f in files])
        frame_idx = 0
        winname = 'playing reconstructed tracks..'
        getsize = cv2.VideoCapture(files[0])
        ret_size,img_size = getsize.read()
        if ret_size:
            res = (int(img_size.shape[1] / 2), int(img_size.shape[0] / 2))
        else:
            print('Error capturing resolution')
            quit()
        cv2.namedWindow(winname, 16 | 1)
        fourcc = cv2.VideoWriter_fourcc(*'X264')
        out = cv2.VideoWriter('out.mkv', fourcc, 30, res, True)
        for f in files:
            cap = cv2.VideoCapture(f)
            ret,img = cap.read()
            while ret and out.isOpened():
                img = cv2.flip(img, 0)
                past = self.data.subset([(frame_idx - 50) <= f <= frame_idx for f in self.data.frame])
                current = self.data.subset(self.data.frame == frame_idx)
                for t in range(past.n()):
                    draw(img, past.x[t], past.y[t], past.corners[t], past.identity[t], self.colDict, 2, -1)
                for t in range(current.n()):
                    draw(img, current.x[t], current.y[t], current.corners[t], current.identity[t], self.colDict, 2, 1)
                cv2.putText(img, 'frame: ' + str(frame_idx), (20, 40), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255), 1, 8, 0)
                img = cv2.resize(img, res)
                cv2.imshow(winname, img)
                if toVideo == True:
                    out.write(img)
                cv2.waitKey(30)
                frame_idx += 1
                ret,img = cap.read()
        out.release()


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

### Learning TensorFlow with Laurence Moroney, Google Brain on Coursera
#/Users/trannguyen/TranData/WORK/BioinformaticsSpecialization_Tran_2019/
#MachineLearning/TensorFlow_LanguageProcessing/codes/

# Need to use tensorflow 2.0.0
# Check version with 'print(tf.__version__)'
#'pip install tensorflow==2.0.0-beta0' for install
# if it's not 2.00 => include this line of code:
# 'tf.enable_eager_execution()'

# imdb dataset is in the tensorflow-datasets
# install the tensorflow dataset:
# 'pip install -q tensorflow-datasets'

import os
import io
import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

def imdb_data_import():
    imdb, infor = tfds.load("imdb_reviews", with_info = True,
                                as_supervised = True)
    train_data, test_data = imdb['train'], imdb['test']

    ##convert data into numpy array for tokenizer
    train_sentences = []; train_labels = []
    test_sentences = []; test_labels = []
    for s, l in train_data:
        train_sentences.append(str(s.numpy()))
        train_labels.append(l.numpy())

    for s,l in test_data:
        test_sentences.append(str(s.numpy()))
        test_labels.append(l.numpy())

    return train_sentences, np.array(train_labels),\
                test_sentences, np.array(test_labels),

def tokenizing_data(train_sentences, test_sentences, corpus_size, max_length):
    # changable parameters:
    trunc_type = 'post'
    oov_tok = "<OOV>"

    tokenizer = Tokenizer(num_words = corpus_size, oov_token = oov_tok)
    #use train_sentences to create the corpus
    tokenizer.fit_on_texts(train_sentences)
    # a dictionary of word:frequency
    word_index = tokenizer.word_index
    train_seq = tokenizer.texts_to_sequences(train_sentences)
    padded_train_seq = pad_sequences(train_seq, maxlen = max_length,
                    truncating = trunc_type)
    #create test_seq based on the corpus from train_sentences data
    test_seq = tokenizer.texts_to_sequences(test_sentences)
    padded_test_seq = pad_sequences(test_seq, maxlen = max_length)
    reverse_word_index = dict([(value, key)
                            for (key, value) in word_index.items()])

    return padded_train_seq, padded_test_seq, reverse_word_index

def buiding_nn_model(padded_train_seq, padded_test_seq,
                train_labels, test_labels, corpus_size, max_length):
    # changable parameters:
    embedding_dim = 16
    num_epochs = 10

    model = tf.keras.Sequential([
        tf.keras.layers.Embedding(corpus_size, embedding_dim,
            input_length = max_length),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(6, activation = 'relu'),
        tf.keras.layers.Dense(1, activation = 'sigmoid')
        ])
    model.compile(loss = 'binary_crossentropy', optimizer = 'adam',
                    metrics = ['accuracy'])
    model.summary()
    model.fit(padded_train_seq, train_labels,
                epochs = num_epochs,
                validation_data = (padded_test_seq, test_labels))
    e = model.layers[0]
    weights = e.get_weights()[0]

    return weights

def writing_v_m_files(weights, reverse_word_index, corpus_size):
    vfile = open('data/imdb/vecs.tsv','w', encoding ='utf-8')
    mfile = open('data/imdb/meta.tsv','w', encoding ='utf-8')

    for num in range(1, corpus_size):
        word = reverse_word_index[num]
        embeddings = weights[num]
        mfile.write(word+'\n')
        vfile.write('\t'.join([str(x) for x in embeddings])+'\n')
    vfile.close()
    mfile.close()

########################################################################
# The main() function
def main():

    # changable parameters:
    corpus_size =10000
    max_length = 120

    train_sentences, train_labels, test_sentences,\
                        test_labels = imdb_data_import()

    padded_train_seq, padded_test_seq, reverse_word_index =\
                tokenizing_data(train_sentences, test_sentences,\
                corpus_size, max_length)

    weights = buiding_nn_model(padded_train_seq, padded_test_seq,
                train_labels, test_labels, corpus_size, max_length)

    writing_v_m_files(weights, reverse_word_index, corpus_size)

#######################################################################
# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
  main()


from collections import deque
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        res=[]
        queue=deque()
        lastVisit=root
        while root or queue:
            while root:
                queue.append(root)
                root=root.left
            #先一直往左找，因为先返回的是左
            root =queue[-1]
            if root.right is None or root.right == lastVisit:
                res.append(root.val)
                queue.pop()
                lastVisit=root
                root=None
                #对当前节点判断一下，是该往上走了还是该往右走了，如果
            else:
                root=root.right
        return res


# Copyright (C) 2020 Zurich Instruments
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE file for details.

import pytest
from hypothesis import given, strategies as st

from .context import Parse, Parameter, nodetree_logger, parser_logger

nodetree_logger.disable_logging()
parser_logger.disable_logging()

DUMMY_PARAMETER = {
    "Node": "test/bla/blub",
}

DUMMY_OPTIONS = {
    "1": '"result_of_integration": Complex-valued integration results of the Weighted '
    "Integration in Qubit Readout mode.",
    "3": '"result_of_discrimination": The results after state discrimination.',
}

DUMMY_OPTIONS2 = {
    "0": '"chan0trigin0", "channel0_trigger_input0": Channel 1, Trigger Input A.',
    "1024": '"swtrig0", "software_trigger0": Software Trigger 0.',
}

DUMMY_SET_PARSER = [
    lambda v: Parse.greater_equal(v, 4),
    lambda v: Parse.multiple_of(v, 4, "down"),
]

class Device:
    def __init__(self):
        self.serial = ""
        self._value = None

    def _get(self, path):
        return self._value

    def _set(self, path, v, sync):
        self._value = v
        return self._value

    def _assert_node_value(self, path, v, blocking, timeout, sleep_time):
        print(v, self._get(path))
        if v == self._get(path):
            return True
        else:
            return False

class Parent:
    def __init__(self, dev):
        self._device = dev

def test_parameter_init():
    dev = Device()
    parent = Parent(dev)
    p = Parameter(parent, DUMMY_PARAMETER)
    assert p._parent == parent
    assert p._device == dev

def test_parameter_details():
    dev = Device()
    parent = Parent(dev)
    params = DUMMY_PARAMETER
    params["Description"] = "TEST"
    params["Type"] = "TEST"
    params["Properties"] = "Read, Write"
    params["Options"] = DUMMY_OPTIONS
    params["Unit"] = "TEST"
    p = Parameter(parent, DUMMY_PARAMETER)
    assert p.__repr__() != ""
    p = Parameter(parent, DUMMY_PARAMETER, auto_mapping=True)
    assert p._map == {1: "result_of_integration", 3: "result_of_discrimination"}
    assert p._display_mapping is False
    params["Options"] = DUMMY_OPTIONS2
    p = Parameter(parent, DUMMY_PARAMETER, auto_mapping=True)
    assert p._map == {
        0: ["chan0trigin0", "channel0_trigger_input0"],
        1024: ["swtrig0", "software_trigger0"],
    }
    assert p._display_mapping is False
    p = Parameter(parent, DUMMY_PARAMETER, mapping={0: "key"})
    assert p._map == {0: "key"}
    assert p._display_mapping is True
    params["Options"] = None
    with pytest.raises(nodetree_logger.ToolkitNodeTreeError):
        Parameter(parent, DUMMY_PARAMETER, auto_mapping=True)

@given(st.integers())
def test_set_get(v):
    dev = Device()
    parent = Parent(dev)
    params = DUMMY_PARAMETER
    params["Properties"] = "Read, Write"
    p = Parameter(parent, DUMMY_PARAMETER)
    p(v)
    assert p() == v
    assert p.assert_value(v) is True
    p = Parameter(parent, DUMMY_PARAMETER, set_parser=DUMMY_SET_PARSER)
    p(5)
    assert p() == 4
    assert p.assert_value(4) is True
    p = Parameter(parent, DUMMY_PARAMETER, mapping={0: "key"})
    p("key")
    assert p() == "key"
    assert p.assert_value("key") is True
    p(0)
    assert p() == "key"
    assert p.assert_value("key") is True
    with pytest.raises(ValueError):
        p(1)
    with pytest.raises(ValueError):
        p("key2")
    params["Options"] = DUMMY_OPTIONS
    p = Parameter(parent, DUMMY_PARAMETER, auto_mapping=True)
    p(1)
    assert p() == "result_of_integration"
    assert p.assert_value("result_of_integration") is True
    with pytest.raises(ValueError):
        p(2)
    params["Options"] = DUMMY_OPTIONS2
    p = Parameter(parent, DUMMY_PARAMETER, auto_mapping=True)
    p(0)
    assert p() == "chan0trigin0"
    assert p.assert_value("chan0trigin0") is True
    with pytest.raises(ValueError):
        p(1)

def test_set_get_readonly():
    dev = Device()
    parent = Parent(dev)
    params = DUMMY_PARAMETER
    params["Properties"] = "Read"
    p = Parameter(parent, DUMMY_PARAMETER)
    p()
    with pytest.raises(nodetree_logger.ToolkitNodeTreeError):
        p(0)

@given(st.integers())
def test_set_get_writeonly(v):
    dev = Device()
    parent = Parent(dev)
    params = DUMMY_PARAMETER
    params["Properties"] = "Write"
    p = Parameter(parent, DUMMY_PARAMETER)
    p(v)
    with pytest.raises(nodetree_logger.ToolkitNodeTreeError):
        p()


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

import logging
import sys
import os
from datetime import datetime
from random import shuffle
import numpy as np

from nn import nn_layer
from nn import conv_layer
from nn import pooling_layer
from nn import activation
from nn import simple_nn
from util import mnist

def get_kernels():
    result = []
    uuid = 1

    # 1. 3x3 kernels
    for i in range(8):
        func = activation.reluFunc
        if i % 3 == 0:
            func = activation.tanhFunc
        kernel = conv_layer.Kernel(3, func, uuid)
        result.append(kernel)
        uuid += 1

    # 2. 5x5 kernels
    for i in range(8):
        func = activation.reluFunc
        if i % 3 == 0:
            func = activation.tanhFunc
        kernel = conv_layer.Kernel(5, func, uuid)
        result.append(kernel)
        uuid += 1
    return result

def construct_cnn(l2=0.0):
    img_input = nn_layer.InputLayer("mnist_input", 784)
    output_layer = nn_layer.SoftmaxOutputLayer("mnist_output", 10)

    # 1. set input and output layers
    nn = simple_nn.NNetwork()
    nn.set_input(img_input)
    nn.set_output(output_layer)

    # 2. add Conv-Pooling layers
    c1 = conv_layer.ConvLayer("conv1")
    c1.set_kernels(get_kernels())
    nn.add_hidden_layer(c1)

    # 2x2 none-overlapping max-pooling
    p1 = pooling_layer.MaxPoolingLayer("pool1", 2, 2)
    nn.add_hidden_layer(p1)

    # 3. add some full-connected hidden layers
    h1 = nn_layer.HiddenLayer("h1", 512, activation.tanhFunc)
    h1.set_lambda2(l2)
    nn.add_hidden_layer(h1)

    h2 = nn_layer.HiddenLayer("h2", 128, activation.tanhFunc)
    h2.set_lambda2(l2)
    nn.add_hidden_layer(h2)

    h3 = nn_layer.HiddenLayer("h3", 10, activation.reluFunc)
    h3.set_lambda2(l2)
    nn.add_hidden_layer(h3)

    # 3. complete nn construction
    # print("%s" % (nn))
    fake_img = np.zeros((1, 28, 28))
    img_input.feed(fake_img)
    nn.connect_layers()
    logging.info("NN information:\n" + nn.get_detail())
    return nn

def train_it(nn, train_data, lr):
    labels = train_data[0]
    imgs = train_data[1]

    # shuffle the data
    alist = list(range(labels.shape[0]))
    shuffle(alist)

    num = 0
    for i in alist:
        label = labels[i, :]
        img = imgs[i, :]
        nn.train(img, label, lr)
        if num % 1000 == 0:
            logging.info("num=%d" % num)
        num += 1

    return

def evaluate_it(nn, test_data, prefix):
    labels = test_data[0]
    imgs = test_data[1]
    num = labels.shape[0]

    total_correct = 0
    total_cost = 0

    for i in range(num):
        label = labels[i, :]
        img = imgs[i, :]
        correct, cost = nn.evaluate(img, label)
        total_correct += correct
        total_cost += cost

    accuracy = float(total_correct) / num
    avg_cost = total_cost/num

    msg = "[%s] accuracy=%.4f, avg_cost=%.4f" % (prefix, accuracy, avg_cost)
    logging.info(msg)
    return

def get_lr(step, current_lr):
    """simple learning rate scheduler"""
    lrs = {0: 0.008, 1: 0.005, 2: 0.003, 5: 0.002, 6: 0.001, 8: 0.0008, 10: 0.0005, 15: 0.0001}
    if step in lrs:
        return lrs[step]
    return current_lr

def train_nn(data_dir):
    nn = construct_cnn()
    # l2 = 1e-3
    # nn = construct_big_nn(l2)
    train_data = mnist.load_data_3d(data_dir, "train")
    test_data = mnist.load_data_3d(data_dir, "test")
    if (train_data is None) or (test_data is None):
        logging.error("[ERROR] failed to load data")
        return

    lr = 0.005
    for i in range(100):
        lr = get_lr(i, lr)
        msg = "begin epoch-%s, lr=%.6f" % (i, lr)
        logging.info(msg)
        train_it(nn, train_data, lr)
        evaluate_it(nn, test_data, "test")
        evaluate_it(nn, train_data, "train")
        logging.info("end epoch-%s" % (i, ))

    return

def main():
    train_nn("./data/")
    return

def setup_log():
    logfile = "./train.%s.log" % (os.getpid())
    if len(sys.argv) > 1:
        logfile = sys.argv[1]
    print("logfile=%s" % (logfile,))
    logging.basicConfig(filename=logfile, format='[%(asctime)s] %(message)s', level=logging.DEBUG)
    return

if __name__ == "__main__":
    setup_log()
    main()


import math
import os
import sys

import cv2
import deep_ga
import matplotlib.pyplot as plt
import numpy as np
import pocolog_pybind
from progressbar import progressbar
from plyfile import PlyData, PlyElement

def euler_from_quaternion(quaternion):
        """
        Convert a quaternion into euler angles (yaw, pitch, roll) (in radians)
        """
        x=quaternion["im"][0]
        y=quaternion["im"][1]
        z=quaternion["im"][2]
        w=quaternion["re"]

        t0 = 2*(w*z + x*y)
        t1 = 1 - 2 * (x*x + y*y)
        t1 = w*w + x*x - y*y - z*z
        yaw = math.atan2(t0, t1)

        t2 = 2*(w*y - z*x)
        pitch = math.asin(t2)

        t3 = 2*(w*x + y*z)
        t4 = w*w - x*x - y*y + z*z
        roll = math.atan2(t3, t4)

        return yaw, pitch, roll # in radians

def extract_rigidbody_stream(stream, resolution=1):
    size=gps_stream.get_size()
    size//=resolution
    state = np.empty((size,7))
    for i in progressbar(range(0,size)):
        value = gps_stream.get_sample(i*resolution)
        py_value = value.cast(recursive=True)
        # print(py_value.keys())
        time = py_value["time"]["microseconds"]
        pos = py_value["position"]["data"]
        eul = euler_from_quaternion(py_value["orientation"])
        state[i,0]=time
        state[i,1:4]=pos
        state[i,4:]=eul
        value.destroy()
    return state

dataset='/media/heimdal/Dataset1'

if len(sys.argv)>1:
    traverse=str(sys.argv[1])
else:
    traverse='9June/Traverse/20170609-1909/'

traverse_name=traverse.split('/')[-2]
print(traverse_name)
path = dataset + "/" + traverse
processed_path = dataset + "/processed/" + traverse

print(processed_path)
if not os.path.isdir(processed_path):
    print("No processed path")
    exit()

# create file index. Its possible to specify multiple logfiles
multi_file_index = pocolog_pybind.pocolog.MultiFileIndex()
multi_file_index.create_index([processed_path + "ga_slam.0.log",
                            path + "updated/waypoint_navigation.log"])
streams = multi_file_index.get_all_streams()
dem_stream = streams["/ga_slam.localElevationMapMean"]
gps_stream = streams["/gps_heading.pose_samples_out"]

gps_references = deep_ga.get_gps_references()
state = extract_rigidbody_stream(gps_stream,1)
state[:,1]+=gps_references[0]
state[:,2]+=gps_references[1]
state[:,3]+=gps_references[2]

size=dem_stream.get_size()
dems  = None
times = np.empty((size,))
state_dem = np.empty((size,7))

index_gps = 0

for t in progressbar(range(1,size)):
    try:
        value = dem_stream.get_sample(t)
        py_value = value.cast(recursive=True)
        value.destroy()
    except RuntimeError:
        dems=dems[:t,:,:]
        times=times[:t]
        state_dem=state_dem[:t,:]
        break

    times[t] = py_value["time"]["microseconds"]

    while state[index_gps+1,0]<=py_value["time"]["microseconds"]:
        index_gps+=1
    state_dem[t,:]=state[index_gps,:]

    local_dem = np.array(py_value["data"])
    local_dem = local_dem.reshape((py_value["height"], py_value["width"]), order="F").astype("float32")
    local_dem = local_dem[::-1,::-1].T
    if dems is None:
        w,h = local_dem.shape
        dems = np.empty((size,w,h))
    dems[t,:,:]=local_dem

np.savez_compressed(dataset + "/processed/"+traverse_name+".npz",dem_times=times,dems=dems,gps=state,gps_dem=state_dem)


import os
import rasterio as rio
import numpy as np
import cv2

# Method 2, takes multiple tiff files and converts to a single JPG

file_name = "/Users/ben/Downloads/JD_TEMP/image_grab/image_grab"#"bottom_golf_0.6"

def saveAsSimpleImage(infile):
    # Read in as numpy array
    mask = infile.read(1) # Read first band
    #print(f"Count: {infile.count}")
    #print(f"Test: {type(mask)}")

    #Simplifies Float64 data to uint16 so it can be saved as a PNG
    mask2 = mask.astype(np.uint16)

    #cv2.imwrite('test.jpg', mask2)

with rio.open(f'{file_name}.R.tif') as infile:
    mask = infile.read(1)
    red = mask.astype(np.uint16)

with rio.open(f'{file_name}.B.tif') as infile:
    mask = infile.read(1)
    blue = mask.astype(np.uint16)

with rio.open(f'{file_name}.G.tif') as infile:
    mask = infile.read(1)
    green = mask.astype(np.uint16)

rgb = np.dstack((red,green,blue))

cv2.imwrite('test.jpg', rgb)


import sys
import os
import time
import random
import argparse

import torch
from torch.utils.data import DataLoader
import pickle

import numpy as np

from collections import Counter
from pathlib import Path
from IPython import embed

from model import Net, train_model, test_model
from dataset import ExploreDataset

from tqdm import tqdm

def main():
    # Parser
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch-size', type=int, default=64, metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--epochs', type=int, default=100, metavar='N',
                        help='number of epochs to train (default: 100)')
    parser.add_argument('--lr', type=float, default=0.001, metavar='LR',
                        help='learning rate (default: 0.001)')
    parser.add_argument('--momentum', type=float, default=0.5, metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda', action='store_true', default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed', type=int, default=1, metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument('--log-interval', type=int, default=500, metavar='N',
                        help='how many batches to wait before logging training status')

    parser.add_argument('--load', type=str, default=None, metavar='N')
    parser.add_argument('--auto', type=str, default=None, metavar='N', required=False)

    parser.add_argument('--save-model', action='store_true', default=False,
                        help='For Saving the current Model')
    args = parser.parse_args()
    use_cuda = not args.no_cuda and torch.cuda.is_available()

    # VARS
    INPUT_SIZE = 500
    OUTPUT_SIZE = 24725

    DATASET_MODE = 'dist'
    TEST_LOSS_FN = 'kl'

    # DATASET_MODE = 'max'
    # TEST_LOSS_FN = 'nll'

    torch.manual_seed(args.seed)
    device = torch.device("cuda" if use_cuda else "cpu")

    data_dir = Path("/local/scratch/ms2518/collected/")
    data_paths = list(data_dir.glob("*/*.pickle"))
    # data_paths = [Path("/local/scratch/ms2518/collected/explore/e0.pickle")]

    # AUTOENCODER
    model_auto = None
    if args.auto:
        model_auto = Autoencoder(input_size=INPUT_SIZE).to(device)
        print("* Loading autoencoder model: {0}".format(args.auto))
        model_auto.load_state_dict(torch.load(args.auto + ".model"))

        INPUT_SIZE = 200

    # PROCESS
    model = Net(input_size=INPUT_SIZE, output_size=OUTPUT_SIZE).to(device)

    # models_dir = Path(args.load)
    # model_paths = list(models_dir.glob("*.model"))
    model_paths = [Path("policy_models/run0/9.1558998717.model")]
    model_paths = sorted(model_paths, key=lambda x: int(str(x).split('/')[-1].split('.')[0]))

    last_opt_save_path = None
    for model_path in model_paths:
        print("*** Loading model: {0}".format(str(model_path)), end="\t")
        s_time = time.time()
        model.load_state_dict(torch.load(str(model_path)))
        print("\t(Loaded in {0:.2f}s)".format(time.time() - s_time))

        sp_time = time.time()

        acc_loss_dist = 0
        acc_loss_conf = 0
        acc_correct_dist = 0
        acc_correct_conf = 0
        acc_length = 0
        with tqdm(total=len(data_paths)) as pbar:
            for pickle_idx, pickle_path in enumerate(data_paths):
                sp_time = time.time()

                # TEST
                shard_dataset = ExploreDataset(pickle_path, output_size=OUTPUT_SIZE, mode=DATASET_MODE)
                test_loader = DataLoader(shard_dataset, batch_size=128, shuffle=True, num_workers=4)

                test_loss_dist, test_loss_conf, correct_dist, correct_conf = test_model(args, TEST_LOSS_FN, model, device, test_loader, autoencoder=model_auto, log=False)
                acc_loss_dist += test_loss_dist
                acc_loss_conf += test_loss_conf
                acc_correct_dist += correct_dist
                acc_correct_conf += correct_conf
                acc_length += len(test_loader.dataset)

                pbar.update(1)

        print("Test finished: {0:.2f}s".format(time.time() - sp_time))

        print('\nTest set: Average dist loss: {0:.4f}, Average conf loss: {1:.4f}, Accuracy dist: {2}/{6} ({3:.0f}%), Accuracy conf: {4}/{6} ({5:.0f}%)\n'.format(
            acc_loss_dist / acc_length,
            acc_loss_conf / acc_length,
            acc_correct_dist,
            100. * acc_correct_dist / acc_length,
            acc_correct_conf,
            100. * acc_correct_conf / acc_length,
            acc_length)
        )
        sys.stdout.flush()

if __name__ == "__main__":
    main()


#sample class
import nltk
from nltk.corpus import wordnet as wn
from nltk.corpus import stopwords
import pandas as pd
import string
import simil as si

#helper functions to get synonyms
englishStopWords = stopwords.words('english')
punctuationList = [character for character in string.punctuation]
undesirableTerms = set (englishStopWords + punctuationList)

def getSynonyms (term):
    synsetList = wn.synsets(term)
    allSynonyms = []
    for i in range (len(synsetList) - 1):
        synomyms = synsetList[i].lemma_names()
        allSynonyms = allSynonyms + synomyms
    return list(set(allSynonyms))

def getAllSynonyms (lists):
    synonyms = []
    for sublist in lists:
        for synonym in sublist:
            if synonym not in synonyms:
                synonyms.append(synonym)
    return synonyms

#this is the main class to manipulate targets
class Target:
    def __init__(self, targetNumber, targetText):
        self.targetNumber = targetNumber
        self.targetText = targetText
        #self.tokens = nltk.word_tokenize(self.targetText)
        #self.tokensNoStopWords = [token for token in self.tokens if token not in undesirableTerms]
        #self.lemmas = [nltk.WordNetLemmatizer().lemmatize(token) for token in self.tokens]
        #self.bigrams = [item for item in nltk.bigrams(self.tokensNoStopWords)]
        #self.trigrams = [item for item in nltk.trigrams(self.tokens)]
        #self.synonymsLists = list(map(getSynonyms , self.tokens)) #intermediary step, remove eventually
        #self.synonyms = getAllSynonyms(self.synonymsLists)

    def display(self):      #
        allValues = [self.targetNumber, self.targetText , self.tokens , self.lemmas, self.bigrams , self.trigrams, self.synonyms]
        print(allValues)

    def displayTarget(self):      #
        allValues = [self.targetNumber, self.targetText]
        print(allValues)

#########Targets class definition
## Helper class
def analyzeTargets (dataframe):
    """This function is used to process a dataframe with a lists of targets. The Targets will be analyzed and stored
    in a database or file for future use.
    """
    targets = {}
    for i in range(len(dataframe)):
        a = dataframe.iloc[i]['targetNumber']
        b = dataframe.iloc[i]['targetText']
        targets[a] = Target(a,b)
    return targets

class Targets:
    def __init__(self, csvfile):
        self.mydata = pd.read_csv(csvfile, encoding = "ISO-8859-1") #csv to dataframe
        self.targets = analyzeTargets(self.mydata)

    def printTargetList(self):
        return [self.targets[i].displayTarget() for i in self.targets]

class Comparer_1_to_1:
    def __init__ (self, nameItem1, elementIdItem1, nameItem2, elementIdItem2):
        ''' compares 2 targets of text to each other. The order matters
        e.g. item1 compared to item2 is NOT equal to the reverse.
        '''
        self.item1 = nameItem1.targets[elementIdItem1]
        self.item2 = nameItem2.targets[elementIdItem2]
        # self.tokenScore =  sum([i in self.item1.tokens for i in self.item2.tokens]) #/ len(self.item1.tokens)
        # try:
        #     self.lemmasScore =  sum([i in self.item1.lemmas for i in self.item2.lemmas]) #/ len(self.item1.lemmas)
        # except:
        #     self.lemmasScore = 0
        # try:
        #     self.tokensNoStopWordsScore =  sum([i in self.item1.tokensNoStopWords for i in self.item2.tokensNoStopWords]) / len(self.item1.tokensNoStopWords)
        # except:
        #     self.tokensNoStopWordsScore = 0
        #
        # self.commonTokens = [token for token in self.item1.tokensNoStopWords if token in self.item2.tokensNoStopWords]
        # self.commonBigrams = [bigram for bigram in self.item1.bigrams if bigram in self.item2.bigrams]
        #self.bigramsScore =  0.5 if len(self.commonBigrams)>=1 else 0.0
        # try:
        #     self.bigramsScore =  sum([i in self.item1.bigrams for i in self.item2.bigrams]) / len(self.item1.bigrams)
        # except:
        #     self.bigramsScore = 0
        # self.commonTrigrams = [trigram for trigram in self.item1.trigrams if trigram in self.item2.trigrams]
        # self.trigramsScore = 0.5 if len(self.commonBigrams)>=1 else 0.0
        # try:
        #     self.synonymsScore =  sum([i in self.item1.synonyms for i in self.item2.synonyms]) / len(self.item1.synonyms)
        # except:
        #     self.synonymsScore = 0
        #self.score = (self.trigramsScore + self.bigramsScore + self.tokensNoStopWordsScore + self.synonymsScore) / 3
        #self.score = self.tokensNoStopWordsScore + 2 * self.bigramsScore
        self.score = si.cosine_sim(self.item1.targetText, self.item2.targetText)
    def scores(self):
        return self.trigramsScore , self.bigramsScore , self.tokensNoStopWordsScore, self.synonymsScore

# def comparer (document , targets):
#
#     a = [document.targets[i].targetNumber for i in document.targets] #this works, is the list of identifyers
#     b = [targets.targets[i].targetNumber for i in targets.targets]
#
#     for item in b:     # this works, computes the score for each sentence against each target
#         print('\n Target : ')
#         print (targets.targets[item].displayTarget() )
#
#         for item2 in a :
#             if ts.Comparer_1_to_1(funkySDG  , item2 , SDGTargets , item ).score > 0.25:
#                 print (ts.Comparer_1_to_1(funkySDG  , item2 , SDGTargets , item ).score)
#                 print (funkySDG.targets[item2].displayTarget() )
#

##t###### Testing & cheatsheet
# SDGTargets = Targets('SDGtargets.csv')    #initialize with a csv file
# SDGTargets.printTargetList()              #works print all targets
#SDGTargets.targets[1].display()            #works prints one target with all its fields
#SDGTargets.targets[1].displayTarget()            #works prints one target with all its fields
#print(SDGTargets.targets[1].tokens)         #works prints the tokens of target '1'
#print(SDGTargets.targets[1].lemmas)         #works prints the lemmas of target '1'
#print(SDGTargets.targets[1].tokensNoStopWords)         #works prints the tokens of target '1' without stopwords
#finding strings in the text
# answer = 'eradicate' in SDGTargets.targets[1].tokens      #works
# print(answer)


from django.db import models
from django.utils.html import linebreaks

H1_TYPE = 'h1'
H2_TYPE = 'h2'
H3_TYPE = 'h3'
DIV_TYPE = 'div'
P_TYPE = 'div'
CONTENT_TYPES = (
    (H1_TYPE, H1_TYPE),
    (H2_TYPE, H2_TYPE),
    (H3_TYPE, H3_TYPE),
    (DIV_TYPE, DIV_TYPE),
    (P_TYPE, P_TYPE),
)

class Content(models.Model):
    type = models.CharField(choices=CONTENT_TYPES, max_length=64)
    content = models.TextField()
    weight = models.IntegerField()

    class Meta:
        ordering = ('weight',)

    def __str__(self):
        return "{type}, {content}".format(type=self.type, content=self.content[:20])

    def render_div(self):
        return linebreaks(self.content)

    @property
    def rendered_content(self):
        if self.type == DIV_TYPE:
            content = self.render_div()
        else:
            content = self.content

        return "<{type}>{content}</{type}>".format(type=self.type, content=content)


import my_functions
import pandas as pd
import seaborn as sns
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score, balanced_accuracy_score, roc_curve, auc, f1_score, precision_score, recall_score, cohen_kappa_score
import matplotlib.pyplot as plt

def calculate_f1score(input_file):
    """
    Compute F1 weighted for training and validation

    Arguments:
    input_file (str) -- Path to .csv that has the predictions with columns and Prediction, True_Label, Split

    Returns:
    f1_train (float) -- f1 weighted of training
    f1_val (float) -- f1 weighted of validation
    """
    # Read data and splits into training and vallidation
    df = pd.read_csv(input_file)
    df_train = df[ df["split"] == "training" ]
    df_val = df[ df["split"] == "validation" ]

    # Compute F1 Weighted Training
    y_pred = df_train["Prediction"]
    y_true = df_train["True_Label"]
    f1_train = f1_score(y_true, y_pred, average = "weighted")

    # Compute F1 Weighted Validation
    y_pred = df_val["Prediction"]
    y_true = df_val["True_Label"]
    f1_val = f1_score(y_true, y_pred, average = "weighted")

    return f1_train, f1_val

def calculate_satisfying_metric(input_file):
    """
    Compute auc, balanced accuracy, precision, recall for training and validation

    Arguments:
    input_file (str) -- Path to .csv that has the predictions with columns and Prediction, True_Label, Split
    """
    # Read data and splits into training and vallidation
    df = pd.read_csv(input_file)
    df_train = df[ df["split"] == "training" ]
    df_val = df[ df["split"] == "validation" ]

    # Training
    # Divide y_pred and y_true
    y_pred = df_train["Prediction"]
    y_true = df_train["True_Label"]

    # Compute Satisfying Metrics
    baccuracy_train = balanced_accuracy_score(y_true, y_pred)
    cohen_train = cohen_kappa_score(y_true, y_pred, weights="linear")

    # Validation
    # Compute F1 Weighted
    y_pred = df_val["Prediction"]
    y_true = df_val["True_Label"]

    # Compute Satisfying Metrics
    baccuracy_val = balanced_accuracy_score(y_true, y_pred)
    cohen_val = cohen_kappa_score(y_true, y_pred, weights="linear")

    return baccuracy_train, cohen_train, baccuracy_val, cohen_val

def make_predictions(input_file, model, format_type, index_column = "PassengerId", target_column = "Survived"):
    """
    Make predictions using a sklearn model and put the true label in the same df

    Arguments:
    input_file (str) -- Path to .csv that has the features of the previously trained model
    model -- Sklearn model previously trained
    format_type (str) -- Format type of the save data (csv or pickle)
    index_column -- Index column of the dataset
    target_column -- Target column of the dataset

    Returns:
    df -- Pandas Dataframe with three columns index, predictions and the true label
    """
    # Read the data
    if format_type == "csv":
        df = pd.read_csv(input_file).set_index(index_column)
    elif format_type == "pickle":
        df = pd.read_pickle(input_file)

    df.rename(columns = {"Survived":"True_Label"}, inplace = True) # To have a more meaningful name

    # Create X (train values with features already computed)
    X, _ = my_functions.create_features_target(input_file, target_column = target_column, index_column = index_column, format_type = format_type)

    # Make predictions
    y_true = df["True_Label"]
    y_pred = pd.DataFrame(model.predict(X), index=df.index, columns = ["Prediction"])

    df = pd.concat([y_pred, y_true], axis=1)

    # Label data into live and die to have a more meaningful name
    df["True_Label"] = df["True_Label"].apply(lambda x: 'die' if x == 0 else 'live')
    df["Prediction"] = df["Prediction"].apply(lambda x: 'die' if x <= 0.5 else 'live') #  It is set like this so it works for models that return probabilities, 0.5 serves as a threshold

    return df

def save_predictions(train_file, val_file, output_file, model, format_type, index_column = "PassengerId", target_column = "Survived"):
    """
    Make predictions on the train and validation data and saves it to a .csv

    Arguments:
    train_file (str) -- Path to .csv that has the features
    val_file (str) -- Path to .csv that has the features
    output_file (str) -- Path to .csv where the predictions are going to be saved
    index_column -- Index column of the dataset
    target_column -- Target column of the dataset
    """

    # Make predictions for training
    df_train = make_predictions(train_file, model, format_type, index_column, target_column)
    df_train["split"] = "training"

    # Make predictions for validation
    df_val = make_predictions(val_file, model, format_type, index_column, target_column)
    df_val["split"] = "validation"

    df = df_train.append(df_val)
    df.to_csv(output_file)

# Metrics Plots

def plot_roc_curve(model, X, y, title = "Receiver Operating Characteristic"):
    """
    Plot ROC curve

    Arguments:
    model -- Sklearn model already train
    X -- Data to predict on. The features has to be the same with which the model was trained on
    y -- target value of the X data
    title (str) -- title of the new plot

    Returns:
    matplotlib ROC curve
    """

    probs = model.predict_proba(X)
    preds = probs[:,1]
    fpr, tpr, threshold = roc_curve(y, preds) # calculate the fpr and tpr for all thresholds of the classification
    roc_auc = auc(fpr, tpr)

    # Graph
    plt.title(title)
    plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
    plt.legend(loc = 'lower right')
    plt.plot([0, 1], [0, 1],'r--')
    plt.xlim([0, 1])
    plt.ylim([0, 1])
    plt.ylabel('True Positive Rate')
    plt.xlabel('False Positive Rate')
    plt.show()

def plot_confusion_matrix(input_file, split):
    """
    Plot Confusion Matrix

    Arguments:
    input_file (str) -- Path to .csv that has the predictions with columns and Prediction, True_Label, Split
    split (str) -- training or validation
    Returns:
    Confusion Matrix matplotlib plot
    """

    df = pd.read_csv(input_file)
    df = df[df['split'] == split]

    labels = ['live', 'die']

    cm = confusion_matrix(df.True_Label, df.Prediction, normalize = 'true')
    cm = cm*100 # Multiply by 100 so te percentage is 99.7% instead of 0.997%
    cm = pd.DataFrame(cm, index=labels, columns=labels)

    # Customize heatmap (Confusion Matrix)
    sns.set(font_scale = 1.5)
    ax = sns.heatmap(cm, cmap='BuGn', annot=True, annot_kws={"size": 18,"weight":"bold"}, cbar=False, fmt='.3g')
    for t in ax.texts: t.set_text(t.get_text() + " %") # Put percentage in confusion matrix
    plt.xlabel('Predicted',weight='bold')
    plt.ylabel('Known',weight='bold')
    plt.yticks(rotation=0)
    plt.xticks(rotation=0)

def plot_confusion_matrices(input_file):
    """
    Plot Confusion Matrices of training and validation set

    Arguments:
    input_file (str) -- Path to .csv that has the predictions with columns and Prediction, True_Label, Split

    Returns:
    Confusion Matrix matplotlib plot
    """

    plt.rcParams['figure.figsize'] = [18, 5]
    plt.subplot(1,2,1)
    plot_confusion_matrix(input_file, "training")
    plt.title("Matrix Training")
    plt.subplot(1,2,2)
    plot_confusion_matrix(input_file, "validation")
    plt.title("Matrix Validation")

def plot_classification_report(input_file, split):
    """
    Plot Classification Report of sklearn

    Arguments:
    input_file (str) -- Path to .csv that has the predictions with columns and Prediction, True_Label, Split
    split (str) -- training or validation
    Returns:
    Classification Report plot
    """

    df = pd.read_csv(input_file)
    df = df[df['split'] == split]

    # Compute sklearn Classification Report and saves it in a pandas dataframe
    report = pd.DataFrame(classification_report(df.True_Label, df.Prediction, digits=3, output_dict=True)).transpose()
    report = report.loc[:, ["precision", "recall", "f1-score"]].drop(['accuracy', "macro avg"]) # Select what parts of the classification report to plot
    report = report*100 # Multiply by 100 so te percentage is 99.7% instead of 0.997%

    # Customize heatmap (Classification Report)
    sns.set(font_scale = 1.3)
    rdgn = sns.diverging_palette(h_neg=10, h_pos=130, s=80, l=62, sep=3, as_cmap=True)
    ax=sns.heatmap(report, cmap=rdgn, annot=True, annot_kws={"size": 14}, cbar=True, fmt='.3g', cbar_kws={'label':'%'}, center=90, vmin=0, vmax=100)
    ax.xaxis.tick_top()
    for t in ax.texts: t.set_text(t.get_text() + " %") #Put percentage
    plt.yticks(rotation = 0)

def plot_classification_reports(input_file):
    """
    Plot Classification Reports of training and validation set

    Arguments:
    input_file (str) -- Path to .csv that has the predictions with columns and Prediction, True_Label, Split
    Returns:
    Classification Report plot
    """

    plt.rcParams['figure.figsize'] = [18, 5]
    plt.subplot(1,2,1)
    plot_classification_report(input_file, split="training")
    plt.title("Training")
    plt.subplot(1,2,2)
    plot_classification_report(input_file, split="validation")
    plt.title("Validation")

def print_accuracies(input_file):
    """
    Print accuracy and balanced accuracy

    Arguments:
    input_file (str) -- Path to .csv that has the predictions with columns and Prediction, True_Label, Split
    Returns:
    accuracy and balanced accuracy
    """

    df = pd.read_csv(input_file)
    splits = ["training", "validation"]

    for split in splits:
        df_intermediate = df[df['split'] == split]
        print("The " + str(split) + " accuracy is: ", round(accuracy_score(df_intermediate.True_Label, df_intermediate.Prediction)*100,2), "%")
        print("The " + str(split) + " balanced accuracy is: ", round(balanced_accuracy_score(df_intermediate.True_Label, df_intermediate.Prediction)*100,2), "%")
        print()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import torch
import ctypes
import logging

from amd.rali.pipeline import Pipeline
import amd.rali.ops as ops
import amd.rali.types as types

import sys
import numpy as np
from enum import Enum
import cv2
import time

class COCOPipeline(Pipeline):
    def __init__(self, batch_size, num_threads, device_id, data_dir, ann_dir, crop, rali_cpu=True):
        super(COCOPipeline, self).__init__(batch_size, num_threads,
                                           device_id, seed=12 + device_id, rali_cpu=rali_cpu)
        self.input = ops.COCOReader(
            file_root=data_dir, annotations_file=ann_dir)
        rali_device = 'cpu' if rali_cpu else 'gpu'
        decoder_device = 'cpu' if rali_cpu else 'mixed'
        # device_memory_padding = 211025920 if decoder_device == 'mixed' else 0
        # host_memory_padding = 140544512 if decoder_device == 'mixed' else 0
        # self.decode = ops.ImageDecoderRandomCrop(device=decoder_device, output_type=types.RGB,
        # 											device_memory_padding=device_memory_padding,
        # 											host_memory_padding=host_memory_padding,
        # 											random_aspect_ratio=[0.8, 1.25],
        # 											random_area=[0.1, 1.0],
        # 											num_attempts=100)
        self.decode = ops.ImageDecoder(
            device=decoder_device, output_type=types.RGB)
        self.crop = ops.SSDRandomCrop(num_attempts=5)
        self.res = ops.Resize(device=rali_device, resize_x=crop, resize_y=crop)
        self.twist = ops.ColorTwist(device=rali_device)
        self.cmnp = ops.CropMirrorNormalize(device="gpu",
                                            output_dtype=types.FLOAT,
                                            output_layout=types.NCHW,
                                            crop=(crop, crop),
                                            image_type=types.RGB,
                                            mirror=0,
                                            mean=[0.485 * 255, 0.456 *
                                                  255, 0.406 * 255],
                                            std=[0.229 * 255, 0.224 * 255, 0.225 * 255])
        # Random variables
        self.rng1 = ops.Uniform(range=[0.5, 1.5])
        self.rng2 = ops.Uniform(range=[0.875, 1.125])
        self.rng3 = ops.Uniform(range=[-0.5, 0.5])
        print('rali "{0}" variant'.format(rali_device))

    def define_graph(self):
        saturation = self.rng1()
        contrast = self.rng1()
        brightness = self.rng2()
        hue = self.rng3()
        self.jpegs, self.bb, self.labels = self.input(name="Reader")
        images = self.decode(self.jpegs)
        images = self.crop(images)
        images = self.res(images)
        images = self.twist(images, saturation=saturation,
                            contrast=contrast, brightness=brightness, hue=hue)
        output = self.cmnp(images)
        return [output, self.bb, self.labels]

class RALICOCOIterator(object):
    """
    COCO RALI iterator for pyTorch.

    Parameters
    ----------
    pipelines : list of amd.rali.pipeline.Pipeline
                List of pipelines to use
    size : int
           Epoch size.
    """

    def __init__(self, pipelines, tensor_layout=types.NCHW, reverse_channels=False, multiplier=[1.0, 1.0, 1.0], offset=[0.0, 0.0, 0.0], tensor_dtype=types.FLOAT):

        # self._num_gpus = len(pipelines)
        assert pipelines is not None, "Number of provided pipelines has to be at least 1"

        self.loader = pipelines
        self.tensor_format = tensor_layout
        self.multiplier = multiplier
        self.offset = offset
        self.reverse_channels = reverse_channels
        self.tensor_dtype = tensor_dtype
        self.bs = self.loader._batch_size
        self.w = self.loader.getOutputWidth()
        self.h = self.loader.getOutputHeight()
        self.n = self.loader.getOutputImageCount()
        self.rim = self.loader.getRemainingImages()
        print("____________REMAINING IMAGES____________:", self.rim)
        color_format = self.loader.getOutputColorFormat()
        self.p = (1 if color_format is types.GRAY else 3)
        if self.tensor_dtype == types.FLOAT:
            self.out = np.zeros(
                (self.bs*self.n, self.p, int(self.h/self.bs), self.w,), dtype="float32")
        elif self.tensor_dtype == types.FLOAT16:
            self.out = np.zeros(
                (self.bs*self.n, self.p, int(self.h/self.bs), self.w,), dtype="float16")

    def next(self):
        return self.__next__()

    def __next__(self):
        print("In the next routine of COCO Iterator")
        if(self.loader.isEmpty()):
            timing_info = self.loader.Timing_Info()
            print("Load     time ::", timing_info.load_time)
            print("Decode   time ::", timing_info.decode_time)
            print("Process  time ::", timing_info.process_time)
            print("Transfer time ::", timing_info.transfer_time)
            raise StopIteration

        if self.loader.run() != 0:
            raise StopIteration
        self.lis = []  # Empty list for bboxes
        self.lis_lab = []  # Empty list of labels

        if(types.NCHW == self.tensor_format):
            self.loader.copyToTensorNCHW(
                self.out, self.multiplier, self.offset, self.reverse_channels, int(self.tensor_dtype))
        else:
            self.loader.copyToTensorNHWC(
                self.out, self.multiplier, self.offset, self.reverse_channels, int(self.tensor_dtype))

        self.img_names_length = np.empty(self.bs, dtype="int32")
        self.img_names_size = self.loader.GetImageNameLen(self.img_names_length)
        print("Image name length:",self.img_names_size)
# Images names of a batch
        self.Img_name = self.loader.GetImageName(self.img_names_size)
        print("Image names in a batch ",self.Img_name)
#Count of labels/ bboxes in a batch
        self.bboxes_label_count = np.zeros(self.bs, dtype="int32")
        self.count_batch = self.loader.GetBoundingBoxCount(self.bboxes_label_count)
        print("Count Batch:",self.count_batch)
# 1D labels array in a batch
        self.labels = np.zeros(self.count_batch, dtype="int32")
        self.loader.GetBBLabels(self.labels)
        print(self.labels)
# 1D bboxes array in a batch
        self.bboxes = np.zeros((self.count_batch*4), dtype="float32")
        self.loader.GetBBCords(self.bboxes)
        print(self.bboxes)
#Image sizes of a batch
        self.img_size = np.zeros((self.bs * 2),dtype = "int32")
        self.loader.GetImgSizes(self.img_size)
        print("Image sizes:",self.img_size)
        count =0
        sum_count=0
        for i in range(self.bs):
            count = self.bboxes_label_count[i]
            print("labels:",self.labels[sum_count : sum_count+count])
            print("bboxes:",self.bboxes[sum_count*4 : (sum_count+count)*4])
            print("Image w & h:",self.img_size[i*2:(i*2)+2])
            print("Image names:",self.Img_name[i*16:(i*16)+12])
            self.img_name = self.Img_name[i*16:(i*16)+12]
            self.img_name=self.img_name.decode('utf_8')
            self.img_name = np.char.lstrip(self.img_name, chars ='0')
            print("Image names:",self.img_name)
            self.label_2d_numpy = (self.labels[sum_count : sum_count+count])
            self.label_2d_numpy = np.reshape(self.label_2d_numpy, (-1, 1)).tolist()
            self.bb_2d_numpy = (self.bboxes[sum_count*4 : (sum_count+count)*4])
            self.bb_2d_numpy = np.reshape(self.bb_2d_numpy, (-1, 4)).tolist()
            self.lis_lab.append(self.label_2d_numpy)
            self.lis.append(self.bb_2d_numpy)
            sum_count = sum_count +count

        self.target = self.lis
        self.target1 = self.lis_lab

        max_cols = max([len(row) for batch in self.target for row in batch])
        max_rows = max([len(batch) for batch in self.target])
        self.bb_padded = [
            batch + [[0] * (max_cols)] * (max_rows - len(batch)) for batch in self.target]
        self.bb_padded = torch.FloatTensor(
            [row + [0] * (max_cols - len(row)) for batch in self.bb_padded for row in batch])
        self.bb_padded = self.bb_padded.view(-1, max_rows, max_cols)
        # print(self.bb_padded)

        max_cols1 = max([len(row) for batch in self.target1 for row in batch])
        max_rows1 = max([len(batch) for batch in self.target1])
        self.labels_padded = [
            batch + [[0] * (max_cols1)] * (max_rows1 - len(batch)) for batch in self.target1]
        self.labels_padded = torch.LongTensor(
            [row + [0] * (max_cols1 - len(row)) for batch in self.labels_padded for row in batch])
        self.labels_padded = self.labels_padded.view(-1, max_rows1, max_cols1)
        # print(self.labels_padded)

        if self.tensor_dtype == types.FLOAT:
            return torch.from_numpy(self.out), self.bb_padded, self.labels_padded
        elif self.tensor_dtype == types.FLOAT16:
            return torch.from_numpy(self.out.astype(np.float16)), self.bb_padded, self.labels_padded

    def reset(self):
        self.loader.raliResetLoaders()

    def __iter__(self):
        self.loader.raliResetLoaders()
        return self

def main():
    if len(sys.argv) < 5:
        print('Please pass the folder image_folder Annotation_file cpu/gpu batch_size')
        exit(0)

    image_path = sys.argv[1]
    ann_path = sys.argv[2]
    if(sys.argv[3] == "cpu"):
        _rali_cpu = True
    else:
        _rali_cpu = False
    bs = int(sys.argv[4])
    nt = 1
    di = 0
    crop_size = 224
    pipe = COCOPipeline(batch_size=bs, num_threads=nt, device_id=di,
                        data_dir=image_path, ann_dir=ann_path, crop=crop_size, rali_cpu=_rali_cpu)
    pipe.build()
    imageIterator = RALICOCOIterator(
        pipe, multiplier=pipe._multiplier, offset=pipe._offset)
    for i, it in enumerate(imageIterator, 0):
        print("**************", i, "*******************")
        print("**************starts*******************")
        print("\nBBOXES:\n", it[1])
        print("\nLABELS:\n", it[2])
        print("**************ends*******************")
        print("**************", i, "*******************")

if __name__ == '__main__':
    main()


from __future__ import absolute_import, division, print_function, unicode_literals

from hal.eth import UdpComm
import threading
import datetime

class battery():
    """
class to retrieve battery state
    """
    def eventReceiver(self, message):
        if message.startswith(self.battery_name + b":"):
            self.status = message + b" - " + str.encode(str(datetime.datetime.now()))
            self.Statusevent.set()

    def __init__(self, name, udp):
        self.battery_name = name
        self.UDP = udp
        self.Statusevent = threading.Event()
        self.status = b""

    def charge(self):
        self.Statusevent.clear()
        msg = b"%s.charge" % self.battery_name
        self.UDP.send_udp(msg)

        #wait for asynchronous callback with data
        if self.Statusevent.wait(10) != True:
            print(u"error: did not receive status within 10 seconds")
        return self.status


import os, pathlib

# SOURCE_PATH = 'D:/Pictures/All/Special/'
SOURCE_PATH = 'D:/Pictures/'

def printContents(directory):
    dirCount = 0
    fileCount = 0
    for root, dirs, files in os.walk(directory):
        level = root.replace(directory, '').count(os.sep)
        indent = "    " * level
        print(f"{indent}{pathlib.PureWindowsPath(root)}")
        dirCount += 1
        fileCount += len(files)
        for file in files:
            print(f"{indent}    {file}")
    print('{} directories (including root) and {} files'.format(dirCount, fileCount))

printContents(SOURCE_PATH)


from PyQt5.QtCore import QSize

from API.Database.Models.Users import DBModel_Users
from GUI.Server.Users.addDialog import AddDialog
from Source.GUI.MessageBoxes.default_box import DefaultMessageBox
from Source.GUI.Widgets.Dialog import MYDialog
from Source.GUI.Widgets.ModuleButtonContainer import MYButtonContainer
from Source.GUI.Widgets.TableView import MYTableView
from Source.GUI.Widgets.Widget import MYWidget
from Source.GUI.icons import ICON
from Source.namespace import DPL

class UserManager(MYDialog):
    def __init__(self, db, parent=None):
        super(UserManager, self).__init__(
            parent=parent,
            layout='V',
            title='Пользователи',
            layout_spacing=0,
            layout_margins=[0, 0, 0, 0]
        )
        self.__init_attributes(db)
        self.__init_layouts()
        self.__init_widgets()
        self.__init_tools()
        self.__init_parameters()
        self.__init_layouting()
        self.__init_connects()
        self.__init_styleSheets()
        self.__init_effects()

    def __init_attributes(self, db):
        self.__model = DBModel_Users(db, self)

    def __init_layouts(self):
        pass

    def __init_widgets(self):
        self.__messagebox = DefaultMessageBox(self)
        self.__add_dialog = AddDialog(self)
        self.__view = MYTableView(self)
        self.__tools = MYButtonContainer(
            btn_size=QSize(40, 40),
            btn_icon_size=QSize(30, 30),
            icondelta=QSize(10, 10),
            bluradius=10
        )

    def __init_tools(self):
        self.__tools.addTool(
            'add',
            ICON.DEFAULT.add(),
            'Добавить пользователя',
            self.addUser
        )
        self.__tools.addTool(
            'remove',
            ICON.DEFAULT.remove(),
            'Улалить пользователя',
            self.removeUser
        )

    def __init_parameters(self):
        self.resize(400, 500)
        self.close_btn.hide()
        self.__view.setModel(self.__model)

    def __init_layouting(self):
        self.main_layout.addWidget(self.__tools)
        self.main_layout.addWidget(self.__view)

    def __init_connects(self):
        pass

    def __init_styleSheets(self):
        self.__tools.setStyleSheet(
            """
            border: 0px solid rgba(0, 200, 250, 150);
            border-bottom: 0px solid rgba(0, 200, 250, 150);
            border-top: 0px solid rgba(0, 200, 250, 150);
            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                        stop: 0.0 rgb(205, 200, 195),
                                        stop: 1.0 rgb(130, 120, 115));
            """
        )

    def __init_effects(self):
        pass

    def addUser(self):
        if self.__add_dialog.exec_():
            user = self.__add_dialog.user
            error = self.__model.addUser(user)
            descript = self.__model.addUserErrors[error]
            if error != 0:
                if self.__messagebox.showMessage(
                    title='Новый пользователь',
                    type=DPL.DefaultMessageTypes.error,
                    theme='Ошибка добавления',
                    descript=descript
                ):
                    self.addUser()
                else:
                    self.__add_dialog.clear()
            else:
                self.__messagebox.showMessage(
                    title='Новый пользователь',
                    type=DPL.DefaultMessageTypes.success,
                    theme='Успешное добавление',
                    descript=descript
                )

    def removeUser(self):
        index = self.__view.selectedIndexes()
        if index:
            if self.__messagebox.showMessage(
                    title='Удаление пользователя',
                    type=DPL.DefaultMessageTypes.question,
                    theme='Подтверждение',
                    descript='Вы действительно хотите удалить выбранного пользователя'
            ):
                index = index[0]
                row = index.row()
                self.__model.removeUser(row)

if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication
    from API.Database.database import Database
    db = Database()
    app = QApplication([])
    win = UserManager(db)
    win.show()
    sys.exit(app.exec_())


"""CSC148 Lab 4: Abstract Data Types

=== CSC148 Winter 2023 ===
Department of Computer Science,
University of Toronto

=== Module Description ===
This module runs timing experiments to determine how the time taken
to enqueue and push take for LinkedList implementations of the ADTs
"""
from timeit import timeit
from typing import List, Tuple
from linked_list_adts import LinkedListStack, LinkedListQueue

###############################################################################
# Running timing experiments
#
# In this part of the lab, you will be conducting timing experiments on Stack
# and Queue methods when they're implemented with Linked Lists.
###############################################################################
# Experiment Parameters
# Below are our experiment settings: you may want to change these values.
#
# QUEUE_SIZES: This represents the queue sizes we'll be experimenting with.
#              i.e. enqueueing and dequeueing from Queues with size 10000,
#              20000, etc.
# NUM_TRIALS:  This represents the number of times we will repeat an
#              experiment: when we run our timing experiments, we want to get
#              use the average time over a number of trials in order to
#              minimize the effect of any outliers.
###############################################################################
SIZES = [100, 200, 400, 800, 1600]
NUM_TRIALS = 20

def _setup_queues(size: int, n: int) -> List[LinkedListQueue]:
    """Return a list of <n> queues, each with <size> elements.
    """
    queue_list = []
    for _ in range(n):
        q = LinkedListQueue()
        for _ in range(size):
            q.enqueue(1)
        queue_list.append(q)

    return queue_list

def _setup_stacks(size: int, n: int) -> List[LinkedListStack]:
    """Return a list of <n> stacks, each with <size> elements.
    """
    stack_list = []
    for _ in range(n):
        s = LinkedListStack()
        for _ in range(size):
            s.push(1)
        stack_list.append(s)

    return stack_list

def time_methods() -> Tuple[List[float], List[float]]:
    """Run timing experiments for enqueue on LinkedListQueue and
    push for LinkedListStack, returning lists with the average time it took to
    enqueue a single element to queues with sizes QUEUE_SIZES over
    NUM_TRIALS trials."""
    # These two lists will hold our timing results.
    queue_times = []
    stack_times = []

    # This loop runs the timing experiment for enqueueing one item to
    # LinkedListQueue.
    print("Running LinkedListQueue.enqueue experiments...")
    for size in SIZES:
        # 1. Initialize the sample queues
        queues = _setup_queues(size, NUM_TRIALS)

        # 2. For each queue created, call the function timeit.
        #    timeit takes three arguments:
        #        - a *string* representation of a piece of code to run
        #        - the number of times to run it (just 1 for us)
        #        - globals is a technical argument that you DON'T need to
        #          care about
        time = 0
        for queue in queues:
            time += timeit('queue.enqueue(1)', number=1, globals=locals())

        # 3. Get the average time in microseconds (μs)
        average_time = time / NUM_TRIALS * 1e6

        # 4. Report the average time taken and add that to our list of
        #    results.
        queue_times.append(average_time)
        print(f'enqueue: Queue size {size:>7}, time: {average_time}')

    print("Running LinkedListStack.push experiments...")
    # TODO: Using the above code as an example, run the same experiment
    #       but on LinkedListStack.push
    #       (You can just copy the above code and make minor modifications!)
    #       Add the results to stack_times instead of queue_times

    # Do not change the return statement below.
    return queue_times, stack_times

def plot_experiment() -> None:
    """Run the timing experiment on LinkedListQueue and LinkedListStack
     and plot a graph."""
    import matplotlib.pyplot as plt

    # Run the experiments and store the results
    queue_times, stack_times = time_methods()

    # Plot the results of our experiments and assign labels to each plot.
    # Our call to plt.plot takes 3 arguments:
    #     - The x-coordinates of the values to plot
    #     - The y-coordinates of the values to plot
    #     - The format we want to plot with.
    #       'ro' is 'red circle'
    #       'bo' is 'blue circle'
    #       Other formats include 'rx' (red X), 'bx' (blue X) and many more!
    start_plt, = plt.plot(SIZES, queue_times, 'ro')
    start_plt.set_label("LinkedListQueue.enqueue")

    end_plt, = plt.plot(SIZES, stack_times, 'bo')
    end_plt.set_label("LinkedListStack.push")

    # After we finish plotting everything, we can create the legend of
    # our graph and label the axes
    plt.legend()
    plt.xlabel("Size")
    plt.ylabel("Average Time (μs)")

    # Show our plotted results. This line must be called after
    # all of the other setup.
    plt.show()

if __name__ == '__main__':
    time_methods()

    # # Uncomment the line below to see the plotted graph once you have
    # # time_enqueue() working.
    # plot_experiment()


import random
from collections import namedtuple

STATS = ['HP', 'ATK', 'DEF', 'SPA', 'SPD', 'SPE']
ETC = ['nature', 'ability', 'item']
Pokemon = namedtuple('Pokemon', STATS + ETC)

def choose_stats(father, mother):
    '''Returns an IV spread for a child given pokemon mother and father as the parents.'''
    baby = {}
    baby['nature'] = random.choice((father.nature, mother.nature))
    baby['ability'] = random.choice((father.ability, mother.ability))
    baby['item'] = ''

    stats = list(STATS)
    random.shuffle(stats)
    if 'destiny knot' in (father.item.lower(), mother.item.lower()):
        inherited_stats = stats[:5]
        for stat in inherited_stats:
            chosen_parent = random.choice((mother, father))
            baby[stat] = getattr(chosen_parent, stat)
        baby[stats[5]] = random.randint(0, 31)
    else:
        for index, stat in enumerate(stats[:3]):
            baby[stat] = getattr(father, stat)

        for index, stat in enumerate(stats[3:]):
            baby[stat] = getattr(mother, stat)

    return Pokemon(**baby)

vespa = Pokemon(31, 31, 31, 31, 31, 0, 'Modest', 'Flame Body', '')
dknot_vespa = Pokemon(31, 31, 31, 31, 31, 31, 'Modest', 'Flame Body', 'Destiny Knot')


 #imports

import tensorflow as tf
import pickle
import matplotlib.pyplot as plt
import cv2
import numpy as np

#opening migrated data stored in the pickle files

classes = ['AbnormalEEG' , 'NormalEEG']

pickle_in = open("x_train.pickle", "rb")
x_train = pickle.load(pickle_in)
pickle_in.close()

pickle_in = open("y_train.pickle" , "rb")
y_train = pickle.load(pickle_in)
pickle_in.close()

print(len(x_train))
print(x_train.shape)
print(x_train[0].shape)

plt.imshow(x_train[0].reshape(30,30))
plt.show()

#for i in range(10):
#	cv2.imshow("img", x_train[i])
#	cv2.waitKey(0)

X = x_train[0]
nodes = 50

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape = (30, 30, 1)))
model.add(tf.keras.layers.Dense(nodes, activation = tf.nn.sigmoid))
model.add(tf.keras.layers.Dense(nodes, activation = tf.nn.sigmoid))
model.add(tf.keras.layers.Dense(2, activation = tf.nn.softmax))

#compiling / importing in the training data

model.compile(optimizer = "adam",
            loss = "sparse_categorical_crossentropy",
            metrics = ['accuracy'])

model.fit(x_train, y_train, epochs = 20)

import os

direc = "C:\\Users\\HP\\Desktop\\Science Fair Project\\Image Data\\Testing\\"
c_ = 0

for c in classes:
    direc_ = ''.join((direc, c))
    for file in os.listdir(direc_):
        filex = os.path.join(direc_, file)
        r = cv2.imread(filex)
        r.resize(30,30)
        pred = model.predict(r.reshape(-1,30,30,1))
        pred = np.argmax([pred])
        print(file, pred, c)
        if classes[pred] == c:
            c_ += 1
print("accuracy : " + str(float(c_/100)))


import logging
import socket
from z_channel import ZChannel
from z_buffer import ZBuffer

class ZTcpConnection(object):
    DISCONNECTED = 0
    CONNECTING = 1
    CONNECTED = 2
    DISCONNECTING = 3
    def __init__(self, event_loop, name, sock, peer_addr):
        self._event_loop = event_loop
        self._name = name
        self._socket = sock
        self._peer_addr = peer_addr
        self._channel = ZChannel(event_loop, sock.fileno())
        self._channel.set_read_callback(self.handle_read)
        self._channel.set_write_callback(self.handle_write)
        self._channel.set_close_callback(self.handle_close)
        self._channel.set_error_callback(self.handle_error)
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
        self._high_watermark = 64 * 1024 * 1024
        self._state = self.CONNECTING
        self._connection_callback = None
        self._message_callback = None
        self._close_callback = None
        self._write_complete_callback = None
        self._high_watermark_callback = None
        self._context = None
        self._input_buffer = ZBuffer()
        self._output_buffer = ZBuffer()

    def name(self):
        return self._name

    def get_event_loop(self):
        return self._event_loop

    def set_connection_callback(self, cb):
        self._connection_callback = cb

    def set_message_callback(self, cb):
        self._message_callback = cb

    def set_close_callback(self, cb):
        self._close_callback = cb

    def set_write_complete_callback(self, cb):
        self._write_complete_callback = cb

    def set_high_watermark_callback(self, cb):
        self._high_watermark_callback = cb

    def _write_complete_callback_wrapper(self):
        self._write_complete_callback(self)

    def set_context(self, ct):
        self._context = ct

    def get_context(self):
        return self._context

    def send(self, data):
        def send_in_loop_wrapper():
            self.send_in_loop(data)

        if self._state == self.CONNECTED:
            if self._event_loop.is_in_loop_thread():
                self.send_in_loop(data)
            else:
                self._event_loop.run_in_loop(send_in_loop_wrapper)

    def send_in_loop(self, data):
        self._event_loop.assert_in_loop_thread()

        if self._state == self.DISCONNECTED:
            logging.warn('disconnected connection, give up writing')
            return

        siz = len(data)
        remaining = siz
        has_fatal_error = False
        try:
            if not self._channel.is_writing() and self._output_buffer.readable_bytes() == 0:
                nwritten = self._socket.send(data)
                remaining = siz - nwritten
                if remaining == 0 and self._write_complete_callback:
                    self._event_loop.queue_in_loop(self._write_complete_callback_wrapper)
        except socket.error as e:
            logging.error('ZTcpConnection.send_in_loop fail to write with error %s' %str(e))
            if e.errno != errno.EWOULDBLOCK:
                if e.errno == errno.EPIPE:
                    has_fatal_error = True

        if not has_fatal_error and remaining > 0:
            logging.info('ZTcpConnection.send_in_loop I am going to write more data')
            old_len = self._output_buffer.readable_bytes()
            if old_len + remaining >= self._high_watermark and \
               old_len < self._high_watermark and \
               self._high_watermark_callback:
                siz = old_len + remaining
                def high_watermark_callback_wrapper():
                    self._high_watermark_callback(self, siz)
                self._event_loop.queue_in_loop(_high_watermark_callback_wrapper)
                self._output_buffer.append(data[nwritten:])
                if not self._channel.is_writting():
                    self._channel.enable_writing()

    def handle_read(self, receive_time):
        self._event_loop.assert_in_loop_thread()
        # FIXME recv_into
        try:
            data = self._socket.recv(65536)
            if data:
                self._input_buffer.append(data)
                self._message_callback(self, self._input_buffer, receive_time)
            else:
                self.handle_close()
        except socket.error as e:
            logging.error('ZTcpConnection.handle_read fail to read with error %s' %str(e))
            self.handle_error()

    def handle_write(self):
        self._event_loop.assert_in_loop_thread()
        if self._channel.is_writing():
            try:
                n = self._socket.send(self._ouput_buffer.tostring())
                self._output_buffer.retrieve(n)
                if self._output_buffer.readable_bytes() == 0:
                    self._channel.disable_writing()
                    if self._write_complete_callback:
                        self._event_loop.queue_in_loop(self._write_complete_callback_wrapper)

                    if self._state == self.DISCONNECTING:
                        self.shutdown_in_loop()
            except socket.error as e:
                logging.error('ZTcpConnection.handle_write fail to write with error %s' %str(e))
        else:
            logging.info('ZTcpConnection.handle_write connection is down, no more writing')

    def _set_state(self, st):
        self._state = st

    def connected(self):
        return self._state == self.CONNECTED

    def handle_close(self):
        self._event_loop.assert_in_loop_thread()
        assert self._state == self.CONNECTED or self._state == self.DISCONNECTING
        self._set_state(self.DISCONNECTED)
        self._channel.disable_all()
        self._connection_callback(self)
        self._close_callback(self)

    def handle_error(self):
        logging.error('ZTcpConnection.handle_error error happened')

    def shutdown_write(self):
        if self._state == self.CONNECTED:
            self._set_state(self.DISCONNECTING)
            self._event_loop.run_in_loop(self.shutdown_write_in_loop)

    def shutdown(self):
        if self._state == self.CONNECTED:
            self._set_state(self.DISCONNECTING)
            self._event_loop.run_in_loop(self.shutdown_in_loop)

    def shutdown_write_in_loop(self):
        self._event_loop.assert_in_loop_thread()
        # FIXME what if channel is writing ?
        if not self._channel.is_writing():
            self._socket.shutdown(socket.SHUT_WR)

    def shutdown_in_loop(self):
        self._event_loop.assert_in_loop_thread()
        # FIXME what if channel is writing ?
        if not self._channel.is_writing():
            self._socket.shutdown(socket.SHUT_RDWR)

    def set_tcp_no_delay(on):
        if on:
            self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        else:
            self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 0)

    def connection_established(self):
        self._event_loop.assert_in_loop_thread()
        assert self._state == self.CONNECTING
        self._set_state(self.CONNECTED)
        self._channel.tie(self)
        self._channel.enable_reading()
        self._connection_callback(self)

    def connection_destroyed(self):
        self._event_loop.assert_in_loop_thread()
        if self._state == self.CONNECTED:
            self._set_state(self.DISCONNECTED)
            self._channel.disable_all()
            self._connection_callback(self)
        self._channel.unregister()


# -*- coding: utf-8 -*-
from pymongo import MongoClient

conn = MongoClient("localhost",27017)
db = conn.job

transitlist = db.transit.find({"routes.duration":{"$lt":2000}})

companylist = list(db.companylist.find({"CompanyId":{"$in":[x["CompanyId"] for x in transitlist]}}))
print(len(companylist))

joblist = list(db.joblist.find({"CompanyId":{"$in":[x["CompanyId"] for x in companylist]}}))

with open("c:/joblist.txt","w") as f:
    f.write(str(joblist))


from pioneer_sdk import Pioneer
import pygame
import math
import numpy as np
import cv2

from Joystick import Joystick

#######################
"""Параметры Пионера"""
#######################
# начальные координаты(при взлете) в метрах
x = float(0)
y = float(0)
z = float(1)
yaw = math.radians(float(0))

# прирост координат
increment_xy = float(0.1)
increment_z = float(0.025)
increment_deg = math.radians(float(8))

# флаг отправки команды перемещения
new_command = False

#######################
"""Параметры экрана"""
#######################
FPS = 30
W = 640  # ширина экрана
H = 480  # высота экрана

###########################
"""Инициализация Пионера"""
###########################
pioneer_mini = Pioneer(logger= True) # инициализируем пионера
print('start')
pioneer_mini.arm() # запуск моторов
pioneer_mini.takeoff() # предстартовые проверки

pygame.init() # Иницилизация пугейм
pygame.key.set_repeat(1, 20) # Включение обработки зажатой клавиши

sc = pygame.display.set_mode((W, H))
clock = pygame.time.Clock()

##############################
"""Инициализация джойстиков"""
##############################
joy = Joystick()

if __name__ == '__main__':
    running = True
    while running:
        # обработка остановочных нажатий
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False

        x, y, z, yaw = joy.joyhandler(x, y, z, yaw) # получение координат

        print(round(x, 2), round(y, 2), round(z, 2), round((yaw * 180 / 3.14), 2))  # Вывод координат (просто для удобства)

        # если координаты изменились, то отправляем их
        if joy.new_command:
            pioneer_mini.go_to_local_point(x=round(x, 2), y=round(y, 2), z=round(z, 2), yaw=round(yaw, 2))
            #pioneer_mini.send_rc_channels()
            joy.new_command = False

        # вывод картинки с камеры
        #frame_cv2 = cv2.imdecode(np.frombuffer(pioneer_mini.get_raw_video_frame(), dtype=np.uint8), cv2.IMREAD_COLOR)
        #frame_pygame = pygame.image.frombuffer(frame_cv2.tostring(), (640, 480), "RGB")
        #sc.blit(frame_pygame, (0, 0))

        pygame.display.update()

        clock.tick(FPS)

    pygame.quit()  # Завершение работы
    pioneer_mini.land()


import cv2
import os
import numpy as np

def color_to_bw(color, threshold):
	black_and_white = []
	for image in color:
		bw = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
		_, bw = cv2.threshold(bw, threshold, 255, cv2.THRESH_BINARY)
		black_and_white.append(bw)
	return black_and_white

def median_blur(to_filter, k):
	ret = []
	for image in to_filter:
		median = cv2.medianBlur(image, k)
		ret.append(median)
	return ret

def getImages(path):
	images = []
	files = os.listdir(path)
	for filename in files:
		image = cv2.imread(os.path.join(path, filename))
		images.append(image)
	return images

def rotate(myList, pos):
	return np.concatenate((myList[pos:], myList[:pos]), axis=0)

#This function takes in the beginning and ending of one vector, and returns
#an iterator representing the point where the first item in the second vector is.
def find_first_in(contour, corners):
	for i in range(len(contour)):
		for j in range(len(corners)):
			if ( contour[i][0][0] == corners[j][0][0] and contour[i][0][1] == corners[j][0][1]):
				return i
	return len(contour)

#This returns iterators from the first vector where the value is equal places in the second vector.
def find_all_in(contour, corners):
	places = []
	for i in range(len(contour)):
		for j in range(len(corners)):
			if ( contour[i][0][0] == corners[j][0][0] and contour[i][0][1] == corners[j][0][1]):
				places.append(i)
	return places

def remove_duplicates(vec):
	vec = vec.tolist()
	dupes_found = True;
	while(dupes_found):
		dupes_found = False;
		dup_at = -1;
		for i in range(len(vec)):
			for j in range(len(vec)):
				if(j==i):
					continue
				if(vec[i][0][0] == vec[j][0][0] and vec[i][0][1] == vec[j][0][1]):
					dup_at = j
					dupes_found = True;
					del vec[j]
					break
			if(dupes_found):
				break
	return  np.array(vec)

#Euclidian distance between 2 points.
def distance(a, b):
	return cv2.norm(a-b)

def translate_contour(cnt, offset_x, offset_y):
	ret_contour = []
	offset = (offset_x,offset_y)

	for i in range(len(cnt)):
		x = int(cnt[i][0][0]+offset_x+0.5)
		y = int(cnt[i][0][1]+offset_y+0.5)
		ret_contour.append((x,y))
	return ret_contour;

def filter(to_filter, size):
	morphology = []
	kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(size,size))
	for image in to_filter:
		image = cv2.morphologyEx(image,cv2.MORPH_OPEN,kernel)
		image = cv2.morphologyEx(image,cv2.MORPH_CLOSE,kernel)
		morphology.append(image)
	return morphology

def draw_points(image, points, color = [0,0,255]):
	for p in points:
		cv2.circle(image,(p[0][0],p[0][1]),3,color,-1)
	return image


from json import loads
from Logitech.logitechUtilities import getNormalized, getKeyBoxs

#---CONFIG---"
_CONFIGFILE = "logitech/logitechConfig.json"
with open(_CONFIGFILE, "r") as fs: data = loads(fs.read())

#---DATA---"
KEYS = data["keyConfig"]
NB_KEYS = data["nbKeys"]

NBHUES = data["hues"]
SLICES = data["slices"]
SLICESLEN = len(data["slices"])

NORMALIZED = getNormalized(KEYS)
KEYBOXS = getKeyBoxs()


import logging
import json
import os

from flask import Flask, request, redirect, session, url_for, jsonify
from pyzephyr.session.zephyr_session import ZephyrSession
from pyzephyr.config.config import get_credentials, get_configuration
from pyzephyr.stories import EpicStorybySprint

app = Flask(__name__)

credentials_file = 'client_credentials.json'
credentials_name = 'zephyr2'

config_file = 'var/vsts_config.json'

@app.route('/')
def index():
    """ Create session

    :return:
    """

    zephyr_creds = get_credentials(credentials_file, credentials_name)

    zephyr_endpoint = get_configuration(config_file)

    vsts_session = ZephyrSession(endpoint=zephyr_endpoint, creds=zephyr_creds)

    session['vsts_session'] = vsts_session.serialize()

    return "
Zephyr!
"

@app.route("/dashboard", methods=["GET"])
def dashboard():

    serialized_vsts_session = session['vsts_session']
    vsts_session = ZephyrSession(**serialized_vsts_session)

    query = EpicStorybySprint(epic_path="NL\\NARS Flowpath",
                              story_path="NL\\App Programs",
                              sprint="NL\Sprint 8",
                              session=vsts_session)

    query.refresh()

    session['vsts_session'] = vsts_session.serialize()

    j = query.__str__()
    jl = [v for v in j.values()]
    return jsonify(jl)

def find_oldest_ancestor(story):
    return None

if __name__ == "__main__":

    os.environ['DEBUG'] = "1"
    app.secret_key = os.urandom(24)

    logging.basicConfig(
        filename='var/pyzephyr.log',
        level=logging.DEBUG,
        format='%(asctime)s  %(process)-7s %(name)-20s %(message)s',
        datefmt='%m/%d/%Y %H:%M:%S'
    )
    log = logging.getLogger("pyzephyr")
    log.addHandler(logging.NullHandler())
    log.info('###### Starting PyZephyr App #####')

    app.run(debug=False, port=4998)


"""
Run as :
pytest -v --tb=line --language=en test_main_page.py
pytest -v --tb=line -m login_guest test_main_page.py
pytest -s -v --tb=line -m basket test_main_page.py
"""
import pytest
from pages.main_page import MainPage
from pages.login_page import LoginPage
from pages.basket_page import BasketPage

@pytest.mark.login_guest
class TestLoginFromMainPage():
    def test_guest_can_go_to_login_page(self,browser):
        link = "http://selenium1py.pythonanywhere.com/"
        page = MainPage(browser, link)   # инициализируем Page Object, передаем в конструктор экземпляр драйвера и url адрес
        page.open()                      # открываем страницу
        page.go_to_login_page()          # выполняем метод страницы - переходим на страницу логина
        login_page = LoginPage(browser, browser.current_url)
        login_page.should_be_login_page()

    def test_guest_should_see_login_link(self,browser):
        link = "http://selenium1py.pythonanywhere.com/"
        page = MainPage(browser, link)
        page.open()
        page.should_be_login_link()

    @pytest.mark.basket
    def test_guest_cant_see_product_in_basket_opened_from_main_page(self, browser):
        link = "http://selenium1py.pythonanywhere.com/"
        page = MainPage(browser, link)
        page.open()
        page.go_to_basket_page()
        basket_page = BasketPage(browser, browser.current_url)
        basket_page.basket_is_empty()
        basket_page.basket_contains_empty_message()

"""
    @pytest.mark.basket
    def test_guest_basket_button_exist_on_main_page_1(self, browser):
        link = "http://selenium1py.pythonanywhere.com/"
        page = MainPage(browser, link)
        page.open()
        page.is_basket_button_exist_1()

    @pytest.mark.basket
    def test_guest_basket_button_exist_on_main_page_2(self, browser):
        link = "http://selenium1py.pythonanywhere.com/"
        page = MainPage(browser, link)
        page.open()
        page.is_basket_button_exist_2()
"""


from django.urls import path
# from .views import CustomerCreate
from . import views

urlpatterns = [
    path('register/', views.register_view),
    path('login/', views.login_view),
    path('get-user/', views.get_user),
    path('update-user/', views.update_user)
]


#!/usr/bin/env python
import rospy
from sensor_msgs.msg import Image
import cv2
from cv_bridge import CvBridge, CvBridgeError
import numpy as np

ros_img = Image()

threshold_value = 250

def callback(data):
    global ros_img
    ros_img  = data

def threshold(img_msg):

    bridge = CvBridge()

    try:
        cv_image = bridge.imgmsg_to_cv2(img_msg, "mono8")
        thresh, thresh_img = cv2.threshold(cv_image,threshold_value,255,cv2.THRESH_BINARY)

        cv2.rectangle(thresh_img, (0,0), (640,128), (0,0,0), thickness=cv2.FILLED)
        cv2.rectangle(thresh_img, (0,480), (640,320), (0,0,0), thickness=cv2.FILLED)

        img_msg = bridge.cv2_to_imgmsg(thresh_img, "mono8")
    except CvBridgeError as e:
      print(e)

    return img_msg

def threshold_publisher():
    global ros_img
    img_pub = rospy.Publisher('/sensors/camera/infra1/image_rect_threshold', Image, queue_size=10)
    rospy.init_node('threshold_publisher', anonymous=True)

    rospy.Subscriber("/sensors/camera/infra1/image_rect_raw", Image, callback)

    rate = rospy.Rate(10) # 10hz

    while not rospy.is_shutdown():
        img_msg = Image()
        img_msg = threshold(ros_img)
#        img_msg.header = bild.header
 #       img_msg.height = bild.height
#        img_msg.width = bild.width
#        img_msg.encoding = bild.encoding
#        img_msg.is_bigendian = bild.is_bigendian
#        img_msg.step = bild.step
#        img_msg.data = bild.data

        img_pub.publish(img_msg)
        rate.sleep()

if __name__ == '__main__':
    try:
        threshold_publisher()
    except rospy.ROSInterruptException:
        pass


import os

from jinja2 import Environment, FileSystemLoader

# this tells jinja2 to look for templates
# in the templates subdirectory
env = Environment(
    loader = FileSystemLoader('templates'),
)

input_file = 'main.html'
output_file = 'index.html'

# reading the template
template = env.get_template(input_file)
# render the template.
# in other words, we replace the template tag
# by the contents of the overfitting file
rendered = template.render()

# write the result to disk in index.html
with open(output_file, 'w') as ofile:
    ofile.write(rendered)