"""
31 Dec 2015, Keunwoo Choi

Trim the audio file that contains basic chords with differnt form using many instrument + drum track.

"""
import sys
import os
import numpy as np
import librosa

from constants import *

class template_info:
	def __init__(self):
		#components
		self.harmonic_ins = ['Sine', 'Piano', 'Acoustic_guitar', 'Electric_guiar', 'Saxophone', 'Strings']
		self.percussive_ins=['drums']
		self.instruments = [self.harmonic_ins, self.percussive_ins]

		self.harmonic_chords=['intevals', 'M_m', 'sus4_M7', '7_m', 'dim_blank']
		self.harmonic_keys = ['Bb2', 'Eb2', 'A3', 'G4'] # -2, -9, +9, +19

		self.percussive_rhythms = ['4beat', '8beat', '16beat', 'fills']

if __name__=="__main__":

	tpl_info = template_info()
	#load file

	path_in = 'src_template_original/templates.wav'
	path_out_src = 'src_templates/'
	path_out_SRC = 'src_templates_stft/'

	for path in [path_out_src, path_out_SRC]:
		if not os.path.exists(path):
			os.makedirs(path)

	src, sr = librosa.load(path_in, mono=True, sr=SR)
	#how to segment
	len_seg = 4 # [sec]
	sp_per_seg = len_seg * sr

	len_each_h_ins = len_seg * len(tpl_info.harmonic_chords)
	len_each_key = len_seg * len(tpl_info.instruments)
	len_harmonic = len_each_key * len(tpl_info.harmonic_keys)

	len_percussive = len_seg * len(tpl_info.percussive_rhythms)

	# trim, get stft, and save.
	out_filenames = []
	seg_idx = -1
	for h_key in tpl_info.harmonic_keys:
		for h_inst in tpl_info.harmonic_ins:
			for h_chord in tpl_info.harmonic_chords:
				segment_name = '%s_%s_%s' % (h_key, h_inst, h_chord)
				seg_idx += 1
				sp_from = seg_idx*sp_per_seg
				sp_to = sp_from + sp_per_seg
				src_here = src[sp_from:sp_to]
				SRC = librosa.stft(src_here, n_fft=N_FFT, hop_length=HOP_LEN)
				librosa.output.write_wav(path_out_src + segment_name + '.wav', src_here, sr)
				np.save(path_out_SRC + segment_name + '.npy', SRC)
				out_filenames.append(segment_name + '.npy')

	for p_inst in tpl_info.percussive_ins:
		for p_rhythm in tpl_info.percussive_rhythms:
			segment_name = '%s_%s' % (p_inst, p_rhythm)
			seg_idx += 1
			sp_from = seg_idx*sp_per_seg
			sp_to = sp_from + sp_per_seg
			src_here = src[sp_from:sp_to]
			SRC = librosa.stft(src_here, n_fft=N_FFT, hop_length=HOP_LEN)
			librosa.output.write_wav(path_out_src + segment_name + '.wav', src_here, sr)
			np.save(path_out_SRC + segment_name + '.npy', SRC)
			out_filenames.append(segment_name + '.npy')


"""
    Function to start the experiment for comparison
    between GIPP, OMIP, RNLP.
"""
import time

import printer as prn
from experiment_spec import experiments, cluster_size
from experiment_util import perform_schedulability_test
from taskset_generator import generate_taskset
from taskset_spec import taskset_const
from schedcat.locking.bounds import apply_omip_bounds, apply_gipp_bounds, assign_edf_preemption_levels

def run_experiment():

    # For each mcsl point, 20 samples are computed,
    # therefore each interval of 5 values contains 100 samples
    n_sample        = 40
    mcsl_range      = taskset_const["cs_length_nls_range"]
    mcsl_big_step   = taskset_const["mcsl_step"]
    mcsl_small_step = 1

    # Compute the number of tasksets generated
    # per mcsl value
    num_taskset_per_mcsl_value = n_sample * (mcsl_big_step / mcsl_small_step)

    print(" - execution started - ")
    print("taskset computed per mcsl values", num_taskset_per_mcsl_value)
    print("-------")

    # Iterates over experiment configurations
    # for e_index in range(0, len(experiments)):
    for e_index in range(2, 3):

        # Compute asymmetric flag
        is_asymmetric = False
        if e_index == 3:
            is_asymmetric = True

        # Computed samples
        gipp_samples = []
        omip_samples = []
        rnlp_samples = []

        # Track mcsl values
        mcsl_values  = []

        # Initialize indexes
        e          = experiments[e_index]
        mcsl_index = 0

        start = time.time()
        print("start experiment ", e_index)

        for i in xrange(mcsl_range[0], mcsl_range[1] + mcsl_big_step, mcsl_big_step):

            gipp_mcsl_sample = 0
            omip_mcsl_sample = 0
            rnlp_mcsl_sample = 0

            mcsl_values.append(i)

            for v in xrange(i, i + mcsl_big_step, mcsl_small_step):

                for s in xrange(0, n_sample):

                    # Compute utilization for ls tasks
                    # utilization_ls = e["utilization"] * (float(e["ls_tasks_number"])/e["tasks_number"])
                    utilization_ls = min(e["utilization"] * 0.5, e["ls_tasks_number"] * 0.5)

                    (t_gipp, t_omip, t_rnlp) = generate_taskset(
                        e["tasks_number"],
                        e["ls_tasks_number"],
                        e["utilization"],
                        utilization_ls,
                        e["cpu_number"],
                        e["group_conf"][0],
                        e["group_conf"][1],
                        (1, v),
                        taskset_const["cs_len_ls_range"],
                        e["max_request"],
                        taskset_const["max_issued_req_ls"],
                        taskset_const["period_ls"],
                        taskset_const["period_nls"],
                        taskset_const["resources_ls"],
                        e["resources_nls"],
                        is_asymmetric)

                    # Define functions for bounds evaluation
                    gipp_eval_func = apply_gipp_bounds
                    omip_eval_func = apply_omip_bounds

                    def rnlp_eval_func(rnlp_ts, rnlp_cpun, rnlp_cs):
                        return apply_gipp_bounds(rnlp_ts, rnlp_cpun, rnlp_cs, True)

                    # Compute the number of cluster
                    n_cluster = e["cpu_number"] / cluster_size

                    # Perform tests
                    if(perform_schedulability_test(
                            t_gipp,
                            n_cluster,
                            cluster_size,
                            e["cpu_number"],
                            gipp_eval_func)):
                        gipp_mcsl_sample = gipp_mcsl_sample + 1

                    if(perform_schedulability_test(
                            t_omip,
                            n_cluster,
                            cluster_size,
                            e["cpu_number"],
                            omip_eval_func)):
                        omip_mcsl_sample = omip_mcsl_sample + 1

                    if(perform_schedulability_test(
                            t_rnlp,
                            n_cluster,
                            cluster_size,
                            e["cpu_number"],
                            rnlp_eval_func)):
                        rnlp_mcsl_sample = rnlp_mcsl_sample + 1

            # Add GIPP samples
            gipp_samples.append(float(gipp_mcsl_sample) / num_taskset_per_mcsl_value)

            # Add OMIP samples
            omip_samples.append(float(omip_mcsl_sample) / num_taskset_per_mcsl_value)

            # Add RNLP samples
            rnlp_samples.append(float(rnlp_mcsl_sample) / num_taskset_per_mcsl_value)

            # Increment mcsl_index
            mcsl_index = mcsl_index + 1

        prn.printer(e_index, mcsl_values, [rnlp_samples, omip_samples, gipp_samples])
        end = time.time()

        time_elapsed = end - start
        print("end experiment ", e_index, " in ", time_elapsed)
        print("-------")

    """
    Utilization experiments
    """

    # Compute utilization array
    utilizations = [0.1, 0.3, 0.5]
    gipp_samples_for_u = [[] for _ in utilizations]
    mcsl_values_for_u  = []

    for u_index in range(0, 3):

        # Compute asymmetric flag
        is_asymmetric = False

        # Computed samples
        gipp_samples = []

        # Track mcsl values
        mcsl_values  = []

        # Initialize indexes and parameters
        u          = utilizations[u_index]
        e_index    = 4
        e          = experiments[e_index]
        mcsl_index = 0

        start = time.time()
        print("start utilization experiment for u = ", u)

        for i in xrange(mcsl_range[0], mcsl_range[1] + mcsl_big_step, mcsl_big_step):

            gipp_mcsl_sample = 0

            mcsl_values.append(i)

            for v in xrange(i, i + mcsl_big_step, mcsl_small_step):

                for s in xrange(0, n_sample):

                    # Compute utilization for ls tasks
                    utilization_ls = u * e["utilization"]

                    (t_gipp, t_omip, t_rnlp) = generate_taskset(
                        e["tasks_number"],
                        e["ls_tasks_number"],
                        e["utilization"],
                        utilization_ls,
                        e["cpu_number"],
                        e["group_conf"][0],
                        e["group_conf"][1],
                        (1, v),
                        taskset_const["cs_len_ls_range"],
                        e["max_request"],
                        taskset_const["max_issued_req_ls"],
                        taskset_const["period_ls"],
                        taskset_const["period_nls"],
                        taskset_const["resources_ls"],
                        e["resources_nls"],
                        is_asymmetric)

                    # Define functions for bounds evaluation
                    gipp_eval_func = apply_gipp_bounds

                    # Compute the number of cluster
                    n_cluster = e["cpu_number"] / cluster_size

                    # Perform tests
                    if(perform_schedulability_test(
                            t_gipp,
                            n_cluster,
                            cluster_size,
                            e["cpu_number"],
                            gipp_eval_func)):
                        gipp_mcsl_sample = gipp_mcsl_sample + 1

            # Add GIPP samples
            gipp_samples.append(float(gipp_mcsl_sample) / num_taskset_per_mcsl_value)

            # Increment mcsl_index
            mcsl_index = mcsl_index + 1

        gipp_samples_for_u[u_index] = gipp_samples

        # Save mcsl_values in the last iteration
        if u_index == (len(utilizations) - 1):
            mcsl_values_for_u = mcsl_values

        end = time.time()

        time_elapsed = end - start
        print("end experiment for u = ", u, " in ", time_elapsed)
        print("-------")

    prn.printer_for_u(mcsl_values_for_u, gipp_samples_for_u)

if __name__ == '__main__':
    run_experiment()


"""
"""

import os
import re
import time
from copy import deepcopy
from typing import Sequence, Optional, Any

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import torch
from torch.utils.data import DataLoader
from torch.nn.parallel import (  # noqa: F401
    DistributedDataParallel as DDP,
    DataParallel as DP,
)  # noqa: F401

try:
    from tqdm.auto import tqdm
except ModuleNotFoundError:
    from tqdm import tqdm

# from torch_ecg.torch_ecg.utils.utils_nn import default_collate_fn as collate_fn
# from torch_ecg.torch_ecg.utils.misc import dict_to_str, get_record_list_recursive3
from torch_ecg_bak.torch_ecg.utils.utils_nn import default_collate_fn as collate_fn
from torch_ecg_bak.torch_ecg.utils.misc import dict_to_str, get_record_list_recursive3

from model import ECG_CRNN_CINC2021
from dataset import CINC2021
from cfg import BaseCfg

__all__ = [
    "gather_from_checkpoint",
    "plot_confusion_matrix",
    "append_model_config_if_needed",
    "test_inference_speed",
]

ECG_CRNN_CINC2021.__DEBUG__ = False

def plot_confusion_matrix(
    cm: np.ndarray,
    classes: Sequence[str],
    normalize: bool = False,
    title: Optional[str] = None,
    cmap: mpl.colors.Colormap = plt.cm.Blues,
    fmts: Sequence[str] = ["svg", "pdf"],
) -> Any:
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if not title:
        if normalize:
            title = "Normalized Confusion Matrix"
            save_name = f"normalized_cm_{int(time.time())}"
        else:
            title = "Confusion Matrix"
            save_name = f"not_normalized_cm_{int(time.time())}"
    else:
        save_name = re.sub("[\\s_-]+", "-", title.lower().replace(" ", "-"))

    if normalize:
        cm = cm.astype("float") / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print("Confusion matrix, without normalization")

    fig, ax = plt.subplots(figsize=(15, 15))
    im = ax.imshow(cm, interpolation="nearest", cmap=cmap)
    # We want to show all ticks...
    ax.set(
        xticks=np.arange(cm.shape[1]),
        yticks=np.arange(cm.shape[0]),
        xticklabels=classes,
        yticklabels=classes,
    )
    # ax.set_title(title, fontsize=24)  # currently turn off title
    ax.set_xlabel("Label", fontsize=18)
    ax.set_ylabel("Predicted", fontsize=18)
    ax.tick_params(axis="both", which="major", labelsize=13)

    # Rotate the tick labels and set their alignment.
    plt.setp(ax.get_xticklabels(), rotation=30, ha="right", rotation_mode="anchor")

    # Loop over data dimensions and create text annotations.
    text_fmt = ".2f" if (normalize or cm.dtype == "float") else "d"
    thresh = cm.max() / 2.0
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(
                j,
                i,
                format(cm[i, j], text_fmt),
                ha="center",
                va="center",
                color="white" if cm[i, j] > thresh else "black",
                fontsize=12,
            )
    fig.tight_layout()
    # plt.show()
    for f in fmts:
        plt.savefig(
            os.path.join(BaseCfg.log_dir, f"{save_name}.{f}"),
            format=f,
            dpi=1200,
            bbox_inches="tight",
        )

    return ax

@torch.no_grad()
def gather_from_checkpoint(
    path: str, fmts: Sequence[str] = ["svg", "pdf"], dataset: Optional[CINC2021] = None
) -> None:
    """ """
    device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
    model, train_config = ECG_CRNN_CINC2021.from_checkpoint(path, device=device)
    if torch.cuda.device_count() > 1:
        model = DP(model)
        # model = DDP(model)
        _model = model.module
    else:
        _model = model
    model.to(device=device)
    model.eval()
    print(f"model loaded from {path}")
    if dataset is None:
        dataset = CINC2021(train_config, training=False, lazy=False)
    dl = DataLoader(
        dataset=dataset,
        batch_size=256,
        shuffle=False,
        num_workers=4,
        pin_memory=True,
        drop_last=False,
        collate_fn=collate_fn,
    )

    all_scalar_preds = []
    all_bin_preds = []
    all_labels = []

    start = time.time()
    print("start evaluating the model on the train-validation set...")
    with tqdm(total=len(dataset), unit="signals") as pbar:
        for step, (signals, labels) in enumerate(dl):
            signals = signals.to(device=device)
            labels = labels.numpy()
            all_labels.append(labels)

            if torch.cuda.is_available():
                torch.cuda.synchronize()
            preds, bin_preds = _model.inference(signals)
            all_scalar_preds.append(preds)
            all_bin_preds.append(bin_preds)
            # print(f"{step+1}/{len(dl)}", end="\r")
            pbar.update(signals.shape[0])

    all_scalar_preds = np.concatenate(all_scalar_preds, axis=0)
    all_bin_preds = np.concatenate(all_bin_preds, axis=0)
    all_labels = np.concatenate(all_labels, axis=0)
    classes = dl.dataset.all_classes

    print(f"evaluation used {time.time()-start:.2f} seconds")
    print("start computing the confusion matrix from the binary predictions")

    cm_bin = np.zeros((len(classes), len(classes)), dtype="int")
    for idx in range(all_labels.shape[0]):
        lb = set(all_labels[idx].nonzero()[0].tolist())
        bp = set(all_bin_preds[idx].nonzero()[0].tolist())
        for i in lb.intersection(bp):
            cm_bin[i, i] += 1
        for i in bp - lb:
            for j in lb:
                cm_bin[j, i] += 1
        print(f"{idx+1} / {len(all_labels)}", end="\r")
    title = f"""Confusion Matrix - {train_config["cnn_name"].replace("_", "-")}"""
    if len(train_config["special_classes"]) == 0:
        title += " - NCR"
    plot_confusion_matrix(
        cm=cm_bin,
        classes=classes,
        title=title,
        fmts=fmts,
    )

    print("start computing the ``confusion`` matrix from the scalar predictions")
    cm_scalar = {idx: [] for idx in range(len(classes))}

    for idx in range(all_labels.shape[0]):
        for i in all_labels[idx].nonzero()[0]:
            cm_scalar[int(i)].append(all_scalar_preds[idx])
        print(f"{idx+1} / {len(all_labels)}", end="\r")
    scalar_mean = {
        idx: np.mean(np.column_stack(v), axis=1) for idx, v in cm_scalar.items()
    }
    scalar_std = {
        idx: np.std(np.column_stack(v), axis=1) for idx, v in cm_scalar.items()
    }
    cm_scalar_mean = np.zeros((len(classes), len(classes)))
    cm_scalar_std = np.zeros((len(classes), len(classes)))
    for idx, v in scalar_mean.items():
        cm_scalar_mean[idx, ...] = v
    for idx, v in scalar_std.items():
        cm_scalar_std[idx, ...] = v

    title = f"""Mean Scalar Prediction Matrix - {train_config["cnn_name"].replace("_", "-")}"""
    if len(train_config["special_classes"]) == 0:
        title += " - NCR"
    plot_confusion_matrix(
        cm=cm_scalar_mean,
        classes=classes,
        title=title,
        fmts=fmts,
    )

    title = f"""STD Scalar Prediction Matrix - {train_config["cnn_name"].replace("_", "-")}"""
    if len(train_config["special_classes"]) == 0:
        title += " - NCR"
    plot_confusion_matrix(
        cm=cm_scalar_std,
        classes=classes,
        title=title,
        fmts=fmts,
    )

@torch.no_grad()
def test_inference_speed(path: str, dataset: Optional[CINC2021] = None) -> int:
    """ """
    device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
    model, train_config = ECG_CRNN_CINC2021.from_checkpoint(path, device=device)
    model.to(device=device)
    model.eval()
    print(f"model loaded from {path}")
    if dataset is None:
        dataset = CINC2021(train_config, training=False, lazy=False)
    dl = DataLoader(
        dataset=dataset,
        batch_size=64,
        shuffle=False,
        num_workers=4,
        pin_memory=True,
        drop_last=False,
        collate_fn=collate_fn,
    )

    start = time.time()
    print("start evaluating the model...")
    with tqdm(total=len(dataset), unit="signals") as pbar:
        for step, (signals, labels) in enumerate(dl):
            signals = signals.to(device=device)

            if torch.cuda.is_available():
                torch.cuda.synchronize()
            _ = model(signals)
            pbar.update(signals.shape[0])

    speed = round(len(dataset) / (time.time() - start))

    print(f"evaluation used {time.time()-start:.2f} seconds")

    del model
    torch.cuda.empty_cache()

    return speed

def append_model_config_if_needed() -> None:
    """ """
    results_dir = os.path.join(os.path.dirname(BaseCfg.log_dir), "results")
    results_txt_files = get_record_list_recursive3(results_dir, "TorchECG.*\\.txt")
    for fp in results_txt_files:
        fp = os.path.join(results_dir, fp + ".txt")
        with open(fp, "r") as f:
            lines = f.read().splitlines()[-1000:]
        model_fp = None
        flag = False
        for line in lines:
            tmp = re.findall("/.*BestModel.*\\.pth\\.tar", line)
            if len(tmp) > 0:
                model_fp = tmp[0]
            tmp = re.findall("model configurations", line)
            if len(tmp) > 0:
                flag = True
                break
        if flag:
            print(f"{fp} already has model config")
            continue
        if model_fp is None:
            print(f"{fp} does not have corresponding model")
            continue
        ckpt = torch.load(model_fp, map_location=torch.device("cpu"))
        shrinked_cfg = deepcopy(ckpt["model_config"])
        for k in ckpt["model_config"].cnn:
            if k != ckpt["model_config"].cnn.name:
                shrinked_cfg.cnn.pop(k)
        for k in ckpt["model_config"].rnn:
            if k != ckpt["model_config"].rnn.name:
                shrinked_cfg.rnn.pop(k)
        for k in ckpt["model_config"].attn:
            if k != ckpt["model_config"].attn.name:
                shrinked_cfg.attn.pop(k)
        with open(fp, "a") as f:
            f.write(f"\nmodel configurations: {dict_to_str(shrinked_cfg)}\n")
        print(f"{fp} appended with model config")


import pandas as pd
import numpy as np

dates = pd.date_range("20130101", periods=6)
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list("ABCD"))
print(df)

s = pd.Series([1, 3, 5, np.nan, 6, 8], index=dates).shift(2)
print(s)
df = df.sub(s, axis='index')
print(df)

# Apply
# apply function to data - apply following columns
df = df.apply(np.cumsum)
print(df)

df = df.apply(lambda x: x.max() - x.min())
print(df)

# Histogramming
s = pd.Series(np.random.randint(0, 7, size=10))

print(s)
print(s.value_counts())

# String methods
s = pd.Series(["A", "B", "C", "Aaba", "Baca", np.nan, "CABA", "dog", "cat", 1, 2, 3])
print(s)
print(s.str.lower())

# Merge
df = pd.DataFrame(np.random.randn(10 ,4))
print(df)
pieces = [df[:3], df[3:7], df[7:]]

print(pd.concat(pieces))

# JOIn
left = pd.DataFrame({"key":["foo", "foo"], "lval":[1, 2]})
right = pd.DataFrame({"key": ["foo", "foo"], "rval": [4, 5]})

print(pd.merge(left, right, on="key"))

# Grouping
df = pd.DataFrame(
    {
        "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
        "B": ["one", "one", "two", "three", "two", "two", "one", "three"],
        "C": np.random.randn(8),
        "D": np.random.randn(8),
    }
)

print(df)
print(df.groupby("A").sum())
print(df.groupby(["A", "B"]).sum())

# Reshaping

# Stack
tuples = list(
    zip(
        ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
        ["one", "two", "one", "two", "one", "two", "one", "two"],
    )
)
print(tuples)


#!/usr/bin/env python

# Extract Pfam HMMs representing mobility genes or transposable elements.
# Author: Bingxin Lu
# Affiliation : National University of Singapore
# E-mail : bingxin@comp.nus.edu.sg
#
# Input file:
# Pfam-A.hmm, downloaded from ftp site of Pfam
# ftp://ftp.ebi.ac.uk/pub/databases/Pfam/current_release/Pfam-A.hmm.gz
#
# PATTERN:
# less Pfam-A.hmm.dat | grep -E '[Tt]ransposase|[Ii]ntegrase|[Tt]ransposon|[Rr]esolvase|[Rr]elaxase|[Ii]nsertion element|[Rr]ecombinase|Bacterial mobilisation|[Tt]ransposable|Mu\b|[Tt]ransposition'
#
# Sample command:
# python extract_mobgene.py -i Pfam-A.hmm.dat -o mobgene.list
# hmmfetch -f Pfam-A.hmm mobgene.list > Pfam_mobgene.hmm

import os
import optparse
import re

def extract_mob_domain(infile, outfile):
    with open(infile, 'r') as fin, open(outfile, 'w') as fout:
        for line in fin:
            if line.startswith('#=GF AC'):
                fields = line.strip().split()
                acc = fields[-1]

                for line in fin:
                    content = line.strip()
                    if content.startswith('#=GF DE'):
                        pattern = re.compile('[Tt]ransposase|[Ii]ntegrase|[Tt]ransposon|[Rr]esolvase|[Rr]elaxase|[Ii]nsertion element|[Rr]ecombinase|Bacterial mobilisation|[Tt]ransposable|Mu[-\s]|[Tt]ransposition')
                        res = pattern.search(content)
                        if res != None:
                            line = "%s\n" % (acc)
                            fout.write(line)
                        # break to go into next section
                        break

if __name__ == '__main__':
        parser = optparse.OptionParser()
        parser.add_option("-i", "--infile", dest="infile", help="input file of pfam HMM profile")
        parser.add_option("-o", "--outfile", dest="outfile", help="output file for the HMM profile related to mobility genes")
        options, args = parser.parse_args()

        extract_mob_domain(options.infile, options.outfile)


"""@telegraph Utilities
Available Commands:
.telegraph media as reply to a media
.telegraph text as reply to a large text"""
from telethon import events
import os
from PIL import Image
from datetime import datetime
from telegraph import Telegraph, upload_file, exceptions
from userbot.utils import admin_cmd

telegraph = Telegraph()
r = telegraph.create_account(short_name=Config.TELEGRAPH_SHORT_NAME)
auth_url = r["auth_url"]

@borg.on(admin_cmd("telegraph (media|text) ?(.*)"))
async def _(event):
    if event.fwd_from:
        return
    if Config.PRIVATE_GROUP_BOT_API_ID is None:
        await event.edit("Please set the required environment variable `PRIVATE_GROUP_BOT_API_ID` for this plugin to work")
        return
    if not os.path.isdir(Config.TMP_DOWNLOAD_DIRECTORY):
        os.makedirs(Config.TMP_DOWNLOAD_DIRECTORY)
    await borg.send_message(
        Config.PRIVATE_GROUP_BOT_API_ID,
        "Created New Telegraph account {} for the current session. \n**Do not give this url to anyone, even if they say they are from Telegram!**".format(auth_url)
    )
    optional_title = event.pattern_match.group(2)
    if event.reply_to_msg_id:
        start = datetime.now()
        r_message = await event.get_reply_message()
        input_str = event.pattern_match.group(1)
        if input_str == "media":
            downloaded_file_name = await borg.download_media(
                r_message,
                Config.TMP_DOWNLOAD_DIRECTORY
            )
            end = datetime.now()
            ms = (end - start).seconds
            await event.edit("Downloaded to {} in {} seconds.".format(downloaded_file_name, ms))
            if downloaded_file_name.endswith((".webp")):
                resize_image(downloaded_file_name)
            try:
                start = datetime.now()
                media_urls = upload_file(downloaded_file_name)
            except exceptions.TelegraphException as exc:
                await event.edit("ERROR: " + str(exc))
                os.remove(downloaded_file_name)
            else:
                end = datetime.now()
                ms_two = (end - start).seconds
                os.remove(downloaded_file_name)
                await event.edit("Uploaded to https://telegra.ph{} in {} seconds.".format(media_urls[0], (ms + ms_two)), link_preview=True)
        elif input_str == "text":
            user_object = await borg.get_entity(r_message.from_id)
            title_of_page = user_object.first_name # + " " + user_object.last_name
            # apparently, all Users do not have last_name field
            if optional_title:
                title_of_page = optional_title
            page_content = r_message.message
            if r_message.media:
                if page_content != "":
                    title_of_page = page_content
                downloaded_file_name = await borg.download_media(
                    r_message,
                    Config.TMP_DOWNLOAD_DIRECTORY
                )
                m_list = None
                with open(downloaded_file_name, "rb") as fd:
                    m_list = fd.readlines()
                for m in m_list:
                    page_content += m.decode("UTF-8") + "\n"
                os.remove(downloaded_file_name)
            page_content = page_content.replace("\n", "<br>")
            response = telegraph.create_page(
                title_of_page,
                html_content=page_content
            )
            end = datetime.now()
            ms = (end - start).seconds
            await event.edit("Pasted to https://telegra.ph/{} in {} seconds.".format(response["path"], ms), link_preview=True)
    else:
        await event.edit("Reply to a message to get a permanent telegra.ph link. (Inspired by @ControllerBot)")

def resize_image(image):
    im = Image.open(image)
    im.save(image, "PNG")


"""empty message

Revision ID: c2ebca010555
Revises: 539082e1e38a
Create Date: 2018-10-26 13:45:45.547912

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = 'c2ebca010555'
down_revision = '539082e1e38a'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('dk_classify_model',
    sa.Column('classify_id', sa.Integer(), nullable=False),
    sa.Column('model_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['classify_id'], ['dk_classify.id'], ),
    sa.ForeignKeyConstraint(['model_id'], ['dk_goodsmodel.id'], ),
    sa.PrimaryKeyConstraint('classify_id', 'model_id')
    )
    op.drop_constraint('dk_goodsmodel_ibfk_1', 'dk_goodsmodel', type_='foreignkey')
    op.drop_column('dk_goodsmodel', 'classify_id')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('dk_goodsmodel', sa.Column('classify_id', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False))
    op.create_foreign_key('dk_goodsmodel_ibfk_1', 'dk_goodsmodel', 'dk_classify', ['classify_id'], ['id'])
    op.drop_table('dk_classify_model')
    # ### end Alembic commands ###


#!/usr/bin/python3

import gzip , sys , os , Bio.PDB.Polypeptide , Bio.PDB.PDBParser

From = int(sys.argv[1])
To = int(sys.argv[2])

#Collect Structures
os.system('rsync -rlpt -v -z --delete --port=33444 rsync.wwpdb.org::ftp/data/structures/divided/pdb/ ./DATABASE')
current = os.getcwd()
os.mkdir('PDBDatabase')
filelist = os.listdir('DATABASE')
for directories in filelist:
	files = os.listdir(current + '/DATABASE/' + directories)
	for afile in files:
		location = (current + '/DATABASE/' + directories + '/' + afile)
		print(location)
		os.rename(location , current + '/PDBDatabase/' + afile)
os.system('rm -r ./DATABASE')
#Separate Chains
pdbfilelist = os.listdir('PDBDatabase')
for thefile in pdbfilelist:
	#Open File
	TheFile = current + '/PDBDatabase/' + thefile
	TheName = TheFile.split('.')
	#Extract Each Chain and Save as Different Files
	InFile = gzip.open(TheFile, 'rb')
	for line in InFile:
		line = line.decode()
		if line.startswith('ATOM') or line.startswith('ANISOU'):
			chain = line[21]
			Name = TheName[0].split('pdb')
			output = open(Name[1] + '_' + chain + '.pdb' , 'a')
			output.write(line)
			output.close()
	os.remove(TheFile)
#Remove Unwanted Structures
pdbfilelist = os.listdir('PDBDatabase')
for thefile in pdbfilelist:
	TheFile = current + '/PDBDatabase/' + thefile
	structure = Bio.PDB.PDBParser(QUIET=True).get_structure('X' , TheFile)
	ppb = Bio.PDB.Polypeptide.PPBuilder()
	Type = ppb.build_peptides(structure)
	#Delete Non-Protein Files
	if Type == []:
		print('[-] NOT PROTEIN\t' , thefile)
		os.remove(TheFile)
	else:
		#Delete Structures Larger Than 150 or Smaller Than 100 Amino Acids
		length = int(str(Type[0]).split()[2].split('=')[1].split('>')[0])
		if length > To or length < From:
			print('[-] WRONG SIZE\t' , thefile)
			os.remove(TheFile)
		else:
			print('[+] GOOD\t' , thefile)


"""
The purpose of this code is to count the number of ligands for each protein in the bpp_data folder
It can be run using
$ ~/miniconda/bin/python3 protein_ligand_counter.py
"""

import os

if __name__ == '__main__':
    folder = "/scratch/PI/rondror/combind/bpp_data"
    proteins = os.listdir(folder)

    for protein in proteins:
        if protein[0] != '.':
            ligand_folder = folder + "/" + protein + "/docking/grids"
            ligands = os.listdir(ligand_folder)
            print(protein, " has ",len(ligands), " ligands")


# -*- coding: utf-8 -*-
import os
import glob

import numpy as np
import cv2

IMAGE_SIZE = 224
images = []
labels = []

def resize_with_pad(image, height=IMAGE_SIZE, width=IMAGE_SIZE):
    resized_image = cv2.resize(image, (height, width))

    return resized_image

def traverse_dir(path):
    categories = os.listdir(path)
    x = []  # 이미지 데이터
    y = []  # 레이블 데이터

    for idx, cat in enumerate(categories):
        image_dir = path + "/" + cat
        files = glob.glob(image_dir + "/*.jpg")
        print('----', cat, '처리 중')

        for i, f in enumerate(files):
            img = read_image(f)
            x.append(img)
            y.append(idx)
    x = np.array(x)
    y = np.array(y, dtype=np.int)
    print(x)
    print(y)
    return x, y

def read_image(file_path):
    image = cv2.imread(file_path)
    image = resize_with_pad(image, IMAGE_SIZE, IMAGE_SIZE)

    return image

def extract_data(path):
    images, labels = traverse_dir(path)
    images = np.array(images)
    print(labels)

    return images, labels


# This file is responsible for extracting biographical data.
# You need to actively review the extraction results, which will then get saved in a Python array
# There is quite a long wait involved as all of the data is pre-loaded.

import pandas as pd
import wikipedia
import json

# This database is taken from the Vilna Troupe project and can be found at http://vilnatroupe.com
# It is not included in the current dataset for reasons of rights and updatability.
df = pd.read_excel("../current_database.xls")

# The code below parses the original file and saves it as members:
members = df.loc[:,["Full Name"]].values.tolist()
members = [x[0] for x in members if isinstance(x[0], str)]
wiki_biographies = {}
biographies = {}

# Geographies are then extracted as a Python dictionary.
# They can then be added to the data-set as a separate column and displayed.
for member in members:

    try:
        wiki_biography = wikipedia.summary(member)
    except:
        wiki_biography = ''

    if wiki_biography != '':
        wiki_biographies[member] = wiki_biography

for bio in wiki_biographies:
    test = input("\n" +  bio + "###" + "Include for (y/n): " + wiki_biographies[bio] + "\n" + "\n")
    if test == "y":
        biographies[bio] = wiki_biographies[bio]

with open('biographies.json', 'w') as convert_file:
    convert_file.write(json.dumps(biographies))


# -*- coding: utf-8 -*-
"""
    CreatedDate: 2022-03-31
    FileName   : lengthOfLongestSubstring.py
    Author     : Honghe
    Descreption: 剑指 Offer 48. 最长不含重复字符的子字符串
"""

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        if not s:
            return 0
        sub_string = set()
        max_len = 1
        start = 0
        for index,i in enumerate(s):
            if i not in sub_string:
                sub_string.add(i)
                max_len = max(max_len,len(sub_string))
            else:
                i_index = start+s[start:index].find(i)
                sub_string = set(s[i_index+1:index+1])
                start = i_index+1
        return max_len

if __name__ == '__main__':
    sol = Solution()
    s = "umvejcuuk"
    print(sol.lengthOfLongestSubstring(s))


import logging
import hli_python3_utils
import os
from predictions.pre_prediction_processor import PrePredictionProcessor

import torch
import torch.nn.functional as F

from model.vnet3d import VNet_Parallelism

file_utils = hli_python3_utils.client("FileUtils")
logging_utils = hli_python3_utils.client("LoggingUtils")

class Predictor:
    '''
    Performs the actual prediction on the the pre-processed images. Loads the model definition file and runs
    predict on batch.
    '''
    def __init__(self, logging_dir, input_shape, num_classes, model_file_path):
        '''Initializes the predictor.

        :param logging_dir: The logging directory
        :param model_file_path: The path to the local model file
        '''
        self.logger = logging_utils.get_logger(os.path.join(logging_dir, "predictor.log"))
        self.model = VNet_Parallelism(in_channels=1, classes=num_classes)
        self.logger.info(f'model_file_path: {model_file_path}')
        self.model.load_state_dict(torch.load(model_file_path))
        self.logger.info(f'model load success')
        self.model.eval()

    # We'll need to tune how large the predict on batch can be...the more you can fit in memory the faster it will go.
    def predict(self, processed_images):
        '''The main call to the model for the predictions.

        :param processed_images: The pre-processed images
        '''
        self.logger.info(f"precessed images shape is : {processed_images.shape}")
        with torch.no_grad():
            predictions = self.model(torch.from_numpy(processed_images))
            predictions_prob = F.softmax(predictions, dim=1)
        self.logger.info(f"predictions shape: {predictions_prob.shape}")
        return predictions_prob.numpy()


import re

def find_name(soup):
    try:
        name = soup.find(id='something').text
        return name
    except:
        return ''

def find_address(soup):
    try:
        soup.findNext
        el = soup.find(text=re.compile("Dirección"))
        result = el.findNext().get_text(separator=u' ')
        return result
    except:
        return ''


intro = input("enter about yourself: ")
characterCount = 0
wordCount = 1
for i in intro:
    characterCount = characterCount + 1
    if(i==' '):
        wordCount = wordCount + 1
print("number of words: ")
print(wordCount)
print("number of characters in intro")
print(characterCount)


from django.db.models import fields
from rest_framework import serializers
from .models import *

class SubmissionsSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    poll = serializers.SlugRelatedField(
        queryset=Poll.objects.all(), slug_field='id')
    question = serializers.SlugRelatedField(
        queryset=Question.objects.all(), slug_field='id')
    option = serializers.SlugRelatedField(
        queryset=Option.objects.all(), slug_field='id', allow_null=True, required=False)
    option_text = serializers.CharField(
        max_length=200, allow_null=True, required=False)
    userId = serializers.IntegerField()

    class Meta:
        model = Submission
        fields = '__all__'

        def create(self, validated_data):
            print('data', **validated_data)
            return Submission.objects.create(**validated_data)

        def update(self, instance, validated_data):
            for key, value in validated_data.items():
                setattr(instance, key, value)
            instance.save()
            return instance

        def validate(self, attrs):
            question_type = Question.objects.get(
                id=attrs['question'].id).question_type
            try:
                if question_type == "one" or question_type == "text":
                    obj = Submission.objects.get(question=attrs['question'].id, survey=attrs['poll'],
                                                 user_id=attrs['user_id'])
                elif question_type == "multiple":
                    obj = Submission.objects.get(question=attrs['question'].id, survey=attrs['poll'],
                                                 user_id=attrs['user_id'],
                                                 option=attrs['option'])
            except Submission.DoesNotExist:
                return attrs
            else:
                raise serializers.ValidationError('Уже ответил')

class OptionSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    question = serializers.SlugRelatedField(
        queryset=Question.objects.all(), slug_field='id')
    option_text = serializers.CharField(max_length=200)

    def validate(self, attrs):
        try:
            obj = Option.objects.get(
                question=attrs['question'].id,  option_text=attrs['option_text'])
        except Option.DoesNotExist:
            return attrs
        else:
            raise serializers.ValidationError('Выбор уже существует')

    class Meta:
        model = Option
        fields = "__all__"

    def create(self, validated_data):
        return Option.objects.create(**validated_data)

    def update(self, instance, validated_data):
        for key, value in validated_data.items():
            setattr(instance, key, value)
        instance.save()
        return instance

class QuestionSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    poll = serializers.SlugRelatedField(
        queryset=Poll.objects.all(), slug_field='id')
    text_q = serializers.CharField(max_length=200)
    type = serializers.CharField(max_length=200)
    options = OptionSerializer(many=True, read_only=True)

    def validate(self, attrs):
        type = attrs['type']
        if type == 'one' or type == 'multiple' or type == 'text':
            return attrs
        raise serializers.ValidationError(
            'Тип вопроса может быть только один(one), несколько(multiple) или текстовый(text)')

    class Meta:
        model = Question
        fields = "__all__"

    def create(self, validated_data):
        return Question.objects.create(**validated_data)

    def update(self, instance, validated_data):
        for key, value in validated_data.items():
            setattr(instance, key, value)
        instance.save()
        return instance

class PollSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    name = serializers.CharField(max_length=200)
    startDate = serializers.DateField()
    finishDate = serializers.DateField()
    description = serializers.CharField(max_length=200)

    class Meta:
        model = Poll
        fields = "__all__"

    def create(self, validated_data):
        return Poll.objects.create(**validated_data)

    def update(self, instance, validated_data):
        if 'startDate' in validated_data:
            raise serializers.ValidationError(
                {'startDate': 'Вы не можете изменять это поле.'})
        for key, value in validated_data.items():
            setattr(instance, key, value)
        instance.save()
        return instance


"""
Задача №1
Запрограммировать следующее выражение:
(a + b - f / a) + f * a * a - (a + b)
Числа а, b, f вводятся с клавиатуры.
Организовать пользовательский интерфейс, таким образом, чтобы было понятно, в каком порядке должны вводиться числа.
"""

a = float(input('Введите значение для а: '))
b = float(input('Введите значение для b: '))
f = float(input('Введите значение для f: '))

result = (a + b - f  / a) + f * a * a - (a + b)

print ("Результат вычисления выражения (a + b - f / a) + f * a * a - (a + b) равен: ",result)


from django.urls import path
from . import views
from .views import *

app_name = 'penonton'

urlpatterns = [
    path('', views.penontonDashboard, name='penontonDashboard'),
    path('listPertandingan', listPertandingan, name='listPertandingan'),
    path('pilihstadium/',views.pilihStadium,name='pilihStadium'),
    path('listwaktu/<str:nama>/<str:tgl>/',views.listWaktu,name='listWaktu'),
    path('belitiket/<str:id>/',views.beliTiket,name='beliTiket'),
    path('tiketListPertandingan/<str:id>/',views.tiketListPertandingan,name='tiketListPertandingan'),
]


from django.shortcuts import render
from django.http import HttpResponse
from .data import animals, families
import json

# Create your views here.
def display_all_animals(request):

    cleaned_animals = []

    for animal in animals:
        cleaned_animal = animal.copy()
        del cleaned_animal["id"]
        cleaned_animals.append(cleaned_animal)

    result = "\n\n".join(json.dumps(cleaned_animal, indent=4) for cleaned_animal in cleaned_animals)

    return HttpResponse(result, content_type="application/json")

def display_all_families(request):
    animals = list(map(lambda family: f"{family['name']}<br>", families))
    return HttpResponse(animals)

def display_one_animal(request, animal_id):
    if 1 <= animal_id <= len(animals):
        animal = animals[animal_id - 1]
        cleaned_animal = animal.copy()
        del cleaned_animal['id']

        result = json.dumps(cleaned_animal, indent=4)
        return HttpResponse(result, content_type='application/json')
    else:
        return HttpResponse('Animal not found')

def display_animal_per_family(request, family_id):
    animals_in_family = [animal for animal in animals if animal['family'] == family_id]
    return HttpResponse(animals_in_family)


# Find Minimum in Rotated Sorted Array II
# Suppose a sorted array is rotated at some pivot unknown to you beforehand
# (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
# Find the minimum element.
# The array may contain duplicates.
#
# Example
# Given [4,4,5,6,7,0,1,2] return 0
#
#
#

class Solution:
    # @param num: a rotated sorted array
    # @return: the minimum number in the array
    def findMin(self, num):

        if len(num) == 1:
            return num[0]

        start, end = 0, len(num) - 1
        while start + 1 < end:
            mid = start + (end - start) / 2
            if num[mid] > num[end]:
                start = mid
            elif num[mid] < num[end]: ## Have to use 'elif' here, not 'if' !!
                # Because you may change 'end' in the 2nd step and the changed
                # 'end' will also go through step 3.
                end = mid
            else:
                end -= 1 #Only dare to move one step

        # Note!! We have to use (start == 0) here, not (end == len(num)-1)
        # For example [1, 2, 2, 2, 2]
        if (start == 0) and (num[end] >= num[0]):
            return num[0]
        return num[end]

02-Binary-Search-Sorted-Array/14 Find Minimum in Rotated Sorted Array II.py


from __future__ import print_function

import os
import re
import sys

from elodie import constants
from elodie import geolocation
from elodie import log
from elodie.compatability import _decode
from elodie.filesystem import FileSystem
from elodie.localstorage import Db
from elodie.media.base import Base, get_all_subclasses
from elodie.media.media import Media
from elodie.media.text import Text
from elodie.media.audio import Audio
from elodie.media.photo import Photo
from elodie.media.video import Video
from elodie.result import Result

def main(argv):
    filesystem = FileSystem()
    result = Result()
    subclasses = get_all_subclasses()

    paths = argv[1:]

    for path in paths:
        path = os.path.expanduser(path)
        if os.path.isdir(path):
            for source in filesystem.get_all_files(path, None):
                status = add_original_name(source, subclasses)
                result.append((_decode(source), status))
        else:
            status = add_original_name(path, subclasses)
            result.append((_decode(path), status))

    result.write()

def add_original_name(source, subclasses):
    media = Media.get_class_by_file(source, subclasses)
    if media is None:
        print('{} is not a valid media object'.format(source))
        return

    metadata = media.get_metadata()
    if metadata['original_name'] is not None:
        print('{} already has OriginalFileName...Skipping'.format(source))
        return

    original_name = parse_original_name_from_media(metadata)
    return media.set_original_name(original_name)

def parse_original_name_from_media(metadata):
    # 2015-07-23_04-31-12-img_9414-test3.jpg
    base_name = metadata['base_name']
    title = metadata['title']
    extension = metadata['extension']
    date_regex = r'^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}-'
    if not re.match(date_regex, base_name):
        print("File name did not match date pattern...Skipping")
        return

    trimmed_base_name = re.sub(date_regex, '', base_name)
    if title:
        normalized_title = re.sub(r'\W+', '-', title.lower())
        trimmed_base_name = trimmed_base_name.replace(
            '-{}'.format(normalized_title),
            ''
        )

    return '{}.{}'.format(trimmed_base_name, extension)

if __name__ == "__main__":
    main(sys.argv)


from typing import List

class Solution:
    def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
        def dfs(row: int, col: int) -> int:
            if not (0 <= row < row_num and 0 <= col < col_num):
                return 0
            if grid[row][col] != 1:
                return 0

            grid[row][col] = -1
            current_area = 1 + dfs(row+1, col) + dfs(row-1, col) + dfs(row, col+1) + dfs(row, col-1)
            return current_area

        max_area = 0
        row_num, col_num = len(grid), len(grid[0])
        for row in range(row_num):
            for col in range(col_num):
                if grid[row][col] == 1:
                    max_area = max(max_area, dfs(row, col))

        return max_area


from django.shortcuts import render
from openpyxl.styles import PatternFill
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.styles import Color, PatternFill, Font, Border
from openpyxl.styles import colors
from openpyxl.cell import Cell

# Create your views here.
def results(request):
    print("this is a test!")
    path = "C:/Users/foy/Desktop/test.xlsx"
    wb = load_workbook(path)
    sheet_ranges = wb['Sheet1']
    my_sheet_data = SheetData(sheet_ranges['A2'].value, sheet_ranges['B2'].value, sheet_ranges['E2'].value, )
    sheet_ranges['A10'].fill = PatternFill(start_color='FFFF0000',end_color='FFFF0000', fill_type='solid')
    wb.save("C:/Users/foy/Desktop/test.xlsx")
    my_list = []

    for row in sheet_ranges.iter_rows():
        my_sheet_data = SheetData(row[0].value, row[1].value, row[4].value )
        my_list.append(my_sheet_data)
    return render(request, 'results.html',{'my_list':my_list})

class SheetData():
    def __init__(self, name, manager, user_type):
        self.name= name
        self.manager = manager
        self.user_type = user_type


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

from argparse import ArgumentParser
import codecs  # use explicit codec decodes to support utf-8 on Python 2
from collections import Counter, deque
from datetime import datetime, timedelta
from operator import itemgetter
import re
from time import time as clock

# Log line format.
LINE_PATTERN = re.compile(r'(.*) - - \[(.+) -0400\] ["“](.+)["”] (.+) (.+)')

MONTHS = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
          'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}

def parse_timestamp(value):
    """Parse a timestamp formatted as '%d/%b/%Y:%H:%M:%S'.

    This is a faster version of
    datetime.strptime(value, '%d/%b/%Y:%H:%M:%S'), which turns out to
    be quite slow.

    Parameters
    ----------
    value : str
        Timestamp formatted exactly as '%d/%b/%Y:%H:%M:%S'.

    Returns
    -------
    timestamp : datetime.datetime
    """
    return datetime(int(value[7:11]), # %Y
                    MONTHS[value[3:6]], # %m
                    int(value[0:2]), # %d
                    int(value[12:14]), # %H
                    int(value[15:17]), # %M
                    int(value[18:20])) # %s

def parse_line(line):
    """Parse a line of the log file.

    Parameters
    ----------
    line : str
        Single line of the log file.

    Returns
    -------
    host : str
        Host making request.
    timestamp : datetime
        Time of request.
    request : str
        The HTTP request (no quotes).
    code : int
        The HTTP response code.
    nbytes : int
        Number of bytes.
    """
    match = LINE_PATTERN.match(line)
    if not match:
        raise ValueError("unexpected line format: {!r}".format(line))

    host, raw_timestamp, request, code, nbytes = match.groups()
    timestamp = parse_timestamp(raw_timestamp)
    code = int(code)
    nbytes = 0 if nbytes == '-' else int(nbytes)

    return host, timestamp, request, code, nbytes

class TopKDict(dict):
    """Dictionary that only stores up to k entries, sorted by value.

    To take precedence over existing values, new values must be strictly
    greater (``>``). This applies to d[key] = value for an existing key
    in the dictionary as well.

    Examples
    --------

    ```
    >>> d = TopKDict(2)
    >>> d.update({'a': 1, 'b': 2, 'c': 3, 'd': 4})
    >>> d
    {'c': 3, 'd': 4}
    ```

    Dictionary is unchanged for smaller values of existing keys:

    ```
    >>> d['c'] = 2
    >>> d
    {'c': 3, 'd': 4}
    ```

    ... But is updated for larger values:

    ```
    >>> d['c'] = 5
    >>> d
    {'c': 5, 'd': 4}
    ```
    """
    def __init__(self, k):
        self.k = k
        self._sorted_keys = []  # up to length k list of ordered keys.
        dict.__init__(self)

    def __setitem__(self, key, value):
        # if already there, only update if new value is greater.
        if key in self:
            if self[key] < value:
                dict.__setitem__(self, key, value)

        # always add if length is less than k
        elif len(self) < self.k:
            dict.__setitem__(self, key, value)
            self._sorted_keys.append(key)

        # otherwise, only add if value is greater than smallest existing.
        elif value > self[self._sorted_keys[-1]]:
            del self[self._sorted_keys[-1]]
            dict.__setitem__(self, key, value)
            self._sorted_keys[-1] = key
        else:
            return

        # If we got this far, we updated the dictionary and need to re-sort.
        self._sorted_keys.sort(key=lambda x: self[x], reverse=True)

    def update(self, other):
        for k, v in other.items():
            self[k] = v

class BlockList(object):
    """Ledger of recent login failures and blocked hosts.

    This class handles all attempted logins, internally keeping track
    of failures in order to determine whether each new login should be
    blocked or not. The main method is ``BlockList.handle()``.

    Parameters
    ----------
    fail_limit : int
        Number of login failures in ``fail_time`` seconds that will trigger
        blocking.
    fail_time : float
        Time interval (in seconds) over which ``fail_limit`` login failures
        will trigger blocking.
    block_time : float
        Time interval (in seconds) over which to block attempted logins once
        blocking has been triggered for a particular host.
    """

    def __init__(self, fail_limit=3, fail_time=20.0, block_time=300.0):
        self.fail_limit = fail_limit
        self.fail_time = fail_time
        self.block_time = block_time
        self._blocking = {}  # maps currently blocked hosts to time blocked
        self._failures = {}  # maps not-yet-blocked hosts to a list of failed
                             # login times

    def handle(self, host, time, success):
        """Handle login request and determine whether request is blocked.

        Parameters
        ----------
        host : str
            Name of host attempting login.
        time : datetime.datetime
            Timestamp of request.
        success : bool
            Whether or not login was successful.

        Returns
        -------
        is_blocked : bool
            True if request should be blocked, false otherwise.
        """

        # If we're currently blocking this host, check if the time has
        # expired. If so, remove from block list and carry on checking
        # the login. Otherwise, we *are* still blocking and nothing else
        # needs to be checked.
        if host in self._blocking:
            time_since_block = (time - self._blocking[host]).total_seconds()
            if time_since_block > self.block_time:
                del self._blocking[host]
            else:
                return True

        # We're not blocking. If login succeeds, clear previous failures
        # and return.
        if success:
            if host in self._failures:
                del self._failures[host]
            return False

        # If we get here, this is a failed login that is not blocked.
        # We'll log the failure and check if it is over the limit.
        if host not in self._failures:
            self._failures[host] = []
        failures = self._failures[host]

        # Remove failure times not in the last 20 seconds.
        while (len(failures) > 0 and
               (time - failures[0]).total_seconds() > self.fail_time):
            failures.pop(0)

        # Add new failure and check if we are over the limit
        failures.append(time)
        if len(failures) >= self.fail_limit:
            self._blocking[host] = time
            del self._failures[host]

        return False

class Sessions(object):
    """Log of user sessions: request(s) grouped closely in time.

    Parameters
    ----------
    inactive_limit : float, optional
        Maximum number of seconds between requests in the same session
        (exclusive).
    """
    def __init__(self, inactive_limit=1800.):
        self.inactive_limit = inactive_limit
        self._active_sessions = {}  # maps host to [start_time, end_time]

        self._clear_interval = 100000
        self._logs_since_cleared = 0
        # running totals
        self._nsessions = 0
        self._total_length = 0.
        self._max_length = 0

    def _clear_inactive(self, current_time):
        inactive = []
        for host in self._active_sessions:
            duration = self._active_sessions[host]
            idle_time = (current_time - duration[1]).total_seconds()
            if (idle_time >= self.inactive_limit):
                self._save_session(duration)
                inactive.append(host)

        for host in inactive:
            del self._active_sessions[host]

    def _clear_all(self):
        for host in self._active_sessions:
            self._save_session(self._active_sessions[host])
        self._active_sessions = {}

    def log(self, host, time):
        """Log a request.

        Parameters
        ----------
        host : str
            Name of host.
        time : datetime.datetime
            Time of request.
        """
        # purely as a memory optimization, we occasionally clear inactive
        # sessions
        self._logs_since_cleared += 1
        if self._logs_since_cleared > self._clear_interval:
            self._clear_inactive(time)
            self._logs_since_cleared = 0

        if host not in self._active_sessions:
            self._active_sessions[host] = [time, time]
            return

        duration = self._active_sessions[host]
        idle_time = (time - duration[1]).total_seconds()
        if  idle_time >= self.inactive_limit:
            self._save_session(duration)
            duration[0] = duration[1] = time
        else:
            duration[1] = time

    def _save_session(self, duration):
        self._nsessions += 1
        length = (duration[1] - duration[0]).total_seconds()
        self._total_length += length
        if length > self._max_length:
            self._max_length = length

    def summary_statistics(self):
        """Return (number, average_length, maximum_length)."""
        self._clear_all()
        return (self._nsessions,
                self._total_length / self._nsessions,
                self._max_length)

def main(argv=None):
    parser = ArgumentParser(prog="process_log.py",
                            description="Process a log file.")
    parser.add_argument("log", help="Filename of log file to process.")
    parser.add_argument("hosts",
                        help="Output list of top hosts making requests.")
    parser.add_argument("hours", help="Output list of top busiest hours.")
    parser.add_argument("resources",
                        help="Output list of top resources by bandwidth.")
    parser.add_argument("blocked", help="Output log of blocked requests.")
    parser.add_argument("sessions", help="Statistics on session lengths.")
    args = parser.parse_args(argv)

    # Initialize structures that will hold running totals and counts.
    host_requests = Counter()
    resource_bytes = Counter()
    recent_timestamps = deque()
    top_hours = TopKDict(10)
    block_list = BlockList(fail_limit=3, fail_time=20.0, block_time=300.0)
    sessions = Sessions(inactive_limit=1800.0)

    # Open files: we read the log file in binary mode and decode to unicode
    # separately in order to get proper line splits (e.g., avoid splitting on
    # unicode FORMFEED characters that might be in the request).
    # We open the output file of blocked hosts using `codecs` in order to
    # support Python 2.
    log_file = open(args.log, 'rb')
    block_file = codecs.open(args.blocked, 'w', encoding='utf-8')

    # For measuring processing speed.
    nlines = 0
    t0 = clock()

    for rawline in log_file:
        line = codecs.decode(rawline, 'utf-8', 'replace')
        nlines += 1

        host, time, request, code, nbytes = parse_line(line)

        host_requests[host] += 1  # count host requests

        # Update our queue of recent timestamps (in the last hour).
        # For each hour that has now ended, add number of requests in that
        # hour to our dictionary of "top hours."
        while (len(recent_timestamps) > 0 and
               (time - recent_timestamps[0]).total_seconds() > 3600.0):
            n = len(recent_timestamps)
            hour_start = recent_timestamps.popleft()
            top_hours[hour_start] = n

        recent_timestamps.append(time)

        sessions.log(host, time)

        # The other analyses below require us to be able to
        # parse the request and extract a resource. We'll skip these
        # if the request is not well-formatted according to our
        # webserver.
        if code == 400:
            continue
        try:
            words = request.split()
            method = words[0]
            resource = words[1]
        except:
            continue

        # Count resources requested.
        resource_bytes[resource] += nbytes

        # Handle logins; decide whether to block.
        if method == 'POST' and resource == '/login':
            is_blocked = block_list.handle(host, time, code == 200)
            if is_blocked:
                block_file.write(line)

    runtime = clock() - t0
    print("{:d} lines in {:8.3f}s: {:.0f} lines/s"
          .format(nlines, runtime, nlines / runtime))

    log_file.close()
    block_file.close()

    # Clear the queue of recent timestamps.
    while len(recent_timestamps) > 0:
        n = len(recent_timestamps)
        hour_start = recent_timestamps.popleft()
        top_hours[hour_start] = n

    # Output host requests.
    host_requests_tuples = sorted(host_requests.items(), key=itemgetter(0))
    host_requests_tuples.sort(key=itemgetter(1), reverse=True)
    with codecs.open(args.hosts, 'w', encoding='utf-8') as host_file:
        for host, count in host_requests_tuples[0:10]:
            host_file.write("{:s},{:d}\n".format(host, count))

    # Output resource requests.
    resource_bytes_tuples = sorted(resource_bytes.items(), key=itemgetter(0))
    resource_bytes_tuples.sort(key=itemgetter(1), reverse=True)
    with codecs.open(args.resources, 'w', encoding='utf-8') as resource_file:
        for name, _ in resource_bytes_tuples[0:10]:
            resource_file.write("{:s}\n".format(name))

    # Output top hours.
    sorted_top_hours = sorted(top_hours.items(), key=lambda t: t[1],
                              reverse=True)
    with codecs.open(args.hours, 'w', encoding='utf-8') as hours_file:
        for timestamp, counts in sorted_top_hours:
            hours_file.write("{:s} -0400,{:d}\n".format(
                timestamp.strftime("%d/%b/%Y:%H:%M:%S"), counts))

    # Output session statistics
    with codecs.open(args.sessions, 'w', encoding='utf-8') as sessions_file:
        n, avg, maxlen = sessions.summary_statistics()
        sessions_file.write("total sessions: {:d}\n".format(n))
        sessions_file.write("average session: {:.1f}\n".format(avg))
        sessions_file.write("maximum session: {:.1f}\n".format(maxlen))

if __name__ == '__main__':
    main()


from casadi import *
import numpy as NP
import core_do_mpc
import scipy.io
from keras.models import model_from_json
from pdb import set_trace as bp
from vars import *

def model():
    lbub = NP.load('Neural_Network\lbub_60.npy')
    x_lb_NN = lbub[0]
    x_ub_NN = lbub[1]
    y_lb_NN = lbub[2]
    y_ub_NN = lbub[3]

    json_file = open('Neural_Network\model_60.json', 'r')
    model_json = json_file.read()
    json_file.close()
    model = model_from_json(model_json)
    # load weights into new model
    model.load_weights("Neural_Network\model_60.h5")
    Theta = {}
    i = 0
    for j in range(len(model.layers)):
        weights = model.layers[j].get_weights()
        Theta['Theta'+str(i+1)] =  NP.insert(weights[0].transpose(),0,weights[1].transpose(),axis=1)
        i+=1

    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertainties as CasADi symbols

    alpha   = MX.sym("alpha")
    # Define the differential states as CasADi symbols
    # T(t-1) + u_rad(t-1) + u_ahu(t-1) + v_IG(t-1) + u_blinds(t-1) + v(t-1)
    #+ len(zones_Heating) + len(zones_ahu) + len(zones)
    x = MX.sym("x", (numbers-1)*(len(zones)  + 12))
    # Define the control inputs as CasADi symbols
    u_blinds_E = MX.sym("u_blinds_E")
    u_blinds_N = MX.sym("u_blinds_N")
    u_blinds_S = MX.sym("u_blinds_S")
    u_blinds_W = MX.sym("u_blinds_W")

    u = vertcat(u_blinds_E, u_blinds_N, u_blinds_S, u_blinds_W)

    for i in range(len(zones_ahu)):
        tmp = 'u_AHU_' + zones_ahu[i]
        vars()[tmp] = MX.sym(tmp)
        u = vertcat(u,vars()[tmp])

    for i in range(len(zones_Heating)):
        tmp = 'u_rad_' + zones_Heating[i]
        vars()[tmp] = MX.sym(tmp)
        u = vertcat(u,vars()[tmp])
        if i == 0:
            u_rad = MX.sym('u_rad')
            u_rad = vars()[tmp]
        else:
            u_rad = vertcat(u_rad,vars()[tmp])

    # Define time-varying parameters that can chance at each step of the prediction and at each sampling time of the MPC controller. For example, future weather predictions
    for i in range(len(zones)):
        tmp = 'v_IG_' + zones[i]
        vars()[tmp] = MX.sym(tmp)
        if i == 0:
            v = vars()[tmp]
        else:
            v = vertcat(v,vars()[tmp])

    v_Tamb = MX.sym("v_Tamb")
    v_solGlobFac_S = MX.sym("v_solGlobFac_S")
    v_solGlobFac_W = MX.sym("v_solGlobFac_W")
    v_solGlobFac_N = MX.sym("v_solGlobFac_N")
    v_solGlobFac_E = MX.sym("v_solGlobFac_E")
    v_windspeed = MX.sym("v_windspeed")
    v_Hum_amb = MX.sym("v_Hum_amb")
    v_P_amb = MX.sym("v_P_amb")

    u_ahu_ub = MX.sym("u_ahu_ub")
    setp_ub = MX.sym("setp_ub")
    setp_lb = MX.sym("setp_lb")

    v = vertcat(v, v_Tamb, v_solGlobFac_E, v_solGlobFac_N, v_solGlobFac_S, v_solGlobFac_W, v_windspeed, v_Hum_amb, v_P_amb)

    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """
    input = vertcat(x,u,v)
    input = NP.divide(input - x_lb_NN, x_ub_NN - x_lb_NN)

    for i in range(1,len(model.layers)):
        tmp_a = 'a' +  str(i) + '_'
        if i == 1:
            vars()[tmp_a] = input
        vars()[tmp_a] = vertcat(1, vars()[tmp_a])
        tmp_z = 'z' + str(i) + '_'
        vars()[tmp_z] = mtimes(Theta['Theta'+str(i)], vars()[tmp_a])
        tmp_a = 'a' + str(i+1) + '_'
        if i < len(model.layers):
            vars()[tmp_a] = tanh(vars()[tmp_z])
        else:
            vars()[tmp_a] = vars()[tmp_z]

    dT = MX.sym('dT')
    dHeatrate = MX.sym('dHeatrate')
    for i in range(len(zones)  + len(zones_Heating)):
        if i < len(zones):
            tmp = 'dT_' + zones[i]
        else:
            tmp = 'dHeatrate_' + zones_Heating[i-len(zones)]

        vars()[tmp] = MX.sym(tmp)
        vars()[tmp] = vars()[tmp_a][i]
        vars()[tmp] = NP.multiply(vars()[tmp_a][i], y_ub_NN[i]-y_lb_NN[i]) + y_lb_NN[i]

        if i == 0:
            dT = vars()[tmp]
        elif i < len(zones):
            dT = vertcat(dT,vars()[tmp])
        elif i == len(zones):
            dHeatrate = vars()[tmp]
        else:
            dHeatrate = vertcat(dHeatrate,vars()[tmp])

    # dT = vars()[tmp_a][0:14]
    dx = vertcat(dT,u[0:4],v[len(zones):])
    # Concatenate differential states, algebraic states, control inputs and right-hand-sides
    _x = x

    _z = vertcat([])

    _u = u

    _xdot = dx

    _zdot = vertcat([])

    _p = vertcat(alpha)

    _tv_p = vertcat(v, u_ahu_ub ,setp_ub, setp_lb)
    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial condition for the states
    disturbances = NP.load('Neural_Network\disturbances.npy').squeeze()
    x0 = NP.concatenate( (18*NP.ones(len(zones)), NP.zeros(4), disturbances[len(zones):,0] ))
    # No algebraic states
    z0 = NP.array([])
    # Bounds on the states. Use "inf" for unconstrained states

    disturbances_lb = NP.min(disturbances,axis =1)
    disturbances_ub = NP.max(disturbances,axis =1)
    x_lb = NP.concatenate((x_lb_NN[0:len(zones) + 0*len(zones_Heating) + 0*len(zones_ahu)] - 1e-2, NP.zeros(4) ,disturbances_lb[len(zones):]))
    x_ub = NP.concatenate((x_ub_NN[0:len(zones) + 0*len(zones_Heating) + 0*len(zones_ahu)] + 1e-2, NP.ones(4) ,disturbances_ub[len(zones):]))
    # No algebraic states
    z_lb = NP.array([])
    z_ub = NP.array([])

    # Bounds on the control inputs. Use "inf" for unconstrained inputs
    u_lb = NP.array([0, 0, 0, 0])
    u_lb = NP.concatenate((u_lb, NP.zeros(len(zones_ahu)), 16*NP.ones(len(zones_Heating))))
    u_ub = NP.array([1, 1, 1, 1])
    u_ub = NP.concatenate((u_ub, NP.ones(len(zones_ahu)), 22*NP.ones(len(zones_Heating))))
    u0 = NP.concatenate((NP.zeros(4 + len(zones_ahu)), 18*NP.ones(len(zones_Heating))))

    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling = NP.ones(x.shape[0])
    z_scaling = NP.array([])
    u_scaling = NP.ones(4 + len(zones_ahu) + len(zones_Heating))
    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    # cons = vertcat(x[0:len(zones)]-setp_ub, -(x[0:len(zones)]-setp_lb))
    cons = vertcat(x[0]-setp_ub, -(x[0]-setp_lb))
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    # cons_ub = NP.zeros(2*len(zones))
    cons_ub = NP.zeros(2)

    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 1
    # Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = 1e6*NP.ones(2)
    # Maximum violation for the constraints
    maximum_violation = 20*NP.ones(2)

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat()
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])

    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    # Define the cost function
    lterm = mtimes(0.5*NP.ones((dHeatrate.shape[1],dHeatrate.shape[0])) , dHeatrate) + mtimes(10*NP.ones((u_rad.shape[1],u_rad.shape[0])) , u_rad)
    mterm = mtimes(0.5*NP.ones((dHeatrate.shape[1],dHeatrate.shape[0])) , dHeatrate) + mtimes(10*NP.ones((u_rad.shape[1],u_rad.shape[0])) , u_rad)
    # Penalty term for the control movements 1e4, 100
    rterm = NP.concatenate((1e4*NP.ones(4), 1e4*NP.ones(len(zones_ahu)), 20*NP.ones(len(zones_Heating))))
    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
    """
    bp()
    model_dict = {'x':_x,'u': _u, 'rhs':_xdot,'p': _p, 'z':_z, 'aes': _zdot,'x0': x0, 'z0':z0, 'x_lb': x_lb,'x_ub': x_ub, 'z_lb': z_lb,'z_ub': z_ub, 'u0':u0,
    'u_lb':u_lb, 'u_ub':u_ub, 'x_scaling':x_scaling, 'z_scaling':z_scaling, 'u_scaling':u_scaling, 'cons':cons, 'tv_p':_tv_p,
    "cons_ub": cons_ub, 'cons_terminal':cons_terminal, 'cons_terminal_lb': cons_terminal_lb, 'cons_terminal_ub':cons_terminal_ub, 'soft_constraint': soft_constraint, 'penalty_term_cons': penalty_term_cons, 'maximum_violation': maximum_violation, 'mterm': mterm,'lterm':lterm, 'rterm':rterm}
    model = core_do_mpc.model(model_dict)

    return model


# coding=utf-8

car = [
    u"You're looking for a car insurance, we have the following options for you:"
]

detailed_insurance = [
    lambda name: u"%s got you covered. Here are our conditions:" % name
]

extended_detailed_insurance = [
    lambda name: u"The extended %s got you covered in any. Here are the conditions:" % name
]

offer_extended = [
    u"There's also an extended version, in case you want to know about it."
]

quote = [
    lambda url: u"Sure! Here you go: %s" % url,
    lambda url: u"Definitely! %s" % url
]

available = [
    "We got the following types of insurances. Which ones are you interested in?"
]

bye = [
    "Catch you later!",
    "Bye!",
    "Have a nice day!",
    "Cheersy"
]


import os
import DisjointSet

def create_graph(file_name):
    this_folder = os.path.dirname(os.path.abspath(__file__))
    my_file = os.path.join(this_folder, file_name)
    graph_set = set()
    with open(my_file) as f:
        for i, line in enumerate(f):
            if i > 0:
                graph_set.add(int(line.strip().replace(" ", ""), 2))
    return graph_set

def hamming_distance_1_candidates(x):
    bits = 24
    candidates = set()
    for i in range(bits):
        candidates.add(x ^ (1 << i))
    return candidates

def hamming_distance_2_candidates(x):
    bits = 24
    candidates = set()
    for i in range(bits):
        for j in range(i + 1, bits):
            candidates.add(x ^ (1 << i) ^ (1 << j))
    return candidates

def max_spacing_k_clustering_big(graph_set):
    disjoint_set = DisjointSet.DisjointSet()

    # Create sets
    for node in list(graph_set):
        disjoint_set.makeSet(DisjointSet.Node(node))

    # Merge distance 1 nodes
    for node in list(graph_set):
        candidates = hamming_distance_1_candidates(node)
        matches = candidates & graph_set
        for match in list(matches):
            n1 = disjoint_set.nodes[match]
            n2 = disjoint_set.nodes[node]
            disjoint_set.union(n1, n2)

    # Merge distance 2 nodes
    for node in list(graph_set):
        candidates = hamming_distance_2_candidates(node)
        matches = candidates & graph_set
        for match in list(matches):
            n1 = disjoint_set.nodes[match]
            n2 = disjoint_set.nodes[node]
            disjoint_set.union(n1, n2)
    return disjoint_set.connected_components

if __name__ == "__main__":
    graph = create_graph("clustering_big.txt")
    print(max_spacing_k_clustering_big(graph))


import telebot
from telebot import types
import pygame
import random

from bot import bot, send, send_to_dev
from love import love
from numbers import numbers_game, guess
from cities import cities_game, name_city
from words import set_word, words

MODE = 'SELECT'

@bot.message_handler(commands=['start'])
def start(message):
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
    love_button = types.KeyboardButton('❤ Love')
    numbers_game_button = types.KeyboardButton('🔢 Numbers')
    cities_game_button = types.KeyboardButton('🏙 Cities')
    words_game_button = types.KeyboardButton('🔠 Words')
    keyboard.add(love_button, numbers_game_button, cities_game_button, words_game_button)

    bot.send_message(message.chat.id, f'Hello, {message.from_user.first_name}', reply_markup=keyboard )

#@bot.message_handler(func=lambda x: MODE == 'LOVE')

@bot.message_handler(func=lambda x: MODE == 'NUMBERS')
def play_numbers(message):
    global MODE
    if not guess(message):
        MODE = 'SELECT'

@bot.message_handler(func=lambda x: MODE == 'CITIES')
def play_cities(message):
    global MODE
    if not name_city(message):
        MODE = 'SELECT'

@bot.message_handler(func=lambda x: MODE == 'WORDS')
def play_words(message):
    global MODE
    if not words(message):
        MODE = 'SELECT'

@bot.message_handler(content_types=['text'])
def message_processing(message):
    global MODE
    if message.text == '❤ Love':
        love(message)
        MODE = 'LOVE'
    elif message.text == '🔢 Numbers':
        MODE = 'NUMBERS'
        numbers_game(message)
    elif message.text == '🏙 Cities':
        MODE = 'CITIES'
        cities_game(message)
    elif message.text == '🔠 Words':
        MODE = 'WORDS'
        set_word(message)
    else:
        send(message, "I'm not advanced enough to understand this message")
    print(MODE)

bot.polling(none_stop=True)


import pandas as pd
import warnings
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import confusion_matrix, accuracy_score, classification_report, f1_score
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron
from sklearn.neural_network import MLPClassifier
# a lot of warning from tuning hyper params
warnings.filterwarnings("ignore")

out_path = "out/"
benchmark_file = "drugs-performance.txt"

def load_data():
    drug_path = "data/drug200.csv"
    drug_data = pd.read_csv(drug_path)
    return drug_data

def plot_dist(drug_data):
    drug_classes = {}
    key_list = []

    for item in drug_data['Drug']:
        if drug_classes.get(item) is None:
            drug_classes.update({item : len([x for x in drug_data['Drug'] if x == item])})
            key_list.append(item)

    # sorting
    key_list.sort()
    temp = {}
    for k in key_list:
        temp.update({k : drug_classes.get(k)})

    drug_classes = temp

    # plotting to drug-distribution.pdf
    plt.plot(list(drug_classes.keys()), list(drug_classes.values()), 'r')
    plt.xlabel("Classes")
    plt.ylabel("Count")
    plt.savefig(f"{out_path}drug-distribution.pdf")

def analyze_performance(name, params, y_true, y_pred, append=False):
    write_mode = "a" if append else "w"

    confusion = confusion_matrix(y_true, y_pred)
    f1_measure = classification_report(y_true, y_pred, digits=3, zero_division=0)
    f1_score_macro = f1_score(y_true, y_pred, average="macro")
    f1_score_weighted = f1_score(y_true, y_pred, average="weighted")
    acc = accuracy_score(y_true, y_pred)

    with open(f"{out_path}{benchmark_file}", write_mode) as f:
        f.write("=" * 100)
        f.write(f"\n(a)\nModel: {name}\n")
        f.write(f"Params:\n{params}\n")
        f.write(f"\n(b) Confusion Matrix\n{confusion}\n")
        f.write(f"\n(c)\n{f1_measure}\n")
        f.write(f"\n(d) \nAccuracy of model: {acc:.2%}\n")
        f.write(f"Macro-average F1 of model: {f1_score_macro:.2%}\n")
        f.write(f"Weighted-average F1 of model: {f1_score_weighted:.2%}\n")
        f.write("=" * 100)
        f.write("\n")

# Train and predict
def train_and_predict(clf, X_test, X_train, y_train):
    # Training multinomial Naive Bayes
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    return y_pred

def main():
    # Loading dataset
    drug_data = load_data()

    # Plot
    plot_dist(drug_data)

    # Nominal to numerical
    df = pd.get_dummies(drug_data, columns=['Sex'])

    # Ordinal to numerical
    df['BP'].replace({'LOW':1, 'NORMAL':2, 'HIGH':3}, inplace=True)
    df['Cholesterol'].replace({'LOW':1, 'NORMAL':2, 'HIGH':3}, inplace=True)
    df =df[['Age', 'BP', 'Cholesterol','Na_to_K', 'Sex_F', 'Sex_M', 'Drug']] # re-ordering cols

    # splitting
    X = df.drop('Drug', axis=1).values
    y = df['Drug'].values

    # I had reversed these previously...
    # X_test, X_train, y_test, y_train = train_test_split(X, y)
    X_train, X_test, y_train, y_test = train_test_split(X, y)

    # GaussianNB
    clf = GaussianNB()
    y_pred = train_and_predict(clf, X_test, X_train, y_train)
    analyze_performance("GaussianNB", "Default", y_test, y_pred)

    # Base-DT
    clf = DecisionTreeClassifier()
    y_pred = train_and_predict(clf, X_test, X_train, y_train)
    analyze_performance("Base-DT", "Default", y_test, y_pred, True)

    # Top-DT
    dt = DecisionTreeClassifier()
    clf = GridSearchCV(dt,
        param_grid={
            'criterion' : ['gini', 'entropy'],
            'max_depth' : [5, 8],
            'min_samples_split' : [2, 4, 8]
    },cv=2, return_train_score=False)
    clf.fit(X_train, y_train)

    best_dt_params = clf.best_params_

    clf = DecisionTreeClassifier(criterion=best_dt_params['criterion'],
                            max_depth=best_dt_params['max_depth'],
                            min_samples_split=best_dt_params['min_samples_split'])

    y_pred = train_and_predict(clf, X_test, X_train, y_train)
    analyze_performance("Top-DT", str(best_dt_params), y_test, y_pred, True)

    # PER
    clf = Perceptron()
    y_pred = train_and_predict(clf, X_test, X_train, y_train)
    analyze_performance("PER", "Default", y_test, y_pred, True)

    # Base-MLP
    clf = MLPClassifier(hidden_layer_sizes=(100), activation='logistic', solver='sgd')
    y_pred = train_and_predict(clf, X_test, X_train, y_train)
    analyze_performance("Base-MLP", """1 hidden layer of 100 neurons, sigmoid/logistic as activation function,
stochastic gradient descent, and default values for the rest of the parameters.""",
        y_test, y_pred, True)

    # Top-MLP
    mlp = MLPClassifier()
    clf = GridSearchCV(mlp,
                       param_grid={
                       'hidden_layer_sizes' : [(30, 50), (10, 10, 10)],
                       'activation' : ['logistic', 'tanh', 'relu', 'identity'],
                       'solver' : ['sgd', 'adam']
                   })

    clf.fit(X_train, y_train)
    best_mlp_params = clf.best_params_

    clf = MLPClassifier(hidden_layer_sizes=best_mlp_params['hidden_layer_sizes'],
                            activation=best_mlp_params['activation'],
                            solver=best_mlp_params['solver'])
    y_pred = train_and_predict(clf, X_test, X_train, y_train)
    analyze_performance("Top-MLP", str(best_mlp_params), y_test, y_pred, True)

    ###############################################################################
    # Redo steps 6 10 times
    ###############################################################################

    clfs = [GaussianNB(),
            DecisionTreeClassifier(),
            DecisionTreeClassifier(criterion=best_dt_params['criterion'],
                            max_depth=best_dt_params['max_depth'],
                            min_samples_split=best_dt_params['min_samples_split']),
            Perceptron(),
            MLPClassifier(hidden_layer_sizes=(100), activation='logistic', solver='sgd'),
            MLPClassifier(hidden_layer_sizes=best_mlp_params['hidden_layer_sizes'],
                            activation=best_mlp_params['activation'],
                            solver=best_mlp_params['solver'])]

    avg_acc = {}
    avg_acc_std = {}
    avg_macro_avg = {}
    avg_macro_avg_std = {}
    avg_wgt_avg = {}
    avg_wgt_avg_std = {}
    for clf in clfs:
        acc = []
        acc_std = 0
        macro_avg = []
        macro_std_std = 0
        wgt_avg = []
        wgt_std = 0
        for i in range(10):
            y_pred = train_and_predict(clf, X_test, X_train, y_train)
            acc.append(accuracy_score(y_test, y_pred))
            macro_avg.append(f1_score(y_test, y_pred, average="macro"))
            wgt_avg.append(f1_score(y_test, y_pred, average="weighted"))
        avg_acc.update({str(clf) : (sum(acc)/10)})
        avg_acc_std.update({str(clf) : np.std(acc)})
        avg_macro_avg.update({str(clf) : (sum(macro_avg)/10)})
        avg_macro_avg_std.update({str(clf) : np.std(macro_avg)})
        avg_wgt_avg.update({str(clf) : (sum(wgt_avg)/10)})
        avg_wgt_avg_std.update({str(clf) : np.std(wgt_avg)})

    with open(f'{out_path}{benchmark_file}', 'a') as f:
        f.write("#" * 100)
        f.write("\nRunning 10x runs of each models:\n")
        for key in avg_acc:
            f.write(f"\n{key}\n")
            f.write(f"average accuracy: {avg_acc[key]:.2%}\n")
            f.write(f"std accuracy: {avg_acc_std[key]}\n")
            f.write(f"average macro average F1: {avg_macro_avg[key]:.2%}\n")
            f.write(f"std macro average F1: {avg_macro_avg_std[key]}\n")
            f.write(f"average weighted average F1: {avg_wgt_avg[key]:.2%}\n")
            f.write(f"std weighted average F1: {avg_wgt_avg_std[key]}\n")

if __name__ == "__main__":
    main()


import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import Normalizer
from sklearn.svm import SVC
from random import choice

data = np.load('FriendsFaceEmbeddingData.npz')
trainX, trainy, valX, valy = data['arr_0'], data['arr_1'], data['arr_2'], data['arr_3']
# print('Dataset: training examples =', trainX.shape, 'val examples =', valX.shape[0])

in_encoder = Normalizer(norm='l2')
trainX = in_encoder.transform(trainX)
valX = in_encoder.transform(valX)

out_encoder = LabelEncoder()
out_encoder.fit(trainy)
trainy = out_encoder.transform(trainy)
valy = out_encoder.transform(valy)

#fit the model
model = SVC(kernel='linear', probability=True)
model.fit(trainX, trainy)

#predict
yhat_train = model.predict(trainX)
yhat_val = model.predict(valX)

#accuracy
score_train = accuracy_score(trainy, yhat_train)
score_test = accuracy_score(valy, yhat_val)

# print('Accuracy: train = %.3f, test = %.3f' %(score_train*100, score_test*100))

selection = choice([i for i in range(valX.shape[0])])
random_face_pixels = valX[selection]
random_face_emb = valX[selection]
random_face_class = valy[selection]
random_face_name = out_encoder.inverse_transform([random_face_class])
samples = random_face_emb.reshape(-1, random_face_emb.shape[0])
yhat_class = model.predict(samples)
yhat_prob = model.predict_proba(samples)

# get name
class_index = yhat_class[0]
class_probability = yhat_prob[0,class_index] * 100
predict_names = out_encoder.inverse_transform(yhat_class)
# print('Predicted: %s (%.3f)' % (predict_names[0], class_probability))
# print('Expected: %s' % random_face_name[0])


import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D
from keras.callbacks import TensorBoard
import numpy as np
import matplotlib.pyplot as plt

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

train_images = train_images.reshape(60000,28,28,1).astype(np.float32)
train_labels = keras.utils.to_categorical(train_labels, 10).astype(np.float32)

test_images = test_images.reshape(-1,28,28,1).astype(np.float32)
test_labels = keras.utils.to_categorical(test_labels, 10).astype(np.float32)

#create model
model = Sequential()
#add layers
#first layer: 5x5 filter, stride = 1, no padding, output (channels, height, width): (3,24,24)
model.add(Conv2D(filters=3, kernel_size=5, strides = (1,1), padding='same', input_shape=(28, 28, 1), data_format='channels_last', activation='relu'))
#second layer: stride = 2, output: (3,12,12)
model.add(MaxPooling2D(pool_size=(2,2), strides=2, input_shape = (24, 24, 3), data_format='channels_last'))
#third layer: 3x3 filter, stride = 1, no padding, output (channels, height, width): (3,12,12)
model.add(Conv2D(filters=3, kernel_size=3, strides = 1, padding='valid', input_shape = (12, 12, 3), data_format='channels_last', activation='relu'))
#fourth layer: stride = 2, output: (3,6,6)
model.add(MaxPooling2D(pool_size=(2,2), strides=2, input_shape = (6, 6, 3), data_format='channels_last'))
#flatten for fully-connected layer and add the fully-connected layers
model.add(Flatten())
model.add(Dense(100, input_shape=(108,), activation='relu'))
model.add(Dense(50, input_shape=(100,), activation='relu'))
model.add(Dense(10, input_shape=(50,), activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics = ['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, verbose=2)
print("training finished")

loss_values = history.history['loss']
epochs = range(1, len(loss_values)+1)

loss_acc = model.evaluate(test_images, test_labels, verbose=0)
print("\nTest data loss = %0.4f  accuracy = %0.2f%%" % \
  (loss_acc[0], loss_acc[1]*100) )
# print model.summary

plt.plot(epochs, loss_values, label='Training Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()


"""
for i in range (1,100) :
    for j in range(1, i) :
        ship = j // 10
        ill = j % 10

        if i == j + ship + ill :
            print(i)

    # 생성자가 있는 경우
"""

arr = set(range(1, 10001))  # 중복 값을 제거하고 1 ~ 10000까지 수를 담음
num = set()

for i in range(1, 10001) :
    for j in str(i) : # i의 값을 나누기 위해 str로 형변환 / i = '8', '5', '0'
        i += int(j) # i = i + int(j) >> 850 + 8 >> 858 + 5 >> 863 + 0 >> 863
    num.add(i) # 863

# num : set 형
self_num = sorted(arr - num) # 1 ~ 10001사이에 num에 들어있는 값들은 빼고 저장
# sorted : 해당 값을 리스트형에 오름차순으로 저장
# self_num : list형태
for i in self_num :
    print(i)


"""Synthesizes speech from the input string of text or ssml.

Note: ssml must be well-formed according to:
    https://www.w3.org/TR/speech-synthesis/
"""
from google.cloud import texttospeech
from ..python_prog.splitter import TextSplitter

def determine_language(term):
    try:
        words = TextSplitter().split_text(term, 1)
        words = "".join(words)
        eng_abc_range = range(65, 123)
        ukr_abc_range = range(1028, 1170)
        chars_ukr = 0
        chars_eng = 0
        for c in words:
            if ord(c) in eng_abc_range:
                chars_eng += 1
            elif ord(c) in ukr_abc_range:
                chars_ukr += 1
        if chars_ukr > 0:
            return "uk-UA"
        else:
            return "en-US"
    except ValueError:
        return "en-US"

def convert_text_to_speech(text, output_file):
    # Instantiates a client
    client = texttospeech.TextToSpeechClient()

    # Set the text input to be synthesized
    synthesis_input = texttospeech.types.SynthesisInput(text=text)

    # Build the voice request, select the language code and the ssml
    # voice gender ("neutral")
    language = determine_language(text)
    voice = texttospeech.types.VoiceSelectionParams(
        language_code=language,
        ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)

    # Select the type of audio file you want returned
    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.MP3)

    # Perform the text-to-speech request on the text input with the selected
    # voice parameters and audio file type
    response = client.synthesize_speech(synthesis_input, voice, audio_config)

    # The response's audio_content is binary.
    with open(output_file, 'wb') as out:
        # Write the response to the output file.
        out.write(response.audio_content)
        print('Audio content written to file "{}"'.format(output_file))


#IMPORTACIÓN DE LIBRERIAS
import tensorflow as tf
import os

#QUITAR LOS MENSAJES DE AVISO
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

#CREAR EL MODELO
#Entradas
A = tf.constant([4], tf.int32, name='A')
B = tf.constant([5], tf.int32, name='B')
C = tf.constant([6], tf.int32, name='C')
x = tf.placeholder(tf.int32, name='x')

#CREAR OPERACIONES
#y1= Ax^2 + Bx + C
with tf.name_scope("Ecuacion_1"):
	Ax2 = tf.multiply(A, tf.pow(x, 2), name="Ax2")
	Bx = tf.multiply(B, x, name="Bx")
	y1 = tf.add_n([Ax2, Bx, C], name="suma1")

#y2 = A + Bx + Cx^2
with tf.name_scope("Ecuacion_2"):
    Bx = tf.multiply(B, x, name="Bx")
    Cx2 = tf.multiply(C, tf.pow(x, 2), name="Cx2")
    y2 = tf.add_n([A, Bx, Cx2], name="suma2")

#y = y1 + y2
with tf.name_scope("Suma_final"):
	y = y1 + y2

#INICIAR SESIÓN
with tf.Session() as sesion:

    #Ejecución operaciones
    for i in range(10):
        print ("Para x={} -->{}".format(i,sesion.run(y, feed_dict={x: [i]})))

    #Creación del grafo
    writer = tf.summary.FileWriter('/grafo_niveles', sesion.graph)
    print("Guardados los datos para ver el grafo")
    writer.close()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# AnPyLar documentation build configuration file, created by
# sphinx-quickstart on Thu Nov 16 21:52:35 2017.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# sys.path.insert(0, os.path.abspath('.'))

import os.path
import sys

sys.path.insert(0, os.path.abspath('../../anpylar'))
sys.path.insert(0, os.path.abspath('../brython'))
if True:  # pep-8 warning avoidance
    import browser  # trigger brython/browser documentation noops for anpylar

    # Autodoc hack for observable_operators/sources to correctly show
    # parameters and docstrings with the proper usage name
    import collections
    from inspect import signature
    import types

    from anpylar import Observable
    import anpylar.observable_sources

    add_module_names = False

    # Document observable operators
    obs = {}
    for opname, cls in Observable._dynamicmethods.items():
        def funcx():
            pass

        s = signature(cls.__init__)
        newparams = list(s.parameters.values())[1:]
        funcx.__signature__ = s.replace(parameters=newparams)
        funcx.__doc__ = cls.__doc__
        obs[opname] = funcx

    obsops = types.ModuleType('obs_ops', 'Observable Operators')
    obsops.__dict__.update(obs)
    sys.modules['anpylar.obs_ops'] = obsops

    # Document observable sources
    obs = {}
    for srcname in Observable._staticmethods:
        def funcx():
            pass

        recapname = '_'.join(x.capitalize() for x in srcname.split('_'))
        name = recapname + '_Source'
        cls = getattr(anpylar.observable_sources, name)

        s = signature(cls.__init__)
        newparams = list(s.parameters.values())[1:]
        funcx.__signature__ = s.replace(parameters=newparams)
        funcx.__doc__ = cls.__doc__
        obs[srcname] = funcx

    obssrc = types.ModuleType('obs_src', 'Observable Sources')
    obssrc.__dict__.update(obs)
    sys.modules['anpylar.obs_src'] = obssrc

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

def setup(app):
    app.add_stylesheet('css/custom.css')  # may also be an URL

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.todo',
    'sphinx_tabs.tabs',
    # 'sphinxcontrib.images',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = 'AnPyLar'
copyright = '2018 The AnPyLar Team'
author = 'AnPyLar'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '1.1.5'
# The full version, including alpha/beta/rc tags.
release = ''

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
# html_theme = 'alabaster'
html_theme = 'sphinx_rtd_theme'
html_show_sourcelink = True
html_show_sphinx = False

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#
html_theme_options = {
    'navigation_depth': 3,
}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# -- Options for HTMLHelp output ------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'AnPyLardoc'

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    #
    # 'papersize': 'letterpaper',

    # The font size ('10pt', '11pt' or '12pt').
    #
    # 'pointsize': '10pt',

    # Additional stuff for the LaTeX preamble.
    #
    # 'preamble': '',

    # Latex figure (float) alignment
    #
    # 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (master_doc, 'AnPyLar.tex', 'AnPyLar Documentation',
     'AnPyLar', 'manual'),
]

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'anpylar', 'AnPyLar Documentation',
     [author], 1)
]

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (master_doc, 'AnPyLar', 'AnPyLar Documentation',
     author, 'AnPyLar', 'One line description of project.',
     'Miscellaneous'),
]

images_config = {
    'default_image_width': 'auto',
}