# vim: set ts=8 sts=4 sw=4 tw=99 et:
#
# This file is part of AMBuild.
#
# AMBuild is free software: you can Headeristribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# AMBuild is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AMBuild. If not, see <http://www.gnu.org/licenses/>.
import collections
import os
import re
import subprocess
from ambuild2 import util
from ambuild2.frontend.version import Version
from ambuild2.frontend import paths

# Helper for building AddCommand() invocations for protoc.
class ProtocRunner(object):
    def __init__(self, protoc, builder, includes):
        self.protoc = protoc
        self.builder = builder

        self.argv = [self.protoc.path] + self.protoc.extra_argv
        self.seen_languages = set()

        for include in self.protoc.includes + includes:
            if not os.path.isabs(include):
                include = os.path.join(builder.currentSourcePath, include)
            self.argv += ['--proto_path={}'.format(include)]

        self.languages = collections.OrderedDict()
        self.gen_map = {}

    def AddOutput(self, language, folder):
        if language in self.languages:
            raise Exception('Output language {} specified twice'.format(language))

        self.languages[language] = {
            'folder': folder,
        }
        self.gen_map[language] = {}

        if folder is None:
            folder_path = '.'
        else:
            folder_path = folder.path

        self.argv += ['--{}_out={}'.format(language, folder_path)]

    def AddSource(self, source_path):
        source_name = os.path.basename(source_path)
        if source_name.endswith('.proto'):
            proto_name = source_name[:-len('.proto')]
        else:
            proto_name = source_name

        gen_file_list = []
        gen_file_map = {}
        for language in self.languages:
            folder = self.languages[language]['folder']

            gen_info = gen_file_map.setdefault(language, {
                'sources': 0,
                'headers': 0,
            })
            gen_source_names = []
            gen_header_names = []

            if language == 'python':
                if '.' in proto_name:
                    # This is not supported since it complicates folder entry tracking.
                    raise Exception(
                        'Python proto files cannot contain extra "." characters: {}'.format(
                            proto_name))
                gen_source_names += ['{}_pb2.py'.format(proto_name)]
            elif language == 'cpp':
                gen_source_names += ['{}.pb.cc'.format(proto_name)]
                gen_header_names += ['{}.pb.h'.format(proto_name)]
            else:
                raise Exception('Language not supported yet: {}'.format(language))

            for file in gen_source_names + gen_header_names:
                gen_file_list += [paths.Join(folder, file)]

            gen_info['sources'] += len(gen_source_names)
            gen_info['headers'] += len(gen_header_names)

        dep_file = paths.Join(folder, '{}.d'.format(source_name))
        argv = self.argv + [
            '--dependency_out={}'.format(dep_file),
            source_path,
        ]

        gen_entries = self.builder.AddCommand(inputs = [source_path],
                                              argv = argv,
                                              outputs = gen_file_list,
                                              dep_type = 'md',
                                              dep_file = dep_file,
                                              shared_outputs = [dep_file],
                                              folder = None)

        # Translate the list of generated output entries.
        cursor = 0
        for language in self.languages:
            gen_info = gen_file_map[language]
            gen_sources = gen_entries[cursor:cursor + gen_info['sources']]
            cursor += len(gen_sources)

            gen_headers = gen_entries[cursor:cursor + gen_info['headers']]
            cursor += len(gen_headers)

            self.gen_map[language].setdefault('sources', []).extend(gen_sources)
            if gen_headers:
                self.gen_map[language].setdefault('headers', []).extend(gen_headers)

        # Should be one entry remaining, for the .d file.
        assert (cursor == len(gen_entries))

# Named tuple for protoc.StaticLibrary() results.
class ProtocCppNode(object):
    def __init__(self, lib, headers):
        self.lib = lib
        self.headers = headers

# Like cpp.Compiler, but for protobufs.
class Protoc(object):
    def __init__(self, path, name, version):
        super(Protoc, self).__init__()
        self.path = path
        self.name = name
        self.version = version
        self.extra_argv = []
        self.includes = []

    def clone(self):
        clone = Protoc(self.path, self.name, self.version)
        clone.extra_argv = self.extra_argv[:]
        clone.includes = self.includes[:]
        return clone

    # Each output entry is either a language, or a tuple of (language, folder_entry).
    def Generate(self, builder, sources, outputs, includes = []):
        runner = ProtocRunner(self, builder, includes + ['.'])

        if not outputs:
            raise Exception('No output languages were specified')

        # Add outputs for each language, tracking which generated files we expect.
        for output in outputs:
            if type(output) is tuple:
                language, folder = output
            else:
                language, folder = (output, builder.localFolder)
            runner.AddOutput(language, folder)

        # Add sources. Fixup relative paths since we don't run in the source dir.
        for source in sources:
            if not os.path.isabs(source):
                source = os.path.join(builder.currentSourcePath, source)
            runner.AddSource(source)

        return runner.gen_map

    def StaticLibrary(self, name, builder, cxx, sources, includes = []):
        gen_map = self.Generate(builder, sources, ['cpp'], includes)
        binary = cxx.StaticLibrary(name)
        binary.compiler.sourcedeps += gen_map['cpp']['sources']
        binary.compiler.sourcedeps += gen_map['cpp']['headers']
        for source in gen_map['cpp']['sources']:
            binary.sources += [os.path.join(builder.buildPath, source.path)]
        out = builder.Add(binary)
        return ProtocCppNode(out, gen_map['cpp']['headers'])

FoundProtocMap = set()

def DetectProtoc(**kwargs):
    path = kwargs.pop('path', None)
    if len(kwargs):
        raise Exception('Unknown argument: {}'.format(kwargs.items()[0]))

    if path is None:
        path = 'protoc'

    argv = [path, '--version']
    p = util.CreateProcess(argv)
    if p is None:
        raise Exception('Failed to find protobuf compiler {}'.format(path))
    if util.WaitForProcess(p) != 0:
        raise Exception('Failed to run protoc: {}'.format(p.returncode))

    text = p.stdoutText.strip()
    parts = text.split(' ')
    name = parts[0]
    version = Version(parts[1])

    if path not in FoundProtocMap:
        util.con_out(util.ConsoleHeader, 'found protoc {}-{}'.format(name, version))
        FoundProtocMap.add(path)

    return Protoc(path, name, version)

class ProtocTool(object):
    def __init__(self):
        pass

    def evaluate(self, cmd):
        gen_map = cmd.data.protoc.Generate(builder = cmd.context,
                                           sources = cmd.data.sources,
                                           outputs = [('cpp', cmd.localFolderNode)])
        cmd.sourcedeps += gen_map['cpp']['sources']
        cmd.sourcedeps += gen_map['cpp']['headers']
        for source in gen_map['cpp']['sources']:
            cmd.sources += [os.path.join(cmd.context.buildPath, source.path)]

class ProtocJob(object):
    def __init__(self, protoc = None, sources = []):
        if protoc is None:
            self.protoc = DetectProtoc()
        else:
            self.protoc = protoc.clone()
        self.sources = sources[:]
        self.tool = ProtocTool()


import numpy as np
#import pandas as pd
#import nltk
#import sklearn
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

#data_test = pd.read_csv("./Wine/wine_test.csv")
#data_train = pd.read_csv("./Wine/wine_train.csv")

data_test = np.loadtxt("./Wine/wine_test.csv", delimiter=";", unpack=True, skiprows=1).T
data_train = np.loadtxt("./Wine/wine_train.csv", delimiter=";", unpack=True, skiprows=1).T
X_train = []
Y_train = []

for line in data_train:
    print(len(line))
    Y_train.append(line[-1]) # last item is output
    X_train.append(line[:-1]) # all but last

print(X_train[0])

print(type(X_train[0]))
lin_reg = LinearRegression()
lin_reg.fit(X_train, Y_train)

print("Trained!")

test_index = 300

X_test_1 = data_test[test_index][:-1].reshape(1, -1)
print(X_test_1)

res_real = [Y for Y in (data_test[i][-1] for i in range(len(data_test)))]
res_predict = [lin_reg.predict(line[:-1].reshape(1, -1)) for line in data_test]

for i in range(200,210):
    print(res_real[i], res_predict[i])

res_diff = []
for i in range(len(res_real)):
    res_diff.append(res_real[i] - res_predict[i])

#plt.scatter(res_predict)
#plt.scatter(res_real)
plt.plot(res_diff)
plt.show()


import numpy as np
import pandas as pd
from tqdm import tqdm
import json

from glob import glob
import os

from sklearn.model_selection import train_test_split
import albumentations as A

import cv2
from easydict import EasyDict

def label_preprocessing(path):
    #     path = os.path.join(path, 'train.csv')
    labels = pd.read_csv(path)

    cnt = 0
    label_encoder = {}
    for i, label in enumerate(tqdm(sorted(labels['label']))):

        if label not in label_encoder.values():
            label_encoder[cnt] = label
            cnt += 1

    label_decoder = {val: key for key, val in label_encoder.items()}

    return label_encoder, label_decoder

def csv_feature_dict(path, csv_features):
    csv_files = sorted(glob(os.path.join(path, '*/*.csv')))

    temp_csv = pd.read_csv(csv_files[0])[csv_features]
    max_arr, min_arr = temp_csv.max().to_numpy(), temp_csv.min().to_numpy()

    # feature 별 최대값, 최솟값 계산
    for csv in tqdm(csv_files[1:]):
        temp_csv = pd.read_csv(csv)[csv_features]
        temp_csv = temp_csv.replace('-', np.nan).dropna()
        if len(temp_csv) == 0:
            continue
        temp_csv = temp_csv.astype(float)
        temp_max, temp_min = temp_csv.max().to_numpy(), temp_csv.min().to_numpy()
        max_arr = np.max([max_arr, temp_max], axis=0)
        min_arr = np.min([min_arr, temp_min], axis=0)

    # feature 별 최대값, 최솟값 dictionary return
    return {csv_features[i]: [min_arr[i], max_arr[i]] for i in range(len(csv_features))}

def data_split(path, label_decoder, kfold=False, mode="train", test_size=0.2):
    imgs = glob(os.path.join(path, '*/*.jpg'))

    if mode == "train":
        json_files = glob(os.path.join(path, '*/*.json'))

        label_list = []
        for json_path in tqdm(json_files):
            json_file = json.load(open(json_path, 'r'))

            crop = json_file["annotations"]["crop"]
            disease = json_file["annotations"]["disease"]
            risk = json_file["annotations"]["risk"]

            label = f'{crop}_{disease}_{risk}'
            label_list.append(label_decoder[label])

        if kfold:
            return imgs, label_list
        else:
            return train_test_split(imgs, test_size=test_size, shuffle=True, stratify=label_list)

    else:
        return imgs

def transform(size=224):
    train_transforms = A.Compose([
        A.Resize(size, size),
        A.OneOf([
            A.Rotate(),
            A.HorizontalFlip(),
            A.VerticalFlip()
        ], p=1)
    ])

    val_transforms = A.Compose([
        A.Resize(size, size)
    ])

    return train_transforms, val_transforms

def rand_bbox(size, lam):
    W = size[2]
    H = size[3]
    cut_rat = np.sqrt(1. - lam)
    cut_w = int(W * cut_rat)
    cut_h = int(H * cut_rat)

    # uniform
    cx = np.random.randint(W)
    cy = np.random.randint(H)

    bbx1 = np.clip(cx - cut_w // 2, 0, W)
    bby1 = np.clip(cy - cut_h // 2, 0, H)
    bbx2 = np.clip(cx + cut_w // 2, 0, W)
    bby2 = np.clip(cy + cut_h // 2, 0, H)

    return bbx1, bby1, bbx2, bby2

def softvoting(models, item, opt):
    img = item['img'].to(opt.device)
    seq_len = item['seq_len'].to(opt.device)
    csv_feature = item['csv_feature'].to(opt.device)

    predicts = torch.zeros(img.size(0), opt.num_classes)
    with torch.no_grad():
        for idx, model in enumerate(models):
            output = model(img, csv_feature, seq_len)

            output = F.softmax(output.cpu(), dim=1)
            predicts += output

    # 둘다 값은 똑같이 나옴.
    # pred_avg = predicts / len(models)
    # answer = pred_avg.argmax(dim=-1)
    # _, answer2 = torch.max(pred_avg, 1)

    return predicts.detach().cpu() / len(models)

def hardvoting(models, item, opt):
    img = item['img'].to(opt.device)
    seq_len = item['seq_len'].to(opt.device)
    csv_feature = item['csv_feature'].to(opt.device)

    predicts = torch.zeros(img.size(0), opt.num_classes)
    results = {}
    tmp_vals = [0] * 16
    tmp_inds = [0] * 16
    with torch.no_grad():
        for idx, model in enumerate(models):

            outputs = model(img, csv_feature, seq_len)

            outputs = F.softmax(outputs.cpu(), dim=1)

            vals, indices = torch.max(outputs, 1)

            for i, (val, idx) in enumerate(zip(vals, indices)):
                if tmp_vals[i] < val:
                    tmp_vals[i] = val.item()
                    tmp_inds[i] = idx.item()

            #             print(tmp_vals)
            #             print(tmp_inds)

            predicts += outputs

    # 둘다 값은 똑같이 나옴.
    # pred_avg = predicts / len(models)
    # answer = pred_avg.argmax(dim=-1)
    # _, answer2 = torch.max(pred_avg, 1)

    return predicts.detach().cpu() / len(models)

def VillageLabel_preprocessing(path) :
    public_dataset_folder = os.listdir(path)

    label_encoder = {}
    for idx, data_name in enumerate(public_dataset_folder) :
        label_encoder[idx] = data_name

    label_decoder = {val:key for key, val in label_encoder.items()}
    return label_encoder, label_decoder

def VillageData_split(path, label_decoder, test_size=0.2):
    imgs = glob(os.path.join(path, '*/*.jpg'))
    label_list = [label_decoder[img_path.split('\\')[-2]] for img_path in imgs]

    return train_test_split(imgs, test_size=test_size, shuffle=True, stratify=label_list)


import torch
import torch.nn as nn

class ResnetBlockConv1d(nn.Module):
    """ 1D-Convolutional ResNet block class.
    Args:
        size_in (int): input dimension
        size_out (int): output dimension
        size_h (int): hidden dimension
    """

    def __init__(self, c_dim, size_in, size_h=None, size_out=None,
                 norm_method='batch_norm', legacy=False):
        super().__init__()
        # Attributes
        if size_h is None:
            size_h = size_in
        if size_out is None:
            size_out = size_in

        self.size_in = size_in
        self.size_h = size_h
        self.size_out = size_out
        # Submodules
        if norm_method == 'batch_norm':
            norm = nn.BatchNorm1d
        elif norm_method == 'sync_batch_norm':
            norm = nn.SyncBatchNorm
        else:
             raise Exception("Invalid norm method: %s" % norm_method)

        self.bn_0 = norm(size_in)
        self.bn_1 = norm(size_h)

        self.fc_0 = nn.Conv1d(size_in, size_h, 1)
        self.fc_1 = nn.Conv1d(size_h, size_out, 1)
        self.fc_c = nn.Conv1d(c_dim, size_out, 1)
        self.actvn = nn.ReLU()

        if size_in == size_out:
            self.shortcut = None
        else:
            self.shortcut = nn.Conv1d(size_in, size_out, 1, bias=False)

        # Initialization
        nn.init.zeros_(self.fc_1.weight)

    def forward(self, x, c):
        net = self.fc_0(self.actvn(self.bn_0(x)))
        dx = self.fc_1(self.actvn(self.bn_1(net)))

        if self.shortcut is not None:
            x_s = self.shortcut(x)
        else:
            x_s = x

        out = x_s + dx + self.fc_c(c)

        return out

class Decoder(nn.Module):
    """ Decoder conditioned by adding.

    Example configuration:
        z_dim: 128
        hidden_size: 256
        n_blocks: 5
        out_dim: 3  # we are outputting the gradient
        sigma_condition: True
        xyz_condition: True
    """
    def __init__(self, _, cfg):
        super().__init__()
        self.cfg = cfg
        self.z_dim = z_dim = cfg.z_dim
        self.dim = dim = cfg.dim
        self.out_dim = out_dim = cfg.out_dim
        self.hidden_size = hidden_size = cfg.hidden_size
        self.n_blocks = n_blocks = cfg.n_blocks

        # Input = Conditional = zdim (shape) + dim (xyz) + 1 (sigma)
        c_dim = z_dim + dim + 1
        self.conv_p = nn.Conv1d(c_dim, hidden_size, 1)
        self.blocks = nn.ModuleList([
            ResnetBlockConv1d(c_dim, hidden_size) for _ in range(n_blocks)
        ])
        self.bn_out = nn.BatchNorm1d(hidden_size)
        self.conv_out = nn.Conv1d(hidden_size, out_dim, 1)
        self.actvn_out = nn.ReLU()

    # This should have the same signature as the sig condition one
    def forward(self, x, c):
        """
        :param x: (bs, npoints, self.dim) Input coordinate (xyz)
        :param c: (bs, self.zdim + 1) Shape latent code + sigma
        :return: (bs, npoints, self.dim) Gradient (self.dim dimension)
        """
        p = x.transpose(1, 2)  # (bs, dim, n_points)
        batch_size, D, num_points = p.size()

        c_expand = c.unsqueeze(2).expand(-1, -1, num_points)
        c_xyz = torch.cat([p, c_expand], dim=1)
        net = self.conv_p(c_xyz)
        for block in self.blocks:
            net = block(net, c_xyz)
        out = self.conv_out(self.actvn_out(self.bn_out(net))).transpose(1, 2)
        return out


from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from .models import Asignatura, Calendario, Categoria, Curso, Pod, ProfesorCurso

# Register your models here.

admin.site.register(Asignatura)
admin.site.register(Categoria)
admin.site.register(Pod)

@admin.register(Curso)
class CursoAdmin(admin.ModelAdmin):
    list_display = ('id', 'curso_academico', 'nombre', 'id_nk', 'estado')
    list_filter = ('anyo_academico', 'estado')
    fields = (
        'nombre',
        'categoria',
        'estado',
        'fecha_solicitud',
        'motivo_solicitud',
        'comentarios',
    )
    ordering = ('estado', 'nombre')
    readonly_fields = ('estado', 'fecha_solicitud', 'motivo_solicitud', 'comentarios')

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'categoria':
            nombres = Categoria.NO_REGLADAS + ('Varios',)
            kwargs['queryset'] = Categoria.objects.filter(
                anyo_academico=Calendario.objects.get(slug='actual').anyo, nombre__in=nombres
            ).order_by('nombre')
        return super().formfield_for_foreignkey(db_field, request, **kwargs)

@admin.register(ProfesorCurso)
class ProfesorCursoAdmin(admin.ModelAdmin):
    fields = ('profesor', 'curso', 'fecha_alta', 'fecha_baja')
    model = ProfesorCurso
    list_display = ('profesor', 'nombre_profesor', 'curso')
    # list_filter = ('profesor', 'curso')
    ordering = ('profesor', 'curso')
    readonly_fields = ('profesor', 'curso', 'fecha_alta')

    def nombre_profesor(self, obj):
        return obj.profesor.full_name

admin.site.site_header = _('Administración de Geoda')
admin.site.site_title = _('Administración de Geoda')
admin.site.index_title = _('Inicio')


import torch
import clip
from PIL import Image
from datetime import datetime
import pandas as pd

def save_results_to_csv(df, batch_number):
    csv_filename = f"csvs/img_predictions_batch_{batch_number}.csv"
    df.to_csv(csv_filename, index=False)
    print(f"Saved results for batch {batch_number} to {csv_filename}")

def predict(df, categories, batch_size=100, save_every=1000):
    device = "cuda" if torch.cuda.is_available() else "cpu"

    # Load clip model
    model, preprocess = clip.load("ViT-B/32", device=device)

    text = clip.tokenize(categories).to(device)

    predicted_labels = []
    predicted_scores = []

    num_images = len(df)
    num_batches = (num_images + batch_size - 1) // batch_size

    processed_images = 0  # Counter for processed images

    batch_images_filenames = []

    for batch_idx in range(num_batches):
        start_idx = batch_idx * batch_size
        end_idx = min(start_idx + batch_size, num_images)

        batch_images = []

        for i in range(start_idx, end_idx):
            local_img_path = df.loc[i, 'localImagePath']
            filename = df.loc[i, 'filename']

            try:
                image = preprocess(Image.open(local_img_path)).to(device)
                batch_images.append(image)
                batch_images_filenames.append(filename)

            except Exception as e:
                print(f"Error opening image: {df.loc[i, 'filename']} from {local_img_path}: {str(e)}")
                continue  # Skip this image and continue with the next one

        if not batch_images:
            continue  # Skip processing the batch if there are no valid images

        batch_images = [img for img in batch_images if img is not None]  # Remove None placeholders

        if not batch_images:
            continue  # Skip processing the batch if there are no valid images left

        batch_images = torch.stack(batch_images)

        with torch.no_grad():
            logits_per_image, logits_per_text = model(batch_images, text)

        predicted_indices = logits_per_image.argmax(dim=1)
        predicted_labels.extend([categories[idx] if idx < len(categories) else "N/A" for idx in predicted_indices])
        predicted_scores.extend(logits_per_image.tolist())

        processed_images += len(batch_images)  # Increment the counter

        current_time = datetime.now().strftime("%Y-%m-%d %H:%M")
        print(f"[{current_time}] Processed batch {batch_idx + 1}/{num_batches} (size: {len(batch_images)})")

        # Check if it's time to save results
        if processed_images >= save_every:
            save_results_to_csv(pd.DataFrame({'filename': batch_images_filenames, 'predicted_class': predicted_labels, 'scores': predicted_scores}), batch_idx + 1)
            # Reset counters
            processed_images = 0

    return predicted_labels, predicted_scores

def main():
    class_prediction = ['a photo of an algorithm',
                    'a photo of an architecture diagram',
                    'a photo of a bar chart',
                    'a photo of a boxplot',
                    'a photo of a confusion matrix',
                    'a photo of a graph',
                    'a photo of a line chart',
                    'a photo of a map',
                    'a photo of a pareto',
                    'a photo of a venn diagram',
                    'a photo of a word cloud',
                    'a photo of a natural image',
                    'a photo of a neural networks',
                    'a photo of an NLP (natural language processing) grammar',
                    'a photo of a pie chart',
                    'a photo of a scatter plot',
                    'a photo of a screenshot',
                    'a photo of a tables',
                    'a photo of a tree graph']

    csv = "image_data.csv"
    df = pd.read_csv(csv)

    predicted_labels, predicted_scores = predict(df, class_prediction, 100, 1000)

    # Add lists to the dataframe
    df['predicted_class'] = predicted_labels
    df['scores'] = predicted_scores

    df.to_csv("img_predictions.csv", index=False)

    print("done")

if __name__ == "__main__":
    start_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print("Starting Time: " + start_time)
    main()
    end_time = start_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print("Ending Time : " + end_time)


import socket, threading, os, struct,selectors#,atexit
from pathlib import Path
from tkinter import *
import queue # thread-safe

Q = queue.Queue()
sel = selectors.DefaultSelector()

serv_fd = None
CLIENT_PORT = 9999
MAX_MSG = 4096
last_cmd = ''
last_print = ''

class CleanExit:
  pass

class TextEditor:
	#@staticmethod
	def quit_app(self, event=None):
		os.remove(self.fname)
		self.root.destroy()

	def save_file(self, event=None):
		fil_path = self.fname
		content = self.text_area.get('1.0', END + '-1c')
		if(content[-1:] != '\n'):
			content += '\n'
		self.fd.sendall(('updt '+fil_path+';'+content+'Āā').encode())
		self.quit_app(self)

	def __init__(self, root, fname,fd):
		self.fname = fname
		self.root = root
		self.fd = fd
		self.text_to_write = ""
		root.title("Text Editor")
		root.geometry("600x550")
		frame = Frame(root, width=600, height=550)
		scrollbar = Scrollbar(frame)
		self.text_area = Text(frame, width=600, height=550,
				        yscrollcommand=scrollbar.set,
				        padx=10, pady=10)
		scrollbar.config(command=self.text_area.yview)
		scrollbar.pack(side="right", fill="y")
		self.text_area.pack(side="left", fill="both", expand=True)
		frame.pack()
		the_menu = Menu(root)
		file_menu = Menu(the_menu, tearoff=0)
		file_menu.add_command(label="Save", command=self.save_file)
		file_menu.add_separator()
		file_menu.add_command(label="Quit (Unsaved)", command=self.quit_app)
		the_menu.add_cascade(label="Option", menu=file_menu)
		root.config(menu=the_menu)
		self.text_area.delete(1.0, END)
		with open(fname) as _file:
			self.text_area.insert(1.0, _file.read())
		root.update_idletasks()

def recCliMsg(fd):
	sel.register(fd, selectors.EVENT_READ)
	global last_cmd
	global last_print
	cmd = ''
	while True:
		print('<cmd>: ',end='',flush=True)
		events = sel.select()
		for key, mask in events:
			#print(key)
			#print(key.fd)
			#print(fd)
			if key.fd==fd.fileno():
				try:
					data = fd.recv(MAX_MSG).decode()
				except ConnectionResetError:
					data = ''
				if(len(data)>0):
					if(data[1:4]=='wr%'):#1 cause cmdParse sends \n
						tpath=data[3:data.find(';')]
						while(data[-2:]!='Āā'):
							data +=fd.recv(MAX_MSG).decode()
						try:
							with open(tpath, 'x') as f:
								f.write(data[data.find(';')+1:-2])
							print('\nFile Opened')
							if(os.fork()==0):
								root = Tk()
								text_editor = TextEditor(root, tpath, fd)
								root.mainloop()
								sel.close()
								Q.put('quit')
								return
								#os.execvp('gedit',['gedit', tpath])
						except Exception as e:
							print('\nUpdate File First')
							continue
					elif(data[1:4]=='con'):
						#print('Connecting To '+data[4:])
						sel.unregister(fd)
						last_cmd = cmd
						Q.put('discon')#data[4:])
						return
					else:
						print(data)
				else:
					print('\nConnection Closed.')
					sel.unregister(fd)
					last_cmd = ''
					Q.put('close')
					#ipq.put(CleanExit)
					return
			elif key.fd ==0:
				last_cmd = cmd
				cmd = input()
				if cmd == "close" or cmd == 'exit':
					sel.close()
					Q.put('quit')
					return
				elif cmd[:5] == 'updt ':
					fil_path ='%'+cmd[5:].replace('/','%')
					exp=False
					try:
						content = Path(fil_path).read_text()
						os.remove(fil_path)
					except Exception as e:
						exp = True
						print(e)
					if(exp==True):
						continue
					fd.sendall(('updt '+fil_path+';'+content+'Āā').encode())
				else:
					fd.sendall(cmd.encode())

def MakeSock():
	global serv_fd
	serv_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	serv_fd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
	serv_fd.settimeout(4)
	serv_fd.bind(('', CLIENT_PORT))
	serv_fd.listen(0)

def main():
	print(r'''
	       .--.                   .---.
	   .---|__|           .-.     |~~~|
	.--|===|--|_          |_|     |~~~|--.
	|  |===|  |'\     .---!~|  .--|   |--|
	|%%| D |  |.'\    |===| |--|%%|   |  |
	|%%| F |  |\.'\   |   | |__|  |   |  |
	|  | S |  | \  \  |===| |==|  |   |  |
	|  |   |__|  \.'\ |   |_|__|  |~~~|__|
	|  |===|--|   \.'\|===|~|--|%%|~~~|--|
	^--^---'--^    `-'`---^-^--^--^---'--'
	Connected To
		     Peek-achu
			       File System
''')
	sel.register(0, selectors.EVENT_READ) #input
	global serv_fd
	#-----------------------------------------------------------
	multicast_group = ('224.4.255.255', 50000)
	mult_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	mult_sock.settimeout(0.2)
	ttl = struct.pack('b', 1)
	mult_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
	mult_sock.sendto(b'cli', multicast_group)
	#-----------------------------------------------------------
	MakeSock()
	while(True):
		while(True):
			if(last_cmd == ''):
				print('Connecting To System...')
			try:
				conn, addr = serv_fd.accept()
			except socket.timeout:
				mult_sock.sendto(b'cli', multicast_group)
			else:
				serv_fd.shutdown(socket.SHUT_RDWR)
				serv_fd.close()
				break
				#print('Out of accept')
		if(last_cmd != ''):
			conn.sendall(last_cmd.encode())
		t1 = threading.Thread(target=recCliMsg, kwargs={'fd':conn})
		t1.daemon = True
		t1.start()
		data = Q.get()
		#print(data)
		conn.close()
		t1.join()
		if(data == 'quit'):
			mult_sock.close()
			sys.exit()
		elif(data == 'close'):
			MakeSock()
			mult_sock.sendto(b'cli', multicast_group)
		elif(data == 'discon'):
			MakeSock()
			continue

if __name__ == "__main__":
	main()


import sys
import logging
import os
import time
from logging.handlers import RotatingFileHandler

from app.settings import DEBUG,MAXLOGCOUNT

def setupLogger():

    def consoleLogger(logger):
        consoleLog = logging.StreamHandler()
        consoleLog.setLevel(level)
        logConsoleFormat = logging.Formatter()
        consoleLog.setFormatter(logConsoleFormat)
        logger.addHandler(consoleLog)

    def fileLogger(logger,fileLogLevel=None):

        global MAXLOGCOUNT

        if MAXLOGCOUNT == 0:
            return

        MAXLOGCOUNT = MAXLOGCOUNT if MAXLOGCOUNT > 1 else 1

        fileLogLevel = fileLogLevel if fileLogLevel else logging.DEBUG

        if not os.path.exists('logs'):
            os.mkdir('logs')

        logFileName = 'prebuild-' + time.strftime("%Y%m%d-%H%M%S")

        logList = os.listdir('logs')
        logList.sort()
        if len(logList) > MAXLOGCOUNT-1:
            try:
                os.remove(os.path.join('logs',logList[0]))
            except FileNotFoundError:
                logger.critical("Exception while removing old logs")

        logFilePath = os.path.join('logs',logFileName + '.log')

        fileLog = RotatingFileHandler(logFilePath, mode='w', maxBytes=50*1024*1024,
                                         backupCount=5, encoding=None, delay=False)

        fileLog.setLevel(fileLogLevel)
        logFileFormat = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        fileLog.setFormatter(logFileFormat)
        logger.addHandler(fileLog)

    def handle_unhandled_exception(exc_type, exc_value, exc_traceback,thread_identifier=None):
        """Handler for unhandled exceptions that will write to the logs"""
        if issubclass(exc_type, KeyboardInterrupt):
            # call the default excepthook saved at __excepthook__
            sys.__excepthook__(exc_type, exc_value, exc_traceback)
            return
        if thread_identifier:
            logger.critical(f"Unhandled exception in {thread_identifier}",exc_info=(exc_type, exc_value, exc_traceback))
        else:
            logger.critical(f"Unhandled exception",exc_info=(exc_type, exc_value, exc_traceback))

    level = logging.DEBUG if DEBUG else logging.ERROR
    logger = logging.getLogger("__main__")
    logger.setLevel(level)
    consoleLogger(logger)
    fileLogger(logger)
    sys.excepthook = handle_unhandled_exception
    return logger

logger = setupLogger()


from django.conf.urls.defaults import url

from tastypie_mongoengine import resources
from tastypie.authorization import Authorization
from tastypie import fields

from models import LocalInfo

class LocalInfoResource(resources.MongoEngineResource):
    class Meta:
        queryset = LocalInfo.objects.all()
        allowed_methods = ('get',)
        authorization = Authorization()
        filtering = {
            'type' : resources.QUERY_TERMS_ALL,
            'name' : resources.QUERY_TERMS_ALL,
        }

    id = fields.CharField(attribute='name')

    def override_urls(self):
            return [
                url(r"^(?P<resource_name>%s)/(?P<name>[\w\d_.-]+)/$" %
                    self._meta.resource_name,
                    self.wrap_view('dispatch_detail'),
                    name="api_dispatch_detail"),
            ]


from tkinter import Tk
from tkinter.filedialog import askopenfilename

import matplotlib.pyplot as plt
import numpy as np
import csv

from collections import defaultdict

from scipy.interpolate import make_interp_spline, BSpline

""" Global Performance dictionnary structure (G_Perf)
    {
        'NxK 1': {
            (N, K): NK_Perf, # variation 1 (N < K)
            (N, K): NK_Perf, # variation 2 (N = K)
            (N, K): NK_Perf, # variation 3 (N > K)
        },
        'NxK 2': }
            (N, K): NK_Perf,
            (N, K): NK_Perf,
            (N, K): NK_Perf,
        },
        ...
    }
"""

""" NK Performance dictionnary structure (NK_Perf)
    {
        'Performance': [],
        // I can add other measures like generating performance, etc..
    }
"""

THREADS_ARR = np.array(['1', '2', '4', '8'])

def draw_bars(n:int, k:int, nk:int, nk_perf:defaultdict):
    measure = 's'

    m = max(nk_perf['Performance'])
    if m >= 120:
        nk_perf['Performance'] = [x/60 for x in nk_perf['Performance']]
        measure = 'min'
    elif m < 1:
        nk_perf['Performance'] = [x*1000 for x in nk_perf['Performance']]
        measure = 'ms'

    rects = plt.bar(THREADS_ARR, nk_perf['Performance'], label='Performance')

    # for each rect annotate the performance attained
    for i, rect in enumerate(rects):
        x, y = rect.xy
        h = rect.get_height()
        plt.annotate('%.2f %s' % (nk_perf['Performance'][i], measure), (x, h))

    plt.grid()
    plt.xlabel('Threads')
    plt.ylabel(f'Performance ({measure})')
    nk_cmp = 'N = K'
    if n < k:
        nk_cmp = 'N < K'
    elif n > k:
        nk_cmp = 'N > K'

    plt.title(f'N = {n} K = {k}  ({nk_cmp})\nNK = {nk}')

    filename = f'bars/{nk} - ({n},{k}).jpg'
    plt.figlegend()
    plt.savefig(filename)
    plt.clf()
    print(f'plot saved to {filename}')

if __name__ == "__main__":

    Tk().withdraw()
    filename = askopenfilename()

    g_perf = dict()

    nk_var_counter = 0 # --> len(THREADS_ARR)
    nk_counter = 0 # --> 3 * len(THREADS_ARR)

    with open(filename, 'r') as csvfile:
        data = csv.DictReader(csvfile, delimiter=',')
        for row in data:
            nk_var_counter += 1
            nk_counter += 1

            nk = int(row['NxK'])
            n = int(row['N'])
            k = int(row['K'])

            if nk_counter == 1:
                g_perf[nk] = dict()
                nk_var = dict()

            if nk_var_counter == 1:
                nk_perf = defaultdict(list)

            nk_perf['Performance'].append(float(row['Performance']))

            if nk_var_counter == len(THREADS_ARR):
                nk_var_counter = 0
                nk_var[(n, k)] = nk_perf

            if len(nk_var) == 3:
                g_perf[nk] = nk_var

            if nk_counter == 3 * len(THREADS_ARR):
                nk_counter = 0

    for nk, g_perf_v in g_perf.items():
        for nk_var, nk_perf in g_perf_v.items():
            (n , k) = nk_var
            draw_bars(n, k, nk, nk_perf)


from math import ceil, log, floor
import torch
import torch.nn.functional as F

def i_poly(q, S, a, b, c):
    q_b = floor(b/S)
    S_out = a*S*S
    q_c = floor(c/S_out)
    q_out = (q + q_b)*(q+q_b)+q_c
    return q_out, S_out

def i_exp(q, S):
    a, b, c = 0.3585, 1.353, 0.344
    q_ln = floor(log(2)/S)
    z = -(q / q_ln).floor()
    q_p = q+z*q_ln
    q_o, S_o = i_poly(q_p, S, a, b, c)
    q_o >>= z
    return q_o, S_o

def i_sqrt(n, n_bits):
    x = 2 << ceil(n_bits // 2)
    for i in range(10):
        x = (x + (n / x)/floor())
        x >>= 1
    return x

def i_softmax(q, S):
    q_exp, S_exp = i_exp(q, S)
    q_sum = q_exp.sum(dim=1)
    return q_exp/q_sum

if __name__ == "__main__":
    x = torch.rand(5, 5)*4-2
    scale = 4 / 256
    q = (x / scale).floor()
    print(F.softmax(x, dim=1))
    print(i_softmax(q, scale))


from __future__ import annotations
import math
from typing import Tuple, List, Sequence, Callable, Any, Dict
import dataclasses
import logging
import textwrap
from functools import lru_cache
import click
import pycyphal
import yakut
from yakut.enum_param import EnumParam
from yakut.yaml import EvaluableLoader
from yakut.subject_specifier_processor import process_subject_specifier, SubjectResolver
from ._executor import Executor, Publication

_MIN_SEND_TIMEOUT = 0.1
"""
With a slow garbage-collected language like Python, having a smaller timeout does not make practical sense.
This may be made configurable later.
"""

@dataclasses.dataclass(frozen=True)
class ExpressionContextModule:
    """
    Defines a Python module that is imported and available for use within YAML-embedded user expressions.
    """

    module_name: str
    docs_uri: str
    alias: str = ""
    wildcard: bool = False

    def __str__(self) -> str:
        if self.wildcard:
            body = f"from {self.module_name} import *"
        elif self.alias:
            body = f"import {self.module_name} as {self.alias}"
        else:
            body = f"import {self.module_name}"
        return body.ljust(25) + f"# {self.docs_uri}"

    @staticmethod
    def load(what: Sequence[ExpressionContextModule]) -> Dict[str, Any]:
        import inspect

        out: Dict[str, Any] = {}
        for ecm in what:
            _logger.debug("Importing: %r", ecm)
            mod = __import__(ecm.module_name)
            out[ecm.module_name] = mod
            if ecm.alias:
                out[ecm.alias] = mod
            if ecm.wildcard:
                out.update({name: member for name, member in inspect.getmembers(mod) if not name.startswith("_")})

        _logger.debug("Expression context contains %d items (listed on the next line):\n%s", len(out), list(out))
        return out

_EXPRESSION_CONTEXT_MODULES = [
    ExpressionContextModule("random", "https://docs.python.org/library/random.html", wildcard=True),
    ExpressionContextModule("time", "https://docs.python.org/library/time.html", wildcard=True),
    ExpressionContextModule("math", "https://docs.python.org/library/math.html", wildcard=True),
    ExpressionContextModule("os", "https://docs.python.org/library/os.html"),
    ExpressionContextModule("pycyphal", "https://pycyphal.readthedocs.io"),
    ExpressionContextModule("numpy", "https://numpy.org", alias="np"),
    ExpressionContextModule("scipy.constants", "https://docs.scipy.org/doc/scipy/reference/constants.html"),
    ExpressionContextModule("scipy.interpolate", "https://docs.scipy.org/doc/scipy/reference/interpolate.html"),
    ExpressionContextModule("scipy.linalg", "https://docs.scipy.org/doc/scipy/reference/linalg.html"),
    ExpressionContextModule("scipy.spatial", "https://docs.scipy.org/doc/scipy/reference/spatial.html"),
]

_EXAMPLES = """
Example: publish constant messages (no embedded expressions, just regular YAML):

\b
    yakut pub uavcan.diagnostic.record '{text: "Hello world!", severity: {value: 4}}' -N3 -T0.1 -P hi
    yakut pub 33:uavcan.si.unit.angle.scalar 2.31 uavcan.diagnostic.Record 'text: "2.31 radian"'

Example: publish sinewave with frequency 1 Hz, amplitude 10 meters:

\b
    yakut pub -T 0.01 1234:uavcan.si.unit.length.scalar '!$ "sin(t * pi * 2) * 10"'

Example: as above, but control the frequency of the sinewave and its amplitude using sliders 10 and 11
of the first connected controller (use `yakut joystick` to find connected controllers and their axis mappings):

\b
    yakut pub -T 0.01 1234:uavcan.si.unit.length.Scalar '{meter: !$ "sin(t * pi * 2 * A(1,10)) * 10 * A(1,11)"}'

Example: publish 3D angular velocity setpoint, thrust setpoint, and the arming switch state:

\b
    yakut pub -T 0.1 \\
        5:uavcan.si.unit.angular_velocity.Vector3 '!$ "[A(1,0)*10, A(1,1)*10, (A(1,2)-A(1,5))*5]"' \\
        6:uavcan.si.unit.power.Scalar '!$ A(2,10)*1e3' \\
        7:uavcan.primitive.scalar.Bit '!$ T(1,5)'

Example: simulate timestamped measurement of voltage affected by white noise with standard deviation 0.25 V:

\b
    yakut pub -T 0.1 6:uavcan.si.sample.voltage.scalar \\
        '{timestamp: !$ time()*1e6, volt: !$ "A(2,10)*100+normalvariate(0,0.25)"}'
""".strip()

_HELP = f"""
Publish messages on the specified subjects.
Unless the local transport is configured in anonymous node,
the local node will also publish on standard subjects like Heartbeat and provide some standard RPC-services
like GetInfo.

The command accepts a list of space-separated pairs like:

\b
    SUBJECT_SPECIFIER  YAML_FIELDS

The first element -- SUBJECT_SPECIFIER -- defines the subject-ID and/or the data type name;
refer to the subscription command for details.

The second element -- YAML_FIELDS -- specifies the values of the message fields in YAML format
(or JSON, which is a subset of YAML).
Missing fields will be left at their default values.
For more info about the format see DSDL API docs at https://pycyphal.readthedocs.io.

The number of such pairs can be arbitrary; all defined messages will be published synchronously.
If no such pairs are specified, only the heartbeat will be published, unless the local node is anonymous.

The YAML document may embed arbitrary Python expressions that are re-evaluated immediately before publication.
Such expressions are annotated with the YAML tag `!$` (exclamation followed by dollar).
The result of such expression is substituted into the original YAML structure;
as such, the result can be of arbitrary type as long as the final YAML structure can be applied to the specified
DSDL instance.

The YAML-embedded expressions have access to the following variables:

{Executor.SYM_INDEX}: int --- index of the current publication cycle, zero initially.

{Executor.SYM_TIME}: float --- time elapsed since first message (t=n*period).

{Executor.SYM_DTYPE}: type --- message class.

{Executor.SYM_CTRL_AXIS}: (controller,axis:int)->float ---
read the normalized value of `axis` from `controller` (e.g., joystick or MIDI fader).
To see the list of available controllers and determine their channel mapping, refer to `yakut joystick`.

{Executor.SYM_CTRL_BUTTON}: (controller,button:int)->bool ---
read the state of `button` from `controller` (true while held down).

{Executor.SYM_CTRL_TOGGLE}: (controller,toggle:int)->bool ---
read the state of `toggle` from `controller`.

The following Python modules are imported and usable within embedded expressions;
refer to their API docs for usage info:

\b
{textwrap.indent(chr(0x0A).join(map(str, _EXPRESSION_CONTEXT_MODULES)), " " * 4)}

{_EXAMPLES}
"""

def _validate_message_spec(
    ctx: click.Context,
    param: click.Parameter,
    value: Tuple[str, ...],
) -> List[Tuple[str, str]]:
    if len(value) % 2 != 0:
        raise click.BadParameter(
            f"Message specifier shall have an even number of paired arguments (found {len(value)} arguments)",
            ctx=ctx,
            param=param,
        )
    return [(s, f) for s, f in (value[i : i + 2] for i in range(0, len(value), 2))]  # pylint: disable=R1721

@yakut.subcommand(help=_HELP, aliases=["pub", "p"])
@click.argument(
    "message",
    type=str,
    callback=_validate_message_spec,
    metavar="SUBJECT FIELDS [SUBJECT FIELDS]...",
    nargs=-1,
)
@click.option(
    "--period",
    "-T",
    type=float,
    default=1.0,
    show_default=True,
    metavar="SECONDS",
    help=f"""
Message publication period.
All messages are published synchronously, so the period setting applies to all specified subjects.
The send timeout equals the period as long as it is not less than {_MIN_SEND_TIMEOUT} seconds.
""",
)
@click.option(
    "--count",
    "-N",
    type=int,
    default=2**64 - 1,
    metavar="CARDINAL",
    help=f"Number of publication cycles before exiting normally. Unlimited by default.",
)
@click.option(
    "--priority",
    "-P",
    default=pycyphal.presentation.DEFAULT_PRIORITY,
    type=EnumParam(pycyphal.transport.Priority),
    help=f"Priority of published message transfers. [default: {pycyphal.presentation.DEFAULT_PRIORITY.name}]",
)
@yakut.pass_purser
@yakut.asynchronous(interrupted_ok=True)
async def publish(
    purser: yakut.Purser,
    message: Sequence[Tuple[str, str]],
    period: float,
    count: int,
    priority: pycyphal.transport.Priority,
) -> None:
    _logger.debug("period=%s, count=%s, priority=%s, message=%s", period, count, priority, message)
    assert all((isinstance(a, str) and isinstance(b, str)) for a, b in message)
    assert isinstance(period, float) and isinstance(count, int) and isinstance(priority, pycyphal.transport.Priority)
    if period < 1e-9 or not math.isfinite(period):
        raise click.BadParameter("Period shall be a positive real number of seconds")
    if count <= 0:
        _logger.warning("Nothing to do because count=%s", count)
        return
    try:
        from pycyphal.application import Node
    except ImportError as ex:
        from yakut.cmd.compile import make_usage_suggestion

        raise click.ClickException(make_usage_suggestion(ex.name))

    finalizers: list[Callable[[], None]] = []
    try:
        # Parse the field specs we were given to validate syntax.
        # Do this early before other resources are initialized.
        yaml_loader = EvaluableLoader(ExpressionContextModule.load(_EXPRESSION_CONTEXT_MODULES))
        evaluators: list[Callable[..., Any]] = []
        for _, field_spec in message:
            try:
                evaluators.append(yaml_loader.load_unevaluated(field_spec))
            except ValueError as ex:
                raise click.BadParameter(f"Invalid field spec {field_spec!r}: {ex}") from None

        # Node construction should be delayed as much as possible to avoid unnecessary interference
        # with the bus and hardware. This is why we use the factory here instead of constructing the node eagerly.
        @lru_cache(None)
        def get_node() -> Node:
            node = purser.get_node("publish", allow_anonymous=True)
            finalizers.append(node.close)
            return node

        @lru_cache(None)
        def get_subject_resolver() -> SubjectResolver:
            node = get_node()
            if node.id is None:
                raise click.ClickException(
                    f"Cannot use automatic discovery because the local node is anonymous, "
                    f"so it cannot access the introspection services on remote nodes. "
                    f"You need to either fully specify the subjects explicitly or assign a local node-ID."
                )
            sr = SubjectResolver(node)
            finalizers.append(sr.close)
            return sr

        # Resolve subject-IDs and dtypes. This may or may not require the local node.
        subject_id_dtype_pairs: list[tuple[int, Any]] = [
            await process_subject_specifier(subject_spec, get_subject_resolver) for subject_spec, _ in message
        ]

        # We delayed node construction as much as possible. It may already be constructed if subject resolver was used.
        send_timeout = max(_MIN_SEND_TIMEOUT, period)
        node = get_node()
        publications = [
            Publication(
                subject_id=sbj_id,
                dtype=dty,
                evaluator=evl,
                node=node,
                priority=priority,
                send_timeout=send_timeout,
            )
            for (sbj_id, dty), evl in zip(subject_id_dtype_pairs, evaluators)
        ]
        executor = Executor(loader=yaml_loader, publications=publications)
        finalizers.append(executor.close)

        # Everything is ready, the node can be started now. It will be stopped during finalization.
        node.start()
        _logger.info(
            "Publishing %d subjects with period %.3fs, send timeout %.3fs, count %d, priority %s",
            len(message),
            period,
            send_timeout,
            count,
            priority.name,
        )
        try:
            await executor.run(count=count, period=period)
        finally:
            _log_final_report(node.presentation)
    finally:
        pycyphal.util.broadcast(finalizers[::-1])()

def _log_final_report(presentation: pycyphal.presentation.Presentation) -> None:
    if _logger.isEnabledFor(logging.INFO):
        _logger.info("%s", presentation.transport.sample_statistics())
        for s in presentation.transport.output_sessions:
            ds = s.specifier.data_specifier
            if isinstance(ds, pycyphal.transport.MessageDataSpecifier):
                _logger.info("Subject %d: %s", ds.subject_id, s.sample_statistics())

_logger = yakut.get_logger(__name__)


# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),
    html.Div(children='Dash: A web application framework for Python.'),

    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )
])

if __name__ == '__main__':
    app.run_server()


"""Blogly application."""

from flask import Flask, render_template, redirect, request
from models import db, connect_db, User, Post
from flask_debugtoolbar import DebugToolbarExtension

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///blogly'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
app.config['SECRET_KEY'] = 'secret_key'
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
toolbar = DebugToolbarExtension(app)

connect_db(app)
db.create_all()

@app.route('/')
def take_home():

    user = User.query.all()

    return render_template('users.html', user=user)

@app.route('/create')
def go_create():

    return render_template('create.html')

@app.route('/create', methods=['POST'])
def create_user():

    first = request.form['first_name']
    last = request.form['last_name']
    url = request.form['url'] if request.form['url'] else None
    user = User(first_name=first, last_name=last, img_url=url)

    db.session.add(user)
    db.session.commit()

    return redirect('/')

@app.route('/<int:id>')
def get_user_detail(id):

    user = User.query.get_or_404(id)

    return render_template('user_detail.html', user=user)

@app.route('/<int:id>/edit')
def edit_user_detail(id):

    user = User.query.get_or_404(id)

    return render_template('user_edit.html', user=user)

@app.route('/<int:id>/edit', methods=['POST'])
def update_user_detail(id):

    user = User.query.get_or_404(id)

    user.first_name = request.form['first_name']
    user.last_name = request.form['last_name']
    user.img_url = request.form['url']

    db.session.add(user)
    db.session.commit()

    return redirect('/')

@app.route('/<int:id>/delete', methods=["POST"])
def users_destroy(id):
    """Handle form submission for deleting an existing user"""

    user = User.query.get_or_404(id)

    db.session.delete(user)
    db.session.commit()

    return redirect('/')

@app.route('/<int:id>/create_post')
def create_post(id):

    user = User.query.get_or_404(id)

    return render_template('create_post.html', user=user)

@app.route('/<int:id>/create_post', methods=['POST'])
def post_post(id):

    user = User.query.get_or_404(id)
    title = request.form['title']
    content = request.form['content']
    post = Post(title=title, content=content)

    db.session.add(post)
    db.session.commit()

    return redirect('/update_post')

# hello


from typing import List

def kth_smallest_element(elements: List[int], low: int, high: int, k):
    if 0 < k <= (high - low + 1):
        pk = lomutoro_partition(elements, low, high)

        if pk - low == (k - 1):
            return elements[pk]

        if pk - low > (k - 1):
            return kth_smallest_element(elements, low, pk - 1, k)
        return kth_smallest_element(elements, pk + 1, high, k - 1 - (pk - low))

def quick_sort(elements: List[int], low: int, high: int):
    if low < high:
        pk = lomutoro_partition(elements, low, high)

        # pk = hoare_partition(elements, low, high)

        # while using lomutoro's partition algorithm, call the quick sort excluding
        # the partition position
        quick_sort(elements, low, pk - 1)

        # while using hoare's partition algorithm, call the quick sort including
        # the partition position
        # quick_sort(elements, low, pk)
        quick_sort(elements, pk + 1, high)

def lomutoro_partition(elements: List[int], low: int, high: int):
    pivot: int = elements[high]
    i = low

    for j in range(low, high):
        if elements[j] >= pivot:
            elements[i], elements[j] = elements[j], elements[i]
            i += 1
    elements[i], elements[high] = elements[high], elements[i]
    return i

def hoare_partition(elements: List[int], low: int, high: int):
    i: int = low - 1
    j: int = high + 1
    pivot: int = elements[low]

    while True:
        i += 1
        while elements[i] < pivot:
            i += 1

        j -= 1
        while elements[j] > pivot:
            j -= 1

        if i >= j:
            return j

        elements[i], elements[j] = elements[j], elements[i]

if __name__ == '__main__':
    input_list: List[int] = [5, 2, 3, 9, 17, 6, 9, 1]
    #
    # print(f"List before sorting {input_list}")
    #
    # quick_sort(input_list, 0, len(input_list) - 1)
    #
    # print(f"List after sorting {input_list}")
    print(kth_smallest_element(input_list, 0, len(input_list) - 1, 3))


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep

options = Options()
options.binary_location = '/usr/bin/google-chrome-stable'
options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=options)

## mission-chan
# url = 'https://e-hentai.org/s/7b1b30f886/409815-1'
# count = 1
# count_max = 218

## work the dial
# url = 'https://e-hentai.org/s/517068ec74/409830-1'
# count = 1
# count_max = 15

## Saterii
# url = 'https://e-hentai.org/s/b9afb813e9/409821-1'
# count = 1
# count_max = 15

## Cygnet
# url = 'https://e-hentai.org/s/c407b61338/409872-1'
# count = 1
# count_max = 44

## Konamilk Tsushin
url = 'https://e-hentai.org/s/03c1d190b1/205814-1'
count = 1
count_max = 34

img_width = 450
img_height = 600
wait = 3

driver.set_window_size(img_width, img_height)
driver.get(url)
while count <= count_max:
    imgurl = driver.find_element_by_id('img').get_attribute('src')
    driver.get(imgurl)
    sleep(wait)
    if driver.save_screenshot(str(count) + '.png'):
        print(imgurl)
    driver.back()
    driver.find_element_by_id('next').click()
    count += 1


""" Config structure for smurf. """
import os
import sys
import json

home_path = os.path.join(os.path.expanduser("~"), ".smurf")

information_types = ["rootdir", "host"]

def main():
    args = parse_command_line_args()
    c = Config()

    if args.subparser_name in ["show"] or args.subparser_name is None:
        args.func(c)
    elif args.subparser_name in ["get"]:
        args.func(c, args.key)
    else:
        args.func(c, args.key, args.value)

def parse_command_line_args():
    import argparse, argcomplete
    import pprint
    parser = argparse.ArgumentParser()
    parser.set_defaults(func=Config.print)

    subparsers = parser.add_subparsers(dest='subparser_name')
    parser_add = subparsers.add_parser('add', help='Add a config item.')
    parser_add.add_argument("key",
                            choices=information_types,
                            help="What to set.")
    parser_add.add_argument("value")
    parser_add.set_defaults(func=Config.add)

    parser_remove = subparsers.add_parser('remove',
                                          help='Remove a config item.')
    parser_remove.add_argument("key",
                               choices=information_types,
                               help="What to set.")
    parser_remove.add_argument("value")
    parser_remove.set_defaults(func=Config.remove)

    parser_show = subparsers.add_parser('show', help='Show the config.')
    parser_show.set_defaults(func=Config.print)
    parser_get = subparsers.add_parser(
        'get', help='Return the value of a root level config item.')
    parser_get.add_argument("key", help="What to get.")
    parser_get.set_defaults(func=Config.print_value)
    argcomplete.autocomplete(parser)
    args = parser.parse_args()
    return args

def expand_path(path):
    abspath = os.path.abspath(os.path.expanduser(path))
    if not os.path.exists(abspath):
        raise FileNotFoundError("No such directory: {}".format(path))
    return abspath

def check_information_type(info_type):
    if not any((info_type == t for t in information_types)):
        raise AttributeError(
            "Information type {} not supported".format(info_type))

class Config:
    def __init__(self):
        if not os.path.exists(home_path):
            os.makedirs(home_path)
        self.config_file = os.path.join(home_path, "config.json")
        self.load()

    def add(self, what, val):
        check_information_type(what)
        if what == "rootdir":
            self.add_rootdir(val)
        elif what == "host":
            list_name = what + "_list"
            if not list_name in self.data:
                self.data[list_name] = []
            self.data[list_name].append(val)
        else:
            self.data[what] = val
        self.save()

    def remove(self, what, val):
        check_information_type(what)
        list_name = what + "_list"
        try:
            for n in range(len(self.data[list_name])):
                if self.data[list_name][n] == val:
                    del self.data[list_name][n]
                    break
            self.save()
        except KeyError:
            print("No config for type", what)
            pass

    def add_rootdir(self, path):
        if not "rootdir_list" in self.data:
            self.data["rootdir_list"] = []
        abspath = expand_path(path)
        # only insert if not already there
        try:
            for n, p in enumerate(self.data["rootdir_list"]):
                # check if its a parent of an already registered dir
                if abspath == os.path.commonpath([abspath, p]):
                    self.data["rootdir_list"][n] = abspath
                    raise StopIteration
        except StopIteration:
            pass
        else:
            self.data["rootdir_list"].append(abspath)
        finally:
            self.save()

    def __getitem__(self, key):
        return self.data[key]

    def __setitem__(self, key, val):
        self.data[key] = val

    def save(self):
        self.data["type"] = "smurf config"
        self.data["version"] = "0.1"
        with open(self.config_file, "w") as outfile:
            outfile.write(json.dumps(self.data, indent=4))

    def load(self):
        try:
            with open(self.config_file, "r") as infile:
                self.data = json.load(infile)
        except FileNotFoundError:
            self.data = {}
            self.data["rootdir_list"] = []
            self.data["host_list"] = []
            self.data["home_path"] = home_path
            self.data["local_cache"] = "local_simcache.json"
            self.data["remote_cache"] = "remote_simcache.json"

    def print(self):
        import pprint
        pprint.pprint(self.data)

    def print_value(self, key):
        try:
            print(self[key])
        except KeyError:
            print("Error: No config value found for key '{}'".format(key))
            sys.exit(1)

if __name__ == "__main__":
    main()


from glob import glob
from scipy.optimize import bisect
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
from spt3g import core

fnames_noise = glob('/spt/user/adama/20190911_noise_gainmatch_cal/fullrate/gainmatching_noise_*.g3')

def knee_func(x, readout, A, alpha, photon, tau):
    return (A * (x)**(-1*alpha)) - photon / (1 + 2*np.pi*((x*tau)**2)) - readout

f_knee_dict = {}
for fname in fnames_noise:
    print(fname)
    fr = list(core.G3File(fname))[1]

    for jband, band in enumerate([90., 150., 220.]):
        for jwafer, wafer in enumerate(['w172', 'w174', 'w176', 'w177', 'w180',
                                        'w181', 'w188', 'w203', 'w204', 'w206']):
            group = '{:.1f}_{}'.format(band, wafer)

            if band not in f_knee_dict:
                f_knee_dict[band] = {}
            if wafer not in f_knee_dict[band]:
                f_knee_dict[band][wafer] = []

            try:
                ff_diff = np.array(fr['AverageASDDiff']['frequency']/core.G3Units.Hz)
                asd_diff = np.array(fr['AverageASDDiff'][group])
                par_diff = fr["AverageASDDiffFitParams"][group]

                f_knee = bisect(knee_func, a=0.01, b=10.0, args=tuple(par_diff))
                f_knee_dict[band][wafer].append(f_knee)
            except:
                pass

fnames = glob('/spt/user/adama/20190911_noise_gainmatch_cal/fullrate/gainmatching_noise_83521028.g3')
d = list(core.G3File(fnames))[1]

f_hi = 73
freq = np.array(d["AverageASDDiff"]['frequency']) / core.G3Units.Hz

fig = plt.figure(figsize=(12,4))
gridspec.GridSpec(4,3)
band_labels = {90:'95', 150:'150', 220:'220'}

for jband, band in enumerate(f_knee_dict):
    plt.subplot2grid((4,3), (0,jband), colspan=1, rowspan=1)
    f_knees = np.hstack([f_knee_dict[band][wafer] for wafer in f_knee_dict[band].keys()])
    plt.hist(f_knees, bins=np.logspace(np.log10(1e-2), np.log10(73), 21),
             alpha=0.5, color='C0') #, normed=True)
    plt.hist(f_knees, bins=np.logspace(np.log10(1e-2), np.log10(73), 21),
             histtype='step', color='C0') #, normed=True)
#     plt.axis([1e-2, 73, 0, 11])
    plt.axis([1e-2, 73, 0, 220])
    plt.gca().set_xscale('log')
    plt.gca().set_xticklabels([])
    plt.gca().set_yticklabels([])
    plt.title('{} GHz'.format(band_labels[band]))

    if jband == 0:
        plt.ylabel('$1/f$ knees of\nnoise stares')

# ax = plt.subplot(2,3,4)
plt.subplot2grid((4,3), (1,0), colspan=1, rowspan=3)
asd_diff = np.array(d["AverageASDDiff"]['90.0_w204'])
asd_sum = np.array(d["AverageASDSum"]['90.0_w204'])
plt.loglog(freq[freq<f_hi], asd_diff[freq<f_hi] / np.sqrt(2.))
plt.loglog(freq[freq<f_hi], asd_sum[freq<f_hi] / np.sqrt(2.))
print(np.mean(asd_diff[(freq>1) & (freq<5)]) / np.sqrt(2.))
plt.grid()
plt.axis([1e-2, 73, 200, 50000])
plt.ylabel('NET [$\mu$K $\sqrt{s}$]')

# plt.subplot(2,3,5)
plt.subplot2grid((4,3), (1,1), colspan=1, rowspan=3)
asd_diff = np.array(d["AverageASDDiff"]['150.0_w204'])
asd_sum = np.array(d["AverageASDSum"]['150.0_w204'])
plt.loglog(freq[freq<f_hi], asd_diff[freq<f_hi] / np.sqrt(2.))
plt.loglog(freq[freq<f_hi], asd_sum[freq<f_hi] / np.sqrt(2.))
print(np.mean(asd_diff[(freq>1) & (freq<5)]) / np.sqrt(2.))
plt.grid()
plt.axis([1e-2, 73, 200, 50000])
plt.gca().set_yticklabels([])
plt.xlabel('frequency [Hz]')

# plt.subplot(2,3,6)
plt.subplot2grid((4,3), (1,2), colspan=1, rowspan=3)
asd_diff = np.array(d["AverageASDDiff"]['220.0_w204'])
asd_sum = np.array(d["AverageASDSum"]['220.0_w204'])
plt.loglog(freq[freq<f_hi], asd_diff[freq<f_hi] / np.sqrt(2.))
plt.loglog(freq[freq<f_hi], asd_sum[freq<f_hi] / np.sqrt(2.))
print(np.mean(asd_diff[(freq>1) & (freq<5)]) / np.sqrt(2.))
plt.grid()
plt.axis([1e-2, 73, 200, 50000])
plt.gca().set_yticklabels([])

plt.tight_layout()
fig.subplots_adjust(wspace=0, hspace=0)
plt.savefig('fig_lowf_noise_summary.pdf')


import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from cmath import pi
import warnings
warnings.filterwarnings("ignore")
import pickle
from statsmodels.tsa.arima.model import ARIMA

#pickled_model_1 = pickle.load(open("model_OLS.pkl", 'rb'))

###################################################################################
st.title('Inventory Optimization of Steel Rods | Forecasting')

uploaded_file = st.file_uploader(" ", type=['csv']) #Only accepts csv file format

if uploaded_file is not None:

    data = pd.read_csv(uploaded_file)
    st.dataframe(data)
######################################################################################
    #fig = plt.figure(figsize = (16,6))
    #sns.lineplot(x = data[data.columns[0]], y = data[data.columns[1]])
    #st.pyplot(fig)

    sd = st.selectbox(
        "Select a Plot", #Drop Down Menu Name
        [
            "Line Plot", #First option in menu
            "Hist Plot"  #Seconf option in menu
        ]
    )

    fig = plt.figure(figsize=(12, 6))

    if sd == "Line Plot":
        sns.lineplot(x = data[data.columns[0]], y = data[data.columns[1]])

    elif sd == "Hist Plot":
        sns.histplot(x = data[data.columns[1]])

    st.pyplot(fig)
#####################################################################################

    data["t"] = np.arange(1,len(data) + 1)
    data["t2"] = data["t"]**2
    data["log_value"] = np.log(data[data.columns[1]])
    Sin = np.sin((2 * pi * data["t"]) / 52.143)
    Cos = np.cos((2 * pi * data["t"]) / 52.143)

    data["Sin"] = Sin
    data["Cos"] = Cos

    #st.dataframe(data)

    pickled_model_1 = pickle.load(open("model_OLS.pkl", 'rb'))
    pred_OLS = pickled_model_1.predict(data)
    #st.dataframe(pred_OLS)
#######################################################################################

    prediction_df = data.iloc[:,[0]]
    prediction_df["t"] = np.arange(1,len(data) + 1)
    prediction_df["t2"] = prediction_df["t"]**2

    Sin = np.sin((2 * pi * prediction_df["t"]) / 52.143)
    Cos = np.cos((2 * pi * prediction_df["t"]) / 52.143)

    prediction_df["Sin"] = Sin
    prediction_df["Cos"] = Cos

    pred_on_predict_data_dataset = pickled_model_1.predict(prediction_df)

    residual = data[data.columns[1]] - pickled_model_1.predict(data)
#####################################################################################
    model = ARIMA(residual, order=(1,1,1))
    results = model.fit()
    forecast = results.predict(start = len(residual), end = len(residual) + len(prediction_df) - 1, typ='levels').rename('Forecast')
    forecast.reset_index(drop = True, inplace = True)

    final_df = pred_on_predict_data_dataset + forecast

    df = pd.DataFrame(columns = ["week", "prediction"])
    df["week"] = data.iloc[:,[0]]
    df["prediction"] = np.round(final_df, decimals = 2)
    #df
    st.title('Prediction Values for next 52 weeks: ')
    st.dataframe(df)

    fig = plt.figure(figsize = (16,6))
    sns.lineplot(x = df[df.columns[0]], y = df[df.columns[1]])
    st.pyplot(fig)

########################################################################

    @st.cache_data
    def convert_df(df):
        return df.to_csv(index=False).encode('utf-8')

    csv = convert_df(df)

    st.download_button("Press to Download",
                       csv,
                       "file.csv",
                       "text/csv",
                       key = 'download-csv')


import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer

#Read the dataset from sklearn with breast cancer data
df = load_breast_cancer()
# Create a dataframe with the keys and data
medical_df = pd.DataFrame(df.data,columns=df.feature_names)
# Target is the filed with the information on the cases that are benign or malignant
medical_df['target'] = df.target

# Create a series that show the umber of bening and malignant cases from the dataset
malignant = len(medical_df[medical_df['target']==0])
benign = len(medical_df[medical_df['target']==1])

target_data = {'malignant':malignant, 'benign':benign}
target = pd.Series(target_data,index=['malignant','benign'])

#Display the results
print(target)


from aiogram.types import ReplyKeyboardMarkup, KeyboardButton

#main menu
btnCurrency = KeyboardButton('💱 Актуальные курсы')
btnExchange = KeyboardButton('💵 Обмен валюты')
btnTransfer = KeyboardButton('🌐 Перестановки')
btnService = KeyboardButton('📋 Услуги компании')
btnAbout = KeyboardButton('🏦 Kiev Exchange 🏦')

mainMenu = ReplyKeyboardMarkup(resize_keyboard = True).add(btnCurrency, btnExchange).add(btnTransfer, btnService).add(btnAbout)


from typing import overload
import numpy as np
from multipledispatch import dispatch
import scipy as sp
sp.random.seed(12345)

class model:

    def __init__(self,data,lr=0.01):
        x=data
        self.m,self.n=x.shape
        self.w=sp.random.normal(loc=0.0, scale=1.0, size=(self.n,1))
        self.b=0
        self.lr=lr
        self.dw=None
        self.db=None

    def normalize(self,X):
        m, n = X.shape

        for i in range(n):
            X = (X - X.mean(axis=0))/X.std(axis=0)
        return X

    def sigmoid(self,z):
        return 1.0/(1 + np.exp(-z))

    def loss(self,y, y_hat):
        loss = -np.mean(y*(np.log(y_hat)) - (1-y)*np.log(1-y_hat))
        return loss

    def gradients(self,X, y, y_hat):
        # Gradient of loss w.r.t weights.
        dw = (1/self.m)*np.dot(X.T, (y_hat - y))
        # Gradient of loss w.r.t bias.
        db = (1/self.m)*np.sum((y_hat - y))
        return dw, db

    def forward(self,x):
        self.x = self.normalize(x)
        z = (np.dot(self.x, self.w) + self.b)
        return z

    def compute_gradient(self,z,y):
        y_hat=self.sigmoid(z)
        self.dw, self.db = self.gradients(self.x, y, y_hat)
        return self.dw,self.db

    def update_model_(self,dw,db,y):
        self.dw=dw
        self.db=db
        # Updating the parameters.
        self.w -= self.lr*self.dw
        self.b -= self.lr*self.db
        l = self.loss(y, self.sigmoid(np.dot(self.x, self.w) + self.b))
        return l
    def update_model(self,dw,db):
        self.dw=dw
        self.db=db
        # Updating the parameters.
        self.w -= self.lr*self.dw
        self.b -= self.lr*self.db

    def get_gradients(self):
        if (self.dw and self.db) is not None:
            return self.dw, self.db
        else:
            return None

    def predict(self, X):
        x = self.normalize(X)
        preds = self.sigmoid(np.dot(x, self.w) + self.b)
        pred_class = []
        pred_class = [1 if i > 0.5 else 0 for i in preds]
        return np.array(pred_class)

    def accuracy(self, y, y_hat):
        accuracy = np.sum(y == y_hat) / len(y)
        return accuracy


import numpy as np
import matplotlib.pyplot as plt
import os

np.random.seed(0)

# Draw 1000 samples from a very long
# period sinusoid
t = np.linspace(0, 1, 300)
z = 1 + 0.1 * np.sin(
    2 * np.pi / 10 * t.reshape(1, -1)
    + 2 * np.pi * np.random.random(size=(1000, 1))
)

# Normalize each sample to the mean
znorm = z / np.mean(z, axis=1).reshape(-1, 1)

# Figure setup
fig, ax = plt.subplots(2, figsize=(12, 8), sharex=True)
ax[1].set_xlabel("time", fontsize=18)
ax[0].set_ylabel("true flux", fontsize=18)
ax[1].set_ylabel("normalized flux", fontsize=18)
ax[0].margins(0, 0.1)
ax[1].margins(0, 0.1)
ax[0].set_rasterization_zorder(2)
ax[1].set_rasterization_zorder(2)

# Plot
for k in range(len(z)):
    ax[0].plot(t, z[k], color="C0", lw=1, alpha=0.1)
    ax[1].plot(t, znorm[k], color="C0", lw=1, alpha=0.1)

# We're done
fig.savefig(
    os.path.abspath(__file__).replace(".py", ".pdf"),
    bbox_inches="tight",
    dpi=300,
)


from django.shortcuts import get_object_or_404
from django.contrib.auth.mixins import UserPassesTestMixin

class ActiveObjectFilterMixin:

    def get_queryset(self):
        queryset = super().get_queryset()
        if not self.request.user.is_authenticated:
            queryset = queryset.filter(is_active=True)
        return queryset

    def get_object(self, queryset=None):
        if not self.request.user.is_authenticated:
            obj = get_object_or_404(self.model, pk=self.kwargs['pk'], is_active=True)
        else:
            obj = get_object_or_404(self.model, pk=self.kwargs['pk'])

        return obj

    def filter_queryset(self, qs):
        title = self.request.GET.get('title')
        if title:
            return qs.filter(title__icontains=title)
        else:
            return qs

class FilterViewMixin:
    filter_class = None
    template_name = None
    paginate_by = 6

    def get_context_data(self, **kwargs):
        obj_list = self.filter_class(self.request.GET, queryset=self.get_queryset())
        context = super().get_context_data(
            object_list=obj_list.qs.order_by('-added_at'),
            **kwargs
        )
        context['filter'] = obj_list
        return context


import sys
from itertools import permutations

input = sys.stdin.readline

N = int(input())

"""
0 -> 1
1 -> 10
배열 2개
  0 1 2 3 4 5 6 7
A 1 0 1 1 2 3 5 8 13 21 34
B 0 1 1 2 3 5 8 13
"""
string_N = str(N)
l = sorted(string_N, reverse=True)
temp = sorted(set(p for p in permutations(string_N, len(string_N))))
answer = temp.index(tuple(string_N))
if answer < len(temp) -1:
    print(''.join(temp[answer + 1]))
else:
    print(0)


from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
es = Elasticsearch([{'host': '130.245.168.89', 'port': 9200}])

def filter_with_query(query, timestamp, limit, sort_by, tags, has_media, accepted):
   must_not ={ "should":[] ,"must": [  { "range": { "timestamp": { "lte": timestamp }}}], 'must_not': [] }

   if len(query) != 0:
      must_not['should'].append(   {"match" : {"title": query }})
      must_not['should'].append(   {"match" : {"body": query }})

   if accepted == True:
      must_not['must'].append({ "exists": {"field": "accepted_answer_id" }}   ) # field must exist

   if has_media == True:
      must_not['must_not'].append( {"terms" : { "media" : [] }} )

   if tags != []:
      for item in tags:
         term = {"terms" : {'tags': [item]}}
         must_not['must'].append(term)

   body={
         "sort" : [{ sort_by : {"order" : "desc"}}],
         "from" : 0, "size" : limit,
         "query" : {
            "constant_score" : {
               "filter" : {
                  "bool" : must_not
               }
            }
         }
      }

   questFilter =[]
   res = es.search(index="test-index", body=body)
   for hit in res['hits']['hits']:
      # print( hit["_source"])
      temp = {
               'id': str(q['_id']),
               'title':q["_source"]['title'],
               'body': q["_source"]['body'],
               'tags': q["_source"]['tags'],
               'answer_count': 0,
               'media': q["_source"]['media'],
               'accepted_answer_id': q["_source"]['accepted_answer_id'] ,
               'user':q["_source"]['user'],
               'timestamp': q["_source"]['timestamp'],
               'score': q["_source"]['score'],
               "view_count": q["_source"]['view_count']
            }
      questFilter.append(temp)


from regexp import quantifiers, matchers, symbols, operators, groups
from regexp import RegularExpression, Expression, Matchable
from cursor import Cursor

# @TODO: Переделать архитектуру (Matchable)

def matches(cursor: Cursor, character: str) -> bool:
  if cursor.current == character:
    cursor.next()
    return True

  return False

def character_token(sub_type: str, value: str):
  return { 'type': f'CHARACTER.{sub_type.upper()}', 'value': value }

def sequence_token(sub_type: str, value):
  return { 'type': f'SEQUENCE.{sub_type.upper()}', 'value': value }

def range_token(sub_type: str, value):
  return { 'type': f'RANGE.{sub_type.upper()}', 'value': value }

def group_token(sub_type: str, name: str, expression: list):
  return { 'type': f'GROUP.{sub_type.upper()}', 'value': { 'name': name, 'expression': expression } }

def quantifier_token(sub_type: str, expression):
  return { 'type': f'QUANTIFIER.{sub_type.upper()}', 'value': expression }

reserved_symbols = ['\\', '.', '[', ']', '(', ')', '{', '}', '^', '|', '*', '+', '?']

def parse(regular_expression: str):
  cursor = Cursor(regular_expression)
  syntax_tree = []

  while not cursor.done:
    if matches(cursor, '\\'):
      syntax_tree.append(character_token('escaped', cursor.current))
      cursor.next()
    elif matches(cursor, '.'):
      syntax_tree.append(character_token('any', '.'))
    elif matches(cursor, '['):
      sequence = []
      inversion = False

      if matches(cursor, '^'):
        inversion = True

      while cursor.current != ']' and (not cursor.done):
        if matches(cursor, '\\'):
          sequence.append(character_token('escaped', cursor.current))
          cursor.next()
          continue

        if cursor.lookup(1) == '-':
          start = character_token('substring', cursor.current)
          cursor.next()
          cursor.next()
          end = character_token('substring', cursor.current)
          cursor.next()

          sequence.append(range_token('common', [ start, end ]))
          continue

        substring = str()

        while (cursor.lookup(1) != '-') and (cursor.current != '\\') and (cursor.current != ']'):
          substring += cursor.current
          cursor.next()

        sequence.append(character_token('substring', substring))

      cursor.next()

      if inversion:
        syntax_tree.append(sequence_token('inversion', sequence))
      else:
        syntax_tree.append(sequence_token('common', sequence))
    elif matches(cursor, '('):
      sub_type = 'ordered'
      name = str()

      # Очевидно, что это state-машина
      if matches(cursor, '?'):
        if matches(cursor, ':'):
          sub_type = 'expression'
        if matches(cursor, '<'):
          sub_type = 'named'

          while cursor.current != '>':
            name += cursor.current
            cursor.next()

          cursor.next()
        if matches(cursor, '\''):
          sub_type = 'named'

          while cursor.current != '\'':
            name += cursor.current
            cursor.next()

          cursor.next()

      level = 0
      sub_expression = str()

      while True:
        if cursor.current == '(': level += 1
        if cursor.current == ')': level = max(0, level - 1)

        sub_expression += cursor.current
        cursor.next()

        if cursor.done or ((cursor.current == ')') and (level == 0)): break

      syntax_tree.append(group_token(sub_type, name, parse(sub_expression)))
    elif matches(cursor, '+'):
      syntax_tree[len(syntax_tree) - 1] = quantifier_token('at_least_once', syntax_tree[len(syntax_tree) - 1])
    elif matches(cursor, '*'):
      syntax_tree[len(syntax_tree) - 1] = quantifier_token('at_least_zero', syntax_tree[len(syntax_tree) - 1])
    elif matches(cursor, '?'):
      syntax_tree[len(syntax_tree) - 1] = quantifier_token('at_most_once', syntax_tree[len(syntax_tree) - 1])
    else:
      substring = str()

      while (not cursor.done) and (cursor.current not in reserved_symbols):
        substring += cursor.current
        cursor.next()

      if substring != str():
        syntax_tree.append(character_token('substring', substring))

      cursor.next()

  return syntax_tree

# (["'])([^"']*)(\0)
expression = RegularExpression([
  groups.Ordered([ matchers.Sequence([ '"', '\'' ]) ]),
  groups.Ordered([ quantifiers.AtLeastZero(matchers.NonSequence([ '"', '\'' ])) ]),
  groups.Ordered([ matchers.Reference('0') ]),
  symbols.Terminate()
])

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

space = matchers.Sequence([ '\t', '\n', '\r', '\v', ' ' ])
multi_space: Matchable = quantifiers.AtLeastZero(space)

def multiline_expression(regular_expression: list[Matchable]) -> Matchable:
  return quantifiers.AtLeastOnce(
    Expression([
      multi_space,
      Expression(regular_expression),
      multi_space
    ])
  )

def read_in_brackets_by_delimiter(brackets: tuple[str, str], delimiter: str) -> Matchable:
  return Expression([
    quantifiers.AtLeastZero(
      groups.Ordered([
        groups.NonCapture(multi_space),
        quantifiers.AtLeastOnce(matchers.NonSequence([delimiter, brackets[1]])),
        groups.NonCapture(
          Expression([
            multi_space,
            matchers.Substring(delimiter),
            multi_space
          ])
        )
      ])
    ),
    groups.Ordered([
      groups.NonCapture(multi_space),
      quantifiers.AtLeastZero(matchers.NonSequence([brackets[1], ' ', '\t', '\n', '\v', '\r'])),
      groups.NonCapture(multi_space)
    ]),
  ])

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

identifier = Expression([
  quantifiers.AtLeastOnce(
    operators.Or([
      matchers.Range('A', 'Z'),
      matchers.Range('a', 'z'),
      matchers.Range('0', '9'),
      matchers.Substring('_')
    ])
  )
])

function_assignation_parser = multiline_expression([
  groups.Separate(
    Expression([
      matchers.Substring('function'),
      multi_space,
      groups.Named('function_name', [ identifier ]),
      multi_space,
      matchers.Substring('('),
      multi_space,
      read_in_brackets_by_delimiter(('(', ')'), ','),
      multi_space,
      matchers.Substring(')'),
      multi_space,
      matchers.Substring('{'),
      multi_space,
      read_in_brackets_by_delimiter(('{', '}'), ';'),
      multi_space,
      matchers.Substring('}')
    ])
  )
])

variables_parser = multiline_expression([
  groups.Separate(
    Expression([
      groups.Named('variable_name', [
        identifier,
        groups.NonCapture(
          Expression([
            multi_space,
            matchers.Substring('=')
          ])
        )
      ]),
      multi_space,
      groups.Named('variable_value', [ quantifiers.AtLeastOnce(matchers.NonSequence([ ';' ])) ]),
      matchers.Substring(';')
    ])
  )
])

callee_parser = multiline_expression([
  groups.Separate(
    Expression([
      groups.Named('callee_name', [
        identifier,
        groups.NonCapture(
          Expression([
            multi_space,
            matchers.Substring('('),
            multi_space
          ])
        )
      ]),
      read_in_brackets_by_delimiter(('(', ')'), ','),
      matchers.Substring(')')
    ])
  )
])

language_parser = quantifiers.AtLeastOnce(
  operators.Or([
    callee_parser,
    function_assignation_parser,
    variables_parser
  ])
)

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

expression_input = '''
  function add(x, y) {
    a = x + y;
    b = x * y;
    return a + b;
  }

  x = 1;
  y = 2;

  add(x, y)
  add(x - 1, y + 1)
'''

language = RegularExpression([ language_parser ])

import json
print(
  json.dumps(
    parse('let[\t\n\r\v]*(?<name>[a-zA-Z0-9_]+)[\t\n\r\v]*=[\t\n\r\v]*(?<value>[^;]+);'),
    indent=2,
    sort_keys=True
  )
)
print(language.match(expression_input))


# -*- coding: utf-8 -*-
"""
@author: Chiu-Cheng-Chun
"""

import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm

#讓matplotlib補上中文顯示
myfont = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')
fig = plt.figure()

t0 = time.clock()

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

"""
定義訓練集
"""
X_Train = mnist.train.images#TrainingSet
X_Train = X_Train[0:8000,:]
Y_Train = mnist.train.labels#TrainingLabels
Y_Train = Y_Train[0:8000,:]

"""
定義變數
"""
W = []
b = []

x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])

"""
定義卷積
"""
def Convolution(inputs, num_of_filters, kernel_size):
    h = tf.layers.conv2d(inputs=inputs, filters=num_of_filters, kernel_size=kernel_size, padding="same", activation=tf.nn.relu)
    return h

"""
定義池化
"""
def Max_pool(conv):
    return  tf.layers.max_pooling2d(inputs=conv, pool_size=[2, 2], strides=2)

"""
定義全聯接層模型
"""
def fc_model(X, n_neurons, W, b):
    for i in range(0, len(n_neurons) - 2):
        X = tf.nn.relu(tf.matmul(X,W[i]) + b[i])
    result = tf.matmul(X, W[-1]) + b[-1]
    return result

"""
定義損失函數，此為交叉熵代價函數
"""
def Cost(y_label, prediction):
    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y_label))
    return cross_entropy

x_image = tf.reshape(x, [-1, 28, 28, 1])

epoches = 1500  #設定迭代次數
learning_rate = 0.01 #學習速率

"""
卷積格式:Convolution(輸入矩陣, filter個數, filter大小)
池化格式:Max_pool(輸入矩陣)
"""
h1 = Convolution(x_image, 5, [5, 5])#第一次卷積
p1 = Max_pool(h1)#第一次池化
h2 = Convolution(p1, 5, [5, 5])#第二次卷積
p2 = Max_pool(h2)#第二次池化

"""
將池化後的3維矩陣攤平成2維矩陣
"""
pool_times = 2
pool_flat = tf.reshape(p2, [-1, 7 * 7 * 5])
input_size = 7 * 7 * 5

n_neurons = [input_size, 100, 10]

"""
建構權重及偏差
"""
for i in range(0, len(n_neurons)-1):
    W.append(tf.Variable(tf.truncated_normal([n_neurons[i],n_neurons[i+1]])))
    b.append(tf.Variable(tf.zeros([1.0 ,n_neurons[i+1]])))

prediction = fc_model(pool_flat, n_neurons, W, b)
cross_entropy = Cost(y, prediction)
train = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)

#--------------------------------------------train-----------------------------------------

sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())

costs = np.ones((epoches+1,1))

"""
開始訓練
"""
for i in range(epoches+1):
    batch_xs, batch_ys = mnist.train.next_batch(150)
    sess.run(train, feed_dict = {x: batch_xs, y: batch_ys})
    costs[i] = sess.run(cross_entropy, feed_dict = {x: batch_xs, y: batch_ys})
    if i % 100 == 0:
        print(i,"epoch(es)", " Done!")

    """
    顯示圖片及其辨識結果
    """
    if i % 200 == 0:
        res = sess.run(prediction, feed_dict = {x: batch_xs[0].reshape(1,784)})
        print("辨識結果為:", res.argmax())
        print("正確結果為:", batch_ys[0].argmax())
        one_pic_arr = np.reshape(batch_xs[0],(28,28))
        pic_matrix = np.matrix(one_pic_arr,dtype = "float")
        plt.imshow(pic_matrix)
        plt.show()

#plot
x_axis = np.arange(epoches)
fig, ax1 = plt.subplots()
ax1.plot(x_axis, costs[1:], "green", label = "Cost")
ax1.set_ylabel('Cost值',fontproperties=myfont);
ax1.set_xlabel('訓練次數',fontproperties=myfont)
ax1.legend(loc=1,prop=myfont)
plt.show()

"""
計算訓練集準確度
"""
correct_prediction = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Train Set Accuracy:", accuracy.eval(feed_dict={x: X_Train, y: Y_Train}) * 100, "%")

"""
開始測試
"""
X_Test = mnist.test.images
Y_Test = mnist.test.labels

correct_prediction = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Test Set Accuracy:", accuracy.eval(feed_dict={x: mnist.test.images, y: mnist.test.labels}) * 100, "%")

print("總共費時:",time.clock()-t0,"秒")


"""
A demo for training Sequential Variational Autoencoders.

python -m dedo.run_svae --logdir=~/local/dedo --num_envs 12 --unsup_algo VAE

tensorboard --logdir=/tmp/dedo --bind_all --port 6006

--unsup_algo choices are: VAE, SVAE, PRED

Note: this code is for research i.e. quick experimentation; it has minimal
comments for now, but if we see further interest from the community -- we will
add further comments, unify the style, improve efficiency and add unittests.

@contactrika

"""
from copy import deepcopy

import gym
import numpy as np
from stable_baselines3.common.env_util import (
    make_vec_env, DummyVecEnv, SubprocVecEnv)
from tensorboardX import SummaryWriter
import torch

from dedo.utils.args import get_args
from dedo.utils.train_utils import init_train, object_to_str
from dedo.vaes.nets import ConvStack
from dedo.vaes.svae import SVAE  # used dynamically
from dedo.vaes.svae_utils import do_logging, fill_seq_bufs_from_rollouts
from dedo.vaes.svae_viz import viz_samples

def get_batch(env, rollout_len):
    x_1toT = []
    act_1toT = []
    mask_1toT = []
    for _ in range(rollout_len):
        act01 = np.random.rand(env.num_envs, env.action_space.shape[-1])
        act = act01*2 - 1.0  # [0,1] -> [-1,1]
        obs, rwd, done, next = env.step(act)
        masks = np.array([[0.0] if done_ else [1.0] for done_ in done])
        x_1toT.append(obs)
        act_1toT.append(act)
        mask_1toT.append(masks)
        if np.random.rand() > 0.9:  # reset envs randomly for data variety
            env_id = np.random.randint(env.num_envs)
            env.env_method('reset', indices=[env_id])
    x_1toT = torch.from_numpy(np.stack(x_1toT)).float()
    act_1toT = torch.from_numpy(np.stack(act_1toT)).float()
    mask_1toT = torch.from_numpy(np.stack(mask_1toT)).float()
    x_1toT = x_1toT.transpose(0, 1).transpose(2, -1).transpose(-2, -1)
    act_1toT = act_1toT.transpose(0, 1)   # put bsz 0th, time 1st
    mask_1toT = mask_1toT.transpose(0, 1)  # put bsz 0th, time 1st
    return x_1toT, act_1toT, mask_1toT

def main(args):
    assert(args.unsup_algo is not None), 'Please provide --unsup_algo'
    if args.cam_resolution not in ConvStack.IMAGE_SIZES:
        print(f'Setting cam_resolution to 512 (was {args.cam_resolution:d})')
        args.cam_resolution = 512  # set default image resolution if needed
    args.logdir, args.device = init_train(args.unsup_algo, args)
    tb_writer = SummaryWriter(args.logdir)
    tb_writer.add_text('args', object_to_str(args))
    print('svae_demo with args:\n', args)
    train_args = deepcopy(args)
    train_args.debug = False  # no debug during training
    train_args.viz = False  # no viz during training
    vec_env = make_vec_env(
        args.env, n_envs=args.num_envs,
        vec_env_cls=SubprocVecEnv if args.num_envs > 1 else DummyVecEnv,
        env_kwargs={'args': train_args})
    vec_env.seed(args.seed)
    print('Created', args.task, 'with observation_space',
          vec_env.observation_space.shape, 'action_space',
          vec_env.action_space.shape)
    #
    # Train unsupervised learner.
    #
    unsup_algo_params = 'PARAMS_'+args.unsup_algo
    unsup_algo_class = 'SVAE'
    svae = eval(unsup_algo_class)(
        im_sz=args.cam_resolution, act_sz=vec_env.action_space.shape[-1],
        params_class=unsup_algo_params, device=args.device)
    optim = torch.optim.Adam(svae.parameters(), lr=args.lr)
    seq_len = svae.pr.past+svae.pr.pred
    rlt_len = 50
    num_inner_epochs = 100 if args.unsup_algo == 'VAE' else 50
    mini_batch_size = 96 if args.unsup_algo == 'VAE' else 24
    if args.unsup_algo == 'PRED':
        mini_batch_size = 16
    vec_env.reset()
    steps_done = 0
    epoch = 0
    print('Getting test env data... ')
    test_x_1toT, test_act_1toT, test_mask_1toT = get_batch(vec_env, 300)
    print('got', test_x_1toT.shape)
    while steps_done < args.total_env_steps:
        print(f'Epoch {epoch:d}: getting train env data... ')
        all_x_1toT, all_act_1toT, all_mask_1toT = get_batch(vec_env, rlt_len)
        all_x_1toT.to(args.device)
        all_act_1toT.to(args.device)
        all_mask_1toT.to(args.device)
        print('got', all_x_1toT.shape)
        steps_done += rlt_len*args.num_envs
        for inner_epoch in range(num_inner_epochs):
            do_log_viz = inner_epoch+1 == num_inner_epochs
            x_1toT, act_1toT = fill_seq_bufs_from_rollouts(
                all_x_1toT, all_act_1toT, all_mask_1toT,
                mini_batch_size, seq_len, args.device)
            optim.zero_grad()
            loss, debug_dict = svae.loss(x_1toT, act_1toT, debug=do_log_viz)
            loss.backward()
            optim.step()
            if do_log_viz:
                do_logging(epoch, debug_dict, {}, tb_writer, 'train')
                viz_samples(svae, x_1toT, act_1toT, epoch, tb_writer, 'train')
                tmp_x_1toT, tmp_act_1toT = fill_seq_bufs_from_rollouts(
                    test_x_1toT, test_act_1toT, test_mask_1toT,
                    mini_batch_size, seq_len, args.device)
                steps_done += seq_len*args.num_envs
                loss, debug_dict = svae.loss(
                    tmp_x_1toT, tmp_act_1toT, debug=do_log_viz)
                do_logging(epoch, debug_dict, {}, tb_writer, 'test')
                viz_samples(svae, tmp_x_1toT, tmp_act_1toT, epoch, tb_writer,
                            'test')
        epoch += 1
    #
    # Clean up.
    vec_env.close()

if __name__ == "__main__":
    main(get_args())


import os
import openai
from functions.functions import load_data_from_file

load_data_from_file(r"data/key.json")
openai.api_key = os.environ["AI_API_KEY"]

response = openai.Completion.create(
  model="text-davinci-003",
  prompt="Correct this to standard English:\n\nShe no went to the market.",
  temperature=0,
  max_tokens=60,
  top_p=1.0,
  frequency_penalty=0.0,
  presence_penalty=0.0
)

print(response)


from sklearn.metrics import *
from collections import Counter
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
import re

def tokenize(sent):
    return [x.strip().lower() for x in re.split('(\W+)?', sent.strip()) if x.strip()]

def map_to_idx(x, vocab):
    return [vocab[w] if w in vocab else vocab["unk"] for w in x]

def map_to_txt(x, vocab):
    textify = map_to_idx(x, inverse_map(vocab))
    return ' '.join(textify)

def inverse_map(vocab):
    return {vocab[item]: item for item in vocab}

def concat_in_out(X, Y, vocab):
    numex = X.shape[0] # num examples
    glue = vocab["delimiter"]*np.ones(numex).reshape(numex,1)
    inp_train = np.concatenate((X,glue,Y),axis=1)
    return inp_train

def load_data(train, vocab, labels = {'0':0,'1':1,0:0,1:1}):
    X,Y,Z = [],[],[]
    for p,h,l in train:
        p = map_to_idx(tokenize(p), vocab)
        h = map_to_idx(tokenize(h), vocab)
        if l in labels:
            X += [p]
            Y += [h]
            Z += [labels[l]]
    return X,Y,Z

def load_test_data(test, vocab, labels = {'0':0,'1':1,0:0,1:1}):
    X,Y = [],[]
    for p,h in test:
        p = map_to_idx(tokenize(p), vocab)
        h = map_to_idx(tokenize(h), vocab)
        X += [p]
        Y += [h]
    return X,Y

def get_vocab(data):
    vocab = Counter()
    for ex in data:
        tokens = tokenize(ex[0])
        tokens += tokenize(ex[1])
        vocab.update(tokens)
    tokens = ["unk", "delimiter", "pad_tok"] + [x for x, y in sorted(vocab.iteritems()) if y > 0]
    vocab = {y:x for x,y in enumerate(tokens)}
    return vocab

def plot_attention(matrix, title='Attention matrix', cmap=plt.cm.Blues, labels=None):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    cax = ax.matshow(matrix, cmap=cmap)
    plt.title(title)
    fig.colorbar(cax)
    if labels:
        ax.set_xticklabels([''] + labels[1])
        ax.set_yticklabels([''] + labels[0])
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
    ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
    plt.show()

def getPRCurveKeras (x_test, y_test, model, label='DL Model'):
    p_proba = model.predict(x_test)
    precision, recall, thresholds = precision_recall_curve(y_test, p_proba)
    plt.plot(recall, precision, label='{0} (AUC = {1:0.2f})'.format(label, auc(recall, precision)))

def getAccuracy(x_test, y_test, model):
    pred = model.predict(x_test)
    pred = np.round(pred)
    s = 0
    for i in range(len(pred)):
        if pred[i][0] == y_test[i]:
            s += 1
    return s/float(len(y_test))

def getROCCurveKeras (x_test, y_test, model, label='DL Model'):
    p_proba = model.predict(x_test)
    fpr, tpr, thresholds = roc_curve(y_test, p_proba)
    plt.plot(fpr, tpr, label='{0} (AUC = {1:0.2f})'.format(label, auc(fpr, tpr)))