# -*- coding: utf-8 -*-
"""
Created on Fri Apr 13 19:29:36 2018

@author: zuoquan gong
"""

#import sys
import random
from process.template import DataSet,Vocabulary,IndexSet,BatchBucket,PretrainEmbed
from model.modelloader import ModelLoader
random.seed(10)

class Pythagoras:
#==============================================================================
#     'Everything is number.'  ——Pythagoras
#==============================================================================
    def __init__(self,params):

        params.__show__()
        self.params=params#***
        self.proc_params=params.process_params
        self.model_params=params.model_params
        self.train_eval_params=params.train_eval_params

        self.train_set=[]
        self.dev_set=[]
        self.test_set=[]
        self.vocab=[]

#==============================================================================
#     一、train model
#==============================================================================
    def process(self):

#       ******************  1.data process  ***********************************

        train =DataSet(self.proc_params.path+self.proc_params.trainFile,self.proc_params)
        dev =DataSet(self.proc_params.path+self.proc_params.devFile,self.proc_params)
        test =DataSet(self.proc_params.path+self.proc_params.testFile,self.proc_params)

        vocab=Vocabulary(train,self.proc_params)
        self.proc_params.vocabSize=vocab.word_vocab_size
        self.proc_params.labelSize=vocab.label_vocab_size
        print('vocab_size:',self.proc_params.vocabSize)
        print('label_size:',self.proc_params.labelSize)
        self.model_params.vocabSize=vocab.word_vocab_size
        self.model_params.outputSize=vocab.label_vocab_size
        self.model_params.padId=vocab.padding_id
        self.model_params.startId=vocab.start_id

        train=IndexSet(train,vocab)
        dev=IndexSet(dev,vocab)
        test=IndexSet(test,vocab)

        print('\n[  DataSet Parameters  ]\n')
        print('trainset_size: ',train.size)
        print('devset_size: ',dev.size)
        print('testset_size: ',test.size)

        if self.model_params.embedFile!='':
            self.model_params.pretrain=PretrainEmbed(self.model_params.embedFile,
                                                     vocab.word2id)
        else:
            self.model_params.pretrain=None

        self.train_set=train
        self.dev_set=dev
        self.test_set=test
        self.vocab=vocab

    def train(self):
#       ******************  2.model train  ************************************

        self.process()
        print('\n[  Model Info  ]\n')
        self.models=ModelLoader(self.model_params)#models: model+optimizer##

        self.models.save_models_params_display()
        indexes = [idx for idx in range(self.train_set.size)]

        batch_size=self.train_eval_params.batch_size
        batchBlock = self.train_set.size// batch_size

        #
#        __stdout__ = sys.stdout
#        sys.stdout = open('config.save','w',encoding='utf-8')
#        self.params.__show__()
#        sys.stdout.close()
#        sys.stdout = __stdout__

        print('\n[  Train Iterator  ]\n')
        for iter in range(self.train_eval_params.maxIter):

            print('\n###  Iteration ' + str(iter) + "  ###")
#            random.shuffle(indexes)

            self.models.train()###

            for updateIter in range(batchBlock):

                self.models.clear_grads()##

                #--start batch--
                start_pos = updateIter * batch_size
                end_pos = (updateIter + 1) * batch_size
                feats=[]
                labels=[]
                for idx in range(start_pos, end_pos):
                    feats.append(self.train_set.word_mat[indexes[idx]])
                    labels.append(self.train_set.label_mat[indexes[idx]])
                batch_instance=BatchBucket( feats,labels, self.train_eval_params,
                                           self.vocab.padding_id)

                #--end batch--

                self.models.forward_all(batch_instance,iter_num=updateIter)
                self.models.optim_step()##

                if (updateIter + 1) % self.train_eval_params.showIter == 0:
                   self.models.batch_show(idx)

            self.models.eval()###
            self.models.best_show()
            dev_rate=self.eval_predict(self.dev_set,)
            test_rate=self.eval_predict(self.test_set)
            self.models.save_best_models(dev_rate,test_rate,iter)
#            difference=self.models.save_best_model(dev_rate,test_rate,iter)
#            if difference<self.train_eval_params.threshold:
#                break

#==============================================================================
#     二、evaluate model
#==============================================================================

#    def evaluate_shell(self):
#        pass

    def eval_predict(self,indexset):
        vocab=self.vocab
        correct_num=0####
        total_num=0####
        batchBlock=len(indexset.label_mat)//self.train_eval_params.batch_size
        for updateIter in range(batchBlock):

            #--start batch--
            start_pos = updateIter * self.train_eval_params.batch_size
            end_pos = (updateIter + 1) * self.train_eval_params.batch_size
            feats=[]
            labels=[]
            for idx in range(start_pos, end_pos):
                feats.append(indexset.word_mat[idx])
                labels.append(indexset.label_mat[idx])
            batch_instance=BatchBucket( feats,labels, self.train_eval_params,
                                           vocab.padding_id)
            #--end batch--

            predict_labels=self.models.predict_labels(batch_instance).transpose(0,1)

            predict_labels=predict_labels.masked_select(batch_instance.masks)
            gold_labels=batch_instance.batch_labels.masked_select(batch_instance.masks)

            predict_labels=predict_labels.long()
            for i in range(predict_labels.size()[0]):
                if predict_labels.data[i]==gold_labels.data[i]:
                    correct_num+=1
                total_num+=1
#            if updateIter==0:
#                print(predict_labels.data[:20])
#                print(gold_labels.data[:20])

        rate=correct_num/total_num
        print('total_num: {:^10d} | correct_num: {:^10d}'.format(total_num,correct_num))
        print('correct_rate: {:^10f}'.format(rate))
        return rate


from time import sleep
from django.db import connections
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand, CommandError

class Command(BaseCommand):
    help = 'Closes the specified poll for voting'

    def handle(self, *args, **options):
        conn = connections['default']
        connected = False
        while not connected:
            try:
                c = conn.cursor()
            except OperationalError:
                sleep(2)
                print("Waiting for the database")
            else:
                print("Connected to the database")
                connected = True


def find132pattern(self, nums):
    if len(nums) < 3:
        return False
    st = []
    s3 = float('-inf')

    for i in range(len(nums) - 1, -1, -1):
        if nums[i] < s3:
            return True
        else:
            while st and st[-1] < nums[i]:
                s3 = st.pop()
            st.append(nums[i])
    return False


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
# from sklearn.neighbors import KNeighborsClassifier
# Create dataframe contain .csv
url = './data_mobile.csv'
df = pd.read_csv(url, encoding='utf-8')
X = df.iloc[:, 1] # Get all data at column 1; iloc[:,:4] => get from column 1 -> 3
Y = df.iloc[:, 2] # Get all data at column 2

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2) # test size 20%
cls = DecisionTreeClassifier()
cls.fit(X_train, Y_train) # X_train, Y_train must be number
comment = ['Điện thoại màn hình quá tệ']


import json
from sys import argv
import time as dt
import urllib.request
import zipfile

data=[]
webpage = "http://data.itsfactory.fi/journeys/api/1/vehicle-activity"
count = 0
i = 0
while i < int(argv[3]):
    websource = urllib.request.urlopen(webpage)
    jsonData = json.loads(websource.read().decode())
    dt.sleep(int(argv[2]))
    i = i + int(argv[2])
    for key in jsonData:
        count = count + 1
        if count >= 3:
            value = jsonData[key]
            data.append(value)

with open(argv[1], "w", encoding="UTF-8") as outfile:
    json.dump(data, outfile, indent=2)

with zipfile.ZipFile ("busdata.zip","w") as myzip:
    myzip.write(argv[1])


# Copyright 2018-Present Shanghai Yitu Technology Co., Ltd.
# Licensed under the Apache License, Version 2.0

import base64
import os
import hashlib

# Converts file into base64 encoded string
def load_file_base64(file_path):
    if not os.path.isfile(file_path):
        return None

    with open(file_path, "rb") as audio_file:
        encoded_string = base64.b64encode(audio_file.read())
        return encoded_string.decode('utf-8')

def md5sum(filename):
    #python3
    fd = open(filename, "rb")
    #python2
    # fd = open(filename, "rb")
    fcont = fd.read()
    fd.close()
    return hashlib.md5(fcont).hexdigest()


# everything shall start from 0
import argparse
import itertools
import json
import logging
import matplotlib
import multiprocessing as mp
import numpy as np
import os
import pprint
matplotlib.rcParams['backend'] = 'agg' if os.environ.get('CI') else 'TkAgg'
import matplotlib.pyplot as plt
from os import path, environ
from collections import deque
from functools import partial

# parse_args to add flag
parser = argparse.ArgumentParser(description='Set flag for functions')
parser.add_argument("-d", "--debug",
                    help="activate debug log",
                    action="store_const",
                    dest="loglevel",
                    const=logging.DEBUG,
                    default=logging.INFO)
args = parser.parse_args([]) if environ.get('CI') else parser.parse_args()

# Goddam python logger
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
handler.setFormatter(
    logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s'))
logger.setLevel(args.loglevel)
logger.addHandler(handler)
logger.propagate = False

pp = pprint.PrettyPrinter(indent=2)

plt.rcParams['toolbar'] = 'None'  # mute matplotlib toolbar
plotters = {}  # hash of matplotlib objects for live-plot

PROBLEMS = json.loads(open(
    path.join(path.dirname(__file__), 'assets', 'problems.json')).read())

# the keys need to be implemented by a sys_var
# the constants (capitalized) are problem configs,
# set in assets/problems.json
required_sys_keys = {
    'RENDER',
    'GYM_ENV_NAME',
    'SOLVED_MEAN_REWARD',
    'MAX_STEPS',
    'MAX_EPISODES',
    'MAX_HISTORY',
    'param',
    'epi',
    'history',
    'mean_rewards',
    'solved'
}

def init_sys_vars(problem='CartPole-v0', param={}):
    '''
    init the sys vars for a problem by reading from
    assets/problems.json, then reset the other sys vars
    on reset will add vars: {param, epi, history, mean_rewards, solved}
    '''
    sys_vars = PROBLEMS[problem]
    if mp.current_process().name != 'MainProcess':
        sys_vars['RENDER'] = False  # mute on parallel
    if environ.get('CI'):
        sys_vars['RENDER'] = False
        sys_vars['MAX_EPISODES'] = 2
    sys_vars['param'] = param
    reset_sys_vars(sys_vars)
    init_plotter(sys_vars)
    return sys_vars

def reset_sys_vars(sys_vars):
    '''reset and check RL system vars before each new session'''
    sys_vars['epi'] = 0
    sys_vars['history'] = deque(maxlen=sys_vars.get('MAX_HISTORY'))
    sys_vars['mean_rewards'] = 0
    sys_vars['solved'] = False
    check_sys_vars(sys_vars)
    return sys_vars

def check_sys_vars(sys_vars):
    '''ensure the requried RL system vars are specified'''
    sys_keys = sys_vars.keys()
    assert all(k in sys_keys for k in required_sys_keys)

def get_env_spec(env):
    '''Helper: return the env specs: dims, actions, reward range'''
    return {
        'state_dim': env.observation_space.shape[0],
        'state_bounds': np.transpose(
            [env.observation_space.low, env.observation_space.high]),
        'action_dim': env.action_space.n,
        'actions': list(range(env.action_space.n)),
        'reward_range': env.reward_range
    }

def report_speed(real_time, total_t):
    '''Report on how fast each time step runs'''
    avg_speed = float(real_time)/float(total_t)
    logger.info('Mean speed: {:.4f} s/step'.format(avg_speed))

def update_history(sys_vars,
                   total_t,
                   total_rewards):
    '''
    update the hisory (list of total rewards)
    max len = MAX_HISTORY
    then report status
    '''

    sys_vars['history'].append(total_rewards)
    # Calculating mean_reward over last 100 episodes
    if (len(sys_vars['history']) > 200):
        mean_rewards = np.mean(sys_vars['history'][-200:])
    else:
        mean_rewards = np.mean(sys_vars['history'])
    solved = (mean_rewards >= sys_vars['SOLVED_MEAN_REWARD'])
    sys_vars['mean_rewards'] = mean_rewards
    sys_vars['solved'] = solved
    live_plot(sys_vars)

    logs = [
        '',
        'Episode: {}, total t: {}, total reward: {}'.format(
            sys_vars['epi'], total_t, total_rewards),
        'Mean rewards over last {} episodes: {:.4f}'.format(
            sys_vars['MAX_HISTORY'], mean_rewards),
        '{:->20}'.format(''),
    ]
    logger.debug('\n'.join(logs))
    check_session_ends(sys_vars)
    return sys_vars

def check_session_ends(sys_vars):
    if (sys_vars['solved'] or
            (sys_vars['epi'] == sys_vars['MAX_EPISODES'] - 1)):
        logger.info('Problem solved? {}. At epi: {}. Params: {}'.format(
            sys_vars['solved'], sys_vars['epi'],
            pp.pformat(sys_vars['param'])))
    if not sys_vars['RENDER']:
        return
    plt.savefig('{}.png'.format(sys_vars['GYM_ENV_NAME']))

def init_plotter(sys_vars):
    if not sys_vars['RENDER']:
        return
    # initialize the plotters
    fig = plt.figure(facecolor='white')

    ax1 = fig.add_subplot(211,
                          frame_on=False,
                          title=str(sys_vars['param']) +
                          '\ntotal rewards per episode',
                          ylabel='total rewards')
    p1, = ax1.plot([], [])
    plotters['total rewards'] = (ax1, p1)

    ax2 = fig.add_subplot(212,
                          frame_on=False,
                          title='mean rewards over last 100 episodes',
                          ylabel='mean rewards')
    p2, = ax2.plot([], [])
    plotters['mean rewards'] = (ax2, p2)

    plt.ion()  # for live plot

def live_plot(sys_vars):
    '''do live plotting'''
    if not sys_vars['RENDER']:
        return
    ax1, p1 = plotters['total rewards']
    p1.set_ydata(np.append(p1.get_ydata(), sys_vars['history'][-1]))
    p1.set_xdata(np.arange(len(p1.get_ydata())))
    ax1.relim()
    ax1.autoscale_view(tight=True, scalex=True, scaley=True)

    ax2, p2 = plotters['mean rewards']
    p2.set_ydata(np.append(p2.get_ydata(), sys_vars['mean_rewards']))
    p2.set_xdata(np.arange(len(p2.get_ydata())))
    ax2.relim()
    ax2.autoscale_view(tight=True, scalex=True, scaley=True)

    plt.draw()
    plt.pause(0.01)

# convert a dict of param ranges into
# a list of cartesian products of param_range
# e.g. {'a': [1,2], 'b': [3]} into
# [{'a': 1, 'b': 3}, {'a': 2, 'b': 3}]
def param_product(param_range):
    keys = param_range.keys()
    range_vals = param_range.values()
    return [dict(zip(keys, vals)) for vals in itertools.product(*range_vals)]

# advanced, experimental code for parallelization
def run_session_average(run_session, problem, param={}):
    '''
    executes the defined run_session function with sys_vars
    run session multiple times for a param
    then average the mean_rewards from them
    '''
    SESSIONS_PER_PARAM = 5
    logger.info(
        'Running average session with param = {}'.format(pp.pformat(param)))
    mean_rewards_history = []
    for i in range(SESSIONS_PER_PARAM):
        sys_vars = run_session(problem, param)
        mean_rewards_history.append(sys_vars['mean_rewards'])
        sessions_mean_rewards = np.mean(mean_rewards_history)
        if sys_vars['solved']:
            break
    logger.info(
        'Sessions mean rewards: {} with param = {}'.format(
            sessions_mean_rewards, pp.pformat(param)))
    return {'param': param, 'sessions_mean_rewards': sessions_mean_rewards}

def select_best_param(run_session, problem, param_grid):
    '''
    Parameter selection
    by running session average for each param parallel
    executes the defined run_session function with problem
    then sort by highest sessions_mean_rewards first
    return the best
    '''
    # NUM_CORES = mp.cpu_count()
    # p = mp.Pool(NUM_CORES)
    # params_means = p.map(
    #     partial(run_session_average, run_session, problem),
    #     param_grid)
    params_means = list(map(
        partial(run_session_average, run_session, problem),
        param_grid))
    params_means.sort(key=lambda pm: pm['sessions_mean_rewards'], reverse=True)
    for pm in params_means:
        logger.info(pp.pformat(pm))
    return params_means[0]


'''
Copyright and license disclosure:
a partial replica of https://github.com/KratosMultiphysics/Kratos/blob/master/kratos/python_scripts/compare_two_files_check_process.py
'''

from .validate_and_assign_defaults import validate_and_assign_defaults
import unittest
from numpy import isclose as t_isclose

# Other imports
import filecmp
import os
import math

class CompareTwoFilesCheck(unittest.TestCase):
    """This process compares files that are written during a simulation
    against reference files.
    Please see the "ExecuteFinalize" functions for details about the
    available file-formats
    """
    def __init__(self, params):

        ## Settings string in json format
        default_parameters = {
            "help"                  : "This process checks that two files are the same. This can be used in order to create tests, where a given solution is expected",
            "reference_file_name"   : "",
            "output_file_name"      : "",
            "remove_output_file"    : True,
            "tolerance"             : 1e-6,
            "relative_tolerance"    : 1e-9
        }

        # backwards compatibility
        if "decimal_places" in params:
            if "tolerance" in params or "relative_tolerance" in params:
                raise Exception('Conflicting settings specified, please remove "decimal_places"')
            decimal_places = params["decimal_places"]
            warning =  'W-A-R-N-I-N-G: You have specified "decimal_places", '
            warning += 'which is deprecated and will be removed soon.\n'
            warning += 'Please specify "tolerance" and "relative_tolerance" instead!'
            print(warning)
            tol = 0.1**decimal_places
            params["tolerance"] = tol

        ## Overwrite the default settings with user-provided parameters
        validate_and_assign_defaults(default_parameters, params)

        self.reference_file_name = params["reference_file_name"]
        self.output_file_name = params["output_file_name"]

        self.remove_output_file = params["remove_output_file"]

        # TODO: perhaps enhance for various types of comparison
        # self.comparison_type = params["comparison_type"]

        self.tol = params["tolerance"]
        self.reltol = params["relative_tolerance"]

        self.info_msg = "".join([  "\n[%s]: Failed with following parameters:\n" % self.__class__.__name__,
                                    str(params)
                                ])

    def execute(self):
        self.__CompareDatFile()

        # TODO: perhaps enhance with various types of comparison
        # elif (self.comparison_type == "dat_file"):
        #     self.__CompareDatFile()
        # elif (self.comparison_type == "csv_file"):
        #     self.__CompareCSVFile()
        # elif (self.comparison_type == "dat_file_variables_time_history"):
        #     self.__CompareDatFileVariablesTimeHistory()

        if self.remove_output_file:
            if os.path.exists(self.output_file_name):
                os.remove(self.output_file_name)

    def __GetFileLines(self):
        """This function reads the reference and the output file
        It returns the lines read from both files and also compares
        if they contain the same numer of lines
        """
        # check if files are valid
        if not os.path.isfile(self.reference_file_name):
            err_msg  = 'The specified reference file name "'
            err_msg += self.reference_file_name
            err_msg += '" is not valid!'
            raise Exception(err_msg)
        if not os.path.isfile(self.output_file_name):
            err_msg  = 'The specified output file name "'
            err_msg += self.output_file_name
            err_msg += '" is not valid!'
            raise Exception(err_msg)

        # "readlines" adds a newline at the end of the line,
        # which will be removed with rstrip afterwards
        with open(self.reference_file_name,'r') as ref_file:
            lines_ref = ref_file.readlines()
        with open(self.output_file_name,'r') as out_file:
            lines_out = out_file.readlines()

        # removing trailing newline AND whitespaces than can mess with the comparison
        lines_ref = [line.rstrip() for line in lines_ref]
        lines_out = [line.rstrip() for line in lines_out]

        num_lines_ref = len(lines_ref)
        num_lines_out = len(lines_out)

        err_msg  = "Files have different number of lines!"
        err_msg += "\nNum Lines Reference File: " + str(num_lines_ref)
        err_msg += "\nNum Lines Output File: " + str(num_lines_out)
        self.assertTrue(num_lines_ref == num_lines_out, msg=err_msg + self.info_msg)

        return lines_ref, lines_out

    def __CompareDatFile(self):
        """This function compares files with tabular data.
        => *.dat
        Lines starting with "#" are comments and therefore compared for equality
        Other lines are compared to be almost equal with the specified tolerance
        """
        lines_ref, lines_out = self.__GetFileLines()

        # assert headers are the same
        lines_ref, lines_out = self.__CompareDatFileComments(lines_ref, lines_out)

        # assert values are equal up to given tolerance
        self.__CompareDelimittedFileResults(lines_ref, lines_out, None)

    def __CompareCSVFile(self):
        """This function compares files with tabular data.
        => *.csv
        Lines starting with "#" are comments and therefore compared for equality
        Other lines are compared to be almost equal with the specified tolerance
        """
        lines_ref, lines_out = self.__GetFileLines()

        # assert headers are the same
        lines_ref, lines_out = self.__CompareDatFileComments(lines_ref, lines_out)

        # assert values are equal up to given tolerance
        self.__CompareDelimittedFileResults(lines_ref, lines_out, ",")

    def __CompareDatFileVariablesTimeHistory(self):
        """This function compares files with tabular data.
        => *.dat
        Lines starting with "#" are comments and therefore compared for equality
        Other lines are compared to be almost equal with the specified tolerance
        If the comparison fails, it prints the location of failure with details
        The expected format is the one written by the PointOutputProcess:

        # some basic file information
        # time var_name_1 var_name_2
        0.1 1.2345 2.852
        0.2 0.889 -89.444
        .
        .
        .
        """
        lines_ref, lines_out = self.__GetFileLines()

        # extracting the names of output variables eg: time, VELOCITY_X, VELOCITY_Y, VELOCITY_Z
        variable_names = lines_ref[1].split()[2:]

        # assert headers are the same
        lines_ref, lines_out = self.__CompareDatFileComments(lines_ref, lines_out)

        # assert values are equal up to given tolerance
        self.__CompareDatFileResultsWithLocation(lines_ref, lines_out, variable_names)

    def __CompareDatFileComments(self, lines_ref, lines_out):
        """This function compares the comments of files with tabular data
        The lines starting with "#" are being compared
        These lines are removed from the list of lines
        """
        for line_ref, line_out in zip(lines_ref, lines_out):
            if line_ref.lstrip()[0] == '#' or line_out.lstrip()[0] == '#':
                self.assertTrue(line_ref == line_out, msg = self.info_msg)

        lines_ref = [line for line in lines_ref if not(line.lstrip()[0] == '#')]
        lines_out = [line for line in lines_out if not(line.lstrip()[0] == '#')]

        return lines_ref, lines_out

    def __CompareDelimittedFileResults(self, lines_ref, lines_out, delimiter):
        """This function compares the data of files with tabular data
        The comment lines were removed beforehand
        """
        for line_ref, line_out in zip(lines_ref, lines_out):
            for v1, v2 in zip(line_ref.split(delimiter), line_out.split(delimiter)):
                self.__CheckCloseValues(float(v1), float(v2))

    def __CompareDatFileResultsWithLocation(self, lines_ref, lines_out, variable_names):
        """This function compares the data of files with tabular data
        It also prints the exact location where data doesnt match each other
        The comment lines were removed beforehand
        """
        for line_ref, line_out in zip(lines_ref, lines_out):
            for i_var, (v1, v2) in enumerate(zip(line_ref.split(), line_out.split())):
                if i_var == 0: # comparing time:
                    additional_info = 'Different time found!'
                    self.__CheckCloseValues(float(v1), float(v2), additional_info)
                    current_time = v1
                else: # comparing variables
                    additional_info  = 'Failed for variable ' + variable_names[i_var-1]
                    additional_info += ' at time: ' + current_time
                    self.__CheckCloseValues(float(v1), float(v2), additional_info)

    def __CheckCloseValues(self, val_a, val_b, additional_info=""):
        isclosethis = t_isclose(val_a, val_b, rtol=self.reltol, atol=self.tol)
        full_msg =  self.info_msg + "\n"
        full_msg += str(val_a) + " != " + str(val_b) + ", rel_tol = "
        full_msg += str(self.reltol) + ", abs_tol = " + str(self.tol)
        if additional_info != "":
            full_msg += "\n" + additional_info
        self.assertTrue(isclosethis, msg=full_msg)


# Histogram Equalization using inbuilt function

import cv2
import numpy as np

# READ an BGR image using imread() function of OpenCV
original = cv2.imread('./resources/itachi.jpg')

# Creating a numpy matrix with dimentions same as the
# original image and data type uint8 (unsigned int 8 bit)
img = np.zeros(original.shape,dtype='uint8')

# Dsplaying the original image
cv2.imshow('Original',original)

# Applying the histogram equalization for the R,G and B channels of the original
# image using the equalizeHist() function provided in the OpenCV library
img[:,:,0]=cv2.equalizeHist(original[:,:,0])
img[:,:,1]=cv2.equalizeHist(original[:,:,1])
img[:,:,2]=cv2.equalizeHist(original[:,:,2])

# Displaying the histogram equalized image
cv2.imshow('Equalised',img)

# Stacking the two images horizontally using the hstack() function provided in
# the numpy library and storing the result as a new image
res = np.hstack((original,img))

# Saving the result image to the disk using the imwrite() function provided in the openCV library
cv2.imwrite('Ques5a.png',res)

# Display the images and wait till any key is pressed
cv2.waitKey(0)
# Destroy all the windows created by the imshow() function of the OpenCV
cv2.destroyAllWindows()


import argparse

# constant
__version__ = '1.0'
__description__ = 'schat - a simple security multiple users chat room '
__epilog__ = 'Report bugs to <cjyeh@cs.nctu.edu.tw>'

def parse_argv():
	# init
	parser = argparse.ArgumentParser(
		description=__description__,
		epilog=__epilog__
	)

	# main parser
	parser.add_argument(
		'-v', '-V', '--version',
		action='version',
		help='Print program version',
		version='v{}'.format(__version__)
	)

	# sub parser
	subparsers = parser.add_subparsers(
		dest='command',
		help='command help'
	)
	subparsers.required = True

	# server
	parser_server = subparsers.add_parser('server')
	parser_server.add_argument(
		'-p', '--port',
		action='store',
		help='Specified port, default=7000',
		type=int,
		default=7000
	)
	parser_server.add_argument(
		'-C', '--capacity',
		action='store',
		help='Maximum capacity for chatroom, default=10',
		type=int,
		default=10
	)
	parser_server.add_argument(
		'-k', '--key',
		action='store',
		help='Private key'
	)
	parser_server.add_argument(
		'-c', '--cert',
		action='store',
		help='Certification'
	)

	# client
	parser_client = subparsers.add_parser('client')
	parser_client.add_argument('host')
	parser_client.add_argument(
		'-p', '--port',
		action='store',
		help='Specified port, default=7000',
		type=int,
		default=7000
	)
	parser_client.add_argument(
		'-s', '--secure',
		action='store_true',
		help='Secure connection',
		default=False
	)
	parser_client.add_argument(
		'-v', '-V', '--version',
		action='version',
		help='Print program version',
		version='v{}'.format(__version__)
	)

	results = parser.parse_args()

	return results.__dict__


#calss header
class _MEANTIME():
	def __init__(self,):
		self.name = "MEANTIME"
		self.definitions = [u'until something expected happens, or while something else is happening: ']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'nouns'

	def run(self, obj1 = [], obj2 = []):
		return self.jsondata


# -*- coding: utf-8 -*-
import logging
import os
import sys

import click
import marshmallow
from click import echo

from pcad import PcbPcadWriter
from pcb import PcbFromSchematic

from . import qucs

logging.basicConfig(
    format='%(levelname)s %(module)s:%(lineno)s: %(message)s',
    stream=sys.stdout,
)

log = logging.getLogger(__name__)

@click.command()
@click.argument('qucs_file', type=click.File('r'))
@click.option('-v', '--verbose', count=True)
def qucs_parse(qucs_file, verbose):
    """
    """

    logging.getLogger().setLevel(
        [logging.WARNING, logging.INFO, logging.DEBUG]
        [min(verbose, 2)]
    )

    qucs_scematic = qucs.schem.loads(qucs_file.read().decode('utf-8'))

    for comp in qucs_scematic.components:
        log.info(str(comp))

@click.command()
@click.option('-v', '--verbose', count=True)
def dump_components(verbose):
    logging.getLogger().setLevel(
        [logging.WARNING, logging.INFO, logging.DEBUG]
        [min(verbose, 2)]
    )

    components = qucs.schem.global_component_factory._register.values()

    for j in components:
        log.info('%s %s', j, qucs.components.utils.component_dumps(j))


# Sofia's implementation of some
# simple sorting algorithms

def select(unsorted_list):
    wip_list = list(unsorted_list)

    # assume that the index we start at is smallest unless
    # our check indicates otherwise
    for i in range(len(wip_list)):
        smallest_position = i
        for j in range(i, len(wip_list)):
            if wip_list[j] < wip_list[smallest_position]:
                smallest_position = j
        swap(wip_list, i, smallest_position)

    return wip_list

def insert(unsorted_list):
    wip_list = list(unsorted_list)
    #sorted_list = [wip_list.pop()]
    wip_list.sort()
    return wip_list # placeholder

    while wip_list:
        item = wip_list.pop()
        print(item)
        for i in range(len(sorted_list)):
            if item < sorted_list[i]:
                sorted_list.insert(item, i)
                break
            sorted_list.append(item)

    return sorted_list

def bubble(unsorted_list):
    wip_list = list(unsorted_list)

    # iterate over the size in reverse order
    # last list element is sorted in bubble sort
    for i in reversed(range(len(wip_list))):
        for j in range(i):
            if wip_list[j] > wip_list[j+1]:
                swap(wip_list, j, j+1)

    return wip_list

def swap(some_list, i, j):
    tmp = some_list[i]
    some_list[i] = some_list[j]
    some_list[j] = tmp

def rmerge(unsorted_list):
#    return unsorted_list.sort()

    # Base case: list is a single element
    # That's sorted!
    if len(unsorted_list) <= 1:
        return unsorted_list

    # Split up list into chunks
    middle = len(unsorted_list) >> 1
    chunk1 = rmerge(unsorted_list[:middle])
    chunk2 = rmerge(unsorted_list[middle:])

    return merge(chunk1, chunk2)

def merge(chunk1, chunk2):
    if not chunk1:
        return chunk2
    if not chunk2:
        return chunk1
    if chunk1[0] < chunk2[0]:
        return [chunk1[0]] + merge(chunk1[1:], chunk2)
    return [chunk2[0]] + merge(chunk1, chunk2[1:])


#!/bin/env python3
#coding=utf-8
# @Time    : 16/12/5 18:15
# @Author  : kelly
# @Site    :
# @File    : multithread.py
# @Software: PyCharm

import sys
sys.path.append("./")
sys.path.append("../")

import threading
import business_logic.insurance_logic as ins_logic
import dbconfig

# print(dbconfig.position(),'imported')

if dbconfig.env == 'prd':
    threading_num = 20
else:
    threading_num = 2

class myThread:
    def set_parameter(self,parameter):
        self.p = [[] for a in range(threading_num)]
        i = 0
        parameter = list(parameter)
        while len(parameter) > 0:
            i = 0
            while i < threading_num:
                if len(parameter) == 0:
                    break
                self.p[i].append(parameter.pop())
                i += 1

    def set(self,program):
        self.program = program
        if self.program == 'auto_quote_daily':
            request_program = ins_logic.refresh_quote()
            request_program.get_quotable_objects()
            self.set_parameter(request_program.quotable_result)

    def main(self):
        threads = []
        while len(threads) < threading_num:
            # threads.append(threading.Thread(target=self.program.refresh_objects(),name=self.main_program))
            if self.program == 'auto_quote_daily':
                threads.append(threading.Thread(target=ins_logic.refresh_quote().refresh_objects(self.p[len(threads)])))
            else:
                break

        print(dbconfig.position(),'%s threads ready...go' % len(threads))
        for t in threads:
            # t.setDaemon(True)
            t.start()

        for t in threads:
            t.join()

        print(dbconfig.position(), "all over")


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

import json
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
from godsome.main.models import get_or_none
from godsome.message.models import MobileDevice

@csrf_exempt
def push(request):
    state = []
    uid = request.GET.get("uid")
    regid = request.GET.get("regid")
    os = request.GET.get("os",0)
    user = get_or_none(User,id=uid)
    if regid and user :
        md = get_or_none(MobileDevice,uid=regid)
        if md :
            md.user = user
            md.os = os
        else :
            MobileDevice.objects.get_or_create(user=user,uid=regid,os=os)
    state.append(regid)
    return HttpResponse(json.dumps(state), content_type="application/json")


from sys import stdin
#1 4 2 2 3 5 3 4

n=[int(xx) for xx in stdin.readline().strip().split(" ")]
def paredes (n):
    if len[n]>10:
        return paredes
    else:
        altos=0
        bajos=0
        pos1=-1
        pos2=0
        for mario in n:
            pos1+=1
            pos2+=1
            if n[pos1]>n[pos2]:
                bajos+=1
            else:
                n[pos1]<n[pos2]
                altos+=1
        print (altos)
        print (bajos)

paredes (n)


import warnings

import numpy as np

BC_OPTS = ['periodic', 'grounded']  # available BC options

class NBody():
    """Particle Mesh (PM) NBody solver

    Modelling gravitational N-Body problem with particle mesh method in n-D.
    Solving for the potential with Fourier methods and flexible mass/force
    assignment/interpolation.

    Args:
        m    (array or float): Mass of the particles. Same mass for all
                               particles if scalar, one mass per ptcl if array.
        npart           (int): number of particles
        ngrid           (int): number of grid (mesh) pts/cells
        soft          (float): softening constant applied to distance in
                               potential/force calculation
        dt            (float): time steps
        pos0          (array): Initial position array with shape (npart, ndim).
                               Uniformly distributed if None, default is None.
                               All positions must be within [0, ngrid).
        vel0 (array or float): Initial velocity array with shape (npart, ndim).
                               If float, input times normal random number.
                               If None, automatically set to 0.
        G             (float): gravitational constant
        bc              (str): Keyword specifying how to deal with boundary
                               conditions.
                               Options are:
                                   - 'periodic': periodic boundary condtions.
                                                 The particles reappear on the
                                                 other side of the space.
                                   - 'grounded': potential is set to 0 around
                                                 the boundary.
        cosmo          (bool): If True, masses are initialized according to a
                               scale-invariant power spectrum.
                               Default is False.
        ndim            (int): Number of dimensions in the model.
    """

    def __init__(self, m=1.0, npart=1000, ngrid=500, soft=0.1, dt=0.1,
                 pos0=None, vel0=None, G=1.0, bc='periodic', cosmo=False,
                 ndim=2):
        # Constant parameters/attributes
        # Number of ptcls
        self._npart = int(npart)
        # Grid pts
        if ngrid % 2 == 0:
            self._ngrid = int(ngrid)
        else:
            warnings.warn('Arg. ngrid must be even. Adding 1 to input',
                          RuntimeWarning)
            self._ngrid = int(ngrid) + 1
        # Softening constant.
        if np.isscalar(soft):
            self._soft = soft
        else:
            raise TypeError('soft must be a scalar value')
        # Time steps.
        if np.isscalar(dt):
            self._dt = dt
        else:
            raise TypeError('dt must be a scalar value')
        # Grav. constant
        if np.isscalar(G):
            self._G = G
        else:
            raise TypeError('G must be a scalar value')
        # BCs
        if bc not in BC_OPTS:
            raise ValueError('bc must be one of {}'.format(BC_OPTS))
        self._bc = bc
        if isinstance(cosmo, bool):
            self._cosmo = cosmo
        else:
            raise TypeError('cosmo must be boolean.')
        self._ndim = int(ndim)

        # Check and init ptcls position and velocity, both in (npart, ndim)
        # array. Results stored in self._pos and self._vel
        self._init_pos(pos0)
        self._init_vel(vel0)

        # Check and init mass in an (npart, 1) array. The shape is to
        # faciliate array operations with pos and vel.
        # Result stored in self._m
        self._init_mass(m)

        # Initialize density (same call as all other density computations).
        # Result stored in self._density. Also assigns self._meshpts with same
        # shape as self._pos, with mesh points for each ptcl.
        self._compute_density()

        # Initialize Green's function for gravitational problem, with symmetry
        # to ensure periodicity of BCs. Result is stored in self._green
        self._init_green()

    # Safe client-side access to attribues. Prevents them from being modified.
    @property
    def npart(self):
        return self._npart

    @property
    def ngrid(self):
        return self._ngrid

    @property
    def ndim(self):
        return self._ndim

    @property
    def soft(self):
        return self._soft

    @property
    def dt(self):
        return self._dt

    @property
    def G(self):
        return self._G

    @property
    def bc(self):
        return self._bc

    @property
    def m(self):
        return self._m

    @property
    def pos(self):
        return self._pos

    @property
    def vel(self):
        return self._vel

    @property
    def density(self):
        return self._density

    @property
    def cosmo(self):
        return self._cosmo

    def _init_mass(self, m):
        if not self.cosmo:
            # Sanity check and simple array manipulation.
            if np.isscalar(m):
                self._m = m * np.ones([self.npart, 1], dtype=float)
            else:
                m = np.asarray(m, dtype=float)
                m = m.flatten()
                if m.size != self.npart:
                    raise ValueError('m must have size npart or be scalar')
                self._m = np.array([m.copy()]).T
        # Using scale-invariant power spectrum to scale mass distribution. In
        # this case, the grid points will be used to obtain nd spacial
        # frequencies (k-vector), and this will be used to obtain a power
        # spectrum. We will then re-scale mass distribution and assign scaled
        # masses to each particle.
        # (https://cds.cern.ch/record/583256/files/0209590.pdf)
        else:
            # First, need to uniformize masses for all ptcls
            if not np.isscalar(m):
                msg = ('m must be scalar when cosmo is True. Assigning '
                       'm to mean of input array')
                warnings.warn(msg, RuntimeWarning)
                m = float(np.mean(m))
            self._m = m * np.ones([self.npart]).T

            # Compute density grid
            self._compute_density()
            mavg = np.mean(self._m)
            davg = np.mean(self.density)

            # Fourier space and power spectrum.
            knd = np.fft.rfftfreq(self.ngrid) * 2*np.pi
            knd = np.repeat([knd], self.ndim, axis=0)
            kmesh = np.array(np.meshgrid(*knd))  # mesh in all dims
            ksqr = np.sum(kmesh**2, axis=0)  # k2 on grid
            soft = self.soft
            ksqr[ksqr < soft**2] = soft**2
            ksqr += soft**2
            k = np.sqrt(ksqr)
            powerspec = 1.0 / k**3

            # Draw params for each fluctuation mode (each k), and construct FT.
            amps = np.random.rayleigh(scale=np.sqrt(powerspec))
            phases = 2*np.pi*np.random.random_sample(amps.shape)
            ft = amps * np.exp(1j*phases)

            # IFT for fluctuations, then derive ptcl masses
            fluct = np.fft.irfftn(ft, s=self.density.shape)
            density = davg * fluct + davg  # new density
            ncell = self.density / mavg  # ptcls in each cell
            mcell = density.copy()
            mcell[ncell > 0] /= ncell[ncell > 0]  # mass for ptcls in each cell

            # Assign back to mass.
            m = mcell[self._meshinds]

            self._m = np.array([m.copy()]).T

    def _init_pos(self, pos0):
        if self.bc == 'periodic':
            xmin, xmax = 0, self.ngrid
        else:
            xmin, xmax = 1, self.ngrid - 1
        if pos0 is not None:
            # Sanity checks.
            pos = np.asarray(pos0, dtype=float)
            if pos.shape != (self.npart, self.ndim):
                raise ValueError('pos0 must have shape (npart, ndim)')
            if pos.max() >= xmax or pos.min() < xmin:
                raise ValueError('positions in all dimension must be within'
                                 ' [0, ngrid) for periodic and [1,ngrid-1)'
                                 ' otherwise.')
        else:
            pos = xmax - xmin
            pos *= np.random.random_sample([self.npart, self.ndim])
        self._pos = pos.copy()

    def _init_vel(self, vel0):
        if vel0 is None:
            vel0 = 0.0
        if np.isscalar(vel0):
            vel = float(vel0)
            vel *= np.random.standard_normal([self.npart, self.ndim])
        # Sanity checks.
        else:
            vel = np.asarray(vel0, dtype=float)
            if vel.shape != (self.npart, self.ndim):
                raise ValueError('vel0 must have shape (npart, ndim)')
        self._vel = vel.copy()

    def _compute_density(self):
        # Get mesh points for each ptcl. Take modulo to assign xi=L to xi=0,
        # such that mesh points can be integers in range [0, L-1].
        self._meshpts = np.rint(self.pos).astype('int') % self.ngrid
        # for quick access in grid-like arrays
        self._meshinds = tuple(self._meshpts[:, i] for i in range(self.ndim))

        # For each particle, assign density (mass bc unitary cells) to mesh
        # point.
        # NGP assigment scheme
        # Use ndim histogram to save some time (no loop)
        edges = np.linspace(0, self.ngrid-1, num=self.ngrid+1)
        edges = np.repeat([edges], self.ndim, axis=0)
        hist = np.histogramdd(self._meshpts, bins=edges,
                              weights=self.m.flatten())
        self._density = hist[0]

    def _init_green(self):
        # Compute Green's function in one corner of mesh grid.
        greensize = self.ngrid // 2
        meshrange = np.arange(greensize, dtype=float)
        meshrange = np.repeat([meshrange], self.ndim, axis=0)
        mesh = np.array(np.meshgrid(*meshrange))
        rsqr = np.sum(mesh**2, axis=0)
        rsqr[rsqr < self.soft**2] = self.soft**2
        rsqr += self.soft**2
        r = np.sqrt(rsqr)
        green = 1 / (4*np.pi*r)

        # flip along each dimension
        for i in range(self.ndim):
            green = np.append(green, np.flip(green, axis=i), axis=i)

        self._green = green

    def get_pot(self):
        """Compute model potential
        Get potential for each mesh cell by convoluting pre-calculated Green's
        function. Convolution is performed with 2 fourier transform.
        Returns:
            pot (array): potential at every mesh point.
        """
        # Perform convolution
        pot = np.fft.rfftn(self._green) * np.fft.rfftn(self.density)
        pot = np.fft.irfftn(pot)

        # Because of our periodic Green's function definition used in the FFT,
        # potential is not symmetric wrt ptcl positions, so we need to shift
        # and average to center it back on ptcls.
        for i in range(self.ndim):
            pot = 0.5 * (np.roll(pot, 1, axis=i) + pot)

        # enforce BCs
        if self.bc == 'grounded':
            pad = tuple((slice(1, -1),)) * self.ndim
            pot = np.pad(pot[pad], 1)

        return pot

    def get_fmesh(self):
        """Compute mesh forces
        Get force components for each mesh point by differentiating current
        potential grid.
        Returns:
            fmesh (array): forces at each grid point. Array of shape
            (ndim, (ngrid)*ndim) where first axis 0 is the x and y components.
        """
        # Init force array.
        shape = [self.ndim]
        shape.extend([self.ngrid]*self.ndim)
        fmesh = np.zeros(shape, dtype=float)

        # Get pot.
        pot = self.get_pot()

        # periodic gradient in each direction
        for i in range(self.ndim):
            fmesh[i] = 0.5*(np.roll(pot, 1, axis=i) - np.roll(pot, -1, axis=i))

        # Times density and -1 to get actual forces
        fmesh *= - self.density * self.G

        return fmesh

    def get_fpart(self):
        """Compute particle forces
        Get force components for each particle by interpolating from mesh
        forces.
        Returns:
            fpart (array): forces on particle along each axis. Array has shape
                           (npart, 2).
        """
        # Get mesh forces and transpose such that callig fmesh[i,j] will give
        # x and y components for mesh cell (i,j).
        fmesh = np.moveaxis(self.get_fmesh(), 0, -1)

        # Interpolate for each praticle using same scheme as in mass
        # assignment.
        # NGP interpolation.
        fpart = fmesh[self._meshinds]

        return fpart

    def get_energy(self):
        """Get energy of the system
        Kinetic energy is calculated using ptcl velocities and masses.
        Potential energy is obtained with get_pot(), summing over all cells
        """
        # compute kin and pot as in class
        kin = np.sum(self.m * self.vel**2)
        pot = -0.5*np.sum(np.sum(self.get_pot()) * self.density)

        return kin + pot

    def evolve(self, nsteps=1):
        """Evolve sytem
        Let system evolve by a given number of timesteps. Leapfrog integration
        is used to solve for position and velocity.
        Args:
            nsteps (int): number of steps taken in given direction
        Returns:
            eg (float): total energy after letting the system evolve
        """
        for i in range(nsteps):
            # Get force for each particle
            forces = self.get_fpart()

            # update velocity
            self._vel += forces * self.dt / self._m

            # update position
            self._pos += self.vel * self.dt
            self._pos = self._pos % self.ngrid

            # update grid
            self._compute_density()

        return self.get_energy()


import sys  # связь с ОС
from PyQt5 import QtWidgets
import design

class ChatWindow(QtWidgets.QMainWindow, design.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.init_handlers()

    def init_handlers(self):  # инициализация обработчиков
        self.pushButton.clicked.connect(self.send_message)

    def send_message(self):
        message = self.lineEdit.text()
        self.plainTextEdit.appendPlainText(message)
        self.lineEdit.clear()

app = QtWidgets.QApplication(sys.argv)
window = ChatWindow()
window.show()  # показ окна
app.exec_()  # запуск интерфейса


import nibabel as nib
from skimage.measure import find_contours
from skimage.filters import gaussian
import numpy as np
from shapely.geometry import Polygon

class ContoursFromLabelNii(object):
	def __init__(self, labelNii, origNiiPath, nlist):
		#super(ContoursFromLabel, self).__init__()
		self.labelNii = labelNii
		self.origNiiPath = origNiiPath
		self.contourDict = self.getContours( nlist )

	def getContours(self, nlist ):
		self.nList = nlist
		labelImg = self.labelNii.get_data()
		n = 1 # roi number for bitget
		# eventually use for n in self.nList:
		##### BITGET ######
		# should return a binary image with 1's in
		# the voxels where the ROI is
		roiImg = (labelImg & (1<<n)) >> n

		# remove z slices where the structure of interest does not exist
		roiCrop = roiImg[:,:,~(roiImg==0).all(axis=(0,1))]
		zcrop = ~(roiImg==0).all(axis=(0,1))
		#imgCrop = origImg[:,:,~(roiImg==0).all(axis=(0,1))]
		contourDict = {}
		for k, kslice in enumerate(np.rollaxis(roiImg,2)):
			if zcrop[k]:
				filteredRoi = gaussian(kslice, sigma=1, preserve_range =True)
				contours = find_contours(filteredRoi, 0.5, \
					fully_connected='high', positive_orientation='high')
				contourDict = getContourDict(contours, k, self.labelNii.affine, \
					contourDict)
		return contourDict

	def getContourDict(self, contours, k, affine, contourDict, \
		getPolygons = True):
		for contour in contours:
			contourXyz = getContourXyz(contour, k, affine)
			z = contourXyz[0,2]
			if np.abs(z - round(z)) < 1e-3:
				z = round(z)
			contourXyz[:,2] = z
			polygon = Polygon(contourXyz)
			if not contourDict.has_key(z):
				if getPolygons:
					contourDict[z] = [polygon]
				else:
					contourDict[z] = [contourXyz]
			else:
				if getPolygons:
					contourDict[z].append(polygon)
				else:
					contourDict[z].append(contourXyz)
		return contourDict

	def getContourXyz(self, ctr, k, affine):
		ctr = np.c_[ctr, np.ones(ctr.shape[0])*k, np.ones(ctr.shape[0])]
		ctr_xyz1 = np.matmul(affine, ctr[...,np.newaxis]).\
			reshape(ctr.shape)
		return ctr_xyz1[:,0:3]

if __name__ == '__main__':
	labelNii = nib.load("C:\\Users\\megan\\Documents\\label_test.nii")
	origNiiPath = 'N:\\NiiForOptimisation\\p1\\20110905_091632.nii'
	c = ContoursFromLabel(labelnii, origNiiPath)


# -*- coding: utf-8 -*-
import logging
from binascii import hexlify

from cryptoauthlib.constant import INFO_MODE_REVISION, INFO_MODE_STATE
from cryptoauthlib.device import ATECC508A

log = logging.getLogger("atecc508a.tests_info")

def run(atecc508a=None):
    if not atecc508a:
        atecc508a = ATECC508A()

    expected = b'\x07\x00\x00P\x00\x03\x91'
    packet = atecc508a.atcab_info()
    assert expected == packet.response_data
    log.debug("atcab_info: %s", hexlify(packet.response_data))

    packet = atecc508a.atcab_info_base(INFO_MODE_REVISION)
    assert expected == packet.response_data
    log.debug("atcab_info_base - revision: %s", hexlify(packet.response_data))

    expected = b'\x07\x00\x00\x00\x00\x03\xad'
    packet = atecc508a.atcab_info_base(INFO_MODE_STATE)
    assert expected == packet.response_data
    log.debug("atcab_info_base - state: %s", hexlify(packet.response_data))


#
# Copyright 2016 The BigDL Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from typing import Optional, List, Set, Dict
from importlib.util import find_spec
from .utils import AccelerationOption
from bigdl.nano.utils import CPUInfo

def _inc_checker():
    '''
    check if intel neural compressor is installed
    '''
    return not find_spec("neural_compressor") is None

def _ipex_checker():
    '''
    check if intel pytorch extension is installed
    '''
    return not find_spec("intel_extension_for_pytorch") is None

def _onnxruntime_checker():
    '''
    check if onnxruntime and onnx is installed
    '''
    onnxruntime_installed = not find_spec("onnxruntime") is None
    onnx_installed = not find_spec("onnx") is None
    return onnxruntime_installed and onnx_installed

def _openvino_checker():
    '''
    check if openvino-dev is installed
    '''
    return not find_spec("openvino") is None

def _bf16_checker():
    '''
    bf16 availablity will be decided dynamically during the optimization
    '''
    cpuinfo = CPUInfo()
    return cpuinfo.has_bf16

def available_acceleration_combination(excludes: Optional[List[str]],
                                       includes: Optional[List[str]],
                                       full_methods: Dict[str, AccelerationOption],
                                       all_methods: Dict[str, AccelerationOption] = None):
    '''
    :return: a dictionary states the availablity (if meet depdencies)
    '''
    dependency_checker = {"inc": _inc_checker,
                          "ipex": _ipex_checker,
                          "onnxruntime": _onnxruntime_checker,
                          "openvino": _openvino_checker,
                          "pot": _openvino_checker}
    if excludes is None:
        exclude_set: Set[str] = set()
    else:
        exclude_set: Set[str] = set(excludes)
        exclude_set.discard("original")

    if includes is None:
        include_set: Set[str] = set(full_methods.keys())
    else:
        include_set: Set[str] = set(includes)
        include_set.add("original")
        if all_methods is not None:
            for method in include_set:
                if method not in full_methods:
                    # append include method into full methods
                    full_methods[method] = all_methods[method]

    available_dict = {}
    for method, option in full_methods.items():
        if method not in include_set:
            continue

        if method in exclude_set:
            continue

        available_iter = True
        for name, value in option.__dict__.items():
            if value is True:
                if name in dependency_checker and not dependency_checker[name]():
                    available_iter = False
        available_dict[method] = available_iter
    return available_dict


from decimal import Decimal

from corehq.apps.accounting.models import (
    FeatureType,
    SoftwarePlanEdition,
    SoftwareProductType,
)

BOOTSTRAP_EDITION_TO_ROLE = {
    SoftwarePlanEdition.COMMUNITY: 'community_plan_v1',
}

FEATURE_TYPES = [
    FeatureType.USER,
    FeatureType.SMS,
]

PRODUCT_TYPES = [
    SoftwareProductType.COMMCARE,
    SoftwareProductType.COMMCONNECT,
    SoftwareProductType.COMMTRACK,
]

BOOTSTRAP_PRODUCT_RATES = {
    SoftwarePlanEdition.COMMUNITY: dict(),
}

BOOTSTRAP_FEATURE_RATES = {
    SoftwarePlanEdition.COMMUNITY: {
        FeatureType.USER: dict(monthly_limit=10, per_excess_fee=Decimal('1.00')),
        FeatureType.SMS: dict(monthly_limit=0),
    },
}

BOOTSTRAP_FEATURE_RATES_FOR_TESTING = {
    SoftwarePlanEdition.COMMUNITY: {
        FeatureType.USER: dict(monthly_limit=2, per_excess_fee=Decimal('1.00')),
        FeatureType.SMS: dict(monthly_limit=0),
    },
}


#!/usr/bin/env python3

from neml import history, interpolate
from neml.math import tensors, rotations
from neml.cp import crystallography, slipharden, sliprules

from common import differentiate
from nicediff import *

import unittest
import numpy as np
import numpy.linalg as la

class CommonSlipRule(object):
  def test_d_slip_d_stress(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        d = self.model.d_slip_d_s(g, i, self.S, self.Q, self.H, self.L, self.T, self.fixed)
        nd = diff_scalar_symmetric(lambda s: self.model.slip(g, i, s, self.Q, self.H,
          self.L, self.T, self.fixed), self.S)
        self.assertEqual(d, nd)

  def test_d_slip_d_hist(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        d = np.array(self.model.d_slip_d_h(g, i, self.S, self.Q, self.H, self.L, self.T, self.fixed))
        nd = np.array(diff_history_scalar(lambda h: self.model.slip(g, i, self.S, self.Q, h,
          self.L, self.T, self.fixed), self.H))
        self.assertTrue(np.allclose(nd.reshape(d.shape), d))

  def test_d_hist_rate_d_stress(self):
    d = np.array(self.model.d_hist_rate_d_stress(self.S, self.Q, self.H, self.L, self.T, self.fixed))
    nd = diff_history_symmetric(lambda s: self.model.hist_rate(s, self.Q, self.H, self.L,
      self.T, self.fixed), self.S)
    self.assertTrue(np.allclose(nd.reshape(d.shape), d))

  def test_d_hist_rate_d_hist(self):
    d = np.array(self.model.d_hist_rate_d_hist(self.S, self.Q, self.H, self.L, self.T, self.fixed))
    nd = diff_history_history(lambda h: self.model.hist_rate(self.S, self.Q, h, self.L,
      self.T, self.fixed), self.H)
    self.assertTrue(np.allclose(nd.reshape(d.shape), d))

class CommonSlipStrengthSlipRule(object):
  def test_init_hist(self):
    H1 = history.History()
    self.model.populate_history(H1)
    self.model.init_history(H1)

    H2 = history.History()
    self.strengthmodel.populate_history(H2)
    self.strengthmodel.init_history(H2)

    self.assertTrue(np.allclose(np.array(H1),
      np.array(H2)))

  def test_slip(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        rs = self.L.shear(g, i, self.Q, self.S)
        strength = self.strength + self.static
        self.assertTrue(np.isclose(self.model.slip(g, i, self.S, self.Q, self.H, self.L, self.T, self.fixed),
          self.model.sslip(g, i, rs, strength, self.T)))

  def test_d_hist_rate(self):
    self.assertTrue(np.allclose(
      np.array(self.model.hist_rate(self.S, self.Q, self.H, self.L, self.T, self.fixed)),
      np.array(self.strengthmodel.hist(self.S, self.Q, self.H, self.L, self.T, self.model, self.fixed))))

  def test_d_sslip_d_tau(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        nd = differentiate(lambda t: self.model.sslip(g, i, t, self.strength, self.T),
            self.tau)
        d = self.model.d_sslip_dtau(g, i, self.tau, self.strength, self.T)
        self.assertTrue(np.isclose(nd,d))

  def test_d_sslip_d_strength(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        nd = differentiate(lambda s: self.model.sslip(g, i, self.tau, s, self.T), self.strength)
        d = self.model.d_sslip_dstrength(g, i, self.tau, self.strength, self.T)
        print(nd)
        print(d)
        self.assertTrue(np.isclose(nd, d))

class TestPowerLawSlip(unittest.TestCase, CommonSlipStrengthSlipRule, CommonSlipRule):
  def setUp(self):
    self.L = crystallography.CubicLattice(1.0)
    self.L.add_slip_system([1,1,0],[1,1,1])

    self.Q = rotations.Orientation(35.0,17.0,14.0, angle_type = "degrees")
    self.S = tensors.Symmetric(np.array([
      [100.0,-25.0,10.0],
      [-25.0,-17.0,15.0],
      [10.0,  15.0,35.0]]))
    self.strength = 35.0
    self.H = history.History()
    self.H.add_scalar("strength")
    self.H.set_scalar("strength", self.strength)

    self.T = 300.0

    self.tau0 = 10.0
    self.tau_sat = 50.0
    self.b = 2.5

    self.strengthmodel = slipharden.VoceSlipHardening(self.tau_sat, self.b, self.tau0)

    self.static = self.tau0

    self.g0 = 1.0
    self.n = 3.0
    self.model = sliprules.PowerLawSlipRule(self.strengthmodel, self.g0, self.n)

    self.tau = 33.0

    self.fixed = history.History()

  def test_scalar_rate(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        self.assertTrue(np.isclose(self.model.sslip(g, i, self.tau, self.strength, self.T),
          self.g0 * np.abs(self.tau/self.strength)**(self.n-1.0) * self.tau/self.strength))

class TestBiVoceSlip(unittest.TestCase, CommonSlipStrengthSlipRule, CommonSlipRule):
  def setUp(self):
    self.L = crystallography.CubicLattice(1.0)
    self.L.add_slip_system([1,1,0],[1,1,1])

    self.Q = rotations.Orientation(35.0,17.0,14.0, angle_type = "degrees")
    self.S = tensors.Symmetric(np.array([
      [100.0,-25.0,10.0],
      [-25.0,-17.0,15.0],
      [10.0,  15.0,35.0]]))
    self.strength_1 = 35.0
    self.strength_2 = 25.0
    self.strength = self.strength_1 + self.strength_2
    self.H = history.History()
    self.H.add_scalar("strength0")
    self.H.set_scalar("strength0", self.strength_1)
    self.H.add_scalar("strength1")
    self.H.set_scalar("strength1", self.strength_2)

    self.T = 300.0

    self.tau0 = 10.0
    self.tau_sat = 50.0
    self.b = 2.5

    self.strengthmodel = slipharden.SumSlipSingleStrengthHardening(
        [slipharden.VoceSlipHardening(self.tau_sat, self.b, self.tau0),
          slipharden.VoceSlipHardening(self.tau_sat/2, self.b/2, self.tau0/2)])

    self.static = self.tau0 + self.tau0 / 2

    self.g0 = 1.0
    self.n = 3.0
    self.model = sliprules.PowerLawSlipRule(self.strengthmodel, self.g0, self.n)

    self.tau = 33.0

    self.fixed = history.History()


'''
Ighoise Odigie
June 11, 2020
Youtube: https://www.youtube.com/channel/UCud4cJjtCjEwKpynPz-nNaQ?
Github: https://github.com/Iggy-o
Preview: https://repl.it/@IghoiseO/Machine-Learning-Basics#main.py
'''

#MACHINE LEARNING: EXPLAINED
'''
  I'm relatively new to the world of machine learning but here's what I've
  learned. Originally, the AI is initialized with completely new brain made
  up of random connections and as a result like a baby it is very ineffective.
  But it is trained against a premade data set and its connections are tweaked
  to make it's answers closer to the dataset. Hopefully after training the AI's
  brain can answer related questions accurately.
  The AI's brain structure resembles a tree =>    o     Result
                                                / | \   Weights/Connections
                                               o  o  o  Inputs
'''

#Here is an amazing tutorial series
'''
https://www.youtube.com/playlist?list=PLxt59R_fWVzT9bDxA76AHm3ig0Gg9S3So
'''

#The Instructions
instructions = '''
---BASICS OF MACHINE LEARNING---

How To Use
----------
The Test Data is currently set up for Phones Vs. Laptops.
The program should learn from the premade data and answer
your given input correctly, more iterations results in
better answers. You must input in the following format:
-----------
Screen Size(Inches)
Battery Life(Hrs)
Weight(lb)
# of AI Interations(a.k.a Wait Time)
-----------
\n\n
               '''

#A few libraries must be imported for the program to run
import numpy as np

#The instructions are displayed to the user and they are prompted to enter values for the mystery node below
print(instructions)
screenSize = float(input("What is your device's screen size => "))
batteryLife = float(input("What is your device's battery life => "))
weight = float(input("What is your device's weight=> "))

#The training rate defines the size of the change between iterations, the attempts refers to the number of iterations
training_Attempts = int(input("How many iterations do you want => "))
training_Rate = 0.3

#<!--The Training Data-->

#This variable stores the data which the AI is trained against (Screen Size, Weight[kg], Answer)
data = [
        [19, 7, 20, 1],
        [20, 6, 21, 1],
        [21, 7, 21, 1],
        [22, 8, 22, 1],
        [23, 4, 25, 1],
        [24, 5, 23, 1],
        [25, 9, 26, 1],
        [26, 5, 24, 1],
        [27, 8, 27, 1],
        [28, 6, 28, 1],
        [29, 7, 30, 1],
        [30, 7, 30, 1],
        [31, 7, 29, 1],
        [32, 6, 24, 1],
        [33, 9, 21, 1],
        [4, 17, 0.3, 0],
        [7, 8, 0.3, 0],
        [5, 16, 0.4, 0],
        [6, 15, 0.45, 0],
        [6, 12, 0.5, 0],
        [7, 17, 0.8, 0],
        [6, 14, 0.6, 0],
        [4, 13, 0.4, 0],
        [6, 10, 0.6, 0],
        [5, 9, 0.6, 0],
        [6, 16, 0.45, 0],
        [7, 17, 0.5, 0],
        [9, 17, 0.55, 0],
        ]

#The mystery node is the final test to see if the AI is properly trained
mystery_node = [screenSize, batteryLife, weight]

#<!--The Artificial Brain-->

#There are four weights and a bias (a.k.a The Brain's connections) that are assigned randomly and altered over several iterations to get a more accurate answer
initValue = np.random.randn()
weight1 = initValue
weight2 = initValue
weight3 = initValue
bias = initValue

#This class holds most of the machine learning algorithm
class machineBrain:
  #When you call this class inputs must be provided
  def __init__(self, a, b, c, d = "null"):
    self.input1 = a
    self.input2 = b
    self.input3 = c
    self.target = d

  #Sigmoid is just an "S" shaped squashing function that squashes a very large or small value between 1 and 0
  def sigmoid(self, value):
    return 1/(1 + np.exp(-value))
  def d_sigmoid(self, value):
    return self.sigmoid(value) * (1 - self.sigmoid(value))

  #This function returns the computer's prediction on a 0 to 1 scale
  def prediction(self):
    prediction = weight1 * self.input1 + weight2 * self.input2 + weight3 * self.input3 + bias
    squashedPrediction = self.sigmoid(prediction)
    return prediction, squashedPrediction

  #This function calculates how far from correct the computer's answer is
  def err(self):
    pred, squashPred = self.prediction()
    err = np.square(squashPred - self.target)
    return err

  #This function attempts to make the computer's guesses closer to the answer's given by the data
  #This is done by calculating the derivative back to the weights, which a graph can be derived from
  #The weights are altered based on the slopes of the derivative's graph
  #This can push the computer's error to the lowest possible value and make them closer to the target answer
  #Hopefully that point is 0 and after repeating this error suppression process
  #The computer will hopefully be pretty accurate
  def errSuppress(self):
    #Create a bunch of derivitaves for each function that is used
    pred, squashPred = self.prediction()
    derr_dSquashpred = 2 * (squashPred - self.target)
    dSquashpred_dpred = self.d_sigmoid(pred)
    dpred_dw1 = self.input1
    dpred_dw2 = self.input2
    dpred_dw3 = self.input3
    dpred_db = 1

    #Combine the derivatives from above into general ones for each weight (and bias)
    derr_dpred = derr_dSquashpred * dSquashpred_dpred
    derr_dw1 = derr_dpred * dpred_dw1
    derr_dw2 = derr_dpred * dpred_dw2
    derr_dw3 = derr_dpred * dpred_dw3
    derr_db = derr_dpred * dpred_db

    #Finally the weights are altered based on the training rate
    global weight1, weight2, weight3, bias
    weight1 -= derr_dw1*training_Rate
    weight2 -= derr_dw2*training_Rate
    weight3 -= derr_dw3*training_Rate
    bias -= derr_db*training_Rate

    #Returns the margin of error
    return self.err()

#The following loop is for the training process of the AI
print("Training...")
for attempts in range(training_Attempts):
  for data_point in range(len(data)):
    brain = machineBrain(data[data_point][0], data[data_point][1], data[data_point][2], data[data_point][3])
    error = brain.errSuppress()

#After the AI is trained it will output based on the user's mystery node
print("Complete")
brain = machineBrain(mystery_node[0], mystery_node[1],mystery_node[2])
pred, Squashpred = brain.prediction()
if Squashpred > 0.5:
  answer = "Your device is a Laptop"
else:
  answer = "Your device is a Phone"
print(f"\nThe computer says: {answer}")
#print(f"The weights: {weight1}, {weight2}, {weight3}")