#  used the split method , to test  only some amount of data set
import joblib
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

# Reading the data set csv file
df = pd.read_csv("homeprice.csv")

# Define your feature matrix (X) and target vector (y)
X = df[['area']]
y = df['price']

# Split the data into training and testing sets
# used 0.5 because of small data, use 0.2 for test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42)
# print(len(X_train))
# print(len(X_test))
# Create a LinearRegression model
reg = LinearRegression()

# Fit the model to the training data
reg.fit(X_train, y_train)
print(reg.predict(X_test))
print(y_test)
print(reg.score(X_test,y_test))
# Saving the model to a joblib file
joblib.dump(reg, 'house_joblib2')


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch.optim as optim

from pytorch_image_classification.utils import (
    AverageMeter,
    DummyWriter,
    compute_accuracy,
    count_op,
    create_logger,
    create_tensorboard_writer,
    find_config_diff,
    get_env_info,
    get_rank,
    save_config,
    set_seed,
    setup_cudnn,
)

import pathlib
from myattack import cw_whitebox , pgd_whitebox, fgsm_attack

def squared_l2_norm(x):
    flattened = x.view(x.unsqueeze(0).shape[0], -1)
    return (flattened ** 2).sum(1)

def l2_norm(x):
    return squared_l2_norm(x).sqrt()

def trades_loss(model,
                x_natural,
                y,
                optimizer,
                step_size=0.003,
                epsilon=0.031,
                perturb_steps=10,
                beta=1.0,
                distance='l_inf'):
    # define KL-loss
    criterion_kl = nn.KLDivLoss(size_average=False)
    model.eval()
    batch_size = len(x_natural)
    # generate adversarial example
    x_adv = x_natural.detach() + 0.001 * torch.randn(x_natural.shape).cuda().detach()
    if distance == 'l_inf':
        for _ in range(perturb_steps):
            x_adv.requires_grad_()
            with torch.enable_grad():
                loss_kl = criterion_kl(F.log_softmax(model(x_adv, None, 0.055), dim=1),
                                       F.softmax(model(x_natural, None, 0.055), dim=1))
            grad = torch.autograd.grad(loss_kl, [x_adv])[0]
            x_adv = x_adv.detach() + step_size * torch.sign(grad.detach())
            x_adv = torch.min(torch.max(x_adv, x_natural - epsilon), x_natural + epsilon)
            x_adv = torch.clamp(x_adv, 0.0, 1.0)
    elif distance == 'l_2':
        delta = 0.001 * torch.randn(x_natural.shape).cuda().detach()
        delta = Variable(delta.data, requires_grad=True)

        # Setup optimizers
        optimizer_delta = optim.SGD([delta], lr=epsilon / perturb_steps * 2)

        for _ in range(perturb_steps):
            adv = x_natural + delta

            # optimize
            optimizer_delta.zero_grad()
            with torch.enable_grad():
                loss = (-1) * criterion_kl(F.log_softmax(model(adv), dim=1),
                                           F.softmax(model(x_natural), dim=1))
            loss.backward()
            # renorming gradient
            grad_norms = delta.grad.view(batch_size, -1).norm(p=2, dim=1)
            delta.grad.div_(grad_norms.view(-1, 1, 1, 1))
            # avoid nan or inf if gradient is 0
            if (grad_norms == 0).any():
                delta.grad[grad_norms == 0] = torch.randn_like(delta.grad[grad_norms == 0])
            optimizer_delta.step()

            # projection
            delta.data.add_(x_natural)
            delta.data.clamp_(0, 1).sub_(x_natural)
            delta.data.renorm_(p=2, dim=0, maxnorm=epsilon)
        x_adv = Variable(x_natural + delta, requires_grad=False)
    else:
        x_adv = torch.clamp(x_adv, 0.0, 1.0)
    model.train()

    x_adv = Variable(torch.clamp(x_adv, 0.0, 1.0), requires_grad=False)
    # zero gradient
    optimizer.zero_grad()
    # calculate robust loss
    logits = model(x_natural)
    loss_natural = F.cross_entropy(logits, y)
    loss_robust = (1.0 / batch_size) * criterion_kl(F.log_softmax(model(x_adv), dim=1),
                                                    F.softmax(model(x_natural), dim=1))
    loss = loss_natural + beta * loss_robust
    return loss

def multiple_adv_loss(model,
                x_natural,
                y,
                optimizer,
                step_size=0.003,
                epsilon=0.031,
                perturb_steps=10,
                beta=1.0,
                distance='l_inf', logger=None, tem=0.1, conf=0.2):

    # define KL-loss
    criterion_kl = nn.KLDivLoss(size_average=False)
    model.eval()
    batch_size = len(x_natural)
    # generate adversarial examplei

    x_adv1 = x_natural.detach() + 0.001 * torch.randn(x_natural.shape).cuda().detach()
    #x_adv2 = x_natural.detach() + 0.001 * torch.randn(x_natural.shape).cuda().detach()
    for _ in range(perturb_steps):
        x_adv1.requires_grad_()
        with torch.enable_grad():
            loss_kl = criterion_kl(F.log_softmax(model(x_adv1, None, 0.055), dim=1),
                                       F.softmax(model(x_natural, None, 0.055), dim=1))
        grad = torch.autograd.grad(loss_kl, [x_adv1])[0]
        x_adv1 = x_adv1.detach() + step_size * torch.sign(grad.detach())
        x_adv1 = torch.min(torch.max(x_adv1, x_natural - epsilon), x_natural + epsilon)
        x_adv1 = torch.clamp(x_adv1, 0.0, 1.0)

    '''
    for _ in range(perturb_steps):
        x_adv2.requires_grad_()
        with torch.enable_grad():
            loss_kl = criterion_kl(F.log_softmax(model(x_adv2, None, 0.055), dim=1),
                                       F.softmax(model(x_natural, None, 0.055), dim=1))
        grad = torch.autograd.grad(loss_kl, [x_adv2])[0]
        x_adv2 = x_adv1.detach() + step_size * torch.sign(grad.detach())
        x_adv2 = torch.min(torch.max(x_adv2, x_natural - epsilon), x_natural + epsilon)
        x_adv2 = torch.clamp(x_adv2, 0.0, 1.0)
    '''
    model.train()

    x_adv1 = Variable(torch.clamp(x_adv1, 0.0, 1.0), requires_grad=False)
    # x_adv2 = Variable(torch.clamp(x_adv2, 0.0, 1.0), requires_grad=False)

    #x_adv2 = x_natural
    #x_adv2.requires_grad = True
    #outputs = model(x_adv2)
    #loss = F.nll_loss(outputs, y)
    #loss.backward()

    #x_adv2 = fgsm_attack(x_adv2, 8/255.0, x_adv2.grad.data)

    # zero gradient
    optimizer.zero_grad()
    # calculate robust loss
    logits = model(x_natural)
    loss_natural = F.cross_entropy(logits, y)
    y_adv = model(x_adv1)

    consit_loss = consistancy_loss(y_adv, logits, tem, conf, logger)

    #print(loss_robust)
    loss_robust = (1.0 / batch_size) * criterion_kl(F.log_softmax(y_adv, dim=1),
                                                    F.softmax(logits, dim=1))

    #loss_robust += (1.0 / batch_size) * criterion_kl(F.log_softmax(model(x_adv2), dim=1),
    #                                                F.softmax(model(x_natural), dim=1))

    #print(loss_robust, consit_loss, loss_natural)
    #loss = loss_natural + beta * (loss_robust + 0.1*consit_loss)
    loss = loss_natural + beta * (consit_loss)
    return loss

def consistancy_loss(y_adv, logits, tem, conf, logger):

    y_1 = F.softmax(y_adv, dim=1)
    y_2 = F.softmax(logits, dim=1)
    avg_y = (y_1 + y_2)/2

    num = torch.pow(avg_y, 1./tem)
    deno = torch.sum(torch.pow(avg_y, 1./tem), dim=1, keepdim=True) + 1e-8
    sharp_p = (num / deno).detach()

    loss = 0

    loss += torch.mean((-sharp_p * torch.log(y_1+1e-8)).sum(1)[avg_y.max(1)[0] > conf])
    loss += torch.mean((-sharp_p * torch.log(y_2+1e-8)).sum(1)[avg_y.max(1)[0] > conf])
    return loss/2

'''
if __name__ == '__main__':
   output_dir = pathlib.Path('/scratch/ag7644/output/cifar-small-V7/adv/robust-wrt-s3')

   logger = create_logger(name=__name__,
                           distributed_rank=0,
                           output_dir=output_dir,
                           filename='log.txt')

'''


import inspect
# NOT USED import textwrap

from kivy._event import partial
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.event import EventDispatcher
from kivy.modules import inspector
from kivy.properties import ListProperty, NumericProperty, ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.textinput import TextInput

from modules.navigation import navcont
from modules.map import MapControl
from modules.view import viewcontrol
from modules import CommandHandler

class InputEvents(EventDispatcher):
    __events__ = ('on_input',)

    def run_command(self, cmd, *args):
        self.dispatch('on_input', cmd)

class MainContainer(BoxLayout, InputEvents):
    font_size = NumericProperty(16)
    font_name = StringProperty('RobotoMono-Regular')
    foreground_color = ListProperty((1, 1, 1, 1))
    background_color = ListProperty((0, 0, 0, 0))
    text_display_label = ObjectProperty(None)
    command_console = ObjectProperty(None)

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.app = App.get_running_app()

    def add_text(self, text, on_newline, *args):
        Clock.schedule_once(partial(self.text_display_label.add_text, text, on_newline))

    def on_input(self, _input):
        display_text = f'[color=00FF00]> {_input}[/color]\n'
        self.add_text(display_text, True)

        self.app.pass_command(_input)

    def on_complete(self, _input):
        pass

class CustomImage(Image):
    def __init__(self, **kwargs):
        Image.__init__(self, **kwargs)

class NavContainer(RelativeLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.app = App.get_running_app()

    def on_kv_post(self, base_widget):
        self.buttons = {
            'north': self.canvas.get_group('n'),
            'east': self.canvas.get_group('e'),
            'south': self.canvas.get_group('s'),
            'west': self.canvas.get_group('w')
        }

    def point_inside_polygon(self, x, y, poly):
        """Taken from http://www.ariel.com.au/a/python-point-int-poly.html"""
        n = len(poly)
        inside = False
        p1x = poly[0]
        p1y = poly[1]
        for i in range(0, n + 2, 2):
            p2x = poly[i % n]
            p2y = poly[(i + 1) % n]
            if y > min(p1y, p2y):
                if y <= max(p1y, p2y):
                    if x <= max(p1x, p2x):
                        if p1y != p2y:
                            xinters = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
                        if p1x == p2x or x <= xinters:
                            inside = not inside
            p1x, p1y = p2x, p2y
        return inside

    def on_touch_down(self, touch):
        for k, v in self.buttons.items():
            points = v[0].points
            x, y = self.to_local(touch.pos[0], touch.pos[1])
            if self.point_inside_polygon(x, y, points):
                self.app.fake_command(k)

        return super(NavContainer, self).on_touch_down(touch)

class LookContainer(NavContainer):
    def on_kv_post(self, base_widget):
        self.buttons = {
            'north': self.canvas.get_group('n'),
            'east': self.canvas.get_group('e'),
            'south': self.canvas.get_group('s'),
            'west': self.canvas.get_group('w')
        }
        # self.buttons = {  # change with LookControl
        #     navcont.Directions.NORTH: self.canvas.get_group('n'),
        #     navcont.Directions.EAST: self.canvas.get_group('e'),
        #     navcont.Directions.SOUTH: self.canvas.get_group('s'),
        #     navcont.Directions.WEST: self.canvas.get_group('w')
        # }

    def on_touch_down(self, touch):
        for k, v in self.buttons.items():
            points = v[0].points
            x, y = self.to_local(touch.pos[0], touch.pos[1])
            if self.point_inside_polygon(x, y, points):
                self.app.fake_command(k)

        return super(NavContainer, self).on_touch_down(touch)

class TextDisplayContainer(ScrollView):
    pass

class TextDisplayLabel(Label):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def add_text(self, text, on_newline, *args):
        if self.parent.scroll_y > 0:
            self.parent.scroll_to(self)
        text = inspect.cleandoc(text)
        if on_newline:
            text = '\n' + text
        self.text += text

class CommandInput(TextInput):
    shell = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(CommandInput, self).__init__(**kwargs)
        self._cursor_pos = 0
        self.cmd_history = ['']
        self.bind(focus=self.auto_focus)

        self._init_input()

    def _init_input(self, **kwargs):
        self.prompt()

    def _run_cmd(self, cmd, *args):
        self.shell.run_command(cmd)

    def keyboard_on_key_down(self, window, keycode, text, modifiers):
        # ENTER key
        if keycode[0] == 13:
            self._on_enter()

        # UP ARROW key
        elif keycode[0] == 273:
            self.set_input_text(self.cmd_history[-1])
            temp = self.cmd_history.pop()
            self.cmd_history.insert(0, temp)

        # DOWN ARROW key
        elif keycode[0] == 274:
            self.set_input_text(self.cmd_history[1])
            temp = self.cmd_history.pop(0)
            self.cmd_history.append(temp)

        if self.cursor_index() <= self._cursor_pos:  # if cursor tries to go behind prompt
            return False

        return super(CommandInput, self).keyboard_on_key_down(
            window, keycode, text, modifiers
        )

    def _on_enter(self):
        self.validate_cursor_pos()
        text = self.text[self._cursor_pos:]
        if text.strip() == '':
            self.prompt()
            return
        Clock.schedule_once(partial(self._run_cmd, text))
        self.cmd_history.append(text)

        self.text = ''
        self.prompt()

    def validate_cursor_pos(self, *args):
        if self.cursor_index() < self._cursor_pos:
            self.cursor = self.get_cursor_from_index(self._cursor_pos)

    def set_input_text(self, text):  # used for command history
        self.text = ''
        self.prompt()
        self.text += text

    def prompt(self, prompt_text='>'):
        self._cursor_pos = self.cursor_index() + len(prompt_text)
        self.text = prompt_text

    def auto_focus(self, instance, focused):
        if not focused:
            self.focus = True

    def select_all(self):
        self.select_text(1, len(self.text))

class GUIApp(App):
    """ To use this class, import this -> from kivy.app import App
        and use -> app = App.get_running_app() """

    def build(self):
        self.gui = MainContainer()
        self.start_game()
        inspector.create_inspector(Window, self.gui)
        return self.gui

    def start_game(self):
        self.add_text("""You find yourself in a cave with a flickering torch on the wall.
        You can make out four paths, each equally foreboding.
        """)
        self.nav_control = navcont.NavControl()
        self.map_control = MapControl.DungeonMap()
        self.view_control = viewcontrol.ViewControl()

        self.nav_control.subscribe(self.map_control.callback)
        self.map_control.subscribe(self.view_control.callback)
        self.view_control.subscribe(self.add_text)
        self.command_handler = CommandHandler.CommandHandler(self,
                                                             nav_control=self.nav_control,
                                                             map_control=self.map_control,
                                                             view_control=self.view_control)

    def add_text(self, text, on_newline=True):
        self.gui.add_text(text, on_newline)

    def pass_command(self, text):
        self.command_handler.parse_command(text)

    def fake_command(self, text):
        self.gui.command_console.set_input_text(text)
        self.gui.command_console._on_enter()

if __name__ == '__main__':
    GUIApp().run()


from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from comments.forms import CommentForm
from .models import Post, Category

# Create your views here.

def index(request):
    post_list = Post.objects.all().order_by('-created_time')
    category_list = Category.objects.all()
    context = {'post_list': post_list,
               'category_list': category_list
               }
    return render(request, 'blog/index.html', context=context)

def category(request, pk):
    cate = get_object_or_404(Category, pk=pk)
    post_list = Post.objects.filter(category=cate).order_by('-created_time')
    return render(request, 'blog/index.html', context={'post_list': post_list})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    post.increase_views()

    form = CommentForm()
    comment_list = post.comment_set.all()

    context = {'post': post,
               'form': form,
               'comment_list': comment_list
               }
    return render(request, 'blog/post.html', context=context)


class ModelBuilder:
    def getModel(self, name):
        model = {}
        rel = {}
        with open("./evaluation-data/qrels", "r") as f:
            for line in f:
                queryNo, _, docId, jud = tuple(line.split())
                if queryNo not in rel:
                    rel[queryNo] = {docId: int(jud)}
                else:
                    rel[queryNo][docId] = int(jud)
        with open("./evaluation-data/" + name + ".trecrun", "r") as f:
            for line in f:
                queryNo, _, docId, rank, score, __ = tuple(line.split())
                if queryNo not in model:
                    model[queryNo] = [(docId, int(rank))]
                else:
                    model[queryNo].append((docId, int(rank)))

        for queryNo in model.keys():
            model[queryNo] = list(sorted(model[queryNo], key=lambda x: x[1]))
        return (model, rel)


from pipes import producer, worker, consumer
import os as _os

@producer
def listdir(path):
    for entry in _os.listdir(path):
        yield entry

def listdir_abs(path):
    return listdir(path) | join_path(path)

@worker
def filter_only_dirs(entries):
    for entry in entries:
        if _os.path.isdir(entry):
            yield entry
filter_only_dirs = filter_only_dirs()

@worker
def filter_only_files(entries):
    for entry in entries:
        if _os.path.isfile(entry):
            yield entry
filter_only_files = filter_only_files()

@worker
def filter_by_ext(files, extensions):
    for file in files:
        _, ext = _os.path.splitext(file)
        if ext.lower() in extensions:
            yield file

@worker
def join_path(names, root):
    for name in names:
        yield _os.path.join(root, name)

@worker
def exists(paths):
    for path in paths:
        yield _os.path.exists(path)
exists = exists()

@worker
def get_ext(filenames):
    for filename in filenames:
        _, ext = _os.path.splitext(filename)
        yield ext
get_ext = get_ext()


# class for all gamerGorl board
# Brian Wagner 3/25/19

import utime
from machine import I2C, Pin, PWM
import machine
import sh1106
from neopixel import NeoPixel
import urandom
import framebuf

tones = {
    'c4': 262,
    'd4': 294,
    'e4': 330,
    'f4': 349,
    'f#4': 370,
    'g4': 392,
    'g#4': 415,
    'a4': 440,
    "a#4": 466,
    'b4': 494,
    'c5': 523,
    'c#5': 554,
    'd5': 587,
    'd#5': 622,
    'e5': 659,
    'f5': 698,
    'f#5': 740,
    'g5': 784,
    'g#5': 831,
    'a5': 880,
    'b5': 988,
    'c6': 1047,
    'c#6': 1109,
    'd6': 1175,
    ' ': 0
}

# from https://stackoverflow.com/questions/24852345/hsv-to-rgb-color-conversion
def hsv_to_rgb(h, s, v):
    if s == 0.0:
        v *= 255
        return (v, v, v)
    i = int(h * 6.)  # XXX assume int() truncates!
    f = (h * 6.) - i
    p, q, t = int(255 * (v * (1. - s))), int(255 * (v * (1. - s * f))), int(255 * (v * (1. - s * (1. - f))))
    v *= 255
    i %= 6
    if i == 0:
        return (v, t, p)
    if i == 1:
        return (q, v, p)
    if i == 2:
        return (p, v, t)
    if i == 3:
        return (p, q, v)
    if i == 4:
        return (t, p, v)
    if i == 5:
        return (v, p, q)

class gamerGorl:

    def __init__(self):

        wemos_d1_pins = {
            'D0': 16,  # GPIO
            'D1': 5,  # GPIO, I2C SCL
            'D2': 4,  # GPIO, I2C SDA
            'D3': 0,  # GPIO
            'D4': 2,  # GPIO
            'D5': 14,  # GPIO, SPI SCK (Serial Clock)
            'D6': 12,  # GPIO, SPI MISO (Master in, Slave out)
            'D7': 13,  # GPIO, SPI MOSI (Master out, Slave in)
            'D8': 15,  # GPIO, SPI SS (Slave select)
            'A0': 0,  # Analog in, via ADC
            'RX': 3,  # Receive
            'TX': 1  # Transmit
        }

        # Game pins
        self.Buzzer = wemos_d1_pins['D8']
        self.Neo = wemos_d1_pins['D4']
        self.SDA = wemos_d1_pins['D2']
        self.SCL = wemos_d1_pins['D1']
        self.X_Sel_Pin = wemos_d1_pins['D6']
        self.Y_Sel_Pin = wemos_d1_pins['D5']
        self.Joy_Sw = wemos_d1_pins['D3']
        self.A_Sw = wemos_d1_pins['D7']
        self.B_Sw = wemos_d1_pins['D3']

        self.i2c = I2C(scl=Pin(self.SCL), sda=Pin(self.SDA), freq=400000)
        self.display = sh1106.SH1106_I2C(128, 64, self.i2c, Pin(16), 0x3c)

        # button pins
        self.A = Pin(self.A_Sw, Pin.IN, Pin.PULL_UP)
        self.B = Pin(self.B_Sw, Pin.IN, Pin.PULL_UP)

        # joystick pins
        self.X_Sel = Pin(self.X_Sel_Pin, Pin.OUT)
        self.Y_Sel = Pin(self.Y_Sel_Pin, Pin.OUT)
        self.X_Sel.value(0)
        self.Y_Sel.value(0)
        self.adc = machine.ADC(wemos_d1_pins['A0'])

        #led
        self.numLEDs = 19
        self.np = NeoPixel(Pin(self.Neo), self.numLEDs)

        #sound on
        self.playSound = True

    def soundOn(self):
        self.playSound = True

    def soundOff(self):
        self.playSound = False

    def getNumLEDs(self):
        return self.numLEDs

    def setNumLEDs(self, n):
        self.numLEDs = n

    def randint(self, a, b):
        x = urandom.getrandbits(16)
        x = x % (b+1-a) + a
        return x

    def clearLEDs(self):
        # clear
        for i in range(self.numLEDs):
            self.np[i] = (0, 0, 0)
        self.np.write()

    def fillLEDs(self, color, fromLED, toLED):
        for i in range(fromLED, toLED):
            self.np[i] = color
        self.np.write()

    def cycleLEDs(self, color, mstime, times):
        for i in range(times * self.numLEDs):
            for j in range(self.numLEDs):
                self.np[j] = (0, 0, 0)
            self.np[i % self.numLEDs] = color
            self.np.write()
            utime.sleep_ms(mstime)

    def bounceLEDs(self, color, mstime, times):
        for i in range(times * self.numLEDs):
            for j in range(self.numLEDs):
                self.np[j] = color
            if (i // self.numLEDs) % 2 == 0:
                self.np[i % self.numLEDs] = (0, 0, 0)
            else:
                self.np[self.numLEDs - 1 - (i % self.numLEDs)] = (0, 0, 0)
            self.np.write()
            utime.sleep_ms(mstime)

    def rainbowLEDs(self, mstime, times, increment):
        hue = increment
        for i in range(times * self.numLEDs):
            color = hsv_to_rgb(hue, 1.0, 1.0)
            color = (int(color[0]),int(color[1]), int(color[2]))
            #print(color)
            self.np[i % self.numLEDs] = color
            self.np.write()
            hue += increment
            utime.sleep_ms(mstime)

    def getButtons(self):
        firstA = self.A.value()
        firstB = self.B.value()
        utime.sleep_ms(10)
        secondA = self.A.value()
        secondB = self.B.value()
        APressed = not (firstA and secondA)
        BPressed = not (firstB and secondB)
        # print(APressed)
        # print(BPressed)
        return APressed, BPressed

    def getJoyStickX(self):
        self.Y_Sel.value(0)
        self.X_Sel.value(1)
        # utime.sleep_ms(200)
        X = self.adc.read()
        self.X_Sel.value(0)
        return X

    def getJoyStickY(self):
        self.X_Sel.value(0)
        self.Y_Sel.value(1)
        # utime.sleep_ms(200)
        Y = self.adc.read()
        self.Y_Sel.value(0)
        return 1024 - Y

    def beep(self):
        if self.playSound:
            beeper = PWM(Pin(self.Buzzer, Pin.OUT), freq=1000, duty=512)
            utime.sleep_ms(20)
            beeper.deinit()

    def lowBeep(self):
        if self.playSound:
            beeper = PWM(Pin(self.Buzzer, Pin.OUT), freq=440, duty=512)
            utime.sleep_ms(20)
            beeper.deinit()

    def playTone(self, tone, tone_duration, total_duration):
        if self.playSound:
            print(str(tone_duration) + " " + str(total_duration))
            beeper = PWM(Pin(self.Buzzer, Pin.OUT), freq=tones[tone], duty=512)
            utime.sleep_ms(tone_duration)
            beeper.deinit()
            utime.sleep_ms(int(total_duration * 1000)-tone_duration)

    #https://forum.micropython.org/viewtopic.php?t=4901
    #
    def showPBM(self, filename):
        f = open(filename, 'rb')
        f.readline()  # Magic number
        f.readline()  # Creator comment
        f.readline()  # Dimensions
        #data = bytearray(f.read())
        fbuf = framebuf.FrameBuffer(bytearray(f.read()), 128, 64, framebuf.MONO_HLSB)

        self.display.invert(1)
        self.display.blit(fbuf, 0, 0)
        self.display.show()
        f.close()

    def flashRed(self, fromLED, toLED):
        for i in range(0, 2 * 256, 1):
            for j in range(fromLED, toLED):
                if (i // 256) % 2 == 0:
                    val = i & 0xff
                else:
                    val = 255 - (i & 0xff)
                self.np[j] = (val, 0, 0)
            self.np.write()

        # clear
        for i in range(self.numLEDs):
            self.np[i] = (0, 0, 0)
        self.np.write()

    def flashGreen(self, fromLED, toLED):
        for i in range(0, 2 * 256, 1):
            for j in range(fromLED, toLED):
                if (i // 256) % 2 == 0:
                    val = i & 0xff
                else:
                    val = 255 - (i & 0xff)
                self.np[j] = (0, val, 0)
            self.np.write()

        # clear
        for i in range(self.numLEDs):
            self.np[i] = (0, 0, 0)
        self.np.write()

    def flashBlue(self, fromLED, toLED, fromLED2, toLED2):
        for i in range(0, 2 * 256, 1):
            for j in range(fromLED, toLED):
                if (i // 256) % 2 == 0:
                    val = i & 0xff
                else:
                    val = 255 - (i & 0xff)
                self.np[j] = (0, 0, val)
            for j in range(fromLED2, toLED2):
                if (i // 256) % 2 == 0:
                    val = i & 0xff
                else:
                    val = 255 - (i & 0xff)
                self.np[j] = (0, 0, val)
            self.np.write()

        # clear
        for i in range(self.numLEDs):
            self.np[i] = (0, 0, 0)
        self.np.write()

    def flashYellow(self, fromLED, toLED):
        for i in range(0, 2 * 256, 1):
            for j in range(fromLED, toLED):
                if (i // 256) % 2 == 0:
                    val = i & 0xff
                else:
                    val = 255 - (i & 0xff)
                self.np[j] = (val, val, 0)
            self.np.write()

        # clear
        for i in range(self.numLEDs):
            self.np[i] = (0, 0, 0)
        self.np.write()


# build "Hikoukigumo no Mukougawa" character images from parts [kirikiri engine]

import sys
import codecs
import re
import os

if len(sys.argv) < 4:
    print ("usage: {0} [-e ENCODING] CHAR_DEF.txt BASE-ID OVERLAY-ID".format (sys.argv[0]))
    sys.exit()

CONVERT = 'convert.exe'

sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
argn = 1
encoding = 'shift-jis'
if sys.argv[argn] == '-e':
    encoding = sys.argv[argn+1]
    argn += 2
txt_name = sys.argv[argn]
base_id = sys.argv[argn+1]
argn += 2

name = re.sub (r'\..*$', '', txt_name)

imgset = []
layers = {}

with open (txt_name, 'r', encoding=encoding) as txt:
    first_line = txt.readline()
    if first_line.startswith(u'\uFEFF'):
        first_line = first_line[1:]
    keys = first_line.split ("\t")
    columns = {}
    for i in range (len (keys)):
        columns[keys[i]] = i
    if not 'width' in columns or not 'height' in columns:
        raise Exception('incorrect image definition')
    row = txt.readline().split ("\t")
    width = int(row[columns['width']])
    height = int(row[columns['height']])
    for line in txt:
        row = line.split ("\t")
        img = {}
        for key in keys:
            img[key] = row[columns[key]]
        imgset.append (img)
        if '0' == img['#layer_type']:
            layers[img['layer_id']] = img

if not base_id in layers:
    raise Exception ("base {0} not found".format (base_id))
base = layers[base_id]
target_name = '{0}_{1}'.format (name, base['layer_id'])
base_name = target_name+ '.png'

cmd = [ '-size', '{0}x{1}'.format (width, height), 'xc:none' ]
x = int(base['left'])
y = int(base['top'])
geometry = '{0:+d}{1:+d}'.format (x, y)
cmd.extend ([base_name, '-geometry', geometry, '-composite'])

for overlay_id in sys.argv[argn:]:
    if not overlay_id in layers:
        raise Exception ("overlay {0} not found".format (overlay_id))
    overlay = layers[overlay_id]

    overlay_name = '{0}_{1}.png'.format (name, overlay['layer_id'])
    x = int(overlay['left'])
    y = int(overlay['top'])
    alpha = int(overlay['opacity'])
    geometry = '{0:+d}{1:+d}'.format (x, y)
    cmd.extend ([overlay_name, '-geometry', geometry, '-composite'])
#    if alpha != 255:
#        cmd.extend (['-channel', 'A', '-evaluate', 'set', '{0}%'.format(int(alpha*100/255))])
    target_name += '+{0}'.format (overlay['layer_id']);

target_name += '.png';
cmd.append (target_name)
print (' '.join (['convert']+cmd))
os.system (' '.join ([CONVERT] + cmd));


#!/usr/bin/env python2.7
"Set of common functions for scripts to communicate with bunitu C&Cs"

__AUTHOR__ = 'Sergei Frankoff, hasherezade'
__VERSION__ = '1.0'

from util import *

###bitbang lib##
# Author: Satoshi Tanda
def _rol(val, bits, bit_size):
    return (val << bits % bit_size) & (2 ** bit_size - 1) | \
           ((val & (2 ** bit_size - 1)) >> (bit_size - (bits % bit_size)))
### end bitbang ##

####
# bot id util:
#
def get_rtdsc():
    return int (time.time()*1000000000)

def make_new_bot_id():
    eax = get_rtdsc()
    eax = _rol(eax, 3, 32)
    return _make_bot_id(eax, 10)

def _make_bot_id(chunk1, id_len=10):
    botID = bytearray()
    eax = chunk1
    botID += bytearray(struct.pack("<I", eax))
    eax -= 1
    eax = (_rol(eax, 2, 32)) + 1
    botID += bytearray(struct.pack("<I", eax))
    eax = (_rol(eax, 1, 32)) & 0xFFFF
    botID += bytearray(struct.pack("<H", eax))
    return botID[0:id_len]

####
# calculate C&C IP
#
def get_c2_ip(hostname, xorval=0x16ec1a31):
    orig_ip = socket.gethostbyname(hostname)
    b_orig_ip = struct.unpack('<L', socket.inet_aton(orig_ip))[0]
    return socket.inet_ntoa(struct.pack('<L',b_orig_ip ^ xorval))


# -*- coding: utf-8 -*-
import unittest

import numpy as np
import pandas as pd
import pandas.util.testing as tm

import dota.helpers as h

class TestExtraction(unittest.TestCase):

    def setUp(self):
        pbs = ([False] * 4 + [True] * 4 + [False] * 4
               + [True] * 4 + [False] * 2 + [True] * 2)
        team = np.array([0, 1, 0, 1, 0, 1, 1, 0, 0, 1,
                         0, 1, 1, 0, 1, 0, 1, 0, 1, 0])
        self.df = pd.DataFrame({'hero_id': range(20),
                                'is_pick': pbs,
                                'order': range(20),
                                'team': team,
                                'team_id': team,
                                'team_name': team,
                                'match_id': np.ones(20),
                                'team_id_f': team,
                                'hero_id_f': range(20)
                                })

    def test_opponent(self):
        expected = np.abs(self.df['team_id_f'] - 1)

        for order in range(20):
            result = h.pb_opponent_id(self.df, order=order)
            self.assertEqual(result, expected.iloc[order])

    def test_previous_pbs(self):

        for i in range(1, 20):
            d = {'pb_{}'.format(j): j for j in range(i)}
            expected = pd.DataFrame(d, index=[0])
            expected = expected[sorted(d.keys(),
                                       key=lambda x: int(x.split('_')[-1]))]
            result = h.pb_previous_pbs(self.df, i)
            tm.assert_frame_equal(result, expected)

    def test_only_complete(self):

        result = h.pb_only_complete_drafts(self.df)
        tm.assert_frame_equal(result, self.df)

        bad = self.df.copy()
        bad.loc[0, 'team_id'] = np.nan
        bad['match_id'] = bad['match_id'] + 1
        bad = pd.concat([bad, self.df])

        result = h.pb_only_complete_drafts(bad)
        tm.assert_frame_equal(result, self.df, check_dtype=False)

    def test_full_draft(self):

        bad = self.df.copy().loc[:5]
        bad['match_id'] = bad['match_id'] + 1
        bad = pd.concat([bad, self.df])
        result = h.pb_only_complete_drafts(bad)
        tm.assert_frame_equal(result, self.df, check_dtype=False)


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode],prev = None) -> Optional[ListNode]:
        # #Empty list base case
        # if head == None:
        #     return None
        # #initialising iterating variables
        # current = head
        # prev = None
        # next1 = current.next
        # #Visiting and rerouting Nodes
        # while(next1 != None):
        #     current.next = prev
        #     prev = current
        #     current = next1
        #     next1 = next1.next
        # #Rerouting New head node and returning it
        # current.next = prev
        # return current
        if not head:return prev
        current = head.next
        head.next = prev
        prev = head
        return self.reverseList(current,prev)


import webbrowser

import wx.html

from python_toolbox.wx_tools.widgets.cute_window import CuteWindow

class CuteHtmlWindow(wx.html.HtmlWindow, CuteWindow):

    event_modules = wx.html

    def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=wx.html.HW_DEFAULT_STYLE,
                 name=wx.html.HtmlWindowNameStr):
        wx.html.HtmlWindow.__init__(self, parent=parent, id=id, pos=pos,
                                    size=size, style=style, name=name)
        self.bind_event_handlers(CuteHtmlWindow)

    def _on_html_link_clicked(self, event):
        webbrowser.open_new_tab(
            event.GetLinkInfo().GetHref()
        )


# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import combinations
no_of_elements = int(input())
elements = input().split()
size = int(input())
temp = [int(i) for i in range(1,no_of_elements+1)]
combinations_list = combinations(temp,size)
indices_of_a = []
for ind,val in enumerate(elements):
    if val == 'a':
        indices_of_a.append(ind+1)
a_comb=0
total_comb=0
for comb in combinations_list:
    total_comb+=1
    for i in indices_of_a:
        if i in comb:
            a_comb+=1
            break

print("{:.4f}".format(a_comb/total_comb))


import os
import re
import pandas as pd
import logging
import numpy as np

from pyarc.qcba.data_structures import QuantitativeDataFrame
import time

from pyids.algorithms.ids_classifier import mine_CARs
from pyids.algorithms.ids import IDS
from pyids.data_structures.ids_ruleset import IDSRuleSet
from pyids.algorithms.ids_classifier import IDSClassifier
from pyids.algorithms.ids import IDS
from pyarc.data_structures import TransactionDB
from pyarc.algorithms import M1Algorithm
from pyarc import CBA

#logging.basicConfig(level=logging.DEBUG)

datasets = [
    "iris",
    "breast-w",
    "anneal",
    "hypothyroid",
    "ionosphere",
    "lymph",
    "vehicle",
    "autos",
    "diabetes",
    "glass",
    "heart-h",
    "tic-tac-toe",
    "australian",
    "sick",
    "segment",
    "spambase",
    "sonar",
    "vowel",
    "hepatitis",
    "credit-a",
    "mushroom",
    "house-votes-84",
    "soybean",
    "primary-tumor",
    "credit-g",
    "audiology",
    "breast-cancer",
    "balance-scale",
    "heart-c",
    "kr-vs-kp",
    "pima",
    "heart-statlog"]

dataset_files = [ f"{dataset_name}0.csv" for dataset_name in datasets ]

dataset_path = "C:/code/python/machine_learning/assoc_rules/"
dataset_path_train = os.path.join(dataset_path, "train")
dataset_path_test = os.path.join(dataset_path, "../../../test")

benchmark_list = []

for dataset_filename in dataset_files:
    print(dataset_filename)

    df_train = pd.read_csv(os.path.join(dataset_path_train, dataset_filename))
    df_test = pd.read_csv(os.path.join(dataset_path_test, dataset_filename))

    txns_train = TransactionDB.from_DataFrame(df_train)
    txns_test = TransactionDB.from_DataFrame(df_test)

    quant_df_train = QuantitativeDataFrame(df_train)
    quant_df_test = QuantitativeDataFrame(df_test)

    cba = CBA(support=0.1, confidence=0.1)
    cba.fit(txns_train)

    rules = cba.clf.rules
    ids_ruleset = IDSRuleSet.from_cba_rules(rules)

    ids = IDS()
    ids.clf = IDSClassifier(ids_ruleset.ruleset)
    ids.clf.default_class = cba.clf.default_class

    metrics_dict = ids.score_interpretability_metrics(quant_df_test)

    benchmark_dict = dict(
        dataset_filename=dataset_filename,
        algorithm="cba"
    )

    benchmark_dict.update(metrics_dict)
    print(benchmark_dict)

    benchmark_list.append(benchmark_dict)

benchmark_df = pd.DataFrame(benchmark_list)
benchmark_df.to_csv("output_data/cba_interpretability_benchmark.csv", index=False)


from bs4 import BeautifulSoup
import requests
import pandas as pd
from selenium import webdriver

class NaverMovie(object):

    url = 'https://movie.naver.com/movie/sdb/rank/rmovie.nhn'
    class_name = ''
    driver = webdriver.Chrome('C:/program Files/Google/Chrome/chromedriver')
    driver.implicitly_wait(3)
    df = None
    dt = {}
    ls = []
    ls1 = []
    ls2 = []
    ls3 = []

    def scrap(self):
        self.driver.get(self.url)
        soup = BeautifulSoup(self.driver.page_source, 'lxml')
        ls_soup = soup.find_all('div', {'class':self.class_name})
        for i in ls_soup:
            self.ls1.append(i.find("a").text)
        for href in ls_soup:
            self.ls2.append(f' - https://movie.naver.com' + href.find("a")['href'])
        self.ls = [self.ls1, self.ls2]
        #self.ls = [ x + y for x, y in zip(self.ls1,self.ls2)]
        print(self.ls)

       # self.ls2 = list(range(1,51))
        for j in range(1,51):
            self.ls3.append(j)#(f'{j}위')
        print(self.ls3)

    def ls_to_dt(self):

        self.dt = dict(zip(self.ls3, self.ls))
        print(self.dt)

    def dt_to_dataframe(self):
        self.df = pd.DataFrame.from_dict(self.dt)#, orient='index', columns=['         TITLE         ','         LINK         '])
        self.df.rename(columns={0: '', 1: '           영화제목          ', 2: '          링크           ', 3: '런타임'}, inplace=True)
        print(self.df)

    def df_to_csv(self):
        path = './csv/naver_movie.csv'
        self.df.to_csv(path,sep='.', na_rep='-')

if __name__ == '__main__':
    nm = NaverMovie()
    nm.class_name = 'tit3'
    nm.scrap()
    nm.ls_to_dt()
    nm.dt_to_dataframe()
    nm.df_to_csv()


__author__ = 'amrit'

import copy
import math
import multiprocessing as mp
from collections import OrderedDict, namedtuple
from random import choice, randint, random, sample, seed, uniform

import numpy as np

__all__ = ["DE"]
Individual = namedtuple("Individual", "ind fit")

class DE(object):
    def __init__(
        self,
        F=0.3,
        CR=0.7,
        NP=10,
        GEN=2,
        Goal="Max",
        termination="Early",
        random_state=1,
        threads=None,
    ):
        """

        :param F: Mutation Rate to mutate a candidate
        :param CR: Crossover Rate to see if the candidate will survive in the next generation
        :param NP: Number of candidates in the population
        :param GEN: Number of generations to run Differential Evolution for.
        :param Goal: Whether DE is suppose to maximize or minimise the fitness function ("Max" or "Min")
        :param termination: 2 termination criteria, "Early" runs upto given GEN, "Late" runs upto the time there is no
                            improvement in the current generation than the past one.
        :param random_state: random seed
        :param threads: No. of threads to do multi-processing, if not parallelization then do -1,
                    select based on memory availability, then "None"
        """
        self.F = F
        self.CR = CR
        self.NP = NP
        self.GEN = GEN
        self.GOAL = Goal
        self.termination = termination
        self.threads = (
            max(2, math.ceil(mp.cpu_count() * 0.1)) if threads is None else threads
        )
        seed(random_state)
        np.random.seed(random_state)

    def initial_pop(self):
        """
        :return: the initial population of candidates for DE to work with.
        """
        current_generation = []
        for _ in range(self.NP):
            dic = OrderedDict()
            for i in range(self.para_len):
                dic[list(self.para_dic.keys())[i]] = self.calls[i](self.bounds[i])
            current_generation.append(dic)
        return current_generation

    # Need a tuple for integer and continuous variable but need the whole list for category
    def randomisation_functions(self):
        """
        Set the class variable "calls" based on the type of parameter you get.
        """
        random_calls = []
        for i in self.para_category:
            if i == "integer":
                random_calls.append(self._randint)
            elif i == "continuous":
                random_calls.append(self._randuniform)
            elif i == "categorical":
                random_calls.append(self._randchoice)
        self.calls = random_calls

    # Paras will be keyword with default values, and bounds would be list of tuples
    def solve(self, fitness, paras=None, bounds=None, category=None, **r):  # noqa: C901
        """
        :param fitness: (func) The models fitness function for which DE to minimize and maximize
        :param paras: (Ordered Dic) Order of the key-value (parameter name - its default settings) pair to tune.
                        Key should be same as the fitness function keyword parameter name
        :param bounds: (List of tuples) Similar Orderering as "paras" :param, with upper and lower bounds selected
        :param category: (List) Similar ordering as "paras" :param, and the values to be chosen from
                        ["integer", "continuous", "categorical"] case sensitive
        :param r: any additional keyword arguments required by the fitness function.
        :return: the best index and its corresponding sub-optimal score, also the last generations all - suboptimal candidates
        """
        self.para_len = len(paras.keys())
        self.para_dic = paras
        self.para_category = category
        self.bounds = bounds

        if not callable(fitness):
            raise ValueError("Check function {} is not callable".format(fitness))

        if sorted(set(self.para_category)) != ["categorical", "continuous", "integer"]:
            raise ValueError(
                "Parameter categories should be 'categorical', 'continuous', 'integer'. Please choose among a valid "
                "parameter type."
            )
        for i, e in enumerate(self.para_category):
            para_dic = list(self.para_dic.keys())
            if (e == "continuous" or e == "integer") and (
                len(self.bounds[i]) != 2 or type(self.bounds[i]) != tuple
            ):
                raise ValueError(
                    "Check Parameter {} for its category which should be among 'categorical', 'continuous', 'integer' "
                    "or check bounds which should be of tuple.".format(para_dic[i])
                )
            elif (e == "categorical") and (
                type(self.bounds[i]) != tuple or len(self.bounds[i]) == 0
            ):
                raise ValueError(
                    "Check Parameter {} for its category which should be among 'categorical', 'continuous', 'integer' "
                    "or check bounds which should be of tuple.".format(para_dic[i])
                )

        self.randomisation_functions()
        initial_population = self.initial_pop()

        if self.threads == -1:
            self.cur_gen = []
            for ind in initial_population:
                self.cur_gen.append(Individual(OrderedDict(ind), fitness(ind, **r)))
        else:
            self.cur_gen = self.parallelize(
                fitness, initial_population, threads=self.threads, **r
            )

        if self.termination == "Early":
            return self.early_termination(fitness, **r)

        else:
            return self.late_termination(fitness, **r)

    def early_termination(self, fitness, **r):
        """
        :param fitness: (func) The models fitness function for which DE to minimize and maximize
        :param r: any additional keyword arguments required by the fitness function.
        :return: the best index and its corresponding sub-optimal score, also the last generations all - suboptimal candidates
        """
        if self.GEN > 1:
            for _ in range(self.GEN - 1):
                population = [
                    self._extrapolate(i, ind) for i, ind in enumerate(self.cur_gen)
                ]
                if self.threads == -1:
                    trial_generation = [
                        Individual(OrderedDict(v), fitness(v, **r)) for v in population
                    ]
                else:
                    trial_generation = self.parallelize(
                        fitness, population, threads=self.threads, **r
                    )
                current_generation = self._selection(trial_generation)
                self.cur_gen = current_generation
            best_index = self._get_best_index()
            return self.cur_gen[best_index], self.cur_gen
        else:
            best_index = self._get_best_index()
            return self.cur_gen[best_index], self.cur_gen

    def late_termination(self, fitness, **r):
        """
        :param fitness: (func) The models fitness function for which DE to minimize and maximize
        :param r: any additional keyword arguments required by the fitness function.
        :return: the best index and its corresponding sub-optimal score, also the last generations all - suboptimal candidates
        """
        lives = 1
        while lives != 0:
            population = [
                self._extrapolate(i, ind) for i, ind in enumerate(self.cur_gen)
            ]
            if self.threads == -1:
                trial_generation = [
                    Individual(OrderedDict(v), fitness(v, **r)) for v in population
                ]
            else:
                trial_generation = self.parallelize(
                    fitness, population, threads=self.threads, **r
                )
            current_generation = self._selection(trial_generation)
            if sorted(self.cur_gen) == sorted(current_generation):
                lives = lives - 1
            else:
                self.cur_gen = current_generation

        best_index = self._get_best_index()
        return self.cur_gen[best_index], self.cur_gen

    def _extrapolate(self, ix, ind):  # noqa: C901
        """
        :param ix: the current index of the candidate for which you are mutating against.
        :param ind: a candidate to mutate via selecting 3 other candidates.
        :return: returns the mutated candidate.
        """
        if random() < self.CR:  # nosec - B311
            a, b, c = self._select3others(ix)
            mutated = []
            for x, i in enumerate(self.para_category):
                if i == "continuous":
                    new_can = a[list(a.keys())[x]] + self.F * (
                        b[list(b.keys())[x]] - c[list(c.keys())[x]]
                    )
                    mutated.append(new_can)
                elif i == "integer":
                    new_can = a[list(a.keys())[x]] + self.F * (
                        b[list(b.keys())[x]] - c[list(c.keys())[x]]
                    )
                    mutated.append(int(new_can))
                else:
                    mutated.append(self.calls[x](self.bounds[x]))
            check_mutated = []
            for i in range(self.para_len):
                if (
                    self.para_category[i] == "continuous"
                    or self.para_category[i] == "integer"
                ):
                    check_mutated.append(
                        max(self.bounds[i][0], min(mutated[i], self.bounds[i][1]))
                    )
                else:
                    check_mutated.append(mutated[i])
            dic = OrderedDict()
            for i in range(self.para_len):
                dic[list(self.para_dic.keys())[i]] = check_mutated[i]
            return dic
        else:
            dic = OrderedDict()
            for i in range(self.para_len):
                key = list(self.para_dic.keys())[i]
                dic[list(self.para_dic.keys())[i]] = ind.ind[key]
            return dic

    def _select3others(self, ix):
        """
        :param ix: the current index of the candidate for which you are mutating against.
        :return: returns the 3 candidate to be mutated against from the current generation
        """
        sample_gen = copy.deepcopy(self.cur_gen)
        sample_gen.pop(ix)
        val = sample(sample_gen, 3)
        return [a.ind for a in val]

    def _selection(self, trial_generation):
        """see if the new candidates in the current generation is better or in the new generation
        :param trial_generation: evaluated candidates equal to NP for the new generation
        :return: the final generation with the candidates that are optimal out of new and previous generation
        """
        generation = []

        for a, b in zip(self.cur_gen, trial_generation):
            if self.GOAL == "Max":
                if a.fit >= b.fit:
                    generation.append(a)
                else:
                    generation.append(b)
            else:
                if a.fit <= b.fit:
                    generation.append(a)
                else:
                    generation.append(b)

        return generation

    def _get_best_index(self):
        """
        :return: the index of the optimal candidate from the final generation
        """
        if self.GOAL == "Max":
            best = 0
            max_fitness = -float("inf")
            for i, x in enumerate(self.cur_gen):
                if x.fit >= max_fitness:
                    best = i
                    max_fitness = x.fit
            return best
        else:
            best = 0
            max_fitness = float("inf")
            for i, x in enumerate(self.cur_gen):
                if x.fit <= max_fitness:
                    best = i
                    max_fitness = x.fit
            return best

    @staticmethod
    def _randint(a):
        """
        :param a: accepts a tuple of (Min, Max) for which a random integer needs to be selected.
        """
        return randint(*a)  # nosec - B311

    @staticmethod
    def _randchoice(a):
        """
        :param a: accepts a tuple of categorical choices for which a random choice needs to be selected.
        """
        return choice(a)  # nosec - B311

    @staticmethod
    def _randuniform(a):
        """
        :param a: accepts a tuple of (Min, Max) for which a random continuous needs to be selected.
        """
        return uniform(*a)  # nosec - B311

    @staticmethod
    def thread_callable(inprequest):
        fitness = inprequest[0]
        ind = inprequest[1]
        r = inprequest[2]
        return Individual(OrderedDict(ind), fitness(ind, **r))

    # Add Multiprocessor support
    def parallelize(self, fitness, params_list, threads=2, **r):
        """
        Initializes the multiprocessor threads to process a respective function
        :param fitness (obj): function call mapped to each
        :param params_list (list): list of candidates from the population needed by func
        :param threads (int): no of cpus/threads to use for parallel processing
        :return: list of namedtuple with candidate and fitness score.
        """

        def inputGenerator():
            for d in params_list:
                yield (fitness, d, r)

        inputrequests = inputGenerator()
        pool = mp.Pool(threads)
        data = pool.map(self.thread_callable, inputrequests)
        pool.close()
        pool.join()
        return data


"""
Event binding
"""

from tkinter import *

def handler(event):
    print(f"I was clicked via bind(). {event.x} {event.y}")

root = Tk()
root.title("Demo of Button and CLick")
root.geometry("640x480+300+300")
root.config(bg="white")

# command option
btn1 = Button(root, text="Click me")
btn1.pack()

btn1.bind('<Button-1>', handler)

# no stop to receive the update
root.mainloop()


from pathlib import Path
from django.contrib.gis.utils import LayerMapping
from .models import countries

world_mapping = {
    'name' : 'NAME',
    'geometry' : 'POLYGON',
}

world_shp = Path(__file__).resolve().parent / 'data' / 'countries.shp'

def run(verbose=True):
    lm = LayerMapping(countries, world_shp, world_mapping, transform=False)
    lm.save(strict=True, verbose=verbose)


from crypt import methods
from flask import Flask, render_template, redirect, session, make_response, jsonify, request
from boggle import Boggle

bogs = Boggle()

app = Flask(__name__)
app.config['SECRET_KEY'] = "boggleMind"

@app.route('/')
def board_to_page():
    if session == {}:
        make_board = bogs.make_board()
        session['board'] = make_board
        board = session['board']
        print(session)
    else:
        board = session['board']
    return render_template('base.html', board = board)

@app.route('/word_check')
def is_word():
    word = request.args["word"]
    board = session['board']
    print(board)
    print (word)
    valid_word = bogs.check_valid_word(board=board, word=word)
    print(valid_word)
    return jsonify({'result':valid_word})

@app.route('/new_board')
def new_board():
    session.clear()
    return redirect('/')


# %%
"""
.. _plot_02_load_1D_NMR_spectrum_Kea:

=====================================
Load two 1D NMR spectra in Kea format
=====================================

In this example we demonstrate how to import two ODNP-enhanced NMR spectra, one recorded with a microwave power of 0 W (off-signal) and one with a microwave power of 2 W (on-signal). The spectra are recorded using a Magritek Kea system.

The example script has three different sections:

#. Load and Process Off-Signal
#. Load and Process On-Signal
#. Create a Figure and Plot On/Off Spectra

"""
# %%
# Make sure to start with importing DNPLab
import dnplab as dnp

# %%
# Load and Process Off-Signal
# -----------------------------
# The next section demonstrates how the FID is imported into DNPLab and processed. Processing involves removing any DC offset, followed by a 15 Hz linewidth appodization, prior to performing the Fourier transformation.

########## OFF Signal (P = 0 W) ##########
data_off = dnp.load("../../data/prospa/10mM_TEMPO_Water/1Pulse_20200929/35/data.1d")

data_off = dnp.remove_background(data_off)
data_off = dnp.apodize(data_off, lw=15)
data_off = dnp.fourier_transform(data_off)

# %%
# Load and Process ON-Signal
# ----------------------------
# Importing the on-signal involves the same steps as importing the off-signal. Once processed the data is copied to the results buffer 'onSignal'.

########## ON Signal (P = 2 W) ##########
data_on = dnp.load("../../data/prospa/10mM_TEMPO_Water/1Pulse_20200929/51/data.1d")

data_on = dnp.remove_background(data_on)
data_on = dnp.apodize(data_on, lw=15)
data_on = dnp.fourier_transform(data_on)

# %%
# Plot Microwave On/Off DNP Spectra
# ---------------------------------
# First plot spectra individually

sampleTag = "10 mM TEMPO in Water"

dnp.plt.figure()
dnp.fancy_plot(data_on, title=sampleTag + ", MW On Spectrum")

dnp.plt.figure()
dnp.fancy_plot(data_off, title=sampleTag + ", MW Off Spectrum")
dnp.plt.show()

# %%
# Next plot both spectra in the same figure

dnp.plt.figure()
dnp.fancy_plot(data_on, xlim=[-20, 20])
dnp.fancy_plot(data_off * 50, xlim=[-20, 20])
dnp.plt.title(sampleTag + ", MW ON/OFF(*50)")
dnp.plt.show()


# Testing Images with caffe model
# H.H.
# Oct 2017

import os
import glob
import cv2
import sys
caffe_root = '/home/carrot/caffe/'
sys.path.insert(0, caffe_root + 'python')

import caffe
import lmdb
import numpy as np
from caffe.proto import caffe_pb2

caffe.set_mode_gpu()

#Size of images
IMAGE_WIDTH = 304
IMAGE_HEIGHT = 228

def transform_back(img, img_width=IMAGE_WIDTH, img_height=IMAGE_HEIGHT):

    #Histogram Equalization
    img[:, :, 0] = cv2.equalizeHist(img[:, :, 0])
    img[:, :, 1] = cv2.equalizeHist(img[:, :, 1])
    img[:, :, 2] = cv2.equalizeHist(img[:, :, 2])

    #Image Resizing
    img = cv2.resize(img, (img_width, img_height), interpolation = cv2.INTER_CUBIC)

    return img

def transform_img(img, img_width=IMAGE_WIDTH, img_height=IMAGE_HEIGHT):

    #Histogram Equalization
    img[:, :, 0] = cv2.equalizeHist(img[:, :, 0])
    img[:, :, 1] = cv2.equalizeHist(img[:, :, 1])
    img[:, :, 2] = cv2.equalizeHist(img[:, :, 2])

    #Image Resizing
    img = cv2.resize(img, (img_width, img_height), interpolation = cv2.INTER_CUBIC)

    return img

#Test images path
test_img_paths = [img_path for img_path in glob.glob("/home/carrot/NYU/validation/images/*jpg")]

#Read model architecture and trained model's weights
net = caffe.Net('/home/carrot/NYU/DD_deploy2.prototxt',
                '/home/carrot/NYU/snaps/_iter_5000.caffemodel',
                caffe.TEST)

#Define image transformers
transformer = caffe.io.Transformer({'data_images': net.blobs['data_images'].data.shape})
transformer.set_transpose('data_images', (2,0,1))

i=0
for img_path in train_img_paths:
    #img_path = "/home/carrot/NYU/images/validation/00000_image.jpg"

    #reading an image
    img = cv2.imread(img_path, cv2.IMREAD_COLOR)
    img = transform_img(img, img_width=IMAGE_WIDTH, img_height=IMAGE_HEIGHT)

    #feeding through net
    net.blobs['data_images'].data[...] = transformer.preprocess('data_images', img)
    out = net.forward(end='depth_out')

    #resizing and saving
    transform_back(out, img_width=IMAGE_WIDTH, img_height=IMAGE_HEIGHT)
    cv2.imwrite('/home/carrot/NYU/images/test_out/'+'{0:05}'.format(i)+'_depth'+'.jpg',depth_*255/20)
    i=i+1


#!/usr/bin/python3
"""Defines a Rectangle subclass Square."""
Rectangle = __import__('9-rectangle').Rectangle

class Square(Rectangle):
    """class Square that inherits from Rectangle (9-rectangle.py)."""
    def __init__(self, size):
        """
        Init a new Square.

        Args:
            size (int): The size of square
        """
        self.integer_validator("size", size)
        super().__init__(size, size)
        self.__size = size


#!/usr/bin/env python3

import os
import sys
import re
import argparse
import csv
import pkg_resources
from subprocess import call

try:
    from __init__ import __version__
except ImportError:
    from emm_typing import __version__

from Bio.Blast.Applications import NcbiblastnCommandline

EMM_VERSION = __version__

def emm_argument_parser():
    parser = argparse.ArgumentParser(
        description='Group A streptococci emm-typer, version %s' % EMM_VERSION)
    parser.add_argument('-f', '--fasta',
                        help='FASTA file to type.', nargs='+', required=True, type=argparse.FileType('r'))
    parser.add_argument('--db', type=argparse.FileType('r'),
                        help='Database for trimmed emm types. (If using non-default). It must be blastn database. Only'
                             ' provide the file that do not end with ".n*" something (do not use for example'
                             ' /blast_db.sequences.fasta.nhr)', required=False)
    parser.add_argument('-o', '--outdir', type=str, default='.', help='Output directory where to write all results.')
    parser.add_argument('-v', '--version', help='Show version and exit.', action='version', version=EMM_VERSION)
    args = parser.parse_args()

    return args

def choose_best_match(lines):
    # Verify EMM <= 124
    # Length = 180
    # Pident = 100.0
    # For multiple bests, report in list
    matches = []
    unvalmatches = []
    for row in lines:
        contig = row[0]
        allele = row[1]
        pident = float(row[2])
        length = int(row[3])
        if allele.startswith("EMM"):
            alleleclean = re.match("^EMMG?(\d+)\.\d+$", allele).group(1)
            if not int(alleleclean) <= 124:
                # NOT a verified type
                if pident == 100 and length >= 180:
                    unvalmatches.append([contig, allele, pident, length])
            else:
                if pident == 100 and length >= 180:
                    newbest = [contig, allele, pident, length]
                    # matches.insert(0, newbest)
                    if len(matches) == 0:
                        # If no matches so far
                        matches.append(newbest)
                    else:
                        unvalmatches.append(newbest)
        else:
            if pident == 100 and length >= 180:
                matches.append([contig, allele, pident, length])

    if len(matches) == 0:
        matches = None
    if len(unvalmatches) == 0:
        unvalmatches = None

    return matches, unvalmatches

def main():
    print('===> RUNNING: {}'.format(os.path.realpath(__file__)))

    args = emm_argument_parser()

    args.fasta = [os.path.abspath(fasta.name) for fasta in args.fasta]

    args.outdir = os.path.abspath(args.outdir)
    if not os.path.isdir(args.outdir):
        os.makedirs(args.outdir)

    if not os.path.isdir(os.path.join(args.outdir, 'emm_typing_blast', '')):
        os.makedirs(os.path.join(args.outdir, 'emm_typing_blast', ''))

    # Create DB symbolic link to outdir (avoid permission problems)
    if args.db is not None:
        args.db = os.path.abspath(args.db)
        if not os.path.isfile(args.db):
            sys.exit('Blast DB was not found')
        files = [f for f in os.listdir(os.path.dirname(args.db)) if
                 not f.startswith('.') and os.path.isfile(os.path.join(os.path.dirname(args.db), f)) and
                 f.startswith(os.path.basename(args.db))]
        for file_found in files:
            if os.path.islink(os.path.join(args.outdir, 'emm_typing_blast', file_found)):
                os.remove(os.path.join(args.outdir, 'emm_typing_blast', file_found))
            os.symlink(os.path.join(os.path.dirname(args.db), file_found),
                       os.path.join(args.outdir, 'emm_typing_blast', file_found))
    else:
        args.db = pkg_resources.resource_filename(__name__, os.path.join('data', 'trimmed_emm_types.tfa'))
        files = [f for f in pkg_resources.resource_listdir(__name__, os.path.join('data', '')) if
                 not f.startswith('.') and
                 pkg_resources.resource_exists(__name__, os.path.join('data', f)) and
                 f.startswith(os.path.basename(args.db))]
        for file_found in files:
            if os.path.islink(os.path.join(args.outdir, 'emm_typing_blast', file_found)):
                os.remove(os.path.join(args.outdir, 'emm_typing_blast', file_found))
            os.symlink(os.path.abspath(pkg_resources.resource_filename(__name__, os.path.join('data', file_found))),
                       os.path.join(args.outdir, 'emm_typing_blast', file_found))
    args.db = os.path.join(args.outdir, 'emm_typing_blast', 'trimmed_emm_types.tfa')

    # Test isolate names
    for fasta in args.fasta:
        try:
            _ = re.match("^([\w_\-]+)\.(fasta|fa)$", os.path.basename(fasta)).group(1)
        except AttributeError as e:
            print(e)
            sys.exit("Could not understand isolatename for {} file.\n"
                     "Only a-z, A-Z, numbers, dash and underscore is allowed".format(os.path.basename(fasta)))

    # Sequentially BLAST all fastas against database
    for fasta in args.fasta:
        isolatename = re.match("^([\w_\-]+)\.(fasta|fa)$", os.path.basename(fasta)).group(1)
        assert isolatename

        blastn_cline = NcbiblastnCommandline(query=fasta, db=args.db, perc_identity=100, outfmt=6, max_target_seqs=10,
                                             out=os.path.join(args.outdir,
                                                              'emm_typing_blast',
                                                              '{}_emm_results_blast.tab'.format(isolatename)))
        print(blastn_cline)
        call(blastn_cline(), shell=True)

    # Remove DB symbolic links
    files = [f for f in os.listdir(os.path.join(args.outdir, 'emm_typing_blast', '')) if
             not f.startswith('.') and os.path.islink(os.path.join(args.outdir, 'emm_typing_blast', f)) and
             f.startswith(os.path.basename(args.db))]
    for file_found in files:
        os.remove(os.path.join(args.outdir, 'emm_typing_blast', file_found))

    # Write all results to communal file (or alternatively, to stdout)
    with open(os.path.join(args.outdir, 'emm_results.tab'), 'w') as communalfile:
        header = ["Isolate", "contig", "emm-type", "pident", "length", "unvalidatedmatches"]
        communal = csv.writer(communalfile, delimiter="\t")
        communal.writerow(header)
        # communal.write("\t".join(header) + "\n")
        for fasta in args.fasta:
            isolatename = re.match("^([\w_\-]+)\.(fasta|fa)$", os.path.basename(fasta)).group(1)
            resfile = os.path.join(args.outdir, 'emm_typing_blast', '{}_emm_results_blast.tab'.format(isolatename))
            with open(resfile, 'rU') as individual:
                ilines = csv.reader(individual, delimiter="\t")
                matches, unvalmatches = choose_best_match(ilines)

                if unvalmatches is not None:
                    unvalidatedmatches = [",".join([u[1] for u in unvalmatches])]
                else:
                    unvalidatedmatches = []

                if matches is not None:
                    matches = matches[0]
                else:
                    matches = ['', '', '', '']

                communal.writerow([isolatename] + matches + unvalidatedmatches)

if __name__ == '__main__':
    main()


"""This class runs integrational test cases for testing CoffeeMachine"""

from __future__ import absolute_import

import logging
import unittest

import machine.machine_lib as ml

logger = logging.getLogger(__name__)

class TestMachine(unittest.TestCase):

    def test_integration(self):
        """
        This will load input from specified json file,fetching
        Coffee Machine details: outlets,ingredients, and list of
        beverages
        Create a coffee machine with these details
        Generate all possible outputs(combinations,quantity and ingredients left)
        for any successful permutation of beverages, for this machine
        Run the coffee machine to serve input beverages, sufficient times to
        generate all possible outputs, and verify the output
        Refill the machine after each serve
        """

        machine, beverages = ml.load_json('payload\input1.json')

        initial_ingredients = dict()
        for i in machine.ingredients:
            initial_ingredients[i.name] = i.quantity

        logger.info("available ingredients: %s", initial_ingredients)
        # all possible outputs
        completed_beverages_list, left_ingredients_dict = ml.get_outputs(
            machine, beverages)
        possible_outputs = len(completed_beverages_list)

        for i in range(2*possible_outputs):
            results, left_ingredients = machine.serve_beverages(beverages)
            completed = []
            logger.info("#%s status:", i)
            for result in results:
                is_completed, beverage_name, status = result
                logger.info(status)
                if is_completed:
                    completed.append(beverage_name)

            completed.sort()
            logger.info("left ingredients: %s", left_ingredients)

            # Asserting if completed beverages and left ingredients matches any output
            self.assertIn(completed, completed_beverages_list)
            self.assertEqual(left_ingredients,
                             left_ingredients_dict[tuple(completed)])

            machine.refill()


import os
import re
import csv
import codecs
import numpy as np
import pandas as pd
import operator

from nltk.corpus import stopwords
from nltk.stem import SnowballStemmer
from string import punctuation
from collections import defaultdict
from sklearn.model_selection import train_test_split

import sys

#A function that use dataset path to load the training set
#and the test set

#input: directory path for the dataset folder
#output:
# train_df: dataframe for training
# test_df: dataframe for test
def load_data(path):
    TRAIN_DATA_FILE = path + 'train.csv'
    TEST_DATA_FILE = path + 'test.csv'

    train_df = pd.read_csv(TRAIN_DATA_FILE)
    test_df = pd.read_csv(TEST_DATA_FILE)

    return train_df, test_df

#A function that load the cleaned words that help data pre-processing
#input: path for the cleanwords txt in the form typo,correct

#output: A dictionary with key typo and value corrected

def load_clean(path):
    cl_path = path
    clean_word_dict = {}
    with open(cl_path, 'r', encoding='utf-8') as cl:
        for line in cl:
            line = line.strip('\n')
            typo, correct = line.split(',')
            clean_word_dict[typo] = correct
    return clean_word_dict

#A function that splot the dataset to training_set and dev_set
#input: train_df: The original training set
#percent: the percentage of the original training set we want to put into dev_set
#The default dev set size is set to be 20%

#There is no output for this part, just save two new csv in the dataset
#folder

def train_split(train_df, percent = 0.2):
    np.random.seed(1)
    msk = np.random.rand(len(train_df)) < percent
    train = train_df[~msk]
    dev = train_df[msk]
    return train, dev

#A function that execute the data cleaning duty
#Input: text: the comment texts
#clean_word_dict: dictionary that correct words
# remove_stopwords: If True, remove stop words
# stem_wrods: if True, turn words into their stem form
# count_null_words:

def preprocess(text, clean_word_dict , remove_stopwords = False, stem_words = False):

    # Regex to remove all Non-Alpha Numeric and space
    special_character_removal=re.compile(r'[^?!.,:a-z\d ]',re.IGNORECASE)

    # regex to replace all numerics
    replace_numbers=re.compile(r'\d+',re.IGNORECASE)

    #Making all the words to be lower case
    #Also remove special characters and numeric values
    text = text.lower()

    for typo, correct in clean_word_dict.items():
        text = re.sub(typo, " " + correct + " ", text)

    #expand abbreviation and separate punctuations
    #to be a separate characters
    #This part is from last year's competition
    text = re.sub(r"what's", "what is ", text)
    text = re.sub(r"\'s", " ", text)
    text = re.sub(r"\'ve", " have ", text)
    text = re.sub(r"can't", "cannot ", text)
    text = re.sub(r"n't", " not ", text)
    text = re.sub(r"i'm", "i am ", text)
    text = re.sub(r"\'re", " are ", text)
    text = re.sub(r"\'d", " would ", text)
    text = re.sub(r"\'ll", " will ", text)
    text = re.sub(r",", " ", text)
    text = re.sub(r"\.", " ", text)
    text = re.sub(r"!", " ! ", text)
    text = re.sub(r"\/", " ", text)
    text = re.sub(r"\?", " ? ", text)
    text = re.sub(r"\!", " ! ", text)
    text = re.sub(r"\"", " ", text)
    text = re.sub(r"\^", " ^ ", text)
    text = re.sub(r"\+", " + ", text)
    text = re.sub(r"\-", " - ", text)
    text = re.sub(r"\=", " = ", text)
    text = re.sub(r"'", " ", text)
    text = re.sub(r"(\d+)(k)", r"\g<1>000", text)
    text = re.sub(r":", " : ", text)
    text = re.sub(r" e g ", " eg ", text)
    text = re.sub(r" b g ", " bg ", text)
    text = re.sub(r" u s ", " american ", text)
    text = re.sub(r"\0s", "0", text)
    text = re.sub(r" 9 11 ", "911", text)
    text = re.sub(r"e - mail", "email", text)
    text = re.sub(r"j k", "jk", text)
    text = re.sub(r"\s{2,}", " ", text)

    #remove the numerics
    text = replace_numbers.sub(' ', text)

    #remove the special characters
    text = special_character_removal.sub('',text)

    # Optionally, shorten words to their stems
    if stem_words:
        text = text.split()
        stemmer = SnowballStemmer('english')
        stemmed_words = [stemmer.stem(word) for word in text]
        text = " ".join(stemmed_words)

    # Optionally, remove stop words of the text
    if remove_stopwords:
        stop_words = set(stopwords.words('english'))
        text = text.split()
        filtered_sentence = [w for w in text if not w in stop_words]
        text = " ".join(filtered_sentence)

    return (text)

#performing the data cleaning procesure on the dataset
#input: dataset_path plus / at the end
#clean_word_path: clean word text path

#This function will save cleaned data into two new csv files
#Also will split the data into training and dev set

def clean(dataset_path, clean_word_path):
    #preprocess texts in the datasets
    train_df, test_df = load_data(dataset_path)
    clean_word_dict = load_clean(clean_word_path)

    print('*' * 25)
    print('Start pre-process the texts in the dataset!')
    print('*' * 25)

    list_sentences_train = train_df["comment_text"].fillna("no comment").values
    list_sentences_test = test_df["comment_text"].fillna("no comment").values

    comments = [preprocess(text,clean_word_dict) for text in list_sentences_train]
    test_comments=[preprocess(text,clean_word_dict) for text in list_sentences_test]

    print('*' * 25)
    print('Finished pre-process the texts in the dataset!')
    print('*' * 25)

    # This save the clearned data to the corersponding directory
    train_df['comment_text'] = comments
    test_df['comment_text'] = test_comments

    #Here we split the trian to train and dev for check performance
    new_train_df, new_dev_df = train_split(train_df)

    #Now save those as well
    train_df.to_csv(dataset_path + 'cleaned_train.csv', index=False)
    test_df.to_csv(dataset_path + 'cleaned_test.csv', index=False)
    new_train_df.to_csv(dataset_path + 'cleaned_new_train.csv', index=False)
    new_dev_df.to_csv(dataset_path + 'cleaned_dev.csv', index=False)

#call the function to test
if __name__ == "__main__":
    clean('/Users/MiYu/Desktop/phd/ThirdQuarter/CS229/Projects/ProjectCode/dataset/', '/Users/MiYu/Desktop/phd/ThirdQuarter/CS229/Projects/ProjectCode/features/cleanwords.txt')

    #Use to create a tiny set
    #train_df = pd.read_csv('/Users/MiYu/Desktop/phd/ThirdQuarter/CS229/Projects/ProjectCode/dataset/cleaned_train.csv')
    #_, tiny = train_split(train_df, percent = 0.001)
    #tiny_train, tiny_dev = train_split(tiny, percent = 0.2)
    #tiny_train.to_csv('/Users/MiYu/Desktop/phd/ThirdQuarter/CS229/Projects/ProjectCode/dataset/tiny_train.csv')
    #tiny_dev.to_csv('/Users/MiYu/Desktop/phd/ThirdQuarter/CS229/Projects/ProjectCode/dataset/tiny_dev.csv')


import sys, bridge, time
from os import startfile
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.Qt import QThread
import sys, os, json

from PyQt5 import QtCore, QtGui

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QDialog, QApplication, QWidget, QLineEdit
from PyQt5.QtWebKitWidgets import QWebView

class WebView(QWebView):
    def __init__(self, server_address):
        super().__init__()
        self.server_address = server_address
        self.page().settings().setAttribute(self.settings().DeveloperExtrasEnabled, True)
        frame = self.page().currentFrame()

        self.page().loadFinished.connect(self.loadingFinished)
        self.initUI()

    def loadingFinished(self, bool):
        print('load finished')
        frame = self.page().currentFrame();

        frame.addToJavaScriptWindowObject("obj", self)
        self.mark_sites()

    @pyqtSlot(str)
    def display_media(self, media_file_name):
        print("Media to Display: "+media_file_name)
        media_file_name=media_file_name.replace('//', '\\')
        try:
            startfile(media_file_name)
        except Exception:
            print("Error Occurred while displaying media")

    @pyqtSlot(dict, str)
    def handle_new_alert(self, message_content, media_file_name):
        id = message_content["SourceID"]
        time = message_content["Time"]
        # Time=rectify_time_zone(Time)

        activity = message_content["activity_recognized"]
        location_description = message_content["location_description"]
        frame = self.page().currentFrame();
        print("Sending control to javascript to handle alert")
        print("Media Name:  " + media_file_name)
        frame.evaluateJavaScript('alertHandler({0},{1})'.format(json.dumps(message_content),
                                                                json.dumps(media_file_name)))

    def initUI(self):
        # self.grid.addWidget(QPushButton('c'), 10, 11)

        file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "gmap.html"))
        url = QUrl.fromLocalFile(file_path)
        self.load(url)
        self.show()

    def mark_sites(self):
        surveillance_sites = bridge.get_all_surveillance_sites(self.server_address)
        for site in surveillance_sites:
            self.add_marker(site)

    def add_marker(self, surveillance_site):
        frame = self.page().currentFrame()
        frame.evaluateJavaScript('addSurveillanceSite({0})'.format(json.dumps(surveillance_site)))

def rock(server_address):
    app = QApplication(sys.argv)
    ex = WebView(server_address)

    # ex.metaObject().invokeMethod(ex,'test_function',Qt.QueuedConnection)

    return ex, app, QThread.currentThread()


"""
A simple calculator
"""

print("+-" * 40)
print ("\t\tA SIMPLE CONSOLE CALCULATOR IMPLEMENTED BY @OyugoObonyo")
print("+-" * 40)
operation = input ("Select desired operation. Multiplication, Division, Addition or Subtraction?")
num1 = input ("First number you would like to operate on?")
num2 = input ("Second number you would like to operate on?")
if operation == "Multiplication":
    try:
        product = float(num1) * float(num2)
        print(product)
    except:
        print("ERROR: OPERATION POSSIBLE ONLY ON NUMBERS")
elif operation == "Division":
    try:
        result = float(num1) / float(num2)
        print(result)
    except:
        print("ERROR: OPERATION POSSIBLE ONLY ON NUMBERS")
elif operation == "Addition":
    try:
        result = float(num1) + float(num2)
        print(result)
    except:
        print("ERROR: OPERATION POSSIBLE ONLY ON NUMBERS")
elif operation == "Subtraction":
    try:
        result = float(num1) - float(num2)
        print(result)
    except:
        print("ERROR: OPERATION POSSIBLE ONLY ON NUMBERS")


import cv2
import numpy as np
import math
import matplotlib.pyplot as plt
from matplotlib import colors
from matplotlib.ticker import PercentFormatter

class Imagem:
    def __init__(self, path, mode = None):
        self.path = path
        self.mode = mode

    def open(self):
        try:
            if(self.mode != None):
                self.imgArr = cv2.imread(self.path, self.mode)
                return self.imgArr

            self.imgArr = cv2.imread(self.path)
            return self.imgArr
        except:
            print("Erro ao abrir arquivo de imagem.")
            return None

    def getChannelInGrey(self, channel):

        #check shape

        channels = [
            lambda : self.imgArr[:,:,2], #red
            lambda : self.imgArr[:,:,1], #green
            lambda : self.imgArr[:,:,0]  #blue
        ]

        if channel >= len(channels):
            return None

        return channels[channel]()

    def getChannelInRGB(self, channel):
        #check shape

        img = self.imgArr.copy()

        if channel == 'red':
            img[:, :, 0] = 0
            img[:, :, 1] = 0
        elif channel == 'green':
            img[:, :, 0] = 0
            img[:, :, 2] = 0
        elif channel == 'blue':
            img[:, :, 1] = 0
            img[:, :, 2] = 0

        return img

    def calculateHist(self, pixels):
        histogram = np.zeros(256, dtype=int)

        for i in range(pixels.shape[0]):
            for j in range(pixels.shape[1]):
                histogram[pixels[i,j]] = histogram[pixels[i,j]] + 1

        showHistogram(histogram)

        return histogram

def showHistogram(histogram):
    plt.bar(range(0,256), histogram)
    plt.show()

def threshold(histogram, div: int):
    acumulado1 = 0
    den1 = 0
    acumulado2 = 0
    den2 = 0
    for i in range(0, len(histogram)):

        if i < div:
            acumulado1 += histogram[i] * i
            den1 +=histogram[i]
        else:
            acumulado2 += histogram[i] * i
            den2 +=histogram[i]

    mean = ((acumulado1/den1) + (acumulado2/den2) ) / 2

    if round(mean) == div:
        return div

    return threshold(histogram, round(mean))

def limiarizacao(imagem: np.array, threshold: int) -> np.array:

    lin = imagem.shape[0]
    col = imagem.shape[1]
    lim = np.zeros(shape=(lin, col))

    for i in range(0,lin):
        for j in range(0,col):
            if imagem[i,j] <= threshold:
                lim[i,j] = 0
            else:
                lim[i,j] = 1

    return lim

def main():
    Im = Imagem("vandao.jpg", mode=0)

    imagem = Im.open()

    hist = Im.calculateHist(imagem)
    t = threshold(hist, len(hist)/2 )

    lim = limiarizacao(imagem, t)
    cv2.imshow("Imagem", lim)
    cv2.waitKey(0)

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


import os
import json
from flask import Flask, request, jsonify
from flask_cors import CORS

from model.predictor import Predictor

app = Flask(__name__)
CORS(app)

@app.route('/')
def hello_word():
    return predict("xin chao")

@app.route('/api', methods=['POST'])
def get_prediction():
    text = request.json['text']
    pred = predict(text)
    res = {
        "input": text,
        "predict": pred
    }
    return jsonify(res)

@app.route('/topk', methods=['POST'])
def get_topk_prediction():
    text = request.json['text']
    lm_alpha = request.json['lm_alpha']
    beam_size = request.json['beam_size']
    post_process = request.json['post_process'] if ('post_process' in request.json) else True
    re_scale = request.json['re_scale'] if ('re_scale' in request.json) else True
    preds = Predictor().predict_topk(text, beam_size=beam_size, alpha=lm_alpha, post_process=post_process, re_scale=re_scale)
    res = {}
    for i, p in enumerate(preds):
        res[str(i)] = {
            "score": 10**p[0],
            "text": p[1]
        }
    return jsonify(res)

def init_predictor(config_path):
    with open(config_path) as f:
        config = json.load(f)
    dir_name = os.path.abspath(os.path.dirname(config_path))
    config['src_vocab'] = os.path.join(dir_name, config['src_vocab'])
    config['tgt_vocab'] = os.path.join(dir_name, config['tgt_vocab'])
    config['model_weight'] = os.path.join(dir_name, config['model_weight'])
    if config['lm_path']:
        config['lm_path'] = os.path.join(dir_name, config['lm_path'])

    Predictor(config)

def predict(text):
    predictor = Predictor()
    return predictor.predict(text)

if __name__ == "__main__":
    init_predictor("weight/config.json")
    app.run('0.0.0.0', port=5000)


from flask import jsonify, Blueprint, render_template
from app import db
import datetime, time, pprint
from app.models import Exhibitor_Profile

mod = Blueprint('search', __name__)

@mod.route('/<search_id>', methods=['GET'])
def index(search_id):
	search_results = db.session.query(Exhibitor_Profile).filter((Exhibitor_Profile.location.like('%%%s%%' % search_id)) | (Exhibitor_Profile.fullname.like('%%%s%%' % search_id)) | (Exhibitor_Profile.companyname.like('%%%s%%' %  search_id)) | (Exhibitor_Profile.companyinfo.like('%%%s%%' %  search_id)) | (Exhibitor_Profile.position.like('%%%s%%' %  search_id))).all()
	#pprint.pprint(search_results)

	search = []
	for search_result in search_results:
		search_element = {}
		search_element['fullname'] = search_result.fullname
		search_element['position'] = search_result.position
		search_element['companyname'] = search_result.companyname
		search_element['location'] = search_result.location
		search_element['companyinfo'] = search_result.companyinfo
		search.append(search_element)

	return jsonify({"search": search})


import pygame
import sys
import random
import noise

from pygame.locals import *

# INITIALISATIONS
clock = pygame.time.Clock()

pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.init()

pygame.mixer.set_num_channels(64)  # how many sounds can play at the same time

pygame.display.set_caption("Pygame Window")

main_font = pygame.font.SysFont("comicsans", 50)

# VARIABLES
WINDOW_SIZE = (600, 400)
TILE_SIZE = 16
FPS = 60

screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32)

display = pygame.Surface((300, 200))

# player_image = pygame.transform.scale(pygame.image.load('Assets/player.png'), (20, 20))
# player_image.set_colorkey((255, 255, 255))
# this makes the white pixels in the player image transparent

grass_image = pygame.transform.scale(pygame.image.load('Assets/grass.png'), (TILE_SIZE, TILE_SIZE))
dirt_image = pygame.transform.scale(pygame.image.load('Assets/grass.png'), (TILE_SIZE, TILE_SIZE))
# this should be dirt.png but i'm temporarily just gonna use grass for all

plant_image = pygame.transform.scale(pygame.image.load('Assets/GraveRobber.png'), (TILE_SIZE, TILE_SIZE))

tile_index = {1: grass_image,
              2: dirt_image,
              3: plant_image}

background_objects = [[0.25, [120, 10, 70, 400]],
                      [0.25, [280, 30, 40, 400]],
                      [0.5, [30, 40, 40, 400]],
                      [0.5, [130, 90, 100, 400]],
                      [0.5, [300, 80, 120, 400]]]

# def load_map(path):
#     f = open(path + '.txt', 'r')
#     data = f.read()
#     f.close()
#     data = data.split('\n')
#     game_map = []
#     for row in data:
#         game_map.append(list(row))
#     return game_map

# game_map = load_map('map')  # creates map from a list which is not that good, a dictionary is better

CHUNK_SIZE = 8

def generate_chunk(x, y):
    # whole world -> chunk -> tile
    chunk_data = []
    for y_pos in range(CHUNK_SIZE):
        for x_pos in range(CHUNK_SIZE):
            target_x = x * CHUNK_SIZE + x_pos
            target_y = y * CHUNK_SIZE + y_pos
            tile_type = 0  # nothing
            height = int(noise.pnoise1(target_x * 0.2, repeat=9999999) * 5)
            if target_y > 8 - height:
                tile_type = 2  # dirt
            elif target_y == 8 - height:
                tile_type = 1  # grass
            elif target_y == 7 - height:
                if random.randint(1, 5) == 1:
                    tile_type = 3  # plant
            if tile_type != 0:
                chunk_data.append([[target_x, target_y], tile_type])
    return chunk_data

game_map = {}

# only add chunks when they would appear on screen

global animation_frames
animation_frames = {}

def load_animation(path, frame_durations):
    global animation_frames
    animation_name = path.split('/')[-1]
    animation_frame_data = []
    n = 0
    for frame in frame_durations:
        animation_frame_id = animation_name + '_' + str(n)
        img_loc = path + '/' + animation_frame_id + '.png'
        # print(img_loc)
        animation_image = pygame.transform.scale(pygame.image.load(img_loc), (20, 30))
        animation_image.set_colorkey((0, 0, 0))
        animation_frames[animation_frame_id] = animation_image.copy()
        for i in range(frame):
            animation_frame_data.append(animation_frame_id)
        n += 1
    return animation_frame_data

#
# def load_sprite(path, frame_durations):
#     image = pygame.image.load(path)
#     for i in range(len(frame_durations)):
#
#
#

animation_database = {}

animation_database['run'] = load_animation('Assets/player_animation/run', [7, 7, 7]) #, 7, 7, 7])
animation_database['idle'] = load_animation('Assets/player_animation/idle', [7, 7, 14, 7])

# Action variables
player_action = 'idle'
player_frame = 0
player_flip = False

# Sounds
jump_sound = pygame.mixer.Sound('Assets/sound/platformer_jump.mp3')
grass_sounds = [pygame.mixer.Sound('Assets/sound/platformer_jump.mp3'),
                pygame.mixer.Sound('Assets/sound/platformer_jump.mp3')]
grass_sounds[0].set_volume(0.4)
grass_sounds[1].set_volume(0.4)
# make sounds for the grass

pygame.mixer.music.load('Assets/sound/platformer_soundtrack.mp3')
pygame.mixer.music.play(-1)

# Grass sound stuff
grass_sound_timer = 0

def change_action(action_var, frame, new_value):
    if action_var != new_value:
        action_var = new_value
        frame = 0
    return action_var, frame

def collision_test(rect, tiles):
    hit_list = []
    for tile in tiles:
        if rect.colliderect(tile):
            hit_list.append(tile)
    return hit_list

def move(rect, movement, tiles):
    collision_types = {'top': False, 'bottom': False, 'right': False, 'left': False}
    rect.x += movement[0]
    hit_list = collision_test(rect, tiles)
    for tile in hit_list:
        if movement[0] > 0:
            rect.right = tile.left
            collision_types['right'] = True
        elif movement[0] < 0:
            rect.left = tile.right
            collision_types['left'] = True

    rect.y += movement[1]
    hit_list = collision_test(rect, tiles)
    for tile in hit_list:
        if movement[1] > 0:
            rect.bottom = tile.top
            collision_types['bottom'] = True
        elif movement[1] < 0:
            rect.top = tile.bottom
            collision_types['top'] = True
    return rect, collision_types

moving_right = False
moving_left = False

player_y_momentum = 0
air_timer = 0

true_scroll = [0, 0]

player_rect = pygame.Rect(50, 50, 30, 30)
test_rect = pygame.Rect(100, 100, 100, 50)

camera_offset_x = 120
camera_offset_y = 70

# MAIN LOOP
while True:
    display.fill((146, 244, 255))

    if grass_sound_timer > 0:
        grass_sound_timer -= 1

    if moving_left:
        camera_offset_x = 140

    true_scroll[0] += (player_rect.x - true_scroll[0] - camera_offset_x) / 10
    true_scroll[1] += (player_rect.y - true_scroll[1] - camera_offset_y) / 10
    scroll = true_scroll.copy()
    scroll[0] = int(scroll[0])
    scroll[1] = int(scroll[1])

    pygame.draw.rect(display, (7, 80, 75), pygame.Rect(0, 120, 300, 80))
    for background_object in background_objects:
        obj_rect = pygame.Rect(background_object[1][0] - scroll[0] * background_object[0],
                               background_object[1][1] - scroll[1] * background_object[0],
                               background_object[1][2], background_object[1][3])
        if background_object[0] == 0.5:
            pygame.draw.rect(display, (14, 222, 150), obj_rect)
        else:
            pygame.draw.rect(display, (9, 91, 85), obj_rect)

    tile_rects = []

    # Tile rendering
    for y in range(4):
        for x in range(4):
            target_x = x - 1 + int(round(scroll[0] / (CHUNK_SIZE * TILE_SIZE)))
            target_y = y - 1 + int(round(scroll[1] / (CHUNK_SIZE * TILE_SIZE)))
            target_chunk = str(target_x) + ';' + str(target_y)
            if target_chunk not in game_map:
                game_map[target_chunk] = generate_chunk(target_x, target_y)
            for tile in game_map[target_chunk]:
                display.blit(tile_index[tile[1]],
                             (tile[0][0] * TILE_SIZE - scroll[0], tile[0][1] * TILE_SIZE - scroll[1]))
                if tile[1] in [1, 2]:
                    tile_rects.append(pygame.Rect(tile[0][0] * TILE_SIZE,
                                                  tile[0][1] * TILE_SIZE, TILE_SIZE, TILE_SIZE))

    # y = 0
    # for row in game_map:
    #     x = 0
    #     for tile in row:
    #         if tile == '1':
    #             display.blit(dirt_image, (x * TILE_SIZE - scroll[0], y * TILE_SIZE - scroll[1]))
    #         if tile == '2':
    #             display.blit(grass_image, (x * TILE_SIZE - scroll[0], y * TILE_SIZE - scroll[1]))
    #         if tile != '0':
    #             tile_rects.append(pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE))
    #         x += 1
    #     y += 1
    # This is code that used the list map

    player_movement = [0, 0]
    if moving_right:
        player_movement[0] += 2
    if moving_left:
        player_movement[0] -= 2
    player_movement[1] += player_y_momentum
    player_y_momentum += 0.2
    if player_y_momentum > 3:
        player_y_momentum = 3

    if player_movement[0] > 0:
        player_action, player_frame = change_action(player_action, player_frame, 'run')
        player_flip = False
    if player_movement[0] == 0:
        player_action, player_frame = change_action(player_action, player_frame, 'idle')
    if player_movement[0] < 0:
        player_action, player_frame = change_action(player_action, player_frame, 'run')
        player_flip = True

    player_rect, collisions = move(player_rect, player_movement, tile_rects)

    if collisions['bottom']:
        player_y_momentum = 0
        air_timer = 0
        if player_movement[0] != 0:
            if grass_sound_timer == 0:
                grass_sound_timer = 120
                random.choice(grass_sounds).play()
    else:
        air_timer += 1
    if collisions['top']:
        player_y_momentum = 0

    player_frame += 1
    if player_frame >= len(animation_database[player_action]):
        player_frame = 0
    player_img_id = animation_database[player_action][player_frame]
    player_image = animation_frames[player_img_id]
    display.blit(pygame.transform.flip(player_image, player_flip, False),
                 (player_rect.x - scroll[0], player_rect.y - scroll[1]))

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_w:
                pygame.mixer.music.fadeout(1000)
            if event.key == K_e:
                pygame.mixer.music.play(-1)
            if event.key == K_RIGHT:
                moving_right = True
            if event.key == K_LEFT:
                moving_left = True
            if event.key == K_UP:
                if air_timer < 6:
                    jump_sound.play()
                    player_y_momentum = -5
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                moving_right = False
            if event.key == K_LEFT:
                moving_left = False

    fps = str(int(clock.get_fps()))
    fps_label = main_font.render(fps, True, (255, 255, 255))
    display.blit(fps_label, (WINDOW_SIZE[0] / 4 - fps_label.get_width() / 2, 2))

    dummy_surface = pygame.transform.scale(display, WINDOW_SIZE)
    screen.blit(dummy_surface, (0, 0))

    pygame.display.update()
    clock.tick(FPS)


from pytest import fixture, fail
from pytest_bdd import scenarios, when, then, parsers
from types import SimpleNamespace
from countdown import solve
from hamcrest import assert_that, equal_to, none

scenarios("./countdown.feature")

@fixture
def context():
    return SimpleNamespace()

@when(parsers.parse("I call the solver with target number <target> and numbers <numbers>"))
@when(parsers.parse("I call the solver with target number {target} and numbers {numbers}"))
def step(context, target, numbers):
    context.target = int(target)
    context.numbers = [int(n.strip()) for n in numbers.split(",")]
    context.result = solve(context.target, *context.numbers)

@then(parsers.parse("a solution is found whose value equals the target number and which uses <count> numbers"))
def step(context, count):
    check_found_result(context.result, context.target, int(count), context.numbers)

@then(parsers.parse("a solution is found whose value equals {value} and which uses {count} numbers"))
def step(context, value, count):
    check_found_result(context.result, int(value), int(count), context.numbers)

def check_found_result(result, expected_value, expected_count, expected_numbers):
    assert_that(result.value, equal_to(expected_value))
    assert_that(len(result.numbers), equal_to(expected_count))
    for n in result.numbers:
        try:
            expected_numbers.remove(n)
        except:
            fail(f"Unexpected number {n}")

@then("no solution is found")
def step(context):
    assert_that(context.result, none())


import requests
import pickle

def log_users(users: list[dict], res: dict, rel: dict):
    for user in users:
        res[user["id"]] = {"id": user["id"], "username": user["username"], "email": user["email"], "posts": 0, "comments": 0}
        rel[user["email"]] = user["id"]

def log_posts(posts: list[dict], res: dict):
    for post in posts:
        userid = post["userId"]
        res[userid]["posts"] += 1

def log_comments(comments: list[dict], res: dict, rel: dict):
    for comment in comments:
        email = comment["email"]
        if email in rel:
            res[rel[comment["email"]]]["comments"] += 1

base_url = "https://jsonplaceholder.typicode.com/"
users_url = base_url + "users"
posts_url = base_url + "posts"
comments_url = base_url + "comments"

users = requests.get(url=users_url).json()
posts = requests.get(url=posts_url).json()
comments = requests.get(url=comments_url).json()

res = {}
rel = {}

log_users(users, res, rel)
log_posts(posts, res)
log_comments(comments, res, rel)
rly_res = [stat_user for stat_user in res.values()]

print(rly_res)
response = requests.post("https://webhook.site/f4aa8350-71a1-40d4-9301-ef32e3eae270", json={"statistics": rly_res})

with open("solution.pickle", "wb") as f:
    pickle.dump(response, f)