# To do:
#   1)Make sure start and end matches.
#       -Take last element in list and compare to click destination
#       -if (x orgin - x dest) < 5  and (y orgin - y dest) < 5
#       -if not remove

import pygame
import random
import keyboard
import os
import win32gui
import win32api
import time
import threading

os.environ['SDL_VIDEO_CENTERED'] = "1"

# Global Variables
DISPLAY_WIDTH = 1344
DISPLAY_HEIGHT = 756

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

GREEN = (0, 200, 0)
BRIGHT_GREEN = (0, 255, 0)

RED = (200, 0, 0)
BRIGHT_RED = (255, 0, 0)

RED_BUTTON_SIZE = 20

stop_thread = False
recording_mouse = False
mouse_path_list = []

def text_objects(text, fontsize):
    """Set text's size and color"""
    font = pygame.font.SysFont(None, fontsize)
    textSurface = font.render(text, True, BLACK)
    return textSurface, textSurface.get_rect()

def draw_red_button(screen, x, y):
    """Draw an interactive red button on screen"""
    mouse_position = pygame.mouse.get_pos()
    mouse_pos_x = mouse_position[0]
    mouse_pos_y = mouse_position[1]

    if x + RED_BUTTON_SIZE > mouse_pos_x > x and y + RED_BUTTON_SIZE > mouse_pos_y > y:
        pygame.draw.rect(screen, BRIGHT_RED, (x, y, RED_BUTTON_SIZE, RED_BUTTON_SIZE))
        return mouse_position
    else:
        pygame.draw.rect(screen, RED, (x, y, RED_BUTTON_SIZE, RED_BUTTON_SIZE))
    return False

def draw_green_button(screen, x, y):
    """Draw an interactive green button on screen"""
    mouse_position = pygame.mouse.get_pos()
    mouse_pos_x = mouse_position[0]
    mouse_pos_y = mouse_position[1]

    if x + 150 > mouse_pos_x > x and y + 75 > mouse_pos_y > y:
        pygame.draw.rect(screen, BRIGHT_GREEN, (x, y, 150, 75))
        return True
    else:
        pygame.draw.rect(screen, GREEN, (x, y, 150, 75))
    return False

def timer_text(screen, time_left):
    """Display a countdown Timer in the game"""
    textSurf, textRect = text_objects("Timer: " + str(time_left), 25)

    # Set Timer text at top right corner
    return screen.blit(textSurf, (1257, 10))

def points_text(screen, points):
    """Display the current point in the game"""
    textSurf, textRect = text_objects("Points: " + str(points), 25)

    # Set Points text at top left corner
    return screen.blit(textSurf, (10, 10))

def click(x, y):
    """Aimbot easter egg in the game."""
    fg_win = win32gui.GetForegroundWindow()
    x1, y1 = win32gui.ClientToScreen(fg_win, (x, y))
    x1 += 1
    y1 += 1
    win32api.SetCursorPos((x1, y1))

def record_mouse():
    """Records mouse movement and save it to a list"""
    global mouse_path_list
    while True:
        time.sleep(0.025)

        global recording_mouse
        if recording_mouse:
            x, y = pygame.mouse.get_pos()
            time_of_coords = len(mouse_path_list)
            mouse_path_list.append((x, y, time_of_coords))

        global stop_thread
        if stop_thread:
            break

def main():
    global stop_thread
    global recording_mouse
    global mouse_path_list

    clock = pygame.time.Clock()
    pygame.init()
    screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
    pygame.display.set_caption('Aim map')

    button_location_x = random.randint(86, 1238)
    button_location_y = random.randint(44, 692)

    # Game Information
    points = 0

    timer_secs = 30
    pygame.time.set_timer(pygame.USEREVENT, 1000)

    cursor_coordinates = False

    times_up = True
    reset = True

    t1 = threading.Thread(target=record_mouse)
    t1.start()

    file_counter = 1
    exist = True

    while exist:
        file_path = "./Recordings/recording" + str(file_counter) + ".txt"
        exist = os.path.isfile(file_path)

    with open(file_path, "w") as f:
        done = 0

        while not done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = 1
                    break
                elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
                    if cursor_coordinates:
                        recording_mouse = True
                        if mouse_path_list:
                            f.write(str(mouse_path_list) + "\n")
                        f.write("Mouse: %s\n" % str(pygame.mouse.get_pos()))
                        mouse_path_list = []
                        points += 1
                        button_location_x = random.randint(86, 1238)
                        button_location_y = random.randint(44, 692)
                    elif reset:
                        timer_secs = 30
                        points = 0
                        reset = False
                        times_up = False
                elif event.type == pygame.USEREVENT:
                    if timer_secs > 0:
                        timer_secs -= 1
                    else:
                        times_up = True

                        # Disable red button
                        cursor_coordinates = False

            screen.fill(WHITE)

            if times_up:
                recording_mouse = False

                textSurf, textRect = text_objects("Game Over!", 100)
                textRect.center = (DISPLAY_WIDTH / 2), (DISPLAY_HEIGHT / 2 - 200)
                screen.blit(textSurf, textRect)

                textSurf, textRect = text_objects("Points: " + str(points), 70)
                textRect.center = (DISPLAY_WIDTH / 2), (DISPLAY_HEIGHT / 2 - 100)
                screen.blit(textSurf, textRect)

                reset = draw_green_button(screen, 597, 350)

            else:
                timer_text(screen, timer_secs)
                points_text(screen, points)

                # If cursor coordinates is in the button, store it in cursor_coordinates
                cursor_coordinates = draw_red_button(screen, button_location_x, button_location_y)

                if keyboard.is_pressed('q'):
                    times_up = True

                    # Disable red button
                    cursor_coordinates = False

                if keyboard.is_pressed('w'):
                    # Easter egg
                    click(button_location_x, button_location_y)

            pygame.display.update()
            clock.tick(60)

    stop_thread = True
    t1.join()

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
import os
import sys
import psutil
import subprocess
import time
from pathlib import Path

import typer
from unoserver import converter

# from pdf2pdfa import pdf2pdfa

def start_uno_server():
    if uno_server_running():
        return

    subprocess.Popen(
        Path(Path.home(), ".local/bin/unoserver"),
        start_new_session=True,
        stdout=subprocess.DEVNULL,
        stderr=subprocess.STDOUT,
    )
    time.sleep(3)  # Wait for server to start up

def uno_server_running():
    for process in psutil.process_iter():
        if process.name() == "unoserver":
            return True

    return False

def unoconv2x(source_path: str, target_path: str):
    """
    Convert spreadsheet, MS Word or rtf files to pdf or html.
    Spreadsheet files can be converted to html or pdf specified with the extension of target_path.
    Word and rtf files can only be converted to pdf.

    Example: python3 -m bin.unoconv2x ./example/testfile.xls ./result pdf
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

    Supported mime types:
        - application/vnd.ms-excel
        - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
        - application/msword
        - application/rtf

    Args:
        source_path: path for the file to be converted
        target_path: path for the converted file
    Returns:
        Nothing if successful otherwise exits with exit code 1
    """

    start_uno_server()
    target_ext = Path(target_path).suffix
    _converter = converter.UnoConverter()
    result = _converter.convert(source_path, None, target_path, target_ext)

    if not Path(target_path).is_file() or result is not None:
        sys.exit(1)

if __name__ == "__main__":
    typer.run(unoconv2x)


# -*- encoding: utf-8 -*-
"""
-------------------------------------------------
   File Name：    vocabulary.py
   Description :
   Author :       Wings DH
   Time：         2020/1/4 2:47 下午
-------------------------------------------------
   Change Activity:
                   2020/1/3: Create
-------------------------------------------------
"""
from abc import ABC, abstractmethod
from collections import OrderedDict
import os

from tqdm import tqdm

from common.log import logger
from data_processing.text_processor import ITextProcessor
from data_processing.tokenizer import SpaceTokenizer, ITextTokenizer
import sentencepiece as spm

NO_LIMIT_MAX_N_WORD = -1
NO_LIMIT_MIN_FREQ = 0

class IVocabulary(ABC):

    @property
    @abstractmethod
    def n_tokens(self):
        return

    @abstractmethod
    def __len__(self):
        pass

    @abstractmethod
    def text_to_sequence(self, text, *args, **kwargs):
        pass

def _save_vocab_to_file(vocab, save_file_path):
    """ 保存词库到txt文件
    :param save_file_path:
    :param vocab:
    Returns:
    """
    if save_file_path is not None:
        with open(save_file_path, 'w', encoding='utf-8') as f:
            for index in vocab.index_token:
                _token = vocab.index_token[index]
                f.write('{token}:{index}\n'.format(token=_token, index=index))
            logger.info('Save vocab into {vocab_file}'.format(vocab_file=save_file_path))

def _arrange_vocab_from_corpus(corpus_iter,
                               tokenizer=None,
                               text_processor=None,
                               max_n_tokens=NO_LIMIT_MAX_N_WORD,
                               min_freq=NO_LIMIT_MIN_FREQ):
    """
    从语料中获取词库
    :param corpus_iter: 关于corpus的迭代器, 每次迭代输出一个token列表
    :return vocab: 构建好的Vocabulary
    """

    if not corpus_iter:
        raise ValueError('file_path and dir_path cant be None in the same time!')

    if not tokenizer:
        tokenizer = SpaceTokenizer.new_instance()
    elif not isinstance(tokenizer, ITextTokenizer):
        raise ValueError('tokenizer:<{}> must instance of ITextTokenizer'.format(type(tokenizer)))

    vocab = Vocabulary(tokenizer=tokenizer, text_processor=text_processor)
    for line in tqdm(corpus_iter):
        # 如果需要经过文本预处理，那统计词频应当同样经过预处理
        if text_processor is not None:
            line = text_processor.process(line)

        if len(line) <= 0:
            continue

        tokens = tokenizer.tokenize(line)

        for token in tokens:
            vocab.count_token(token)

    vocab.build_vocab_from_word_count(max_n_tokens, min_freq)

    return vocab

def _load_from_token_count_txt(path, tokenizer=None, text_processor=None):
    vocab = Vocabulary(tokenizer=tokenizer, text_processor=text_processor)
    try:
        logger.info('Start load token_count txt')
        with open(path, 'r', encoding='utf-8') as f:
            for line in f:
                try:
                    coef = line.strip().split(':')

                    # 防止token=:的情况
                    _token = coef[0]
                    _index = coef[-1]

                    vocab.token_index[_token] = int(_index)
                    vocab.index_token[int(_index)] = _token
                except ValueError:
                    logger.warning('Invalid line:{}'.format(line))

        logger.info('End   load token_count txt, read {nb_tokens} tokens'.format(nb_tokens=len(vocab)))
    except FileNotFoundError:
        logger.warning(
            'File not exit, please check {error_file}'.format(error_file=path))
    return vocab

class Vocabulary(IVocabulary):
    """
    词汇库类
    """

    @property
    def n_tokens(self):
        return len(self.index_token)

    TOKEN_PAD = '<pad>'
    TOKEN_UNKNOWN = '<unk>'
    TOKEN_START = '<s>'
    TOKEN_END = '</s>'

    INDEX_PAD = 0
    INDEX_UNKNOWN = 1
    INDEX_START = 2
    INDEX_END = 3
    INDEX_ORIGIN_CUSTOM = 4

    @property
    def index_token(self):
        return self._index_token

    @property
    def token_index(self):
        return self._token_index

    @property
    def token_count(self):
        return self._token_count

    @classmethod
    def new_from_corpus(cls, corpus_iter,
                        tokenizer=None,
                        text_processor=None,
                        max_n_tokens=NO_LIMIT_MAX_N_WORD,
                        min_freq=NO_LIMIT_MIN_FREQ):
        return _arrange_vocab_from_corpus(corpus_iter, tokenizer, text_processor, max_n_tokens, min_freq)

    @classmethod
    def new_from_save_file(cls, f_path, tokenizer=None, text_processor=None):
        return _load_from_token_count_txt(f_path, tokenizer, text_processor=text_processor)

    @staticmethod
    def save_vocab(vocab, f_path):
        """
        保存vocab到文件
        :param vocab: 需要保存的Vocabulary实例
        :param f_path: 保存路径
        :return:
        """
        _save_vocab_to_file(vocab, f_path)

    def __init__(self, tokenizer=None, text_processor=None):

        # 初始化词库
        self._token_index = {}
        self._index_token = {}
        self._token_count = OrderedDict()

        # 初始化分词器
        if tokenizer is None or not isinstance(tokenizer, ITextTokenizer):
            # 默认使用空格来做分词
            self.tokenizer = SpaceTokenizer.new_instance()
        else:
            self.tokenizer = tokenizer

        # 初始化文本处理器
        if text_processor is not None and not isinstance(text_processor, ITextProcessor):
            raise TypeError("text_processor isn't instance of ITextProcessor but {}".format(type(text_processor)))
        self.text_processor = text_processor

    def count_token(self, token):
        if token in self.token_count:
            self.token_count[token] += 1
        else:
            self.token_count[token] = 1

    def _add_token_index(self, token, index):
        self._token_index[token] = index
        self._index_token[index] = token

    def build_vocab_from_word_count(self, max_n_tokens=NO_LIMIT_MAX_N_WORD, min_freq=NO_LIMIT_MIN_FREQ):
        self._token_index = {}
        self._index_token = {}

        # 默认占前4位索引
        self._add_token_index(Vocabulary.TOKEN_PAD, Vocabulary.INDEX_PAD)
        self._add_token_index(Vocabulary.TOKEN_UNKNOWN, Vocabulary.INDEX_UNKNOWN)
        self._add_token_index(Vocabulary.TOKEN_START, Vocabulary.INDEX_START)
        self._add_token_index(Vocabulary.TOKEN_END, Vocabulary.INDEX_END)

        index = Vocabulary.INDEX_ORIGIN_CUSTOM

        # 根据词频排序，只保留高频词
        wcounts = list(self.token_count.items())
        wcounts.sort(key=lambda x: x[1], reverse=True)

        for cnt, (w, c) in enumerate(wcounts):
            if max_n_tokens != NO_LIMIT_MAX_N_WORD and cnt > max_n_tokens - 1:
                break

            if c < min_freq:
                break

            self._add_token_index(w, index)
            index += 1
        logger.info('Build vocab, volume:{size_word_index}'.format(size_word_index=len(self.token_index)))

    def tokens_to_sequence(self, tokens, add_start=False, add_end=False):
        """
        将一句话的token转成序列
        :param tokens:
        :param add_start:
        :param add_end:
        :return:
        """
        sequence = []
        if add_start:
            sequence.append(self.token_index[Vocabulary.TOKEN_START])

        sequence.extend(
            [self.token_index.get(c, self.token_index[Vocabulary.TOKEN_UNKNOWN]) for c in tokens if c != ' '])

        if add_end:
            sequence.append(self.token_index[Vocabulary.TOKEN_END])

        return sequence

    def text_to_sequence(self, text, add_start=False, add_end=False, *args, **kwargs):
        """
        文本转成序列
        :param text:
        :param add_start:
        :param add_end:
        :return:
        """

        # 文本预处理
        if self.text_processor is not None:
            text = self.text_processor.process(text)

        # 用分词器将一段文本转成tokens
        tokens = self.tokenizer.tokenize(text)

        # token转为索引
        return self.tokens_to_sequence(tokens, add_start=add_start, add_end=add_end)

    def texts_to_sequences(self, texts, add_start=False, add_end=False):
        """
        多个文本转成序列
        :param texts:
        :param add_start:
        :param add_end:
        :return:
        """
        sequences = []
        for text in texts:
            seq = self.text_to_sequence(text, add_start=add_start, add_end=add_end)
            sequences.append(seq)
        return sequences

    def sequences_to_texts(self, sequences):
        texts = []
        for seq in sequences:
            texts.append([self.index_token.get(token, Vocabulary.TOKEN_UNKNOWN) for token in seq])
        return texts

    def __len__(self):
        return len(self._token_index)

    def __contains__(self, item):
        return item in self._token_index

    def __getitem__(self, item):
        return self.token_index.get(item, self.token_index[Vocabulary.TOKEN_UNKNOWN])

    def __str__(self):
        """
        Returns: summary str
        """
        return 'Word Total: {total}'.format(total=len(self))

class SentencePieceVocabulary(IVocabulary):
    """
    应用Sentence piece工具分词
    https://github.com/google/sentencepiece/blob/master/python/
    """
    ARG_KEY_SPM_F_PATH = 'path'

    @property
    def n_tokens(self):
        return self.spm_processor.get_piece_size()

    @classmethod
    def new_instance(cls, *args, **kwargs):
        f_path = kwargs.get(cls.ARG_KEY_SPM_F_PATH, None)
        if f_path is None:
            raise ValueError('param: path can not be None!')
        elif not os.path.exists(f_path):
            raise FileExistsError('param: path {} must exist'.format(f_path))

        return cls(f_path)

    def tokenize_as_pieces(self, text):
        return self.spm_processor.EncodeAsPieces(text)

    def __len__(self):
        return self.spm_processor.get_piece_size()

    def text_to_sequence(self, text, *args, **kwargs):
        return self.spm_processor.EncodeAsIds(text)

    def sequences_to_texts(self, sequences):
        texts = []
        for seq in sequences:
            text = self.spm_processor.DecodeIds(seq.tolist())
            texts.append(text)
        return texts

    def __init__(self, spm_model_f_path):
        """
        :param spm_model_f_path: str, sentence piece 模型文件
        """
        self.spm_model_f_path = spm_model_f_path
        self.spm_processor = spm.SentencePieceProcessor()
        self.spm_processor.load(spm_model_f_path)


import unittest
from unittest.mock import patch

import cli
import click
from click.testing import CliRunner

order_data = {
    "data_format": "csv",
    "products": [
        {
            "product_category": "drink",
            "type": "coke_zero"
        },
        {
            "product_category": "pizza",
            "size": "small",
            "type": "custom",
            "toppings": []
        },
        {
            "product_category": "pizza",
            "size": "extra_large",
            "type": "pepperoni",
            "toppings": ["olive", "beef"]
        },

    ],
    "delivery_method": {
        "type": "foodora",
        "details": {
            "address": "27 random street",
            "order_no": 2
        }
    }
}

class TestCliUtilityFunctions(unittest.TestCase):
    def test_convert_to_csv(self):
        expected = "drink,coke_zero,\npizza,custom,small,\npizza,pepperoni,extra_large,olive|beef\nfoodora,27 random street,2"
        actual = cli.convert_to_csv(order_data)
        self.assertEqual(actual, expected)

class TestCli(unittest.TestCase):
    def setUp(self):
        pass

    @patch("cli.requests.get")
    def test_valid_full_menu(self, mock_get):
        mock_get.return_value.status_code = 200
        mock_get.return_value.json = lambda: {
            "Part A": {"Item 1": 1.00, "Item 2": 1.99}}
        runner = CliRunner()
        result = runner.invoke(cli.menu)
        self.assertTrue(result.exit_code == 0 and not result.exception)
        self.assertEqual(
            result.output,
            "Part A:\nItem 1       :  $1.0\nItem 2       :  $1.99\n\n")

    @patch("cli.requests.get")
    def test_valid_menu_item(self, mock_get):
        mock_get.return_value.status_code = 200
        mock_get.return_value.text = "1.00"
        result = CliRunner().invoke(cli.menu, args=["random_item"])
        self.assertTrue(result.exit_code == 0 and not result.exception)
        self.assertEqual(result.output, "random_item: $1.00\n")

    @patch("cli.requests.get")
    def test_invalid_menu_item(self, mock_get):
        mock_get.return_value.status_code = 400
        mock_get.return_value.text = "Error text"
        result = CliRunner().invoke(cli.menu, args=["random_item"])
        self.assertTrue(result.exit_code == 0 and not result.exception)
        self.assertEqual(
            result.output,
            "Error: random_item is not a valid menu item\n")

    def test_add_pizza_and_drink(self):
        expected = {"products": [{
                "product_category": "pizza",
                "size": "small",
                "type": "custom",
                "toppings": [
                    "olive",
                ]
            },
            {
                "product_category": "drink",
                "type": "coke",
            },
        ],
            "delivery_method": {
                "type": "pickup",
                "details": {}
            }
        }

        base_order = {
            "products": [],
            "delivery_method": {
                "type": "pickup",
                "details": {}
            }
        }
        context = {"current_order": base_order}
        result = CliRunner().invoke(
            cli.order,
            args=["pizza", "small", "custom", "-t", "olive"],
            obj=context)
        self.assertTrue(result.exit_code == 0 and not result.exception)
        result = CliRunner().invoke(
            cli.order,
            args=["drink", "coke"],
            obj=context)
        self.assertTrue(result.exit_code == 0 and not result.exception)
        self.assertEqual(context["current_order"], expected)


import cv2
import matplotlib.pyplot as plt

img = cv2.imread('A.jpeg')
cv2.imshow('Original',img)
cv2.waitKey()

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('GrayScale',gray)
cv2.imwrite('gray.jpeg',gray)
cv2.waitKey()

hist = cv2.calcHist([gray],[0],None,[256],[0,256])

plt.xlabel('Pixel Value')
plt.ylabel('Number of Pixels')
plt.plot(hist)
plt.savefig('HistogramBefore')
plt.show()

equ = cv2.equalizeHist(gray)

hist = cv2.calcHist([equ],[0],None,[256],[0,256])

plt.xlabel('Pixel Value')
plt.ylabel('Number of Pixels')
plt.plot(hist)
plt.savefig('HistogramAfter')
plt.show()

cv2.imshow('Equalised Histogram',equ)
cv2.imwrite('HistEqu.jpeg',equ)
cv2.waitKey()


### Parameters
RUN_ONCE = False
SECONDS_TO_SLEEP_FOR_NEXT_REQUEST = 30 # in seconds
BACKUP_TIME = 30 # INT: Every X minutes a Backup of the data is done.
BACKUP_FILE_PATH_NAME = "Backup_RSS_Feed-scrap.csv" #Backup file name with .csv
# URLSs to RSS-Feeds
SRF_FEED = "https://www.srf.ch/news/bnf/rss/1646"
NZZ_FEED = "https://www.nzz.ch/recent.rss"
TA_FEED = "https://partner-feeds.publishing.tamedia.ch/rss/tagesanzeiger/ticker"
BLICK_FEED = "https://www.blick.ch/news/rss.xml"
AGZ_FEED = "https://www.aargauerzeitung.ch/rss2.xml"

# Array of urls to News RSS-Feeds
RSS_FEEDS = [SRF_FEED, TA_FEED, BLICK_FEED, NZZ_FEED, AGZ_FEED]
NAME_OF_FEEDS = ['SRF',  'TA', 'Blick', 'NZZ', 'AGZ']


#Program wykorzystuje model perceptronu do klasyfikacji danych ze zbioru iris z rozroznieniem na poszczegolne klasy
#Wynikiem działania jest wypisanie ilości niepoprawnych klasyfikacji danych ze zbioru testujacego

import numpy as np
import pandas as pd

#Klasa modelujaca perceptron - wykorzystano propozycje klasy z repozytorium wskazanego przez prowadzącego zajęcia
class Perceptron(object):

    def __init__(self, eta=0.01, epochs=50):
        self.eta = eta
        self.epochs = epochs

    def train(self, X, y):

        self.w_ = np.zeros(1 + X.shape[1])
        self.errors_ = []

        for _ in range(self.epochs):
            errors = 0
            for xi, target in zip(X, y):
                update = self.eta * (target - self.predict(xi))
                self.w_[1:] +=  update * xi
                self.w_[0] +=  update
                errors += int(update != 0.0)
            self.errors_.append(errors)
        return self

    def net_input(self, X):
        return np.dot(X, self.w_[1:]) + self.w_[0]

    def predict(self, X):
        return np.where(self.net_input(X) >= 0.0, 1, -1)

#Wczytanie danych
df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None)
#print(df.columns)

#Zapisanie wyjsc odpowiadajacych wykorzystywanym klasom
#Kazda tablica 150 elementow zawiera 1 w polach odpowiadajacych podanej nazwie i -1 w pozostalych
y2 = np.where(df[4] == 'Iris-setosa', 1, -1)
y3 = np.where(df[4] == 'Iris-versicolor', 1, -1)
y4 = np.where(df[4] == 'Iris-virginica', 1, -1)

#Losowy wybor zestawow danych trenujacych stanowiacych okolo 80% calego zbioru danych
x_train_id = np.random.rand(len(df)) < 0.8
#print(x_train_id)
#print(len(x_train_id))

#Wyselekcjonowanie pozadanych wartosci dla wybranych danych trenujacych
#Oczekiwana liczebnosc kazdego z ponizszych to 120
y2_train = y2[x_train_id]
y3_train = y3[x_train_id]
y4_train = y4[x_train_id]

# print(y2_train)
# print(len(y2_train))

#Wyselekcjonowanie pozadanych wartosci dla wybranych danych testujacych
#Negacje zastosowano w celu dopelnienia zbioru danych trenujacych
#Oczekiwana liczebnosc kazdego z ponizszych to 30
y2_test = y2[~x_train_id]
y3_test = y3[~x_train_id]
y4_test = y4[~x_train_id]

# print(y2_test)
# print(len(y2_test))

#Wybor zestawu parametrow decydujacych o klasyfikacji obiektu do jednej z klas
X2_train = df.iloc[x_train_id, [0,1,2,3]].values #Dla danych trenujacych
X2_test = df.iloc[~x_train_id, [0,1,2,3]].values #Dla danych testujacych

#Zdefiniowanie perceptronow - edycja wartosci epochs i eta bezsposrednio przeklada sie na jakosc nauki
ppn2 = Perceptron(epochs=50, eta=0.001)
ppn3 = Perceptron(epochs=50, eta=0.001)
ppn4 = Perceptron(epochs=50, eta=0.001)

#Trenowanie perceptronow
ppn2.train(X2_train, y2_train)
ppn3.train(X2_train, y3_train)
ppn4.train(X2_train, y4_train)

#Wypisanie wynikow dla kazdego z perceptronow

# print('Weights: %s' % ppn2.w_)
# plot_decision_regions(X2_test, y2_test, clf=ppn2)
# plt.title('Perceptron 1 - Klasyfikacja do Iris-setosa')
# plt.xlabel('sepal length [cm]')
# plt.ylabel('petal length [cm]')
# plt.show()

print('Incorrect test data classifications for perceptron 1: %d' % ((y2_test != ppn2.predict(X2_test)).sum()))

#Zapisanie tabeli okresjalacej prawdopodobienstwo nalezenia obiektu dla danej klasy
propability = ppn2.net_input(X2_test)

#print(propability)

print('Incorrect test data classifications for perceptron 2: %d' % ((y3_test != ppn3.predict(X2_test)).sum()))

propability2 = ppn3.net_input(X2_test)
#print(propability2)

print('Incorrect test data classifications for perceptron 3: %d' % ((y4_test != ppn4.predict(X2_test)).sum()))

propability3 = ppn4.net_input(X2_test)
#print(propability3)

#Czesc programu odpowiadajaca za zestawienie wynikow
results = []

#Petla dodaje do tablicy results najbardziej prawdopodobne wyniki
for prob1, prob2, prob3 in zip(propability.tolist(), propability2.tolist(), propability3.tolist()):
  if max(prob1, prob2, prob3) == prob1:
    results.append('Iris-setosa')
  elif max(prob1, prob2, prob3) == prob2:
    results.append('Iris-versicolor')
  elif max(prob1, prob2, prob3) == prob3:
    results.append('Iris-virginica')

#Wizualizacja wynikow - porownanie wartosci uzyskanych z rzeczywistymi
#print(results)

#"Wyciagniecie" z pliku prawdziwych nazw gatunkow
y_real_value = df.iloc[~x_train_id, 4]

#print(y_real_value)

#Zsumowanie ilosci blednych klasyfikacji
print('Total number of misclassifications (by probability) : %d na %d' % ((y_real_value != results).sum(), len(results)))


# This Python 3 environment comes with many helpful analytics libraries installed

# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python

# For example, here's several helpful packages to load

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory

# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os

for dirname, _, filenames in os.walk('/kaggle/input'):

    for filename in filenames:

        print(os.path.join(dirname, filename))

# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All"

# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
train_csv = pd.read_csv('/kaggle/input/santander-value-prediction-challenge/train.csv')

test_csv = pd.read_csv('/kaggle/input/santander-value-prediction-challenge/test.csv')
print(train_csv.shape, test_csv.shape)
train_csv.head()
import matplotlib.pyplot as plt

import seaborn as sns
plt.figure(figsize=(12, 8))

plt.scatter(range(train_csv.shape[0]), np.sort(train_csv.target.values))

plt.grid()

plt.xlabel('index', fontsize=12)

plt.ylabel('Target', fontsize=12)

plt.title("Target Distribution", fontsize=14)
plt.figure(figsize=(12,8))

plt.hist(train_csv.target.values, bins=50)

plt.xlabel('Target', fontsize=12)

plt.title("Target Histogram", fontsize=14)
plt.figure(figsize=(12,8))

plt.hist(np.log1p(train_csv.target.values), bins=50)

plt.xlabel('Target', fontsize=12)

plt.title("Target Histogram", fontsize=14)
num_nunique = train_csv.nunique().reset_index()

num_nunique.columns = ['Col_name', 'Value_columns']

num_nunique
only1_nuique = num_nunique[num_nunique.Value_columns == 1]

only1_nuique.shape
from scipy.stats import spearmanr
from tqdm import tqdm, tqdm_notebook

from scipy.stats import spearmanr, pearsonr

import warnings

warnings.filterwarnings('ignore')

col_names = []

cor_value = []

for col in tqdm(train_csv.columns, ncols=100 , leave= True, desc="Spearman r : "):

    if col not in ['ID','target']:

        col_names.append(col)

        cor_value.append(spearmanr(train_csv[col].values, train_csv.target.values)[0])

corrs = pd.DataFrame({'Feature_Name':col_names,'Corr_value':cor_value})

corrs = corrs.sort_values(by = 'Corr_value')
corr_df = corrs[(corrs.Corr_value > 0.1) | (corrs.Corr_value < -0.1)].reset_index()

corr_df.drop('index', axis=1, inplace=True)
corr_df = corr_df.set_index('Feature_Name')

corr_df.plot(kind='barh', figsize = (12,15), title='Correlation of variables')
corr_df = corrs[(corrs.Corr_value > 0.11) | (corrs.Corr_value < -0.11)].reset_index()

corr_df.drop('index', axis = 1, inplace = True)

corr_df = corr_df.set_index('Feature_Name')

corr_df.plot(kind='barh', figsize = (12,15), title='Correlation of variables')
labels = []

values = []

for col in tqdm(train_csv.columns, ncols = 100, desc = 'Pearson r :'):

    if col not in ['ID', 'target']:

        corr_value = pearsonr(train_csv[col].values, train_csv['target'].values)

        values.append(corr_value)

        labels.append(col)

corr_df = pd.DataFrame({'Feature_Name' : labels, 'Feature_Value' : values})

corr_df = corr_df.sort_values(by = 'Feature_Value')
target = train_csv.target

train_csv.drop(list(only1_nuique.Col_name) + ['ID', 'target'], axis = 1, inplace=True)

test_csv.drop(list(only1_nuique.Col_name), axis = 1, inplace=True)
test_id = test_csv.ID

test_csv.drop('ID', axis = 1, inplace=True)
target = np.log1p(target)
from sklearn import ensemble

model = ensemble.ExtraTreesRegressor(n_estimators=200, max_depth=20, max_features=0.5,

                                     n_jobs=-1, random_state=50)

model.fit(train_csv, target)
feature_importances = pd.DataFrame({'Feature_Name' : train_csv.columns, 'feature_importance' : model.feature_importances_})

feature_importances = feature_importances.set_index('Feature_Name')

# sort by importances

feature_importances = feature_importances.sort_values(by = 'feature_importance')

std_feat_importances = np.std([tree.feature_importances_ for tree in model.estimators_],axis=0)

feature_importances.iloc[-50:].plot(kind='barh',figsize = (10,20),color = 'g',

                         xerr=std_feat_importances[-50:], align='center',

                                   title='top 50 Feature Importances')

import lightgbm as lgb

from sklearn import  model_selection, metrics

params = {

    "objective" : "regression",

    "metric" : "rmse",

    "num_leaves" : 30,

    "learning_rate" : 0.01,

    "bagging_seed" : 1884,

    "device" : "gpu",

    "gpu_platform_id" : 0,

    "gpu_device_id" : 0,

    "num_thread" : 8

}

val_data = train_csv[0:500]

val_label = target[0:500]

val_set = lgb.Dataset(val_data, val_label)

train_set = lgb.Dataset(train_csv, label=target)

model = lgb.train(params, train_set ,5000, valid_sets = [val_set],  early_stopping_rounds = 100, verbose_eval = 200)
lgb.plot_importance(model, max_num_features=50, height=0.8, figsize=(12, 20))

plt.title("LightGBM - Feature Importance top 50", fontsize=15)
result = model.predict(test_csv, num_iteration=model.best_iteration)

result = np.expm1(result)
submit = pd.DataFrame({'ID':test_id, 'target':result})

submit.to_csv('submit3_3.csv',index = False)
importance = model.feature_importance(importance_type='split')

feature_name = model.feature_name()
features = pd.DataFrame({'Feature_Name' : feature_name, 'Feature_Important' : importance}).sort_values(by=['Feature_Important'], ascending=[False])[:1000]

train = train_csv[list(features.Feature_Name)]

test = test_csv[list(features.Feature_Name)]
ntrain = train.shape[0]

ntest = test.shape[0]
weight = ((train != 0).sum()/len(train)).values

tmp_train = train[train!=0]

tmp_test = test[test!=0]

train["weight_count"] = (tmp_train*weight).sum(axis=1)

test["weight_count"] = (tmp_test*weight).sum(axis=1)

train["count_not0"] = (train != 0).sum(axis=1)

test["count_not0"] = (test != 0).sum(axis=1)

train["sum"] = train.sum(axis=1)

test["sum"] = test.sum(axis=1)

train["var"] = tmp_train.var(axis=1)

test["var"] = tmp_test.var(axis=1)

train["median"] = tmp_train.median(axis=1)

test["median"] = tmp_test.median(axis=1)

train["mean"] = tmp_train.mean(axis=1)

test["mean"] = tmp_test.mean(axis=1)

train["std"] = tmp_train.std(axis=1)

test["std"] = tmp_test.std(axis=1)

train["max"] = tmp_train.max(axis=1)

test["max"] = tmp_test.max(axis=1)

train["min"] = tmp_train.min(axis=1)

test["min"] = tmp_test.min(axis=1)

train["skew"] = tmp_train.skew(axis=1)

test["skew"] = tmp_test.skew(axis=1)

train["kurtosis"] = tmp_train.kurtosis(axis=1)

test["kurtosis"] = tmp_test.kurtosis(axis=1)
train["weight_count"] = train["weight_count"].fillna(0)

train["count_not0"] = train["count_not0"].fillna(0)

train["sum"] = train["sum"].fillna(0)

train["var"] = train["var"].fillna(0)

train["median"] = train["median"].fillna(0)

train["mean"] = train["mean"].fillna(0)

train["std"] = train["std"].fillna(0)

train["max"] = train["max"].fillna(0)

train["min"] = train["min"].fillna(0)

train["skew"] = train["skew"].fillna(0)

train["kurtosis"] = train["kurtosis"].fillna(0)

test["weight_count"] = test["weight_count"].fillna(0)

test["count_not0"] = test["count_not0"].fillna(0)

test["sum"] = test["sum"].fillna(0)

test["var"] = test["var"].fillna(0)

test["median"] = test["median"].fillna(0)

test["mean"] = test["mean"].fillna(0)

test["std"] = test["std"].fillna(0)

test["max"] = test["max"].fillna(0)

test["min"] = test["min"].fillna(0)

test["skew"] = test["skew"].fillna(0)

test["kurtosis"] = test["kurtosis"].fillna(0)
train.shape
from sklearn import random_projection

NUM_OF_COM = 100

tmp = pd.concat([train,test])

transformer = random_projection.SparseRandomProjection(n_components = NUM_OF_COM)

RP = transformer.fit_transform(tmp)

rp = pd.DataFrame(RP)

columns = ["RandomProjection{}".format(i) for i in range(NUM_OF_COM)]

rp.columns = columns

rp_train = rp[:ntrain]

rp_test = rp[ntrain:]

rp_test.index = test.index

#concat RandomProjection and raw data

train = pd.concat([train,rp_train],axis=1)

test = pd.concat([test,rp_test],axis=1)

train.shape
from sklearn import ensemble

model = ensemble.ExtraTreesRegressor(n_estimators=200, max_depth=20, max_features=0.5,

                                     n_jobs=-1, random_state=50)

model.fit(train, target)
result = model.predict(test)

result = np.expm1(result)

submit = pd.DataFrame({'ID':test_id, 'target':result})

submit.to_csv('submit3_4_extra.csv',index = False)
import xgboost as xgb

import lightgbm as lgb

model_xgb = xgb.XGBRegressor(colsample_bytree=0.055, colsample_bylevel =0.5,

                             gamma=1.5, learning_rate=0.02, max_depth=32,

                             objective='reg:linear',booster='gbtree',

                             min_child_weight=57, n_estimators=1000, reg_alpha=0,

                             reg_lambda = 0,eval_metric = 'rmse', subsample=0.7,

                             silent=1, n_jobs = -1, early_stopping_rounds = 14,

                             random_state =7, nthread = -1)

model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=144,

                              learning_rate=0.005, n_estimators=720, max_depth=13,

                              metric='rmse',is_training_metric=True,

                              max_bin = 55, bagging_fraction = 0.8,verbose=-1,

                              bagging_freq = 5, feature_fraction = 0.9)

model_xgb.fit(train, target)

model_lgb.fit(train, target)
result = model_xgb.predict(test)

result = np.expm1(result)

submit = pd.DataFrame({'ID':test_id, 'target':result})

submit.to_csv('submit3_4_xgb.csv',index = False)
result = model_lgb.predict(test)

result = np.expm1(result)

submit = pd.DataFrame({'ID':test_id, 'target':result})

submit.to_csv('submit3_4_lgb.csv',index = False)


# A subscribers example

import rx
from rx.subjects import Subject
from datetime import datetime

# You can choose which of these to use
# source = rx.from_([2, 3, 5, 7])
# source = rx.from_iterable([2, 3, 5, 7])
source = rx.from_list([2, 3, 5, 7])

class TimeStampSubject(Subject):
    """ Class implementing a Subject """

    def on_next(self, value):
        print('Subject Received', value)
        super().on_next((value, datetime.now()))

    def on_completed(self):
        print('Data Stream Completed')
        super().on_completed()

    def on_error(self, error):
        print('In Subject - Error Occurred', error)
        super().on_error(error)

def prime_number_reporter(value):
    print('Function Received', value)

print('Set up')

# Create the Subject
subject = TimeStampSubject()

# Set up subscribers for the subject
subject.subscribe(prime_number_reporter)
subject.subscribe(lambda value: print('Lambda Received', value))
subject.subscribe(
    on_next = lambda value: print('Received on_next', value),
    on_error = lambda exp: print('Error Occurred', exp),
    on_completed = lambda: print('Received completed notification')
)

# Subscribe the Subject to the observable source
source.subscribe(subject)

print('Done')


try:
    import pynitf
except ImportError:
    pass
from test_support import *
from .mars_igc import *

@require_spice
@require_mars_spice
def test_igc_mro_context(mars_test_data):
    igc = igc_mro_context(mars_test_data + "P16_007388_2049_XI_24N020W.IMG")
    print(igc)

@require_spice
@require_mars_spice
@require_pynitf
def test_glas_mro_context(isolated_dir, mars_test_data):
    igc_r = igc_mro_context(mars_test_data + "P16_007388_2049_XI_24N020W.IMG")
    igc_g = igc_mro_context_to_glas(igc_r)
    f = pynitf.NitfFile()
    img = pynitf.NitfImageWriteNumpy(1,1,np.uint8)
    img[0,0] = 0
    f.image_segment.append(pynitf.NitfImageSegment(img))
    f.image_segment[0].create_glas_gfm(igc_g)
    f.write("glas_test.ntf")
    f2 = NitfFile("glas_test.ntf")
    print(f2)
    with open("glas_test.bin", "wb") as fh:
        fh.write(serialize_write_binary(igc_g))
    with open("glas_test.bin", "rb") as fh:
        igc_gr = serialize_read_binary(fh.read())
    print(igc_gr)

@require_spice
@require_mars_spice
def test_igc_mex_hrsc(mars_test_data):
    igc = igc_mex_hrsc(mars_test_data + "h1326_0000_nd2.img")
    print(igc)

@require_spice
@require_mars_spice
def test_igc_mro_hirise(mars_test_data):
    igc = igc_mro_hirise(mars_test_data + "esp_025012_1745_red2.norm.cub")
    print(igc)
    d = igc.ipi.camera.sc_look_vector(FrameCoordinate(0,0),0).direction
    print([d[0], d[1], d[2]])
    d = igc.cf_look_vector_lv(ImageCoordinate(100,0)).direction
    print([d[0], d[1], d[2]])
    print(igc.ground_coordinate(ImageCoordinate(100,0)).convert_to_cf())
    print(igc.dem)
    print(Planetocentric(igc.ground_coordinate(ImageCoordinate(100,0))))

@require_spice
@require_mars_spice
def test_hirise_camera(mars_test_data):
    '''This looks specifically at the camera, comparing it to results that
    we have from the ISIS program campt to verify out camera model.'''
    igc = igc_mro_hirise(mars_test_data + "esp_025012_1745_red2.norm.cub")
    cam = igc.ipi.camera
    # This is the pixel 0,0 from campt (0.5,0.5 is ISIS coordinates).
    fc = cam.frame_coordinate(ScLookVector(-0.0080898394215051,
                                            0.0060382951197886,
                                            0.99994904544691),0)
    assert(abs(fc.line) < 1e-3)
    assert(abs(fc.sample) < 1e-3)
    d = cam.sc_look_vector(fc,0).direction
    assert_almost_equal(d[0], -0.0080898394215051)
    assert_almost_equal(d[1], 0.0060382951197886)
    assert_almost_equal(d[2], 0.99994904544691)
    fc2 = cam.frame_coordinate(cam.sc_look_vector(fc,0),0)
    assert(abs(fc2.line) < 1e-3)
    assert(abs(fc2.sample) < 1e-3)

    # This is the pixel 0,2048 in campt (0.5,248.5 is ISIS coordinates
    fc = cam.frame_coordinate(ScLookVector(-0.0081013647757587,
                                           0.0039895794354517,
                                           0.99995922474104),0)
    assert(abs(fc.line) < 1e-3)
    assert(abs(fc.sample - 2048) < 1e-3)
    fc2 = cam.frame_coordinate(cam.sc_look_vector(fc,0),0)
    assert(abs(fc2.line) < 1e-3)
    assert(abs(fc2.sample - 2048) < 1e-3)


'''
expEYES Junior calibration program
Author  : Ajith Kumar B.P, bpajith@gmail.com
License : GNU GPL version 3
'''
from __future__ import print_function

import gettext, sys, os, os.path, time
if sys.version_info.major==3:
        from tkinter import *
else:
        from Tkinter import *

sys.path=[".."] + sys.path

import expeyes.eyesj

import expeyes.eyesj as eyes
import expeyes.eyeplot as eyeplot
import expeyes.eyemath as eyemath

gettext.bindtextdomain("expeyes")
gettext.textdomain('expeyes')
_ = gettext.gettext

from numpy import mean, zeros

def msg(s, col='blue'):
	msgwin.config(text=s, fg=col)

def save_calib():	# Saves scale factors of A1 & A2 to calibrationFile
	v = p.set_voltage(2.0)
	v1 = p.get_voltage(1)
	if abs(v1-v) > 0.1:
		msg(_('PVS is NOT connected to A1'),'red')
		return
	v2 = p.get_voltage(2)
	if(abs(v2-v) > 0.1):
		msg(_('PVS is NOT connected to A2'), 'red')
		return

	np = 10
	x = zeros(np)				# Calibrate A1
	y  = zeros(np)
	iv = 50						# DAC binary value
	for k in range(np):
		p.write_dac(iv)
		time.sleep(0.01)
		x[k] = p.read_adc(1)				# binary from A1, after level shifting
		y[k] = p.read_adc(12)*p.refval/4095		# voltage at PVS (connected to A1)
		iv += 350
	xbar = mean(x)							# Calculate m & c for A1  , 12 bit
	ybar = mean(y)
	m1 = sum(y*(x-xbar)) / sum(x*(x-xbar))
	m1 = float(m1)
	c1 = ybar - xbar * m1
	c1 = float(c1)
	# Do some sanity check here
	ucm = 10.0/4095		# Uncalibrated values of m and c
	ucc = -5.0
	dm = ucm * 0.03			# maximum 3% deviation
	dc = 5 * 0.03
	if abs(m1 - ucm) > dm or abs(c1 - ucc) > dc:
		msg(_('Too much error in A1: m = %f  c=%f')%(m1,c1),'red')
		return

	iv = 10						# Calibrate A2
	for k in range(np):
		p.write_dac(iv)
		time.sleep(0.01)
		x[k] = p.read_adc(2)				# binary from A2, after level shifting
		y[k] = p.read_adc(12)*p.refval/4095	# voltage at PVS (connected to A2)
		iv += 350
	xbar = mean(x)							# Calculate m & c for A1  , 12 bit
	ybar = mean(y)
	m2 = sum(y*(x-xbar)) / sum(x*(x-xbar))
	m2 = float(m2)
	c2 = ybar - xbar * m2
	c2 = float(c2)
	if abs(m2 - ucm) > dm or abs(c2 - ucc) > dc:	# Error check
		msg(_('Too much error in A2: m = %f  c=%f')%(m2,c2),'red')
		return
	print (m1,c1,m2,c2)
	if p.storeCF_a1a2(m1, c1, m2, c2) == None:	# Store to EEPROM
		msg(_('EEPROM write failed. Old Firmware ?'),'red')
		return
	ss =_('m1 = %f   c1 = %6.3f\nm2 = %f   c2 = %6.3f')%(m1, c1, m2, c2)
	msg(_('A1&A2 Calibration Saved to EEPROM\n')+ss)
	print (ss)

#------------------------------------------------------------------------------
def save_calibref():	# Saves reference voltage
	try:
		v = float(Rref.get())
	except:
		msg(_('Enter the valid voltage'), 'red')
		return
	print (v)
	if (v < 4.950) or (v > 5.050):
		msg(_('Too much error in reference %5.3f voltage')%v, 'red')
		return
	if p.storeCF_ref(v) == None:					# Store to EEPROM
		msg(_('EEPROM write failed. Old Firmware ?'),'red')
		return
	else:
		msg(_('Calibrated Reference. Vref =%5.0f')%v )

#------------------------------------------------------------------------------
def save_calibsen():	# Saves scale factors of A1 & A2 to file 'eyesj.cal'
	v = p.get_voltage(5)
	try:
		r = float(Rsen.get())
		R = r * (5.0 - v) / v
	except:
		msg(_('Enter the R connected to SEN'), 'red')
		return
	print (R)
	if (R < 4950) or (R > 5250):
		msg(_('Wrong Resistor ??. Calculated Rp =%5.1f Ohm')%R, 'red')
		return
	if p.storeCF_sen(R) == None:					# Store to EEPROM
		msg(_('EEPROM write failed. Old Firmware ?'),'red')
		return
	else:
		msg(_('Calibrated SEN. Rp =%5.0f')%R )

#------------------------- Capacitance calibration ------------------------
socket_cap = 0

def measure_socketcap():
	global socket_cap
	sc = p.measure_cap_raw()
	if 10 < sc < 60:
		socket_cap = sc
		msg(_('Empty Socket is %5.1f pF')%socket_cap)
	else:
		msg(_('IN1 not empty'), 'red')

def save_calibcap():
	global socket_cap
	if socket_cap == 0:
		msg(_('Measure Socket capacitance first'), 'red')
		return
	try:
		creal = float(Cin.get())
	except:
		msg(_('Enter the C connected to IN1'), 'red')
		return
	try:
		cm = p.measure_cap_raw() - socket_cap
		error = creal/cm
	except:
		msg(_('Mesuring capacitor failed'),'red')
		return

	print (creal, cm, error, cm*error)
	if error < 0.7 or error > 1.3 or socket_cap > 60:	# Error check
		msg(_('Too much error: Socket C= %f CF=%f')%(socket_cap, error),'red')
		return
	if p.storeCF_cap(socket_cap, error) == None:		# Store to EEPROM
		msg(_('Write to EEPROM failed'),'red')
	else:
		msg(_('Saved: Socket C = %5.1f pF. CF = %5f %%')%(socket_cap, error))

#---------------------------------------------------------------------------------
root = Tk()
Label(root, text = _('Reference Calibration'),font=('Helvetica', 14),\
		fg='blue').pack(side=TOP)
f = Frame(root, relief = SUNKEN)
f.pack(side=TOP)
Label(f, text = _('Enter measured value of Vref')).pack(side=LEFT)
Refval = StringVar()
Rref = Entry(f, width=6, textvariable=Refval)
Rref.pack(side=LEFT)
Refval.set('5.000')
Label(f, text = _('V')).pack(side=LEFT)
Button(root,text = _("and Click here to Set Reference"), \
	command = save_calibref).pack(side = TOP)
separator = Frame(height=2, bd=1, relief=SUNKEN)

separator.pack(fill=X, padx=5, pady=5)
Label(root, text = _('Calibration of Inputs A1 & A2'), fg='blue',\
			font=('Helvetica', 14)).pack(side=TOP)
Label(root, text = _('Connect PVS to both A1 and A2')).pack(side=TOP)
Button(root, text = _("Calibrate A1 & A2"), command = save_calib).pack(side = TOP)
separator = Frame(height=2, bd=1, relief=SUNKEN)
separator.pack(fill=X, padx=5, pady=5)

Label(root, text = _('Calibration of Resistor on SEN'),font=('Helvetica', 14),\
		fg='blue').pack(side=TOP)
f = Frame(root, relief = SUNKEN)
f.pack(side=TOP)
Label(f, text = _('Enter the Resistance connected from SEN to GND=')).pack(side=LEFT)
Rval = StringVar()
Rsen = Entry(f, width=6, textvariable=Rval)
Rsen.pack(side=LEFT)
#Rval.set('4984')
Label(f, text = _('Ohm')).pack(side=LEFT)
Button(root,text = _("and Click here to Calibrate SEN"), \
	command = save_calibsen).pack(side = TOP)
separator = Frame(height=2, bd=1, relief=SUNKEN)
separator.pack(fill=X, padx=5, pady=5)

Label(root, text = _('Calibration of IN1 for Capacitance'),font=('Helvetica', 14),\
		fg='blue').pack(side=TOP)
Button(root,text = _("First, Click Here without Capacitor on IN1"), \
	command = measure_socketcap).pack(side = TOP)
f = Frame(root)
f.pack(side=TOP)
Label(f, text = _('Enter the Capacitance connected to IN1')).pack(side=LEFT)
Cval = StringVar()
Cin = Entry(f, width=6, textvariable = Cval)
Cin.pack(side=LEFT)
#Cval.set('980')
Label(f, text = _('pF')).pack(side=LEFT)
Button(root,text = _("and Click Here to Calibrate IN1"), command = save_calibcap).pack(side = TOP)
separator = Frame(height=2, bd=1, relief=SUNKEN)
separator.pack(fill=X, padx=5, pady=5)

msgwin = Label(root, text = '')
msgwin.pack(side=TOP)
separator = Frame(height=2, bd=1, relief=SUNKEN)
separator.pack(fill=X, padx=5, pady=5)

Button(text = _("Exit"), command = sys.exit).pack(side = TOP)

p = expeyes.eyesj.open()
if p == None:
	root.title(_('EYES Junior Hardware not found'))
eyeplot.pop_image('pics/calibrate.png', _('Calibrate A1 & A2'))

cfvals = _('Existing calibration Data\n')+\
_('Ref=%5.3fV\nm1=%6.3f c1=%6.3f\nm2=%6.3f c2=%6.3f\nIN1: CF=%6.3f Socket=%6.3fpF\n SEN pullup=%6.1fOhm')\
		%(p.refval,p.m12[1], p.c[1],p.m12[2], p.c[2], p.cap_calib, p.socket_cap,p.sen_pullup)
msg(cfvals)
root.title(_('EYES Junior Calibration'))
root.mainloop()


"""bussiness_admin URL Configuration."""

# Django
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include(('app.users.urls', 'users'), namespace='users')),
    path('', include(('app.companies.urls', 'companies'), namespace='companies')),
]


import torch.nn.functional as F
import torch, torch.nn as nn

from ._head import Detect
from ._blocks import MLP, Conv
from .yolov5.models.yolo import check_anchor_order

"""
just another bad model
"""

class BlockMLPv2(nn.Module):
    def __init__(self, ch_in, ch_out, mlp_size=5):
        super().__init__()
        self.ms = mlp_size
        self.mlp = MLP(mlp_size**2, mlp_size**2, mlp_size**2, 1) # looks at whole image
        self.cnn = nn.Sequential(
            Conv(ch_in, ch_in, 3),
            Conv(ch_in, ch_out-1, 3)
        )
        self.lin = nn.Sequential(                                # FFNN for channels
            nn.BatchNorm2d(ch_out),
            nn.Conv2d(ch_out, ch_out, 1),
            nn.SiLU(),
        )

    def forward(self, x):
        # forward
        y1 = self.cnn(x)
        y2 = F.adaptive_avg_pool2d(y1[:,0], (self.ms,self.ms)).flatten(1)
        y2 = self.mlp(y2).view(-1, 1, self.ms, self.ms)
        y2 = F.adaptive_avg_pool2d(y2, y1.shape[2:])
        y = torch.cat((y1,y2), dim=1)
        y = self.lin(y)

        # skip conn
        if x.shape[2:] != y.shape[2:]:
            x = F.adaptive_avg_pool2d(x, y.shape[2:])
        if y.shape[1] == x.shape[1]:
            y = y+x
        else:
            common = min(y.shape[1], x.shape[1])
            tmp = y[:,:common] + x[:,:common]
            y = torch.cat((tmp, y[:,common:]), dim=1)

        return y

def getdownsampler(n, ch, m):
    n = int(n) % 4
    if n==0:
        return nn.Sequential(
            nn.AvgPool2d(kernel_size=3, stride=2, padding=1),
            nn.BatchNorm2d(ch),
            nn.Conv2d(ch, ch, 3, padding=1, groups=ch),
            nn.Conv2d(ch, ch, 1),
            nn.ReLU(),
            nn.AvgPool2d(kernel_size=3, stride=2, padding=1)
            )
    if n==1:
        return nn.AvgPool2d(kernel_size=3, stride=2, padding=1)
    if n==2:
        return nn.Sequential(
            nn.AvgPool2d(kernel_size=3, stride=2, padding=1),
            nn.AvgPool2d(kernel_size=3, stride=2, padding=1)
        )
    if n==3:
        return nn.Sequential(
            BlockMLPv2(ch_in=ch,ch_out=ch, mlp_size=m),
            nn.AvgPool2d(kernel_size=3, stride=2, padding=1),
        )

class MLPDetectorv2(nn.Module):
    """FFNN(wholeimage), CNN(locality)  --> Detection"""
    POS = 12

    def __init__(self, ch_in=1, usepos=False, ch_mult=1, mlp_size=5, down=0, anchors=None):
        super().__init__()
        anchors = anchors if anchors else [[ 3,7,  5,14,  8,20],[13,31, 20,50, 36,80]]
        ch = int(4*ch_mult)

        downsampler = getdownsampler(down, ch*4, mlp_size)
        modules = [
            BlockMLPv2(ch_in=ch_in,ch_out=ch,mlp_size=mlp_size),

            Conv(ch, ch, 3),
            Conv(ch, ch*2, 3),
            nn.AvgPool2d(kernel_size=3, stride=2, padding=1),

            Conv(ch*2, ch*2, 3),
            Conv(ch*2, ch*4, 3),
            nn.AvgPool2d(kernel_size=3, stride=2, padding=1),

            BlockMLPv2(ch_in=ch*4,ch_out=ch*4,mlp_size=mlp_size),
            nn.AvgPool2d(kernel_size=3, stride=2, padding=1),

            downsampler,
            Detect(1, anchors, [ch*4, ch*4]),
        ]

        self.model = nn.ModuleList(modules)
        # Build strides, anchors
        m = self.model[-1]  # Detect()
        if isinstance(m, Detect):
            s = torch.tensor([128,160])
            with torch.no_grad():
                self.S = torch.tensor([x.shape[-3:-1] for x in self.forward(torch.zeros(1, ch_in, *s))[1]])
            m.stride = s / self.S  # forward
            check_anchor_order(m)  # must be in pixel-space (not grid-space)
            m.anchors /= m.stride.view(-1, 1, 2)
            self.stride = m.stride
        self._init_weights()

    def forward(self, x):
        for m in self.model[:-2]:
            x = m(x)
        y = self.model[-2](x)
        return self.model[-1]([y, x])

    @torch.no_grad()
    def _init_weights(self):
        def get_eye(d1,d2):
            w = torch.cat([torch.eye(d2) for _ in range(int(d1/d2))], dim=0)
            return w + torch.randn_like(w) * 1e-3
        for name, param in self.named_parameters():
            if '.mlp' in name:
                if 'weight' in name: # MLP.weight
                    param.copy_(get_eye(*param.shape))
                else:                # MLP.bias
                    param.copy_(torch.zeros_like(param))


'''Train CIFAR10 with PyTorch.'''
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn

import torchvision
import torchvision.transforms as transforms

import os
import argparse

from models import *
from utils import progress_bar

parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')

# data path
parser.add_argument('--data_path', default='./data', type=str, help='data folder path')

# training parameter
parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
parser.add_argument('--resume', '-r', action='store_false',
                    help='resume from checkpoint')

args = parser.parse_args()

device = 'cuda' if torch.cuda.is_available() else 'cpu'
best_acc = 0  # best test accuracy
start_epoch = 0  # start from epoch 0 or last checkpoint epoch

# Data
print('==> Preparing data..')
transform_train = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])

transform_test = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])

trainset = torchvision.datasets.CIFAR10(
    root=args.data_path, train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(
    trainset, batch_size=1024, shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(
    root=args.data_path, train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(
    testset, batch_size=1024, shuffle=False, num_workers=2)

classes = ('plane', 'car', 'bird', 'cat', 'deer',
           'dog', 'frog', 'horse', 'ship', 'truck')

# Model
print('==> Building model..')
net = ResNet18()

net = net.to(device)
if device == 'cuda':
    net = torch.nn.DataParallel(net)
    cudnn.benchmark = True

if args.resume:
    # Load checkpoint.
    print('==> Resuming from checkpoint..')
    assert os.path.isdir('checkpoint'), 'Error: no checkpoint directory found!'
    checkpoint = torch.load('./checkpoint/ckpt.pth')
    net.load_state_dict(checkpoint['net'])
    # best_acc = checkpoint['acc']
    best_acc = 0
    start_epoch = checkpoint['epoch']

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=args.lr,
                      momentum=0.9, weight_decay=5e-4)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=200)

upper = torch.tensor([1-0.4914, 1-0.4822, 1-0.4465])
upper /= torch.tensor([0.2023, 0.1994, 0.2010])
lower = torch.tensor([-0.4914, -0.4822, -0.4465])
lower /= torch.tensor([0.2023, 0.1994, 0.2010])

def fgsm_attack(image, epsilon, data_grad):
    # Collect the element-wise sign of the data gradient
    sign_data_grad = data_grad.sign()
    # Create the perturbed image by adjusting each pixel of the input image
    perturbed_image = image + epsilon*sign_data_grad
    # Adding clipping to maintain in the range
    # transpose the batch and channel dimension
    perturbed_image = perturbed_image.transpose(0,1)
    # perturbed_image = perturbed_image.detach().cpu().numpy()
    for channel, _ in enumerate(perturbed_image):
        u_limit, l_limit = upper[channel], lower[channel]
        # perturbed_image[channel] = torch.clamp(perturbed_image[channel].data, l_limit, u_limit)
        perturbed_image[channel] = perturbed_image[channel].data.clamp(l_limit, u_limit)
    perturbed_image = perturbed_image.transpose(0,1)
    # Return the perturbed image
    return perturbed_image

def test_fgsm(model, device, data, target, epsilon=0.05):

    # Send the data and label to the device
    #data, target = data.to(device), target.to(device)
    # Set requires_grad attribute of tensor. Important for Attack
    data.requires_grad = True
    # Forward pass the data through the model
    output = model(data)

    # Calculate the loss
    loss = criterion(output, target)
    # Zero all existing gradients
    model.zero_grad()
    # Calculate gradients of model in backward pass
    loss.backward()

    # Collect datagrad
    data_grad = data.grad.data
    # Call FGSM Attack
    perturbed_data = fgsm_attack(data, epsilon, data_grad)
    #perturbed_data = data
    # Return the accuracy and an adversarial example

    return perturbed_data

# train the model
def train_one_epoch(epoch):
    print('\nEpoch: %d' % epoch)
    net.train()
    train_loss, correct, total, correct_adv, total_adv = 0,0,0,0,0
    for batch_idx, (inputs, targets) in enumerate(trainloader):
        # normal training
        inputs, targets = inputs.to(device), targets.to(device)
        optimizer.zero_grad()
        outputs = net(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        optimizer.step()

        train_loss += loss.item()
        _, predicted = outputs.max(1)
        total += targets.size(0)
        correct += predicted.eq(targets).sum().item()

        # adversarial training
        # adv_input = inputs
        adv_input = test_fgsm(net, device, inputs, targets)#.to(device)
        adv_input = adv_input.clone().detach().to(device)
        optimizer.zero_grad()
        outputs = net(adv_input)
        loss = criterion(outputs, targets)
        loss.backward()
        optimizer.step()

        train_loss += loss.item()
        _, predicted = outputs.max(1)
        total_adv += targets.size(0)
        correct_adv += predicted.eq(targets).sum().item()

        progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                     % (train_loss/(batch_idx+1), 100.*correct/total, correct, total))
        progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                     % (train_loss/(batch_idx+1), 100.*correct_adv/total_adv, correct_adv, total_adv))

# test the accuracy of the model
def test(epoch):
    global best_acc
    #net.eval()
    test_loss = 0
    correct = 0
    total = 0
    total_adv, correct_adv = 0,0
    # with torch.no_grad():
    for batch_idx, (inputs, targets) in enumerate(testloader):
        inputs, targets = inputs.to(device), targets.to(device)
        outputs = net(inputs)
        loss = criterion(outputs, targets)

        test_loss += loss.item()
        _, predicted = outputs.max(1)
        total += targets.size(0)
        correct += predicted.eq(targets).sum().item()

        # adversarial training
        # adv_input = inputs
        adv_input = test_fgsm(net, device, inputs, targets)#.to(device)
        adv_input = adv_input.clone().detach().to(device)
        optimizer.zero_grad()
        outputs = net(adv_input)
        loss = criterion(outputs, targets)

        test_loss += loss.item()
        _, predicted = outputs.max(1)
        total_adv += targets.size(0)
        correct_adv += predicted.eq(targets).sum().item()

        progress_bar(batch_idx, len(testloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                        % (test_loss/(batch_idx+1), 100.*correct/total, correct, total))
        progress_bar(batch_idx, len(testloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
        % (test_loss/(batch_idx+1), 100.*correct_adv/total_adv, correct_adv, total_adv))

    # Save checkpoint.
    acc = 100.*correct/total
    acc += 100.*correct_adv/total_adv
    acc = acc/2
    if acc > best_acc:
        print('Saving..')
        state = {
            'net': net.state_dict(),
            'acc': acc,
            'epoch': epoch,
        }
        if not os.path.isdir('checkpoint'):
            os.mkdir('checkpoint')
        torch.save(state, './checkpoint/ckpt_adv_scratch.pth')
        best_acc = acc

for epoch in range(start_epoch, start_epoch+200):
    train_one_epoch(epoch)
    test(epoch)
    scheduler.step()


import re
from collections import OrderedDict

from rest_framework import serializers
from rest_framework.relations import Hyperlink, PKOnlyObject

from drf_img_crop.serializers import ImageCreateSerializer

from core.models import Profile
from core.api.serializers import ProfileDetailSerializer, ProfileListSerializer

from jinja2 import Environment, BaseLoader

#from drf_hateoas.fields import HateoasUrlField
from drf_hateoas.serializers import HateoasSerializer
from drf_hateoas.fields import HateoasUrlField

from ..models import Blog, Category, Page, Section, HtmlContent, ContentImage
from .validators import TitleValidator

CONTENT_FIELDS = [
        'pk',
        'title',
        'slug',
        'is_visible',
        'creation_date',
        'changed_date',
    ]

HTML_CONTENT_FIELDS = CONTENT_FIELDS + [
        'body',
        'images',
    ]

BASE_KWARGS = {
    'pk': {'read_only': True},
    'slug': {'read_only': True},
    'creation_date': {'read_only': True},
    'changed_date': {'read_only': True},
}

READ_ONLY_KWARGS = {**BASE_KWARGS, **{'title': {'read_only': True}}}

CONTENT_FIELDS_KWARGS = {**BASE_KWARGS, **{'title': {'required': True}}}

BASE_PATH = '/api/content/'

URLS = {
    'page': BASE_PATH + 'page',
    'section': BASE_PATH + 'section',
    'category': BASE_PATH + 'category',
    'blog': BASE_PATH + 'blog',
    'image': BASE_PATH + 'image',
}

def GET_CONTENT_FIELDS_KWARGS(queryset):
    return {**BASE_KWARGS, **{'title': {'required': True, 'validators':[TitleValidator(queryset=queryset)]}}}

#SLUG_REGEX = re.compile('^(\w+)$')

class BaseListSerializer(HateoasSerializer):
    class Meta:
        fields = CONTENT_FIELDS
        extra_kwargs = READ_ONLY_KWARGS

class SectionListSerializer(HateoasSerializer):
    class Meta:
        model = Section
        fields = CONTENT_FIELDS
        extra_kwargs = GET_CONTENT_FIELDS_KWARGS(queryset=model.objects.all())

class SectionSerializer(HateoasSerializer):
    pages = HateoasUrlField(read_only=True, name='Page', many=True,
                view_name='content-api:page-detail')
    class Meta:
        model = Section
        fields = CONTENT_FIELDS + ['pages']
        extra_kwargs = GET_CONTENT_FIELDS_KWARGS(queryset=model.objects.all())

class PageListSerializer(BaseListSerializer):
    class Meta(BaseListSerializer.Meta):
        model = Page

class PageSerializer(HateoasSerializer):
    #images = ContentImageSerializer(many=True, read_only=True)
    #section = SectionSerializer(read_only=True)
    #section = serializers.SerializerMethodField()
    section = HateoasUrlField(read_only=False, name='Section',
        view_name='content-api:section-detail',
        queryset=Section.objects.all(),
        required=False)

    def create(self, validated_data):
        if self.context['section'] is not None:
            validated_data['section'] = self.context['section']
        return super().create(validated_data)

    class Meta:
        model = Page
        fields = CONTENT_FIELDS + [
            'body',
            'section',
        ]
        extra_kwargs = GET_CONTENT_FIELDS_KWARGS(queryset=model.objects.all())

class CategoryListSerializer(BaseListSerializer):
    class Meta(BaseListSerializer.Meta):
        model = Category

class CategorySerializer(serializers.ModelSerializer):
    #blogs = BlogListSerializer(many=True, read_only=True)
    class Meta:
        model = Category
        fields = CONTENT_FIELDS# + ['blogs']
        extra_kwargs = GET_CONTENT_FIELDS_KWARGS(queryset=model.objects.all())

class BlogListSerializer(BaseListSerializer):
    class Meta(BaseListSerializer.Meta):
        model = Blog

class BlogSerializer(HateoasSerializer):
    author = serializers.PrimaryKeyRelatedField(queryset=Profile.objects.all(), required=False)
    #images = ContentImageSerializer(many=True)
    #categories = CategoryListSerializer(read_only=False, many=True)
    class Meta:
        model = Blog
        fields = CONTENT_FIELDS + [
            'body',
            'author',
            #'section',
        ]
        extra_kwargs = GET_CONTENT_FIELDS_KWARGS(queryset=model.objects.all())

class ContentImageListSerializer(BaseListSerializer):
    class Meta(BaseListSerializer.Meta):
        model = ContentImage

class ContentImageSerializer(serializers.ModelSerializer):
    #pages = PageListSerializer(read_only=True, many=True)
    class Meta:
        model = ContentImage
        fields = CONTENT_FIELDS + [
            'image',
            #'pages',
        ]
        extra_kwargs = {
            'image': {'read_only': True},
            'pk': {'read_only': True},
        }

class ContentImageCreateSerializer(ImageCreateSerializer):

    class Meta:
        model = ContentImage
        fields = CONTENT_FIELDS + [
            'image',
            'crop',
        ]
        extra_kwargs = {**{
            'image': {'required': True},
        }, **GET_CONTENT_FIELDS_KWARGS(queryset=model.objects.all())}

#Read only public serializers

class NavbarPageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Page
        fields = [
            'title',
            'slug',
        ]

class NavbarSerializer(serializers.ModelSerializer):
    pages = serializers.SerializerMethodField('get_nav_pages')
    #pages = NavbarPageSerializer(many=True, read_only=True, required=False)

    title = serializers.SerializerMethodField('get_nav_title')
    slug = serializers.SerializerMethodField('get_nav_slug')
    class Meta:
        model = Section
        fields = [
            'title',
            'slug',
            'pages',
        ]
    def to_representation(self, instance):
        result = super(NavbarSerializer, self).to_representation(instance)
        return OrderedDict([(key, result[key]) for key in result if result[key] is not None])

    def get_nav_title(self, obj):
        return obj.nav_title()

    def get_nav_slug(self, obj):
        return obj.nav_slug()

    def get_nav_pages(self, obj):
        if obj.not_dropdown():
            return None
        else:
            return NavbarPageSerializer(obj.nav_pages(), many=True).data

class HtmlPageSerializer(serializers.ModelSerializer):
    body = serializers.SerializerMethodField()
    class Meta:
        model = Page
        fields = [
            'body',
        ]

    def get_body(self, obj):
        templateVars = {}
        for img in obj.images.all():
            templateVars['img__' + str(img.slug)] = '<img src="' + img.image.url +'" />'
        rtemplate = Environment(loader=BaseLoader()).from_string(obj.body)
        return rtemplate.render(templateVars)

class HtmlBlogSerializer(serializers.ModelSerializer):
    class Meta:
        model = Page
        fields = [
            'body',
            'categories',
        ]


import sys
from collections import deque

vertexList = ['0', '1', '2', '3', '4', '5', '6']
edgeList = [(0,1), (0,2), (1,3),(2,4),(2,5), (4,6)]
graphs=vertexList,edgeList

def DFS(graph,root):
    vertexList,edgeList=graph
    visitedList=[]
    stack=[root]

    adjacencyList = [[] for vertex in vertexList]

    # fill adjacencyList from graph
    for edge in edgeList:
        adjacencyList[edge[0]].append(edge[1])

    while stack:
        current = stack.pop()
        for neighbor in adjacencyList[current]:
            if not neighbor in visitedList:
                stack.append(neighbor)
        visitedList.append(current)
    return visitedList

print(DFS(graphs,0))


import numpy as np
import math
'''
une rgression lineaire exige la continuite mathematique et la convexite quadratique
si on travaille avec des lables discretes il est important de modeliser une telle response avec une fonction mathematique. Parmis les
fonctions les plus utilises on peut citer softmax, 1/(1+exp(-z)) qui se cractrisent par une reponse assez discrete (escalier).
'''
#neurone yes or ok, we don't have a linear relation !!!!
#https://www.youtube.com/watch?v=9KM9Td6RVgQ

class Neural_Network(object):
  def __init__(self,x,y):
     self.data=x/np.amax(x,axis=0)
     self.label=y
     self.label=self.label/100
     #design of network : model  architecture
     self.input_lyaer=self.data.shape[1]

     self.heden_layer_1=3
     self.heden_layer_2=3
     self.heden_layer_3=3
     self.heden_layer=3
     self.output_layer=self.label.shape[1]
     self.W1=np.random.randn(self.input_lyaer,self.heden_layer)
     self.W2=np.random.randn(self.heden_layer,self.output_layer)
     #training parameter
     self.alpha=0.2
     self.max_iteration =10**8
     self.epsilon=10**(-5)

  #Neuron activation 1 or zero
  def sigmoid(self,z):
      return 1/(1+np.exp(-z))
  #prime function of sigmoid
  def sigmoid_prime(self,z):
      return np.exp(-z)/(1+np.exp(-z))**2

  def error(self,a,b):
      vec_error=0
      for i in range(a.shape[0]):
         if (a[i] !=0):
            vec_error=vec_error+(1-b[i]/a[i])**2
         else:
             vec_error=vec_error+(a[i]-b[i])**2
      u=float(vec_error)/a.shape[0]
      #sum(cste_norm
      return u

  def forward_algorithme(self):
      #connections (like convolution)
      self.z2=np.dot(self.data,self.W1)
      #not linear but non-linear relation to characterize neuro response
      self.a2=self.sigmoid(self.z2)
      # new input==> new connections
      self.z3=np.dot(self.a2,self.W2)
      #neuron final response : activation
      self.label_hat=self.sigmoid(self.z3)
      return np.round(self.label_hat,2)

  def forward_algorithme_rule_chaine(self):
      #connections (like convolution)
      self.z2=np.dot(self.data,self.W1)
      #not linear but non-linear relation to characterize neuro response
      self.a2=self.sigmoid(self.z2)
      # new input==> new connections
      self.z3=np.dot(self.a2,self.W2)
      #neuron final response : activation
      self.label_hat=self.sigmoid(self.z3)
      return np.round(self.label_hat,2)

  def cost_function_rule_chaine(self):
      #you use rule chaine aT_(l-1)* delta_l, where delta_l=W(L+1)T*delta_l*sigmoide_prime_l, and aT_(0)=data.transpose
      self.label_hat=self.forward_algorithme()
      self.DJW={}
      delta[self.number_layer]=np.multiply(-(self.label-self.label_hat),self.sigmoid_prime (self.z[self.number_layer]) )
      self.DJW[self.number_layer-1]=np.dot(self.a[number_layer-1].T, delta[self.number_layer])
      for layer in range(self.number_layer):
         delta[self.number_layer-(layer+1)]=np.dot(delta[self.number_layer-(layer+0)],\
         self.W[self.number_layer-(layer+1)])*self.sigmoid_prime (self.z[self.number_layer-(layer+1)])
         self.DJW[self.number_layer-(layer+1)]=np.dot(self.a[number_layer-(layer+2)].T, delta[self.number_layer-(layer+1)])

      return self.DJW

  def cost_function(self):

      self.label_hat=self.forward_algorithme()
      self.diff=self.error(self.label_hat, self.label)
      ''' error cost : 1/2 sum(label- label_hat)^2, first given as raw data, second is given
       as a function of the network'design (w,input)
      rule chaine to explain the gradient with respect to both w parameters '''

      delta3=np.multiply(-(self.label-self.label_hat),self.sigmoid_prime (self.z3) )
      DJW2=np.dot(self.a2.T, delta3)

      delta2=np.dot(delta3, self.W2.T)* self.sigmoid_prime (self.z2)
      DJW1=np.dot(self.data.T, delta2)
      return DJW1, DJW2

  def set_wights_update(self, wights):
      W1_start=0
      W1_end=self.heden_layer*self.heden_layer
      self.W1=np.reshape(wights[W1_start:W1_end], (self.input_lyaer,self.heden_layer))

      W2_end=W1_end+self.heden_layer*self.ouput_layer
      self.WZ=np.reshape(wights[W1_end:W2_end], (self.heden_layer,self.output_lyaer))

  def Backpropagation_algorithme(self):
      #estimate W parameters
      iter_number=0
      fin_condition=False
      while (iter_number<self.max_iteration  and fin_condition==False):

          dJDW1, dJDW2=self.cost_function()
          #update W
          self.W1=self.W1-self.alpha*dJDW1
          self.W2=self.W2-self.alpha*dJDW2
          #take decision
          error_conver=self.error(self.label,self.label_hat)
          fin_condition=error_conver<self.epsilon
          iter_number=iter_number+1

      return  self.label_hat, float(iter_number)/self.max_iteration

#use

size_test=10
a=np.array(np.random.randint(10, size=(size_test,2))).astype(float)
b=np.array(np.random.randint(90, size=(size_test,1))).astype(float)

my_network=Neural_Network(a,b)
error=my_network.forward_algorithme()
a,b=my_network.cost_function()
prdicted_y,iter_number=my_network.Backpropagation_algorithme()
print(my_network.label)
print('\n')
print(my_network.label_hat)
print('\n')
print(iter_number)


'''
@program: Otsu_Thresholding.py

@description: Otsu最佳全局阈值处理

@author: doubleZ

@create: 2019/12/14
'''
import cv2

def Otsu_Thresholding(image):
    '''
    :description: Otsu最佳全局阈值

    :param image: 原图

    :return gray: 原图的灰度图像
    :return thresh: 简单阈值处理后的图像
    :return th1: 对灰度图像进行Otsu最佳全局阈值处理后对图像
    :return blurred: 高斯模糊后的图像
    :return th2:对高斯滤波后的图像进行Otsu最佳全局阈值处理后的图像
    '''
    gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)     # rgb图像变换为灰度图像

    (T, thresh) = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)  # 简单阈值处理

    (T, th1) = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)  # 对灰度图像进行Otsu最佳全局阈值处理

    blurred = cv2.GaussianBlur(gray, (5, 5), 0)     # 高斯滤波降噪

    (T, th2) = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)  # 对高斯滤波后对图像进行Otsu最佳全局阈值处理

    return [gray, thresh, th1, blurred, th2]

if __name__ == "__main__":

    image = cv2.imread('../Resources/bubble.jpeg')

    [gray, thresh, th1, blurred, th2] = Otsu_Thresholding(image)

    cv2.imshow('Original Noisy Image', gray)
    cv2.imshow('Global Thresholding (v=127)', thresh)
    cv2.imshow("Otsu's Thresholding", th1)
    cv2.imshow('Gaussian filtered Image', blurred)
    cv2.imshow("Otsu's Thresholding", th2)

    cv2.waitKey(0)
    cv2.destroyAllWindows()


def trocar_elementos(vetor):
    tamanho = len(vetor)
    for i in range(tamanho // 2):
        vetor[i], vetor[tamanho - 1 - i] = vetor[tamanho - 1 - i], vetor[i]

# Leitura do vetor
vetor = []

print("Digite os valores do vetor (20 elementos):")
for i in range(20):
    numero = int(input())
    vetor.append(numero)

# Impressão do vetor original
print("Vetor original:")
print(vetor)

# Troca dos elementos
trocar_elementos(vetor)

# Impressão do vetor após a troca
print("Vetor após a troca:", vetor)


import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, LSTM
from keras.optimizers import SGD
from keras.utils import to_categorical
from keras.callbacks import EarlyStopping
from keras.losses import BinaryCrossentropy
from sklearn.dummy import DummyClassifier
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score

from preprocessing import full_pipeline, stack_windows

data = pd.read_csv('data/cache.csv')
data.fillna(method='ffill', inplace=True)

scaled_data = full_pipeline.fit_transform(data)

# Split into training and testing sets
total_intervals, n_features = scaled_data.shape
train_len = int(total_intervals * 0.8)
train, test = scaled_data[:train_len], scaled_data[train_len:]

window_len = 15
units_ahead = 0 # 0 is the minimum valid value, representing the next unit in the future

X_train, y_train_pct_change = stack_windows(train, window_len, units_ahead, True)
X_test, y_test_pct_change = stack_windows(test, window_len, units_ahead, True)

y_train = to_categorical(y_train_pct_change > 0)
y_test = to_categorical(y_test_pct_change > 0)

# Create and fit the LSTM model
model = Sequential()
model.add(LSTM(128, activation='relu', input_shape=(window_len, n_features)))
model.add(Dense(2, activation = 'softmax'))

opt = SGD(learning_rate=0.01, momentum=0.0)

model.compile(loss='binary_crossentropy', optimizer=opt)

es = EarlyStopping(monitor='val_loss', mode='min', patience=10, verbose=1)
history = model.fit(X_train, y_train, epochs=100, batch_size=100, verbose=1,
                    validation_data=(X_test, y_test), callbacks=[es])

y_pred = model.predict(X_test)

# loss, accuracy = model.evaluate(X_test, y_test)
# print(loss, accuracy)

# Plot loss over time
fig1, ax1 = plt.subplots()

baseline = BinaryCrossentropy()(y_test, np.ones_like(y_test)*0.5)
ax1.plot(history.history['loss'], label='Train Loss')
ax1.plot(history.history['val_loss'], label='Validation Loss')
ax1.axhline(baseline, color='k', ls='--', label='Naive Loss')
ax1.set_xlabel('epochs')
ax1.set_ylabel('loss')
ax1.legend()

# Compare with DummyClassifier
dummy_classifiers = ['stratified', 'most_frequent', 'prior', 'uniform']
for i in dummy_classifiers:
        dummy_classifier = DummyClassifier(strategy = i).fit(X_train, y_train)
        s = dummy_classifier.score(X_test, y_test)
        print('Score for', i ,'classifier is:', s)

y_pred = np.argmax(y_pred, axis=1)
y_test = np.argmax(y_test, axis=1)

print(y_pred)
print(y_test)

# Calculate accuracy, precision and recall
print(confusion_matrix(y_test, y_pred))
print('Accuracy:', accuracy_score(y_test, y_pred))
print('Precision:', precision_score(y_test, y_pred))
print('Recall:', recall_score(y_test, y_pred))

price_scaler = full_pipeline.named_transformers_['price_pipeline'].named_steps['scaler']
y_test_pct_change = price_scaler.inverse_transform(y_test_pct_change[:, np.newaxis])

test_days = len(y_train) + np.arange(len(y_test))

trading = np.cumprod(y_test_pct_change.flatten() * y_pred + 1)
hold = np.cumprod(y_test_pct_change.flatten() + 1)

# Plot comparison of buy & hold vs trading
fig4, ax4 = plt.subplots()
ax4.plot(test_days, hold, 'k-', label='Hold')
ax4.plot(test_days, trading, 'r-', label='Trading')
ax4.legend()

plt.show()


import requests
import pandas as pd

API_URL = "https://data.binance.com/api/v3/ticker/24hr"

response = requests.get(API_URL)

if response.status_code == 200:
    data = response.json()
else:
    print("Failed to retrieve the data from the API")
    exit()

df = pd.DataFrame(data)

csv_filename ="api_data.csv"
df.to_csv(csv_filename, index=False)
print(f"CSV dataset saved as {csv_filename}")
df = pd.read_csv("api_data.csv")


import os
import utils
import re

scans = utils.read_file(os.getcwd() + "\\input.txt")

def parse_input(scans):
    map = {}
    for y, line in enumerate(scans[:-2]):
        for x, char in enumerate(line):
            if char != " ":
                map[x, y] = char

    instructions = []
    current_num = ""
    for char in scans[-1]:
        if char.isdigit():
            current_num += char
        else:
            instructions.append((int(current_num), char))
            current_num = ""
    instructions.append((int(current_num), "N"))

    return map, instructions

def get_opposite_square(pos, map):
    if pos[2] == 0:
        for x in range(len(scans[pos[1]])):
            if (x, pos[1]) in map.keys():
                return x, pos[1]

    elif pos[2] == 1:
        for y in range(len(scans) - 2):
            if (pos[0], y) in map.keys():
                return pos[0], y

    elif pos[2] == 2:
        for x in range(len(scans[pos[1]]) - 1, -1, -1):
            if (x, pos[1]) in map.keys():
                return x, pos[1]

    elif pos[2] == 3:
        for y in range(len(scans) - 3, -1, -1):
            if (pos[0], y) in map.keys():
                return pos[0], y

    print("error")

def get_forward_square(pos, map):
    forward_pos = (0, 0)
    if pos[2] == 0:
        forward_pos = (pos[0] + 1, pos[1])
    elif pos[2] == 1:
        forward_pos = (pos[0], pos[1] + 1)
    elif pos[2] == 2:
        forward_pos = (pos[0] - 1, pos[1])
    elif pos[2] == 3:
        forward_pos = (pos[0], pos[1] - 1)

    if forward_pos in map.keys():
        return forward_pos
    return get_opposite_square(pos, map)

def navigate(map, instructions):
    pos = (([x for x in range(len(scans[0])) if (x, 0) in map.keys()][0]), 0, 0)

    for curr_instruction in instructions:
        for i in range(curr_instruction[0]):
            coord_next_square = get_forward_square(pos, map)
            if map[coord_next_square] == '#':
                break
            pos = [coord_next_square[0], coord_next_square[1], pos[2]]

        if curr_instruction[1] != 'N':
            new_rotation = (pos[2] + (1 if curr_instruction[1] == "R" else -1)) % 4
            pos = (pos[0], pos[1], new_rotation)

    return pos

if __name__ == "__main__":
    map, instructions = parse_input(scans)
    pos = navigate(map, instructions)
    print(str(1000 * (pos[1] + 1) + 4 * (pos[0] + 1) + pos[2]))


import cv2
import glob
import os

inputFolder='/content/drive/My Drive/Dataset/test_set/right'
folderLen=len(inputFolder)
os.mkdir('right_test')
for img in glob.glob(inputFolder+"/*jpg"):
	image = cv2.imread(img)
	imgResized=cv2.resize(image,(256,256))
	cv2.imwrite("right_test"+img[folderLen:],imgResized)


import math
def check(p, c): # 检查p点是否在圆c内，p包含[x轴座标, y轴座标]，c包含[圆心的x轴座标, 圆心的y轴座标, 圆的半径]
    x = p[0] - c[0] # 计算x轴的差距
    y = p[1] - c[1] # 计算y轴的差距
    distance = math.sqrt(math.pow(x, 2) + math.pow(y, 2)) # 毕氏定理
    if (distance <= c[2]): # 距离是否小于半径
        return True
    else:
        return False

circle = list(map(int, input().split(','))) # 读入圆的资讯
c1 = [circle[0], circle[1], circle[2]] # 将第一个圆的x轴座标, y轴座标, 半径放入c1的串列中
c2 = [circle[3], circle[4], circle[5]] # 将第二个圆的x轴座标, y轴座标, 半径放入c2的串列中

times = int(input()) # 测资数量
for t in range(times): # 循环times次
    output = {'inside': 0, 'outside': 0, 'c1': 0, 'c2': 0} # 该行测资的点位置资讯
    point = list(map(int, input().split(','))) # 读入所有点的座标
    for i in range(0, len(point), 2): # 因为每两个为一组(x,y)，因此每次循环执行后i需要+2
        p = [point[i], point[i+1]] # 建立点p的座标串列

        result = [check(p, c1), check(p, c2)] # 计算p是否在第一个圆和第二个圆，若是则为True
        if (result[0] and result[1]): # 若result的两个结果都是True，代表点p同时在两个圆内
            output['inside'] += 1
        elif (result[0] and not result[1]): # 当result = [True, False]代表点p只在第一个圆内
            output['c1'] += 1
        elif (not result[0] and result[1]): # 当result = [False, True]代表点p只在第二个圆内
            output['c2'] += 1
        else: # 当result的两个结果都是False，则代表点p在两个圆外
            output['outside'] += 1

    print(f"{output['inside']},{output['outside']},{output['c1']},{output['c2']}")


""""// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :
"""

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

# BFS-
class Solution:
    def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        result = []
        queue = [root]
        if not root:
            return result

        while queue:
            size = len(queue)
            li = []
            for i in range(0, size):
                Tnode = queue.pop(0)
                if Tnode.left != None:
                    queue.append(Tnode.left)
                if Tnode.right != None:
                    queue.append(Tnode.right)
                li.append(Tnode.val)
            result.append(li)
        return result

# DFS-
# class Solution:
#     def __init__(self):
#         self.result=[]
#     def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
#         self.helper(root,0)
#         return self.result
#     def helper(self, root, level):
#         if not root:
#             return
#         if len(self.result)==level:
#             self.result.append([])
#         self.result[level].append(root.val)

#         self.helper(root.left, level+1)
#         self.helper(root.right, level+1)

# For printing output [3,9,20,15,7] BFS
# class Solution:
#     def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
#         result=[]
#         queue=[root]
#         if not root:
#             return result

#         while queue:
#             # size=len(queue)
#             # li=[]
#             # for i in range(0,size):
#             Tnode=queue.pop(0)
#             if Tnode.left!=None:
#                 queue.append(Tnode.left)
#             if Tnode.right!=None:
#                 queue.append(Tnode.right)
#             result.append(Tnode.val)
#             # result.append(li)
#         print(result)
#         return result


# จงสร้าง class Queue โดยใช้ linkedlist หรือ list หรือ deque แล้วเขียน method ต่อไปนี้ให้สมบูรณ์ และทำงานตามตัวอย่างได้

#     __str__():  ใช้สำหรับแสดงข้อมูล

#     __len__():  ใช้สำหรับบอกจำนวนสมาชิกข

#     enQueue(data):  ใช้สำหรับเพิ่มข้อมูลเข้าคิว

#     deQueue():  ใช้สำหรับนำข้อมูลออกจากคิว

#     isEmpty():  ตรวจสอบว่าคิวว่างหรือไม่ ถ้าว่าจะแสดงค่า True ถ้าไม่ใช้ แสดง False

class Queue:
    def __init__(self):
        self.items = list()

    def enQueue(self, value):
        self.items.append(value)

    def __len__(self):
        return len(self.items)

    def isEmpty(self):
        return len(self.items) <= 0

    def deQueue(self):
        if not self.isEmpty():
            return self.items.pop(0)
        else:
            pass

    def __str__(self):
        if not self.isEmpty():
            s = 'Queue data : '
            for ele in self.items:
                s = s+str(ele)+' '
            return s
        else:
            return 'Empty Queue'

n = int(input('Enter choice : '))
if n == 1:
    q1 = Queue()
    q1.enQueue(10)
    q1.enQueue(20)
    q1.enQueue(30)
    print(q1)
    q1.deQueue()
    q1.enQueue(40)
    print("Size of Queue :",len(q1))
    print(q1)

elif n == 2:
    q1 = Queue()
    q1.enQueue(100)
    q1.enQueue(200)
    q1.enQueue(300)
    q1.deQueue()
    print(q1)
    print("Queue is Empty :",q1.isEmpty())

elif n == 3:
    q1 = Queue()
    q1.enQueue(11)
    q1.enQueue(22)
    q1.enQueue(33)
    q1.deQueue()
    q1.deQueue()
    q1.deQueue()
    print(q1)
    print("Size of Queue :",len(q1))
    print("Queue is Empty :",q1.isEmpty())


from typing import List

class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        def isSubstring(s:str, t:str):
            return s[:len(t)] == t

        dp = [False] * (len(s) + 1)
        dp[len(s)] = True
        for i in range(len(s), -1, -1):
            for word in wordDict:
                print(s[i:])
                if isSubstring(s[i:], word):
                    print(i, word, dp)
                    dp[i] = dp[i + len(word)]
                    if dp[i]:
                        break
        print(dp)
        return dp[0]

Solution().wordBreak(s = "abcd", wordDict = ["a","abc","b","cd"])

#       catsandog -cats-> andog
#                 -cat-> sandog -sand-> og
#
#
#
#
#
#
#
#
#