# SVM Optimization method implementation by Niharika Gauraha

from cvxopt import matrix, solvers
import numpy as np
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
import svmUtils as utils

# svm Optimization Problem
# X: design matrix
# y:  labels {+1, -1}
# K: kernel
# C: tuning parameter for slack variables
def svmOptProb(X, y, kernel="linear", C=None, param = None):
    nSamples, nFeatures = X.shape

    if kernel == "linear":
        kernelMethod = utils.linearKernel
    elif kernel == "poly":
        kernelMethod = utils.polyKernel
    else:
        kernelMethod = utils.rbfKernel

    # compute the matrix K (nSamples X nSamples) using kernel function
    K = np.zeros((nSamples, nSamples))
    for i in range(nSamples):
        for j in range(nSamples):
            K[i, j] = kernelMethod(X[i,:], X[j,:], param)

    P = matrix(np.outer(y, y) * K)
    q = matrix(np.ones(nSamples)* -1)
    A = matrix(y, (1, nSamples), tc="d")
    b = matrix(0.0)

    if C is None:
        G = matrix(np.diag(np.ones(nSamples) * -1))
        h = matrix(np.zeros(nSamples))
    else:
        tmp1 = np.diag(np.ones(nSamples) * -1)
        tmp2 = np.identity(nSamples)
        G = matrix(np.vstack((tmp1, tmp2)))
        tmp1 = np.zeros(nSamples)
        tmp2 = np.ones(nSamples) * C
        h = matrix(np.hstack((tmp1, tmp2)))

    # solve the QP problem
    sol = solvers.qp(P, q, G, h, A, b)

    # Lagrange multipliers
    alpha = np.ravel(sol['x'])

    #alpha = np.array(sol['x'])
    # Support vectors have non zero lagrange multipliers
    sv = alpha > 1e-3  # tolerance
    ind = np.arange(len(alpha))[sv]
    alpha = alpha[sv]
    print("%d support vectors out of %d points" % (len(alpha), nSamples))
    sv_x = X[sv]
    sv_y = y[sv]

    bias = 0

    for n in range(len(alpha)):
        bias += sv_y[n] - np.sum(alpha * sv_y * K[ind[n], sv])

    bias /= len(alpha)

    # Weight vector
    if kernel=="linear":
        w = np.zeros(nFeatures)
        for n in range(len(alpha)):
            w += alpha[n] * sv_y[n] * sv_x[n]
    else:
        w = None

    clf = {}
    clf['sv_x'] = sv_x
    clf['sv_y'] = sv_y
    clf['alpha'] = alpha
    clf['w'] = w
    clf['b'] = bias
    clf['kernel'] = kernelMethod
    clf['kernelParam'] = param

    return clf

def funcLine(x, w, b, c=0):
    # given x1, return x2 such that [x1,x2] satisfy the line w.x + b = c
    return (-w[0] * x - b + c) / w[1]

# for plotting margin
def plotSvmMargin(X, y, clf):
    sv = clf['sv_x']
    X1_train = X[y == 1]
    X2_train = X[y == -1]

    plt.plot(X1_train[:, 0], X1_train[:, 1], "ro")
    plt.plot(X2_train[:, 0], X2_train[:, 1], "bo")
    plt.scatter(sv[:, 0], sv[:, 1], s=100, c="g")

    # plot the separating line w.x + b = 0
    a0 = -8;
    a1 = funcLine(a0, clf['w'], clf['b'])
    b0 = 4;
    b1 = funcLine(b0, clf['w'], clf['b'])
    plt.plot([a0, b0], [a1, b1], "k")

    # plot the supporting lines, w.x + b = 1
    a0 = -8;
    a1 = funcLine(a0, clf['w'], clf['b'], 1)
    b0 = 4;
    b1 = funcLine(b0, clf['w'], clf['b'], 1)
    plt.plot([a0, b0], [a1, b1], "k--")

    # plot the supporting lines, w.x + b = -1
    a0 = -8;
    a1 = funcLine(a0, clf['w'], clf['b'], -1)
    b0 = 4;
    b1 = funcLine(b0, clf['w'], clf['b'], -1)
    plt.plot([a0, b0], [a1, b1], "k--")

    plt.axis("tight")
    plt.show()

# test linearly separable case
def testSVM():
    #generate linearly sep data
    X, y = make_blobs(n_samples=100, centers=2, center_box=(-4, 4), random_state=9)
    y[y==0] = -1
    clf = svmOptProb(X, y)
    #plotSvmMargin(X, y, clf)
    utils.plot_contour(X, y, clf)

# test linearly separable but overlapping case
def testSVMSoftMargin():
    #generate linearly sep data
    X, y = make_blobs(n_samples=100, centers=2, center_box=(-4, 3), random_state=9)
    y[y==0] = -1
    clf = svmOptProb(X, y, C=1)
    utils.plot_contour(X, y, clf=clf)
    #plotSvmMargin(X, y, clf)

def testSVMNonLinSep():
    X, y = utils.genNonLinSepData()
    y[y==0] = -1
    clf = svmOptProb(X, y, kernel="rbf", C=1)
    utils.plot_contour(X, y, clf)

def testSVMCirSepdata():
    X, y = utils.genCircularSepData(100)
    clf = svmOptProb(X, y, kernel="poly", C=1, param = 2)
    #clf = svmOptProb(X, y, kernel="rbf", C=1000, param  = .5)
    utils.plot_contour(X, y, clf)

if __name__ == "__main__":
    #testSVMCirSepdata()
    #testSVMSoftMargin()
    testSVM()


import click

from muon.database.database import Database

def main():
    database = Database()
    with database.conn as conn:
        query = """
            ALTER TABLE sources ADD COLUMN source_type
                INTEGER NOT NULL DEFAULT 0;
        """
        conn.execute(query)
        conn.commit()

        query = "SELECT source_id,source_type FROM sources"
        cursor = conn.execute(query)
        query = "UPDATE sources SET source_type=? WHERE source_id=?"

        for source_id, source_type in cursor:
            if 'SIMS' in source_id and source_type != 1:
                conn.execute(query, (1, source_id))
        conn.commit()

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
from collections import OrderedDict

from .module import Module

class Sequential(Module):
    r"""A sequential container.
    Modules will be added to it in the order they are passed in the constructor.
    Alternatively, an ordered dict of modules can also be passed in.

    Examples:

        .. testcode::

            import numpy as np
            import megengine as mge
            import megengine.module as M
            import megengine.functional as F
            from collections import OrderedDict

            batch_size = 64
            data = mge.tensor(np.zeros((batch_size, 28 * 28)), dtype=np.float32)
            label = mge.tensor(np.zeros(batch_size,), dtype=np.int32)

            net0 = M.Sequential(
                    M.Linear(28 * 28, 320),
                    M.Linear(320, 10)
                )
            pred0 = net0(data)

            modules = OrderedDict()
            modules["fc0"] = M.Linear(28 * 28, 320)
            modules["fc1"] = M.Linear(320, 10)
            net1 = M.Sequential(modules)
            pred1 = net1(data)
    """

    def __init__(self, *args, **kwargs):
        super().__init__(**kwargs)
        self.layer_keys = []
        if len(args) == 1 and isinstance(args[0], OrderedDict):
            for key, module in args[0].items():
                # self.add_module(key, module)
                setattr(self, key, module)
                self.layer_keys.append(key)
        else:
            for idx, module in enumerate(args):
                # self.add_module(str(idx), module)
                setattr(self, str(idx), module)
                self.layer_keys.append(str(idx))

    def __getitem__(self, idx):
        if isinstance(idx, slice):
            return self.__class__(
                OrderedDict(zip(self.layer_keys[idx], self.layer_values[idx]))
            )
        else:
            return getattr(self, self.layer_keys[idx])

    def __setitem__(self, idx, module):
        key = self.layer_keys[idx]
        return setattr(self, key, module)

    def __delitem__(self, idx):
        if isinstance(idx, slice):
            for key in self.layer_keys[idx]:
                delattr(self, key)
                del self.layer_keys[idx]
        else:
            delattr(self, self.layer_keys[idx])
            del self.layer_keys[idx]

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

    def __iter__(self):
        return iter(self.layer_values)

    @property
    def layer_values(self):
        return [getattr(self, key) for key in self.layer_keys]

    def forward(self, inp):
        # avoid layer_values as a name prefix, see Module.__getattribute__
        for layer in [getattr(self, key) for key in self.layer_keys]:
            inp = layer(inp)
        return inp


import os
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications.vgg16 import VGG16
import keras
from tensorflow.keras.optimizers import RMSprop
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers
base_dir="/home/ashifer/code/datasets/cats_and_dogs_filtered"
train_dir=os.path.join(base_dir,'train')
validation_dir=os.path.join(base_dir,'validation')
trained_model=VGG16(input_shape=(150,150,3),include_top=False,weights='imagenet')
for layer in trained_model.layers:
    layer.trainable=False
trained_model.summary()
last_layer = trained_model.get_layer('block5_pool')
print('last layer output shape: ', last_layer.output_shape)
last_output = last_layer.output
x=tf.keras.layers.Flatten()(last_output)
x=tf.keras
model=tf.keras.Model(trained_model.input)
x = layers.Flatten()(last_output)
x = layers.Dense(1024, activation='relu')(x)
x = layers.Dropout(0.2)(x)
x = layers.Dense  (1, activation='sigmoid')(x)
model = tf.keras.Model(trained_model.input, x)
model.summary()
model.compile(optimizer = RMSprop(lr=0.0001), loss = 'binary_crossentropy', metrics = ['accuracy'])
train_datagen = ImageDataGenerator( rescale = 1.0/255. )
test_datagen  = ImageDataGenerator( rescale = 1.0/255. )
train_generator=train_datagen.flow_from_directory(train_dir,batch_size=20,class_mode='binary',target_size=(150,150))
valid_generator=test_datagen.flow_from_directory(validation_dir,batch_size=20,class_mode='binary',target_size=(150,150))
history=model.fit(train_generator,steps_per_epoch=50,epochs=2,verbose=1,validation_data=valid_generator,validation_steps=50)
model.save("vgg_cats.h5")
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
plt.show()


# Create a New window in
from tkinter import *
from tkinter import messagebox
root=Tk()
root.title("Create New Window ")
root.geometry("500x500+700+150")
root.resizable(width=False,height=False)

def create_new_window():
    new_win = Toplevel()
    new_win.title("New window")
    new_win.geometry("300x300")

    Label(new_win, text="Welcome Mahid").pack()

    Button(new_win, text="Close Window",command=new_win.destroy).pack()

btn=Button(root, text="Create a New Window", command=create_new_window)
btn.pack()

root.mainloop()

Graphical User Interface (GUI)/New_Window.py


import os, sys

import shlex
import glob
import json
from tqdm import tqdm
from multiprocessing import Pool
from unifold.msa.mmcif import parse

rot_keys = """_pdbx_struct_oper_list.matrix[1][1]
_pdbx_struct_oper_list.matrix[1][2]
_pdbx_struct_oper_list.matrix[1][3]
_pdbx_struct_oper_list.matrix[2][1]
_pdbx_struct_oper_list.matrix[2][2]
_pdbx_struct_oper_list.matrix[2][3]
_pdbx_struct_oper_list.matrix[3][1]
_pdbx_struct_oper_list.matrix[3][2]
_pdbx_struct_oper_list.matrix[3][3]""".split(
    "\n"
)
tran_keys = """_pdbx_struct_oper_list.vector[1]
_pdbx_struct_oper_list.vector[2]
_pdbx_struct_oper_list.vector[3]""".split(
    "\n"
)

def process_block_to_dict(content):
    ret = {}
    lines = content.split("\n")
    if lines[0] == "loop_":
        keys = []
        values = []
        last_val = []
        for line in lines[1:]:
            line = line.strip()
            if line.startswith("_"):
                keys.append(line)
            else:
                num_key = len(keys)
                cur_vals = shlex.split(line)
                last_val.extend(cur_vals)
                assert len(last_val) <= num_key, (
                    num_key,
                    len(last_val),
                    last_val,
                    cur_vals,
                )
                if len(last_val) == num_key:
                    values.append(last_val)
                    last_val = []
        if last_val:
            assert len(last_val) == num_key
            values.append(last_val)
        for i, k in enumerate(keys):
            ret[k] = [vals[i] for vals in values]
    else:
        last_val = []
        for line in lines:
            t = shlex.split(line)
            last_val.extend(t)
            if len(last_val) == 2:
                ret[last_val[0]] = [last_val[1]]
                last_val = []
        if last_val:
            assert len(last_val) == 2
            ret[last_val[0]] = [last_val[1]]
    return ret

def get_transform(data, idx):
    idx = int(idx) - 1
    rot = []
    for key in rot_keys:
        rot.append(float(data[key][idx]))
    trans = []
    for key in tran_keys:
        trans.append(float(data[key][idx]))
    rot, trans = tuple(rot), tuple(trans)
    if rot == (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0) and trans == (
        0.0,
        0.0,
        0.0,
    ):
        return "I"
    else:
        return rot, trans

def parse_assembly(mmcif_path):
    name = os.path.split(mmcif_path)[-1].split(".")[0]
    with open(mmcif_path) as f:
        mmcif_string = f.read()
        mmcif_lines = mmcif_string.split("\n")
    parse_result = parse(file_id="", mmcif_string=mmcif_string)
    if "No protein chains found in this file." in parse_result.errors.values():
        return name, [], [], [], "no protein"
    mmcif_obj = parse_result.mmcif_object
    if mmcif_obj is None:
        print(name, parse_result.errors)
        return name, [], [], [], "parse error"
    mmcif_to_author_chain_id = mmcif_obj.mmcif_to_author_chain_id
    valid_chains = mmcif_obj.valid_chains.keys()
    valid_chains = set(valid_chains)  # valid chains is not auth_id
    new_section = False
    is_loop = False
    cur_lines = []
    assembly = None
    assembly_gen = None
    oper = None
    error_type = None
    try:
        for line in mmcif_lines:
            line = line.strip().replace(";", "")
            if not line:
                continue
            if line == "#":
                cur_str = "\n".join(cur_lines)
                if "revision" in cur_str:
                    continue
                if "_pdbx_struct_assembly.id" in cur_str:
                    assembly = process_block_to_dict(cur_str)
                if "_pdbx_struct_assembly_gen.assembly_id" in cur_str:
                    assembly_gen = process_block_to_dict(cur_str)
                if "_pdbx_struct_oper_list.id" in cur_str:
                    oper = process_block_to_dict(cur_str)
                cur_lines = []
            else:
                cur_lines.append(line)
    except Exception as e:
        print(name, e)
        return name, [], [], [], "parse"
    if not (assembly is not None and assembly_gen is not None and oper is not None):
        return name, [], [], [], "miss"
    try:
        counts = assembly["_pdbx_struct_assembly.oligomeric_count"]
        asym_id = assembly_gen["_pdbx_struct_assembly_gen.assembly_id"]
        op_idx = assembly_gen["_pdbx_struct_assembly_gen.oper_expression"]
        assembly_chains = assembly_gen["_pdbx_struct_assembly_gen.asym_id_list"]
        chains = []
        chains_ops = []
        for i, j in enumerate(asym_id):
            if j == "1":
                sss = (
                    op_idx[i]
                    .replace("(", "")
                    .replace(")", "")
                    .replace("'", "")
                    .replace('"', "")
                )
                if "-" in sss:
                    s, t = sss.split("-")
                    indices = range(int(s), int(t) + 1)
                else:
                    indices = sss.split(",")
                for idx in indices:
                    chains.append(assembly_chains[i].split(","))
                    chains_ops.append(get_transform(oper, idx))
        len_ops = len(chains)
        count = int(counts[0])
        all_chains = []
        all_chains_ops = []
        all_chains_label = []
        for i, cur_chains in enumerate(chains):
            for chain in cur_chains:
                if chain not in valid_chains:
                    continue
                auth_chain = mmcif_to_author_chain_id[chain]
                all_chains_label.append(chain)
                all_chains.append(auth_chain)
                all_chains_ops.append(chains_ops[i])
        return name, all_chains, all_chains_label, all_chains_ops, "success"
    except Exception as e:
        print(name, e)
        return name, [], [], [], "index"

input_dir = sys.argv[1]
output_file = sys.argv[2]
input_files = glob.glob(input_dir + "*.cif")

file_cnt = len(input_files)
meta_dict = {}
failed = []
with Pool(64) as pool:
    for ret in tqdm(pool.imap(parse_assembly, input_files), total=file_cnt):
        name, all_chains, all_chains_label, all_chains_ops, error_type = ret
        if all_chains:
            meta_dict[name] = {}
            meta_dict[name]["chains"] = all_chains
            meta_dict[name]["chains_label"] = all_chains_label
            meta_dict[name]["opers"] = all_chains_ops
        else:
            failed.append(name + " " + error_type)

json.dump(meta_dict, open(output_file, "w"), indent=2)

def write_list_to_file(a, file):
    with open(file, "w") as output:
        for x in a:
            output.write(str(x) + "\n")

write_list_to_file(failed, "failed_mmcif.txt")


def maxOfSubarray(arr, n, k):
    # write your code here
    # return a list denoting the answer.
    i, j = 0, 0
    out = []
    res = []
    while j < n:
        out.append(arr[j])
        if j - i + 1 < k:
            j += 1
        elif j - i + 1 == k:
            val = max(out)
            res.append(val)
            out.pop(0)
            i += 1
            j += 1

    return res

# arr = [10, 7, 8, 11]
# n = len(arr)
# k = 2

# print(maxOfSubarray(arr, n, k))
if __name__ == "__main__":
    arr = []
    T = int(input())
    for i in range(T):
        val = input().split()
    print(val, type(val))
    print(type(val[0]), type(val[1]))
    for i in range(int(val[0])):
        # print(i)
        arr.append(input())

    print(arr)
    print(type(arr[0]))
    arr = list(map(int, arr))
    print(arr)
    print(type(arr))

    print(maxOfSubarray(arr, int(val[0]), int(val[1])))


import random

COMPARE_EXCHANGE_REPS = 4

def swap(arr, i, j):
    '''
    menukar arr[i] dan arr[j] secara in-place
    '''
    arr[i], arr[j] = arr[j], arr[i]

def compare_swap(arr, i, j):
    '''
    melakukan perbandingan antara arr[i] dan arr[j],
    jika urutan mereka terbalik maka lakukan swap()
    '''
    if ((i < j and arr[i] > arr[j]) or (i > j and arr[i] < arr[j])):
        swap(arr, i, j)

def permute_random(arr):
    '''
    melakukan shuffling untuk array arr secara in-place
    '''
    for i in range(len(arr)):
        j = random.randint(i, len(arr) - 1)
        swap(arr, i, j)

def compare_regions(arr, s, t, offset):
    '''
    membandingkan dan menukar dua region yang ditandai oleh arr[s] dan arr[t]
    '''
    mate = list(range(offset))
    for _ in range(COMPARE_EXCHANGE_REPS):
        permute_random(mate)
        for i in range(offset):
            compare_swap(arr, s + i, t + mate[i])

def randomized_shell_sort(arr: list) -> None:
    '''
    asumsi: panjang arr adalah pangkat 2
    '''
    n = len(arr)
    offset = n // 2
    while offset > 0: # log n iterations
        for i in range(0, n - offset, offset):
            compare_regions(arr, i, i + offset, offset)
        for i in range(n - offset, offset - 1, -offset):
            compare_regions(arr, i - offset, i, offset)
        for i in range(0, n - 3 * offset, offset):
            compare_regions(arr, i, i + 3 * offset, offset)
        for i in range(0, n - 2 * offset, offset):
            compare_regions(arr, i, i + 2 * offset, offset)
        for i in range(0, n, 2 * offset):
            compare_regions(arr, i, i + offset, offset)
        for i in range(offset, n - offset, 2 * offset):
            compare_regions(arr, i, i + offset, offset)
        offset //= 2


import unittest
import unittest.mock
import time
import pandas as pd
from open_captcha.common_types import Challenge, ServerContext
from open_captcha.captcha_generator import (
    _get_timestamp, _generate_challenge_id, _verify_timeout, _verify_text_is_close, CaptchaGenerator
)
from tests.fake_template import QuestTemplate

class HelperFunctionsTest(unittest.TestCase):
    def test_get_timestamp(self):
        t = _get_timestamp()
        self.assertIsInstance(t, int)
        time.sleep(1.1)
        t2 = _get_timestamp()
        self.assertGreaterEqual(t2 - t, 1)
        self.assertLessEqual(t2 - t, 3)

    def test_generate_challenge_id(self):
        challenge_ids = {_generate_challenge_id() for _ in range(10)}
        self.assertEqual(len(challenge_ids), 10)  # no duplicates
        for cid in challenge_ids:
            assert isinstance(cid, str)
            self.assertEqual(len(cid), 32)

    @unittest.mock.patch('open_captcha.captcha_generator._get_timestamp')
    def test_verify_timeout(self, mock_get_timestamp):
        t0 = 100
        delta = 10
        mock_get_timestamp.return_value = t0 + delta
        self.assertEqual(_verify_timeout(t0, timeout=delta), True)
        mock_get_timestamp.assert_called_once_with()
        self.assertEqual(_verify_timeout(t0, timeout=delta - 1), False)

    def test_verify_text_is_close(self):
        self.assertEqual(_verify_text_is_close('abcde', 'abcde', 5), True)
        self.assertEqual(_verify_text_is_close('abcde', 'abc', 5), False)
        self.assertEqual(_verify_text_is_close('abcde', 'XbcdX', 5), False)
        self.assertEqual(_verify_text_is_close('abcdef', 'XbcdXf', 5), True)
        self.assertEqual(_verify_text_is_close('abcde', 'XbcdX', 4), True)
        self.assertEqual(_verify_text_is_close('abcd', 'XbcdX', 4), False)

class CaptchaGeneratorTest(unittest.TestCase):
    def setUp(self):
        super().setUp()

        self.data = {
            'report_counts': [
                dict(city_name='New York', num_symptoms=9666, num_deaths=123),
                dict(city_name='Los Angeles', num_symptoms=5000, num_deaths=23),
                dict(city_name='Boston', num_symptoms=800, num_deaths=250),
                dict(city_name='Detroit', num_symptoms=0, num_deaths=1),
                dict(city_name='West Yellowstone', num_symptoms=5, num_deaths=2),
            ]
        }

        self.template_configs = [
            ('quest', dict()),
            ('quest', dict(quest='peace')),
            ('quest', dict(quest='some quiet')),
        ]
        self.mock_timeout = unittest.mock.Mock()
        self.mock_typos = unittest.mock.Mock()
        self.mock_rendering_options = unittest.mock.Mock()

    def _get_captcha_generator(self, data, template_configs, verify_config=True):
        return CaptchaGenerator(
            data=data,
            template_configs=template_configs,
            response_timeout_sec=self.mock_timeout,
            num_letters_per_allowed_typo=self.mock_typos,
            verify_config=verify_config
        )

    @unittest.mock.patch('open_captcha.captcha_generator.RNG')
    def test_configuration_sanity(self, mock_RNG):
        captcha = self._get_captcha_generator(self.data, self.template_configs)
        self.assertEqual(captcha.data.keys(), {'report_counts'})
        pd.testing.assert_frame_equal(
            captcha.data['report_counts'],
            pd.DataFrame.from_records(self.data['report_counts'])
        )
        self.assertEqual(len(captcha.templates), len(self.template_configs))
        for template in captcha.templates:
            self.assertIsInstance(template, QuestTemplate)
        mock_RNG.assert_called_once_with(None)
        self.assertEqual(captcha._non_crypto_rng, mock_RNG.return_value)

    def test_bad_configs(self):
        template_configs = [
            ('quest', dict()),
            ('quest', dict(quest='peace', error_msg='boom!')),
        ]
        self._get_captcha_generator(self.data, template_configs, verify_config=False)
        with self.assertRaisesRegex(Exception, 'boom!'):
            self._get_captcha_generator(self.data, template_configs)

    @unittest.mock.patch('open_captcha.captcha_generator._get_timestamp')
    @unittest.mock.patch('open_captcha.captcha_generator._generate_challenge_id')
    def test_generate_challenge(self, mock_generate_challenge_id, mock_get_timestamp):
        # Arrange
        silly_challenge = Challenge('stuff', b'some bytes', ['A', 'B', 'C'])
        correct_answer = 'correct!'
        mock_template = unittest.mock.Mock()
        mock_template.generate_challenge.return_value = silly_challenge, correct_answer
        mock_rng = unittest.mock.Mock()
        mock_rng.choice.return_value = mock_template
        captcha = self._get_captcha_generator(self.data, self.template_configs)
        captcha._non_crypto_rng = mock_rng
        attempt_number = 666

        # Act
        challenge_id, challenge, context = captcha.generate_challenge(attempt_number, self.mock_rendering_options)

        # Assert
        mock_template.generate_challenge.assert_called_once_with(captcha.data, mock_rng, self.mock_rendering_options)
        self.assertEqual(challenge_id, mock_generate_challenge_id.return_value)
        self.assertEqual(challenge, silly_challenge)
        expected_context = ServerContext(
            timestamp=mock_get_timestamp.return_value,
            verification_attempt_number=attempt_number,
            correct_answer=correct_answer
        )
        self.assertEqual(context, expected_context)

    @unittest.mock.patch('open_captcha.captcha_generator._verify_text_is_close')
    @unittest.mock.patch('open_captcha.captcha_generator._verify_timeout')
    def test_verify_response(self, mock_verify_timeout, mock_verify_text):
        captcha = self._get_captcha_generator(self.data, self.template_configs)
        context = ServerContext(
            timestamp=456,
            verification_attempt_number=666,
            correct_answer='42'
        )
        user_answer = 'The butler did it'

        def check(timeout_ok, text_ok):
            mock_verify_timeout.reset_mock()
            mock_verify_text.reset_mock()
            mock_verify_timeout.return_value = timeout_ok
            mock_verify_text.return_value = text_ok
            is_ok = captcha.verify_response(user_answer, context)
            self.assertEqual(is_ok, timeout_ok and text_ok)
            mock_verify_timeout.assert_called_once_with(context.timestamp, self.mock_timeout)
            if timeout_ok:
                mock_verify_text.assert_called_once_with(context.correct_answer, user_answer, self.mock_typos)
            else:
                mock_verify_text.assert_not_called()

        check(False, False)
        check(False, True)
        check(True, False)
        check(True, True)

if __name__ == '__main__':
    unittest.main()


import sys, queue
sys.path.append('../common')
import toolbox as tb
import files as f
import strings as s
import numpy as np

sys.setrecursionlimit(10000)

# https://rosalind.info/problems/ctea

# Levenstein distance adapted to python from
# https://en.wikipedia.org/wiki/Levenshtein_distance
def mineditlen(s, t):
    m = len(s)
    n = len(t)
    C = np.ndarray((m+1, n+1))
    for i in range(m+1):
        C[i,0] = int(i)
    for j in range(n+1):
        C[0,j] = int(j)

    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if s[i-1] == t[j-1]:
                substcost = 0
            else:
                substcost = 1
            C[i,j] = int(min(C[i-1,j]+1, C[i,j-1]+1, C[i-1,j-1] + substcost))

    return C, C[m,n]

def backtrack(C, s, t):
    i = len(s)
    j = len(t)
    s2 = ''
    t2 = ''
    while i!=0 or j != 0:
        minval = min(C[i-1,j-1], C[i-1,j], C[i,j-1])
        if minval == C[i-1,j-1]: # subst or match
            s2 = s[i-1] + s2
            t2 = t[j-1] + t2
            i-=1
            j-=1
        elif minval == C[i-1,j]: # ins?
            s2 = s[i-1] + s2
            t2 = '-' + t2
            i -= 1
        else:
            s2 = '-' + s2
            t2 = t[j-1] + t2
            j -= 1

    return s2, t2

def backtrack2(C, s, t):
    q = queue.Queue()
    seen = set()
    q.put((C, s, t, ''))
    n = 1

    while not q.empty():
        C, s, t, p = q.get()
        i = len(s)
        j = len(t)
        # if (C, s, t) in seen:
        #     continue
        seen.add((C, s, t))
        print(f'({i},{j}) {s} {t}')
        print(p)
        print(C)

        if i == 0 or j == 0:
            print(n, n % 134217727)
            return

        diag = C[i-1,j-1]
        up = C[i-1,j]
        left = C[i,j-1]
        minval = min(diag, left, up)

        # At least two are equal
        count = -1
        dir = ''
        if minval == diag:
            dir += 'diag '
            q.put((C[0:i,0:j], s[:i-1], t[:j-1], f'{p} ({i}, {j})'))
            count += 1
        if minval == up:
            dir += 'up '
            q.put((C[0:i,0:j+1], s[:i-1], t[:j], f'{p} ({i}, {j})'))
            count += 1
        if minval == left:
            dir += 'left '
            q.put((C[0:i+1,0:j], s[:i], t[:j-1], f'{p} ({i}, {j})'))
            count += 1

        if count > 0:
            dir = 'branch ' + dir

        print(dir)

        n *= (2**count)
        continue

#
# #
#

filename = f.filefromargv(sys.argv)
lines = f.readlines(filename)
n, names, seqs = f.readfasta(lines)
#print(n, names, strings)

assert n == 2

s1 = seqs[0]
m = len(s1)
s2 = seqs[1]
n=len(s2)
print(f's1 len: {len(s1)}')
print(f's2 len: {len(s2)}')
c, l = mineditlen(s1,s2)
print('len:', l)

backtrack2(c, s1, s2)


#!/usr/bin/env python3

import numpy as np

class ODESolver:
    def __init__(self, f):
        self.f = f

    def reset(self, x_start=None, t_start=0.0):
        self.x = x_start
        self.t = t_start

    def compute(self, dt, solver="MIDPOINT"):
        x0, x1, T = [self.x[0]], [self.x[1]], [self.t] # Init

        while self.x[0] > 0.0:
            if solver == "EULER":
                self.x += self.f(self.x, self.t) * dt
            elif solver == "MIDPOINT":
                x_mp = self.x + self.f(self.x, self.t) * dt/2
                self.x += self.f(x_mp, self.t + dt/2) * dt
            elif solver == "RK2":
                k1 = self.f(self.x, self.t)
                k2 = self.f(self.x + k1 * dt, self.t + dt)
                self.x += 0.5 * (k1 + k2) * dt
            elif solver == "RK4":
                k1 = self.f(self.x, self.t)
                k2 = self.f(self.x + k1 * dt/2, self.t + dt/2)
                k3 = self.f(self.x + k2 * dt/2, self.t + dt/2)
                k4 = self.f(self.x + k3 * dt, self.t + dt)
                self.x += 1/6 * (k1 + 2*k2 + 2*k3 + k4) * dt
            else:
                raise ValueError("Invalid solver method")

            self.t += dt

            x0.append(np.copy(self.x[0])) # Altitude
            x1.append(np.copy(self.x[1])) # Velocity
            T.append(self.t)

        return np.array([x0, x1]), T


from AnyQt.QtGui import (
    QColor, QBrush, QPalette, QFont, QTextDocument,
    QSyntaxHighlighter, QTextCharFormat, QTextCursor, QKeySequence,
)
from AnyQt.QtCore import Qt, QRegExp, QByteArray, QItemSelectionModel

class CsHighlighter(QSyntaxHighlighter):
    def __init__(self, parent):
        QSyntaxHighlighter.__init__(self, parent)

        self._multiLineCommentFormat = QTextCharFormat()
        self._multiLineCommentFormat.setForeground(Qt.lightGray)

        self._commentStartExpression = QRegExp("/\\*")
        self._commentEndExpression = QRegExp("\\*/")

        self.createKeywords()
        self.createOperators()
        self.createStrings()
        self.createArrows()
        self.createComments()

    def createKeywords(self):
        keywordFormat = QTextCharFormat()
        keywordFormat.setForeground(QColor(64, 155, 11))

        keywords = [
            '\\babstract\\b', '\\bas\\b', '\\bbase\\b', '\\bbool\\b',
            '\\bbreak\\b', '\\bbyte\\b', '\\bcase\\b', '\\bcatch\\b',
            '\\bchar\\b', '\\bchecked\\b', '\\bclass\\b', '\\bconst\\b',
            '\\bcontinue\\b', '\\bdecimal\\b', '\\bdefault\\b',
            '\\bdelegate\\b', '\\bif\\b', '\\bimplicit\\b', '\\bin\\b',
            '\\bdo\\b', '\\bdouble\\b', '\\bint\\b', '\\binterface\\b',
            '\\belse\\b', '\\benum\\b', '\\bevent\\b', '\\bexplicit\\b',
            '\\bextern\\b', '\\bfalse\\b', '\\btrue\\b', '\\binternal\\b',
            '\\bfinally\\b', '\\bfixed\\b', '\\bfloat\\b', '\\bfor\\b',
            '\\bforeach\\b', '\\bgoto\\b', '\\bis\\b', '\\block\\b',
            '\\blong\\b', '\\bnamepsace\\b', '\\bnew\\b', '\\bnull\\b',
            '\\bobject\\b', '\\boperator\\b', '\\bout\\b', '\\boverride\\b',
            '\\bparams\\b', '\\bprivate\\b', '\\bprotected\\b', '\\bpublic\\b',
            '\\breadonly\\b', '\\bref\\b', '\\breturn\\b', '\\bsbyte\\b',
            '\\bsealed\\b', '\\bshort\\b', '\\bsizeof\\b', '\\bstackalloc\\b',
            '\\bstatic\\b', '\\bstring\\b', '\\bstruct\\b', '\\bswitch\\b',
            '\\bthis\\b', '\\bthrow\\b', '\\btry\\b', '\\btypeof\\b',
            '\\buint\\b', '\\bulong\\b', '\\bunchecked\\b', '\\bunsafe\\b',
            '\\bushort\\b', '\\busing\\b', '\\bvirtual\\b', '\\bvoid\\b',
            '\\bvolatile\\b', '\\bwhile\\b',
            '\\bpackage\\b', '\\bimport\\b', '\\bthrows\\b'
        ]

        self.highlightingRules = [(QRegExp(pattern), keywordFormat)
                                  for pattern in keywords]

    def createOperators(self):
        operatorFormat = QTextCharFormat()
        operatorFormat.setForeground(QColor(148, 99, 233))
        operatorFormat.setFontWeight(QFont.Bold)

        operators = ['\\+', '-', '/', '\\*',
                     '=', '==', '!=', '<=', '>=', '<', '>']

        self.highlightingRules.extend(
            [(QRegExp(pattern), operatorFormat) for
             pattern in operators])

    def createStrings(self):
        stringFormat = QTextCharFormat()
        stringFormat.setForeground(Qt.darkGray)
        self.highlightingRules.append((QRegExp('\".*\"'), stringFormat))
        self.highlightingRules.append((QRegExp('\'.*\''), stringFormat))

    def createArrows(self):
        arrowFormat = QTextCharFormat()
        arrowFormat.setForeground(Qt.red)
        arrows = ['<-', '->']
        self.highlightingRules.extend(
            [(QRegExp(pattern), arrowFormat) for pattern in arrows])

    def createComments(self):
        singleLineCommentFormat = QTextCharFormat()
        singleLineCommentFormat.setForeground(Qt.gray)
        self.highlightingRules.append(
            (QRegExp('//[^\n]*'), singleLineCommentFormat))

    def highlightBlock(self, text):
        for pattern, format in self.highlightingRules:
            expression = pattern
            index = expression.indexIn(text)

            while index >= 0:
                length = expression.matchedLength()
                self.setFormat(index, length, format)
                index = expression.indexIn(text, index + length)

        self.setCurrentBlockState(0)

        startIndex = 0

        if self.previousBlockState() != 1:
            startIndex = self._commentStartExpression.indexIn(text)

        while startIndex >= 0:
            endIndex = self._commentEndExpression.indexIn(text, startIndex)

            if endIndex == -1:
                self.setCurrentBlockState(1)
                commentLength = len(text) - startIndex
            else:
                commentLength = endIndex - startIndex + \
                                self._commentEndExpression.matchedLength()

            self.setFormat(
                startIndex, commentLength, self._multiLineCommentFormat)
            startIndex = self._commentStartExpression.indexIn(
                text, startIndex + commentLength)


import sys
from argparse import ArgumentParser
from dataclasses import dataclass
from io import StringIO, TextIOBase, TextIOWrapper
from pathlib import Path
from typing import Iterable, Tuple, Union

@dataclass
class Config:
    """Struct indicating the input model and output name to assign to it."""
    model: Path
    output: str = ""
    force: bool = False

    def __post_init__(self):
        self.model = self.model.expanduser().resolve()
        if not self.model.is_dir():
            raise ValueError(f"Expected `model` to be a directory but received {self.model}")
        if self.output == "":
            raise ValueError(f"Expected `output` to not be an empty string")

def create_parser() -> ArgumentParser:
    parser = ArgumentParser('add_model.py', description='Add a new Sensory model to `app/include/model`')
    parser.add_argument('model', type=Path, help='Folder containing the Sensory model to add to this application')
    parser.add_argument('--output', '-o', type=str, required=True, help='The name to assign to the output model. If omitted, this uses the folder name of `model`')
    parser.add_argument('--force', '-f', action='store_true', help='Flag to allow overwriting an existing model ')
    return parser

def append(from_file: TextIOBase, to_file: TextIOWrapper):
    to_file.write(from_file.read())

def merge_file_contents_into(source_files: Iterable[Union[Path, TextIOBase]], output_file: Path):
    """Erases the existing contents of `output_file` then iteratively merges the contents in `source_files` into `output_file`."""
    with open(output_file, 'w') as outfile:
        for input_file in source_files:
            if isinstance(input_file, TextIOBase):
                append(input_file, outfile)
            else:
                with open(input_file, 'r') as infile:
                    append(infile, outfile)

def sensory_search_files(model_dir: Path) -> Tuple[Path, Path]:
    """Locates the Sensory search (grammar) files for the given model."""
    try:
        search_source_file = next(model_dir.glob('*search.c'))
        search_header_file = next(model_dir.glob('*search.h'))
    except StopIteration:
        raise ValueError(f"Expected *search.c and *search.h files in model directory {model_dir}")

    return search_header_file, search_source_file

def sensory_net_file(model_dir: Path) -> Path:
    """Locates the Sensory net files for the given model."""

    try:
        return next(model_dir.glob('*net.c'))
    except StopIteration:
        raise ValueError(f"Expected *net.c file in model directory {model_dir}")

def add_aligned_attribute(file: Path) -> int:
    count_changed_lines: int = 0
    with open(file, 'r') as f:
        lines = f.readlines()
    for i in range(len(lines)):
        line = lines[i]
        if 'const unsigned short' in line and not 'const unsigned short __ALIGNED' in line:
            lines[i] = line.replace('const unsigned short', 'const unsigned short __ALIGNED(4)')
            count_changed_lines += 1
    with open(file, 'w') as f:
        f.writelines(lines)
    return count_changed_lines

def add_model(config: Config) -> Path:
    """Adds a new model to the `sensory_wakeupword` demo application.

    This writes the model source/header files from `config.model` into
    `net.h` and `search.h` in `app/include/model/<config.output>`.
    The output is written as header files only, as these are in the existing include path
    which means no SLCP changes or project regeneration is needed afterwards.

    A #MODEL_NAME define is written to the `search.h` file so this can be displayed in the app banner.

    Returns:
        Path: The output directory the model was written to
    """
    models_directory = Path(__file__).parent.parent / "app/include/model"
    if not models_directory.exists():
        raise IOError(f"Output directory {models_directory} does not exist")
    output_directory = models_directory / config.output
    input_directory = config.model
    if output_directory.exists() and not config.force:
        raise IOError(f"Output directory {models_directory} exists. Use the `-f` flag if you want to overwrite it.")
    output_directory.mkdir(exist_ok=True, parents=True)

    search_files = sensory_search_files(input_directory)
    merge_file_contents_into(
        [
            search_files[0],
            StringIO(
                f'\n\n/* The following identifier was automatically generated by {Path(__file__).name} */'
                f'\n#define MODEL_NAME "{config.output}"\n\n'
            ),
            search_files[1],
        ],
        output_directory / "search.h"
    )

    merge_file_contents_into(
        [sensory_net_file(input_directory)],
        output_directory / "net.h"
    )

    add_aligned_attribute(output_directory / "net.h")
    add_aligned_attribute(output_directory / "search.h")

    return output_directory

def main():
    parser = create_parser()
    args = parser.parse_args()
    try:
        config = Config(**vars(args))
        output_directory = add_model(config)
        print(f"Wrote {output_directory.relative_to(Path('.').resolve())}")
    except ValueError as e:
        print(e, file=sys.stderr)
        sys.exit(1)
    except IOError as e:
        print(e, file=sys.stderr)
        sys.exit(1)

if __name__ == "__main__":
    main()


import argparse
from process_archive_data import *

parser = argparse.ArgumentParser(description='Process arguments.')
parser.add_argument('-i', type=str,help='input archive path, with all the translated individuals')
parser.add_argument('-o', type=str, help="output archive path, with translated invididuals reduced to calculate the correct coverage" )
args = parser.parse_args()

def transform_bd_mapelites(bd,behav_shape):
    behav = []
    for i in range(len(bd)):
        b = int(bd[i] * behav_shape[i])
        behav.append(min(b, behav_shape[i] - 1) / float(behav_shape[i]))
    return tuple(behav)

def add_to_archive(individual,bd , fitness,new_archive, transformation,transformdata):

    bin=transformation(bd, transformdata)
    if bin is not None:
        new_archive[bin] = (individual, fitness)

def reduce_translated_archive(archive_file,transform_condition,new_archive_file,transform_data,individuals=[]):
    """
    from a translated archive, reduce it such that it only contains the best for each bin
    :return:
    """

    new_archive = get_bin_performances_duplicatearchive(archive_file, transform_condition, transform_data,
                                                            as_string=False,match_individuals=individuals)
    # finally print the new archive to file
    f=open(new_archive_file,"w")
    for bd, entry in new_archive.items():
        fitness, individual=entry
        f.write(individual+  "   ")
        if isinstance(bd,int):
            f.write(str(bd) + "   ") # write the index of the centroid
            bd = transform_data[bd] #use the index of the centroid
        for i in range(len(bd)):
            f.write(str(bd[i]) + "   ")
        f.write(str(fitness) + "\n")
        f.flush()
    f.close()
    print("reduction complete at "+str(new_archive_file))

def mapelites_bd_add(behav,behav_shape,new_entry,new_archive,epsilon=0.0):
    behav = np.array(behav, dtype=float)
    behav = transform_bd_mapelites(behav,behav_shape)

    existing_entry = new_archive.get(behav)
    if existing_entry is None:
        new_archive[behav] = new_entry
        return
    new_fitness, _new_ind = new_entry
    old_fitness, _individual = existing_entry
    if new_fitness > old_fitness + epsilon:# remove additional condition because distance to center is no longer relevant (no more evolution)
        new_archive[behav] = new_entry

    #    new_archive[behav] = (individual, fitness)
    # behav_pos=[]
    # for i in range(len(bd)):
    #     behav_pos[i] = round(bd[i] * behav_shape[i]);
    # behav_pos[i] = std::
    #     min(behav_pos[i], behav_shape[i] - 1);
    # assert (behav_pos[i] < behav_shape[i]);
    #

def cvt_mapelites_bd_add(bd, centroids, new_entry,new_archive):
    behav = np.array(bd, dtype=float)
    index, behav = transform_bd_cvtmapelites(behav, centroids)
    existing_entry = new_archive.get(index)
    if existing_entry is None:
        new_archive[index] = new_entry
        return
    old_fitness, _individual = existing_entry
    new_fitness, _new_indiv = new_entry
    if new_fitness > old_fitness:
        new_archive[index] = new_entry
    #  new_archive[behav] = (individual, fitness)

def test_cases():
    """ test some simple cases"""
    new_archive = {}
    mapelites_bd_add(("0.15", "0.0"), (10, 10), (0.8, 0), new_archive)
    mapelites_bd_add(("0.15", "0.0"), (10, 10), (0.9, 0), new_archive)
    mapelites_bd_add(("0.38", "0.0"), (10, 10), (0.5, 0), new_archive)
    mapelites_bd_add(("0.38", "0.0"), (10, 10), (0.8, 0), new_archive)

    print(new_archive)

    new_archive={}
    centroids=[np.array([0.0,0.0]),np.array([1.0,1.0])]
    cvt_mapelites_bd_add(("0.15", "0.0"), centroids, (0.8, 0), new_archive)
    cvt_mapelites_bd_add(("0.15", "0.0"), centroids, (0.9, 0), new_archive)
    cvt_mapelites_bd_add(("0.38", "0.0"), centroids, (0.5, 0), new_archive)
    cvt_mapelites_bd_add(("0.38", "0.0"), centroids, (0.8, 0), new_archive)

    cvt_mapelites_bd_add(("0.55", "0.55"), centroids, (0.8, 0), new_archive)
    cvt_mapelites_bd_add(("0.55", "0.55"), centroids, (0.95, 0), new_archive)
    cvt_mapelites_bd_add(("0.55", "0.55"), centroids, (0.5, 0), new_archive)
    cvt_mapelites_bd_add(("0.555", "0.55"), centroids, (0.8, 0), new_archive)

    print(new_archive)

if __name__ == "__main__":
    #test_cases()
    for fitfun in ["Aggregation","Dispersion","DecayCoverage","DecayBorderCoverage","Flocking"]:
        centroids_sdbc = load_centroids("/home/david/argos-sferes/experiments/centroids/centroids_4096_10.dat")
        centroids_spirit = load_centroids("/home/david/argos-sferes/experiments/centroids/centroids_4096_1024.dat")
        for descriptor in ["history"]:
            print(descriptor)
            for gen in range(30000,30500,500):
                for run in range(1,6):
                    print("run "+str(run))
                    # input="/home/david/Data/"+fitfun+"range0.11/"+descriptor+"/FAULT_NONE/results"+str(run)+"/analysis"+str(gen)+"_handcrafted.dat"
                    # output="/home/david/Data/"+fitfun+"range0.11/"+descriptor+"/FAULT_NONE/results"+str(run)+"/analysis"+str(gen)+"_handcraftedREDUCED.dat"
                    # reduce_translated_archive(input,mapelites_bd_add,output,transform_data=(16,16,16))

                    input="/home/david/Data/"+fitfun+"range0.11/"+descriptor+"/FAULT_NONE/results"+str(run)+"/analysis"+str(gen)+"_sdbc.dat"
                    output="/home/david/Data/"+fitfun+"range0.11/"+descriptor+"/FAULT_NONE/results"+str(run)+"/analysis"+str(gen)+"_sdbcREDUCED.dat"
                    reduce_translated_archive(input,cvt_mapelites_bd_add, output,transform_data=centroids_sdbc)

                    input="/home/david/Data/"+fitfun+"range0.11/"+descriptor+"/FAULT_NONE/results"+str(run)+"/analysis"+str(gen)+"_spirit.dat"
                    output="/home/david/Data/"+fitfun+"range0.11/"+descriptor+"/FAULT_NONE/results"+str(run)+"/analysis"+str(gen)+"_spiritREDUCED.dat"
                    reduce_translated_archive(input,cvt_mapelites_bd_add, output,transform_data=centroids_spirit)
# (archive_file,bd_condition,new_archive_file,helper_data)


#!/usr/bin/env python3
import boto3
import pandas as pd
import re
import csv

def testThis():
# Connect to S3
        s3 = boto3.client('s3')
        bucket_name = 'yipit-oscars-data'
        object_key = 'movie-details'

        # Read the contents of the S3 object
        response = s3.get_object(Bucket=bucket_name, Key=object_key,ResponseContentType='json')['Body'].read().decode('utf-8')

        string = response.strip('{}')
        pairs = string.split(', ')

        new_lst = []
        current_group = []
        in_group = False
        for item in pairs:
            if item == "[":
                in_group = True
            elif item == "]":
                in_group = False
                new_lst.append(current_group)
                current_group = []
            elif in_group:
                current_group.append(item)
            else:
                new_lst.append(item)

        obj = new_lst
        my_dict = {}
        grouping = []

        # converted string dict into a list for simpler formation of hashmap
        for item in obj:
            try:
                key, value = item.split(":")
                key = key.replace('"','')
                my_dict[key.strip()] = {}

                my_dict[key.strip()] = value.strip()
                sub_items = value.split(',')
                for sub_item in sub_items:
                    if key == 'Budget':
                        # sub_item.replace('"','')
                        sub_value = sub_item[3:5]
                        grouping.append(sub_value.strip())
                        my_dict[key] = int(sub_value)
                    else:
                        sub_key, sub_value = sub_item.split(',')
                        grouping.append(sub_value.strip())
                        my_dict[key.strip()][sub_key.strip()] = sub_value
            except ValueError:
                pass
            return my_dict

        # subset to only include wanted keys
        def filter_dict(original_dict, keys_to_keep):
            return {key: original_dict[key] for key in keys_to_keep}

        my_dict = filter_dict(my_dict, ['Title','Budget','Box Office', 'Release dates'])

        # conversion to csv
        def to_Csv(data, file_path):
            with open(file_path, 'w', newline='') as csv_file:
                writer = csv.writer(csv_file)
                writer.writerow(data.keys())
                writer.writerow(data.values())

        to_Csv(my_dict, './oscar-output.csv')
         # setup dataframe
        # df1 = pd.DataFrame([my_dict], index=[len([my_dict])], columns=['Title','Budget','Box Office', 'Release dates'])
        # print(df1)

print(testThis())

# def listBuckets():
#     buckets = []
#     # Run the AWS CLI command to list the contents of the S3 bucket
#     result = subprocess.run(["aws", "s3", "ls", f"s3://yipit-oscars-data"], stdout=subprocess.PIPE)

#     # Split the output into lines
#     lines = result.stdout.decode("utf-8").split("\n")

#     # for item in lines:
#     buckets.append(lines)
#     return buckets


import cv2
import os
from os.path import isfile, join

print(os.listdir("Poses/"))

poses = os.listdir('Poses/')

for pose in poses:
    print(">> Working on pose : " + pose)
    subdirs = os.listdir('Poses/' + pose + '/')
    for subdir in subdirs:
        files = os.listdir('Poses/' + pose + '/' + subdir + '/')
        print(">> Working on examples : " + subdir)
        for file in files:
            if(file.endswith(".png")):
                path = 'Poses/' + pose + '/' + subdir + '/' + file
                # Read image
                im = cv2.imread(path)

                height, width, channels = im.shape
                if not height == width == 28:
                    # Resize image
                    im = cv2.resize(im, (28, 28), interpolation=cv2.INTER_AREA)
                    # Write image
                    cv2.imwrite(path, im)


"""
2.8 Loop Detection.

Given a circular linked list, implement an algorithm that returns the node at
the beginning of the loop.
DEFINITION
Circular linked list: A (corrupt) linked list in which a node's next pointer
points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input:  A -> B -> C -> D -> E -> C [the same C as earlier]
Output: C
"""

class Node:
    """LinkedList Node Class."""

    def __init__(self, d):
        """Init method."""
        self.data = d
        self.next = None

    def append_to_tail(self, d):
        """Method to append to tail of the LinkedList."""
        end = Node(d)
        n = self
        while n.next is not None:
            n = n.next
        n.next = end

    def __str__(self):
        """Method to print the LinkedList."""
        n = self
        s = '[ '
        while(n is not None):
            s = s + str(n.data) + ' '
            n = n.next
        return s + ']'

    def __eq__(self, other):
        """Method to compare if two LinkedLists are equals."""
        n1 = self
        n2 = other
        while n1 is not None:
            if n2 is None or n1.data != n2.data:
                return False
            n1 = n1.next
            n2 = n2.next
        return True

def delete_node(head, d):
    """Method to delete a node in the LinkedList."""
    n = head
    if n.data == d:
        return head.next
    while n.next is not None:
        if n.next.data == d:
            n.next = n.next.next
            return head
        n = n.next
    return head

def loop_detection(head):
    """Method that returns the node at the beginning of the loop."""
    slow = head
    fast = head

    # Find the meeting point. LOOP_SIZE - k steps into the linked list.
    while fast is not None and fast.next is not None:
        slow = slow.next
        fast = fast.next.next
        if slow is fast:
            break

    # Error check - no meeting point, and therefore no loop
    if fast is None or fast.next is None:
        return None

    # Move slow to Head. Keep fast at Meeting Point. Each are k steps from the
    # Loop Start. If they move at the same pace, they must meet at Loop Start.
    slow = head
    while slow is not fast:
        slow = slow.next
        fast = fast.next

    # Both now point to the start of the loop
    return fast

Python/I. Data Structures/Chapter 2 | Linked Lists/2.8 Loop Detection.py


from utils.config import getURI
import psycopg2
from models.registro_predio_estado import RegistroPredioEstado

def getRegistroPredioEstado():
    try:
        url = getURI()
        conn = psycopg2.connect(url)

        predios_estados = []
        inst = "select pred.*, (select e.descripcion from ESTADO e where e.id_estado = pred.id_estado) as descripcion from REGISTRO_PREDIO_ESTADO pred;"
        with conn.cursor() as cursor:
            cursor.execute(inst)
            for row in cursor.fetchall():
                predio_estado = RegistroPredioEstado(row[1], row[2],row[3],row[4],row[5])
                predio_estado.id_registro_predio_estado = row[0]
                predios_estados.append(predio_estado.to_json())
        conn.close()
        return predios_estados
    except Exception as error:
        print(error)


def p1():
    with open("input") as f:
        lines = [l.strip() for l in f.readlines()]
        ops = {}
        for i, l in enumerate(lines):
            dir, steps = l.split(" ")
            ops[i] = [dir, int(steps)]
        for op in ops:
            print(f"{op}: {ops[op]}")

        tail = [0, 0]
        head = tail.copy()

        def update_tail():
            index = str(tail)
            if index in traversed_grid:
                traversed_grid[index] += 1
            else:
                traversed_grid[index] = 1

        def is_adjacent(a, b):
            is_ad = False
            for i in range(-1, 2, 1):
                for j in range(-1,2,1):
                    if b[0] + i == a[0] and b[1] + j == a[1]:
                        is_ad = True
            return is_ad

        dir_op = {"R": [1, 0], "L": [-1, 0], "U": [0, 1], "D": [0, -1]}

        traversed_grid = {str(tail): 1}
        for op in ops:
            dir, steps = ops[op]
            for s in range(steps):
                init_head = head.copy()
                move = dir_op[dir]
                head[0] += move[0]
                head[1] += move[1]
                if not is_adjacent(head, tail):
                    tail[0] = init_head[0]
                    tail[1] = init_head[1]
                    update_tail()

        # print(len(traversed_grid))

# p1()

def p2():
    with open("input") as f:
        lines = [l.strip() for l in f.readlines()]
        ops = {}
        for i, l in enumerate(lines):
            dir, steps = l.split(" ")
            ops[i] = [dir, int(steps)]
        for op in ops:
            print(f"{op}: {ops[op]}")

        rope = [[0,0] for i in range(10)]

        def update_tail():
            index = str(f"{rope[9][0]},{rope[9][1]}")
            if index in traversed_grid:
                traversed_grid[index] += 1
            else:
                traversed_grid[index] = 1

        def is_adjacent(a, b):
            is_ad = False
            for i in range(-1, 2, 1):
                for j in range(-1,2,1):
                    if b[0] + i == a[0] and b[1] + j == a[1]:
                        is_ad = True
            return is_ad

        def show(r):
            print("-------------start----------------")
            for j in reversed(range(-10, 16)):
                for i in range(-15, 15):
                    if [i, j] in r:
                        idx = r.index([i,j])
                        print(f" {idx} ", end="")
                    else:
                        print(" . ", end="")
                print("\n")
            print("--------------end---------------")

        def is_move_diag(r1, r2):
            return abs(r1[0] - r2[0]) + abs(r1[1] - r2[1]) == 2

        def is_align(a, b):
            return a[0] == b[0] or a[1] == b[1]

        dir_op = {"R": [1, 0], "L": [-1, 0], "U": [0, 1], "D": [0, -1]}

        traversed_grid = {str(f"{rope[9][0]},{rope[9][1]}"): 1}
        for op in ops:
            dir, steps = ops[op]
            for s in range(steps):
                init_rope = [[i, j] for [i, j] in rope]
                # show(init_rope)
                move = dir_op[dir]
                rope[0][0] += move[0]
                rope[0][1] += move[1]
                for i in range(1, 10):
                    if not is_adjacent(rope[i], rope[i-1]):
                        if is_move_diag(rope[i-1], init_rope[i-1]):
                            if is_align(rope[i], rope[i-1]):
                                a, b = rope[i], rope[i-1]
                                if a[0] == b[0]:
                                    rope[i][1] +=int((rope[i-1][1] - rope[i][1]) / 2)
                                else:
                                    rope[i][0] += int((rope[i-1][0] - rope[i][0]) / 2)
                            else:
                                rope[i][0] += rope[i-1][0] - init_rope[i-1][0]
                                rope[i][1] += rope[i-1][1] - init_rope[i-1][1]
                        else:
                            rope[i][0] = init_rope[i-1][0]
                            rope[i][1] = init_rope[i-1][1]
                update_tail()

        print(len(traversed_grid))

        def show_tail():
            print("\n\n\n")
            for j in reversed(range(-15, 15)):
                for i in range(-15, 15):
                    if f"{i},{j}" in traversed_grid:
                        print(" # ", end="")
                    else:
                        print(" . ", end="")
                print("\n")
            print("\n\n\n")

        # show_tail()

p2()


list1 = [1 , 2, 3]
list2 = [4, 5, 6, 7, 8]
list3 = []

def main():
	max_count = 0
	larger_amount = 0

	if len(list1) < len(list2):
		max_count = len(list1)
		larger_amount = len(list2)

	elif len(list1) > len(list2):
		max_count = len(list2)
		larger_amount = len(list1)

	else:
		max_count = len(list1)

	for i in range(max_count):
		list3.append(list1[i])
		list3.append(list2[i])

	if max_count < larger_amount:
		if len(list1) > len(list2):
			for i in range(max_count, larger_amount):
				list3.append(list1[i])

		elif len(list1) < len(list2):
			for i in range(max_count, larger_amount):
				list3.append(list2[i])

	print(list3)

main()

Chapter 07 - Lists and Tuples/Assignments/7.5 List Methods and Useful Built-in Functions/51278.py


"""
/*
 * @Author: Yang Zhong
 * @Date: 2021-10-09 13:46:53
 * @Last Modified by: Yang Zhong
 * @Last Modified time: 2021-10-29 21:09:02
 */
"""
import pytorch_lightning as pl
import torch
import torch.nn as nn
import torch.optim as opt
from typing import List, Dict, Union
from torch.nn import functional as F
from .utils import scatter_plot
import numpy as np
import os
import pandas as pd

class Model(pl.LightningModule):
    def __init__(
            self,
            representation: nn.Module,
            output: nn.Module,
            losses: List[Dict],
            validation_metrics: List[Dict],
            lr: float = 1e-3,
            lr_decay: float = 0.1,
            lr_patience: int = 100,
            lr_monitor="training/total_loss",
            epsilon: float = 1e-8,
            beta1: float = 0.99,
            beta2: float = 0.999,
            amsgrad: bool = True,
            max_points_to_scatter: int = 100000,
            post_processing: callable = None
            ):
        super().__init__()

        self.representation = representation
        self.output_module = output

        self.losses = losses
        self.metrics = validation_metrics

        self.lr = lr
        self.lr_decay = lr_decay
        self.lr_patience = lr_patience
        self.lr_monitor = lr_monitor

        self.epsilon = epsilon
        self.beta1 = beta1
        self.beta2 = beta2
        self.amsgrad = amsgrad

        self.max_points_to_scatter = max_points_to_scatter
        # post_processing is used to calculate some physical quantities that rely on gradient backpropagation
        self.post_processing = post_processing

        #self.save_hyperparameters()

        # For gradients
        self.requires_dr = self.output_module.derivative

    def calculate_loss(self, batch, result, mode):
        loss = torch.tensor(0.0, device=self.device)
        for loss_dict in self.losses:
            loss_fn = loss_dict["metric"]

            if "target" in loss_dict.keys():
                pred = result[loss_dict["prediction"]]
                target = batch[loss_dict["target"]]

                loss_i = loss_fn(pred, target)
            else:
                loss_i = loss_fn(result[loss_dict["prediction"]])
            loss += loss_dict["loss_weight"] * loss_i

            if hasattr(loss_fn, "name"):
                lossname = loss_fn.name
            else:
                lossname = type(loss_fn).__name__.split(".")[-1]

            self.log(
                mode
                + "/"
                + lossname
                + "_"
                + loss_dict["prediction"],
                loss_i,
                on_step=False,
                on_epoch=True,
            )

        return loss

    def training_step(self, data, batch_idx):
        self._enable_grads(data)
        pred = self(data)
        loss = self.calculate_loss(data, pred, 'training')
        self.log("training/total_loss", loss, on_step=False, on_epoch=True)
        # self.check_param()
        return loss

    def validation_step(self, data, batch_idx):
        if self.requires_dr:
            torch.set_grad_enabled(True)
        else:
            torch.set_grad_enabled(False)
        self._enable_grads(data)
        pred = self(data)
        val_loss = self.calculate_loss(
            data, pred, 'validation').detach().item()
        self.log("validation/total_loss", val_loss,
                 on_step=False, on_epoch=True)
        self.log_metrics(data, pred, 'validation')
        outputs_pred, outputs_target = {}, {}
        for loss_dict in self.losses:
            outputs_pred[loss_dict["prediction"]] = pred[loss_dict["prediction"]].detach().cpu().numpy()
            outputs_target[loss_dict["target"]] = data[loss_dict["target"]].detach().cpu().numpy()
        return {'pred': outputs_pred, 'target': outputs_target}

    def validation_epoch_end(self, validation_step_outputs):
        for loss_dict in self.losses:
            if "target" in loss_dict.keys():
                pred = np.concatenate([out['pred'][loss_dict["prediction"]]
                                 for out in validation_step_outputs])
                target = np.concatenate([out['target'][loss_dict["target"]]
                                    for out in validation_step_outputs])
                if (pred.dtype == np.complex64) and (target.dtype == np.complex64):
                    lossname = type(loss_dict['metric']).__name__.split(".")[-1]
                    if lossname.lower() == 'abs_mae':
                        pred = np.absolute(pred)
                        target = np.absolute(target)
                    else:
                        pred = np.concatenate([pred.real, pred.imag], axis=-1)
                        target = np.concatenate([target.real, target.imag], axis=-1)
                # Control the number of scatter points to plot
                if pred.size > self.max_points_to_scatter:
                    random_state = np.random.RandomState(seed=42)
                    perm = list(random_state.permutation(np.arange(pred.size)))
                    pred = pred.reshape(-1)[perm[:self.max_points_to_scatter]]
                    target = target.reshape(-1)[perm[:self.max_points_to_scatter]]
                figure = scatter_plot(pred.reshape(-1), target.reshape(-1))
                figname = 'PredVSTarget_' + loss_dict['prediction']
                self.logger.experiment.add_figure(
                    'validation/'+figname, figure, global_step=self.global_step)
            else:
                pass

    def test_step(self, data, batch_idx):
        if self.requires_dr:
            torch.set_grad_enabled(True)
        else:
            torch.set_grad_enabled(False)
        self._enable_grads(data)

        if self.post_processing is not None:
            pred = self.post_processing(data)
            if type(self.post_processing).__name__.split(".")[-1].lower() == 'epc_output':
                proessed_values = {'epc_mat': pred['epc_mat'].detach().cpu().numpy()}
            else:
                raise NotImplementedError
        else:
            pred = self(data)
            proessed_values = None

        loss = self.calculate_loss(data, pred, 'test').detach().item()
        self.log("test/total_loss", loss, on_step=False, on_epoch=True)
        self.log_metrics(data, pred, "test")
        outputs_pred, outputs_target = {}, {}
        for loss_dict in self.losses:
            outputs_pred[loss_dict["prediction"]] = pred[loss_dict["prediction"]].detach().cpu().numpy()
            outputs_target[loss_dict["target"]] = data[loss_dict["target"]].detach().cpu().numpy()
        return {'pred': outputs_pred, 'target': outputs_target, 'processed_values': proessed_values}

    def test_epoch_end(self, test_step_outputs):
        for loss_dict in self.losses:
            if "target" in loss_dict.keys():
                pred = np.concatenate([out['pred'][loss_dict["prediction"]]
                                 for out in test_step_outputs])
                target = np.concatenate([out['target'][loss_dict["target"]]
                                    for out in test_step_outputs])

                if not os.path.exists(self.trainer.logger.log_dir):
                    os.makedirs(self.trainer.logger.log_dir)

                np.save(os.path.join(
                    self.trainer.logger.log_dir, 'prediction_'+loss_dict["prediction"]+'.npy'), pred)
                np.save(os.path.join(self.trainer.logger.log_dir,
                        'target_'+loss_dict["target"]+'.npy'), target)

                # plot
                if (pred.dtype == np.complex64) and (target.dtype == np.complex64):
                    lossname = type(loss_dict['metric']).__name__.split(".")[-1]
                    if lossname.lower() == 'abs_mae':
                        pred = np.absolute(pred)
                        target = np.absolute(target)
                    else:
                        pred = np.concatenate([pred.real, pred.imag], axis=-1)
                        target = np.concatenate([target.real, target.imag], axis=-1)

                # Control the number of scatter points to plot
                if pred.size > self.max_points_to_scatter:
                    random_state = np.random.RandomState(seed=42)
                    perm = list(random_state.permutation(np.arange(pred.size)))
                    pred = pred.reshape(-1)[perm[:self.max_points_to_scatter]]
                    target = target.reshape(-1)[perm[:self.max_points_to_scatter]]

                figure = scatter_plot(pred.reshape(-1), target.reshape(-1))
                figname = 'PredVSTarget_' + loss_dict['prediction']
                self.logger.experiment.add_figure(
                    'test/'+figname, figure, global_step=self.global_step)
            else:
                pass

        if self.post_processing is not None:
            if type(self.post_processing).__name__.split(".")[-1].lower() == 'epc_output':
                processed_values = np.concatenate([out['processed_values']["epc_mat"]
                                        for out in test_step_outputs])
                np.save(os.path.join(
                    self.trainer.logger.log_dir, 'processed_values_'+'epc_mat'+'.npy'), processed_values)

    def forward(self, data):
        # torch.set_grad_enabled(True)
        self._enable_grads(data)
        representation = self.representation(data)
        pred = self.output_module(data, representation)
        return pred

    def log_metrics(self, batch, result, mode):
        for metric_dict in self.metrics:
            loss_fn = metric_dict["metric"]

            if "target" in metric_dict.keys():
                pred = result[metric_dict["prediction"]]

                target = batch[metric_dict["target"]]

                loss_i = loss_fn(
                    pred, target
                ).detach().item()
            else:
                loss_i = loss_fn(
                    result[metric_dict["prediction"]]).detach().item()

            if hasattr(loss_fn, "name"):
                lossname = loss_fn.name
            else:
                lossname = type(loss_fn).__name__.split(".")[-1]

            self.log(
                mode
                + "/"
                + lossname
                + "_"
                + metric_dict["prediction"],
                loss_i,
                on_step=False,
                on_epoch=True,
            )

    def configure_optimizers(
            self,
    ):
        optimizer = opt.AdamW(self.parameters(), lr=self.lr, eps=self.epsilon, betas=(self.beta1, self.beta2), weight_decay=0.0, amsgrad=True)
        scheduler = {
            "scheduler": opt.lr_scheduler.ReduceLROnPlateau(
                optimizer,
                factor=self.lr_decay,
                patience=self.lr_patience,
                threshold=1e-6,
                cooldown=self.lr_patience // 2,
                min_lr=1e-6,
            ),
            "monitor": self.lr_monitor,
            "interval": "epoch",
            "frequency": 1,
            "strict": True,
        }
        return [optimizer], [scheduler]

    def _enable_grads(self, data):
        if self.requires_dr:
            data.pos.requires_grad_()

    def check_param(self):
        for name, parms in self.named_parameters():
            print('-->name:', name, '-->grad_requirs:', parms.requires_grad,
                  '-->grad_value:', parms.grad)


import configparser
#from refextract import extract_references_from_file
from bib_parser import GenRequest, recupID, ShowBar, GenDirectories, sort, ShowBar, Mergefinal
from references import dump_extract, Merge_All_List, filtre, Filtre_title, genBib
import argparse
import shutil
import sys
import subprocess
import os as os

#TODO VERBOSE COMMAND

BASE_DIR="./Results/"
pathname = os.path.dirname(sys.argv[0])
SOURCE_DIR=pathname = os.path.abspath(pathname)+"/"

def ConfigParse():
    config = configparser.ConfigParser()
    config.read('SLR.conf')
    requests=[]
    for i in range(len(config['Request'])):
        r="r"+str(i)
        requests.append(config['Request'][r].split(','))
    CSV=config['RQ']['CSV'].split(',')
    TEXTE=config['RQ']['Text'].split(',')
    COMPARISON=int(config['Filter']['comparison'])
    APPEARANCE=int(config['Filter']['appearance'])
    NKWORDS=int(config['Filter']['NKwords'])
    KWORDS=config['Filter']['Kwords'].split(',')
    #print("Requests = "+str(requests))
    #print("CSV = "+str(CSV))
    #print("Texte = "+str(TEXTE))
    #print("COMPARISON = "+str(COMPARISON))
    #print("APPEARANCE = "+str(APPEARANCE))
    #print("KWORDS = "+str(KWORDS))
    return requests,CSV,TEXTE, COMPARISON, APPEARANCE, KWORDS, NKWORDS

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('command')
    arg = parser.parse_args()
    if arg.command not in ["snowball","mergeBib","start","final"]:
        print("Error, unknow command")
        print("Use : 'slrh start' to create a directory and a empty config file")
        print("Use : 'slrh mergeBib' to merge all the bib file in the 'bib' folder, print some stats about the bib files merged, and create folders for each paper to analyse.")
        print("Use : 'slrh snowball' to dump all references from all PDF in all sub folders of the 'Results' folder, create a CSV file with all extracted references, and a bib file with all the filtered references.")
    else :
        if arg.command == "start":
            if os.path.isdir(BASE_DIR) == False:
                os.mkdir(BASE_DIR)
                print("[+] The Results Folder will contain later the folders for the pdf to analyse")
                srcConf=SOURCE_DIR+"empty.conf"
                if os.path.isfile("SLR.conf"):
                    print("[!] Error SLR.conf already created")
                    print("[!] Please complete it")
                else :
                    shutil.copyfile(srcConf,"SLR.conf")
                if os.path.isdir("./bib") :
                    print("[!] Error bib folder already created")
                else :
                    os.mkdir("./bib")
                    print("[+] SLRHelper create a folder 'bib' to put your bibfile in")
                print("[+] NOW COMPLETE THE SLR.conf FILE FOR FURTHER USE !")
            else :
                print("Error Results folder already exist")
        if arg.command == "mergeBib":
            requests,CSV,TEXTE, COMPARISON, APPEARANCE, KWORDS, NKWORDS = ConfigParse()
            r = GenRequest(requests)
            print("[+] The request have been generated")
            path=os.getcwd()
            ida,db = recupID(r,path)
            idt=sort(ida)
            ShowBar(idt)
            GenDirectories(ida,r,idt,path,CSV,TEXTE,db)
            print("[+] Directories created in the Results folder. Please fill them with the rigth pdf paper")
        if arg.command == "snowball":
            requests,CSV,TEXTE, COMPARISON, APPEARANCE, KWORDS,NKWORDS = ConfigParse()
            print("[*] Reference extraction from all pdf (may take severals minutes)")
            cmd=subprocess.run(["/usr/bin/SLRHelper_0.1/ref_extractor.py",BASE_DIR,"ref.csv"])
            # TODO VERIF
            print("[+] extraction complete, results in 'ref.csv'")
            all = dump_extract("ref.csv")
            print("[*] Snowball strating (may take severals minutes)")
            snowball=Merge_All_List(all,COMPARISON)
            #TODO VERIF
            print("[*] Merge completed")
            print("[*] "+str(len(snowball))+" differents references extracted")
            final=filtre(snowball,APPEARANCE)
            final=Filtre_title(final,KWORDS,NKWORDS)
            print("[*] "+str(len(final))+" references add after filter")
            genBib(final,"snowball.bib")
            print("[+] BibFile 'snowball.bib' created with all the references to add")
            print("[*] Please check the file and change the unknow ref")
        if arg.command == "final":
            if not os.path.isfile("final.bib"):
                print("[!] Error, missing final.bib")
            else :
                if not os.path.isfile("snowball.bib"):
                    print("[!] Error, missing snowball.bib")
                else:
                    db=Mergefinal("final.bib","snowball.bib")

if __name__ == '__main__':
    main()


import random
Num=random.randint(1,100)
for i in range(0,5):
  n=int(input("Enter a number between 1 and 100\n"))
  if(n<Num):
        print("Think a little high 😄\n")
        print("Guesses left=",5-i-1)
  elif(n>Num):
        print("Think a little less 😄\n")
        print("Guesses left=",5-i-1)
  else:
        print("Congratulations you made the right guess 🍫 😄 🍫")
        break;
else:
  print("Sorry 😄, your guesses are over. Better luck next time")


from app import app, command_system
from app.scheduledb import ScheduleDB

def report(uid, key, arg=""):
    if arg != '':
        with ScheduleDB(app.config) as db:
            if db.add_report(uid, arg):
                return "Сообщение принято"
            else:
                return "Случилось что то странное, попробуйте ввести команду заново"
    else:
        return 'Вы отправили пустую строку. Пример: сообщение "ваше сообщение"'

report_command = command_system.Command()

report_command.keys = ['сообщение', 'репорт', '/report', 'report', '/send_report', 'send_report']
report_command.description = 'Можно отправить информацию об ошибке или что то ещё. Введите команду(без кавычек): сообщение "ваше сообщение"'
report_command.process = report


from typing import Tuple

import torch
from torch import distributions as dist
from torch import nn
from torch.nn import functional as F

from .decoder import WaveNetDecoder
from .encoder import TemporalEncoder
from .functional import shift1d
from .modules import AutoEncoder

class WavenetVAE(AutoEncoder):
    """
    The complete WaveNetAutoEncoder model.
    """

    def __init__(self, bottleneck_dims: int, encoder_width: int,
                 decoder_width: int, n_layers: int = 10, n_blocks: int = 3,
                 quantization_channels: int = 256,
                 channels: int = 1, gen: bool = False):
        """
        :param bottleneck_dims: Number of dims in the latent bottleneck.
        :param encoder_width: Width of the hidden layers in the encoder (Non-
            causal Temporal encoder).
        :param decoder_width: Width of the hidden layers in the decoder
            (WaveNet).
        :param n_layers: number of layers in encoder and decoder
        :param n_blocks: number of blocks in encoder and decoder
        :param quantization_channels:
        :param channels: Number of input channels.
        :param gen: Is this generation ?
        """
        super(WavenetVAE, self).__init__()
        self.bottleneck_dims = bottleneck_dims
        self.encoder = TemporalEncoder(bottleneck_dims=2 * bottleneck_dims,
                                       channels=channels, width=encoder_width,
                                       n_layers=n_layers, n_blocks=n_blocks)
        self.decoder = WaveNetDecoder(bottleneck_dims=bottleneck_dims,
                                      channels=channels, width=decoder_width,
                                      n_layers=n_layers, n_blocks=n_blocks,
                                      quantization_channels=quantization_channels,
                                      gen=gen)

    def forward(self, x: torch.Tensor) \
            -> Tuple[torch.Tensor, dist.Normal, torch.Tensor]:
        embedding = self.encoder(x)
        q_loc = embedding[:, :self.bottleneck_dims, :]
        q_scale = F.softplus(embedding[:, self.bottleneck_dims:, :]) + 1e-7

        q = dist.Normal(q_loc, q_scale)
        x_q = q.rsample()
        x_q_log_prob = q.log_prob(x_q)

        x = shift1d(x, -1)
        logits = self.decoder(x, x_q)
        return logits, x_q, x_q_log_prob

    @staticmethod
    def loss_function(model: nn.Module, x: torch.Tensor, y: torch.Tensor,
                      device: str) -> Tuple[torch.Tensor, torch.Tensor]:
        logits, x_q, x_q_log_prob = model(x)

        ce_x = F.cross_entropy(logits, y.to(device))

        zx_p_loc = torch.zeros(x_q.size()).to(device)
        zx_p_scale = torch.ones(x_q.size()).to(device)
        pzx = dist.Normal(zx_p_loc, zx_p_scale)
        kl_zx = torch.sum(pzx.log_prob(x_q) - x_q_log_prob)

        loss = ce_x - kl_zx
        return logits, loss