import os
import torch
import pandas as pd
import scanpy as sc
from sklearn import metrics
import multiprocessing as mp
from sklearn.metrics.cluster import adjusted_rand_score
from sklearn.metrics.cluster import (
    v_measure_score, homogeneity_score, completeness_score)
import glob2
import matplotlib.pyplot as plt
import squidpy as sq
import time
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages

def eval_model(pred, labels=None):
    if labels is not None:
        label_df = pd.DataFrame({"True": labels, "Pred": pred}).dropna()
        # label_df = pd.DataFrame({"True": labels, "Pred": pred}).dropna()
        completeness = completeness_score(label_df["True"], label_df["Pred"])
        hm = homogeneity_score(label_df["True"], label_df["Pred"])
        nmi = v_measure_score(label_df["True"], label_df["Pred"])
        ari = adjusted_rand_score(label_df["True"], label_df["Pred"])

    return completeness, hm, nmi, ari

def read_data(dataset, data_path='/home/sda1/fangzy/data/st_data/raw_data/'):
    if dataset == "STARmap":
        file_fold = data_path + str(dataset)
        adata = sc.read(file_fold+"/STARmap_20180505_BY3_1k.h5ad")
        adata.var_names_make_unique()
        adata.obs['ground_truth'] = adata.obs["label"]
        df_meta = pd.read_table(file_fold + '/Annotation_STARmap_20180505_BY3_1k.txt',
                                sep='\t', index_col=0)
        adata.obs['Annotation'] = df_meta.loc[adata.obs_names, 'Annotation'].values

    if dataset == "Mouse_brain":
        adata = sq.datasets.visium_hne_adata()
        adata.var_names_make_unique()
        adata.obs['ground_truth'] = adata.obs["cluster"]

    if dataset == "Breast_cancer":
        file_fold = data_path + str(dataset) #please replace 'file_fold' with the download path
        adata = sc.read_visium(file_fold, count_file='filtered_feature_bc_matrix.h5',
                               load_images=True)
        adata.var_names_make_unique()
        df_meta = pd.read_table(file_fold + '/metadata.tsv',sep='\t',
                                index_col=0)
        adata.obs['ground_truth'] = df_meta.loc[adata.obs_names, 'ground_truth'].values
    return adata

def run_GraphST(adata, dataset, random_seed = np.random.randint(100),
                device=torch.device('cuda:1' if torch.cuda.is_available() else 'cpu'),
                save_data_path="/home/sda1/fangzy/data/st_data/Benchmark/GraphST/"):
    from GraphST import GraphST
    from GraphST.utils import clustering

    start = time.time()
    # define model
    model = GraphST.GraphST(adata, device=device, random_seed=random_seed)
    # train model
    adata = model.train()
    n_clusters = len(set(adata.obs["ground_truth"].dropna()))
    # set radius to specify the number of neighbors considered during refinement
    radius = 50
    adata.obsm["embedding"] = adata.obsm["emb_pca"]
    clustering(adata, n_clusters, radius=radius, method="mclust",
               refinement=False)
    completeness, hm, nmi, ari = eval_model(adata.obs['domain'],
                                            adata.obs['ground_truth'])
    if dataset=="Breast_cancer":
        clustering(adata, n_clusters, radius=radius, method="mclust",
                   refinement=True)
        completeness_r, hm_r, nmi_r, ari_r = eval_model(adata.obs['domain'],
                                                        adata.obs['ground_truth'])
    else:
        completeness_r, hm_r, nmi_r, ari_r = completeness, hm, nmi, ari
    end = time.time()
    res = {}
    res["nmi"] = nmi
    res["ari"] = ari
    res["completeness"] = completeness
    res["hm"] = hm
    res["dataset"] = dataset
    res["time"] = end-start
    res["nmi_1"] = nmi_r
    res["ari_1"] = ari_r
    res["completeness_1"] = completeness_r
    res["hm_1"] = hm_r
    adata.obs["pred_label"] = adata.obs['domain']

    for metric in ["completeness", "hm", "nmi", "ari", "time",
                "completeness_1", "hm_1", "nmi_1", "ari_1"]:
        adata.uns[metric] = res[metric]
    # adata.write_h5ad(save_data_path+str(dataset)+".h5ad")
    return res, adata

save_data_path="/home/sda1/fangzy/data/st_data/Benchmark/GraphST/"
save_path = "/home/fangzy/project/st_cluster/code/compare/GraphST/res"
results = pd.DataFrame()
# "Breast_cancer", "Mouse_brain",
for dataset in ["Breast_cancer"]:
    adata = read_data(dataset)
    best_ari = 0
    for i in range(1):
        random_seed = 0
        res, adata_h5= run_GraphST(adata.copy(), dataset, random_seed=random_seed)
        res["round"] = i
        results = results.append(res, ignore_index=True)
        results.to_csv(save_path +
                    "/res_STARmap.csv", header=True)
        if res["ari_1"] > best_ari:
            adata_h5.write_h5ad(save_data_path+str(dataset)+".h5ad")
# res_dataset_mean = results.groupby(["dataset"]).mean()
# res_dataset_mean.to_csv(save_path+"/other_data_mean.csv", header=True)

# res_dataset_std = results.groupby(["dataset"]).std()
# res_dataset_mean.to_csv(save_path+"/other_data_std.csv", header=True)

# res_mean = res_dataset_mean.mean()
# res_mean.to_csv(save_path+"/other_mean.csv", header=True)
# res_mean = res_dataset_mean.median()
# res_mean.to_csv(save_path+"/other_median.csv", header=True)


"""
Based largely on code written by Eric Anderson:
https://github.com/anderson-optimization/ao-drawing-spec/blob/master/kml_to_drawing.py
"""

import logging
from pathlib import Path
from typing import Dict, Generator, List, Tuple, Union
import zipfile

from fastkml import kml, Folder, Placemark, LineStyle, PolyStyle, Document
import fastkml
import pandas as pd
import geopandas as gpd
import shapely.ops
from tqdm import tqdm

logger = logging.getLogger(__name__)

def _to_2d(x: float, y: float, z: float) -> tuple:
    return tuple(filter(None, [x, y]))

def loop_features(element: Union[kml.Document, kml.Placemark, kml.Folder],
                  folder_path: str) -> Generator:
    logger.debug(f'Iterating features in folder: {folder_path}')
    features = element.features()
    # for f in tqdm(features, desc='Iterating features'):
    for f in features:
        if isinstance(f, Folder):
            for item in loop_features(f, folder_path+f.name+'/'):
                yield item
        if isinstance(f, Placemark):
            yield {
                "f": f,
                "folder": folder_path
            }
        if isinstance(f, Document):
            for item in loop_features(f, folder_path+f.name+'/'):
                yield item

def get_style_obj(style: kml.Style) -> dict:
    # logger.debug('Havestyle', style)
    obj = {}
    if hasattr(style, 'styles'):
        for s in style.styles():
            try:
                if isinstance(s, LineStyle) and s.color is not None:
                    opacity = s.color[:2]
                    color = s.color[2:]
                    obj['strokeOpacity'] = opacity
                    obj['strokeColor'] = '#'+color.upper()
                    obj['strokeWeight'] = s.width
                if isinstance(s, PolyStyle):
                    opacity = s.color[:2]
                    color = s.color[2:]
                    obj['fillColor'] = '#'+color.upper()
                    obj['fillOpacity'] = opacity
            except TypeError as e:
                logger.error('TypeError getting style')
                logger.error(e)
    return obj

def read_kmz(kmz: str) -> kml.KML:
    """
    Reads data from a KML within a KMZ

    Parameters
    ----------
    kmz: str
        The KMZ or KML file to work with

    Returns
    -------
    kml.KML
    """
    f = zipfile.ZipFile(kmz)
    # Confirm only one KML exists
    files = list(f.namelist())
    kml_files = [f for f in files if f.endswith('.kml')]
    # TODO: Handle multiple KML files
    # Exit if more than one KML file found
    if len(kml_files) > 1:
        logger.error(f'Multiple KML files not supported: {kml_files}')
        raise Exception
    kml_file = kml_files[0]

    # Read KML data
    kml_data = f.open(kml_file).read()

    return kml_data

def read_kml(kml_file: str) -> kml.KML:
    # Read unzipped KML
    with open(kml_file, 'rb') as src:
        kml_data = src.read()
    if not kml_data:
        logger.error('No doc loaded')
        raise Exception

    return kml_data

def load_placemarks(kml_obj: kml.KML) -> Tuple[List[Dict], Dict]:
    # Load styles and features(?)
    styles = {}
    items = []
    for doc in kml_obj.features():
        logger.debug(f'Parsing KML doc: {doc.name}')
        for style in doc.styles():
            styles[style.id] = style

        new_items = loop_features(doc, '/')
        items += new_items

    return items, styles

def attributes_from_extended_data(extended_data: kml.ExtendedData) -> dict:
    attributes = {}
    for element in extended_data.elements:
        for d in element.data:
            attributes[d['name']] = d['value']
            if d['name'] in'Name':
                attributes['name'] = d['value']

    return attributes

def attributes_from_description(description: str) -> dict:
    """
    Parses attributes (key-value pairs) from an HTML table.

    Parameters
    ----------
    description: str
        String of HTML to parse

    Returns
    -------
    dict:
        Dictionary of key-value pairs
    """
    attributes = {}
    data = pd.read_html(description)
    if len(data) == 2:
    # try:
        table = data[1]
        for _idx, row in table.iterrows():
            key = row[0]
            value = row[1]
            if isinstance(value, float):
                continue
            if value:
                attributes[key] = value
    # except Exception as e:  # What exception(s) is being caught here?
    elif len(data) == 1:
        table = data[0]
        for _idx, row in table.iterrows():
            key = row[0]
            value = row[1]
            if key == value:
                continue
            if isinstance(value, float):
                continue
            if value:
                attributes[key] = value
    else:
        logger.error(f'Error reading table for placemark. Data: {data}')

    return attributes

def create_record(placemark: kml.Placemark) -> dict:
    geo = shapely.ops.transform(_to_2d, placemark.geometry)

    record = {
        "name": placemark.name,
        "wkt": geo.wkt,
        "geometry": geo
    }

    # Parse attributes from extended_data (SimpleField falls under this)
    if placemark.extended_data:
        attributes = attributes_from_extended_data(placemark.extended_data)
        record.update(attributes)

    # Parse attributes from description field
    if placemark.description:
        # Read html string into list of pd.DataFrame(s)
        attributes = attributes_from_description(placemark.description)
        record.update(attributes)

    return record

def capture_style(placemark: kml.Placemark, styles: dict) -> dict:
    style_url = placemark.styleUrl.replace('#', '')
    if style_url in styles.keys():
        # Style map, this should be handled better
        # logger.info(style_url)
        # if hasattr(style, 'normal'):
            # style_url = style.normal.url.replace('#', '')

        style = styles[style_url]
        style_obj = get_style_obj(style)
    else:
        style_obj = {}

    return style_obj

def kmz2gdf(kmz: str, parse_style: bool = False, include_folder_path: bool = False) -> gpd.GeoDataFrame:
    logger.info('Parsing KMZ...')

    # Load KML data
    if zipfile.is_zipfile(kmz):
        kml_data = read_kmz(kmz=kmz)
    else:
        kml_data = read_kml(kml_file=kmz)

    kml_obj = kml.KML()
    kml_obj.from_string(kml_data)

    placemarks, styles = load_placemarks(kml_obj)

    out_records = []
    for i, item in tqdm(enumerate(placemarks), desc='Processing KMZ features',
                        total=len(placemarks)):
        placemark = item['f']
        record = create_record(placemark)

        if parse_style:
            record['style'] = capture_style(placemark, styles)
        if include_folder_path:
            record['folder_path'] = item['folder']
        out_records.append(record)

    # Create GeoDataFrame
    # Assumes input KMZ is in 4326
    gdf = gpd.GeoDataFrame(out_records, geometry='geometry', crs='epsg:4326')

    return gdf

def split_gdf_on_folder_path(gdf: gpd.GeoDataFrame, folder_path_field: str='folder_path') -> List[gpd.GeoDataFrame]:
    new_gdfs = {}
    unique_fps = gdf[folder_path_field].unique()
    for uf in unique_fps:
        subset = gdf[gdf[folder_path_field]==uf]
        new_gdfs[Path(uf).stem] = subset
    return new_gdfs


# -*- coding: utf-8 -*-
import json
import numpy as np
import os
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from gensim.models import Word2Vec
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import optimizers
from tensorflow.keras import losses
from tensorflow.keras import metrics
from tensorflow.keras.models import load_model
from konlpy.tag import Okt

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def get_json(path):
    with open(path, 'r', encoding='utf-8') as file:
        json_dict = json.load(file)
    return json_dict

def get_sentences(json_dict):
    sentences = []
    for prod_list in json_dict['list']:
        for review_list in prod_list['review_list']:
            sentences.append(review_list['tf-idf'])

    return sentences

def tfidf_save(sentences):
    tfidf = TfidfVectorizer(min_df=1).fit(sentences)
    pickle.dump(tfidf, open('tfidf/tfidf.pickle', 'wb'))

def tfidf_load():
    tfidf = pickle.load(open('tfidf/tfidf.pickle', 'rb'))
    return tfidf

def get_vector(tfidf, sentences):
    return tfidf.transform(sentences).toarray()

def get_train_data(json_dict, tfidf, sentences):
    label_list = []
    for prod_list in json_dict['list']:
        for review_list in prod_list['review_list']:
            if int(review_list['grade']) < 4:
                label_list.append(0)
            else:
                label_list.append(1)

    train_list = get_vector(tfidf, sentences)

    train_list = np.asarray(train_list).astype('float32')
    label_list = np.asarray(label_list).astype('float32')

    return [train_list, label_list]

def start_train(train_list, label_list, split):
    model = models.Sequential()
    model.add(layers.Dense(64, activation='relu', input_shape=(6892,)))
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dense(1, activation='sigmoid'))

    model.compile(optimizer=optimizers.RMSprop(lr=0.001),
                  loss=losses.binary_crossentropy,
                  metrics=[metrics.binary_accuracy])

    if split == True:
        train_list, train_test, label_list, label_test = train_test_split(train_list, label_list, test_size=0.2, shuffle=True, random_state=1026)
        model.fit(train_list, label_list, epochs=10, batch_size=32)
        print(model.evaluate(train_test, label_test))
        return model

    model.fit(train_list, label_list, epochs=10, batch_size=32)
    return model

def predict_data(model, tfidf, sentence, tagging):
    pos_str = sentence
    if tagging:
        okt = Okt()
        pos = okt.pos(sentence, norm=True, stem=True)
        pos_str = ''
        for pos_elem in pos:
            pos_str += ' ' + pos_elem[0]

    test = tfidf.transform([pos_str]).toarray()

    data = np.expand_dims(np.asarray(test[0]).astype('float32'), axis=0)
    score = float(model.predict(data))

    return score

def model_save(model):
    model.save('tensorflow/review_model.h5')

def model_load():
    return load_model('tensorflow/review_model.h5')


import torchtext
from macros import *
from torchtext.data import Dataset
import torch
from torch import nn
from torch.nn import functional as F
from torch.nn.utils import clip_grad_norm
import numpy as np
import utils
from collections import defaultdict
from sklearn.metrics import accuracy_score, \
    precision_score, recall_score, f1_score
import json

class Example(object):

    def __init__(self, expr, ds, h):
        self.expr = self.tokenizer_expr(expr)
        self.ds = self.tokenizer_ds(ds)
        self.h = int(h)

    def tokenizer_expr(self, seq):
        return seq.split()

    def tokenizer_ds(self, ds):
        return [int(d) for d in ds.split()]

def load_examples(fname):
    examples = []

    with open(fname, 'r') as f:
        for line in f:
            expr, ds, h = \
                line.strip().split('\t')
            examples.append(Example(expr, ds, h))

    return examples

def build_iters(**param):

    ftrain = param['ftrain']
    fvalid = param['fvalid']
    if 'ftest' in param:
        ftest = param['ftest']

    if 'fanaly' in param:
        fanaly = param['fanaly']

    bsz = param['bsz']
    device = param['device']

    examples_train = load_examples(ftrain)

    EXPR = torchtext.data.Field(sequential=True, use_vocab=True,
                               pad_token=PAD,
                               unk_token=UNK,
                               eos_token=None)
    DS = torchtext.data.Field(sequential=True, use_vocab=False, pad_token=PAD_DS)
    H = torchtext.data.Field(sequential=False, use_vocab=False)

    train = Dataset(examples_train, fields=[('expr', EXPR),
                                            ('ds', DS),
                                            ('h', H)])
    EXPR.build_vocab(train)
    examples_valid = load_examples(fvalid)
    valid = Dataset(examples_valid, fields=[('expr', EXPR),
                                            ('ds', DS),
                                            ('h', H)])
    if 'ftest' in param:
        examples_test = load_examples(ftest)
        test = Dataset(examples_test, fields=[('expr', EXPR),
                                                ('ds', DS),
                                                ('h', H)])

    analy = None
    if 'fanaly' in param:
        examples_analy = load_examples(fanaly)
        analy = Dataset(examples_analy, fields=[('expr', EXPR),
                                                ('ds', DS),
                                                ('h', H)])

    train_iter = torchtext.data.Iterator(train, batch_size=bsz,
                                         sort=False, repeat=False,
                                         sort_key=lambda x: len(x.expr),
                                         sort_within_batch=True,
                                         device=device)
    valid_iter = torchtext.data.Iterator(valid, batch_size=bsz,
                                         sort=False, repeat=False,
                                         sort_key=lambda x: len(x.expr),
                                         sort_within_batch=True,
                                         device=device)
    analy_iter = None
    if 'fanaly' in param:
        analy_iter = torchtext.data.Iterator(analy, batch_size=bsz,
                                             sort=False, repeat=False,
                                             sort_key=lambda x: len(x.expr),
                                             sort_within_batch=True,
                                             device=device)
    test_iter = None
    if 'ftest' in param:
        test_iter = torchtext.data.Iterator(test, batch_size=bsz,
                                             sort=False, repeat=False,
                                             sort_key=lambda x: len(x.expr),
                                             sort_within_batch=True,
                                             device=device)

    return {'train_iter': train_iter,
            'valid_iter': valid_iter,
            'test_iter': test_iter,
            'analy_iter': analy_iter,
            'SEQ': EXPR,
            'DS': DS,
            'H': H}

def valid_detail(model, itos, valid_iter):

    def num_extrem_vals(ds):
        res = 0
        for i, d in enumerate(ds):
            if i == 0:
                if d > ds[1]:
                    res += 1
            elif i == len(ds) - 1:
                if d > ds[-2]:
                    res += 1
            elif d > ds[i - 1] and d > ds[i + 1]:
                res += 1

        return res

    nt = defaultdict(int)
    nc = defaultdict(int)
    acc = defaultdict(float)
    padding_idx = model.padding_idx
    incorrect_predicts = []

    with torch.no_grad():
        model.eval()
        for i, batch in enumerate(valid_iter):
            expr = batch.expr
            mask_expr = expr.ne(padding_idx)
            len_expr = mask_expr.sum(0)
            ds_tar = batch.ds
            h = batch.h

            out = model(expr)
            ds_pred = out['ds'].squeeze(-1)
            dstmask = (ds_tar > PAD_DS)
            len_ds = dstmask.sum(0)

            for e, d_pred, d_tar, l_e, l_d, h_b in zip(expr.transpose(0, 1),
                                                  ds_pred.transpose(0, 1),
                                                  ds_tar.transpose(0, 1),
                                                  len_expr, len_ds, h):
                ne = num_extrem_vals(d_tar[:l_d])

                h_b = h_b.item()
                e = list(e[:l_e].cpu().numpy())
                d_pred = list(d_pred[:l_d].cpu().numpy())
                d_tar = list(d_tar[:l_d].cpu().numpy())
                try:
                    tree_pred = utils.to_tree_sd(d_pred, e)
                except:
                    tree_pred = None
                tree_tar = utils.to_tree_sd(d_tar, e)
                if str(tree_pred) == str(tree_tar):
                    nc[h_b] += 1
                else:
                    expr = ' '.join([itos[ch.item()] for ch in e if itos[ch] != PAD])
                    ds_b = ' '.join([str(d) for d in d_tar if d != PAD_DS])
                    incorrect_predicts.append((expr, ds_b, str(h_b)))

                nt[h_b] += 1

    for h_b in nc.keys():
        acc[h_b] = nc[h_b]/nt[h_b]

    acc_total = sum([nc[h] for h in nc.keys()])/sum([nt[h] for h in nt.keys()])

    return acc, nt, incorrect_predicts, acc_total

def valid(model, valid_iter):
    nt = 0
    nc = 0
    padding_idx = model.padding_idx
    with torch.no_grad():
        model.eval()
        for i, batch in enumerate(valid_iter):
            expr = batch.expr
            mask_expr = expr.ne(padding_idx)
            len_expr = mask_expr.sum(0)
            ds_tar = batch.ds

            out = model(expr)
            ds_pred = out['ds'].squeeze(-1)
            dstmask = (ds_tar > PAD_DS)
            len_ds = dstmask.sum(0)

            for e, d_pred, d_tar, l_e, l_d in zip(expr.transpose(0, 1),
                                                  ds_pred.transpose(0, 1),
                                                  ds_tar.transpose(0, 1),
                                                  len_expr, len_ds):
                e = list(e[:l_e].cpu().numpy())
                d_pred = list(d_pred[:l_d].cpu().numpy())
                d_tar = list(d_tar[:l_d].cpu().numpy())
                try:
                    tree_pred = utils.to_tree_sd(d_pred, e)
                except:
                    tree_pred = None
                tree_tar = utils.to_tree_sd(d_tar, e)
                if str(tree_pred) == str(tree_tar):
                    nc += 1
                nt += 1

    return nc / nt

def test_analy(model, itos, analy_iter, enc):
    nt = 0
    nc = 0
    padding_idx = model.padding_idx
    fanalysis = getattr(model.encoder, 'f' + enc)

    with torch.no_grad():
        model.eval()
        for i, batch in enumerate(analy_iter):
            expr = batch.expr
            mask_expr = expr.ne(padding_idx)
            len_expr = mask_expr.sum(0)
            ds_tar = batch.ds

            out = model(expr)
            ds_pred = out['ds'].squeeze(-1)
            dstmask = (ds_tar > PAD_DS)
            len_ds = dstmask.sum(0)

            for e, d_pred, d_tar, l_e, l_d in zip(expr.transpose(0, 1),
                                                  ds_pred.transpose(0, 1),
                                                  ds_tar.transpose(0, 1),
                                                  len_expr, len_ds):
                e = list(e[:l_e].cpu().numpy())
                d_pred = list(d_pred[:l_d].cpu().numpy())
                d_tar = list(d_tar[:l_d].cpu().numpy())
                try:
                    tree_pred = utils.to_tree_sd(d_pred, e)
                except:
                    tree_pred = None
                tree_tar = utils.to_tree_sd(d_tar, e)
                if str(tree_pred) == str(tree_tar):
                    nc += 1
                nt += 1

                is_correct = 1 if str(tree_pred) == str(tree_tar) else 0
                expr = [itos[ch.item()] for ch in expr[:, 0]]
                line = {'type': 'input',
                        'idx': i,
                        'expr': expr,
                        'is_correct': is_correct}
                line = json.dumps(line)
                print(line)
                print(line, file=fanalysis)

    return nc / nt

def rankloss(input, target, mask, exp=False):
    # input: (seq_len, bsz)
    # target: (seq_len, bsz)
    # mask: (seq_len, bsz)
    input = input.transpose(0, 1)
    target = target.transpose(0, 1)
    mask = mask.transpose(0, 1)
    diff = input[:, :, None] - input[:, None, :]
    target_diff = ((target[:, :, None] - target[:, None, :]) > 0).float()
    mask = mask[:, :, None] * mask[:, None, :] * target_diff

    if exp:
        loss = torch.exp(F.relu(target_diff - diff)) - 1
    else:
        loss = F.relu(target_diff - diff)
    loss = (loss * mask).sum() / (mask.sum() + 1e-9)

    return loss

def train(model, iters, opt, optim, scheduler):
    train_iter = iters['train_iter']
    valid_iter = iters['valid_iter']

    basename = "{}-{}-{}-{}".format(opt.task,
                                       opt.sub_task,
                                       opt.enc_type,
                                       utils.time_int())
    log_fname = basename + ".json"
    log_path = os.path.join(RES, log_fname)
    with open(log_path, 'w') as f:
        f.write(str(utils.param_str(opt)) + '\n')
    # print(valid(model, valid_iter))

    best_performance = 0
    losses = []
    gnorms = []
    for epoch in range(opt.nepoch):
        for i, batch in enumerate(train_iter):
            expr = batch.expr
            ds_tar = batch.ds

            model.train()
            model.zero_grad()
            out = model(expr)
            ds_pred = out['ds'].squeeze(-1)
            dstmask = (ds_tar > PAD_DS).float()

            loss = rankloss(ds_pred, ds_tar, dstmask)
            losses.append(loss.item())

            loss.backward()
            gnorm = clip_grad_norm(model.parameters(), opt.gclip)
            gnorms.append(gnorm)

            optim.step()
            loss = {'loss': loss.item(), 'gnorm': gnorm}

            utils.progress_bar(i / len(train_iter), loss, epoch)

            if (i + 1) % int(1 / 4 * len(train_iter)) == 0:
                # print('\r')
                loss_ave = np.array(losses).sum() / len(losses)
                gnorm_ave = np.array(gnorms).sum() / len(gnorms)
                losses = []
                gnorms = []
                accurracy = \
                    valid(model, valid_iter)
                log_str = '{\'Epoch\':%d, \'Format\':\'a/l/g\', \'Metrics\':[%.4f, %.4f, %.4f]}' % \
                          (epoch, accurracy, loss_ave, gnorm_ave)
                print(log_str)
                with open(log_path, 'a+') as f:
                    f.write(log_str + '\n')

                scheduler.step(loss_ave)
                for param_group in optim.param_groups:
                    print('learning rate:', param_group['lr'])

                if accurracy > best_performance:
                    best_performance = accurracy
                    model_fname = basename + ".model"
                    save_path = os.path.join(RES, model_fname)
                    print('Saving to ' + save_path)
                    torch.save(model.state_dict(), save_path)

class Model(nn.Module):

    def __init__(self, encoder, embedding, drop):
        super(Model, self).__init__()
        self.encoder = encoder
        self.embedding = embedding
        self.hdim = self.encoder.odim
        self.padding_idx = embedding.padding_idx
        self.num_words = embedding.num_embeddings
        self.edrop = nn.Dropout(drop)
        self.conv1d = nn.Sequential(nn.Dropout(drop),
                                 nn.Conv1d(self.hdim, self.hdim, 2),
                                 nn.ReLU())
        self.to_ds = nn.Sequential(nn.Dropout(drop),
                                 nn.Linear(self.hdim, self.hdim),
                                 nn.ReLU(),
                                 nn.Dropout(drop),
                                 nn.Linear(self.hdim, 1))

    def enc(self, seq):
        mask = seq.data.eq(self.padding_idx)
        len_total, bsz = seq.shape
        lens = len_total - mask.sum(dim=0)

        # inp = self.embedding_drop(True, seq)
        inp = self.embedding(seq)
        os = self.encoder(embs=inp, mask=1-mask, lens = lens)
        # rep = os[lens - 1, range(bsz)]
        return os

    def forward(self, seq):
        os = self.enc(seq)
        os = os.permute(1, 2, 0) # (bsz, hdim, seq_len)
        conv_os = self.conv1d(os)
        conv_os = conv_os.permute(2, 0, 1) # (seq_len, bsz, hdim)
        ds = self.to_ds(conv_os)

        return {'ds': ds}


class Solution5:
    def expand(self, s, left, right):
        while left >= 0 and right < len(s) and s[left] == s[right]:
            left -= 1
            right += 1
        return left+1, right-1

    def longestPalindrome(self, s: str) -> str:
        start, end = 0, 0
        for i in range(len(s)):
            l1, r1 = self.expand(s, i, i)
            l2, r2 = self.expand(s, i, i + 1)
            if r1 - l1 > end - start:
                start, end = l1, r1
            if r2 - l2 > end - start:
                start, end = l2, r2
        return s[start:end + 1]


# -*- coding:utf-8 -*-
# 蒙特卡洛+自适应迭代
# 不懂想法是不是对的，不过我的想法是，上一次的迭代结果和这一次的迭代结果之间，进行差值比较，如果差值小于设定精度，就停止迭代,并输出结果,第一版应进行不画图只计算
# f(x)=x^2+cos(x),
# 积分上下限:a=-3,b=2
import numpy as np
import matplotlib.pyplot as plt
import docx

cal_e=1e-5  #设定精度
cal_n=40    #初次迭代点数

def f(x):   #被积函数
    return x**2+np.cos(x+2)

def ff(x):  #积分上下限归一化,积分结果*area-1再*5
    b=3
    a=-2
    xx=(b-a)*x+a
    return f(xx)+1

def MonteCarlo(n):  #蒙特卡洛计算单次迭代,n为点数
    x=np.arange(0,1,1/n)
    y=ff(x)
    x_list = np.random.random((1, n)) * 1
    y_list = np.random.random((1, n)) * 11
    yy_list = ff(x_list)
    list = (yy_list-y_list)
    num = 0
    # print(len(x))
    for i in range(len(x)):
        if list[0, i]>0:
            num +=1
    return num/n

def createTable(doc,table):

    hdr_cells = table.rows[0].cells
    hdr_cells[0].text = '迭代次数'
    hdr_cells[1].text = '迭代点数'
    hdr_cells[2].text = '迭代结果'
    hdr_cells[3].text = '积分结果'
    return doc,table

def add_Data_to_Table(doc,table,data):
    cal=20
    for i in range(data.size):
        cal*=2
        row_cells = table.add_row().cells
        row_cells[0].text = str(i+1)
        row_cells[1].text = str(cal)
        row_cells[2].text = str(data[i])
        row_cells[3].text = str((data[i]*11-1)*5)
    doc.save('MonteCarlo.docx')
    return doc,table

def plot_convergence(M_equal):
    x=np.arange(0,M_equal.size,1)
    plt.plot(x,M_equal,'r')
    plt.savefig('./test2.jpg')
    plt.show()

if __name__ == '__main__':
    M_equal=np.array([])
    N=cal_n #迭代点数
    M1=MonteCarlo(N)
    M2=MonteCarlo(N)
    M_equ=(M1+M2)/2
    M_equal=np.append(M_equal,M_equ)
    n=1
    print("初次迭代结果为：",M_equ)
    if abs(M1-M2)<cal_e:
        print("精度满足，迭代结束，结果为：",M_equ)
    while True:
        N*=2
        n+=1
        M1=MonteCarlo(N)
        M_equ=(M_equ+M1)/2
        M_equal=np.append(M_equal,M_equ)
        print("迭代次数为：",n,"迭代结果为：",M_equ)
        if abs(M1-M_equ)<cal_e:
            print("精度满足，迭代结束")
            break
    print("最终结果为：",M_equ)
    doc = docx.Document()
    table = doc.add_table(rows=1, cols=4,style="Table Grid")
    createTable(doc,table)
    add_Data_to_Table(doc,table,M_equal)
    plot_convergence(M_equal)


# Задача 32: Определить индексы элементов массива (списка),
# значения которых принадлежат заданному диапазону
# (т.е. не меньше заданного минимума и не больше заданного максимума)

import subprocess
import os
import random

subprocess.call('clear' if os.name =='posix' else 'cls', shell=True)    # Очистка консоли

n = int(input("Введи количество элементов массива: "))

if n < 1:
    print()
    print("Необходимо ввести число больше нуля!")
    print
else:
    Ai = [] # Создание пустого списка
    otvet = []

    s = 5  # начало диапазона
    f = 9  # конец диапазона

    for i in range(n):
        Ai.append(int(random.random() * 10))

    for i in range(n):
        if Ai[i] > s and Ai[i] < f:
            otvet.append(i)

    print(Ai)
    print(otvet)

Урок_6/Задача 32.py

Задача 32.py


n, m = map(int, input().split())
if n == 0:
    print(0)
    exit(0)
elif n == 1:
    print(1)
    exit(0)
d = 1000000007

def mat_cross(mat1, mat2):
    result = [[0]*2 for _ in range(2)]
    for i in range(2):
        for j in range(2):
            result[i][j] = (mat1[i][0]*mat2[0][j] + mat1[i]
                            [1]*mat2[1][j]) % d
    return result

def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)

def mat_pow(mat, N):
    if N == 1:
        return mat
    result = [[1, 0], [0, 1]]
    while N:
        if N & 1:
            result = mat_cross(result, mat)
        mat = mat_cross(mat, mat)
        N >>= 1
    return result

n = gcd(n, m)

mat = mat_pow([[1, 1], [1, 0]], n-1)
print((mat[0][0]) % d)


# создание искусственных точек

from random import randint, random

from base.const import *
from base.logic import inside, dist, rand
from base.point import Point
from data.position import Position

from fake.graph import graph
from base.mongo import position_collection

# ---------------------------

class Points:
    p = []  # точки

    a, b = 10, 100  # минимальная и максимальная длина траектории
    v = [STOP, WALK, BIKE, BUS, CAR]  # скорости перемещения (стоит, пешком, велосипед, машина)

    r = 50  # максимальная ошибка получения координат
    d = 0.01  # вероятность выброса и смены способа перемещения

    m = 10  # максимальное количество треков у человека
    t = 1000000  # ограничение по времени наблюдения

    k = 3  # количество точек притяжения
    n = 10  # сколько времени человек топчется

    # ---------------------------
    # создание случайных треков

    def make(self, k):
        for i in range(k):
            self.find(i)
        self.p.sort(key=lambda q: q.t)

        self.save()

    # ---------------------------
    # создание трека

    def track(self, t, i, q):

        # ---------------------------
        # 1. топчемся в точке притяжения

        d = 10

        for n in range(self.n):
            r = randint(1, self.r) + (random() < self.d) * self.r
            self.p.append(Position(i, q.x, q.y, t, r))

            q.x += (2 * random() - 1) * self.v[0] * d / LAT
            q.y += (2 * random() - 1) * self.v[0] * d / LON

            t += d

        # ---------------------------
        # 2. выбираем направление

        r = randint(1, self.r) + (random() < self.d) * self.r
        a = Position(i, q.x, q.y, t, r)

        s, f = graph.rand(a.s)
        v = self.v[f]

        q.prob(s)

        r = randint(1, self.r) + (random() < self.d) * self.r
        t += dist(a, q) / v

        b = Position(i, q.x, q.y, t, r)

        a.f = b.f = f
        self.p += [a, b]

        # ---------------------------
        # 3. движемся

        n = randint(self.a, self.b)

        for k in range(n):
            s = graph.prob(a.s, b.s, f)
            q.prob(s)

            r = randint(1, self.r) + (random() < self.d) * self.r
            t += dist(a, q) / v

            a, b = b, Position(i, q.x, q.y, t, r)

            b.f = f
            self.p.append(b)

            if random() < self.d:
                s, f = graph.rand(b.s)
                v = self.v[f]

                q.prob(s)

                r = randint(1, self.r) + (random() < self.d) * self.r
                t += dist(a, q) / v

                a, b = b, Position(i, q.x, q.y, t, r)

                b.f = f
                self.p.append(b)
        return t

    # ---------------------------
    # создание треков человека

    def find(self, i):
        t = NOW + randint(0, self.t)  # время первого трека
        m = randint(1, self.m)  # количество треков

        d = self.t // m  # время между треками
        self.p.clear()

        s = [Point(0, 0) for k in range(self.k)]  # точки притяжения
        for q in s: q.rand()

        for k in range(m):
            t = self.track(t, i, rand(s)) + d

    # ---------------------------
    # сохранение в базу

    def save(self):
        position_collection.drop()
        p = [{
            'i': q.i,
            'x': q.x,
            'y': q.y,
            't': q.t,
            'r': q.r
        } for q in self.p]
        position_collection.insert_many(p)

    # ---------------------------
    # создаем поток данных и применяем к точкам обработчик

    def load(self, f):
        for d in position_collection.get():
            i = int(d['i'])
            x = float(d['x'])
            y = float(d['y'])
            r = float(d['r'])
            t = int(d['t'])

            q = Position(i, x, y, t, r)
            if inside(q): f(q)


from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Arial", 16, "normal")

class Scoreboard(Turtle):
    # Create initializer for Scoreboard class
    def __init__(self, position):
        # We want the Scoreboard class to inherit attributes from super Turtle class
        super().__init__()
        self.goto(position)
        # Utilize write to put scoreboard up
        self.score = 0
        self.color("white")
        self.penup()
        self.write(f"Score: {self.score}", False, align=ALIGNMENT, font=FONT)
        self.hideturtle()
        self.update_scoreboard()

    # Method to update the scoreboard
    def update_scoreboard(self):
        self.write(f"Score: {self.score}", False, align=ALIGNMENT, font=FONT)

    # Method to increase score on scoreboard
    def increase_score(self):
        self.score += 1
        self.clear()
        self.update_scoreboard()


from PIL import Image
import torch
import cv2
import numpy as np
from imutils import face_utils
import io

def eyebrow_roi(detector, predictor, img_path):
    if isinstance(img_path, str):
        frame = cv2.imread(img_path)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    else:
        frame = Image.open(io.BytesIO(img_path)).convert('RGB')
        frame = np.array(frame)
        gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

    frame_copy = frame.copy()
    # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    rect = detector(gray)[0]
    shape = predictor(gray,rect)
    point_array = face_utils.shape_to_np(shape)
    x_max, x_min, y_max, y_min = max(point_array[:,0]), min(point_array[:,0]), max(point_array[:,1]), min(point_array[:,1])
    WIDTH = abs(x_max - x_min)
    HIGHT = abs(y_max - y_min)
    a = WIDTH//38
    b = HIGHT//38
    (x, y, w, h) = cv2.boundingRect(np.array([point_array[17:22]]))
    roi_eyebrow = frame_copy[y-b:y+h+b, x-a:x+w+a]
    roi_eyebrow = cv2.cvtColor(roi_eyebrow, cv2.COLOR_BGR2RGB)

    return roi_eyebrow

def eyebrow_get_max_feature(model, trans_fn, img_, ref_batch, cats):

    img = Image.fromarray(img_).convert('L')
    img_t = trans_fn(img).unsqueeze(0)
    test_batch = torch.cat([img_t] * 10, dim=0).to('cuda:1')
    with torch.no_grad():
        output = model(test_batch, ref_batch).to('cuda:1')

    catagory = cats[output.argmax(0)]

    # Name Editing
    cat_map = {
        'roundLowArch': 'RoundLowArch',
        'hard' : 'SoftAngledHighArch',
        'flat' : 'Flat',
        'softLowArch' : 'SoftAngledLowArch',
        'softHighArch' : 'SoftAngledHighArch',
        'roundMidArch' : 'RoundLowArch',
        'sShape': 'SShaped',
        'roundHighArch' : 'RoundHighArch',
        'straight': 'Straight',
        'softMidArch': 'SoftAngledLowArch',
    }

    return cat_map[catagory]


from flask import Flask, jsonify
import socket

from square import Square

app = Flask(__name__)

@app.route('/')
def health():
    return jsonify({"status":"true"})

@app.route('/greeting')
def greeting():
    hostname = socket.gethostname()
    return 'Hello World from ' + hostname

@app.route('/square/<int:x>')
def square(x=0):
    result = Square(x, x**2).toJson()
    return result

if __name__ == '__main__':
    app.run(debug=True, port=80,host='0.0.0.0')


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn')

from Status import *
from BackTest import BackTest

def plot_rsi_ema(stock, plt_df, plot_full_location, cur_descp, period_rsi,
                 rsi_low, rsi_high, period_short, period_long, column):
    plt_df = plt_df.set_index(pd.DatetimeIndex(plt_df['Date'].values))

    ax = ('ax1', 'ax2')
    fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True, gridspec_kw={'height_ratios': [1, 1]})

    #Plotting RSI and EMA Charts.
    ax[0].set_title(cur_descp)
    ax[0].plot(plt_df.index, plt_df['RSI'])
    ax[0].axhline(0, linestyle='--', alpha = 0.5, color='gray')
    ax[0].axhline(10, linestyle='--', alpha = 0.5, color='orange')
    ax[0].axhline(20, linestyle='--', alpha = 0.5, color='green')
    ax[0].axhline(30, linestyle='--', alpha = 0.5, color='red')
    ax[0].axhline(70, linestyle='--', alpha = 0.5, color='red')
    ax[0].axhline(80, linestyle='--', alpha = 0.5, color='green')
    ax[0].axhline(90, linestyle='--', alpha = 0.5, color='red')
    ax[0].axhline(100, linestyle='--', alpha = 0.5, color='gray')
    ax[1].set_ylabel('RSI')

    ax[1].plot(plt_df.index, plt_df[column], label=column)
    ax[1].plot(plt_df.index, plt_df['EMA{}'.format(period_short)], label='EMA{}'.format(period_short))
    ax[1].plot(plt_df.index, plt_df['EMA{}'.format(period_long)], label='EMA{}'.format(period_long))
    ax[1].set_xlabel('{} to {}'.format(plt_df['Date'][0], plt_df['Date'].iloc[-1]))
    ax[1].legend(title="{} - EMA {} & {}".format(stock, period_short, period_long), fontsize='small', fancybox=True)

    for a in ax:
        a.patch.set_edgecolor('black')
        a.patch.set_linewidth('1')
        a.yaxis.set_label_position("right")
        a.yaxis.tick_right()

    plt.subplots_adjust(wspace=0, hspace=0)
    plot_name = '{}_RSI_EMA.png'.format(stock)
    plot_file = plot_full_location + '/' + stock + '/' + plot_name
    plt.savefig(plot_file, dpi=150)
    rsi_ema_hlink = '=HYPERLINK("{}","RSI_EMA Plot")'.format(plot_file)
    plt.close('all')

    #Plot the Buy and Sell Prices.
    plt.figure(figsize=(12.2, 4.5))
    plt.plot(plt_df[column], label=column, alpha=0.35)
    if not plt_df['Buy_Signal_Price'].isnull().all():
        plt.scatter(plt_df.index, plt_df['Buy_Signal_Price'], label='Buy_Signal', marker='^', alpha=1, color='green')
    if not plt_df['Sell_Signal_Price'].isnull().all():
        plt.scatter(plt_df.index, plt_df['Sell_Signal_Price'], label='Sell_Signal', marker='v', alpha=1, color='red')
    plt.title('Close Price Buy & Sell Signals ({} - {})'.format(stock, cur_descp))
    plt.xlabel('{} to {}'.format(plt_df['Date'][0], plt_df['Date'].iloc[-1]))
    plt.ylabel('Close Price in INR')
    plt.legend(loc='upper left')
    plot_name = '{}_RSI_EMA_Signals.png'.format(stock)
    plot_file = plot_full_location + '/' + stock + '/' + plot_name
    plt.savefig(plot_file, dpi=150)
    signal_hlink = '=HYPERLINK("{}","RSI_EMA Signal Plot")'.format(plot_file)
    plt.close('all')

    return (rsi_ema_hlink, signal_hlink)

def buy_sell_rsi_ema(data, period_short, period_long, rsi_low, rsi_high, column):
    sigPriceBuy = []
    sigPriceSell = []
    flag = -1

    for i in range(len(data)):
        if data['EMA{}'.format(period_short)][i] > data['EMA{}'.format(period_long)][i]:
            if data['RSI'][i] < rsi_low:
                if flag != 1:
                    sigPriceBuy.append(data[column][i])
                    sigPriceSell.append(np.nan)
                    flag = 1
                else:
                    sigPriceBuy.append(np.nan)
                    sigPriceSell.append(np.nan)
            else:
                sigPriceBuy.append(np.nan)
                sigPriceSell.append(np.nan)
        elif data['EMA{}'.format(period_short)][i] < data['EMA{}'.format(period_long)][i]:
            if data['RSI'][i] > rsi_high:
                if flag != 0:
                    sigPriceBuy.append(np.nan)
                    sigPriceSell.append(data[column][i])
                    flag = 0
                else:
                    sigPriceBuy.append(np.nan)
                    sigPriceSell.append(np.nan)
            else:
                sigPriceBuy.append(np.nan)
                sigPriceSell.append(np.nan)
        else:
            sigPriceBuy.append(np.nan)
            sigPriceSell.append(np.nan)

    return (sigPriceBuy, sigPriceSell)

def RSI(period_rsi, column, df_RSI_EMA):
    #Intake dataframe and converts into RSI files(new_df).
    delta = df_RSI_EMA[column].diff(1)
    delta = delta.dropna()

    up = delta.copy()
    down = delta.copy()

    up[up<0] = 0
    down[down>0] = 0

    avg_gain = up.rolling(window=period_rsi).mean()
    avg_loss = abs(down.rolling(window=period_rsi).mean())

    rs = avg_gain/avg_loss
    rsi = 100.0 - (100.0 / (1.0 + rs))

    df_RSI_EMA['RSI'] = rsi

    return df_RSI_EMA

def EMA(df_RSI_EMA, time_period, column):
    #Calculate the Exponential Moving Average for some time period.
    EMA = df_RSI_EMA[column].ewm(span=time_period, adjust=False).mean()

    return EMA

def RSI_EMA(stock, input_var, plot_full_location, wb, cur_descp, stock_summary, stkplt_hlink):
    period_rsi = input_var[0]
    rsi_low = input_var[1]
    rsi_high = input_var[2]
    period_short = input_var[3]
    period_long = input_var[4]
    column = input_var[5]
    df = input_var[6]
    df_RSI_EMA = pd.DataFrame()
    columns = ['Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']
    for col in columns:
        df_RSI_EMA[col] = df[col]

    #Calculating RSI.
    df_RSI_EMA = RSI(period_rsi, column, df_RSI_EMA)

    #Calculating Short and Long Term EMA.
    df_RSI_EMA['EMA{}'.format(period_short)] = EMA(df_RSI_EMA, period_short, column)
    df_RSI_EMA['EMA{}'.format(period_long)] = EMA(df_RSI_EMA, period_long, column)

    x = buy_sell_rsi_ema(df_RSI_EMA, period_short, period_long, rsi_low, rsi_high, column)
    df_RSI_EMA['Buy_Signal_Price'] = x[0]
    df_RSI_EMA['Sell_Signal_Price'] = x[1]

    strategy_hlinks = plot_rsi_ema(stock, df_RSI_EMA, plot_full_location, cur_descp, period_rsi,
                                   rsi_low, rsi_high, period_short, period_long, column)
    stock_status = stock_current_status(df_RSI_EMA)
    bt_data = BackTest('RSI_EMA', stock, df_RSI_EMA, plot_full_location, wb, cur_descp)
    print_report(stock, cur_descp, df_RSI_EMA, bt_data, stock_summary,
                 stock_status, stkplt_hlink, strategy_hlinks)

    return stock_status


from django.shortcuts import render
from django.views.generic.base import View
from django.http import JsonResponse
from apps.goods.models import GoodsSKU
from django_redis import get_redis_connection
from utils.mixin import LoginRequiredMixin

# Create your views here.
#/cart/add
class CartAddView(View):
    '''添加购物车'''
    def post(self,request):
        '''购物车的添加'''
        user = request.user
        #判断用户是否登陆，登陆了才能添加购物车
        if not user.is_authenticated:
            return JsonResponse({'res':0,'errmsg':'用户未登陆'})
        #获取数据
        sku_id = request.POST.get('sku_id') #商品id
        count = request.POST.get('count') #商品的数量
        print("count",type(count))

        #校验数据的有效性
        if not all([sku_id,count]):
            return JsonResponse({'res':1,'errmsg':'数据不完整'})

        try:#商品数量不是整数
            count = int(count)
        except Exception as e:
            return JsonResponse({'res': 2, 'errmsg': '数据不正确'})

        # 校验商品是否存在
        try:
            sku = GoodsSKU.objects.get(id=sku_id)
        except GoodsSKU.DoesNotExist:
            return JsonResponse({'res': 3, 'errmsg': '商品不存在'})

        #进行业务的处理添加购物车
        conn = get_redis_connection('default')
        cart_key = 'cart_%d' % user.id
        #先尝试获取sku_id的值 —>hget cart_key 属性
        #如果sku_id在hash中不存在，hget返回None
        cart_count = conn.hget(cart_key,sku_id)
        print("")
        if cart_count:
            #累加数量
            count += int(cart_count)

        #校验商品是否还有库存
        if sku.stock<count:
            return JsonResponse({'res': 4, 'errmsg': '商品库存已不足'})

        #设置hash中sku_id对应的值
        #hset —>如果sku_id已经存在，更新数据，如果sku_id不存在，添加数量
        conn.hset(cart_key,sku_id,count)
        #获取商品条目数
        total_count = conn.hlen(cart_key)
        #返回应答
        return JsonResponse({'res': 5,'total_count':total_count, 'message': '购物车添加成功'})
#/cart
class CartInfoView(LoginRequiredMixin,View):
    '''购物车信息'''
    def get(self,request):
        '''购物车信息'''

        #从redis中获取购物车信息
        user = request.user
        conn = get_redis_connection('default')
        cart_key = "cart_%s"%user.id

        #存储的购物车格式"card_id":{"商品id sku_id":"商品数量 count"}
        #hgetall Return a Python dict
        cart = conn.hgetall(cart_key)

        #获取商品信息
        skus =[]
        #总数量
        total_count = 0
        #总价格
        total_amount = 0
        for sku_id,count in cart.items():
            #商品
            sku = GoodsSKU.objects.get(id=sku_id)
            #商品总价
            amount = sku.price*int(count)
            #给商品增加属性
            sku.amount = amount
            #给商品增加数量属性
            sku.count = int(count)
            skus.append(sku)
            #商品总价格
            total_amount += amount
            #商品总数量
            total_count +=int(count)

        #组织上下文
        context = {"skus":skus,
                   "total_amount":total_amount,
                   "total_count":total_count}
        #使用模板
        return render(request,'cart.html',context)

# 请求方式：ajax post
# 传递参数：商品id sku_id ,商品数量 count
# 返回： JsonResponse({'res':5, 'message': '更新成功', 'total_count': total_count})
# /cart/update
class CartUpdateView(View):
    '''购物车记录更新'''
    def post(self,request):
        '''购物车记录更新'''
        user = request.user
        # 判断用户是否登陆，登陆了才能添加购物车
        if not user.is_authenticated:
            return JsonResponse({'res': 0, 'errmsg': '用户未登陆'})
        # 获取数据
        sku_id = request.POST.get('sku_id')  # 商品id
        count = request.POST.get('count')  # 商品的数量
        #print("count", type(count))

        # 校验数据的有效性
        if not all([sku_id, count]):
            return JsonResponse({'res': 1, 'errmsg': '数据不完整'})

        try:  # 商品数量不是整数
            count = int(count)
        except Exception as e:
            return JsonResponse({'res': 2, 'errmsg': '数据不正确'})

        # 校验商品是否存在
        try:
            sku = GoodsSKU.objects.get(id=sku_id)
        except GoodsSKU.DoesNotExist:
            return JsonResponse({'res': 3, 'errmsg': '商品不存在'})

        # 进行业务的处理添加购物车
        conn = get_redis_connection('default')
        cart_key = 'cart_%d' % user.id

        if count>sku.stock:
            return JsonResponse({'res': 4, 'errmsg': '商品库存不足'})
        #更新
        conn.hset(cart_key,sku_id,count)

        #计算用户购物车中商品的总件数{'1':5,''2:3}
        total_count = 0
        vals = conn.hvals(cart_key)
        for val in vals:
            total_count+=int(val)
        return JsonResponse({'res':5,'total_count':total_count,'message': '购物车添加成功'})

# 请求方式：ajax post
# 传递参数：商品id sku_id
# /cart/delete
class CartDeleteView(View):
    '''购物车删除'''
    def post(self,request):
        '''购物车删除'''

        #获取用户
        user = request.user
        #判断用户是否登陆
        if not user.is_authenticated:
            return JsonResponse(({'res': 0, 'errmsg': '用户未登陆'}))
        sku_id = request.POST.get('sku_id')

        if not sku_id:
            return JsonResponse(({'res': 1, 'errmsg': '数据不完整'}))

        # 校验商品是否存在
        try:
            sku = GoodsSKU.objects.get(id=sku_id)
        except GoodsSKU.DoesNotExist:
            return JsonResponse({'res': 2, 'errmsg': '商品不存在'})

        # 进行业务的处理删除购物车
        conn = get_redis_connection('default')
        cart_key = 'cart_%d' % user.id
        conn.hdel(cart_key,sku_id)

        # 计算用户购物车中商品的总件数{'1':5,''2:3}
        total_count = 0
        vals = conn.hvals(cart_key)
        for val in vals:
            total_count += int(val)
        #使用模板
        return JsonResponse({'res': 5,'total_count':total_count, 'errmsg': '删除成功'})


from py4j.clientserver import ClientServer, JavaParameters, PythonParameters
from jupyter_client.kernelspec import NoSuchKernel
import json
import sys
from .jvm_kernel_magic import JVMKernelMagic

class PythonEntryPoint(object):

    def __init__(self, kernel_name, context):
        self.pm = JVMKernelMagic(kernel_name, context)

    def evaluate(self, code):
        print('code for evaluate {}'.format(code))
        self.pm.run_cell(code)
        return None

    def getShellMsg(self):
        shellMsg = self.pm.get_shell_msg()
        return json.dumps(shellMsg, default=str)

    def getIopubMsg(self):
        iopubMsg = self.pm.get_iopub_msg()
        return json.dumps(iopubMsg, default=str)

    def shutdownKernel(self):
        self.pm.stop_kernel()
        return None

    def sendMessage(self, msg_raw):
        self.pm.pass_msg(msg_raw)
        return None

    class Java:
        implements = ["com.twosigma.beakerx.kernel.PythonEntryPoint"]

class Py4JServer:
    def __init__(self, port, pyport, kernel_name, context):
        try:
            pep = PythonEntryPoint(kernel_name, context)
        except NoSuchKernel:
            sys.exit(2)
        ClientServer(
            java_parameters=JavaParameters(port=int(port)),
            python_parameters=PythonParameters(port=int(pyport)),
            python_server_entry_point=pep)
        print('Py4j server is running')

if __name__ == '__main__':
    Py4JServer(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])


import pandas as pd
import numpy as np
from gurobipy import *

data = pd.read_csv(
    r"C:\Users\Administrator\Desktop\2023年MathorCup高校数学建模挑战赛赛题\A题\附件\附件1：data_100.csv").to_numpy().T
t = np.array([data[2 * i] for i in range(0, 100)])  # 通过率
h = np.array([data[2 * i + 1] for i in range(0, 100)])  # 坏账率
M = 100  # 信用评分卡数
N = len(t.T)  # 单张信用评分卡的阈值
P = 1000000
I = 0.08
f1 = 0.36
c = np.max(t*h)

model = Model('third_finally')
x = model.addVars(M, N, vtype=GRB.BINARY, name='x')
y = model.addVars(M, vtype=GRB.BINARY, name='y')
w = model.addVars(M, M, M, N, N, N, vtype=GRB.BINARY,name='w')

obj = QuadExpr()
for i in range(M):
    for j in range(i+1, M):
        for k in range(j+1, M):
            for m in range(N):
                for n in range(N):
                    for o in range(N):
                        if 9*(h[i, m]+h[j, n]+h[k, o]) < 2:
                            obj.add(w[i, j, k, m, n, o]*(x[i, m]+x[j, n]+x[k, o]-2),
                                    t[i, m]*t[j, n]*t[k, o]*(f1*(h[i, m]+h[j, n]+h[k, o])-I)
                                    )

model.addConstrs(quicksum(x[i]) == y[i] for i in range(M))
model.addConstr(quicksum(y) == 3, name='最多选取3个')

model.setObjective(obj, GRB.MINIMIZE)
model.optimize()
if model.status == GRB.OPTIMAL:
    order = 0
    for var in model.getVars():
        if var.x == 1.0 and var.varname.startswith('x'):
            order += 1
            print(f'选择的第{order}个信用评分卡和阈值是：', var.varname)
    print('银行最终最优收益是：', -P * model.objVal)


# Ask the user for input N (positive integer)
N = int(input("Enter the value of N (positive integer): "))

# Initialize a list to store the N numbers
numbers = []

# Read N numbers one by one
for i in range(N):
    num = int(input(f"Enter number {i + 1}: "))
    numbers.append(num)

# Ask the user for input X (integer)
X = int(input("Enter the value of X (integer): "))

# Check if X is in the list and find its index
if X in numbers:
    index = numbers.index(X) + 1
    print(f"The index of {X} is: {index}")
else:
    print("-1")


import torch
from torch import nn
from .dl_modules import ChordEncoder, ChordDecoder, \
    TextureEncoder, PianoTreeDecoder

class PolyDisVAE(nn.Module):

    def __init__(self, device, chd_encoder, txt_encoder, pnotree_decoder,
                 chd_decoder):
        super(PolyDisVAE, self).__init__()
        self.device = device
        self.chd_encoder = chd_encoder
        self.txt_encoder = txt_encoder
        self.pnotree_decoder = pnotree_decoder
        self.chd_decoder = chd_decoder

    def chd_encode(self, c, sample=False):
        """
        :param c: (bs, 8, 36)
        :param sample: bool
        :return: z_chd: (bs, 256)
        """
        self.eval()
        with torch.no_grad():
            dist_chd = self.chd_encoder(c)
            z_chd = dist_chd.sample() if sample else dist_chd.mean
        return z_chd

    def txt_encode(self, pr_mat, sample=False):
        """
        :param pr_mat: (bs, 32, 128)
        :param sample: bool
        :return: z_txt: (bs, 256))
        """
        self.eval()
        with torch.no_grad():
            dist_txt = self.txt_encoder(pr_mat)
            z_txt = dist_txt.sample() if sample else dist_txt.mean
        return z_txt

    def chd_decode(self, z_chd):
        """
        :param z_chd: (bs, 256)
        :return: (bs, 8, 12), (bs, 8, 12, 2), (bs, 8, 12)
        """
        self.eval()
        with torch.no_grad():
            root_pred, chroma_pred, bass_pred = \
                self.chd_decoder(z_chd, True, 0., None)
        return root_pred, chroma_pred, bass_pred

    def pnotree_decode(self, z_chd, z_txt):
        """
        :param z_chd: (bs, 256)
        :param z_txt: (bs, 256)
        :return: (bs, 32, 15, 6)
        """
        self.eval()
        with torch.no_grad():
            z = torch.cat([z_chd, z_txt], dim=-1)
            pitch_outs, dur_outs = \
                self.pnotree_decoder(z, True, None, None, 0., 0.)
            pnotree_pred, _, _ = \
                self.pnotree_decoder.output_to_numpy(pitch_outs, dur_outs)
            return pnotree_pred

    def pnotree_to_notes(self, pnotree, bpm, start=0.):
        return \
            self.pnotree_decoder.grid_to_pr_and_notes(pnotree, bpm, start)[1]

    @classmethod
    def init_model(cls, device=None, chd_size=256, txt_size=256,
                   num_channel=10):

        if device is None:
            device = torch.device('cuda' if torch.cuda.is_available()
                                  else 'cpu')
        chd_encoder = ChordEncoder(36, 1024, chd_size)

        txt_encoder = TextureEncoder(256, 1024, txt_size, num_channel)

        chd_decoder = ChordDecoder(z_dim=chd_size)

        pnotree_decoder = PianoTreeDecoder(note_embedding=None,
                                           dec_dur_hid_size=64,
                                           z_size=chd_size + txt_size)

        model = cls(device, chd_encoder, txt_encoder,
                    pnotree_decoder, chd_decoder)

        return model.to(device)

    def load_model(self, model_path):
        dic = torch.load(model_path, map_location=self.device)
        for name in list(dic.keys()):
            dic[name.replace('rhy_encoder', 'txt_encoder')] = dic.pop(name)
        for name in list(dic.keys()):
            dic[name.replace('chd_decoder', 'xxyy')] = dic.pop(name)
        for name in list(dic.keys()):
            dic[name.replace('decoder', 'pnotree_decoder')] = dic.pop(name)
        for name in list(dic.keys()):
            dic[name.replace('xxyy', 'chd_decoder')] = dic.pop(name)
        self.load_state_dict(dic)
        self.to(self.device)


import json

print("This program will validate the JSON training data.")

file = input("Enter the file name with extension: ")

# Load the JSON file
with open(file, "r", encoding="utf8") as f:
    data = json.load(f)

# Check each item in the JSON file
for item in data:
    if "instruction" not in item or "input" not in item or "output" not in item:
        print("Error: Missing key in JSON item.")
        print(item)

print("File done. ")


def main():
    lista_a = []
    for a in range(20):
        n = int(input(f'Digite o {a+1}° número: '))
        lista_a.append(n)

    lista_b = []
    for a in range(20):
        n = int(input(f'Digite o {a+1}° número: '))
        lista_b.append(n)

    print(lista_a)
    print(lista_b)
    lista_c = lista_soma(lista_a, lista_b)
    print(lista_c)

def lista_soma(lista_a, lista_b):
    lista_c = []
    posicao = 0

    while True:
        if len(lista_c) >= 20 :
            break

        lista_c.append(lista_a[posicao] + lista_b[posicao])

        posicao += 1

    return lista_c

if __name__ == "__main__":
    main()


from keras.layers import Dense, Input
from keras.models import Model, load_model
from keras.datasets import mnist
from keras import regularizers
from keras.callbacks import TensorBoard
import matplotlib.pyplot as plt
import seaborn as sbs
import numpy as np
import sys
from scipy.stats import entropy

sbs.set()

# size of encoding representation
encoding_dim = 32
epsilon = 0.0000000001

def build_autoencoder():
    input_img = Input(shape=(784,))
    #encoded = Dense(encoding_dim, activation='relu', activity_regularizer=regularizers.l1(10e-3))(input_img)
    encoded = Dense(encoding_dim, activation='relu')(input_img)
    decoded = Dense(784, activation='sigmoid')(encoded)
    auto_encoder = Model(input_img, decoded)

    sep_encoder = Model(input_img, encoded)

    # placeholder for input
    encoded_input = Input(shape=(encoding_dim,))
    # get the last layer of auto_encoder
    decoder_layer = auto_encoder.layers[-1]
    sep_decoder = Model(encoded_input, decoder_layer(encoded_input))

    auto_encoder.compile(optimizer='adadelta', loss='binary_crossentropy')
    return (auto_encoder, sep_encoder, sep_decoder)

def show_images(orig_imgs, decoded_imgs, filename=None):
    n = 10  # how many digits we will display
    plt.figure(figsize=(20, 4))
    for i in range(n):
        # display original
        ax = plt.subplot(2, n, i + 1)
        plt.imshow(orig_imgs[i].reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)

        # display reconstruction
        ax = plt.subplot(2, n, i + 1 + n)
        plt.imshow(decoded_imgs[i].reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
    if filename == None:
        plt.show()
    else:
        plt.savefig(filename)
    plt.close()

def test_and_draw_single_image(encoder, decoder, rand_img, filename=None):
    rand_img = [rand_img]
    rand_img = np.array(rand_img)
    boo = encoder.predict(rand_img)
    foo = decoder.predict(boo)
    plt.figure()
    ax = plt.subplot(1, 2, 1)
    plt.imshow(rand_img.reshape(28, 28))
    ax = plt.subplot(1, 2, 2)
    plt.imshow(foo.reshape(28, 28))
    plt.gray()
    if filename == None:
        plt.show()
    else:
        plt.savefig(filename)
    plt.close()

def test_single_image(encoder, decoder, rand_img):
    #rand_img = [rand_img]
    #rand_img = np.array(rand_img)
    boo = encoder.predict(rand_img)
    foo = decoder.predict(boo)

    rand_img = rand_img + epsilon
    foo = foo + epsilon
    return entropy(rand_img.flatten(), qk=foo.flatten())

class GraphParams:
    def __init__(self, xlabel, ylabel, legend):
        self.xlabel = xlabel
        self.ylabel = ylabel
        self.legend = legend

def plot_results(noise_levels, entropy_vals, entropy_stds, graph_params, filename=None):
    sbs.set_style("whitegrid", {"axes.grid": True})
    plt.figure()
    plt.errorbar(noise_levels, entropy_vals, yerr=entropy_stds, label=graph_params.legend)
    plt.grid(b=True)
    plt.xlabel(graph_params.xlabel)
    plt.ylabel(graph_params.ylabel)
    if graph_params.legend != None:
        plt.legend(numpoints=1, fancybox=True)
    if not filename:
        plt.show()
    else:
        plt.savefig(filename)
    plt.close()

if __name__ == '__main__':
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    # normalize and flatten into single dimensional vectors
    X_train = x_train.astype('float32') / 255.
    X_train = X_train.reshape((len(X_train), np.prod(X_train.shape[1:])))
    X_test = x_test.astype('float32') / 255.
    X_test = X_test.reshape((len(X_test), np.prod(X_test.shape[1:])))

    # notice that validation is also x_train
    if len(sys.argv) > 1:
        auto_encoder = load_model(sys.argv[1] + "_autoencoder")
        encoder = load_model(sys.argv[1] + "_encoder")
        decoder = load_model(sys.argv[1] + "_decoder")
    else:
        (auto_encoder, encoder, decoder) = build_autoencoder()
        # auto_encoder.fit(X_train, X_train, batch_size=256, epochs=5, shuffle=True, validation_data=(X_test, X_test), verbose=1, callbacks=[TensorBoard(log_dir='C:\\tmp')])
        auto_encoder.fit(X_train, X_train, batch_size=256, epochs=100, shuffle=True, validation_data=(X_test, X_test), verbose=1)
        auto_encoder.save("exp2_autoencoder")
        encoder.save("exp2_encoder")
        decoder.save("exp2_decoder")

    noise_levels = [0.1, 0.3, 0.5, 0.7, 0.9]
    num_runs = 100

    entropy_vals = np.zeros(len(noise_levels))
    entropy_stds = np.zeros(len(noise_levels))
    for noise_ind in range(len(noise_levels)):
        rand_entropy = np.zeros(num_runs)

        for i in range(num_runs):
            rand_img_ind = np.random.randint(0, len(X_test)-1, 1)
            rand_img = X_test[rand_img_ind]
            noisy_img = rand_img + noise_levels[noise_ind] * np.random.normal(loc=0.0, scale=1.0, size=rand_img.shape)
            noisy_img = np.clip(noisy_img, 0., 1.)
            rand_entropy[i] = test_single_image(encoder, decoder, noisy_img)

        entropy_vals[noise_ind] = np.mean(rand_entropy)
        entropy_stds[noise_ind] = np.std(rand_entropy)

    graph_params = GraphParams('Noise Ratio', 'KL Distance', None)
    plot_results(noise_levels, entropy_vals, entropy_stds, graph_params, 'KL_distance_autoencoder.png')

    # show results
    #encoded_imgs = encoder.predict(X_test)
    # decoded_imgs = decoder.predict(encoded_imgs)

    # show_images(X_test[:10], decoded_imgs, "auto_encoder_test_imgs.png")

    print("bye bye")


import sys
sys.stdin = open('input.txt')
input = sys.stdin.readline

n = int(input())

a = [False, False] + [True] * (n-1)
for i in range(2, n+1):
    if a[i]:
        for j in range(2*i, n+1, i):
            a[j] = False

if n < 8:
    print(-1)
else:
    if n % 2 == 0:
        n -= 4
        answer = [2, 2]
    elif n % 2 == 1:
        n -= 5
        answer = [2, 3]

    for i in range(2, n+1):
        if a[i] and a[n-i]:
            answer.extend([i, n-i])
            print(' '.join(list(map(str, sorted(answer)))))
            break
    else:
        print(-1)


import pytest
from tech_news.analyzer.reading_plan import ReadingPlanService  # noqa: F401, E261, E501
from unittest.mock import patch

def mock_data():
    return [
        {
            "url": "https://blog.betrybe.com/novidades/noticia-bacana",
            "title": "Notícia bacana",
            "timestamp": "04/04/2021",
            "writer": "Eu",
            "reading_time": 5,
            "summary": "Algo muito bacana aconteceu",
            "category": "Ferramentas",
        },
        {
            "url": "https://blog.betrybe.com/novidades/noticia-bacana",
            "title": "Notícia super bacana",
            "timestamp": "04/04/2021",
            "writer": "Eu",
            "reading_time": 10,
            "summary": "Algo muito bacana aconteceu",
            "category": "Ferramentas",
        }
    ]

@pytest.fixture
def mock_res():
    return {
        "readable": [
            {
                "unfilled_time": 0,
                "chosen_news": [
                    (
                        "Notícia bacana",
                        5,
                    )
                ],
            },
        ],
        "unreadable": [
            ('Notícia super bacana', 10)
        ],
    }

def test_reading_plan_group_news(mock_res):
    with patch('tech_news.analyzer.reading_plan.find_news', mock_data):
        assert ReadingPlanService.group_news_for_available_time(5) == mock_res
        with pytest.raises(ValueError):
            ReadingPlanService.group_news_for_available_time(-1)


#
# Python stuff to improve portability
#
from __future__ import print_function

#
# Generic python packages used
#
import datetime
import fcntl
import getpass
import os
import select
import SocketServer
import socket
import subprocess
import sys
import threading
import time
from stat import *

#
# XDD packages
#
from xdd.constants import XDD_SINK_TO_SOURCE_DELAY
from xdd.core import XDDError, TransferPreconditionError
from xdd.factory import EndpointFactory

class TransferFailError(XDDError):

    def __init__(source, dest, reason):
        self.source = source
        self.dest = dest
        self.reason = reason

#
# Interface classes to interact with local and remote XDD flows
#
class TransferManager:
    """
    Manager for creating, monitoring, and deallocating local and remote flows.
    """

    def __init__(self):
        """Constructor"""
        self.isCreated = False
        self.isStarted = False
        self.isComplete = False
        self.isSuccess = False
        self.factory = None

        # Temporary variables used for showing progress
        self.beginTime = None
        self.currentSourceFile = None
        self.currentSinkFile = None
        self.currentFlowSize = 0
        self.currentRestartOffset = 0
        self.verboseLog = None

        self.requestSize = 0
        self.transferSize = None
        self.restartFlag = False
        self.sinkDIOFlag = False
        self.sinkSerialFlag = False
        self.sinkTimestampFlag = False
        self.sinkVerboseFlag = False
        self.sinkTarget = None
        self.sinkXddPath = ''
        self.sinks = []
        self.sourceDIOFlag = False
        self.sourceSerialFlag = False
        self.sourceVerboseFlag = False
        self.sourceTimestampFlag = False
        self.sourceTarget = None
        self.sourceXddPath = ''
        self.sources = []

    def setRequestSize(self, reqSize):
        """Set the chunk size of data to move across the wire"""
        self.requestSize = reqSize

    def setTransferSize(self, transferSize):
        """Set the size of data to move across the wire"""
        self.transferSize = transferSize

    def setRestartFlag(self, restartFlag):
        """Set the amount of data previously sent across the wire"""
        self.restartFlag = restartFlag

    def setSinkTarget(self, target, dioFlag=False, serialFlag=False):
        """ Set the name of of the sink target file"""
        assert not self.isCreated
        self.sinkTarget = target
        self.sinkDIOFlag = dioFlag
        self.sinkSerialFlag = serialFlag

    def setSourceTarget(self, target, dioFlag=False, serialFlag=False):
        """Set the name of the source target file"""
        assert not self.isCreated
        self.sourceTarget = target
        self.sourceDIOFlag = dioFlag
        self.sourceSerialFlag = serialFlag

    def setVerbosity(self, verboseLevel, filename):
        """Set the level of verbosity"""
        if 1 <= verboseLevel:
            self.sinkVerboseFlag = True
            self.sourceVerboseFlag = True
        if 2 <= verboseLevel:
            self.sinkTimestampFlag = True
            self.sourceTimestampFlag = True
        self.verboseLog = filename

    def addSink(self, user, hostIP, hostname, threads, ifs = [], port = 40010):
        """Add a sink to the list of sinks"""
        assert not self.isCreated
        assert hostIP
        assert hostname
        assert 0 < threads
        sink = {'ip': hostIP, 'hostname': hostname, 'threads': threads, 'port': port, 'ifs': []}
        if 0 == len(ifs):
            sink['ifs'].append(hostIP)
        else:
            sink['ifs'].extend(ifs)

        self.sinks.append(sink)

    def addSource(self, user, hostIP, hostname, threads, ifs = [], port = 40010):
        """Add a source to the list of sources"""
        assert not self.isCreated
        assert hostIP
        assert hostname
        assert 0 < threads
        source = {'ip': hostIP, 'hostname': hostname, 'threads': threads, 'port': port, 'ifs': []}
        if 0 == len(ifs):
            source['ifs'].append(hostIP)
        else:
            source['ifs'].extend(ifs)

        self.sources.append(source)

    def setSinkXddPath(self, path):
        """Set the path sinks use to find xdd"""
        self.sinkXddPath = path

    def setSourceXddPath(self, path):
        """Set the path sources use to find xdd"""
        self.sourceXddPath = path

    def setXddPath(self, path):
        """Set the path used to find xdd"""
        self.sinkXddPath = path
        self.sourceXddPath = path

    def performPostCreateChecks(self):
        """@return 0 if all checks pass, otherwise non-zero"""
        rc = 0
        # Check that all of the XDD's are the same version
        base = None
        current = None
        for f in self.factory.getEndpoints():
            current = f.protocolVersion()
            if base is None:
                base = current
            if current is None or not current or current != base:
                print("ERROR: XDD Protocols do not match", file=sys.stderr)
                rc = 1
                break

        # Print a generic warning if the sinks do not have pre-allocation
        # support
        for s in self.factory.getSinkEndpoints():
            if not s.hasPreallocateAvailable():
                print("WARNING: XDD preallocation support not available",
                      file=sys.stderr)
        return rc

    def createEndpoints(self):
        """Create local and remote transfer host endpoints"""
        assert not self.isCreated
        assert 0 < self.requestSize
        assert 0 < len(self.sources)
        assert 0 < len(self.sinks)
        assert self.sinkTarget
        assert self.sourceTarget
        self.factory = EndpointFactory(self.requestSize,
                                       self.sourceDIOFlag,
                                       self.sourceSerialFlag,
                                       self.sourceVerboseFlag,
                                       self.sourceTimestampFlag,
                                       self.sourceXddPath,
                                       self.sources,
                                       self.sinkDIOFlag,
                                       self.sinkSerialFlag,
                                       self.sinkVerboseFlag,
                                       self.sinkTimestampFlag,
                                       self.sinkXddPath,
                                       self.sinks)
        rc = self.factory.createEndpoints()
        if 0 == rc:
            self.isCreated = True
            rc = self.performPostCreateChecks()
        return rc

    def createDir(self, sourceDir, targetDir):
        """Create the directory targetDir on the sink"""
        assert self.isCreated
        self.beginTime = time.time()
        self.currentFlowSize = 0
        sink = self.factory.getSinkEndpoints()[0]
        rc = sink.createDirectory(targetDir)
        if 0 == rc:
            self.showProgress(sourceDir, 0)
            print()
        return rc

    def createSymlink(self, sourceLink, targetLink, targetTarget):
        """Create the directory targetDir on the sink"""
        assert self.isCreated
        self.beginTime = time.time()
        self.currentFlowSize = 0
        sink = self.factory.getSinkEndpoints()[0]
        rc = sink.createSymlink(targetTarget, targetLink)
        if 0 == rc:
            self.showProgress(sourceLink, 0)
            print()
        return rc

    def startTransfer(self, sourceFile, sinkFile):
        """Start all flows in correct order"""
        assert self.isCreated
        rc = 0
        self.currentSourceFile = sourceFile
        self.currentSinkFile = sinkFile
        self.currentRestartOffset = 0

        # If needed get a transfer size
        if not self.transferSize:
            s = self.factory.getSourceEndpoints()[0]
            transferSize = s.getFileSize(sourceFile)
        else:
            transferSize = self.transferSize

        # Set a restart offset
        restartOffset = 0
        if self.restartFlag:
            s = self.factory.getSinkEndpoints()[0]
            restartOffset = s.getRestartOffset(sinkFile)
            self.currentRestartOffset = restartOffset

        # Start sinks first
        sinks = self.factory.getSinkEndpoints()
        for s in sinks:
            r = s.startFlow(sinkFile, transferSize,
                            self.restartFlag, restartOffset)
            if 0 != r:
                print('Sink flow failed during startup', s.errorString())
                rc += 1

        # Pause between sink and source startups
        time.sleep(XDD_SINK_TO_SOURCE_DELAY)

        # Then start sources
        sources = self.factory.getSourceEndpoints()
        for s in sources:
            r = s.startFlow(sourceFile, transferSize,
                            self.restartFlag, restartOffset)
            if 0 != r:
                print('Source flow failed during startup', s.errorString())
                rc += 1

        # Update the log with the execute commands
        self.writeLog(isWriteCommand=True)

        # If all went well, set state and start timer
        if 0 == rc:
            self.isStarted = True
            self.beginTime = time.time()
            self.currentFlowSize = transferSize
        return rc

    def monitorTransfer(self, monitorInterval):
        """
        Monitor the current flows.  Print a progress report to the screen
        and update the logfile if necessary.
        """
        assert self.isStarted
        endpoints = self.factory.getEndpoints()
        completedBytes = 0
        completeCount = 0
        failureCount = 0
        while completeCount < len(endpoints) and 0 == failureCount:
            monBegin = time.time()
            completedBytes = 0
            completeCount = 0
            for e in endpoints:
                # Check for completions and failures
                if e.completionStatus() is not None:
                    completeCount += 1
                    if 0 != e.completionStatus():
                        failureCount += 1
                # Get the progress
                currentBytes = e.currentByte()
                if 0 < currentBytes:
                    completedBytes += currentBytes

            # The flows don't really report exactly correct bytes yet, so
            # modify the completed bytes to be correct, this is due to a couple
            # things, sinks report the restart offset and multi-source transfers
            # use -startoffset rather than -restart offset
            correctedBytes = completedBytes - self.currentRestartOffset
            if len(self.sources) > 1:
                # Progress temporarily reports as len(sources) * restartOffset
                # But then shrinks as the actual heartbeats get reported
                initialVal = len(self.sources) * self.currentRestartOffset
                if correctedBytes == initialVal:
                    correctedBytes = self.currentRestartOffset
                else:
                    correctedBytes += self.currentRestartOffset

            # Print a status message
            self.showProgress(self.currentSourceFile, correctedBytes)
            self.writeLog()
            monEnd = time.time()

            # Sleep until its time to monitor again
            interval = monEnd - monBegin
            if interval < monitorInterval:
                time.sleep(monitorInterval - interval)

        # Get all of the remaining log output
        self.writeLog(flushAll=True)

        # Determine the outcome of transfer
        failures = 0
        for e in endpoints:
            if e.completionStatus() is None:
                e.cancelFlow()
            elif 0 != e.completionStatus():
                failures += 1
                print('RC:', e.completionStatus())
                print('ERROR:', e.errorString())

        # If the completion status for all endpoints is success, print out
        # a final progress message and remove the restart cookie
        if 0 == failures:
            self.currentRestartOffset = 0
            self.showProgress(self.currentSourceFile, self.currentFlowSize)
            print('')
            if self.restartFlag:
                for s in self.factory.getSinkEndpoints():
                    # Ignore the return code on purpose
                    rc = s.removeRestartCookie(self.currentSinkFile)

        return failures

    def startAndMonitorTransfers(self, monitorInterval):
        """
        Used to transfer and monitor a directory or multiple files
        as part of a single transfer
        """
        # First determine if the sink path exists
        sink = self.factory.getSinkEndpoints()[0]
        sinkIsDir = sink.pathIsDir(self.sinkTarget)
        sinkExists = sink.pathExists(self.sinkTarget)
        # This is tricky, if the sink path exists from a previous
        # transfer attempt and this is a retry, then we need to lie about it
        if self.restartFlag and sinkExists and \
           sink.transferIsComplete(self.sinkTarget, self.sinkTarget):
            sinkExists = False

        # Use one of the sources to build the transfer pairs
        # Note you have to do this on the source in case the requested
        # path is a directory or symlink
        source = self.factory.getSourceEndpoints()[0]
        (rc, dirs, files, links) = source.buildWalk(self.sourceTarget,
                                                    self.sinkTarget,
                                                    targetExists=sinkExists,
                                                    targetIsDir=sinkIsDir)
        if 0 != rc:
            print('ERROR:  Unable to access', self.sourceTarget)

        #print(rc)
        #print(dirs)
        #print(files)
        #print(links)
        # Build all of the remote directories with the first sink
        if 0 == rc:
            for (sdir, tdir) in dirs:
                rc = self.createDir(sdir, tdir)
                if 0 != rc:
                    print('ERROR transferring', sdir)
                    break
                sink.markTransferCompleted(self.sinkTarget, tdir)

        # Move all files
        if 0 == rc:
            for (sfile, tfile) in files:
                if self.restartFlag and \
                   sink.transferIsComplete(self.sinkTarget, tfile):
                    print(sfile, 'previously completed')
                    continue
                rc = self.startTransfer(sfile, tfile)
                if 0 != rc:
                    print('ERROR starting transfer', sfile)
                    break
                rc = self.monitorTransfer(monitorInterval)
                if 0 != rc:
                    print('ERROR transferring', sfile)
                    break
                if self.restartFlag and 0 == rc:
                    sink.markTransferCompleted(self.sinkTarget, tfile)

        # Create all symlinks
        if 0 == rc:
            for (ssym, tsym, link) in links:
                rc = self.createSymlink(ssym, tsym, link)
                if 0 != rc:
                    print('ERROR transferring', ssym)
                    break
                sink.markTransferCompleted(self.sinkTarget, tsym)

        # Remove the progress file
        if 0 == rc:
            sink.removeTransferProgressFile(self.sinkTarget)

        return rc

    def cleanupEndpoints(self):
        """
        Determine exit status for stopped flows, and kill any remaining flows
        """
        rc = 0
        if self.factory:
            endpoints = self.factory.getEndpoints()
            #for e in endpoints:
            #    if True == e.pollFlow():
            #        f.cancelFlow()
            #        print('Cancelling Flow')
            #        rc = 1
            #    elif 0 != f.completionStatus():
            #        print('Flow exited with code:', e.completionStatus())
            #        print('Flow cmd:', e.executeString())
            #        rc = 2

            self.factory.shutdown()
            del self.factory

        self.isCreate = False
        self.isStarted = False
        self.isCompleted = True
        return rc

    def completedSuccessfully(self):
        """@return true if the flows completed successfully"""
        return self.isComplete and self.isSuccess

    def showProgress(self, filename, completedBytes):
        """Print a status line to stderr without a linefeed"""
        # Calculate the status values
        try:
            currentBytes = completedBytes - self.currentRestartOffset
            pctProgress = (float(completedBytes) / self.currentFlowSize) * 100.0
            transferTime = time.time() - self.beginTime
            bandwidth = float(currentBytes) / transferTime
            eta = '--:--:--'
        except ZeroDivisionError:
            # Zero size flows are completed by definition
            pctProgress = 100.0
            transferTime = time.time() - self.beginTime
            bandwidth = 0.0
            eta = '00:00:00'

        if (bandwidth > 0):
            eta_secs = round((self.currentFlowSize - completedBytes)/bandwidth)
            td = datetime.timedelta(seconds=eta_secs)
            eta = str(td)

        # Get the width of the terminal
        cols = 80
        if sys.stdin.isatty():
            stty_size = os.popen('stty size').read()
            if 0 != len(stty_size):
                (r, c) = stty_size.split()
                cols = int(c)

        # Generate the status string
        status = ''
        if 100.0 <= pctProgress:
            status += str(round(pctProgress)) + '% '
        elif 10.0 <= pctProgress:
            status += str(pctProgress)[0:4] + '% '
        else:
            status += str(pctProgress)[0:3] + '% '
        if (1024*1024*1024) < bandwidth:
            status += str(completedBytes / (1024*1024*1024)) + 'GiB '
        elif (1024*1024) < bandwidth:
            status += str(completedBytes / (1024*1024)) + 'MiB '
        elif 1024 < bandwidth:
            status += str(completedBytes / 1024) + 'KiB '
        else:
            status += str(completedBytes) + 'B '
        status += str(round(bandwidth / 1024.0 / 1024.0)) + 'MiB/s '
        status += eta + ' ETA'

        # Determine the padding length between the file name and status
        padding = ' '
        if cols > (len(status) + len(filename)):
            padding *= cols - len(status) - len(filename)

        # Clear the existing status line with whitespace
        print('\r', ' ' * cols, sep='', end='', file=sys.stderr)

        # Write the new status line
        print('\r', filename, padding, status,
              sep='', end='', file=sys.stderr)

    def writeLog(self, isWriteCommand=False, flushAll=False):
        """
        Log current status to the logfile.  This isn't free.  It sends alot
        of data over the RPC channels if this transfer is remote.
        """
        if self.verboseLog is not None:
            f = open(self.verboseLog, 'a')
            # Write sink data
            if self.sinkVerboseFlag:
                sinks = self.factory.getSinkEndpoints()
                for s in sinks:
                    prefix = s.hostname() + ': '
                    if isWriteCommand:
                        f.write(prefix + s.executeString() + os.linesep)
                    else:
                        lines = s.output(flushAll).split(os.linesep)
                        for l in lines:
                            if l:
                                f.write(prefix + l + os.linesep)
            # Write source data
            if self.sourceVerboseFlag:
                sources = self.factory.getSourceEndpoints()
                for s in sources:
                    prefix = s.hostname() + ': '
                    if isWriteCommand:
                        f.write(prefix + s.executeString() + os.linesep)
                    else:
                        lines = s.output(flushAll).split(os.linesep)
                        for l in lines:
                            if l:
                                f.write(prefix + l + os.linesep)
            f.close()


import json

import numpy as np
from data_prep import SentimentData
from keras.callbacks import EarlyStopping, TensorBoard
from keras.models import Model

def train(model: Model, data: SentimentData, name: str):
    cbs = get_callbacks("./logs/" + name)

    metrics = {}

    # print("Training on English")
    # model.fit(
    #     x=data.en_x,
    #     y=data.en_y,
    #     validation_data=(data.cm_x, data.cm_y),
    #     epochs=20,
    #     initial_epoch=0,
    #     shuffle=True,
    #     callbacks=cbs,
    # )
    #
    # metrics["en"] = model.evaluate(data.test_x, data.test_y)
    # print(metrics)
    #
    # print("Training on Spanish")
    # model.fit(
    #     x=data.es_x,
    #     y=data.es_y,
    #     validation_data=(data.cm_x, data.cm_y),
    #     epochs=30,
    #     initial_epoch=20,
    #     shuffle=True,
    #     callbacks=cbs,
    # )
    #
    # metrics["es"] = model.evaluate(data.test_x, data.test_y)
    # print(metrics)
    #
    print("Training on concat")
    model.fit(
        x=np.concatenate([data.en_x, data.es_x]),
        y=np.concatenate([data.en_y, data.es_y]),
        validation_data=(data.cm_x, data.cm_y),
        epochs=100,
        initial_epoch=0,
        shuffle=True,
        callbacks=cbs,
    )

    metrics["both"] = model.evaluate(data.test_x, data.test_y)
    print(metrics)

    print("Training on cm")
    model.fit(
        x=data.cm_x,
        y=data.cm_y,
        validation_split=0.2,
        epochs=70,
        initial_epoch=50,
        shuffle=True,
        callbacks=cbs,
    )
    metrics["cm"] = model.evaluate(data.test_x, data.test_y)
    print(metrics)

    with open("./logs/" + name + "/metric.json", 'w', encoding='utf-8') as f:
        json.dump(metrics, f, ensure_ascii=False, indent=4)

    return model, metrics

def get_callbacks(log_dir):
    tb = TensorBoard(log_dir=log_dir, histogram_freq=5, write_graph=False, write_images=False, update_freq='batch')
    es = EarlyStopping(monitor='val_f1', mode='max', verbose=1, patience=10, restore_best_weights=True)
    return [tb, es]


import pandas as pd
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.wrappers.scikit_learn import KerasClassifier
import numpy as np

classifier = Sequential()
classifier.add(Dense(units = 8, activation='relu', kernel_initializer='normal', input_dim=30))
classifier.add(Dropout(0.2))
classifier.add(Dense(units = 8, activation='relu', kernel_initializer='normal'))
classifier.add(Dropout(0.2))
classifier.add(Dense(units = 1, activation='sigmoid'))
classifier.compile(optimizer= 'adam', loss='binary_crossentropy', metrics=['binary_accuracy'])

X = pd.read_csv('entradas_breast.csv')
y = pd.read_csv('saidas_breast.csv')

classifier.fit(X, y, batch_size=10, epochs=100)

classifier_json = classifier.to_json()
classifier.save_weights('classifier_weights.h5')

with open('classifier.json', 'w') as json_file:
    json_file.write(classifier_json)


import random

import matplotlib.pyplot as plt
import pandas as pd

data = []

def cal_range(starting_point, total_data_points, phase_number):
    # randomly a coeffiction for each phase
    phase_coef = random.uniform(0.5, 1)

    phase_total_data_points = (total_data_points / 6) * phase_coef
    # print(phase_total_data_points)
    phase_virtual_mean = list(range(starting_point, int(starting_point + phase_total_data_points)))
    phase_last_point = phase_virtual_mean[-1]
    # print(phase_data)

    # 4 phases only
    if phase_number == 1:
        range_coef = random.uniform(0, 1.5)
    if phase_number == 2:
        range_coef = random.uniform(1, 3)
    if phase_number == 3.1:
        range_coef = random.uniform(2, 4)
    if phase_number == 3.2:
        range_coef = random.uniform(3, 10)
    if phase_number == 4.1:
        range_coef = random.uniform(1, 2)
    if phase_number == 4.2:
        range_coef = random.uniform(0.1, 1)

    return phase_virtual_mean, phase_last_point, range_coef

total_points = 350
start_point = 2
boundaries = []

# Phase 1: Stealth
stealth_virtual_mean, stealth_last_point, stealth_range_coef = cal_range(start_point, total_points, 1)
for each_point in stealth_virtual_mean:
    upper_limit = each_point + each_point * stealth_range_coef
    lower_limit = each_point - each_point * stealth_range_coef
    each_point_boundaries = [lower_limit, upper_limit]
    boundaries.append(each_point_boundaries)

# Phase 2: Awareness
aware_virtual_mean, aware_last_point, aware_range_coef = cal_range(stealth_last_point + 1, total_points, 2)
for each_point in aware_virtual_mean:
    upper_limit = each_point + each_point * aware_range_coef
    each_point_boundaries = [each_point, upper_limit]
    boundaries.append(each_point_boundaries)

# Phase 3: Mania
mania1_virtual_mean, mania1_last_point, mania1_range_coef = cal_range(aware_last_point + 1, total_points, 3.1)
for each_point in mania1_virtual_mean:
    upper_limit = each_point + each_point * mania1_range_coef
    each_point_boundaries = [each_point, upper_limit]
    boundaries.append(each_point_boundaries)

mania2_virtual_mean, mania2_last_point, mania2_range_coef = cal_range(mania1_last_point + 1, total_points, 3.2)
for each_point in mania2_virtual_mean:
    upper_limit = each_point + each_point * mania2_range_coef
    each_point_boundaries = [each_point, upper_limit]
    boundaries.append(each_point_boundaries)

# Phase 4: Blow off
blow1_virtual_mean, blow1_last_point, blow1_range_coef = cal_range(mania2_last_point + 1, total_points, 4.1)
for each_point in blow1_virtual_mean:
    upper_limit = each_point + each_point * blow1_range_coef
    each_point_boundaries = [each_point, upper_limit]
    boundaries.append(each_point_boundaries)

blow2_virtual_mean, blow2_last_point, blow2_range_coef = cal_range(blow1_last_point + 1, total_points, 4.2)
for each_point in blow2_virtual_mean:
    upper_limit = each_point + each_point * blow2_range_coef
    lower_limit = each_point - each_point * blow2_range_coef
    each_point_boundaries = [lower_limit, upper_limit]
    boundaries.append(each_point_boundaries)

for each_boundary in boundaries:
    data_point = random.uniform(each_boundary[0], each_boundary[1])
    data.append(data_point)

df = pd.DataFrame(data)
plt.plot(data)
plt.show()


#! /usr/bin/env python

def solution(word):
    chars = list(word)
    if len(chars)%2 != 0:
        return False
    stack = []
    check = ["]", ")", "}"]
    for i in chars:
        if i in check and len(stack) == 0:
            return False
        if i not in check:
            stack.append(i)
        else:
            if i == ")" and stack[-1] == "(":
                stack.pop()
            elif i == "]" and stack[-1] == "[":
                stack.pop()
            elif i == "}" and stack[-1] == "{":
                stack.pop()
            else:
                return False
    if len(stack) != 0:
        return False
    return True

print(solution("(){}[]"))