import streamlit as st
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import os
import copy
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import load_img, img_to_array
from keras.applications.resnet import ResNet50
from keras.applications.vgg16 import VGG16
from keras.layers import Flatten, Dense
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
import matplotlib
import matplotlib.pylab as plt
import numpy as np
import seaborn as sns
from sklearn.utils import shuffle
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import f1_score
from sklearn.model_selection import train_test_split

W = 224
H = 224
#168

label_to_class = {
    'MildDemented': 0,
    'ModerateDemented': 1,
    'NonDemented': 2,
    'VeryMildDemented':3
    }
class_to_label = {v: k for k, v in label_to_class.items()}
n_classes = len(label_to_class)

@st.cache
def get_images(dir_name=os.path.join(os.path.dirname(__file__),'AlzheimerDataset'), label_to_class=label_to_class):
    """read images / labels from directory"""
    Images = []
    Classes = []
    for j in ['/train','/test']:
        for label_name in os.listdir(dir_name+str(j)):
            cls = label_to_class[label_name]
            for img_name in os.listdir('/'.join([dir_name+str(j), label_name])):
                img = load_img('/'.join([dir_name+str(j), label_name, img_name]), target_size=(W, H))
                img = img_to_array(img)
                Images.append(img)
                Classes.append(cls)

    Images = np.array(Images, dtype=np.float32)
    Classes = np.array(Classes, dtype=np.float32)
    Images, Classes = shuffle(Images, Classes, random_state=0)

    return Images, Classes

Images, Classes = get_images()

def createResnetModel():
    model = keras.models.Sequential()
    # load model
    ResNet = ResNet50(include_top=False, input_shape=(W,H,3),pooling='avg')
    # Freezing Layers
    for layer in ResNet.layers:
        layer.trainable=False
    model.add(ResNet)
    model.add(Flatten())
    model.add(Dense(512,activation='relu',kernel_initializer='he_uniform'))
    model.add(Dense(128,activation='relu',kernel_initializer='he_uniform'))
    model.add(Dense(32,activation='relu',kernel_initializer='he_uniform'))
    model.add(Dense(n_classes,activation='softmax'))
    return model

def createVGGModel():
    model = keras.models.Sequential()
    # load model
    VGG = VGG16(include_top=False, input_shape=(W,H,3),pooling='avg')
    # Freezing Layers
    for layer in VGG.layers:
        layer.trainable=False
    model.add(VGG)
    model.add(Flatten())
    model.add(Dense(512,activation='relu',kernel_initializer='he_uniform'))
    model.add(Dense(128,activation='relu',kernel_initializer='he_uniform'))
    model.add(Dense(32,activation='relu',kernel_initializer='he_uniform'))
    model.add(Dense(n_classes,activation='softmax'))
    return model

def preprocess(sel_model):
    if sel_model == 'Resnet50':
        from keras.applications.resnet import preprocess_input
    elif  sel_model == 'VGG16':
        from keras.applications.vgg16 import preprocess_input
    datagen_train = ImageDataGenerator(
        preprocessing_function=preprocess_input, # image preprocessing function
        rotation_range=30,                       # randomly rotate images in the range
        width_shift_range=0.1,                   # randomly shift images horizontally
        height_shift_range=0.1,                  # randomly shift images vertically
        horizontal_flip=True,                    # randomly flip images horizontally
        vertical_flip=False,                     # randomly flip images vertically
    )
    datagen_test = ImageDataGenerator(
        preprocessing_function=preprocess_input, # image preprocessing function
    )

def Train_Val_Plot(acc,val_acc,loss,val_loss,auc,val_auc,precision,val_precision):

    fig, (ax1, ax2,ax3,ax4) = plt.subplots(1,4, figsize= (20,4))
    fig.suptitle(" MODEL'S METRICS VISUALIZATION ")

    ax1.plot(range(1, len(acc) + 1), acc)
    ax1.plot(range(1, len(val_acc) + 1), val_acc)
    ax1.set_title('History of Accuracy')
    ax1.set_xlabel('Epochs')
    ax1.set_ylabel('Accuracy')
    ax1.legend(['training', 'validation'])

    ax2.plot(range(1, len(loss) + 1), loss)
    ax2.plot(range(1, len(val_loss) + 1), val_loss)
    ax2.set_title('History of Loss')
    ax2.set_xlabel('Epochs')
    ax2.set_ylabel('Loss')
    ax2.legend(['training', 'validation'])

    ax3.plot(range(1, len(auc) + 1), auc)
    ax3.plot(range(1, len(val_auc) + 1), val_auc)
    ax3.set_title('History of AUC')
    ax3.set_xlabel('Epochs')
    ax3.set_ylabel('AUC')
    ax3.legend(['training', 'validation'])

    ax4.plot(range(1, len(precision) + 1), precision)
    ax4.plot(range(1, len(val_precision) + 1), val_precision)
    ax4.set_title('History of Precision')
    ax4.set_xlabel('Epochs')
    ax4.set_ylabel('Precision')
    ax4.legend(['training', 'validation'])
    st.pyplot(fig)

header = st.container()
dataset = st.container()
#features = st.beta_container()
model_training = st.container()
visualization = st.container()

with header:
    st.title('Classification of Alzheimer stages using MRI scan dataset')

with dataset:
    st.header('Alzheimer MRI dataset overview')
    st.text('The dataset was downloaded from Kaggle - Alzheimers Dataset ( 4 class of Images ) : ')
    st.text(class_to_label[0])
    st.text(class_to_label[1])
    st.text(class_to_label[2])
    st.text(class_to_label[3])

    st.text('Plotting some sample images with the label : ')
    n_total_images = Images.shape[0]

    for target_cls in [0,1,2,3]:
        indices = np.where(Classes == target_cls)[0] # get target class indices on Images / Classes
        n_target_cls = indices.shape[0]
        label = class_to_label[target_cls]
        print(label, n_target_cls, n_target_cls*100/n_total_images)

        n_cols = 10 # # of sample plot
        fig, axs = plt.subplots(ncols=n_cols, figsize=(25, 3))

        for i in range(n_cols):

            axs[i].imshow(np.uint8(Images[indices[i]]))
            axs[i].axis('off')
            axs[i].set_title(label)

        st.pyplot(fig)

with model_training:
    st.header('Lets train dataset according user selected model : ')
    indices_train, indices_test = train_test_split(list(range(Images.shape[0])), train_size=0.8, test_size=0.2, shuffle=False)
    x_train = Images[indices_train]
    y_train = Classes[indices_train]
    x_test = Images[indices_test]
    y_test = Classes[indices_test]
    y_train = keras.utils.np_utils.to_categorical(y_train, n_classes)
    y_test = keras.utils.np_utils.to_categorical(y_test, n_classes)
    st.text(f'Number of training data : {len(list(y_train))}')
    st.text(f'Number of testing data : {len(list(y_test))}')
    sel_model = st.selectbox('Pick the CNN model to train the data with', ['Resnet50'])
    preprocess(sel_model)
    if sel_model== 'Resnet50':
        model = createResnetModel()
        model.load_weights(os.path.join(os.path.dirname(__file__),'resnet.h5'))
        history=np.load(os.path.join(os.path.dirname(__file__),'resnet_history.npy'),allow_pickle='TRUE').item()
    #elif sel_model == 'VGG16':
    #    model = createResnetModel()
    #    model.load_weights(os.path.join(os.path.dirname(__file__),'resnet.h5'))
    #    history=np.load(os.path.join(os.path.dirname(__file__),'my_history.npy'),allow_pickle='TRUE').item()

with visualization:
    st.header('Lets visualize the training metrics for ' + sel_model + ' : ')
    st.text('Metric graphs : ')
    Train_Val_Plot(history['accuracy'],history['val_accuracy'],
               history['loss'],history['val_loss'],
               history['auc'],history['val_auc'],
               history['precision'],history['val_precision']  )
    predictions=model.predict(x_test)
    CATEGORIES = ['MildDemented','ModerateDemented','NonDemented','VeryMildDemented']
    # plot images - prediction actual
    st.text('Metric graphs : ')
    rows = 2
    columns = 5
    fig, axs= plt.subplots(rows, columns , figsize=(25, 10))
    axs = axs.flatten()
    i=10
    for a in axs:
        Image = Images[indices_test[i]]
        pred_label=predictions.argmax(axis=1)[i]
        actual_label=y_test.argmax(axis=1)[i]
        pred_label=CATEGORIES[pred_label]
        actual_label=CATEGORIES[actual_label]
        label= 'pred: '+ pred_label +' '+'real: '+ actual_label
        #axs[i](rows,columns,i)
        a.imshow(np.uint8(Image))
        a.set_title(label)
        i=i+1
    st.pyplot(fig)

    ## plot confusion matrix
    st.text('Confusion Matrix : ')
    y_preds = model.predict(x_test)
    y_preds = np.argmax(y_preds, axis=1)
    y_trues = np.argmax(y_test, axis=1)
    cm = confusion_matrix(y_trues, y_preds)
    fig, ax = plt.subplots(figsize=(7, 6))
    sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', cbar_kws={'shrink': .3}, linewidths=.1, ax=ax)
    ax.set(
        xticklabels=list(label_to_class.keys()),
        yticklabels=list(label_to_class.keys()),
        title='confusion matrix',
        ylabel='True label',
        xlabel='Predicted label'
    )
    params = dict(rotation=45, ha='center', rotation_mode='anchor')
    plt.setp(ax.get_yticklabels(), **params)
    plt.setp(ax.get_xticklabels(), **params)
    st.pyplot(fig)
    st.text("F1 Score (testing): %.2f%%"% (f1_score(y_trues, y_preds, average='weighted')*100.0))
    st.text("accuracy (testing): %.2f%%"% (accuracy_score(y_trues, y_preds)*100.0))


import logging

from spockbot.mcdata import blocks
from spockbot.plugins.base import PluginBase, pl_announce
from spockbot.plugins.tools.event import EVENT_UNREGISTER
from spockbot.vector import Vector3

from pyhop import hop

from utils.constants import *

FREQUENCY = 3

__author__ = 'Bradley Sheneman'
logger = logging.getLogger('spockbot')

mseq = [
    "move",
    "turn_left",
    "move",
    "move",
    "turn_right",
    "turn_right",
    "move",
    "move",
    "turn_right",
    "move",
    "turn_left",
    "turn_left",
]

# mseq = [
#     "center_on_wall",
#     #"break_obstacle",
#     "break_obstacle",
# ]

@pl_announce('TestAtomicOperators')
class TestAtomicOperatorsPlugin(PluginBase):

    requires = ('Event', 'Timers', 'AtomicOperators', 'Interact',)

    events = {
        #'movement_position_reset':  'handle_position_reset',
        #'movement_path_done':       'handle_path_done',
    }

    def __init__(self, ploader, settings):
        super(TestAtomicOperatorsPlugin, self).__init__(ploader, settings)
        frequency = FREQUENCY
        self.timers.reg_event_timer(frequency, self.sequence_tick)
        self.seq_index = 0

        logger.info("adding atomic operators to pyhop:")
        # note: can call declare_operators multiple times cor current situation
        # hop.declare_operators(
        #     self.atomicoperators.operator_move,
        #     self.atomicoperators.operator_look_left,
        #     self.atomicoperators.operator_look_right)
        # hop.print_operators(hop.get_operators())
        #
        # hop.declare_methods(
        #     'do_test_sequence',
        #     self.do_forward_sequence,
        #     self.do_reverse_sequence)
        # hop.print_methods(hop.get_methods())
        #
        # # have to add fields like start_pos.did_forward_seq = False
        # # and for the goal: goal_pos.did_forward_seq = True
        # self.start_state = hop.State('start_state')
        # self.goal_state1 = hop.Goal('goal_state1')
        # self.goal_state2 = hop.Goal('goal_state2')

        ploader.provides('TestAtomicOperators',self)

    def sequence_tick(self):
        if mseq[self.seq_index] == "move":
            self.atomicoperators.operator_move()
        elif mseq[self.seq_index] == "turn_left":
            self.atomicoperators.operator_look_left()
        elif mseq[self.seq_index] == "turn_right":
            self.atomicoperators.operator_look_right()
        elif mseq[self.seq_index] == "break_obstacle":
            self.atomicoperators.operator_break_obstacle()
        elif mseq[self.seq_index] == "center_on_wall":
            self.interact.look(yaw=DIR_SOUTH,pitch=0.0)

        logger.info("\n******agent executing command: {}******\n".format(mseq[self.seq_index]))
        self.seq_index = (self.seq_index+1)%len(mseq)

    def do_forward_sequence(self, pos):
        if state.dist[x][y] <= 2:
            return [('walk',a,x,y)]
        return False

    def do_reverse_sequence(self, pos):
        if state.dist[x][y] <= 2:
            return [('walk',a,x,y)]
        return False


# -*- coding: utf-8 -*-
"""
https://leetcode.com/problems/kth-smallest-element-in-a-bst/

#1 Naive Way:
In order traversal the tree, then get the K-th element.

#2 In order together with the count
"""

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def kthSmallest(self, root: 'TreeNode', k: 'int') -> 'int':
        curr, stack = root, []
        cnt = 0
        while stack or root:
            if curr:
                stack.append(curr)
                curr = curr.left
            else:
                curr = stack.pop()
                cnt += 1
                if cnt == k:
                    return curr.val
                curr = curr.right


# Break - Continue

# Continue - Quando acha a premissa, continua no laço
for x in range(1, 11):
    if ((x % 2) == 0):
        continue
    print(x)

print()

# Break - Sai do laço de iteração
for x in range(1, 11):
    if (x == 5):
        break
    print(x)

print("Fim!")


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

__author__ = 'laura'

from modelchecker import operators
from modelchecker.ast import nodes
from modelchecker.tokenize import tokens

def from_token(token):
    if isinstance(token, tokens.Proposition):
        return nodes.Proposition.fromToken(token)
    else:
        token_to_token_type = {
            operators.Agent : _from_agent_token,
            operators.Binary: _from_binary_token,
            operators.Unary: _from_unary_token
        }
        return token_to_token_type.get(token.type.__class__)(token)

def _from_agent_token(token):
    token_to_node = {
        operators.Agent.knowledge: nodes.Knowledge,
        operators.Agent.possible: nodes.Possible,
    }
    return token_to_node.get(token.type)(token.agent)

def _from_binary_token(token):
    token_to_node = {
        operators.Binary.conjunction: nodes.Conjunction,
        operators.Binary.disjunction: nodes.Disjunction,
        operators.Binary.implication: nodes.Implication,
        operators.Binary.biimplication: nodes.BiImplication,
    }
    return token_to_node.get(token.type)()

def _from_unary_token(token):
    token_to_node = {
        operators.Unary.negation: nodes.Negation,
        operators.Unary.common: nodes.Common,
        operators.Unary.everybody: nodes.Everybody,
        operators.Unary.implicit: nodes.Implicit
    }
    return token_to_node.get(token.type)()


import requests
from bs4 import BeautifulSoup
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize, sent_tokenize

# Replace "article_url" with the actual URL of the news article you want to summarize
article_url = "https://www.indiatoday.in/technology/news/story/redmi-unveils-300w-fast-charging-tech-fully-charge-xiaomi-phone-under-5-minutes-2340988-2023-03-01"

# Get the HTML content of the article using requests
response = requests.get(article_url)
soup = BeautifulSoup(response.content, 'html.parser')

# Extract the text content of the article
article_text = ' '.join(map(lambda p: p.text, soup.find_all('p')))

# Tokenize the article text into words and sentences
stop_words = set(stopwords.words("english"))
words = word_tokenize(article_text)
sentences = sent_tokenize(article_text)

# Calculate the word frequency of the article
word_frequencies = {}
for word in words:
    if word.lower() not in stop_words:
        if word not in word_frequencies:
            word_frequencies[word] = 1
        else:
            word_frequencies[word] += 1

# Calculate the sentence score of the article based on word frequency
sentence_scores = {}
for sentence in sentences:
    for word in word_tokenize(sentence.lower()):
        if word in word_frequencies:
            if len(sentence.split(" ")) < 30:
                if sentence not in sentence_scores:
                    sentence_scores[sentence] = word_frequencies[word]
                else:
                    sentence_scores[sentence] += word_frequencies[word]

# Get the top 3 sentences with the highest score
summary_sentences = sorted(
    sentence_scores, key=sentence_scores.get, reverse=True)[:3]

# Print the summary of the article
print(" ".join(summary_sentences))


def reading_time(text):
    if text == "":
        raise Exception("Can't estimate reading time for empty text.")
    words = text.split()
    word_count = len(words)
    return word_count / 200

    # word_seconds = 200 / 60
    # string_list = len(text.split())
    # time_per_word = string_list / word_seconds
    # time_per_string = str(datetime.timedelta(seconds = time_per_word))
    # print(time_per_word)
    # return f"I read this text in " + time_per_string


import game
import click
import py5_tools
import skvideo.io

@click.command()
@click.option(
    "--agent",
    "-a",
    type=click.Choice(["human", "bfs", "dfs", "best-first", "astar"]),
    default="human",
    help="Agent type",
)
@click.option(
    "--mode",
    "-m",
    type=click.Choice(["headless", "render"]),
    default="headless",
    help="Operation mode",
)
@click.option(
    "--frame-rate", "-fr", type=float, default=60, help="Frame rate for render mode"
)
def run(agent, mode, frame_rate):
    agent_ = game.Agent(agent)
    environment = game.Environment(agent_, **game.config)
    click.echo(agent)
    if mode == "headless":
        score = environment.run_headless()
        click.echo(f"{agent_.agent_type} agent scored: {score}")
    elif mode == "render":
        environment.run_sketch()

if __name__ == "__main__":
    run()


"""This test script tests most functions of the DDS9m driver.

It is not to be run manually but will instead be found and invoked
automatically by the Pytest test suite.
Obviously, mosts tests require a working DDS9 device connected to a accessible
serial port. However, some basic tests can also be run in "offline" mode, which
can be enabled below (``is_dds9_connected``).
"""
import os
import pytest
import serial
from pyodine.drivers.dds9_control import Dds9Control

__author__ = 'Franz Gutsch'

wrong_port = '/dev/tty0'    # Port must not be accessible (ConnectionError).
dead_port = '/dev/ttyUSB3'  # must be accessible, but no device is connected
live_port = '/dev/ttyUSB2'  # DDS9m must be connected to that port

# Most tests can only be performed when there is a live DDS9m device available.
# We create a marker here to skip those tests automatically if there is no
# device connected.
is_dds9_connected = True   # For now, just manually hard-code this value!

needs_live_device = pytest.mark.skipif(
        not is_dds9_connected, reason="No actual DDS9 is plugged in.")

# Provide a fixture to avoid opening and closing the device connection for
# every single test.
@pytest.fixture
def dds9():
    """Provides the serial connection to the actual DDS9 device."""
    return Dds9Control(live_port)

def test__parse_query_result_on_valid_string():
    """The (private) _parse... function works correctly on legal input."""
    valid_input = 4*'59682F00 0000 029C 0000 00000000 00000000 000301\r\n'
    settings_object = Dds9Control._parse_query_result(valid_input)
    assert settings_object.validate() is True
    assert settings_object.is_zero() is False

def test__parse_query_result_on_invalid_string():
    """The (private) _parse... function works correctly on illegal input."""
    invalid_input = 4*'59682F00 0000 029C 0000 00000000 00000000\r\n'
    settings_object = Dds9Control._parse_query_result(invalid_input)
    assert settings_object.validate() is True
    assert settings_object.is_zero() is True
    invalid_input = 'qwerty'
    settings_object = Dds9Control._parse_query_result(invalid_input)
    assert settings_object.validate() is True
    assert settings_object.is_zero() is True

def test_connect_to_dead_port():
    """Serial port is not accessible."""
    with pytest.raises(serial.SerialException):
        Dds9Control(wrong_port)

@pytest.mark.skipif(not os.path.exists(dead_port),
                    reason="Inaccessible port specified.")
def test_connect_to_dead_device():
    """The port can be opened, but DDS9 doesn't respond."""
    with pytest.raises(ConnectionError):
        device = Dds9Control(dead_port)
        assert device.ping() is False

@needs_live_device
def test_check_device_sanity(dds9: Dds9Control):
    """Connection to device is established and device is in non-zero state."""
    assert dds9.ping() is True

@needs_live_device
def test_recognize_illegal_command(dds9: Dds9Control):
    """DDS9 can tell invalid and valid commands apart."""
    response = dds9._send_command('python')

    # Check if this gives us the "bad phase" error, as "p" actually starts a
    # set phase command.
    print(response)
    assert response.find('?4') > 0

@needs_live_device
def test_set_phase(dds9: Dds9Control):
    """Device accepts and saves phase settings."""
    dds9.reset()
    dds9.set_phase(111)
    dds9.set_phase(99, 1)
    dds9.set_phase(0, 2)
    expected = [111, 99, 0, 111]
    actual = dds9.phases
    diff = [expected[i] - actual[i] for i in range(4)]
    assert max(diff) < 1

@needs_live_device
def test_set_amplitudes(dds9: Dds9Control):
    """Device accepts and saves amplitude settings."""
    dds9.reset()
    dds9.set_amplitude(.777)  # set all channels
    dds9.set_amplitude(1, 1)  # reset some
    dds9.set_amplitude(0, 2)
    expected = [.777, 1, 0, .777]
    actual = dds9.amplitudes
    diff = [expected[i] - actual[i] for i in range(4)]
    assert max(diff) < 0.01

@needs_live_device
def test_set_frequency(dds9: Dds9Control):
    """Device accepts and saves frequency settings."""
    dds9.reset()
    dds9.set_frequency(123)
    dds9.set_frequency(0.007, 1)
    dds9.set_frequency(1000, 2)  # will be capped to 171 MHz!
    expected = [123, 7e-3, 171, 123]
    actual = dds9.frequencies
    print(expected)
    print(actual)
    diff = [expected[i] - actual[i] for i in range(4)]

    # Internally, the chip works with 0.1Hz steps:
    assert max(diff) < 1e-8

@needs_live_device
def test_pause_resume(dds9: Dds9Control):
    """The pause and resume methods act on the amplitude as expected."""
    dds9.reset()
    assert dds9.ping() is True
    dds9.set_amplitude(1)
    assert dds9.amplitudes == 4*[1]
    dds9.pause()
    assert dds9.amplitudes == 4*[0]
    dds9.set_amplitude(.5)
    assert max([abs(dds9.amplitudes[i] - .5) for i in range(4)]) < .01
    dds9.resume()
    assert dds9.amplitudes == 4*[1]

@needs_live_device
def test_switch_reference_source(dds9: Dds9Control):
    """Switching the frequency reference clock throws no errors.

    It also must not alter the set up frequency values.
    """
    dds9.reset()
    dds9.set_frequency(10)
    freq1 = dds9.frequencies
    dds9.switch_to_ext_reference()
    freq2 = dds9.frequencies
    dds9.switch_to_int_reference()
    freq3 = dds9.frequencies
    assert max([abs(freq1[i] - freq2[i]) for i in range(4)]) < 1e-6
    assert max([abs(freq1[i] - freq3[i]) for i in range(4)]) < 1e-6

@needs_live_device
def test_set_frequency_on_external_clock(dds9: Dds9Control):
    """Device accepts and applies frequency settings when on ext. clock."""
    dds9.reset()
    dds9.switch_to_ext_reference()
    dds9.set_frequency(123)
    dds9.set_frequency(0.007, 1)
    dds9.set_frequency(1000, 2)  # will be capped!
    cap_frequency = dds9._settings.max_freq_value / dds9._freq_scale_factor

    # The last channel is supposed to have the same frequency as the first one,
    # as the very first set_ command acts on all channels.
    expected = [123, 7e-3, cap_frequency, 123]

    actual = dds9.frequencies
    diff = [expected[i] - actual[i] for i in range(4)]

    # Internally, the chip works with 0.1Hz steps:
    assert max(diff) < 1e-8

@needs_live_device
def test_get_settings(dds9: Dds9Control):
    """Instance is fueled by a full set of valid settings.

    Furthermore, settings cannot be modified from the outside.
    """
    settings = dds9.get_settings()

    # Run basic sanity test on all setting's values.
    assert float(settings.max_freq_value) > 0
    assert int(settings.baudrate) % 10 == 0
    assert float(settings.timeout) >= 0 and float(settings.timeout) < 100
    assert isinstance(settings.port, str) and settings.port
    assert float(settings.ext_clock) > 0
    assert len(settings.ext_clock_multiplier_setting) == 2

    # Make sure settings can't be modified.
    settings.max_freq_value = -1
    assert dds9.get_settings().max_freq_value != -1


# external library
from math import ceil
import numpy as np

"""
Functions:
lowess Fit a smooth nonparametric regression curve to a scatterplot.
Which references from

"""
# for bigger f value, the function will be much smoother
def lowess(x, y, f, it):
    # np.seterr(divide='ignore', invalid='ignore')
    n = len(x)
    h = []
    # generate several windows in local regression to calculate
    for i in range(n):
        h.append(np.sort(np.abs(x - x[i]))[int(ceil(f * n))])
    w = np.clip(np.abs((x[:, None] - x[None, :]) /h ), 0., 1.)
    # the weight follows as W(x) = (1-|x|^3)^3
    w = (1 - np.abs(w) ** 3) ** 3
    yest = np.zeros(n)
    delta = np.ones(n)
    constant = 6.0
    # add error handler
    try:
        for iteration in range(it):
            for i in range(n):
                # let the weight be delta_k * w_k
                weights = delta * w[:, i]
                b_1 = np.sum(weights * y)
                b_2 = np.sum(weights * y * x)
                b = np.array([b_1, b_2])

                A_1_1 = np.sum(weights)
                A_2_1 = np.sum(weights * x)
                A_1_2 = np.sum(weights * x)
                A_2_2 = np.sum(weights * x * x)
                A = np.array([
                    [A_1_1, A_2_1],
                    [A_1_2, A_2_2],
                    ])
                # if beta is singular value then, return original y value
                beta = np.linalg.solve(A, b)
                yest[i] =  beta[1] * x[i] + beta[0]
            residuals = y - yest
            # calculate the weight: which is B(residual/6s)
            delta = np.clip(residuals / (constant * np.median(np.abs(residuals))), -1, 1)
            delta = np.square(1 - np.square(delta))
    except:
        return y
    return yest


def printRepeating(arr, size):

	print("Repeating elements are ",
						end = '')
	for i in range (0, size):
		for j in range (i + 1, size):
			if arr[i] == arr[j]:
				print(arr[i], end = ' ')

# Driver code
arr = [4, 2, 4, 5, 2, 3, 1]
arr_size = len(arr)
printRepeating(arr, arr_size)


from pygame import *
from math import *
import time
import os
import pickle

init()

screen=display.set_mode((800,600))
running = True
player=[60,449,50,50]
cube=[500,459,40,40]
state='idle'
while running:

     keys=key.get_pressed()
     mb=mouse.get_pressed()
     mx,my=mouse.get_pos()
     for e in event.get():
          if e.type==QUIT:
               running=False

     if keys[K_d]:
          if not Rect(player).colliderect(Rect(cube)):
               player[0]+=5
          else:
               player[0]+=1
               cube[0]+=1
          state='left'
     if keys[K_a]:
          player[0]-=5
          state='right'
     if not keys[K_a] and not keys[K_d]:
          state='idle'
     draw.rect(screen,(255,255,255),(0,0,800,600))
     draw.rect(screen,(0,200,20),(0,500,800,100))
     draw.rect(screen,(0,0,255),Rect(player))
     draw.rect(screen,(255,0,0),Rect(cube))
     print(state)

     display.flip()
quit()


import matplotlib.pyplot as plt
import pandas as pd

def plot_history(train_acc, val_acc, train_loss, val_loss, figsize=(10, 5),
                 dpi=300, acc_path='accuracy.png', loss_path='loss.png'):
  plt.figure(figsize=figsize)
  plt.plot(train_acc)
  plt.plot(val_acc)
  plt.title("Model Accuracy")
  plt.ylabel("Accuracy")
  plt.xlabel("Epoch")
  plt.savefig(acc_path, dpi=dpi)
  print(f"[INFO] Accuracy Figure saved to: {acc_path}")
  plt.figure(figsize=figsize)
  plt.plot(train_loss)
  plt.plot(val_loss)
  plt.title("Model Loss")
  plt.ylabel("Loss")
  plt.xlabel("Epoch")
  plt.savefig(loss_path, dpi=dpi)
  print(f"[INFO] Loss Figure saved to: {loss_path}")

def plot_loss_from_logs(csv_lists):
  print(f"[INFO] Extracting values from csv files...")
  for i, csv in enumerate(csv_lists):
      print(f"[INFO] Getting the values of file {csv}")
      csv_file = pd.read_csv(csv)
      train_acc = csv_file['accuracy']
      train_loss = csv_file['loss']
      val_acc = csv_file['val_accuracy']
      val_loss = csv_file['val_loss']
      # for now save each run independently
      plot_history(train_acc, val_acc, train_loss, val_loss, figsize=(10, 5),
                   dpi=300, acc_path=f'accuracy_{i}.png',
                   loss_path=f'loss_{i}.png')

if __name__ == '__main__':

  import os

  base_path = "/content/drive/MyDrive/FFTCustom"
  csv_lists = []
  for run_dir in os.listdir(base_path):
    run_dir_fullpath = os.path.join(base_path, run_dir)
    log_files = os.listdir(run_dir_fullpath)
    log_filename = "log.csv"
    csv_lists.append(os.path.join(run_dir_fullpath, log_filename))
    plot_loss_from_logs(csv_lists=csv_lists)


import re
import itertools
import datetime
import numpy as np

from dateparser.search import search_dates
from sklearn.feature_extraction.text import TfidfVectorizer

from autociter.data.storage import Table, Record
from autociter.data.queries import contains
from autociter.utils.debugging import debug
from autociter.utils.decorators import timeit

def standardize(data, datatype):
    """Define methods that standardize fields into a singular format that is logical
    and searchable
    """

    def std_markdown(markdown):
        """Remove the substring 'Image\n\n' from markdown."""
        for substring in {"Image\n\n"}:
            markdown = markdown.replace(substring, "")
        return markdown

    def std_html(html):
        """Remove script and style elements from HTML."""

        def remove_elements(html, tag_name):
            """Remove elements of a given type from HTML."""
            while "<" + tag_name in html:
                open_tag_start = html.find("<" + tag_name)
                open_tag_end = html.find(">", open_tag_start)
                close_tag_start = html.find("</" + tag_name, open_tag_end)
                close_tag_end = html.find(">", close_tag_start)
                html = html[:open_tag_start] + html[close_tag_end + 1:]
            return html

        html = remove_elements(html, "script")
        html = remove_elements(html, "style")
        return html

    def std_table(table):
        """Standardizes a default Table object so that it can be processed by
        pipeline. (i.e Author field is created from first, last, first1, last1, etc.)
        """
        std_fields = [
            "title", "author", "publisher", "date", "url", "archive-url"
        ]
        ret = Table(fields=std_fields)
        for rec in table.records:
            author_fields = [("first", "last"), ("first1", "last1"),
                             ("first2", "last2")]
            authors = []
            for i in author_fields:
                author = (rec[i[0]] + " " + rec[i[1]]).strip()
                if author != "":
                    authors.append(author)
            values = []
            for attr in std_fields:
                if contains(attr)(rec):
                    values.append(rec[attr])
                elif attr == 'author':
                    values.append(list(set(authors)))
                else:
                    values.append("")
            ret.add(Record(std_fields, values))
        return ret

    def std_text(text):
        """Standardizes a string representing article text
        Resources: https://stackoverflow.com/questions/19785458/capitalization-of-sentences-in-python
        """

        def clean_text(text):
            """Method that cleans a string to only include relevant characters and words"""
            text = text.replace('\'', '')
            text = text.replace('\"', '')
            matched_words = re.findall(r'\S+|\n', re.sub(
                "[^\w#\n]", " ", text))
            words_and_pound_newline = [
                i for i, j in itertools.zip_longest(
                    matched_words, matched_words[1:]) if i != j
            ]
            words_and_pound_newline = [('#' if '#' in x else x)
                                       for x in words_and_pound_newline]
            words_and_pound_newline = [(x.replace('_', '') if '_' in x else x)
                                       for x in words_and_pound_newline]
            ret = ''
            for i in range(len(words_and_pound_newline)):
                word = words_and_pound_newline[i]
                if i == 0 or word == '\n' or (
                        i > 0 and words_and_pound_newline[i - 1] == '\n'):
                    ret += word
                else:
                    ret += (" " + word)
            return ret

        def uppercase(matchobj):
            return matchobj.group(0).upper()

        def capitalize(s):
            return re.sub('^([a-z])|[\.|\?|\!]\s*([a-z])|\s+([a-z])(?=\.)',
                          uppercase, s)

        text = clean_text(text)
        return capitalize(text)

    def std_author(authors):
        """Method for standardizing a field if it is a list of authors"""
        return [
            ' '.join([
                name.capitalize() for name in author.replace('.', '').replace(
                    '-', ' ').split(' ')
            ]) for author in authors
        ]

    def std_date(date):
        """Method for standardizing a field if it is a date"""
        base = datetime.datetime(1000, 1, 1, 0, 0)
        matches = search_dates(
            date, settings={
                'STRICT_PARSING': True,
                'RELATIVE_BASE': base
            })
        if matches and matches[0][1] - base > datetime.timedelta(days=2 * 365):
            return matches[0][1].strftime('%m/%d/%y')
        return date.lower().replace(',', ' ').replace('-', ' ')

    def std_title(title):
        """Method for standardizing a field if it is a title"""
        return title.title()

    def std_url(url):
        """Method to standardize a url"""
        return url

    try:
        if datatype.lower() in [
                "markdown", "html", "table", "text", "author", "date", "title",
                "url"
        ]:
            return eval("std_{0}(data)".format(datatype.lower()))
        else:
            debug(
                "*** Error in standardization.standardize: {0} not standardizable"
                .format(datatype))
            return data
    except Exception as e:
        debug("*** Error in standardization.standardize: {0}".format(e))
        return ""

def find(field, text, datatype, start=0, end=None, threshold_value=0.6):
    """Attempts to locate a field as a substring of text based on its datatype.
    Assumes text is cleaned by pipeline's clean_text"""

    def find_generic(field, text, start=0, end=None):
        """Basic method for finding any generic field within a text"""
        text = text[start:end]
        index = text.find(field)
        if index != -1:
            return (start + index, start + index + len(field))
        return (-1, -1)

    def find_fuzzy_fast(field, text, start=0, end=None):
        """Fast fuzzy string matching
        References:
        https://bergvca.github.io/2017/10/14/super-fast-string-matching.html
        http://blog.christianperone.com/2013/09/machine-learning-cosine-similarity-for-vector-space-models-part-iii/
        https://stackoverflow.com/questions/52048562/efficient-way-to-compute-cosine-similarity-between-1d-array-and-all-rows-in-a-2d
        https://stackoverflow.com/questions/36013295/find-best-substring-match
        """

        def generate_chunks(string, length):
            return [
                string[0 + i:length + i]
                for i in range(0, len(string), length) if length + i < len(string)
            ]

        def generate_ngrams(string, n=3):
            """Helper method for generating ngrams"""
            ngrams = zip(*[string[i:] for i in range(n)])
            return [''.join(ngram) for ngram in ngrams]

        try:
            text = text[start:end]
            chunks = [field] + generate_chunks(text, len(field))
            vectorizer = TfidfVectorizer(min_df=1, analyzer=generate_ngrams)
            tf_idf_matrix = vectorizer.fit_transform(chunks)
            vec, arr = tf_idf_matrix[0].toarray().flatten(
            ), tf_idf_matrix[1:].toarray()
            a, b = np.linalg.norm(arr, axis=1), np.linalg.norm(vec)
            out = (arr @ vec) / (a * b)
            index = np.argmax(out) * len(field)

            ngrams = [field]
            for i in range(
                    max(0, index - len(field)), min(index + len(field),
                                                    len(text))):
                if i+len(field) >= len(text):
                    break
                ngrams.append(text[i:i + len(field)])

            vectorizer = TfidfVectorizer(min_df=1, analyzer=generate_ngrams)
            tf_idf_matrix = vectorizer.fit_transform(ngrams)
            vec, arr = tf_idf_matrix[0].toarray().flatten(
            ), tf_idf_matrix[1:].toarray()
            a, b = np.linalg.norm(arr, axis=1), np.linalg.norm(vec)
            out = (arr @ vec) / (a * b)
            ans = np.argmax(out) + max(0, index - len(field))
            if np.max(out) < threshold_value:
                return (-1, -1)
            ret = (ans, ans + len(field))
            return (start + ret[0].item(), start + ret[1].item())
        except Exception as e:
            return (-1, -1)

    def find_singular_author(author, text, start=0, end=None):
        """Helper method for finding a singular author"""
        return find_generic(author.lower(), text.lower(), start, end)

    def find_author(authors, text, start=0, end=None):
        """Method for finding an "author" field (a list of authors) in a text"""
        authors = standardize(authors, 'author')
        ret = []
        for author in authors:
            pos = find_singular_author(author, text, start, end)
            ret.append(pos)
        return ret

    def find_date(date, text, start=0, end=None):
        """Method for finding a date field in a text"""
        text = text[start:end]
        date = datetime.datetime.strptime(date, '%m/%d/%y')
        # Pass an impossible relative base so that relative words like "today" won't be detected
        matches = search_dates(
            text,
            settings={
                'STRICT_PARSING': True,
                'RELATIVE_BASE': datetime.datetime(1000, 1, 1, 0, 0)
            })
        if matches:
            for original_text, match in matches:
                if date.date() == match.date():
                    return find_generic(original_text.lower(), text.lower(), start, end)
        return (-1, -1)

    def find_title(title, text, start=0, end=None):
        """Method for finding a title field in a text
        ?? 'http://www.digitalspy.com/gaming/retro-corner/news/a381156/retro-corner-wolfenstein-3d/'
        """
        # return find_generic(title.lower(), text.lower())
        return find_fuzzy_fast(title.lower(), text.lower(), start, end)

    try:
        if datatype.lower() in ["author", "date", "title"]:
            return eval("find_{0}(field, text)".format(datatype.lower()))
        else:
            debug(
                "*** Warning in standardization.find: {0} not a findable field"
                .format(datatype))
            return find_generic(field, text, start, end)
    except Exception as e:
        debug("*** Error in standardization.find: {0}".format(e))
        return (-1, -1)

def clean_to_ascii(foreign_char):
    """Converts a non-ASCII character into it's ASCII equivalent

        >>> clean_to_ascii('ç')
        'c'
    """
    special_chars = {
        'a': ['à', 'á', 'â', 'ä', 'æ', 'ã', 'å', 'ā'],
        'c': ['ç', 'ć', 'č'],
        'e': ['è', 'é', 'ê', 'ë', 'ē', 'ė', 'ę'],
        'i': ['î', 'ï', 'í', 'ī', 'į', 'ì'],
        'l': ['ł'],
        'n': ['ñ', 'ń'],
        'o': ['ô', 'ö', 'ò', 'ó', 'œ', 'ø', 'ō', 'õ'],
        's': ['ß', 'ś', 'š'],
        'u': ['û', 'ü', 'ù', 'ú', 'ū'],
        'y': ['ÿ'],
        'z': ['ž', 'ź', 'ż'],
        'A': ['À', 'Á', 'Â', 'Ä', 'Æ', 'Ã', 'Å', 'Ā'],
        'C': ['Ç', 'Ć', 'Č'],
        'E': ['È', 'É', 'Ê', 'Ë', 'Ē', 'Ė', 'Ę'],
        'I': ['Î', 'Ï', 'Í', 'Ī', 'Į', 'Ì'],
        'L': ['Ł'],
        'N': ['Ñ', 'Ń'],
        'O': ['Ô', 'Ö', 'Ò', 'Ó', 'Œ', 'Ø', 'Ō', 'Õ'],
        'S': ['Ś', 'Š'],
        'U': ['Û', 'Ü', 'Ù', 'Ú', 'Ū'],
        'Y': ['Ÿ'],
        'Z': ['Ž', 'Ź', 'Ż']
    }
    if foreign_char in sum(special_chars.values(), []):
        for k in special_chars:
            if foreign_char in special_chars[k]:
                return k
    debug("Can't convert: " + str(foreign_char))
    return ' '


# -*- coding: UTF-8 -*-
"""Utility function for sending notifications (either local or by email).

"""
from .common import lazy_load_module
from .data.types import is_domain, is_email, is_file, is_port
from ..preimports import os

for _m in ["email", "mimetypes", "plyer", "smtplib"]:
    lazy_load_module(_m)

__all__ = __features__ = ["notify", "send_mail"]

SECURITY = {25: "unencrypted", 465: "ssl", 587: "starttls"}
SERVERS = {
    'aol.com':        ("smtp.aol.com", 465),
    'gmail.com':      ("smtp.gmail.com", 587),
    'gmx.com':        ("smtp.gmx.com", 465),
    'hotmail.com':    ("smtp.live.com", 465),
    'hubspot.com':    ("smtp.hubspot.com", 587),
    'mail.com':       ("smtp.mail.com", 465),
    'microsoft.com':  ("smtp.office365.com", 587),
    'outlook.com':    ("smtp.office365.com", 587),
    'pepipost.com':   ("smtp.pepipost.com", 587),
    'protonmail.com': ("smtp.protonmail.com", 465),
    'yahoo.com':      ("smtp.mail.yahoo.com", 587),
    'zoho.com':       ("smtp.zoho.com", 465),
}

def notify(title="", message="", app="", icon="", timeout=5, ticker=""):
    """ Shortcut to plyer.notification.notify, not considering the 'toast' option, and fail-safe.

    :param title:   title of the notification
    :param message: message of the notification
    :param app:     name of the app launching this notification
    :param icon:    name or path of the icon to be displayed along with the message
    :param timeout: time to display the message for
    :param ticker:  text to display on status bar as the notification arrives
    """
    try:
        plyer.notification.notify(title, message, app, icon, timeout, ticker)
    except NotImplementedError:
        pass

def send_mail(from_mail, to_mail, subject, body, *attachments, **kwargs):
    """ Send an email to a single receiver.

    :param from_mail:   sender's email address
    :param to_mail:     recipient's email address
    :param subject:     email subject
    :param body:        message body (HTML format)
    :param attachments: list of paths to files to be attached
    :param kwargs:      other email parameters (i.e. server, security, auth)
    """
    import email.mime
    ctype = kwargs.get('content_type', "plain")
    srv = kwargs.get('server')
    auth = kwargs.get('auth')
    auth_user, auth_pswd = (None, None) if auth is None else auth if len(auth) == 2 else (from_mail, auth)
    domain = (auth_user or from_mail).split("@")[-1]
    # parameters validation
    if not is_email(from_mail):
        raise ValueError("Bad sender email address")
    if not is_email(to_mail):
        raise ValueError("Bad recipient email address")
    if srv is None and domain in SERVERS.keys():
        try:
            host, port = SERVERS[domain]
        except KeyError:
            raise ValueError("Cannot find SMTP settings for %s" % domain)
    else:
        host, port = srv if isinstance(srv, tuple) and len(srv) == 2 else (srv, 25)
    if host is None or not is_domain(host) or not is_port(port):
        raise ValueError("Bad email server host or port")
    for path in attachments:
        if not is_file(path):
            raise ValueError("Attachment '%s' does not exist" % path)
    sec = kwargs.get('security', SECURITY.get(port, "unencrypted"))
    # create the email
    msg = email.mime.multipart.MIMEMultipart()
    msg['From'], msg['To'], msg['Subject'] = from_mail, to_mail, subject
    msg.attach(email.mime.text.MIMEText(body, ctype))
    for path in attachments:
        ctype, encoding = mimetypes.guess_type(path)
        if ctype is None or encoding is not None:
            ctype = 'application/octet-stream'
        maintype, subtype = ctype.split("/", 1)
        cls = globals().get("MIME" + maintype.capitalize())
        with open(path, 'rb') as f:
            if cls:
                submsg = cls(f.read(), _subtype=subtype)
            else:
                submsg = email.mime.base.MIMEBase(maintype, subtype)
                submsg.set_payload(f.read())
                encoders.encode_base64(submsg)
        submsg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(path))
        msg.attach(submsg)
    # send the email
    session = getattr(smtplib, "SMTP_SSL" if sec == "ssl" else "SMTP")(host, port)
    if sec == "starttls":
        session.starttls()
    if auth:
        session.login(auth_user, auth_pswd)
    session.sendmail(from_mail, to_mail, msg.as_string())
    session.quit()


# -*- coding: UTF-8 -*-
# check.py
# 目的：result.txtの内容の整合性を、何行目の情報かを見ていくことで確認する。

# 正規表現のライブラリをインポート
import re

# ファイルオープン
result = open('result_edit.txt', 'r')

# result.txtの内容を一行ずつ読み込む
lines = result.readlines()

# ファイルクローズ
result.close()

# カウント用の変数を作成
ok = 0
ng = 0

# 数字を格納するリストを作成
list1 = []
list2 = []

# result.txtの内容を一行ずつ読み込む
for line in lines:
	# 数字,数字の行を取り出してlist1に追加
	if re.search('[0-9]+,[0-9]+',line) != None:
		list1.append(line.replace('\n', ''))

	# line:数字の行を取り出してlist2に追加
	if re.match('.*line:.*',line) != None:
		list2.append(line.replace('\n', '').replace('line:', ''))

# デバック用
# print(list1)
# print(list2)

# リストの長さを取得
length = len(list2)
print(length)

# リストの中身を見ていく
for n in range(length):

	# a,b,cを3つ定義
	a = int(list1[n].split(',').pop(0)) # textの行
	b = int(list1[n].split(',').pop(1)) # 次のtextの行
	c = int(list2[n]) #wordの行

	print(a,b,c)

	# OK or NG 判定
	if a < c and c < b: # cがaとbの間にあったらOK
		ok += 1
		print('→OK\n')

	else: # そうでなかったらNG
		ng += 1
		print('→NG\n')

# 割合を計算
x = ok/length*100
y = ng/length*100

# カウントの結果を出力
print('~result~')
print('OK:',ok, x, '%')
print('NG:',ng, '\t', y, '%')


# -*- coding: utf-8 -*-
import zerorpc
import requests
import datetime

c = zerorpc.Client()
c.connect("tcp://192.168.2.154:4242")  # 连接到rpc服务器

user_name1 = "jack"
user_name2 = "tom"
b1 = datetime.datetime.now()
n = 500
for i in range(n):
    c.add_user(user_name1)
    c.add_user(user_name2)
    print(c.all_user(), end="    ")
e1 = datetime.datetime.now()
print()
print("{}次rpc调用耗时: {}秒".format(n, (e1 - b1).total_seconds()))
b2 = datetime.datetime.now()
ses = requests.Session()
u = "http://127.0.0.1:8003/some_api"
params = {"user_name": user_name1}
for i in range(n):
    r = ses.get(url=u, params=params)
    print(r.text, end="    ")
e2 = datetime.datetime.now()
print()
print("{}次restful调用耗时: {}秒".format(n, (e2 - b2).total_seconds()))


import torch
import numpy as np
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import random_split, DataLoader
from sklearn import metrics
from sklearn import tree
from sklearn.tree.export import export_text

from feature_dataset import SafetyFeatureDataset

class SafetyFeatureModel(nn.Module):
    def __init__(self, input_dim, hidden_dim):
        super(SafetyFeatureModel, self).__init__()
        self.layer = nn.Linear(input_dim, hidden_dim)
        self.output = nn.Linear(hidden_dim, 2)

    def forward(self, features):
        output = self.layer(features)
        output = self.output(output)
        return output

def train(model, training, validation, loss_fn, optimizer, num_epochs):
    for epoch in range(num_epochs):
        training_loss, validation_loss = [], []
        training_labels, validation_labels = [], []
        training_predictions, validation_predictions = [], []
        model.train()
        print("Going over training data")
        for i, (feature, label) in enumerate(training):
            if torch.cuda.is_available():
                feature = feature.cuda()
                label = label.cuda()
            model.zero_grad()
            output = model(feature)
            loss = loss_fn(output, label)
            loss.backward()
            optimizer.step()
            training_labels.append(label.cpu().detach().numpy())
            training_predictions.append(np.argmax(output.cpu().detach().numpy(), -1))
            training_loss.append(loss.item())
            training_accuracy = metrics.accuracy_score(training_labels[-1], training_predictions[-1])
            all_same = "ALL SAME" if len(set(training_predictions[-1])) == 1 else ""
            print("Iteration {} loss: {}, accuracy: {} {}".format(i, loss.item(), training_accuracy, all_same))
        model.eval()
        print("Going over validation data")
        for i, (feature, label) in enumerate(validation):
            if torch.cuda.is_available():
                feature = feature.cuda()
                label = label.cuda()
            output = model(feature)
            loss = loss_fn(output, label)
            validation_loss.append(loss.item())
            validation_labels.append(label.cpu().detach().numpy())
            validation_predictions.append(np.argmax(output.cpu().detach().numpy(), -1))
        training_accuracy = metrics.accuracy_score(np.concatenate(training_labels, 0), np.concatenate(training_predictions, 0))
        validation_accuracy = metrics.accuracy_score(np.concatenate(validation_labels, 0), np.concatenate(validation_predictions, 0))
        confusion_matrix = str(metrics.confusion_matrix(np.concatenate(validation_labels, 0), np.concatenate(validation_predictions, 0)))
        print("Epoch {}, Training loss: {}, Training accuracy: {}, Validation loss: {}, Validation accuracy: {}, Validation confusion matrix: {}".format(epoch, np.mean(training_loss), np.mean(validation_loss), training_accuracy, validation_accuracy, confusion_matrix))

if __name__ == '__main__':
    dataset = SafetyFeatureDataset("/Users/arkadyark/Downloads/data/labels/part-00000-e9445087-aa0a-433b-a7f6-7f4c19d78ad6-c000.csv", "/Users/arkadyark/Downloads/data/features/*.csv")
    train_size = int(0.9*len(dataset))
    val_size = len(dataset) - train_size
    training, val = random_split(dataset, [train_size, val_size])
    train_loader = DataLoader(training, batch_size=train_size, shuffle=True)
    val_loader = DataLoader(val, batch_size=val_size, shuffle=False)
    print("Dataset loaded!")
    trees = []
    for i, (feature, label) in enumerate(train_loader):
        for max_depth in (2, 3, 5, 7, 10):
            print("Max depth: {}".format(max_depth))
            clf = tree.DecisionTreeClassifier(max_depth=max_depth)
            clf = clf.fit(np.array(feature), np.array(label))
            training_predictions = clf.predict(np.array(feature))
            training_accuracy = metrics.accuracy_score(np.array(label), training_predictions)
            for j, (val_feature, val_label) in enumerate(val_loader):
                validation_predictions = clf.predict(np.array(val_feature))
                validation_accuracy = metrics.accuracy_score(np.array(val_label), validation_predictions)
                confusion_matrix = str(metrics.confusion_matrix(np.array(val_label), validation_predictions))
            print(training_accuracy)
            print(validation_accuracy)
            print(confusion_matrix)
            import pdb
            pdb.set_trace()
            r = export_text(clf, feature_names=('max_speed', 'max_dec', 'max_acc', 'max_acc_total'))
            print(r)
    # model = SafetyFeatureModel(4, 2)
    # weight = torch.tensor([1, 3], dtype=torch.float)
    # if torch.cuda.is_available():
    #     model.cuda()
    #     weight = weight.cuda()
    # loss_function = nn.CrossEntropyLoss(weight)
    # optimizer = optim.SGD(model.parameters(), lr=0.00001)
    # train(model, train_loader, val_loader, loss_function, optimizer, 500)


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Normal

class MDNDyn(nn.Module):
    def __init__(self, num_inputs, num_outputs,
                    n_hidden=200, n_gaussians=10):
        super(MDNDyn, self).__init__()

        self.linear1 = nn.Linear(num_inputs, n_hidden)

        # self.z_h = nn.Sequential(
        #     nn.Linear(num_inputs, n_hidden),
        #     nn.ReLU()
        # )

        self.z_pi = nn.Linear(n_hidden, n_gaussians)
        self.z_sigma = nn.Linear(n_hidden, n_gaussians * num_outputs)
        self.z_mu = nn.Linear(n_hidden, n_gaussians * num_outputs)

        self.num_outputs = num_outputs
        self.num_gauss   = n_gaussians
        self.num_inputs  = num_inputs

    def sample(self, x, u):
        pi, sigma, mu = self.forward(x, u)
        pi_picked = torch.multinomial(pi, 1)
        # pi_picked = torch.argmax(pi, dim=1, keepdim=True)
        res = []
        for i, r in enumerate(pi_picked):
            res.append(
                mu[i, r]#torch.normal(mu[i, r], sigma[i, r])
            )

        return torch.cat(res)

    def logits(self, x, u, y):
        pi, sigma, mu = self.forward(x, u)
        y_expand = y.unsqueeze(1).expand_as(mu)

        log_pi = torch.log(pi)
        log_pdf = -torch.log(sigma).sum(2)-0.5*((y_expand - mu) * torch.reciprocal(sigma)).pow(2).sum(2)
        return torch.logsumexp(log_pdf + log_pi, dim=1, keepdim=True)

    def forward(self, x, u):
        # z_h = self.z_h(torch.cat([x, u], dim=1))
        z_h = torch.sin(self.linear1(torch.cat([x, u], dim=1)))
        pi = nn.functional.softmax(self.z_pi(z_h) + 1e-3, -1)
        sigma = torch.clamp(self.z_sigma(z_h), -20, 4).exp()

        mu = self.z_mu(z_h).view(-1, self.num_gauss, self.num_outputs)
        mu = mu + x.unsqueeze(1).expand_as(mu)
        return pi, sigma.view(-1, self.num_gauss, self.num_outputs), mu

class MDNModel(nn.Module):

    def __init__(self, num_states, num_actions, def_layers=[200, 200],
                                                n_gaussians=10):

        super(MDNModel, self).__init__()

        self.num_states  = num_states
        self.num_actions = num_actions

        self.mdn_model = MDNDyn(num_states+num_actions, num_states,
                                    n_hidden=def_layers[0], n_gaussians=n_gaussians)
        self.n_params = []

        layers = [num_states] + def_layers + [1]
        for i, (insize, outsize) in enumerate(zip(layers[:-1], layers[1:])):
            var = 'rew_layer' + str(i)
            setattr(self, var, nn.Linear(insize, outsize))
            self.n_params.append(i)

    def predict_reward(self, s):
        #rew = torch.cat([s, a], axis=1)
        rew = s
        for i in self.n_params[:-1]:
            w = getattr(self, 'rew_layer' + str(i))
            rew = w(rew)
            rew = F.relu(rew)
            # rew = torch.sin(rew)

        w = getattr(self, 'rew_layer' + str(self.n_params[-1]))
        rew = w(rew)
        return rew

    def forward(self, s, a, ns):
        """
        dx, rew = forward(s, a)
        dx is the change in the state
        """

        rew = self.predict_reward(s)
        logprob = self.mdn_model.logits(s, a, ns)

        return logprob, rew

    def step(self, x, u):
        ns = self.mdn_model.sample(x, u)
        rew = self.predict_reward(x)
        return ns, rew


import xlwt
def HyperLink():
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    worksheet.col(0).width = 256 * 40
    worksheet.write(0, 0, xlwt.Formula(
        'HYPERLINK("https://www.baidu.com";"BaiDu")'))
    workbook.save('../excelDir/Test.xls')  # 保存文件
if __name__  == "__main__":
    HyperLink()


import cv2
vid = cv2.VideoCapture(0)
hand_xml=cv2.CascadeClassifier('D:/Device-Control-Using-OpenCV-and-ML/Device-Control-Using-OpenCV-and-ML/opencv/hand_detection.xml')

while True:
    rt,frame=vid.read()
    cv2.imwrite("dito.jpg",frame)
    temp=frame
    frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    (thresh, im_bw) = cv2.threshold(frame, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    contours,hierachy = cv2.findContours(im_bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(temp, contours, -1, (0,255,0), 3)

    palm = hand_xml.detectMultiScale(im_bw, 1.1, 4)

    cv2.imshow('Test',im_bw)
    cv2.imshow("color",temp)
    if cv2.waitKey(25) & 0xFF == ord('q'):

        break
vid.realese()
cv2.destroyAllWindows()


#!/usr/bin/env python
"""
Create local working environments for Python.
"""

import ConfigParser
import optparse
import os
import site
import stat
import sys
import types

EXTRA_SITE_PACKAGES = False
SYSTEM_SITE_PACKAGES = False
USER_SITE_PACKAGES = False

def process_commandline():
    global EXTRA_SITE_PACKAGES, SYSTEM_SITE_PACKAGES, USER_SITE_PACKAGES

    parser = optparse.OptionParser(usage="%prog [options] directory")
    parser.add_option(
        '-s',
        '--system-site-packages',
        dest="system_site_packages",
        action="store_true",
        default=False,
        help="Make system site packages available.")
    parser.add_option(
        '-u',
        '--user-site-packages',
        dest="user_site_packages",
        action="store_true",
        default=False,
        help="Make user site packages available.")
    options, args = parser.parse_args()
    SYSTEM_SITE_PACKAGES = options.system_site_packages
    USER_SITE_PACKAGES = options.user_site_packages
    if len(args) != 1:
        print >>sys.stderr, "Error: Only the environment directory should be specified"
        return None
    return args[0]

def create_envscripts(path):
    success = False
    if os.system("mkdir -p %s/bin" % (path,)) == 0:
        os.chdir(path)
        fp = open("bin/activate.sh", "w")
        print >>fp, "ENV_ROOT=%s" % (path,)
        print >>fp
        print >>fp, activate_script
        fp.close()

        fp = open("bin/create_environment.sh", "w")
        print >>fp, "#!/bin/bash"
        print >>fp, "ENV_ROOT=%s" % (path,)
        print >>fp
        print >>fp, "PY_SYSPREFIX=%s" % (sys.prefix,)
        print >>fp, "PY_VERSION=%d.%d" % (sys.version_info.major, sys.version_info.minor)
        print >>fp, create_script
        if os.name == 'posix':
            os.fchmod(fp.fileno(), stat.S_IRWXU
                                   |stat.S_IRGRP|stat.S_IXGRP
                                   |stat.S_IROTH|stat.S_IXOTH)
        fp.close()
        success = True
    if not success:
        print >>sys.stderr, "Failed to create ", path
    return success

# def getsitepackages(userpackages):
#     ret = []
#     defpath = "%s/lib/python%s.%s" % (sys.prefix,
#                                       sys.version_info.major,
#                                       sys.version_info.minor)
#     for dir in sys.path:
#         if (not dir.startswith(defpath)
#             and not dir == os.path.dirname(sys.argv[0])
#             and not dir in userpackages):
#             ret.append(dir)
#     return ret

def ensure_list(arg):
    if type(arg) != types.ListType:
        if arg:
            arg =  [ arg ]
        else:
            arg = []
    return arg

def create_sitecustomize(path):
    extra_sitepacks = []
    sitepacks = ensure_list(site.getsitepackages())
    user_sitepacks = ensure_list(site.getusersitepackages())

    fp = open("bin/sitecustomize.py", "w")

    print >>fp, 'import os, site, sys'

    print >>fp, 'EXTRA_SITE_PACKAGES = ' + str(EXTRA_SITE_PACKAGES)
    print >>fp, 'SYSTEM_SITE_PACKAGES = ' + str(SYSTEM_SITE_PACKAGES)
    print >>fp, 'USER_SITE_PACKAGES = ' + str(USER_SITE_PACKAGES)

    if extra_sitepacks:
        print >>fp, 'extra_sitepacks = ["' + '", "'.join(extra_sitepacks) + '"]'
    else:
        print >>fp, 'extra_sitepacks = [ ]'
    if sitepacks:
        print >>fp, 'sitepacks = ["' + '", "'.join(sitepacks) + '"]'
    else:
        print >>fp, 'sitepacks = [ ]'
    if user_sitepacks:
        print >>fp, 'user_sitepacks = ["' + '", "'.join(user_sitepacks) + '"]'
    else:
        print >>fp, 'user_sitepacks = [ ]'

    print >>fp, site_script

    fp.close()

# Body for "bin/activate.sh"
activate_script = """
deactivate () {
    # reset old environment variables
    if [ -n "$_OLDENV_PATH" ] ; then
        PATH="$_OLDENV_PATH"
        export PATH
        unset _OLDENV_PATH
    fi
    if [ -n "$_OLDENV_PYTHONHOME" ] ; then
        PYTHONHOME="$_OLDENV_PYTHONHOME"
        export PYTHONHOME
        unset _OLDENV_PYTHONHOME
    else
        unset PYTHONHOME
    fi

    # This should detect bash and zsh, which have a hash command that must
    # be called to get it to forget past commands.  Without forgetting
    # past commands the $PATH changes we made may not be respected
    if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
        hash -r 2>/dev/null
    fi

    if [ -n "$_OLDENV_PS1" ] ; then
        PS1="$_OLDENV_PS1"
        export PS1
        unset _OLDENV_PS1
    fi

    unset NONVIRTUAL_ENV
    if [ ! "$1" == "nondestructive" ] ; then
        # Self destruct!
        unset -f deactivate
    fi
}

_OLDENV_PATH="$PATH"
export PATH="${ENV_ROOT}/bin:$PATH"

if [ -n "${PYTHONHOME}" ] ; then
    _OLDENV_PYTHONHOME="${PYTHONHOME}"
    unset PYTHONHOME
fi
export PYTHONHOME="${ENV_ROOT}"

_OLDENV_PS1="$PS1"
export PS1="($(basename ${ENV_ROOT})) ${PS1}"
"""

# Body for "bin/create_environment.sh"
create_script = """
mkdir -p ${ENV_ROOT}/lib/python${PY_VERSION}/site-packages
mkdir -p ${ENV_ROOT}/include

find ${PY_SYSPREFIX}/bin -maxdepth 1 \\
         -name python\* \\
         -exec ln -fns {} ${ENV_ROOT}/bin \;

find ${PY_SYSPREFIX}/include -maxdepth 1 \\
         -exec ln -fns {} ${ENV_ROOT}/include \;

find ${PY_SYSPREFIX}/lib -maxdepth 1 \\
         \! -name python${PY_VERSION} \\
         -exec ln -fns {} ${ENV_ROOT}/lib \;

find ${PY_SYSPREFIX}/lib/python${PY_VERSION} -maxdepth 1 \\
         \! -name site-packages \! -name sitecustomize.\*  \\
         -exec ln -fns {} ${ENV_ROOT}/lib/python${PY_VERSION} \;

ln -s ${ENV_ROOT}/bin/sitecustomize.py ${ENV_ROOT}/lib/python${PY_VERSION}
"""

# Body for "bin/sitecustomize.py"
site_script = """
def list_remove(l, dir):
    i = 0
    while i < len(l):
        if l[i].startswith(dir):
            l.remove(l[i])
        else:
            i = i + 1

for dir in extra_sitepacks+sitepacks+user_sitepacks:
    list_remove(sys.path, dir)

if USER_SITE_PACKAGES:
    for dir in user_sitepacks:
        site.addsitedir(dir)
if SYSTEM_SITE_PACKAGES:
    for dir in sitepacks:
        site.addsitedir(dir)
if EXTRA_SITE_PACKAGES:
    for dir in user_sitepacks:
        site.addsitedir(dir)
"""

if __name__ == '__main__':
    path = process_commandline()
    if path:
        path = os.path.abspath(path)
        create_envscripts(path)
        create_sitecustomize(path)


'''
Sample code for Name Entity Recognition for a corpus
@author: Noushin
'''

import nltk
from nltk.tokenize import word_tokenize, sent_tokenize

def NER(input_file, output_file):
    output = open('{0}_NER.txt'.format(output_file), 'w')
    testset = open(input_file).readlines()
    for line in testset:
        line_clean = line.lower().strip()
        tokens = nltk.word_tokenize(line_clean)
        poss = nltk.pos_tag(tokens)
        mylist = []
        for w in poss:
            s = list(w)
            s1 = s[0].upper()
            tmp = (s1, w[1])
            mylist.append(tmp)
        ner_ = nltk.ne_chunk(mylist)
        output.write(str(ner_))
        output.write(u"----------------------\n")
    output.close()

#output = open('output.txt','w')
#testset = open("dev-muc3-0001-0100").readlines()

NER("dev-muc3-0001-0100","output")
'''
Sample calling:
NER("dev-muc3-0001-0100","output")
'''


#!/usr/bin/python3
"""
The base class of this project
"""

class Base():
    """Base class"""
    __nb_objects = 0

    def __init__(self, id=None):
        """
        Initialise a base class object
        Attr:
            id: the unique id of individual Base objects
        """
        if id:
            self.id = id
        else:
            Base.__nb_objects += 1
            self.id = Base.__nb_objects


from django.test import TestCase,SimpleTestCase
from .models import Post,Review,Categories
from django.urls import resolve, reverse
from django.contrib.auth.models import User
from . import views
from datetime import datetime
# Create your tests here.

class TestPostUrls(SimpleTestCase):

    def test_homepage_url_is_resolved(self):
        url = reverse("posts:homepage")
        self.assertEqual(resolve(url).func,views.HomeList)

    def test_createPost_url_is_resolved(self):
        url = reverse("posts:post-create")
        self.assertEqual(resolve(url).func.view_class,views.CreatePostView)

    def test_search_url_is_resolved(self):
        url = reverse("posts:search")
        self.assertEqual(resolve(url).func,views.SearchFunction)

    def test_user_profile_url_is_resolved(self):
        url = reverse("posts:profile",args=[1])
        self.assertEqual(resolve(url).func,views.userProfile)

    def test_editPost_url_is_resolved(self):
        url = reverse("posts:post-update",args=[1])
        self.assertEqual(resolve(url).func.view_class,views.UpdatePostView)

    def test_deletePost_url_is_resolved(self):
        url = reverse("posts:post-delete",args=[1])
        self.assertEqual(resolve(url).func.view_class,views.DeletePostView)

    def test_category_url_is_resolved(self):
        url = reverse("posts:category",args=[1])
        self.assertEqual(resolve(url).func,views.show_categories)

    def test_post_detail_url_is_resolved(self):
        url = reverse("posts:post-detail",args=[1])
        self.assertEqual(resolve(url).func,views.PostDetail)

class TestPost(TestCase):

    def setUp(self):
    #creating instances of objects in User
       self.user1 = User.objects.create(username="PmPranaya",email="Pmpranaya@gmail.com")
       self.category = Categories.objects.create(name="Arts")
       self.post = Post.objects.create(
            user=self.user1,
            title="Test Title",
            description="This is Test Description",
            date=datetime.now(),
            category=self.category,
            price = 1400)
       self.review = Review.objects.create(
           user=self.user1,
           date=datetime.now(),
           post=self.post,
           review="This is Test Review",
           rating=5
       )

    def test_post_data(self):
        post1 = Post.objects.get(title="Test Title")
        self.assertEqual(post1.description,"This is Test Description")
        self.assertEqual(post1.price,1400)
        self.assertEqual(post1.user.username,"PmPranaya")
        self.assertEqual(post1.category.name,"Arts")

    def test_review_data(self):
        review1 = Review.objects.get(review="This is Test Review")
        self.assertEqual(review1.user.username,"PmPranaya")
        self.assertEqual(review1.post.title,"Test Title")


import numpy as np
import pandas as pd
from sklearn.neighbors.dist_metrics import DistanceMetric

from data_statistics import UtilityOccupancyStatistics

class K_ward:
    """
    K-ward algorithm
    """
    def __init__(self, data, distance_metric,k,rep_mode,**kwargs):
        self.data = data # dataset where each row corresponds to an individual
        self.datasize = data.shape[0] # the number of indivduals contained in the database
        self.k = k # anonymity level
        self.groups = []
        self.group_num = 0
        self.upperbound = 2*self.k
        self.lowerbound = self.k
        self.distance_metric = distance_metric
        if self.distance_metric == 'self-defined':
            self.stat_util = Distance()
        self.rep_mode = rep_mode
        self.cards = []
        self.kwargs = kwargs

        if distance_metric == 'mahalanobis':
            self.VI = kwargs['VI']
        if distance_metric == 'self-defined':
            self.mode = kwargs['mode']
            if self.mode == 'window-usage':
                self.window = kwargs['window']

    def get_distance(self, d_profile_data):
        """
        Given the day profile, return the pairwise distance between each of the two individual series
        """
        data = d_profile_data.copy()
        data = data.fillna(0)
        data = data.as_matrix()
        data_size = data.shape[0]
        if self.distance_metric != 'self-defined':

            if self.distance_metric == 'euclidean':
                dist = DistanceMetric.get_metric('euclidean')
            elif self.distance_metric == 'mahalanobis':
                dist = DistanceMetric.get_metric('mahalanobis', VI=self.VI)
            distance = dist.pairwise(data)

        else:
            distance = np.empty((data_size,data_size))
            cols = d_profile_data.columns

            for i in range(data_size):
                df1 = data[i, :]
                for j in range(data_size):
                    df2 = data[j,:]
                    if self.mode == 'window-usage':
                        distance[i, j] = self.stat_util.get_statistic_distance(df1, df2, index=cols,
                                                                               mode=self.mode,window=self.window)
                    else:
                        distance[i,j] = self.stat_util.get_statistic_distance(df1,df2,index=cols,mode=self.mode)

        df = pd.DataFrame(distance)
        df.columns = d_profile_data.index
        df.index = d_profile_data.index

        distance = df
        x, y = np.meshgrid(distance.index, distance.columns)
        df = pd.DataFrame(columns=["x", "y", "distance"])
        df["x"] = y.ravel()
        df["y"] = x.ravel()
        df["distance"] = distance.as_matrix().ravel()

        df = df[df["x"] != df["y"]]
        df = df.sort_values('distance')
        df.distance.loc[np.isnan(df.distance)] = 0
        return df

    def add_group(self,group):
        self.groups.append(group)
        self.group_num += 1

    def initialize_groups(self):
        # boolean variable indicating if each individual has been assigned or not
        group_assign_status = dict(zip(self.data.index,np.zeros(self.datasize)))
        pairwise_dist = self.get_distance(self.data)
        max_dist = pairwise_dist[pairwise_dist["distance"] == pairwise_dist["distance"].max()]
        max_dist = max_dist.iloc[-1, :]
        extreme_points = [max_dist["x"], max_dist["y"]]
        for extreme_p in extreme_points:

            # select the neighbors of the extreme point
            df_sub = pairwise_dist[pairwise_dist["x"]== extreme_p]

            # if the neighbor has been assigned to a group, then delete
            non_assigned_ind = [i for i in range(len(df_sub)) if group_assign_status[int(df_sub["y"].iloc[i])] == 0]
            df_sub = df_sub.iloc[non_assigned_ind]

            if df_sub.shape[0] == 0:
                print("df_sub error")

            # select the k-1 nearest neighbors
            df_near_neighbors = df_sub.sort_values("distance",ascending=True).iloc[0:self.k-1]

            keys = np.concatenate([df_near_neighbors["y"].values, np.array([int(extreme_p)])])
            values = self.data.loc[keys].as_matrix()

            new_group = Group(keys,values,self.rep_mode)

            for i in range(len(keys)):
                group_assign_status[keys[i]] = 1
            self.add_group(new_group)

        for key in group_assign_status.keys():
            if group_assign_status[key] == 0:
                new_group = Group(id=[key], data=self.data.loc[key].as_matrix(), rep_mode=self.rep_mode)
                self.add_group(new_group)

        self.cards = self.get_cards()

    def get_cards(self):
        cards = [self.groups[i].get_card() for i in range(self.group_num)]
        return cards

    def compare_and_merge(self):
        cards = self.cards
        centroid_df = pd.DataFrame()
        for i in range(self.group_num):
            centroid_df = centroid_df.append(self.groups[i].rep,ignore_index=True)

        pairwise_dist = self.get_distance(centroid_df)
        # never merge two groups that both have size >= k
        good_keys = [i for i in range(len(cards)) if cards[i]<self.lowerbound]

        #guaratee that at least one group has cardinality <k
        pairwise_dist_good = pairwise_dist[np.logical_or(pairwise_dist['x'].isin(good_keys), pairwise_dist['y'].isin(good_keys))]

        min_dist = pairwise_dist_good[pairwise_dist_good["distance"] == pairwise_dist_good["distance"].min()]
        min_dist = min_dist.iloc[-1,:]
        group1_id = int(min_dist["x"])
        group2_id = int(min_dist["y"])
        self.merge(group_id1=group1_id,group_id2=group2_id)

    def merge(self,group_id1,group_id2):
        group1 = self.groups[group_id1]
        group2 = self.groups[group_id2]
        group1.merge_group(group2)
        self.cards[group_id1] = group1.get_card()
        del self.groups[group_id2]
        del self.cards[group_id2]
        self.group_num -= 1

    def replace(self,group_id,groups):
        groups_num = len(groups)
        for i in range(groups_num):
            self.groups.append(groups[i])
            self.cards.append(groups[i].get_card())
        del self.groups[group_id]
        del self.cards[group_id]
        self.group_num = self.group_num - 1 + groups_num

    def get_cluster(self):
        self.initialize_groups()
        i = 0
        while all(card >= self.lowerbound for card in self.cards) is False:
            self.compare_and_merge()

        card_status = [card >= self.upperbound for card in self.cards]
        while any(card_status):
            recurse_ids = [i for i in range(len(card_status)) if card_status[i] == True]
            recurse_id = recurse_ids[0]

            recurse_group = self.groups[recurse_id]
            recurse_df = recurse_group.get_dataframe()
            if len(recurse_df) < self.upperbound:
                print("error")
            recurse_kward = K_ward(data=recurse_df,k=self.k,distance_metric=self.distance_metric,
                                   rep_mode = self.rep_mode,**self.kwargs)
            recurse_kward.get_cluster()
            new_groups = recurse_kward.groups
            self.replace(recurse_id,new_groups)
            card_status = [card >= self.upperbound for card in self.cards]

class Group:
    def __init__(self, id=None, data=None, rep_mode="mean"):
        self.member = {}

        if id is not None:
            if len(id) > 1:
                self.member = dict(zip(id, data))
            else:
                self.member[id[0]] = data
        self.card = len(self.member)
        self.rep_mode = rep_mode
        self.rep = self.get_rep()

    def get_members(self):
        return self.member

    def get_card(self):
        return self.card

    def get_member_ids(self):
        return list(self.member.keys())

    def get_rep(self):
        df = pd.DataFrame().from_dict(self.member, orient='index')

        if self.rep_mode == 'mean':
            rep =  df.mean(axis=0)
        elif self.rep_mode == 'mean-round':
            rep = df.mean(axis=0).round()
        elif self.rep_mode == 'median':
            random_num = np.random.uniform(0,1,(1,len(df.columns)))[0]
            random_ind = []
            for i in range(len(random_num)):
                if random_num[i]>0.5:
                    random_ind.append(-0.1)
                else:
                    random_ind.append(0.1)
            rep = np.around(df.median(axis=0) + random_ind)
        return rep

    def get_member_data(self, identify):
        return self.member[identify]

    def merge_group(self, group):
        new_members = group.get_members()
        self.member = {**self.member,**new_members}

        self.card = self.card + group.get_card()
        self.rep = self.get_rep()

    def get_dataframe(self):
        df = pd.DataFrame().from_dict(self.member).transpose()
        return df

class Distance:
    def get_statistic_distance(self,x_df1,x_df2,index,mode,**kwargs):
        util = UtilityOccupancyStatistics()
        if mode == "arrival":
            stat1 = util.compute_arrival_time(x_df1,index,1)
            stat2 = util.compute_arrival_time(x_df2,index,1)
        elif mode == "departure":
            stat1 = util.compute_departure_time(x_df1, index,1)
            stat2 = util.compute_departure_time(x_df2, index,1)
        elif mode == "usage":
            stat1 = util.compute_total_usage(x_df1, index)
            stat2 = util.compute_total_usage(x_df2, index)
        elif mode == "window-usage":
            window = kwargs['window']
            stat1 = util.compute_window_usage(x_df1, index,window)
            stat2 = util.compute_window_usage(x_df2, index,window)
        dist = abs(stat1-stat2)
        return dist


from django.db.models.signals import post_save
from .models import *
from django.dispatch import receiver
from HIS import settings

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_profile_for_new_user(sender, **kwargs):

    if kwargs['created']:
        user = kwargs['instance']
        if user.role == 'pharmacist':
            Pharmacist.objects.create(user=user)

@receiver(post_save, sender=Prescription)
def update_drug_stock(sender, instance, **kwargs):
    if instance.dispensed_status == 'dispensed':
        for item in instance.prescription.all():
            if item.dispensed == True:
                drug = item.drug
                drug.stock -= item.amount
                drug.save()


from decimal import Decimal
from django.db import models
from .abstract_model import TimestampedModel

class Category(models.Model):
    name = models.CharField(
        max_length=100, verbose_name='Category name'
    )
    slug = models.SlugField(
        max_length=70, unique=True
    )

    class Meta:
        verbose_name_plural = 'Categories'

    def __str__(self):
        return self.name

class Size(models.Model):
    name = models.CharField(max_length=5)

    def __str__(self):
        return self.name

class Brand(models.Model):
    name = models.CharField(max_length=50)
    logo = models.ImageField(upload_to='images/brand', blank=True, null=True)

class Image(models.Model):
    product = models.ForeignKey(
        'shop.Product',
        on_delete=models.CASCADE,
        related_name='images',
    )
    image = models.ImageField(
        upload_to='images/product', max_length=300
    )
    url = models.URLField(
        max_length=512, verbose_name='Image URL'
    )
    size = models.PositiveIntegerField(
        blank=True, null=True
    )

    def __str__(self):
        return self.image.url

class Product(TimestampedModel):
    source_url = models.URLField(
        verbose_name='Source URL', max_length=500
    )
    title = models.CharField(
        max_length=200
    )
    slug = models.SlugField(
        max_length=150, unique=True
    )
    description = models.TextField(
        blank=True, default=''
    )
    characteristics = models.JSONField(
        default=dict, blank=True
    )
    price = models.DecimalField(
        max_digits=10, decimal_places=2,
        blank=True, null=True
    )
    old_price = models.DecimalField(
        max_digits=10, decimal_places=2,
        blank=True, null=True, verbose_name='Old Price'
    )
    discount = models.DecimalField(
        max_digits=10, decimal_places=2,
        blank=True, null=True
    )
    article = models.CharField(
        max_length=30
    )
    categories = models.ManyToManyField(
        Category,
        related_name='products'
    )
    sizes = models.ManyToManyField(
        Size,
        related_name='products'
    )
    brand = models.ForeignKey(
        Brand,
        related_name='products',
        on_delete=models.CASCADE
    )

    def save(self, *args, **kwargs) -> None:
        if self.old_price:
            self.discount = Decimal(
                round((float(self.price) / float(self.old_price))
                      * 100 - 100, 2)
            )
        super().save(*args, **kwargs)

    def __str__(self):
        return self.title


# https://atcoder.jp/contests/arc138/tasks/arc138_b
# # def input(): return sys.stdin.readline().rstrip()
# # input = sys.stdin.readline
# from numba import njit
# from functools import lru_cache

import sys
input = sys.stdin.buffer.readline
# sys.setrecursionlimit(10 ** 7)

def main():
    N = int(input())
    A = list(map(int, (input().split())))
    flag = 1
    l, r = 0, N-1
    while l<=r:
        # print(l, r, flag)
        if A[r]^flag:
            r -= 1
        elif A[l]^flag:
            l += 1
            flag = 1 - flag
        else:
            print("No")
            return
    print("Yes")
    return

main()

# S = input()
# n = int(input())
# N, K = map(int, input().split())
# l = list(map(int, (input().split())))
# A = [[int(i) for i in input().split()] for _ in range(N)]

# import sys
# it = map(int, sys.stdin.buffer.read().split())
# N = next(it)

# @njit('(i8,i8[::1],i4[::1])', cache=True)
# def main():
#     @lru_cache(None)
#     def dfs():
#         return
#     return

# main()


#https://www.hackerrank.com/challenges/insertionsort1/problem

def insertionSort1(n, arr):
    for i in range(n):
        current=arr[i]

        j=i-1
        while (j>=0 and arr[j]>current):
            arr[j+1]=arr[j];
            j=j-1;
            print(arr)
        arr[j+1]=current

    print(arr)


class Node:
	def __init__(self, d):
		self.data = d
		self.next = None

head = None

def detectCycle(head):
	slowPointer = head
	fastPointer = head

	while (slowPointer != None and fastPointer != None and fastPointer.next != None):
		slowPointer = slowPointer.next
		fastPointer = fastPointer.next.next
		if (slowPointer == fastPointer):
			return 1

	return 0

head = Node(12)
head.next = Node(23)
head.next.next = Node(13)
head.next.next.next = Node(36)
head.next.next.next.next = Node(44)

temp = head
while (temp.next != None):
	temp = temp.next

temp.next = head

if (detectCycle(head)):
	print("Cycle exists in the Linked List")
else:
	print("Cycle does not exists in the Linked List")


from utils import DealData

class SearchElm:

    def __init__(self, ganZhiDict=None):
        if ganZhiDict:
            self.ganZhiDict = ganZhiDict.copy()
            self.values = list(ganZhiDict.values())
            for i in range(len(self.values)):
                self.values[i] = DealData.getZhi(self.values[i])

        self.pat = ""
        self.type = ""
        self.year = 0
        self.month = []
        self.dateStr = []
        self.combine = {}

    def __str__(self):
        s = "查找类型为%s，匹配的组是%s，是%d年，" % (self.type, self.pat, self.year)
        for key in self.combine:
            s = s + "其中" + key + "为" + self.combine[key] + "，"
        return s

    def setPattern(self, pat):
        self.pat = ""
        for ch in pat:
            self.pat += ch

    def markLiuYue(self, liuYue):
        for yue in liuYue:
            month = yue.getIndex() + 1
            yueZhi = DealData.getZhi(yue.getGanZhi())
            if self.type == "三合" or self.type == "反拱":
                mid = self.pat[1]
                if yueZhi == mid:
                    self.month.append(month)
            elif self.type == "对冲" or self.type == "复吟":
                if yueZhi == DealData.getZhi(self.combine["流年"]):
                    self.month.append(month)
        self.setDateStr()

    def setDateStr(self):
        for m in self.month:
            ds = str(self.year) + '-' + str(m)
            self.dateStr.append(ds)

def searchSpecType(searchType, *args):
    if searchType == "复吟":
        return searchFuYin(*args)
    else:
        return searchPattern(searchType, *args)

def searchPattern(searchType, ganZhiDict, year, patList):
    targetPat = None
    for pat in patList:
        if DealData.getZhi(ganZhiDict['流年']) in pat:
            targetPat = pat.copy()
            break
    if targetPat is None:
        return None
    searchElm = SearchElm(ganZhiDict)
    searchElm.setPattern(targetPat)
    targetPat.remove(DealData.getZhi(ganZhiDict['流年']))
    if set(targetPat).issubset(set(searchElm.values)):
        searchElm.combine["流年"] = DealData.getZhi(ganZhiDict["流年"])
        searchElm.year = year
        for key in ganZhiDict:
            if DealData.getZhi(ganZhiDict[key]) in targetPat:
                searchElm.combine[key] = DealData.getZhi(ganZhiDict[key])
        searchElm.type = searchType
        return searchElm
    else:
        return None

#   查询复吟
def searchFuYin(ganZhiDict, year, patList):
    elm = SearchElm()
    elm.year = year
    elm.type = "复吟"
    liuNianGanZhi = ganZhiDict["流年"]
    for key in ganZhiDict:
        if key != "流年" and ganZhiDict[key] == liuNianGanZhi:
            elm.combine[key] = ganZhiDict[key]
    if len(elm.combine) == 0:
        return None
    else:
        elm.combine["流年"] = liuNianGanZhi
        return elm


# Essa é a implementação de um jogo de adivinhação de numeros.

import random

print('Olá, qual o seu nome?')
name = input()

numberMin = 0
numberMax = 20
secretNumber = random.randint(numberMin, numberMax)

print("Hm... %s, eu estou pensando em um numero entre %s e %s." % (name, numberMin, numberMax))

# Perguntar ao jogador um numero definido de vezes, por exemplo, se setar a função range como range(1,7)
# o jogador terá 6 tentativas com o range iniciando em 1 e indo até 7.

for guessesTaken in range(1, 7):
    print('Dê um palpite.')
    guess = int(input())
    if(guess < secretNumber):
        print('Você chutou muito baixo')
    elif(guess > secretNumber):
        print('Você chutou muito Alto')
    else:
        break

if(guess == secretNumber):
    print('Bom trabalho %s, você acertou o meu numero em %s tentativas.' % (name, guessesTaken))
else:
    print('Não, o número em que eu estava pensando era %s' % (secretNumber))


"""
数据预处理
"""
from config import *
import os
import numpy as np
import cv2
import shutil
import sys
import datetime

from pfld.utils import calculate_pitch_yaw_roll

def rotate(angle, center, landmark):
    rad = angle * np.pi / 180.0
    alpha = np.cos(rad)
    beta = np.sin(rad)
    M = np.zeros((2,3), dtype=np.float32)
    M[0, 0] = alpha
    M[0, 1] = beta
    M[0, 2] = (1-alpha)*center[0] - beta*center[1]
    M[1, 0] = -beta
    M[1, 1] = alpha
    M[1, 2] = beta*center[0] + (1-alpha)*center[1]

    landmark_ = np.asarray([(M[0,0]*x+M[0,1]*y+M[0,2],
                             M[1,0]*x+M[1,1]*y+M[1,2]) for (x,y) in landmark])
    return M, landmark_

class ImageData():
    def __init__(self, line, imgDir, image_size=112):
        """
        初始化ImageData, 从landmark记录中读取图像属性
        """
        self.image_size = image_size
        line = line.strip().split()
        #0-195: landmark 坐标点  196-199: bbox 坐标点;
        #200: 姿态(pose)         0->正常姿态(normal pose)          1->大的姿态(large pose)
        #201: 表情(expression)   0->正常表情(normal expression)    1->夸张的表情(exaggerate expression)
        #202: 照度(illumination) 0->正常照明(normal illumination)  1->极端照明(extreme illumination)
        #203: 化妆(make-up)      0->无化妆(no make-up)             1->化妆(make-up)
        #204: 遮挡(occlusion)    0->无遮挡(no occlusion)           1->遮挡(occlusion)
        #205: 模糊(blur)         0->清晰(clear)                    1->模糊(blur)
        #206: 图片名称
        assert(len(line) == 207)
        self.list = line
        self.landmark = np.asarray(list(map(float, line[:196])), dtype=np.float32).reshape(-1, 2)
        self.box = np.asarray(list(map(int, line[196:200])),dtype=np.int32)
        flag = list(map(int, line[200:206]))
        flag = list(map(bool, flag))
        self.pose = flag[0]
        self.expression = flag[1]
        self.illumination = flag[2]
        self.make_up = flag[3]
        self.occlusion = flag[4]
        self.blur = flag[5]
        self.path = os.path.join(imgDir, line[206])
        self.img = None

        self.imgs = []
        self.landmarks = []
        self.boxes = []

    def load_data(self, is_train, repeat, mirror=None):
        if (mirror is not None):
            with open(mirror, 'r') as f:
                lines = f.readlines()
                assert len(lines) == 1
                mirror_idx = lines[0].strip().split(',')
                mirror_idx = list(map(int, mirror_idx))
        xy = np.min(self.landmark, axis=0).astype(np.int32)
        zz = np.max(self.landmark, axis=0).astype(np.int32)
        wh = zz - xy + 1

        center = (xy + wh/2).astype(np.int32)
        img = cv2.imread(self.path)
        boxsize = int(np.max(wh)*1.2)
        xy = center - boxsize//2
        x1, y1 = xy
        x2, y2 = xy + boxsize
        height, width, _ = img.shape
        dx = max(0, -x1)
        dy = max(0, -y1)
        x1 = max(0, x1)
        y1 = max(0, y1)

        edx = max(0, x2 - width)
        edy = max(0, y2 - height)
        x2 = min(width, x2)
        y2 = min(height, y2)

        imgT = img[y1:y2, x1:x2]
        if (dx > 0 or dy > 0 or edx > 0 or edy > 0):
            imgT = cv2.copyMakeBorder(imgT, dy, edy, dx, edx, cv2.BORDER_CONSTANT, 0)
        if imgT.shape[0] == 0 or imgT.shape[1] == 0:
            imgTT = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            for x, y in (self.landmark+0.5).astype(np.int32):
                cv2.circle(imgTT, (x, y), 1, (0, 0, 255))
            cv2.imshow('0', imgTT)
            if cv2.waitKey(0) == 27:
                exit()
        imgT = cv2.resize(imgT, (self.image_size, self.image_size))
        landmark = (self.landmark - xy)/boxsize
        assert (landmark >= 0).all(), str(landmark) + str([dx, dy])
        assert (landmark <= 1).all(), str(landmark) + str([dx, dy])
        self.imgs.append(imgT)
        self.landmarks.append(landmark)

        if is_train:
            while len(self.imgs) < repeat:
                angle = np.random.randint(-30, 30)
                cx, cy = center
                cx = cx + int(np.random.randint(-boxsize*0.1, boxsize*0.1))
                cy = cy + int(np.random.randint(-boxsize * 0.1, boxsize * 0.1))
                M, landmark = rotate(angle, (cx,cy), self.landmark)

                imgT = cv2.warpAffine(img, M, (int(img.shape[1]*1.1), int(img.shape[0]*1.1)))

                wh = np.ptp(landmark, axis=0).astype(np.int32) + 1
                size = np.random.randint(int(np.min(wh)), np.ceil(np.max(wh) * 1.25))
                xy = np.asarray((cx - size // 2, cy - size//2), dtype=np.int32)
                landmark = (landmark - xy) / size
                if (landmark < 0).any() or (landmark > 1).any():
                    continue

                x1, y1 = xy
                x2, y2 = xy + size
                height, width, _ = imgT.shape
                dx = max(0, -x1)
                dy = max(0, -y1)
                x1 = max(0, x1)
                y1 = max(0, y1)

                edx = max(0, x2 - width)
                edy = max(0, y2 - height)
                x2 = min(width, x2)
                y2 = min(height, y2)

                imgT = imgT[y1:y2, x1:x2]
                if (dx > 0 or dy > 0 or edx >0 or edy > 0):
                    imgT = cv2.copyMakeBorder(imgT, dy, edy, dx, edx, cv2.BORDER_CONSTANT, 0)

                imgT = cv2.resize(imgT, (self.image_size, self.image_size))

                if mirror is not None and np.random.choice((True, False)):
                    landmark[:,0] = 1 - landmark[:,0]
                    landmark = landmark[mirror_idx]
                    imgT = cv2.flip(imgT, 1)
                self.imgs.append(imgT)
                self.landmarks.append(landmark)

    def save_data(self, path, prefix):
        attributes = [self.pose, self.expression, self.illumination, self.make_up, self.occlusion, self.blur]
        attributes = np.asarray(attributes, dtype=np.int32)
        attributes_str = ' '.join(list(map(str, attributes)))
        labels = []
        TRACKED_POINTS = [33, 38, 50, 46, 60, 64, 68, 72, 55, 59, 76, 82, 85, 16]
        for i, (img, lanmark) in enumerate(zip(self.imgs, self.landmarks)):
            assert lanmark.shape == (98, 2)
            save_path = os.path.join(path, prefix+'_'+str(i)+'.png')
            assert not os.path.exists(save_path), save_path
            cv2.imwrite(save_path, img)

            euler_angles_landmark = []
            for index in TRACKED_POINTS:
                euler_angles_landmark.append(lanmark[index])
            euler_angles_landmark = np.asarray(euler_angles_landmark).reshape((-1, 28))
            pitch, yaw, roll = calculate_pitch_yaw_roll(euler_angles_landmark[0])
            euler_angles = np.asarray((pitch, yaw, roll), dtype=np.float32)
            euler_angles_str = ' '.join(list(map(str, euler_angles)))

            landmark_str = ' '.join(list(map(str,lanmark.reshape(-1).tolist())))

            label = '{} {} {} {}\n'.format(save_path, landmark_str, attributes_str, euler_angles_str)

            labels.append(label)
        return labels

def process_data_list(imgDir,outDir,landmarkFile,is_train:bool,sample_num=-1,train_repeat=1):
    """
    对landmarkFile每一行表示的图像进行处理
    """
    # 读取landmark
    with open(landmarkFile,'r') as f:
        all_lines = f.readlines()
        if sample_num==-1:
            lines=all_lines
        else:
            lines=all_lines[:sample_num]
        labels = []
        save_img = os.path.join(outDir, 'imgs')
        os.mkdir(save_img)

        for i, line in enumerate(lines):
            # 读取和处理图像
            Img = ImageData(line, imgDir)
            img_name = Img.path
            Img.load_data(is_train, train_repeat, MIRROR_FILE)
            _, filename = os.path.split(img_name)
            filename, _ = os.path.splitext(filename)
            label_txt = Img.save_data(save_img, str(i)+'_' + filename)
            labels.append(label_txt)
            if ((i + 1) % 100) == 0:
                print('Processed file: {}/{}'.format(i+1, len(lines)))

    with open(os.path.join(outDir, 'list.txt'),'w') as f:
        for label in labels:
            f.writelines(label)

if __name__ == '__main__':
    # 创建data路径
    if not os.path.exists(DATA_DIR):
        os.mkdir(DATA_DIR)
    # 对已处理数据进行改名处理
    dt_now=datetime.datetime.now().ctime()
    datas_exist=os.listdir(DATA_DIR)
    for i in datas_exist:
        if i==OUT_DIRS["train"] or i==OUT_DIRS["test"]:
            os.rename(DATA_DIR+os.sep+i,DATA_DIR+os.sep+i+'_'+dt_now)
    # 设置数据目录
    train_outDir=os.path.join(DATA_DIR,OUT_DIRS["train"])
    test_outDir=os.path.join(DATA_DIR,OUT_DIRS["test"])
    train_landmark_file=LANDMARK_FILES["train"]
    test_landmark_file=LANDMARK_FILES["test"]
    os.mkdir(train_outDir)
    os.mkdir(test_outDir)
    # 数据预处理
    print("---Processing train data---")
    process_data_list(IMAGE_DIRS,train_outDir,train_landmark_file,is_train=True,sample_num=TRAIN_SAMPLE_NUM,train_repeat=10)
    print("---Processing test data---")
    process_data_list(IMAGE_DIRS,test_outDir,test_landmark_file,is_train=False,sample_num=TEST_SAMPLE_NUM,train_repeat=1)
    print("Data preparation Done")


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import time
import re

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyser = SentimentIntensityAnalyzer()

vaderData = pd.read_csv("VADER_Pruned_Final.csv")
vaderData.drop('Unnamed: 0', axis=1, inplace=True)
vaderData.drop('Unnamed: 0.1', axis=1, inplace=True)

scores = []
# Declare variables for scores
compound_list = []
positive_list = []
negative_list = []
neutral_list = []
for i in range(vaderData['text'].shape[0]):
#print(analyser.polarity_scores(sentiments_pd['text'][i]))
    compound = analyser.polarity_scores(vaderData['text'][i])["compound"]
    pos = analyser.polarity_scores(vaderData['text'][i])["pos"]
    neu = analyser.polarity_scores(vaderData['text'][i])["neu"]
    neg = analyser.polarity_scores(vaderData['text'][i])["neg"]

    scores.append({"Compound": compound,
                       "Positive": pos,
                       "Negative": neg,
                       "Neutral": neu
                  })

sentiments_score = pd.DataFrame.from_dict(scores)
vaderData = vaderData.join(sentiments_score)
vaderData.to_csv("VADER_Sent_Scores.csv", index=True, encoding='utf8')


# -*- coding: utf-8 -*-
import config

__author__ = 'magenn'

import requests
import json
import base64
from insurance_crm.dal import dal_django
from insurance_site import settings

def send_sign_document_request_to_client(client_id):
    client = dal_django.get_client_from_id(client_id)
    agent = client.agent
    agent_name = agent.first_name + " " + agent.last_name

    headers = {'Content-type': 'application/json',
               'Accept': 'application/json',
               "Authorization": "Token  {0}".format(settings.CELLOSIGN_AUTHORIZATION_TOKEN)
               }

    inputvalues = {
        "client_name":"%s %s" % (client.first_name, client.last_name),
        "client_id":client.id_number,
        "agent_name":agent_name,
        "agent_name_2":agent_name,
        "agent_license_number":agent.license_number,
        "agent_phone":agent.phone_number,
    }
    data ={
        "input_file": inputvalues,
        "destination": client.phone_number,  # post SMS w/link to client
        "template": "bd343311cd4645078c9fd94b5b1960b3",
        "rep_email": config.MAIL_ADDRESS,
        "client_redirect": "http://www.google.com",
        "invite_message": config.CLIENT_INVITE_MESSAGE,
        "client_id": client.id
    }
    json_file = json.dumps(data)

    request = requests.post(config.CELLOSIGN_REQUEST_ADDRESS,
                            headers=headers, data=json_file)

    #return request
    return "NIR"

'''
data={
    "client_copy": "omer@cellosign.com",
    "rep_email": ["omer@cellosign.com","iris@cellosign.com"],
    "template":"283c6009a4aa4b1e89ca3ac6b880f25b"
}
input_file = {
    "customer_name" : "קובי שגב",
    "customer_id": "028732436"
}
data['input_file'] = input_file
data = json.dumps(data)

request = requests.post('http://test.cellosign.com/ayalon/rep/create_session',headers=headers,data=data)

'''


def select_student_ok(list,seuil):
    ok={"Accepted":0,"Refused":0}
    accp=[]
    reff=[]
    for x in list:
        if(x[1]<=seuil):
            reff.append(x)
        else:
            accp.append(x)
    reff.sort(key=lambda k: k[1])
    accp.sort(key=lambda k: k[1],reverse=True)
    ok["Accepted"]=accp
    ok["Refused"]=reff
    return ok

select_student_ok([["Kermit Wade", 27],["Hattie Schleusner", 67],["Ben Ball", 5],["William Lee", 2]],20)