import customtkinter as ctk

class ConfirmDeleteOldestBackupDialog(ctk.CTkToplevel):
    def __init__(self, parent, controller, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        # Configure variables
        self.controller = controller
        self.label_text = "You are only allowed 10 backup files. If you save\nthis backup the oldest backup file will be deleted.\n\nAre you sure you want to continue with the backup?"

        # Configure window
        self.geometry("400x180")
        self.title = f"Confirm delete last backup."

        # Configure grid layout
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure((0, 1), weight=1)

        # Create label
        self.label = ctk.CTkLabel(self, text=self.label_text)
        self.label.grid(row=0, column=0, columnspan=2, padx=20, pady=20,
                        sticky="nsew")

        # Create button YES
        self.yes_button = ctk.CTkButton(self, text="Yes", command=lambda: self.controller.save_backup_dialog_event(input=True, dialog=self))
        self.yes_button.grid(row=1, column=0, padx=20, pady=20, sticky="nsew")

        # Create button NO
        self.no_button = ctk.CTkButton(self, text="Cancel", command=lambda: self.controller.save_backup_dialog_event(input=False, dialog=self))
        self.no_button.grid(row=1, column=1, padx=20, pady=20, sticky="nsew")


from django.db import models

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
import uuid
from users.models import Profile
from ckeditor.fields import RichTextField
# Create your models here.

def user_directory_path(instance,filename):
    return 'blogs/{0}/{1}'.format(instance.id,filename)

class Category(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name
class Blog(models.Model):
    owner = models.ForeignKey(
        Profile, null=True, blank=True, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    content = models.TextField()
    likes = models.ManyToManyField(Profile,related_name="blogs",null=True,blank=True)
    category = models.ForeignKey(Category,on_delete=models.PROTECT,default=1)
    favorites = models.ManyToManyField(Profile,related_name='favorite',default=None,blank=True)
    likes = models.ManyToManyField(Profile,related_name='like',default=None,blank=True)
    featured_image = models.ImageField(null=True, blank=True,upload_to=user_directory_path, default="default.jpg")
    created = models.DateTimeField(auto_now_add=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True,primary_key=True, editable=False)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['created']

    @property
    def imageURL(self):
        try:
            url = self.featured_image.url
        except:
            url = ''
        return url
    @property
    def total_likes(self):
        return self.likes.count()
    @property
    def total_comments(self):
        return self.comments.count()
    @property
    def reviewers(self):
        queryset = self.review_set.all().values_list('owner__id', flat=True)
        return queryset

    @property
    def getVoteCount(self):
        reviews = self.review_set.all()
        upVotes = reviews.filter(value='up').count()
        totalVotes = reviews.count()

        ratio = (upVotes / totalVotes) * 100
        self.vote_total = totalVotes
        self.vote_ratio = ratio

        self.save()

class Comment(models.Model):
    owner = models.ForeignKey(Profile,null=True,blank=True,on_delete=models.CASCADE)
    blog = models.ForeignKey(Blog,on_delete=models.CASCADE,related_name="comments")
    content = models.TextField(null=True, blank=True)
    created = models.DateTimeField(default=timezone.now)
    id = models.UUIDField(default=uuid.uuid4,unique=True,primary_key=True,editable=False)
    class Meta:
        ordering = ['-created']

    def __str__(self):
        return f"comment by {self.owner}"


import numpy as np
import pandas as pd
import itertools
from sklearn import metrics
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
# models that are being considered
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import StackingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.svm import SVC
from sklearn.neural_network import MLPClassifier

"""
def grid_search_cross_validation(x_train, y_train, grid, model):
    gridCV = GridSearchCV(model, grid, cv=10)
    gridCV.fit(x_train, y_train.T.squeeze())
    return gridCV.best_params_

def get_svc_best_params(x_train, y_train):
    kernel = ['poly', 'sigmoid']
    degree = [3, 4, 5]
    tol = [ 10**(-3)]
    grid = {
        'kernel' : kernel,
        'degree' : degree,
    }
    res = grid_search_cross_validation(x_train, y_train, grid, SVC())
    print(res)
"""

def print_accuracy_scores(performance_data):
    print('Accuracy scores:')
    for i, data in enumerate(performance_data):
        model_name = data[0]
        pred = data[1]
        test = data[2]
        acc = metrics.accuracy_score(y_true=pred, y_pred=test, normalize=True)
        print(model_name + ' accuracy: ', acc)

def print_f1_score(performance_data):
    print('f1 scores:')
    for i, data in enumerate(performance_data):
        model_name = data[0]
        pred = data[1]
        test = data[2]
        acc = metrics.f1_score(y_true=pred, y_pred=test, average='macro')
        print(model_name + ' f1 score: ', acc)

def cross_validation_acc_score(x, y, clf):
    skfold = StratifiedKFold(n_splits=10).split(x, y)
    score = cross_val_score(clf, x, y, cv=skfold)
    print('Accuracy {}%'.format(score.mean()*100))

"""
def find_model():
    label='Vote'
    x_train = pd.read_csv("x_train.csv", header=0)
    y_train = pd.read_csv("y_train.csv", squeeze=True, header=None)
    x_valid = pd.read_csv("x_valid.csv", header=0)
    y_valid = pd.read_csv("y_valid.csv", squeeze=True, header=None)
    x_test = pd.read_csv("x_test.csv", header=0)
    y_test = pd.read_csv("y_test.csv", squeeze=True, header=None)
    #get_random_forest_best_params(x_train, y_train)
    x = x_train
    y = y_train

    # Best parameters for Random Tree Forest: {'criterion': 'gini', 'max_depth': 30, 'max_features': 'auto', 'min_samples_split': 2, 'n_estimators': 50}
    rand_forest_clf = RandomForestClassifier(criterion='gini', max_depth=50, min_samples_split=5, n_estimators=50)
    cross_validation_acc_score(x, y, rand_forest_clf)
    rand_forest_clf.fit(x, y)
    prediction_rand_forest = rand_forest_clf.predict(x_valid)

    # Best parameters for SVC {'degree': 4, 'kernel': 'poly'}
    svm_poly_clf = SVC(kernel='poly', degree=4, probability=True)
    svm_poly_clf.fit(x, y)
    prediction_svm_poly = svm_poly_clf.predict(x_valid)

    # Multi-layer perceptron classifier
    perceptron_clf = MLPClassifier(activation="relu", alpha=0.1, hidden_layer_sizes=(10, 10, 10),
                                   learning_rate="constant", max_iter=2000)
    perceptron_clf.fit(x, y)
    prediction_perceptron = perceptron_clf.predict(x_valid)

    estimators = [
        ('Random Forest',  RandomForestClassifier(criterion='gini', max_depth=50, min_samples_split=5, n_estimators=50)),
        ('SVC', SVC(kernel='poly', degree=4, probability=True)),
        ('Percepton', MLPClassifier(activation="relu", alpha=0.1, hidden_layer_sizes=(10, 10, 10),
                                   learning_rate="constant", max_iter=2000))
    ]
    blend_clf = StackingClassifier(estimators)
    blend_clf.fit(x, y)
    prediction_blend = blend_clf.predict(x_valid)
    # evaluate and plot confusion matrices

    performance_data = [('Random Forest', prediction_rand_forest, y_valid),
                        ('SVM Polinomial Kernel', prediction_svm_poly, y_valid),
                        ('Perceptron', prediction_perceptron, y_valid),
                        ('Blending ', prediction_blend, y_valid)
                        ]
    print_accuracy_scores(performance_data)
    print_f1_score(performance_data)

    prediction = prediction_blend
    parties = np.unique(prediction)

    num_votes_for_party = lambda party: len([vote for vote in prediction if vote == party])
    list_of_parties = [(party, num_votes_for_party(party)) for party in parties]
    num_votes = len(y_test.index)

    winner = max(list_of_parties, key=lambda item: item[1])
    print('Party with most probable majority of votes')
    print(winner[0], ':', winner[1], ',', winner[1] * 100 / num_votes, '%')

    # 2. Division of voters between the parties
    print('Amount of votes per party')
    for party_votes in sorted(list_of_parties, key=lambda votes: votes[1], reverse=True):
        print(party_votes[0], ':', party_votes[1], ',', party_votes[1] * 100 / num_votes, '%')
"""

if __name__ == '__main__':
    label = 'Vote'
    x_train = pd.read_csv("x_train.csv", header=0)
    y_train = pd.read_csv("y_train.csv", squeeze=True, header=None)
    x_valid = pd.read_csv("x_valid.csv", header=0)
    y_valid = pd.read_csv("y_valid.csv", squeeze=True, header=None)
    x_test = pd.read_csv("x_test.csv", header=0)
    y_test = pd.read_csv("y_test.csv", squeeze=True, header=None)
    # get_random_forest_best_params(x_train, y_train)
    x = x_train
    y = y_train

    estimators = [
        ('Random Forest',  RandomForestClassifier(criterion='gini', max_depth=50, min_samples_split=5, n_estimators=50)),
        ('SVC', SVC(kernel='poly', degree=4, probability=True)),
        ('Percepton', MLPClassifier(activation="relu", alpha=0.1, hidden_layer_sizes=(10, 10, 10),
                                   learning_rate="constant", max_iter=2000))
    ]
    blend_clf = StackingClassifier(estimators)
    blend_clf.fit(x, y)
    prediction = blend_clf.predict(x_test)
    # evaluate and plot confusion matrices

    parties = np.unique(prediction)

    num_votes_for_party = lambda party: len([vote for vote in prediction if vote == party])
    list_of_parties = [(party, num_votes_for_party(party)) for party in parties]
    num_votes = len(y_test.index)

    winner = max(list_of_parties, key=lambda item: item[1])
    print('Party with most probable majority of votes')
    print(winner[0], ':', winner[1], ',', winner[1] * 100 / num_votes, '%')

    # 2. Division of voters between the parties
    print('Amount of votes per party')
    for party_votes in sorted(list_of_parties, key=lambda votes: votes[1], reverse=True):
        print(party_votes[0], ':', party_votes[1], ',', party_votes[1] * 100 / num_votes, '%')

    performance_data = [('Blending ', prediction, y_test)]
    print_accuracy_scores(performance_data)
    print_f1_score(performance_data)


# Import libraries
import numpy as np
import pandas as pd
pd.options.mode.chained_assignment = None
from sqlalchemy import create_engine
from googlesearch import search
from tqdm import tqdm
tqdm.pandas()

# Read data
df = pd.read_csv('data/user-item-interactions.csv')
df_content = pd.read_csv('data/articles.csv')
del df['Unnamed: 0']
del df_content['Unnamed: 0']

# <----- CLEAN DATA [start] ----->
# Remove duplicate articles
df_content = df_content.drop_duplicates(keep='first').reset_index(drop=True)
df_content = df_content.drop_duplicates(subset='article_id', keep='first').reset_index(drop=True)

# Format matching columns to same type
df = df.astype({'article_id': int})

# Make User-id column in df to identify users
user_id_dict = dict()
i=0
for email in df.email:
    if email not in user_id_dict:
        user_id_dict[email] = i
        i+=1
df['user_id'] = df.email.apply(lambda x: user_id_dict[x])
df.drop('email', axis=1, inplace=True)

# Fill in missing document descriptions with empty strings
df_content.doc_description[df_content.doc_description.isnull()] = ''
# <----- CLEAN DATA [finished] ----->

# Merge data-sets on article id
df_merged = df.drop('title', axis=1).merge(df_content[['article_id', 'doc_full_name', 'doc_description']], on='article_id', how='outer')

# Fill in missing document titles
no_title_ids = df_merged.article_id[df_merged.doc_full_name.isnull()].unique().tolist()
for id in no_title_ids:
    title = df.title[df.article_id == id].tolist()[0]
    df_merged.doc_full_name[df_merged.article_id == id] = title

# Fill in missing descriptions with empty string
df_merged.doc_description[df_merged.doc_description.isnull()] = ''

# Make subset of merged dataframe and drop all duplicates
df_subset = df_merged[['article_id', 'doc_full_name', 'doc_description']].drop_duplicates(keep='first').reset_index(drop=True)

# Extract article links through google searches for all articles in the subset dataframe
doc_identifier = df_subset.doc_full_name + ' ' + df_subset.doc_description
def extract_link(text):
    try:
        link = list(search(text, tld="com", num=1, stop=1))[0]
    except:
        link = "https://www.google.com"
    return link
df_subset['link'] = doc_identifier.progress_apply(extract_link)

# Distribute links to all rows of the merged dataframe
df_merged['link'] = df_merged.article_id.apply(lambda x: df_subset.link[df_subset.article_id==x].tolist()[0])

# Save data to database
engine = create_engine('sqlite:///data/data.db')
df_merged.to_sql('user-article-interactions', engine, index=False, if_exists='replace')


import argparse
import os

import cv2
import matplotlib.pyplot as plt
import maxflow
import networkx as nx
import numpy as np

class GraphCuts:

    def __init__(self, src, target, mask, save_graph=False):
        """
        Initialize the graph and computes the min-cut.
        :param src: image to be blended
        :param target: background image
        :param mask: manual mask with constrained pixels
        :param save_graph: if true, graph is saved
        """

        assert (src.shape == target.shape), \
            f"Source and target dimensions must be same: {str(src.shape)} != {str(target.shape)}"

        # Creating the graph and adding nodes
        graph = maxflow.Graph[float]()
        node_ids = graph.add_grid_nodes((src.shape[0], src.shape[1]))

        self.compute_edge_weights(src, target)  # self.edge_weights is inside func(compute_edge_weights)

        # Adding non-terminal edges
        patch_height = src.shape[0]
        patch_width = src.shape[1]
        for row_idx in range(patch_height):
            for col_idx in range(patch_width):
                # Horizontal edge
                if col_idx + 1 < patch_width:
                    weight = self.edge_weights[row_idx, col_idx, 0]
                    graph.add_edge(node_ids[row_idx][col_idx],
                                   node_ids[row_idx][col_idx + 1],
                                   weight,
                                   weight)

                # Vertical edge
                if row_idx + 1 < patch_height:
                    weight = self.edge_weights[row_idx, col_idx, 1]
                    graph.add_edge(node_ids[row_idx][col_idx],
                                   node_ids[row_idx + 1][col_idx],
                                   weight,
                                   weight)

                # Adding terminal edge capacities for the pixels constrained to belong to the source/sink.
                # http://pmneila.github.io/PyMaxflow/maxflow.html
                # 검토) add_tedge 대신 다른 api 쓸 순 없을까? np.inf 넣기 싫은데.
                if np.array_equal(mask[row_idx, col_idx, :], [0, 255, 255]):
                    graph.add_tedge(node_ids[row_idx][col_idx], 0, np.inf)
                elif np.array_equal(mask[row_idx, col_idx, :], [255, 128, 0]):
                    graph.add_tedge(node_ids[row_idx][col_idx], np.inf, 0)

        # Plot graph
        if save_graph:
            nxg = graph.get_nx_graph()
            self.plot_graph_2d(nxg, (patch_height, patch_width))
            # 디버깅
            # print('nxg {}'.format(nxg)) # nxg
            # print('type of nxg {}'.format(type(nxg))) # type of nxg <class 'networkx.classes.digraph.DiGraph'>

        # Computing maxflow / mincut
        flow = graph.maxflow()
        self.sgm = graph.get_grid_segments(node_ids)

    def compute_edge_weights(self, src, target):
        """
        Compute edge weights based on matching quality cost.
        :param src: image to be blended (foreground)
        :param target: background image
        """

        self.edge_weights = np.zeros((src.shape[0], src.shape[1], 2))

        # Create shifted versions of the matrics for vectorized operations.
        src_left_shifted = np.roll(src, -1, axis=1)
        target_left_shifted = np.roll(target, -1, axis=1)
        src_up_shifted = np.roll(src, -1, axis=0)
        target_up_shifted = np.roll(target, -1, axis=0)

        eps = 1e-10  # Numerical stability

        # Horizontal weights
        horizontal_weight = np.sum(np.square(src - target, dtype=np.float) +
                                   np.square(src_left_shifted - target_left_shifted, dtype=np.float),
                                   axis=2)

        horizontal_norm_factor = np.sum(np.square(src - src_left_shifted, dtype=np.float) +
                                        np.square(target - target_left_shifted, dtype=np.float),
                                        axis=2)

        self.edge_weights[:, :, 0] = horizontal_weight / (horizontal_norm_factor + eps)

        # Vertical weights
        vertical_weight = np.sum(np.square(src - target, dtype=np.float) +
                                 np.square(src_up_shifted - target_up_shifted, dtype=np.float),
                                 axis=2)

        vertical_norm_factor = np.sum(np.square(src - src_up_shifted, dtype=np.float) +
                                      np.square(target - target_up_shifted, dtype=np.float),
                                      axis=2)

        self.edge_weights[:, :, 1] = vertical_weight / (vertical_norm_factor + eps)

    def plot_graph_2d(self, graph, nodes_shape,
                      plot_weights=True,
                      plot_terminals=True,
                      font_size=7):
        """
        Plot the graph to be used in graph cuts
        :param graph: Maxflow graph
        :param nodes_shape: patch shape
        :param plot_weights: if true, edge weights are shown
        :param plot_terminals: if true, the terminal nodes are shown
        :param font_size: text font size
        """

        X, Y = np.mgrid[:nodes_shape[0], :nodes_shape[1]]
        aux = np.array([Y.ravel(), X[::-1].ravel()]).T
        positions = {i: v for i, v in enumerate(aux)}
        positions['s'] = (-1, nodes_shape[0] / 2.0 - 0.5)
        positions['t'] = (nodes_shape[1], nodes_shape[0] / 2.0 - 0.5)

        # nx.draw(graph, cmap=plt.get_cmap('jet')) maxflow로 안 가져오고 networkx에서 바로 그리기
        plt.show()
        nxgraph = graph.get_nx_graph()
        print("nxgraph created")
        if not plot_terminals:
            nxgraph.remove_nodes_from(['s', 't'])

        plt.clf()
        nx.draw(nxgraph, pos=positions)

        if plot_weights:
            edge_labels = {}
            for u, v, d in nxgraph.edges(data=True):
                edge_labels[(u, v)] = d['weight']
            nx.draw_networkx_edge_labels(nxgraph,
                                         pos=positions,
                                         edge_labels=edge_labels,
                                         label_pos=0.3,
                                         font_size=font_size)

        plt.axis('equal')
        plt.show()

    def blend(self, src, target):
        """
        Blends the target image with the source image based on the graph cut.
        :param src: Source image
        :param target: Target image
        :return target : Blended image
        """
        target[self.sgm] = src[self.sgm]

        return target

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-i', dest='image_dir', required=True, help='Saved Path of Source & Target Images.')
    args = parser.parse_args()

    # Read the images and the mask.
    image_dir = args.image_dir
    src = cv2.imread(os.path.join(image_dir, 'src.jpg'))
    target = cv2.imread(os.path.join(image_dir, 'target.jpg'))
    mask = cv2.imread(os.path.join(image_dir, 'mask.png'))

    # Compute the min-cut.
    graphcuts = GraphCuts(src, target, mask)

    # Save the output.
    target = graphcuts.blend(src, target)
    cv2.imwrite(os.path.join(image_dir, "result.png"), target)


import json
leer = []
for linea in open('202006_movements.json','r'):
    leer.append(json.loads(linea))

#print (linea)

datos = []
def leer():
    for linea in open('202006_movements.json','r'):
        datos.append(json.loads(linea))

def recogidasPorPunto():
    resultado = dict()
    for obj in datos:
        clave = "Punto " + str(obj['idunplug_station'])
        resultado[clave] = resultado.get(clave, 0) + 1
    print(resultado)
    resultadoSort = list()
    for i in range(len(resultado)):
        resultadoSort.append(resultado.get("Punto " + str(i)))
    print (resultadoSort)
    return

def recogidasPorEdad():
    resultado = dict()
    for obj in datos:
        clave = "ageRange " + str(obj['ageRange'])
        resultado[clave] = resultado.get(clave, 0) + 1
    print(resultado)
    resultadoSort = list()
    for i in range(len(resultado)):
        resultadoSort.append(resultado.get("ageRange " + str(i)))
    print (resultadoSort)
    return

def puntoRecYDev():
    resultado = list()
    for obj in datos:
        if not obj["idplug_station"] in resultado:
            if obj["idplug_station"] == obj["idunplug_station"]:
                resultado.append(obj["idplug_station"])
    resultado.sort()
    print(resultado)
    print(len(resultado))
    return

leer()
recogidasPorEdad()


#!/usr/bin/env python3
import glob
import os.path
import re
import statistics
import sys
from collections import defaultdict
from typing import List, Dict

"""
USAGE:
./simple_spec_summary.py   # all files in /spec/result/
./simple_spec_summary.py 1 10   # result 1-10 from /spec/result/
./simple_Spec_summary.py <list> <of> <csv> <files>
"""

def draw_table(table: List[List[str]], hline_after=()):
    column_width = defaultdict(lambda: 0)
    for row in table:
        for i, col in enumerate(row):
            column_width[i] = max(column_width[i], len(col))
    txt = []
    for i, row in enumerate(table):
        for j, col in enumerate(row):
            txt.append(col + ' ' * (column_width[j] - len(col)))
            if j < len(row) - 1:
                txt.append(' | ')
        txt.append('\n')
        if i in hline_after:
            # txt.append('-' * (sum(column_width.values()) + 3 * len(row) - 3) + '\n')
            txt.append('-|-'.join('-' * v for k, v in sorted(column_width.items(), key=lambda x: x[0])) + '\n')
    return ''.join(txt)

def load_spec_files(files: List[str]) -> Dict[str, Dict[str, List[float]]]:
    """
    :param files:
    :return: {benchmark type: {benchmark name: [list, of, results]}}
    """
    results = {}
    for fname in files:
        if not os.path.exists(fname):
            print('MISSING FILE', fname)
            continue
        with open(fname, 'r') as f:
            text = f.read()
            name = [l[12:-1] for l in text.split('\n') if l.startswith('"test name: ')][0]
            if name == 'llvm-o3-typegraph':
                name = 'llvm-o3-typro'
            if name not in results:
                results[name] = {}
            table = text.split('"Selected Results Table"')[1].split('"Run number:"')[0]
            for l in table.split('\n'):
                if l.startswith('4'):
                    elements = l.split(',')
                    if elements[2]:
                        bench_name = elements[0]
                        if re.match(r'\d{3}\.\w+', bench_name):
                            bench_name = bench_name.split('.', 1)[1]
                        if bench_name not in results[name]:
                            results[name][bench_name] = []
                        results[name][bench_name].append(float(elements[2]))
    return results

def summarize_spec_files(files: List[str]):
    results = load_spec_files(files)
    assert 'llvm-o3-typro' in results, 'No typro runs!'
    assert 'llvm-o3-ref' in results, 'No reference runs!'
    benchmarks = list(sorted(results['llvm-o3-typro']))
    table = [['Benchmark', 'Typro runtime (stdev)', 'Ref runtime (stdev)', 'Overhead']]
    for bench in benchmarks:
        runtime_typro = sum(results['llvm-o3-typro'][bench]) / len(results['llvm-o3-typro'][bench])
        runtime_ref = sum(results['llvm-o3-ref'][bench]) / len(results['llvm-o3-ref'][bench])
        stdev_typro = statistics.stdev(results['llvm-o3-typro'][bench]) / runtime_typro
        stdev_ref = statistics.stdev(results['llvm-o3-ref'][bench]) / runtime_ref
        overhead = runtime_typro / runtime_ref - 1
        table.append([
            bench,
            f'{runtime_typro:5.1f} s     (+-{stdev_typro*100:4.1f}%)',
            f'{runtime_ref:5.1f} s   (+-{stdev_ref*100:4.1f}%)',
            f'{overhead * 100:5.2f}%'])
    print(draw_table(table, (0,)))

if __name__ == '__main__':
    if len(sys.argv) == 3 and re.match(r'\d+', sys.argv[1]) and re.match(r'\d+', sys.argv[2]):
        files = []
        for i in range(int(sys.argv[1]), int(sys.argv[2]) + 1):
            files.append(f'/spec/result/CINT2006.{i:03d}.ref.csv')
            files.append(f'/spec/result/CFP2006.{i:03d}.ref.csv')
        summarize_spec_files(files)
    elif len(sys.argv) > 1:
        summarize_spec_files(sys.argv[1:])
    else:
        summarize_spec_files(glob.glob('/spec/result/*.ref.csv'))


import numpy as np
import matplotlib.pyplot as plt

def plot_with_exponential_averaging(x, y, label, alpha):
    y_ema = [y[0],]
    for y_i in y[1:]:
        y_ema.append(y_ema[-1] * alpha + y_i * (1 - alpha))

    p = plt.plot(x, y_ema, label=label)

    plt.plot(x, y, color=p[0].get_color(), alpha=0.2)

def plot_train_result(result, label="", alpha=0.95, save_path="./", threshold=None):
    rewards = [r['r'] for r in result]
    lengths = [r['l'] for r in result]

    plot_with_exponential_averaging(np.cumsum(lengths), rewards, label, alpha)
    plt.axhline(y=threshold if threshold else int(max(rewards)*1.1), color='grey', linestyle='-')
    plt.xlabel("Training Steps")
    plt.ylabel("Episode Reward")

    plt.legend()
    plt.title(label)
    plt.savefig(save_path)
    plt.cla()


#!/usr/local/python3/bin/python3
import sys
sys.path.append("..")
import tushare as ts
import re
import datetime
import basicdata.basic_mgr as sk
import time
import os
import pandas as pd

g_update_newest=False #True|False
#是否下载最新的概念，一般不需要

g_ctcode_name=None
#g_ctcode_name['TS56']='电改'

g_tscode_concept=None
#g_tscode_concept['000008.SZ']={'id':['TS56','TS59'],'name':['电改','特斯拉']}

def get_ctname_by_tscode(pro, tscode):
    global g_tscode_concept
    if g_tscode_concept is None:
        init_tscode_concept(pro)
    return g_tscode_concept[tscode]['name']

def get_ctcode_by_tscode(pro, tscode):
    global g_tscode_concept
    if g_tscode_concept is None:
        init_tscode_concept(pro)
    return g_tscode_concept[tscode]['id']

def init_tscode_concept(pro):
    global g_tscode_concept
    global g_update_newest
    if g_tscode_concept is None:
        g_tscode_concept = {}
        ts_codes=sk.get_tscodes(pro)
        for i in range(len(ts_codes)):
            ts_code=ts_codes[i]
            path='./concept-data/'+ts_code+'.concept.csv'
            if g_update_newest == False and os.path.exists(path) == True:
                conceptdf=pd.read_csv(path)
            else:
                conceptdf=pro.concept_detail(ts_code=ts_code)
                if conceptdf is not None:
                    conceptdf.to_csv(path)
                time.sleep(1)
                print("download", path)
            if conceptdf is not None:
                conceptids=conceptdf['id'].values.tolist()
                conceptnames=conceptdf['concept_name'].values.tolist()
                g_tscode_concept[ts_code]={'id':conceptids, 'name':conceptnames}

def get_concept_map(pro):
    global g_ctcode_name
    if g_ctcode_name is None:
        init_ctcode_name(pro)
    return g_ctcode_name

def get_name(pro, code):
    global g_ctcode_name
    if g_ctcode_name is None:
        init_ctcode_name(pro)
    return g_ctcode_name[code]

def init_ctcode_name(pro):
    global g_ctcode_name
    if g_ctcode_name is None:
        g_ctcode_name = {}
        conceptdf=pro.concept(src='ts')
        conceptcodes=conceptdf['code'].values.tolist()
        conceptnames=conceptdf['name'].values.tolist()
        for i  in range(len(conceptcodes)):
            g_ctcode_name[conceptcodes[i]]= conceptnames[i]

if __name__== '__main__':
    pro = ts.pro_api('08aedc1cc54171e54a64bbe834ec1cb45026fa2ab39e9e4cb8208cad')
    init_ctcode_name(pro)
    print(g_ctcode_name)
    print(get_name(pro, 'TS2'))
    print(get_ctcode_by_tscode(pro, '600848.SH'))
    #conceptdf.to_csv('./concept.csv')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 18 21:28:29 2021

@author: apolloseeds
"""

from dataset import *

import matplotlib.pyplot as plt
import numpy as np
from scipy.io import loadmat
from sklearn import model_selection
from toolbox_02450 import train_neural_net, draw_neural_net, visualize_decision_boundary
import torch
from scipy import stats
from toolbox_02450 import feature_selector_lr, bmplot, rlr_validate, mcnemar
from toolbox_02450 import train_neural_net, draw_neural_net, visualize_decision_boundary

N2, M2 = contX.shape

def trainANN(X,y,h, K = 10): #returns the optimal h (number of hidden units)
    CV = model_selection.KFold(K,shuffle=True)
    n_replicates = 1        # number of networks trained in each k-fold
    max_iter = 10000

    # Define the model structure

    # The lambda-syntax defines an anonymous function, which is used here to
    # make it easy to make new networks within each cross validation fold
    model = lambda: torch.nn.Sequential(
                        torch.nn.Linear(M2, h), #M features to H hiden units
                        # 1st transfer function, either Tanh or ReLU:
                        torch.nn.Tanh(),                            #torch.nn.ReLU(),
                        torch.nn.Linear(h, 1) # H hidden units to 1 output neuron
                        )

    loss_fn = torch.nn.MSELoss() # notice how this is now a mean-squared-error loss

    print('Training model of type:\n\n{}\n'.format(str(model())))
    errors = [] # make a list for storing generalizaition error in each loop

    for (k, (train_index, test_index)) in enumerate(CV.split(X,y)):
        print('\nCrossvalidation fold: {0}/{1}'.format(k+1,K))

        # Extract training and test set for current CV fold, convert to tensors
        X_train = torch.Tensor(X[train_index,:])
        y_train = torch.Tensor(y[train_index])
        X_test = torch.Tensor(X[test_index,:])
        y_test = torch.Tensor(y[test_index])

        for i in range(0, len(h)):
            #Iterate over every h
            testedH = h[i]
            # Train the net on training data
            net, final_loss, learning_curve = train_neural_net(model,
                                                               loss_fn,
                                                               X=X_train,
                                                               y=y_train,
                                                               n_replicates=n_replicates,
                                                               max_iter=max_iter)

            print('\n\tBest loss: {}\n'.format(final_loss))

            # Determine estimated class labels for test set
            y_sigmoid = net(X_test)
            y_test_est = (y_sigmoid>.5).type(dtype=torch.uint8)

            # Determine errors and errors
            y_test = y_test.type(dtype=torch.uint8)

            e = y_test_est != y_test
            error_rate = (sum(e).type(torch.float)/len(y_test)).data.numpy()
            errors.append(error_rate) # store error rate for current CV fold

        optimalHIndex = errors.index(min(errors))
        optimalH = h[optimalHIndex]

        # Print the average classification error rate
        print('\nEstimated generalization error, RMSE: {0}'.format(round(np.sqrt(np.mean(errors)), 4)))

        return optimalH

def annRegression(X_train, X_test, y_train, y_test, hRange, K = 10):
    # Parameters for neural network classifier
    n_replicates = 1        # number of networks trained in each k-fold
    max_iter = 10000         # stop criterion 2 (max epochs in training)
    loss_fn = torch.nn.MSELoss() # notice how this is now a mean-squared-error loss

    opt_hidden_unit = trainANN(X_train, y_train, hRange, K)
    model = lambda: torch.nn.Sequential(
                        torch.nn.Linear(M, opt_hidden_unit), #M features to H hiden units
                        torch.nn.Tanh(),   # 1st transfer function,
                        torch.nn.Linear(opt_hidden_unit, 1), # H hidden units to 1 output neuron
                        )
    # print('Training model of type:\n\n{}\n'.format(str(model())))

    X_train = torch.Tensor(X_train)
    y_train = torch.Tensor(y_train)
    X_test = torch.Tensor(X_test)
    y_test = torch.Tensor(y_test)

    # Train the net on training data
    net, final_loss, learning_curve = train_neural_net(model,
                                                    loss_fn,
                                                    X=X_train,
                                                    y=y_train,
                                                    n_replicates=n_replicates,
                                                    max_iter=max_iter)

    print('\n\tBest loss: {}\n'.format(final_loss))

    # Determine estimated class labels for test set
    y_test_est = net(X_test)

    # Determine errors and errors
    se = (y_test_est.float()-y_test.float())**2 # squared error
    mse = (sum(se).type(torch.float)/len(y_test)).data.numpy() #mean
    return opt_hidden_unit, mse, y_test_est

C = 2
# Normalize data
annX = stats.zscore(contX)

# Parameters for neural network classifier
h = 1 # number of hidden units, !!!!SELECT A RANGE BY TESTING

serumC = np.array(np.asarray(X[:, 7]), dtype=int)
#y_rings = np.array(np.asarray(rings), dtype=np.int).reshape(-1, 1)

K = 5
lambdas = np.linspace(0.01, 10, 1000)
inner_cvf = 10
CV = model_selection.KFold(K, shuffle=True)
coefficient_norm = np.zeros(K)
# Parameters for neural network classifier
hRange = range(1, 8)
n_replicates = 2        # number of networks trained in each k-fold
max_iter = 10000         # stop criterion 2 (max epochs in training)

square_err_regression_base = np.empty(K)
square_err_regression_RLR = np.empty(K)
square_err_regression_ANN = np.empty(K)
regression_RLR_opt_lambdas = np.empty(K)
regression_opt_hidden_units = np.empty(K)

error_rate_classification_base = np.empty(K)
error_rate_classification_logistic = np.empty(K)
error_rate_classification_ANN = np.empty(K)
classification_opt_hidden_units = np.empty(K)
classification_opt_lambdas = np.empty(K)
w_est_logistic_arr = np.empty((K, X.shape[1]))

y_est_Reg_ANN = []
y_est_Reg_RLR = []
y_est_claf_ANN = []
y_est_claf_logistic = []
y_sex_real = []
y_rings_real = []

for k, (train_index, test_index) in enumerate(CV.split(annX,serumC)):
        X_train = annX[train_index,:]
        X_test = annX[test_index,:]
        y_train = serumC[train_index]
        y_test = serumC[test_index]
        """
        y_rings_train = y_rings[train_index]
        y_rings_test = y_rings[test_index]
        y_sex_real.append(y_sex_test)
        y_rings_real.append(y_rings_test)
        """

        regression_opt_hidden_unit, ANN_mse, y_est_ANN_regression =  annRegression(X_train, X_test, y_train, y_test, hRange, inner_cvf)
        regression_opt_hidden_units[k] = regression_opt_hidden_unit
        square_err_regression_ANN[k] = ANN_mse
        y_est_Reg_ANN.append(y_est_ANN_regression)

print("square_err_regression_ANN: ", square_err_regression_ANN)


import line
import cv2
import time
import serial

# Camera
vid = cv2.VideoCapture(0)

# Elegoo
power_forward = 100
power_sideway_minimal = 130
power_sideway_maximal = 200

compteur = 0
ips = 0
after = time.time() + 1

imprimer_taille_image = True

left_begin = 0
left_end = 85
right_begin = 95
right_end = 180

compteur_did_not_find_lines = 0

def power_engine_from_angle(begin, end, angle):
    diff = end - begin
    diff_angle_percentage = angle / diff
    power = power_sideway_minimal + ((power_sideway_maximal - power_sideway_minimal) * diff_angle_percentage)

    if power > 255:
        power = 255
    return int(power)

def send_command(left, right):
    try:
        cmd = str(left) + ',' + str(right) + ','
        arduino.write(cmd.encode())
        time.sleep(0.1)  # wait for arduino to answer

        arduino.flushOutput()
        arduino.flushInput()
    except Exception as ex:
        print(ex)

if __name__ == '__main__':
    with serial.Serial("/dev/ttyACM0", 9600, timeout=1) as arduino:
        time.sleep(0.1)  # wait for serial to open

        video = input("Voulez vous la vidéo ? Y or N ")
        if video == "Y":
            video = True
        else:
            video = False

        suivi = input("Voulez vous le suivi de commande ? Y or N ")
        if suivi == "Y":
            suivi = True
        else:
            suivi = False

        hist_size = input("Quelle taille d'historique voulez vous ?  > 0")
        angle_hist = line.Historique(hist_size=int(hist_size))

        if arduino.isOpen():
            print("{} connected!".format(arduino.port))

            # Detection de ligne
            while True:
                ret, original = vid.read()
                ips, compteur, after = line.caclulate_ips(ips, compteur, after)

                # si ips == 0 alors les ips ne sont pas affiché
                angle, size, img_line_plus_mean, did_not_find_lines = line.line_detection(hist=angle_hist, ips=ips,
                                                                                display_image=False,
                                                                                display_mean=video,
                                                                                original_picture=original)

                # print image size once
                if imprimer_taille_image:
                    print(size)
                    imprimer_taille_image = False

                # stop the program by pressing q
                if cv2.waitKey(1) & 0xFF == ord('q') & video:
                    break

                if did_not_find_lines:
                    compteur_did_not_find_lines += 1

                # Reaction to angle
                # Les moteur sont inversé
                # ENA, ENB
                if did_not_find_lines and compteur_did_not_find_lines > 10:
                    commande = "Backward"
                    send_command(10, 10)  # ceci est un code
                    power = power_forward
                    compteur_did_not_find_lines = 0
                elif left_end > angle >= left_begin:
                    commande = "left"
                    power = power_engine_from_angle(left_begin, left_end, angle)
                    send_command(power, 0)  # Le robot tourna a droite peu efficace
                elif right_end >= angle > right_begin:
                    commande = "right"
                    power = power_engine_from_angle(right_begin, right_end, angle)
                    send_command(0, power)  # Le robot toune a gauche tres efficace
                elif right_begin >= angle >= left_end:
                    commande = "Forward"
                    send_command(power_forward, power_forward)
                    power = power_forward

                if suivi:
                    print("Commande = " + commande + " " * (10-len(commande)) + "   Angle = " + str(angle) + " " * (10-len(str(angle))) + "   Power_engine = " + str(power))


# coding=utf-8
import mysql.connector
from mysql.connector import Error
import requests
import json
import datetime

dias_semana = ['Domingo', 'Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado']

try:
    # recupera dataset do chat
    url_json = "http://raw.githubusercontent.com/camilabianchi/graces_desafio/master/datasets/chatOnline.jsonl"
    req = requests.get(url_json)
    dicionario = json.loads(req.text)

    if len(dicionario) > 0:
        # abre conexao com o banco
        connection = mysql.connector.connect(host='localhost', port='3306', database='[db]', user='[user]',
                                             password='[pwd]')
        # percorre registros
        for item in dicionario:
            # data em formato string
            data_inicio_str = item["Data da conversa (Inicio)"].replace("/", "-")
            data_fim_str = item["Data da conversa (Fim)"].replace("/", "-")

            # calculo data final com base na duracao da chamada
            dt_inicio = datetime.datetime.strptime(data_inicio_str, '%d-%m-%Y %H:%M')
            dt_termino = datetime.datetime.strptime(data_fim_str, '%d-%m-%Y %H:%M')

            # valores do insert
            email = item["Visitor_Email"]
            nome = item["Visitor_Email"]
            agente = item["Agente"]
            status = "Atendido" if item["Atendido"] == "Sim" else "Não atendido"
            origem = 'Chat'
            semana = 0 if dt_inicio.weekday() == 6 else dt_inicio.weekday() + 1
            semana_nome = dias_semana[semana]

            if connection.is_connected():
                cursor = connection.cursor()
                sql_insert = """INSERT INTO contatos(email, nome, data_inicio, data_termino, agente, status, origem, semana, semana_nome)
                                        VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) """
                record = (email, nome, dt_inicio, dt_termino, agente, status, origem, semana, semana_nome)

                try:
                    cursor.execute(sql_insert, record)
                    connection.commit()
                except Error as e:
                    sql_insert = """INSERT INTO log_erros(log_mensagem) VALUES (%s) """
                    record = (e.msg.replace("'", ""),)
                    cursor.execute(sql_insert, record)
                    connection.commit()
                finally:
                    cursor.close()

        # fecha conexao com o banco
        if connection.is_connected():
            connection.close()
except Error as e:
    print("Error while connecting to MySQL", e.msg)


import math, random
import gym
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.autograd as autograd
import torch.nn.functional as F
import matplotlib.pyplot as plt

USE_CUDA = torch.cuda.is_available()
Variable = lambda *args, **kwargs: autograd.Variable(*args, **kwargs).cuda() if USE_CUDA else autograd.Variable(*args, **kwargs)

from collections import deque

env_id = "CartPole-v0"
env = gym.make(env_id)
# env = env.unwrapped

path = "/Users/saumya/Desktop/CriticalStates_results/"
results_dir = "vanillaDQN"

'''
Double DQN code adapted and modified from https://github.com/higgsfield/RL-Adventure/blob/master/2.double%20dqn.ipynb
'''

class ReplayBuffer(object):
    def __init__(self, capacity):
        self.buffer = deque(maxlen=capacity)

    def push(self, state, action, reward, next_state, done):
        state = np.expand_dims(state, 0)
        next_state = np.expand_dims(next_state, 0)

        self.buffer.append((state, action, reward, next_state, done))

    def sample(self, batch_size):
        state, action, reward, next_state, done = zip(*random.sample(self.buffer, batch_size))
        return np.concatenate(state), action, reward, np.concatenate(next_state), done

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

class DQN(nn.Module):
    def __init__(self, num_inputs, num_actions):
        super(DQN, self).__init__()

        self.layers = nn.Sequential(
            # nn.Linear(env.observation_space.shape[0], 128),
            # nn.ReLU(),
            # nn.Linear(128, 128),
            # nn.ReLU(),
            # nn.Linear(128, env.action_space.n)

            # Function approximator for Q function - modified to less hidden neurons
            nn.Linear(env.observation_space.shape[0], 32),
            nn.ReLU(),
            nn.Linear(32, 32),
            nn.ReLU(),
            nn.Linear(32, env.action_space.n)
        )

    def forward(self, x):
        return self.layers(x)

    def act(self, state, epsilon):
        """
        choose action using epsilon-greedy strategy
        """
        if random.random() > epsilon:
            state = Variable(torch.FloatTensor(state).unsqueeze(0), volatile=True)
            q_value = self.forward(state)
            action = q_value.max(1)[1].item()
        else:
            action = random.randrange(env.action_space.n)
        return action

def update_target(current_model, target_model):
    target_model.load_state_dict(current_model.state_dict())

def compute_td_loss(batch_size):
    """
    Compute the TD loss after sampling transitions(of size - "batch_size") from the replay buffer
    """
    state, action, reward, next_state, done = replay_buffer.sample(batch_size)

    state = Variable(torch.FloatTensor(np.float32(state)))
    next_state = Variable(torch.FloatTensor(np.float32(next_state)))
    action = Variable(torch.LongTensor(action))
    reward = Variable(torch.FloatTensor(reward))
    done = Variable(torch.FloatTensor(done))

    q_values = current_model(state)
    next_q_values = current_model(next_state)
    next_q_state_values = target_model(next_state)

    q_value = q_values.gather(1, action.unsqueeze(1)).squeeze(1)
    next_q_value = next_q_state_values.gather(1, torch.max(next_q_values, 1)[1].unsqueeze(1)).squeeze(1)
    expected_q_value = reward + gamma * next_q_value * (1 - done)

    loss = (q_value - Variable(expected_q_value.data)).pow(2).mean()

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    return loss

def plot(frame_idx, rewards, losses, iter):
    # clear_output(True)
    plt.figure(figsize=(20,5))
    plt.subplot(131)
    # plt.title('frame %s. reward: %s' % (frame_idx, np.mean(rewards[-10:])))
    plt.title('frame %s' % (frame_idx))
    plt.plot(rewards)
    plt.subplot(132)
    plt.title('loss')
    plt.plot(losses)
    plt.savefig(path+results_dir+"/cartpole_dqn_plots_iter_"+str(iter))

def load_model(model_path):
    current_model = DQN(env.observation_space.shape[0], env.action_space.n)
    current_model.load_state_dict(torch.load(model_path,map_location=torch.device('cpu')))

    return current_model

def play(model_path):
    """
    Play or rollout the learnt policy and observe the mean reward obtained over 1000 episodes
    """
    current_model = load_model(model_path)
    avg_test_reward = []
    for t in range(1000):
        # print('play: ',t)
        state = env.reset()
        done = False
        reward_per_episode = 0
        while not done:

            action = current_model.act(state, 0)
            next_state, reward, done, info = env.step(action)
            # env.render()
            reward_per_episode+=reward

            if done:
                # print('rewards: ',reward_per_episode)
                avg_test_reward.append(reward_per_episode)
                break
            else:
                state = next_state

    env.close()

    print(np.mean(avg_test_reward))

if __name__ == "__main__":
    ## Hyperparameters
    epsilon_start = 1.0
    epsilon_final = 0.01
    epsilon_decay = 500
    num_frames = 400000 # increased num of timesteps from 160000
    batch_size = 64
    gamma = 0.99
    update_target_net = 100
    learning_rate = 1e-4 # reduced learning rate from 1e-3

    epsilon_by_frame = lambda frame_idx: epsilon_final + (epsilon_start - epsilon_final) * math.exp(
        -1. * frame_idx / epsilon_decay)

    ## Running for 5 iteration to obtain a mean and std of the reward plots
    for iter in range(5):

        print("iteration: ",iter)

        current_model = DQN(env.observation_space.shape[0], env.action_space.n)
        target_model = DQN(env.observation_space.shape[0], env.action_space.n)

        if USE_CUDA:
            current_model = current_model.cuda()
            target_model = target_model.cuda()

        optimizer = optim.Adam(current_model.parameters(), lr = learning_rate)

        replay_buffer = ReplayBuffer(100000) # increased buffer size from 1000
        update_target(current_model, target_model)

        losses = []
        all_rewards = []
        episode_reward = 0
        ep_num = 0

        ## If the environment is solved is_win is set true
        is_win = False
        state = env.reset()
        for frame_idx in range(1, num_frames + 1):

            epsilon = epsilon_by_frame(frame_idx)
            action = current_model.act(state, epsilon)
            next_state, reward, done, _ = env.step(action)
            replay_buffer.push(state, action, reward, next_state, done)
            state = next_state
            episode_reward += reward

            if done:
                state = env.reset()
                all_rewards.append(episode_reward)
                episode_reward = 0
                ep_num+=1
                avg_reward = float(np.mean(all_rewards[-100:]))
                print('Best 100-episodes average reward', ep_num, avg_reward)

                ## Using the following "solving" criteria
                if len(all_rewards) >= 100 and avg_reward >= 198 and all_rewards[-1] > 198:
                    if not is_win:
                        is_win = True
                        torch.save(current_model.state_dict(), path+results_dir+'/CartPole_dqn_model_iter_'+str(iter))
                    print('Ran %d episodes best 100-episodes average reward is %3f. Solved after %d trials ✔' % (
                    ep_num, avg_reward, ep_num - 100))
                    last_saved = ep_num
                    torch.save(current_model.state_dict(),
                                path+results_dir+'/Final_CartPole_dqn_model_iter_' + str(
                                   iter))

            ## Update the loss
            if len(replay_buffer) > batch_size:
                loss = compute_td_loss(batch_size)
                losses.append(loss.item())

            if frame_idx % 200 == 0:
                plot(frame_idx, all_rewards, losses, iter)

            ## Update the target network
            if frame_idx % update_target_net == 0:
                update_target(current_model, target_model)

        ## Save the reward list - rewards obtained per episode
        np.save(path+results_dir+"/rewards_iter_"+str(iter),all_rewards)

        if not is_win:
            print('Did not solve after %d episodes' % ep_num)
            torch.save(current_model.state_dict(), path+results_dir+'/CartPole_dqn_model_iter_'+str(iter))

        # play(path+results_dir+'/CartPole_dqn_model_iter_'+str(iter))
        # play(path+results_dir+'/Final_CartPole_dqn_model_iter_' + str(iter))
        # Iteration: 0
        # 199.969
        # 200.0
        # iteration: 1
        # 200.0
        # 195.842
        # iteration: 2
        # 200.0
        # 182.442
        # iteration: 3
        # 200.0
        # 200.0
        # iteration: 4
        # 197.461
        # 199.972


import matplotlib.pyplot as plt
from tespy.networks import Network
from tespy.connections import Connection
from tespy.components import (Source, Sink, Condenser, Pump)

# Create a TESPy network
nw = Network(fluids=['water', 'NH3'])

# Add components and connections to the network
source = Source('source')
sink = Sink('sink')
condenser = Condenser('condenser')
pump = Pump('pump')

nw.add_conns(Connection(source, 'out1', condenser, 'in1'))
nw.add_conns(Connection(condenser, 'out1', sink, 'in1'))
nw.add_conns(Connection(condenser, 'out2', pump, 'in1'))
nw.add_conns(Connection(pump, 'out1', condenser, 'in2'))

# Solve the network
nw.solve('design')

# Extract the components and connections information
components = nw.components.keys()
connections = nw.connections.keys()

# Create a figure and axis
fig, ax = plt.subplots()

# Plot the components
for component in components:
    x = nw.components[component].x
    y = nw.components[component].y
    ax.scatter(x, y, label=component)

# Plot the connections
for connection in connections:
    x = [nw.connections[connection].inl.x, nw.connections[connection].outl.x]
    y = [nw.connections[connection].inl.y, nw.connections[connection].outl.y]
    ax.plot(x, y, '-', label=connection)

# Add labels and legend
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()

# Show the plot
plt.show()


import pandas as pd
from flask import Flask, jsonify, request,json
import pickle

model = pickle.load(open('model.pkl','rb'))

app = Flask(__name__)

@app.route('/', methods=['POST'])
def predict():
    # get data

    body_dict = json.loads(request.get_data().decode('utf-8'))
    data = body_dict['0']
    # predictions

    prediction=[]
    for v in data.values():
        p=model.predict([v]).tolist()
        #print(p)
        prediction.append(p[0])
#prediction = model.predict([data['0']]).tolist()
        #print(prediction)
    result = {'prediction': prediction}

    # return data
    return jsonify(prediction)

if __name__ == '__main__':
    app.run(port = 5000, debug=True)


import pygame
#зарускаем программу
pygame.init()
#add colors
black=(   0,   0,   0)
white=( 255, 255, 255)
green=(   0, 255,   0)
red=( 255,   0,   0)
size = [700,700]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Professor Craven's Cool Game")
done = True
clock=pygame.time.Clock()
screen.fill(white)
pygame.display.flip()
while done:
       for event in pygame.event.get():
               if event.type == pygame.QUIT:
                     done=False
       clock.tick(20)
pygame.quit()


import warnings

from cupy import testing
import cupyx.scipy.signal.windows as cu_windows

import pytest
from pytest import raises as assert_raises

try:
    import scipy.signal.windows as cpu_windows  # NOQA
    import scipy.fft  # NOQA
except ImportError:
    pass

window_funcs = [
    ('boxcar', ()),
    ('triang', ()),
    ('parzen', ()),
    ('bohman', ()),
    ('blackman', ()),
    ('nuttall', ()),
    ('blackmanharris', ()),
    ('flattop', ()),
    ('bartlett', ()),
    ('barthann', ()),
    ('hamming', ()),
    ('kaiser', (1,)),
    ('gaussian', (0.5,)),
    ('general_gaussian', (1.5, 2)),
    ('chebwin', (1,)),
    ('cosine', ()),
    ('hann', ()),
    ('exponential', ()),
    ('taylor', ()),
    ('tukey', (0.5,)),
]

class TestBartHann:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-15, atol=1e-15)
    def test_basic(self, xp, scp):
        w1 = scp.signal.windows.barthann(6, sym=True)
        w2 = scp.signal.windows.barthann(7)
        w3 = scp.signal.windows.barthann(6, False)
        return w1, w2, w3

class TestBartlett:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-15, atol=1e-15)
    def test_basic(self, xp, scp):
        w1 = scp.signal.windows.bartlett(6)
        w2 = scp.signal.windows.bartlett(7)
        w3 = scp.signal.windows.bartlett(6, False)
        return w1, w2, w3

class TestBlackman:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-15, atol=1e-15)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.blackman(6, sym=False),
                scp.signal.windows.blackman(7, sym=False),
                scp.signal.windows.blackman(6),
                scp.signal.windows.blackman(7, True))

class TestBlackmanHarris:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-15, atol=1e-15)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.blackmanharris(6, False),
                scp.signal.windows.blackmanharris(7, sym=False),
                scp.signal.windows.blackmanharris(6),
                scp.signal.windows.blackmanharris(7, sym=True))

class TestTaylor:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-15, atol=1e-15)
    def test_normalized(self, xp, scp):
        """Tests windows of small length that are normalized to 1. See the
        documentation for the Taylor window for more information on
        normalization.
        """
        w1 = scp.signal.windows.taylor(1, 2, 15)
        w2 = scp.signal.windows.taylor(6, 2, 15)
        return w1, w2

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-15, atol=1e-15)
    def test_non_normalized(self, xp, scp):
        """Test windows of small length that are not normalized to 1. See
        the documentation for the Taylor window for more information on
        normalization.
        """
        return (scp.signal.windows.taylor(5, 2, 15, norm=False),
                scp.signal.windows.taylor(6, 2, 15, norm=False))

    @testing.numpy_cupy_allclose(scipy_name='scp')
    def test_correctness(self, xp, scp):
        """This test ensures the correctness of the implemented Taylor
        Windowing function. A Taylor Window of 1024 points is created, its FFT
        is taken, and the Peak Sidelobe Level (PSLL) and 3dB and 18dB bandwidth
        are found and checked.

        A publication from Sandia National Laboratories was used as reference
        for the correctness values [1]_.

        References
        -----
        .. [1] Armin Doerry, "Catalog of Window Taper Functions for
               Sidelobe Control", 2017.
               https://www.researchgate.net/profile/Armin_Doerry/publication/316281181_Catalog_of_Window_Taper_Functions_for_Sidelobe_Control/links/58f92cb2a6fdccb121c9d54d/Catalog-of-Window-Taper-Functions-for-Sidelobe-Control.pdf
        """
        M_win = 1024
        N_fft = 131072
        # Set norm=False for correctness as the values obtained from the
        # scientific publication do not normalize the values. Normalizing
        # changes the sidelobe level from the desired value.
        w = scp.signal.windows.taylor(
            M_win, nbar=4, sll=35, norm=False, sym=False)
        f = scp.fft.fft(w, N_fft)
        spec = 20 * xp.log10(xp.abs(f / xp.amax(f)))

        first_zero = xp.argmax(xp.diff(spec) > 0)

        PSLL = xp.amax(spec[first_zero:-first_zero])

        BW_3dB = 2 * xp.argmax(spec <= -3.0102999566398121) / N_fft * M_win
        BW_18dB = 2 * xp.argmax(spec <= -18.061799739838872) / N_fft * M_win

        return PSLL, BW_3dB, BW_18dB

class TestBohman:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.bohman(6),
                scp.signal.windows.bohman(7, sym=True),
                scp.signal.windows.bohman(6, False))

class TestBoxcar:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.boxcar(6),
                scp.signal.windows.boxcar(7),
                scp.signal.windows.boxcar(6, False))

class TestChebWin:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        with warnings.catch_warnings():
            # sup.filter(UserWarning, "This window is not suitable")
            ret = (scp.signal.windows.chebwin(6, 100),
                   scp.signal.windows.chebwin(7, 100),
                   scp.signal.windows.chebwin(6, 10),
                   scp.signal.windows.chebwin(7, 10),
                   scp.signal.windows.chebwin(6, 10, False))
        return ret

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_cheb_odd_high_attenuation(self, xp, scp):
        with warnings.catch_warnings():
            # sup.filter(UserWarning, "This window is not suitable")
            cheb_odd = scp.signal.windows.chebwin(53, at=-40)
        return cheb_odd

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_cheb_even_high_attenuation(self, xp, scp):
        with warnings.catch_warnings():
            # sup.filter(UserWarning, "This window is not suitable")
            cheb_even = scp.signal.windows.chebwin(54, at=40)
        return cheb_even

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_cheb_odd_low_attenuation(self, xp, scp):
        with warnings.catch_warnings():
            # sup.filter(UserWarning, "This window is not suitable")
            cheb_odd = scp.signal.windows.chebwin(7, at=10)
        return cheb_odd

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_cheb_even_low_attenuation(self, xp, scp):
        with warnings.catch_warnings():
            # sup.filter(UserWarning, "This window is not suitable")
            cheb_even = scp.signal.windows.chebwin(8, at=-10)
        return cheb_even

exponential_data = {
    (4, None, 0.2, False): True,
    (4, None, 0.2, True): True,
    (4, None, 1.0, False): True,
    (4, None, 1.0, True): True,
    (4, 2, 0.2, False): True,
    (4, 2, 0.2, True): False,
    (4, 2, 1.0, False): True,
    (4, 2, 1.0, True): False,
    (5, None, 0.2, True): True,
    (5, None, 1.0, True): True,
    (5, 2, 0.2, True): False,
    (5, 2, 1.0, True): False
}

@testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
def test_exponential(xp, scp):
    for args, valid in exponential_data.items():
        if not valid:
            assert_raises(ValueError, scp.signal.windows.exponential, *args)
        else:
            win = scp.signal.windows.exponential(*args)
            return win

class TestFlatTop:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.flattop(6, sym=False),
                scp.signal.windows.flattop(7, sym=False),
                scp.signal.windows.flattop(6),
                scp.signal.windows.flattop(7, True),)

class TestGaussian:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.gaussian(6, 1.0),
                scp.signal.windows.gaussian(7, 1.2),
                scp.signal.windows.gaussian(7, 3),
                scp.signal.windows.gaussian(6, 3, False),)

class TestGeneralCosine:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.general_cosine(5, [0.5, 0.3, 0.2]),
                scp.signal.windows.general_cosine(4, [0.5, 0.3, 0.2],
                                                  sym=False),)

class TestGeneralHamming:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.general_hamming(5, 0.7),
                scp.signal.windows.general_hamming(5, 0.75, sym=False),
                scp.signal.windows.general_hamming(6, 0.75, sym=True),)

class TestHamming:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.hamming(6, False),
                scp.signal.windows.hamming(7, sym=False),
                scp.signal.windows.hamming(6),
                scp.signal.windows.hamming(7, sym=True),)

class TestHann:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.hann(6, sym=False),
                scp.signal.windows.hann(7, sym=False),
                scp.signal.windows.hann(6, True),
                scp.signal.windows.hann(7),)

class TestKaiser:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.kaiser(6, 0.5),
                scp.signal.windows.kaiser(7, 0.5),
                scp.signal.windows.kaiser(6, 2.7),
                scp.signal.windows.kaiser(7, 2.7),
                scp.signal.windows.kaiser(6, 2.7, False),)

@pytest.mark.skip('This has not been implemented yet in CuPy')
class TestKaiserBesselDerived:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        M = 100
        w = scp.signal.windows.kaiser_bessel_derived(M, beta=4.0)
        w2 = scp.signal.windows.get_window(
            ('kaiser bessel derived', 4.0), M, fftbins=False)
        w3 = scp.signal.windows.kaiser_bessel_derived(2, beta=xp.pi / 2)
        w4 = scp.signal.windows.kaiser_bessel_derived(4, beta=xp.pi / 2)
        w5 = scp.signal.windows.kaiser_bessel_derived(6, beta=xp.pi / 2)
        return w, w2, w3, w4, w5

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_exceptions(self, xp, scp):
        M = 100
        # Assert ValueError for odd window length
        msg = ("Kaiser-Bessel Derived windows are only defined for even "
               "number of points")
        with assert_raises(ValueError, match=msg):
            scp.signal.windows.kaiser_bessel_derived(M + 1, beta=4.)

        # Assert ValueError for non-symmetric setting
        msg = ("Kaiser-Bessel Derived windows are only defined for "
               "symmetric shapes")
        with assert_raises(ValueError, match=msg):
            scp.signal.windows.kaiser_bessel_derived(M + 1, beta=4., sym=False)

class TestNuttall:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.nuttall(6, sym=False),
                scp.signal.windows.nuttall(7, sym=False),
                scp.signal.windows.nuttall(6),
                scp.signal.windows.nuttall(7, True),)

class TestParzen:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.parzen(6),
                scp.signal.windows.parzen(7, sym=True),
                scp.signal.windows.parzen(6, False),)

class TestTriang:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        return (scp.signal.windows.triang(6, True),
                scp.signal.windows.triang(7),
                scp.signal.windows.triang(6, sym=False),)

tukey_data = [
    (4, 0.5, True),
    (4, 0.9, True),
    (4, 1.0, True),
    (4, 0.5, False),
    (4, 0.9, False),
    (4, 1.0, False),
    (5, 0.0, True),
    (5, 0.8, True),
    (5, 1.0, True),
    (6, 0),
    (7, 0),
    (6, .25),
    (7, .25),
    (6,),
    (7,),
    (6, .75),
    (7, .75),
    (6, 1),
    (7, 1),
]

class TestTukey:
    @pytest.mark.parametrize('args', tukey_data)
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, args, xp, scp):
        # Test against hardcoded data
        win = scp.signal.windows.tukey(*args)
        return win

dpss_data = [
    (4, 0.1, 2),
    (3, 1.4, 3),
    (5, 1.5, 5),
    (100, 2, 4),
]

@pytest.mark.skip('This has not been implemented yet in CuPy')
class TestDPSS:
    @pytest.mark.parametrize('args', tukey_data)
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, args, xp, scp):
        win, ratios = scp.signal.windows.dpss(*args, return_ratios=True)
        return win, ratios

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_unity(self, xp, scp):
        # Test unity value handling (gh-2221)
        results = []
        for M in range(1, 21):
            # corrected w/approximation (default)
            win = scp.signal.windows.dpss(M, M / 2.1)
            results.append(win)
            # corrected w/subsample delay (slower)
            win_sub = scp.signal.windows.dpss(M, M / 2.1, norm='subsample')
            if M > 2:
                # @M=2 the subsample doesn't do anything
                results.append(win_sub)
            # not the same, l2-norm
            win_2 = scp.signal.windows.dpss(M, M / 2.1, norm=2)
            results.append(win_2)
        return results

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_extremes(self, xp, scp):
        # Test extremes of alpha
        lam1 = scp.signal.windows.dpss(31, 6, 4, return_ratios=True)[1]
        lam2 = scp.signal.windows.dpss(31, 7, 4, return_ratios=True)[1]
        lam3 = scp.signal.windows.dpss(31, 8, 4, return_ratios=True)[1]
        return lam1, lam2, lam3

    @pytest.mark.parametrize('windows', [cu_windows, cpu_windows])
    def test_degenerate(self, windows):
        # Test failures
        assert_raises(ValueError, windows.dpss, 4, 1.5, -1)  # Bad Kmax
        assert_raises(ValueError, windows.dpss, 4, 1.5, -5)
        assert_raises(TypeError, windows.dpss, 4, 1.5, 1.1)
        assert_raises(ValueError, windows.dpss, 3, 1.5, 3)  # NW must be < N/2.
        assert_raises(ValueError, windows.dpss, 3, -1, 3)  # NW must be pos
        assert_raises(ValueError, windows.dpss, 3, 0, 3)
        assert_raises(ValueError, windows.dpss, -1, 1, 3)  # negative M

@pytest.mark.skip('This has not been implemented yet in CuPy')
class TestLanczos:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_basic(self, xp, scp):
        # Analytical results:
        # sinc(x) = sinc(-x)
        # sinc(pi) = 0, sinc(0) = 1
        # Hand computation on WolframAlpha:
        # sinc(2 pi / 3) = 0.413496672
        # sinc(pi / 3) = 0.826993343
        # sinc(3 pi / 5) = 0.504551152
        # sinc(pi / 5) = 0.935489284
        return (scp.signal.windows.lanczos(6, sym=False),
                scp.signal.windows.lanczos(6),
                scp.signal.windows.lanczos(7, sym=True),)

    @pytest.mark.parametrize('windows', [cu_windows, cpu_windows])
    def test_array_size(self, windows):
        for n in [0, 10, 11]:
            assert len(windows.lanczos(n, sym=False)) == n
            assert len(windows.lanczos(n, sym=True)) == n

class TestGetWindow:
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_boxcar(self, xp, scp):
        w1 = scp.signal.windows.get_window('boxcar', 12)

        # window is a tuple of len 1
        w2 = scp.signal.windows.get_window(('boxcar',), 16)
        return w1, w2

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_cheb_odd(self, xp, scp):
        with warnings.catch_warnings():
            # sup.filter(UserWarning, "This window is not suitable")
            w = scp.signal.windows.get_window(
                ('chebwin', -40), 53, fftbins=False)
        return w

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_cheb_even(self, xp, scp):
        with warnings.catch_warnings():
            # sup.filter(UserWarning, "This window is not suitable")
            w = scp.signal.windows.get_window(
                ('chebwin', 40), 54, fftbins=False)
        return w

    @pytest.mark.skip('This has not been implemented yet in CuPy')
    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_dpss(self, xp, scp):
        win1 = scp.signal.windows.get_window(('dpss', 3), 64, fftbins=False)
        win2 = scp.signal.windows.dpss(64, 3)
        return win1, win2

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_kaiser_float(self, xp, scp):
        win1 = scp.signal.windows.get_window(7.2, 64)
        win2 = scp.signal.windows.kaiser(64, 7.2, False)
        return win1, win2

    @pytest.mark.parametrize('windows', [cu_windows, cpu_windows])
    def test_invalid_inputs(self, windows):
        # Window is not a float, tuple, or string
        assert_raises(ValueError, windows.get_window, set('hann'), 8)

        # Unknown window type error
        assert_raises(ValueError, windows.get_window, 'broken', 4)

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_array_as_window(self, xp, scp):
        # scipy github issue 3603
        osfactor = 128
        sig = xp.arange(128)

        win = scp.signal.windows.get_window(('kaiser', 8.0), osfactor // 2)
        if hasattr(scp.signal, 'resample'):
            with assert_raises(ValueError, match='must have the same length'):
                scp.signal.resample(sig, len(sig) * osfactor, window=win)
        return win

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_general_cosine(self, xp, scp):
        return (scp.signal.get_window(('general_cosine', [0.5, 0.3, 0.2]), 4),
                scp.signal.get_window(('general_cosine', [0.5, 0.3, 0.2]), 4,
                                      fftbins=False))

    @testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
    def test_general_hamming(self, xp, scp):
        return (
            scp.signal.get_window(('general_hamming', 0.7), 5),
            scp.signal.get_window(('general_hamming', 0.7), 5, fftbins=False),)

    @pytest.mark.skip('This has not been implemented yet in CuPy')
    def test_lanczos(self, xp, scp):
        return (scp.signal.get_window('lanczos', 6),
                scp.signal.get_window('lanczos', 6, fftbins=False),
                scp.signal.get_window('lanczos', 6),
                scp.signal.get_window('sinc', 6))

@pytest.mark.parametrize('window_info', window_funcs)
@testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
def test_windowfunc_basics(window_info, xp, scp):
    window_name, params = window_info
    if window_name in {'parzen', 'tukey'}:
        pytest.skip()

    window = getattr(scp.signal.windows, window_name)
    results = []
    with warnings.catch_warnings():
        # Check symmetry for odd and even lengths
        w1 = window(8, *params, sym=True)
        w2 = window(7, *params, sym=False)
        results += [w1, w2]

        w1 = window(9, *params, sym=True)
        w2 = window(8, *params, sym=False)
        results += [w1, w2]

        # Check that functions run and output lengths are correct
        results.append(len(window(6, *params, sym=True)))
        results.append(len(window(6, *params, sym=False)))
        results.append(len(window(7, *params, sym=True)))
        results.append(len(window(7, *params, sym=False)))

        # Check invalid lengths
        assert_raises((ValueError, TypeError), window, 5.5, *params)
        assert_raises((ValueError, TypeError), window, -7, *params)

        # Check degenerate cases
        results.append(window(0, *params, sym=True))
        results.append(window(0, *params, sym=False))
        results.append(window(1, *params, sym=True))
        results.append(window(1, *params, sym=False))

        # Check normalization
        results.append(window(10, *params, sym=True))
        results.append(window(10, *params, sym=False))
        results.append(window(9, *params, sym=True))
        results.append(window(9, *params, sym=False))

        # Check that DFT-even spectrum is purely real for odd and even
        results.append(scp.fft.fft(window(10, *params, sym=False)).imag)
        results.append(scp.fft.fft(window(11, *params, sym=False)).imag)

    return results

@pytest.mark.parametrize('windows', [cu_windows, cpu_windows])
def test_needs_params(windows):
    for winstr in ['kaiser', 'ksr', 'kaiser_bessel_derived', 'kbd',
                   'gaussian', 'gauss', 'gss',
                   'general gaussian', 'general_gaussian',
                   'general gauss', 'general_gauss', 'ggs',
                   'dss', 'dpss', 'general cosine', 'general_cosine',
                   'chebwin', 'cheb', 'general hamming', 'general_hamming',
                   ]:
        assert_raises(ValueError, windows.get_window, winstr, 7)

@testing.numpy_cupy_allclose(scipy_name='scp', rtol=1e-13, atol=1e-13)
def test_not_needs_params(xp, scp):
    for winstr in ['barthann',
                   'bartlett',
                   'blackman',
                   'blackmanharris',
                   'bohman',
                   'boxcar',
                   'cosine',
                   'flattop',
                   'hamming',
                   'nuttall',
                   'parzen',
                   'taylor',
                   'exponential',
                   'poisson',
                   'tukey',
                   'tuk',
                   'triangle',
                   ]:
        win = scp.signal.get_window(winstr, 7)
        return win


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import pandas as pd
from selenium.webdriver.common.by import By
import re
from webdriver_manager.chrome import ChromeDriverManager
import json
import time
import os

products_categories = [
    't-shirts-tank-tops',
    'pants',
    'hoodies-sweatshirts',
    'shirts',
    'suits-blazers',
    'cardigans-sweaters',
    'jeans',
    'jackets-coats',
    'shorts',
    'swimwear',
    'sportswear',
    'underwear',
    'socks',
    'accessories',
    'shoes',
    'sleepwear-loungewear',
    'premium-selection',
    'cardigans-sweaters',
    'jackets-coats',
    'knitwear'
]

parent_link = 'https://www2.hm.com/en_us'
additional_link = '?sort=stock&image-size=small&image=model&offset=0&page-size={}'
gender_spec_url = 'men/products'
# get the category from the keys
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
# options.add_argument('--incognito')
# options.add_argument('--headless')
driver = webdriver.Chrome(options=options)

products_links = {}

for category in categories:
    products_links[category] = []
    print(category)
    # create url for this category
    if category in products_categories:
        cat_url = os.path.join(parent_link, gender_spec_url, category+'.html')
        print(cat_url)
        # open this url and get the count of number of items for this product
        driver.get(cat_url)
        time.sleep(0.2)
        # now get the total count of products present in this page
        product_count_element = driver.find_element(By.CLASS_NAME, "filter-pagination")
        product_count_element_text = product_count_element.text
        product_count_str = product_count_element_text.split(' ')[0]
        if product_count_str=='':
            continue
        total_count = int(product_count_str)
        print(total_count)

        all_products_url = cat_url+additional_link.format(total_count)
        driver.get(all_products_url)
        element_by_class = driver.find_element(By.CLASS_NAME, "products-listing")
        products_elements = element_by_class.find_elements(By.CLASS_NAME, "product-item")
        for pe in products_elements:
            single_product = driver.find_element(By.CLASS_NAME, "item-link.remove-loading-spinner")
            href = single_product.get_attribute("href")
            title = single_product.get_attribute('title')
            products_links[category].append([title,href])

f = open('product_links_men.json','w')
json.dump(products_links, f)


import pickle
import json
import yaml
import numpy as np
import torch
import torch.optim as optim
import time
from data_manager import DataManager
from model import BiLSTMCRF
from utils import f1_score, get_tags, format_result
from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter(log_dir='./tensorboard/bioes')

class ChineseNER(object):

    def __init__(self, entry="train"):
        self.load_config()
        self.__init_model(entry)

    def __init_model(self, entry):
        if entry == "train":
            self.train_manager = DataManager(batch_size=self.batch_size, tags=self.tags)
            self.total_size = len(self.train_manager.batch_data)
            data = {
                "batch_size": self.train_manager.batch_size,
                "input_size": self.train_manager.input_size,
                "vocab": self.train_manager.vocab,
                "tag_map": self.train_manager.tag_map,
            }
            self.save_params(data)
            self.dev_manager = DataManager(batch_size=60, data_type="dev")
            # 验证集
            # self.dev_batch = self.dev_manager.iteration()

            self.model = BiLSTMCRF(
                tag_map=self.train_manager.tag_map,
                batch_size=self.batch_size,
                vocab_size=len(self.train_manager.vocab),
                dropout=self.dropout,
                embedding_dim=self.embedding_size,
                hidden_dim=self.hidden_size,
            )
            self.model = self.model.cuda()
            self.restore_model()
        elif entry == "predict" or "evaluate":
            # python main.py predict
            data_map = self.load_params()
            input_size = data_map.get("input_size")
            self.tag_map = data_map.get("tag_map")
            self.vocab = data_map.get("vocab")
            print('input_size',input_size)
            print('tag_map',self.tag_map)
            self.model = BiLSTMCRF(
                tag_map=self.tag_map,
                vocab_size=input_size,
                embedding_dim=self.embedding_size,
                hidden_dim=self.hidden_size
            )
            self.model = self.model.cuda()
            self.test_manager = DataManager(batch_size=60, data_type="dev")
            self.restore_model()
    # 加载配置项
    def load_config(self):
        try:
            fopen = open("models/config.yml")
            config = yaml.load(fopen)
            fopen.close()
        except Exception as error:
            print("Load config failed, using default config {}".format(error))
            fopen = open("models/config.yml", "w")
            config = {
                "embedding_size": 300,
                "hidden_size": 128,
                "batch_size": 30,
                "dropout":0.5,
                "model_path": "models/",
                "tags": ["TREATMENT", "BODY","SIGNS","CHECK","DISEASE"]
            }
            yaml.dump(config, fopen)
            fopen.close()
        self.embedding_size = config.get("embedding_size")
        self.hidden_size = config.get("hidden_size")
        self.batch_size = config.get("batch_size")
        self.model_path = config.get("model_path")
        self.tags = config.get("tags")
        self.dropout = config.get("dropout")

    # 保存模型各种训练参数
    def restore_model(self):
        try:
            self.model.load_state_dict(torch.load(self.model_path + "params_6all.pkl"))
            print("model restore success!")
        except Exception as error:
            print("model restore faild! {}".format(error))

    # 保存模型超参数
    def save_params(self, data):
        with open("models/data_6all.pkl", "wb") as fopen:
            pickle.dump(data, fopen)

    # 加载模型超参数
    def load_params(self):
        with open("models/data_6all.pkl", "rb") as fopen:
            data_map = pickle.load(fopen)
        return data_map

    def train(self):
        optimizer = optim.Adam(self.model.parameters(),weight_decay=0.002,lr=0.0000004) # 0.000001
        # optimizer = optim.SGD(self.model.parameters(), lr=0.00000008,weight_decay=0.001,momentum=0.9) #4e-7
        scheduler_lr = optim.lr_scheduler.ReduceLROnPlateau(optimizer,mode='min',factor=0.5,patience=2,cooldown=5,verbose=True,min_lr=1e-8,eps=1e-8)
        best_loss = 240
        lossList = [0] * self.total_size
        for epoch in range(268,401):
            losses = []
            index = 0
            startTime = time.process_time()
            for batch in self.train_manager.get_batch():
                start = time.process_time()
                index += 1
                self.model.zero_grad()

                sentences, tags, length = zip(*batch)
                # lenght 是句子的原本长度
                # shape (batch_size,max.len(sentence) (20,332) batch_size 和 每个batch最长句子的长度
                sentences_tensor = torch.tensor(sentences, dtype=torch.long).cuda()
                tags_tensor = torch.tensor(tags, dtype=torch.long).cuda()
                length_tensor = torch.tensor(length, dtype=torch.long).cuda()

                loss = self.model.neg_log_likelihood(sentences_tensor, tags_tensor, length_tensor)
                losses.append(loss.cpu().item())
                progress = ("█"*int(index * 60 / self.total_size)).ljust(60)
                loss.backward()
                optimizer.step()
                # torch.save(self.model.state_dict(), self.model_path + 'params_6all.pkl')
                end = time.process_time()
                dur = end - start
                print("""epoch [{}] |{}| {}/{}\n\tloss {:.3f}\t\tlast_loss {:.3f}\t\ttime {}\t\tbest_avg_loss {:.3f}""".format(
                    epoch, progress, index, self.total_size, loss.cpu().tolist()[0],lossList[index - 1],str(dur),best_loss
                    )
                )
                lossList[index - 1] = loss.cpu().item()
                print("-" * 90)
            endTime = time.process_time()
            totalTime = endTime - startTime
            avg_loss = np.mean(losses)
            # 保存最好的模型
            if avg_loss < best_loss:
                best_loss = avg_loss
                torch.save(self.model.state_dict(), self.model_path + 'params_6all.pkl')
            writer.add_scalar('BiLstm_CRF:avg_loss-epoch', avg_loss, epoch)
            print('epoch ',epoch,'   avg_loss ', avg_loss,'   total_time ',totalTime)
            if epoch % 5 == 0:
                self.evaluate(epoch/5,manager=self.dev_manager)
            print("-"*100)
            scheduler_lr.step(avg_loss)
        writer.close()
    # train: BODY 7507, SIGNS 6355, CHECK 6965, DISEASE 474, TREATMENT 805
    # test:
    # 计算f1,评估模型
    def evaluate(self,epoch,manager,add_scalar = True):
        print('正在开始评估')
        all_origins = all_founds = all_rights = 0
        for tag in self.tags:
            origins = founds = rights = 0
            for batch in manager.get_batch():
                sentences, labels, length = zip(*batch)
                _, paths = self.model(sentences)
                origin, found, right = f1_score(labels, paths, tag, self.model.tag_map)
                origins += origin
                founds += found
                rights += right
            all_origins += origins
            all_founds += founds
            all_rights += rights
            recall = 0. if origins == 0 else (rights / origins)
            precision = 0. if founds == 0 else (rights / founds)
            f1 = 0. if recall + precision == 0 else (2 * precision * recall) / (precision + recall)
            print("\t{}\torigins:{}\t\t\tfounds:{}\t\t\trights:{}".format(tag, origins, founds, rights))
            print("\t\t\trecall:{}\tprecision:{}\tf1:{}".format(recall, precision, f1))
            if add_scalar:
                tag_epoch = tag + '-5epoch'
                writer.add_scalars(tag_epoch,{
                    'recall':recall,
                    'precision':precision,
                    'f1':f1
                }, epoch)
        all_recall = 0. if all_origins == 0 else (all_rights / all_origins)
        all_precision = 0. if all_founds == 0 else (all_rights / all_founds)
        all_f1 = 0. if all_recall + all_precision == 0 else (2 * all_precision * all_recall) / (all_precision + all_recall)
        print("\tall_origins:{}\t\t\tall_founds:{}\t\t\tall_rights:{}".format(all_origins, all_founds, all_rights))
        print("\tall_recall:{}\tall_precision:{}\tall_f1:{}".format(all_recall, all_precision, all_f1))
        if add_scalar:
            writer.add_scalars("ALL-5epoch", {
                'all_recall': all_recall,
                'all_precision': all_precision,
                'all_f1': all_f1
            }, epoch)
        print('评估结束')
        return all_recall, all_precision, all_f1
    # 预测方法
    def predict(self, input_str=""):
        if not input_str:
            input_str = input("请输入文本: ")
        # 获取输入句子所有汉字的在vocab的索引
        input_vec = [self.vocab.get(i, 0) for i in input_str]
        # convert to tensor
        sentences = torch.tensor(input_vec,dtype=torch.long).view(1, -1)
        sentences = sentences.cuda()
        # paths 预测出来的标签索引 shape 为 [1,1]
        _, paths = self.model(sentences)

        entities = []
        # "tags": ["ORG", "PER"]
        for tag in self.tags:
            tags = get_tags(paths[0], tag, self.tag_map)
            entities += format_result(tags, input_str, tag)
        print(entities)
        print(json.dumps(entities,indent=4,ensure_ascii=False))
        return entities

if __name__ == "__main__":
    entry = input('请输入train or predict or evaluate: ')
    while entry:
        if entry == 'train':
            cn = ChineseNER("train")
            cn.train()
            break
        elif entry == 'predict':
            cn = ChineseNER("predict")
            while True:
                inputText = input("请输入文本(q退出): ")
                if inputText != 'q':
                    cn.predict(inputText)
                else:
                    break
            break
        elif entry == 'evaluate':
            cn = ChineseNER("evaluate")
            cn.evaluate(epoch=0,manager=cn.test_manager,add_scalar=False)
            break
        else:
            print("请输入正确的命令(train or predict or evaluate)")
            entry = input('请输入train or predict or evaluate: ')