from __future__ import annotations

import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
import torchmetrics as metrics
import torchvision as vision

import flcore
import flcore.utils.data as data_utils
from flcore.client import MetricResult

class FedAvgClient(flcore.Client):
    def __init__(
            self,
            *,
            id_: str,
            max_epoch: int,
            learning_rate: float,
            train_dataloader: data.DataLoader,
            eval_dataloader: data.DataLoader,
            test_dataloader: data.DataLoader,
            device: torch.device,
            num_class: int,
    ):
        self.id = id_
        self.device = device
        self.dataset_size = len(train_dataloader.dataset)  # type: ignore
        self.max_epoch = max_epoch
        self.learning_rate = learning_rate
        self.train_dataloader = train_dataloader
        self.eval_dataloader = eval_dataloader
        self.test_dataloader = test_dataloader
        self.metrics = metrics.MetricCollection(
            {
                "loss": metrics.MeanMetric(nan_strategy="error"),
                "accuracy": metrics.Accuracy(
                    "multiclass", num_classes=num_class, average="micro"
                ),
            }
        )

        self.metrics.to(self.device)

    def train(self):
        self.model.train()
        optimizer = optim.SGD(self.model.parameters(), lr=self.learning_rate)
        loss_fn = nn.CrossEntropyLoss()

        for _ in range(self.max_epoch):
            for x, y in self.train_dataloader:
                x, y = x.to(self.device), y.to(self.device)

                optimizer.zero_grad()

                y_hat = self.model(x)

                loss = loss_fn(y_hat, y)
                loss.backward()
                optimizer.step()

    def _eval(self, dataloader: data.DataLoader) -> flcore.MetricResult:
        self.model.eval()
        self.metrics.reset()
        loss_fn = nn.CrossEntropyLoss()

        with torch.no_grad():
            for x, y in dataloader:
                x, y = x.to(self.device), y.to(self.device)
                y_hat = self.model(x)
                loss = loss_fn(y_hat, y)
                self.metrics.update(preds=y_hat, target=y, value=loss)
        metric_result = flcore.MetricResult(
            {name: value.item() for name, value in self.metrics.compute().items()}
        )

        return metric_result

    def evaluate(self) -> MetricResult:
        return self._eval(self.eval_dataloader)

    def test(self) -> MetricResult:
        return self._eval(self.test_dataloader)

class FedAvgServer(flcore.Server):
    pass

class FedAvg(flcore.FederatedLearning):
    def algorithm(self):
        for global_epoch in self.progress(range(self.server.max_epoch), "Global Epoch"):
            selected_clients = self.server.select_clients()

            # train local models
            local_models = []
            weights = []
            for client in self.progress(selected_clients, "Trained Client"):
                with client:
                    client.receive_model(self.server.model)
                    client.train()
                    model = client.send_model()
                    local_models.append(model)
                    weights.append(
                        client.dataset_size
                        / sum(c.dataset_size for c in selected_clients)
                    )

            self.server.aggregate(local_models, weights)

            # evaluate
            evaluation = self.server.evaluate()

            # log
            self.log(flcore.LogItem(epoch=global_epoch, metrics=evaluation))

        evaluation = self.server.test()

        # save the results
        self.log(
            flcore.LogItem(
                epoch=self.server.max_epoch,
                metrics=evaluation,
                others={
                    "global_model": self.server.model,
                },
            ),
            big_item=True,
            filename="model.pth",
        )

class CnnModel(nn.Module):
    def __init__(self, in_channels=1, num_class=10, dim=1024):
        super().__init__()
        self.conv1 = nn.Sequential(
            nn.Conv2d(in_channels, 32, kernel_size=5, padding=0, stride=1, bias=True),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(2, 2)),
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(32, 64, kernel_size=5, padding=0, stride=1, bias=True),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(2, 2)),
        )
        self.fc1 = nn.Sequential(nn.Linear(dim, 512), nn.ReLU(inplace=True))
        self.fc = nn.Linear(512, num_class)

    def forward(self, x):
        out = self.conv1(x)
        out = self.conv2(out)
        out = torch.flatten(out, 1)
        out = self.fc1(out)
        out = self.fc(out)
        return out

def main():
    transforms = vision.transforms.Compose(
        [
            vision.transforms.ToTensor(),
            vision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
        ]
    )
    dataset = data.ConcatDataset(
        [
            vision.datasets.CIFAR10(
                "tests/data/", download=True, train=True, transform=transforms
            ),
            vision.datasets.CIFAR10(
                "tests/data/", download=True, train=False, transform=transforms
            ),
        ]
    )
    subsets = data_utils.generate_dirichlet_subsets(
        dataset=dataset, alphas=[1] * 100, min_data=40
    )
    train_test_subsets = [
        data.random_split(subset, [0.6, 0.2, 0.2]) for subset in subsets
    ]

    server = FedAvgServer(
        model=CnnModel(3, 10, 1600),
        select_ratio=0.1,
        max_epoch=100,
        learning_rate=1.0,
        robust_fn=None,
    )

    system = FedAvg(server=server, log_dir="output")

    for i in range(100):
        device = torch.device("cuda")
        client = FedAvgClient(
            id_=str(i),
            learning_rate=0.05,
            max_epoch=1,
            train_dataloader=data.DataLoader(
                train_test_subsets[i][0], batch_size=8, shuffle=True, drop_last=True
            ),
            eval_dataloader=data.DataLoader(
                train_test_subsets[i][1], batch_size=8, shuffle=True, drop_last=True
            ),
            test_dataloader=data.DataLoader(
                train_test_subsets[i][2], batch_size=8, shuffle=True, drop_last=True
            ),
            device=device,
            num_class=10,
        )
        server.register_client(client)

    system.run()

if __name__ == "__main__":
    main()


from collections import deque

N ,M = map(int,input().split())

board = [list(map(int,input())) for _ in range(N)]

dx=[0,1,0,-1] # 우 하 좌 상
dy=[1,0,-1,0]

def BFS(x,y):

    queue = deque()
    queue.append((x,y))
    while queue:
        x,y = queue.popleft()

        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if 0<= nx <N and 0<= ny < M:
                if board[nx][ny] == 1:
                    board[nx][ny] = board[x][y] +1
                    queue.append((nx,ny))

    return board[N-1][M-1]

ans = BFS(0,0)
print(ans)


import random
import threading
import time
from loguru import logger
import sys

from lib.rdt_listener.rdt_listener import RDTListener, STOP_AND_WAIT

config = {
    "handlers": [
        {"sink": sys.stdout, "level": "DEBUG"},
    ],
}
logger.configure(**config)

def new_handle_client(stream):
    logger.critical("Sending data to client")
    stream.send(b"Hello from server, this is a test, please ignore")
    time.sleep(random.random() * 5)
    stream.close()

def main():
    listener = RDTListener(STOP_AND_WAIT)

    listener.bind(("127.0.0.1", 1234))
    listener.listen(5)
    threads = []
    for i in range(1):
        stream = listener.accept()
        logger.critical("Stream accepted")
        thread = threading.Thread(target=new_handle_client, args=(stream,))
        thread.start()
        threads.append(thread)

    for thread in threads:
        thread.join()
    listener.close()

if __name__ == "__main__":
    main()


import os.path

from math import log

class TransFIC(object):
	# Consequence group
	CT_SYNONYMOUS = 1
	CT_STOP = 2
	CT_FRAMESHIFT = 3
	CT_NON_SYNONYMOUS = 4

	# Impact class
	HIGH_IMPACT_CLASS = 1
	MEDIUM_IMPACT_CLASS = 2
	LOW_IMPACT_CLASS = 3
	UNKNOWN_IMPACT_CLASS = 4
	NONE_IMPACT_CLASS = 5

	CLASS_NAME = {
		HIGH_IMPACT_CLASS : "high",
		MEDIUM_IMPACT_CLASS : "medium",
		LOW_IMPACT_CLASS : "low",
		UNKNOWN_IMPACT_CLASS : "unknown",
		NONE_IMPACT_CLASS : "none"
	}

	@staticmethod
	def higher_impact(a, b):
		return min(a, b)

	@staticmethod
	def class_name(cls):
		if cls is None:
			return None
		return TransFIC.CLASS_NAME[cls]

	def __init__(self, data_path):
		self.data_path = data_path

		self.__partitions = {}

	def __load_partition(self, name):
		if name not in self.__partitions:
			self.__partitions[name] = {}

		part = self.__partitions[name]

		with open(os.path.join(self.data_path, name + ".genes"), "r") as f:
			f.readline() # discard header
			for line in f:
				fields = line.rstrip("\n").split("\t")
				gene = fields[0]
				#sift_mean, sift_sd, pph2_mean, pph2_sd, ma_mean, ma_sd = [float(x) for x in fields[1:7]]
				part[gene] = tuple([float(x) for x in fields[1:7]])

		return part

	def __get_partition(self, name):
		if name not in self.__partitions:
			self.__load_partition(name)

		return self.__partitions[name]

	def __calculate(self, ct_type, score, mean, sd, class_thresholds, is_ma=False):
		if score is None:
			return (None, None)

		if ct_type == self.CT_SYNONYMOUS:
			tfic = mean - 2.0 * sd
		elif ct_type in [self.CT_FRAMESHIFT, self.CT_STOP]:
			tfic = mean + 2.0 * sd
		elif ct_type == self.CT_NON_SYNONYMOUS:
			if not is_ma:
				if score == 0.0:
					score = 0.001
				elif score == 1.0:
					score = 0.999

				tfic = log(score / (1 - score)) - (mean / sd)
			else:
				tfic = (score - mean) / sd

		if tfic < class_thresholds[0]:
			tfic_class = self.LOW_IMPACT_CLASS
		elif tfic < class_thresholds[1]:
			tfic_class = self.MEDIUM_IMPACT_CLASS
		else:
			tfic_class = self.HIGH_IMPACT_CLASS

		return tfic, tfic_class

	def calculate(self, part_name, gene, ct, sift_score, pph2_score, ma_score):
		part = self.__get_partition(part_name)

		if gene not in part:
			return (None, None, None, None, None, None)

		sift_mean, sift_sd, pph2_mean, pph2_sd, ma_mean, ma_sd = part[gene]

		sift_tfic, sift_class = self.__calculate(ct, sift_score, sift_mean, sift_sd, (0.0, 2.0))
		pph2_tfic, pph2_class = self.__calculate(ct, pph2_score, pph2_mean, pph2_sd, (0.0, 1.5))
		ma_tfic, ma_class = self.__calculate(ct, ma_score, ma_mean, ma_sd, (1.0, 3.0), is_ma=True)

		return (sift_tfic, sift_class, pph2_tfic, pph2_class, ma_tfic, ma_class)


import sys
from gettext import gettext as _
import gaf.netlist.package

class Package(gaf.netlist.package.Package):
    def __init__(self, *args, **kwds):
        gaf.netlist.package.Package.__init__(self, *args, **kwds)

    def error(self, msg):
        sys.stderr.write(_("(re-packaged) package `%s': error: %s\n") % (
                             self.refdes, msg))
        self.netlist.failed = True

    def warn(self, msg):
        sys.stderr.write(_("(re-packaged) package `%s': warning: %s\n") % (
                             self.refdes, msg))

class PackagePin(gaf.netlist.package.PackagePin):
    def __init__(self, *args, **kwds):
        gaf.netlist.package.PackagePin.__init__(self, *args, **kwds)

    def error(self, msg):
        sys.stderr.write(
            _("(re-packaged) package `%s', pin `%s': error: %s\n") % (
                self.package.refdes, self.number, msg))
        self.package.netlist.failed = True

    def warn(self, msg):
        sys.stderr.write(
            _("(re-packaged) package `%s', pin `%s': warning: %s\n") % (
                self.package.refdes, self.number, msg))

def blueprint_requires_refdes(component):
    if component.is_graphical or component.has_netname_attrib \
                              or component.has_portname_attrib:
        return False

    for pin in component.pins:
        if pin.has_netattrib:
            return False

    return True

## Re-group components into packages using a custom refdes function.
#
# With the new netlisting code, netlist generation is completely
# independent from the backend invocation.  Therefore, it is no longer
# possible for a backend to override the way \c gnetlist determines
# the refdes of a component in order to achieve a different grouping
# of components into packages.
#
# This function allows a backend to repeat the grouping stage using a
# custom refdes function.  It doesn't change the actual netlist but
# returns a list of "alternative" package objects which can be used
# instead of \c netlist.packages.
#
# \a refdes_func should be a callback function which returns the
# desired refdes for a given component instance.  In the simplest
# case, it would do some kind of transformation on \c
# component.blueprint.refdes.

def repackage(netlist, refdes_func):
    new_packages = []
    pkg_dict = {}

    for component in netlist.components:
        new_refdes = refdes_func(component)

        if new_refdes is None:
            if blueprint_requires_refdes(component.blueprint):
                component.warning(_("component dropped during re-packaging"))
            continue

        if netlist.flat_package_namespace:
            namespace = None
        else:
            namespace = component.sheet.namespace

        try:
            package = pkg_dict[namespace, new_refdes]
        except KeyError:
            package = Package(netlist, namespace, new_refdes)
            new_packages.append(package)
            pkg_dict[namespace, new_refdes] = package

        package.components.append(component)

        for cpin in component.cpins:
            try:
                ppin = package.pins_by_number[cpin.blueprint.number]
            except KeyError:
                ppin = PackagePin(package, cpin.blueprint.number)
                package.pins.append(ppin)
                package.pins_by_number[cpin.blueprint.number] = ppin
            ppin.cpins.append(cpin)

    for package in new_packages:
        for ppin in package.pins:
            nets = []
            for cpin in ppin.cpins:
                if cpin.local_net.net not in nets:
                    nets.append(cpin.local_net.net)
            assert nets
            if len(nets) > 1:
                ppin.error(_("multiple nets connected to pin "
                             "after re-packaging: %s")
                           % _(" vs. ").join(_("\"%s\"") % net.name
                                             for net in nets))
            ppin.net = nets[0]

    for package in new_packages:
        if package.namespace is not None:
            package.refdes = netlist.refdes_mangle_func(
                package.unmangled_refdes, package.namespace)
        else:
            # If refdes mangling is disabled, packages don't have
            # a sheet attribute, so just use the unmangled refdes.
            package.refdes = package.unmangled_refdes

    # compile convenience hash, checking for cross-page name clashes
    packages_by_refdes = {}
    for package in new_packages:
        if package.refdes in packages_by_refdes:
            other_package = packages_by_refdes[package.refdes]
            self.error(_("refdes conflict across hierarchy after re-packaging: "
                         "refdes `%s' is used by package `%s' on page "
                         "`%s' and by package `%s' on page `%s'") % (
                package.refdes,
                other_package.unmangled_refdes,
                netlist.refdes_mangle_func('', other_package.namespace),
                package.unmangled_refdes,
                netlist.refdes_mangle_func('', package.namespace)))
        packages_by_refdes[package.refdes] = package

    return new_packages


from os.path import basename

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import (QLabel, QPushButton, QVBoxLayout, QGroupBox,
                             QGridLayout)

from videomorph.converter.utils import write_size
from videomorph.converter.utils import write_time

class InfoDialog(QtWidgets.QDialog):
    """Changelog Dialog."""

    def __init__(self, parent=None, position=0, media_list=None):
        super(InfoDialog, self).__init__(parent)
        self.position = position
        self.media_list = media_list

        self.central_widget = QtWidgets.QWidget(self)
        self.resize(300, 400)
        self.setWindowTitle(self.tr('Properties'))

        whole_layout = QVBoxLayout(self.central_widget)

        gb_general = QGroupBox(self.central_widget)
        gb_general.setTitle(self.tr('General'))

        general_layout = QVBoxLayout(gb_general)

        general_grid = QGridLayout()
        general_grid.setColumnStretch(1, 1)

        label_file_name = QLabel(gb_general)
        label_file_name.setText(self.tr('File Name:'))

        self.label_file_name_value = QLabel(gb_general)
        self.label_file_name_value.setText("")

        label_size = QLabel(gb_general)
        label_size.setText(self.tr('Size:'))

        self.label_size_value = QLabel(gb_general)
        self.label_size_value.setText("")

        label_duration = QLabel(gb_general)
        label_duration.setText(self.tr('Duration:'))

        self.label_duration_value = QLabel(gb_general)
        self.label_duration_value.setText("")

        label_format_name = QLabel(gb_general)
        label_format_name.setText(self.tr('Format Name:'))

        self.label_format_name_value = QLabel(gb_general)
        self.label_format_name_value.setText("")

        label_format_long_name = QLabel(gb_general)
        label_format_long_name.setText(self.tr('Format Long Name:'))

        self.label_format_long_name_value = QLabel(gb_general)
        self.label_format_long_name_value.setText("")

        general_grid.addWidget(label_file_name, 0, 0, 1, 1)
        general_grid.addWidget(self.label_file_name_value, 0, 1, 1, 1)
        general_grid.addWidget(label_size, 1, 0, 1, 1)
        general_grid.addWidget(self.label_size_value, 1, 1, 1, 1)
        general_grid.addWidget(label_duration, 2, 0, 1, 1)
        general_grid.addWidget(self.label_duration_value, 2, 1, 1, 1)
        general_grid.addWidget(label_format_name, 3, 0, 1, 1)
        general_grid.addWidget(self.label_format_name_value, 3, 1, 1, 1)
        general_grid.addWidget(label_format_long_name, 4, 0, 1, 1)
        general_grid.addWidget(self.label_format_long_name_value, 4, 1, 1, 1)

        general_layout.addLayout(general_grid)

        gb_video = QGroupBox(self.central_widget)
        gb_video.setTitle(self.tr('Video'))
        video_layout = QVBoxLayout(gb_video)
        video_grid = QGridLayout()
        video_grid.setColumnStretch(1, 1)

        label_bit_rate = QLabel(gb_video)
        label_bit_rate.setText(self.tr('Bit Rate:'))

        self.label_bit_rate_value = QLabel(gb_video)
        self.label_bit_rate_value.setText("")

        label_width = QLabel(gb_video)
        label_width.setText(self.tr('Width:'))

        self.label_width_value = QLabel(gb_video)
        self.label_width_value.setText("")

        label_height = QLabel(gb_video)
        label_height.setText(self.tr('Height:'))

        self.label_height_value = QLabel(gb_video)
        self.label_height_value.setText("")

        label_codec_long_name = QLabel(gb_video)
        label_codec_long_name.setText(self.tr('Codec Long Name:'))

        self.label_codec_long_name_value = QLabel(gb_video)
        self.label_codec_long_name_value.setText("")

        label_codec_name = QLabel(gb_video)
        label_codec_name.setText(self.tr('Codec Name:'))

        self.label_codec_name_value = QLabel(gb_video)
        self.label_codec_name_value.setText("")

        video_grid.addWidget(label_bit_rate, 2, 0, 1, 1)
        video_grid.addWidget(self.label_bit_rate_value, 2, 1, 1, 1)
        video_grid.addWidget(label_width, 3, 0, 1, 1)
        video_grid.addWidget(self.label_width_value, 3, 1, 1, 1)
        video_grid.addWidget(label_height, 4, 0, 1, 1)
        video_grid.addWidget(self.label_height_value, 4, 1, 1, 1)
        video_grid.addWidget(label_codec_long_name, 1, 0, 1, 1)
        video_grid.addWidget(self.label_codec_long_name_value, 1, 1, 1, 1)
        video_grid.addWidget(label_codec_name, 0, 0, 1, 1)
        video_grid.addWidget(self.label_codec_name_value, 0, 1, 1, 1)

        video_layout.addLayout(video_grid)

        gb_audio = QGroupBox(self.central_widget)
        gb_audio.setTitle(self.tr('Audio'))

        audio_layout = QVBoxLayout(gb_audio)

        audio_grid = QGridLayout()
        audio_grid.setColumnStretch(1, 1)

        label_acodec_name = QLabel(gb_audio)
        label_acodec_name.setText(self.tr('Codec Name:'))

        self.label_acodec_name_value = QLabel(gb_audio)
        self.label_acodec_name_value.setText("")

        label_acodec_long_name = QLabel(gb_audio)
        label_acodec_long_name.setText(self.tr('Codec Long Name:'))

        self.label_acodec_long_name_value = QLabel(gb_audio)
        self.label_acodec_long_name_value.setText("")

        self.ok_button = QPushButton()
        self.ok_button.setText('OK')
        self.ok_button.clicked.connect(self.close)

        audio_grid.addWidget(label_acodec_name, 0, 0, 1, 1)
        audio_grid.addWidget(self.label_acodec_name_value, 0, 1, 1, 1)
        audio_grid.addWidget(label_acodec_long_name, 1, 0, 1, 1)
        audio_grid.addWidget(self.label_acodec_long_name_value, 1, 1, 1, 1)
        audio_grid.addWidget(self.ok_button, 2, 2, 2, 2)

        audio_layout.addLayout(audio_grid)

        whole_layout.addWidget(gb_general)
        whole_layout.addWidget(gb_video)
        whole_layout.addWidget(gb_audio)

        self._show_video_info(self.position)

        self.setMinimumSize(whole_layout.minimumSize())

    def _show_video_info(self, position):
        """Show video info on the Info Panel."""
        media_file = self.media_list.get_file(position)
        file_path = media_file.format_info['filename']
        filename = basename(file_path)
        self.label_file_name_value.setText(filename)
        self.label_size_value.setText(
            write_size(media_file.format_info['size']))
        self.label_duration_value.setText(
            write_time(media_file.format_info['duration']))
        self.label_format_name_value.setText(
            media_file.format_info['format_name'])
        self.label_format_long_name_value.setText(
            media_file.format_info['format_long_name'])

        self.label_codec_name_value.setText(
            media_file.video_stream_info['codec_name'])
        self.label_codec_long_name_value.setText(
            media_file.video_stream_info['codec_long_name'])
        self.label_bit_rate_value.setText(
            media_file.video_stream_info['bit_rate'])
        self.label_width_value.setText(media_file.video_stream_info['width'])
        self.label_height_value.setText(media_file.video_stream_info['height'])
        self.label_acodec_name_value.setText(
            media_file.audio_stream_info['codec_name'])
        self.label_acodec_long_name_value.setText(
            media_file.audio_stream_info['codec_long_name'])


from unittest.mock import MagicMock

import pytest
from click.testing import CliRunner

from soocii_services_lib.click import CiTools, build_soocii_cli, bash

class TestCiTools:
    def test_command_call_methods(self):
        """Test if soocii_cli call CiTools' methods"""
        runner = CliRunner()

        tools = CiTools('test')

        test_methods = ['docker_login', 'build', 'build_and_push', 'deploy_to_integ']
        for m in test_methods:
            setattr(tools, m, MagicMock(name=m))

        cli = build_soocii_cli(tools)
        runner.invoke(cli, [m.replace('_', '-') for m in test_methods])

        for m in test_methods:
            getattr(tools, m).assert_called_with()

def test_build_soocii_cli_instance_type_check():
    with pytest.raises(ValueError):
        non_ci_tools_instance = list()
        build_soocii_cli(non_ci_tools_instance)

def test_bash_return_exit_code():
    assert 0 == bash('true')
    assert 1 == bash('false')


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

from pathlib import Path
from typing import Dict

from scripts.preprocess_text import read_text, write_text, OneLineString, AlphaNumericString, calc_occurrences
from scripts.needleman_wunsch import InDelSymbols

def create_oneline_alns(out_dir: Path) -> None:
    for aln_filename in Path("data/7_aligned").rglob("*.txt"):
        ref, query = read_text(aln_filename).split("\n")[0:2]
        query_corrected = []
        for i_char in range(len(ref)):
            if query[i_char] != InDelSymbols.ins and ref[i_char] != InDelSymbols.delet:
                query_corrected.append(ref[i_char])

        write_text(out_dir / aln_filename.name, AlphaNumericString(OneLineString("".join(query_corrected))).text)

def read_csv_stats(stats_file_name: Path) -> Dict[str, float]:
    result = {}
    with open(str(stats_file_name)) as csvfile:
        csvreader = csv.reader(csvfile)
        for row in csvreader:
            result[row[0].strip()] = float(row[1].strip())
    return result

def main():
    vocab_dir = Path("data/ref/4_vocabulary")
    aligned_queries_dir = Path("data/8_aligned_oneline")
    aligned_queries_dir.mkdir(parents=True, exist_ok=True)
    create_oneline_alns(out_dir=aligned_queries_dir)
    calc_occurrences(queries_dir=aligned_queries_dir, vocab_dir=vocab_dir,
                     file_prefixes=("scarlet_letter", "jane_eyre"), out_file_postfix=".aligned")
    freq_before = read_csv_stats(vocab_dir / "word_freq_stats.csv").values()
    freq_after = read_csv_stats(vocab_dir / "word_freq_stats.aligned.csv").values()

    fig, axs = plt.subplots(2)
    fig.suptitle("frequency of occurrence of words in the dictionary")
    for i_plot, (freq, title) in enumerate(((freq_before, "before correction"), (freq_after, "after correction"))):
        axs[i_plot].violinplot(freq, showmedians=True, showextrema=False, vert=False)
        axs[i_plot].scatter(freq, [1 for _ in range(len(freq))], c="black", s=5)
        axs[i_plot].set(ylabel=title, yticks=[])
        print("mean of frequencies ({}): {}".format(title, np.mean(list(freq))))
    plt.yticks([])
    plt.savefig(str(vocab_dir / "frequencies.png"), dpi=1000)

if __name__ == "__main__":
    main()


#!/usr/bin/env python3

import threading
import argparse
import socket
import json
import sys
import pprint

class Router:
    port = 55151        # porta padrão
    sock = None         # socket UDP
    running = None      # indica se roteador está executando
    linkTable = {}      # tabela de links
    routingTable = {}   # tabela de rotas

    host = ''           # endereço de IP do roteador
    tout = 0            # período de alterações
    updateTimer = None  # timer para envio de mensagens de update

    # inicializa o roteador
    def __init__(self, args):
        self.host = args.addr
        self.tout = args.timeout

        try:
            # cria socket UDP
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            # associa o socket ao endereço de IP
            self.sock.bind((self.host, self.port))
            # coloca o socket em modo não-blocante
            self.sock.setblocking(False)
        except socket.error as error_msg:
            self.logExit(error_msg)

        # utiliza configuração do arquivo se este foi especificado
        if args.startup:
            self.startupFile(args.startup)

    # extrai configs do arquivo
    def startupFile(self, filename):
        with open(filename, 'r') as fp:
            lines = fp.read().splitlines()

        for ln in lines:
            self.parseLinkCommand(ln)

    # inicia execução do roteador
    def start(self):
        # cria thread que recebe comandos do prompt
        cli = threading.Thread(target=self.cliThread)
        # cria thread que espera por mensagens
        recvMsg = threading.Thread(target=self.recvThread)
        # cria evento que indica que roteador está executando
        self.running = threading.Event()

        # inicia threads
        self.running.set()
        cli.start()
        recvMsg.start()

        # inicia o primeiro temporizador para enviar mensagens de update
        self.updateTimer = self.setTimer(self.sendUpdate)
        self.updateTimer.start()

    # extraí informações sobre vizinhos
    def parseLinkCommand(self, cmd):
        cmd = cmd.split(' ')
        cmd = [x for x in cmd if len(x) > 0]

        if cmd[0] == 'add':
            if len(cmd) != 3:
                print('Comando inválido')
            elif cmd[1] not in self.linkTable:
                self.addLink(cmd[1], cmd[2])
            else:
                print('Vizinho já existe')

        elif cmd[0] == 'del':
            if len(cmd) != 2:
                print('Comando inválido')
            elif cmd[1] in self.linkTable:
                self.rmvLink(cmd[1])
            else:
                print('Vizinho não existe')

        elif cmd[0] == 'trace':
            self.sendTrace(cmd[1])

        elif cmd[0] == 'quit':
            self.running.clear()
            self.updateTimer.cancel()

        elif cmd[0] == 'log':
            print('links -->',end=" ")
            pprint.pprint(self.linkTable)
            print('rotas -->',end=" ")
            pprint.pprint(self.routingTable)

        else:
            print('Comando inválido')

    # adiciona novo vizinho à tabela de links
    def addLink(self, ip, weight):
        self.linkTable[ip] = {}
        self.linkTable[ip]['weight'] = int(weight)
        self.linkTable[ip]['ttl'] = 4

        self.addRoute(ip, ip, weight)

    # remove vizinho
    def rmvLink(self, ip):
        print('rmvLink deleted node', ip)
        del self.linkTable[ip]

        # testa se o ip está na tabela como destino
        if ip in self.routingTable:
            del self.routingTable[ip]

        # remove todos os caminhos que passavam por esse ip
        self.rmvRoute(ip)
        self.sendUpdate()

    # adiciona um caminho à tabela de roteamento
    def addRoute(self, ip, hop, weight):
        self.routingTable[ip] = {}
        self.routingTable[ip]['hops'] = []
        self.routingTable[ip]['hops'].append(hop)

        self.routingTable[ip]['weight'] = int(weight)
        if ip != hop:
            self.routingTable[ip]['weight'] += self.linkTable[hop]['weight']

        self.routingTable[ip]['nextHop'] = 0

    # remove um caminho
    def rmvRoute(self, ip, safe=[]):
        change = False
        # remove todos os caminhos que eram possíveis via ip
        ips = []
        for i in self.routingTable:
            # testa se o gateway pode ser removido daquele ip
            if i not in safe:
                change = True
                if ip in self.routingTable[i]['hops']:
                    self.routingTable[i]['hops'].remove(ip)

            # caminho inviável
            if len(self.routingTable[i]['hops']) == 0:
                ips.append(i)

        # remove os nodos inalcançáveis
        for i in range(len(ips)):
            print('rmvRoute deleted node', ip)
            del self.routingTable[ips[i]]

        return change

    # atualiza um caminho existente
    def updateRoute(self, ip, hop, weight):
        weight += self.linkTable[hop]['weight']
        change = False

        # se custo é menor, substitui
        if self.routingTable[ip]['weight'] > weight:
            print('updateRoute deleted node', ip)
            change = True
            del self.routingTable[ip]
            self.addRoute(ip, hop, weight)
        # se custo é maior, ignora
        elif self.routingTable[ip]['weight'] < weight:
            return change
        # se custo é igual, adiciona gateway
        else:
            if hop not in self.routingTable[ip]['hops']:
                change = True
                self.routingTable[ip]['hops'].append(hop)

        return change

    # constroi mensagens
    def buildMessage(self, tp, src, dest, pl=None, dist=None):
        msg = {
            'type': tp,
            'source': src,
            'destination': dest
        }

        if tp == 'data':
            msg.update({'payload': pl})
        elif tp == 'update':
            msg.update({'distances': dist})
        elif tp == 'trace':
            msg['hops'] = []
            msg['hops'].append(self.host)

        return msg

    # envia mensagem de update
    def sendUpdate(self):
        updMsg = None
        for ip in self.linkTable:
            distances = self.buildDistanceDict(ip)
            updMsg = self.buildMessage('update', self.host, ip, dist=distances)
            debugPrint = updMsg
            updMsg = json.dumps(updMsg)
            pkg = bytes(updMsg, 'ascii')
            if updMsg != None:
                print("Update sent: ", end='')
                pprint.pprint(debugPrint)
            self.sock.sendto(pkg, (ip, self.port))

        self.updateTimer.cancel()
        self.updateTimer = self.setTimer(self.sendUpdate)
        self.updateTimer.start()

        # diminui o ttl dos vizinhos
        self.controlLinks()

    # constroi dicionário de distâncias, usando split horizon
    def buildDistanceDict(self, ip):
        dist = {}

        for key in self.routingTable:
            if ip != key:
                if len(self.routingTable[key]['hops']) > 1 or ip not in self.routingTable[key]['hops']:
                    dist.update({key: self.routingTable[key]['weight']})

        return dist

    # encaminha uma mensagem fazendo balanceamento de carga
    def forwardMessage(self, msg):
        # separa o ip do destinatário
        ip = msg['destination']
        # acha o próximo hop usando balanceamento de cargas
        hop = self.findNextHop(ip)

        # envia mensagem
        msg = json.dumps(msg)
        pkg = bytes(msg, 'ascii')
        self.sock.sendto(pkg, (hop, self.port))

    # thread que controla recebimento de comandos via teclado
    def cliThread(self):
        while self.running.isSet():
            line = input('~ ')
            self.parseLinkCommand(line)

    # trata as mensagens recebidas
    def parseMessage(self, data, sourceAddr):
        msg = json.loads(data.decode())
        if msg['type'] == 'update':
            print("Update received: ", end='')
            pprint.pprint(msg)
            # Pega os IPs e pesos do vetor distances da msg de update
            for ip, weight in msg['distances'].items():
                # Já está na tabela de roteamento
                if ip in self.routingTable:
                    # Ve se o link existe na tabela
                    if sourceAddr[0] in self.linkTable:
                        currentWeight = int(self.linkTable[sourceAddr[0]]['weight'])
                        knownWeight = self.routingTable[ip]['weight']
                        if (weight + currentWeight) < knownWeight:
                            self.routingTable[ip]['weight'] = int(weight) + currentWeight
                            self.routingTable[ip]['hops'] = []
                            self.routingTable[ip]['hops'].append([sourceAddr[0]]['weight'])
                        elif (weight + currentWeight) == knownWeight:
                            hopIPs = []
                            for hopIP in self.routingTable[ip]['hops']:
                                hopIPs.append(hopIP)
                            if sourceAddr[0] not in hopIPs:
                                self.routingTable[ip]['hops'].append(sourceAddr[0])
                # Não está na tabela de roteamento
                else:
                    self.routingTable[ip] = {}
                    self.routingTable[ip]['weight'] = int(weight) + int(self.linkTable[sourceAddr[0]]['weight'])
                    self.routingTable[ip]['hops'] = []
                    self.routingTable[ip]['hops'].append(sourceAddr[0])
                    self.routingTable[ip]['nextHop'] = 0

            #Reseta TTL
            print("Atualizou o TTL de:", sourceAddr[0])
            self.linkTable[sourceAddr[0]]['ttl'] = 4

        elif msg['type'] == 'data':
            if msg['destination'] == self.host:
                # Esse nó é o destino, mensagem recebida.
                print("Mensagem recebida:", msg['payload'])
            else:
                self.forwardMessage(msg) #? Não era simplesmene enviar pro nextHop como mensagem comum?
        elif msg['type'] == 'trace':
            # Esse nó é o destino do trace
            if msg['destination'] == self.host:
                traceBack = {'type': 'data', 'source': self.host, 'destination': msg['source'], 'payload': msg}
                self.forwardMessage(traceBack)
            # Esse não é o nó destino, encaminha o trace
            else:
                msg['hops'].append(self.host)
                self.forwardMessage(msg)

        elif msg['type'] == 'trace':
            # esse nó é o destino do trace
            if msg['destination'] == self.host:

                traceBack = self.buildMessage('data', self.host, msg['source'], json.dumps(msg))
                self.forwardMessage(traceBack)
            # esse não é o nó destino, encaminha o trace
            else:
                msg['hops'].append(self.host)
                self.forwardMessage(msg)

    # envia mensagem de trace
    def sendTrace(self, ip):
        update = self.buildMessage('trace', self.host, ip)
        self.sock.sendto(json.dumps(update).encode(), (self.findNextHop(ip), self.port))

    # faz o balanceamento de cargas
    def findNextHop(self, dest):
        # separa por qual gateway deve passar a mensagem
        nextHop = self.routingTable[dest]['nextHop']
        # separa o gateway pelo qual essa mensagem vai passar
        hop = self.routingTable[dest]['hops'][nextHop]

        # incrementa o hop para o balanceamento de cargas
        self.routingTable[dest]['nextHop'] += 1
        # se passou o número de gateways da lista, reseta contador
        if self.routingTable[dest]['nextHop'] > len(self.routingTable[dest]['hops']) - 1:
            self.routingTable[dest]['nextHop'] = 0

        return hop

    # thread que controla recebimento de mensagens de update
    def recvThread(self):
        while self.running.isSet():
            try:
                data, sourceAddr = self.sock.recvfrom(1024)
                self.parseMessage(data, sourceAddr)
            except BlockingIOError:
                pass

    # controla os vizinhos baseado no ttl
    def controlLinks(self):
        # diminui o ttl de cada vizinho
        rmv = []
        for ip in self.linkTable:
            self.linkTable[ip]['ttl'] -= 1

            if self.linkTable[ip]['ttl'] == 0:
                rmv.append(ip)

        # remove vizinhos que estouraram o ttl
        for ip in rmv:
            self.rmvLink(ip)

    # retorna um objeto Timer
    def setTimer(self, fn, argList=[], multiplier=1):
        timer = threading.Timer(self.tout * multiplier, fn, argList)
        return timer

    # tratamento de erros
    def logExit(self, error_msg):
        self.sock.close()
        sys.exit(error_msg)

def main():
    # recebe parâmetros por linha de comando
    parser = argparse.ArgumentParser('Parâmetros do roteador')
    parser.add_argument('--addr',
                        dest='addr',
                        type=str,
                        required=True,
                        help='IP do roteador'
                        )
    parser.add_argument('--update-period',
                        dest='timeout',
                        type=float,
                        required=True,
                        help='Período entre envio de mensagens'
                        )
    parser.add_argument('--startup-commands',
                        dest='startup',
                        type=str,
                        required=False,
                        help='Arquivo de comandos'
                        )
    args = parser.parse_args()

    # configura e inicia roteador
    router = Router(args)
    router.start()

if __name__ == '__main__':
    main()


"""
Collect metrics with low memory requirements (avoid collecting superfluous data)
"""
from typing import Any
from ray.util.iter import LocalIterator
from ray.rllib.evaluation.metrics import collect_episodes, summarize_episodes
from ray.rllib.execution.common import STEPS_SAMPLED_COUNTER
from ray.rllib.evaluation.worker_set import WorkerSet
from ray.rllib.execution.metric_ops import OncePerTimeInterval
from ray.tune.integration.wandb import wandb_mixin

def LowMemoryMetricsReporting(train_op: LocalIterator[Any], workers: WorkerSet,
                             config: dict) -> LocalIterator[dict]:
    output_op = train_op \
        .filter(OncePerTimeInterval(max(2, config["min_iter_time_s"]))) \
        .for_each(CollectMetricsLowMem(
            workers, min_history=config["metrics_smoothing_episodes"],
            timeout_seconds=config["collect_metrics_timeout"]))
    return output_op

class CollectMetricsLowMem:
    def __init__(self, workers, min_history=100, timeout_seconds=180, log_to_neptune=False):
        self.workers = workers
        self.episode_history = []
        self.to_be_collected = []
        self.min_history = min_history
        self.timeout_seconds = timeout_seconds
        self.log_to_neptune = log_to_neptune

    def __call__(self, _):
        # Collect worker metrics.
        episodes, self.to_be_collected = collect_episodes(
            self.workers.local_worker(),
            self.workers.remote_workers(),
            self.to_be_collected,
            timeout_seconds=self.timeout_seconds)
        orig_episodes = list(episodes)
        missing = self.min_history - len(episodes)
        if missing > 0:
            episodes.extend(self.episode_history[-missing:])
            assert len(episodes) <= self.min_history
        self.episode_history.extend(orig_episodes)
        self.episode_history = self.episode_history[-self.min_history:]
        res = summarize_episodes(episodes, orig_episodes)

        # Add in iterator metrics.
        metrics = LocalIterator.get_metrics()
        timers = {}
        counters = {}
        info = {}
        info.update(metrics.info)
        for k, counter in metrics.counters.items():
            counters[k] = counter
        for k, timer in metrics.timers.items():
            timers["{}_time_ms".format(k)] = round(timer.mean * 1000, 3)
            if timer.has_units_processed():
                timers["{}_throughput".format(k)] = round(
                    timer.mean_throughput, 3)
        res.update({
            "num_healthy_workers": len(self.workers.remote_workers()),
            "timesteps_total": metrics.counters[STEPS_SAMPLED_COUNTER],
        })
        res["timers"] = timers
        res["info"] = info
        res["info"].update(counters)
        relevant = ["info", "custom_metrics", "sampler_perf", "timesteps_total", "policy_reward_mean",
                    "episode_len_mean"]

        d = {
            k: res[k] for k in relevant
        }
        d["evaluation"] = res.get("evaluation", {})

        if self.log_to_neptune:
            metrics_to_be_logged = ["info", "evaluation"]
            def log_metric(metrics, base_string=''):
                    if isinstance(metrics, dict):
                        for k in metrics:
                            log_metric(metrics[k], base_string + '{}_'.format(k))
                    else:
                        neptune.log_metric(base_string, metrics)
            for k in d:
                if k in metrics_to_be_logged:
                    log_metric(d[k], base_string='{}_'.format(k))

        return d


# This script close the focused window only if
# the process name is in the list below.
PROCESS = [	'pavucontrol',
						'blueman-manager',
						'arandr',
						'qalculate-gtk',
						'feh']

import subprocess
import os

if __name__ == "__main__":
	output = subprocess.check_output("xprop -id $(xdotool getwindowfocus) WM_CLASS", shell=True)
	array = output.split('\"')
	if array[1] in PROCESS:
		os.system("pkill " + array[1])


# -*- coding=UTF-8 -*-
# pyright: strict

from __future__ import annotations

if True:
    import os
    import sys

    sys.path.insert(0, os.path.join(__file__, "../../.."))

import datetime
import io
import json
import logging
import random
import threading
import time
import urllib.parse
import webbrowser
from typing import Tuple

from PIL import Image

from auto_derby import web

def _ts():
    return datetime.datetime.now().astimezone().isoformat()

def _random_level():
    return random.choice(("DEBUG", "INFO", "WARN", "ERROR"))

def _sample_text():
    return {
        "ts": _ts(),
        "lv": _random_level(),
        "t": "TEXT",
        "source": "page_log_example",
        "msg": "this is a text message",
    }

def _sample_screenshot():
    return {
        "ts": _ts(),
        "lv": _random_level(),
        "t": "IMAGE",
        "source": "page_log_example",
        "url": "/files/screenshot.png",
        "caption": "this is a screenshot image ",
    }

def _sample_small_image():
    return {
        "ts": _ts(),
        "lv": _random_level(),
        "t": "IMAGE",
        "source": "page_log_example",
        "url": "/files/small.png",
        "caption": "this is a small image",
    }

def _sample_internet_image():
    return {
        "ts": _ts(),
        "lv": _random_level(),
        "t": "IMAGE",
        "source": "page_log_example",
        "url": "https://httpbin.org/image/png",
        "caption": "this is a internet image",
    }

def _sample_not_saved_image():
    _image_placeholder_svg_template = """\
<svg width="540" height="200" version="1.1" viewBox="0 0 143 53" xmlns="http://www.w3.org/2000/svg">
 <rect width="100%" height="100%" fill="#fff"/>
 <text font-family="sans-serif" font-size="10px"><tspan x="32" y="15">Image not saved</tspan></text>
 <text x="71" y="33" font-family="serif" font-size="5px" text-anchor="middle"><tspan x="71.4" y="33.1">reason:</tspan><tspan x="71.4" y="37.8">image save path not set</tspan><tspan x="71.4" y="42.6">and size greater than inline limit</tspan></text>
 <text x="42" y="25" font-family="sans-serif" font-size="6px"><tspan x="43.5" y="25.9">resolution={width}x{height}</tspan></text>
</svg>
"""
    svg = _image_placeholder_svg_template.format(
        width=540,
        height=950,
    )
    url = f"data:image/svg+xml,{urllib.parse.quote(svg)}"

    return {
        "ts": _ts(),
        "lv": _random_level(),
        "t": "IMAGE",
        "source": "page_log_example",
        "url": url,
        "caption": "this is a placeholder for not saved image",
    }

def _sample_small_layered_image():
    return {
        "ts": _ts(),
        "lv": _random_level(),
        "t": "IMAGE",
        "source": "page_log_example",
        "url": "/files/small.png",
        "layers": [
            {"name": "red", "url": "/files/small.red.png"},
            {"name": "green", "url": "/files/small.green.png"},
            {"name": "blue", "url": "/files/small.blue.png"},
        ],
        "caption": "this is a small image with layers",
    }

def _sample_large_layered_image():
    return {
        "ts": _ts(),
        "lv": _random_level(),
        "t": "IMAGE",
        "source": "page_log_example",
        "url": "/files/large.png",
        "layers": [
            {"name": "red", "url": "/files/large.red.png"},
            {"name": "green", "url": "/files/large.green.png"},
            {"name": "blue", "url": "/files/large.blue.png"},
        ],
        "caption": "this is a large image with layers",
    }

def _sample_inconsistent_layer_size_image():
    return {
        "ts": _ts(),
        "lv": _random_level(),
        "t": "IMAGE",
        "source": "page_log_example",
        "url": "/files/large.png",
        "layers": [
            {"name": "red-small", "url": "/files/small.red.png"},
            {"name": "green-large", "url": "/files/large.green.png"},
            {"name": "blue-small", "url": "/files/small.blue.png"},
        ],
        "caption": "this is a image has inconsistent layer size",
    }

def _constant(size: Tuple[int, int], color: Tuple[int, int, int]):
    b = io.BytesIO()
    Image.new("RGB", size, color).save(b, "PNG")
    return web.Blob(b.getvalue(), "image/png")

if __name__ == "__main__":

    logging.basicConfig(level=logging.INFO)
    samples = [
        *([_sample_text] * 10),
        *([_sample_small_image] * 10),
        *[
            _sample_screenshot,
            _sample_not_saved_image,
            _sample_internet_image,
            _sample_screenshot,
            _sample_small_layered_image,
            _sample_large_layered_image,
            _sample_inconsistent_layer_size_image,
        ],
    ]
    stream = web.Stream("", "text/plain")

    stop = threading.Event()

    def _run():
        with web.create_server(
            ("127.0.0.1", 8300),
            web.Blob(
                web.page.render(
                    {
                        "type": "LOG",
                        "streamURL": "/log",
                    }
                ).encode("utf-8"),
                "text/html; charset=utf-8",
            ),
            web.page.ASSETS,
            web.Path("/log", stream),
            web.Path(
                "/files/small.png",
                _constant((150, 16), (255, 255, 255)),
            ),
            web.Path(
                "/files/small.red.png",
                _constant((150, 16), (255, 0, 0)),
            ),
            web.Path(
                "/files/small.green.png",
                _constant((150, 16), (0, 255, 0)),
            ),
            web.Path(
                "/files/small.blue.png",
                _constant((150, 16), (0, 0, 255)),
            ),
            web.Path(
                "/files/screenshot.png",
                _constant((540, 960), (0, 255, 0)),
            ),
            web.Path(
                "/files/large.png",
                _constant((540, 960), (255, 255, 255)),
            ),
            web.Path(
                "/files/large.red.png",
                _constant((540, 960), (255, 0, 0)),
            ),
            web.Path(
                "/files/large.green.png",
                _constant((540, 960), (0, 255, 0)),
            ),
            web.Path(
                "/files/large.blue.png",
                _constant((540, 960), (0, 0, 255)),
            ),
            web.Route("/dir/", web.Dir(os.path.dirname(__file__))),
            web.middleware.Debug(),
        ) as httpd:

            def _cleanup():
                stop.wait()
                stream.close()
                httpd.shutdown()

            threading.Thread(target=_cleanup).start()
            host, port = httpd.server_address
            url = f"http://{host}:{port}"
            print(f"run at: {url}\npress Ctrl+C to stop")
            webbrowser.open(url)
            httpd.serve_forever()

    threading.Thread(target=_run).start()
    try:
        while True:
            # if not random.randint(0, 20):
            #     stream.write("bad value\n".encode("utf-8"))
            sample = json.dumps(random.choice(samples)())
            stream.write((sample + "\n").encode("utf-8"))
            time.sleep(0.5)
    finally:
        stop.set()


# Author: Aleksander Robnik
# Project Euler: Problem 9 - Special Pythagorean triplet
import math

def SPT(n):
    tripledProd = 0

    for i in range(1, n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
                if math.pow(i, 2) + math.pow(j, 2) == math.pow(k, 2):
                    if i + j + k == n:
                        print(i, j, k)
                        tripledProd = i * j * k
    return tripledProd

def main():
    print(SPT(1000))

if __name__ == "__main__":
    main()


from collections import defaultdict
import csv
corpus = []
labels = []

filenames = []
with open('F:/Rafi/My_Study/4_1/AI_Lab/Python_Codes/Version1/pos2.txt', 'r') as infile:
    for line in infile:
        filenames.append(line.rstrip())
doc = ""
for filename in filenames:
    s = "F:/Rafi/My_Study/4_1/AI_Lab/aclImdb/train/pos/" + filename
    with open(s, 'r') as infile2:
        for line in infile2:
            doc += line
        corpus.append(doc)
        labels.append(1)
        doc = ""
print(len(corpus))
doc = ""
filenames = []
with open('F:/Rafi/My_Study/4_1/AI_Lab/Python_Codes/Version1/neg2.txt', 'r') as infile:
    for line in infile:
        filenames.append(line.rstrip())
doc = ""
for filename in filenames:
    s = "F:/Rafi/My_Study/4_1/AI_Lab/aclImdb/train/neg/" + filename
    with open(s, 'r') as infile2:
        for line in infile2:
            doc += line
        corpus.append(doc)
        labels.append(0)
        doc = ""
print(len(corpus))
print(len(labels))

filenames = []
with open('F:/Rafi/My_Study/4_1/AI_Lab/aclImdb/test/mixed/posnfull.txt', 'r') as infile:
    for line in infile:
        filenames.append(line.rstrip())

filenamesneg = []
with open('F:/Rafi/My_Study/4_1/AI_Lab/aclImdb/test/mixed/negnfull.txt', 'r') as infile:
    for line in infile:
        filenamesneg.append(line.rstrip())

from sklearn import svm
from sklearn.metrics import accuracy_score
import pickle
from sklearn.externals import joblib
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.datasets import load_files
from sklearn.naive_bayes import MultinomialNB
from sklearn.cross_validation import train_test_split
from sklearn import metrics
from sklearn.feature_extraction.text import TfidfVectorizer
tf = TfidfVectorizer(analyzer='word', ngram_range=(1,2), min_df = 0, max_features = 100000, stop_words = 'english')
tfidf_matrix = tf.fit_transform(corpus)
print(tfidf_matrix.shape)
training = tfidf_matrix[:3400]
l_train = labels[:3400]
joblib.dump(tf, 'vectorizer.pkl')


import streamlit as st
import numpy as np
import pandas as pd
from Predict import *
import GatherData as gd
st.set_option('deprecation.showPyplotGlobalUse', False)

mission = ['TESS', 'Kepler']
st.title('Exoplanet Hunter')
st.header('A Tool to analyze/validate Exoplanet Data')

st.sidebar.title('Choose a starting point')
selected_mission = st.sidebar.selectbox('',mission)
DataObject = gd.GatherData(mission=selected_mission)
input_ids = DataObject.IdData['WrapperId']

selected_id = st.sidebar.selectbox(f'Available IDs for {selected_mission}',
input_ids
)

#Predict(mission = 'Kepler', ID=10811496)
if st.sidebar.button('Run ExoMiner Raw Model'):
    try:
        prediction = Predict(selected_mission, selected_id)
        formatted_prediction = f"
 Model output:   {prediction}
"

        st.markdown(formatted_prediction, unsafe_allow_html=True)
        #st.write(Predict(mission = 'Kepler', ID=10811496))

        import matplotlib.pyplot as plt
        import numpy as np
        fig, ax = plt.subplots()
        st.pyplot(gd.KeplerTest(selected_id))
    except:
        st.warning('Unable to fetch/process data for the selected ID, please select another ID.', icon="⚠")


#Setting the requirements
from flask import Flask, request, jsonify
from WatsonAssistant.watson_assistant import create_assistant_session, ask_assistant_question
from WatsonAssistant.helpers import get_response_text

app = Flask(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False

@app.route('/session', methods=['GET'])
def create_session_route():
    """This function call the function that in watson_assistant.py to create a session_id.
     return the session_id """
    return create_assistant_session()

@app.route('/ask', methods=['POST'])
def ask_question_route():
    """This function call the function that is watson_assistant.py to send ask WA a question."""
    return ask_assistant_question()

if __name__ == '__main__':
    app.run(port=3000)


# coding: utf-8

"""
Data Files Summary panel
------------------------

Implements panel with data files summary.
"""

import os.path as osp

from PyQt5 import Qt as Q

from ...common import (bold, common_filters, external_file, external_files,
                       get_file_name, is_reference, load_icon, preformat,
                       translate)
from ...datamodel import FileAttr, synchronize
from .. import Context, NodeType, check_selection
from ..actions import OpenWithAction, OnFileAction
from ..controller import Controller
from ..widgets import TitleWidget
from .objects import File

__all__ = ["DataFilesSummary"]

# note: the following pragma is added to prevent pylint complaining
#       about functions that follow Qt naming conventions;
#       it should go after all global functions
# pragma pylint: disable=invalid-name

UnitRole = Q.Qt.UserRole
StageRole = Q.Qt.UserRole + 1
FilenameRole = Q.Qt.UserRole + 2
SortRole = Q.Qt.UserRole + 3

class DataFilesSummary(Q.QWidget):
    """
    Widget displaying unified list of files from all stages in current case.

    Through the context menu, user can open that file in an app or go to its directory.
    Additionally, when listing files for Current Case, user can update a file's path.
    """

    Edit = 1
    OpenWith = 2
    BrowseDirectory = 3
    PostProcess = 4

    def __init__(self, astergui, parent):
        """
        Create view.

        Arguments:
            astergui (asterstudy.gui.astergui.AsterGui): *AsterGui* instance.
            parent (Q.QWidget): Parent widget.
        """
        super().__init__(parent)
        self.setObjectName('DataFilesSummary')

        self._astergui = astergui
        self._actions = {}

        # initialize layout
        layout = Q.QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        # panel title
        layout.addWidget(TitleWidget(translate("DataFiles", "Data Files Summary"), self))

        # files model
        self._model = _FilesModel(0, 4)
        proxy_model = Q.QSortFilterProxyModel()
        proxy_model.setSourceModel(self._model)
        proxy_model.setSortRole(SortRole)

        # files view
        self._tree = Q.QTreeView(self)
        self._tree.setObjectName('DataFilesSummary_Tree')
        self._tree.setModel(proxy_model)
        self._tree.setRootIsDecorated(False)
        self._tree.setSortingEnabled(True)
        self._tree.setEditTriggers(Q.QAbstractItemView.NoEditTriggers)
        self._tree.setItemDelegate(_EditingDelegate(self._tree, self._astergui))
        self._tree.setContextMenuPolicy(Q.Qt.CustomContextMenu)
        self._tree.doubleClicked.connect(self._edit)
        self._tree.customContextMenuRequested.connect(self._show_context_menu)
        self._tree.header().setSectionsMovable(False)
        self._tree.header().setSectionResizeMode(Q.QHeaderView.ResizeToContents)
        layout.addWidget(self._tree)

        # actions
        # - <Edit>
        action = Q.QAction(translate("DataFiles", "Edit..."), self)
        action.setIcon(load_icon('as_pic_edit_file.png'))
        action.setToolTip(translate("DataFiles", "Edit"))
        action.setStatusTip(translate("DataFiles", "Edit file path"))
        action.setShortcut('F4')
        action.setShortcutContext(Q.Qt.WidgetWithChildrenShortcut)
        action.triggered.connect(self._edit)
        action.setEnabled(False)
        self._actions[self.Edit] = action
        # - <Open With>
        action = OpenWithAction(self, self._astergui.main_window)
        action.setEnabled(False)
        self._actions[self.OpenWith] = action
        # - post-process in the results tab
        action = OnFileAction(translate("AsterStudy", "Post-process"),
                              self,
                              self._astergui.showResultsFile)
        action.setToolTip(translate("AsterStudy", "Post-process"))
        action.setStatusTip(translate("AsterStudy",
                                      "Post-process the result the Results tab"))
        action.setIcon(load_icon("as_pic_open_in_paravis.png"))
        self._actions[self.PostProcess] = action
        # - <Browse Directory>
        action = Q.QAction(translate("DataFiles", "Browse Directory"), self)
        action.setToolTip(translate("DataFiles", "Browse directory"))
        action.setStatusTip(translate("DataFiles", "Show selected file in the explorer"))
        action.triggered.connect(self._browse_directory)
        action.setEnabled(False)
        self._actions[self.BrowseDirectory] = action
        # add actions to this widget to access their via shortcuts
        self.addActions(self._actions.values())

    def setSelection(self, objs):
        """
        Select given objects.

        Arguments:
            objs (list[Entity]): Objects being selected.
        """
        # If user has selected exactly one case, we should show file list for this case.
        # Otherwise, we should show nothing.
        # This function is called from ``Workspace.setSelected()`` or ``update()``.

        if check_selection(objs, size=1, typeid=NodeType.Case):
            study = self._astergui.study()
            case = study.node(objs[0])

            # enable / disable actions
            is_editable = (case is study.history.current_case)
            self._actions[self.Edit].setEnabled(is_editable)
            self._actions[self.OpenWith].setEnabled(is_editable)
            self._actions[self.PostProcess].setEnabled(True)
            self._actions[self.BrowseDirectory].setEnabled(True)

            # intialize create temporary files model from case
            tmp_model = Q.QStandardItemModel(0, 1)
            for stage in case.stages:
                for unit in stage.handle2info:
                    item = DataFileItem(stage, unit)
                    item.setEditable(is_editable)
                    tmp_model.appendRow(item)

            # update files model by syncing it with temporary model
            synchronize(
                tmp_model.invisibleRootItem(),
                self._model.invisibleRootItem(),
                _DataFilesSummaryTreeData())

            # re-sort files view
            self._tree.sortByColumn(1, Q.Qt.AscendingOrder)

        else:
            # clear files model
            self._model.clear()

    @Q.pyqtSlot()
    def update(self):
        """
        Update view.
        """
        # just call `setSelection()` with current selection
        self.setSelection(self._astergui.view(Context.Cases).selection())

    @Q.pyqtSlot(Q.QPoint)
    def _show_context_menu(self, pos):
        """
        Show context menu for a file.

        Arguments:
            pos (QPoint): Cursor position.
        """
        index = self._tree.indexAt(pos)  # type: Q.QModelIndex
        if index.isValid():
            index = index.sibling(index.row(), 1) # get model index for second column
            file_name = index.data(FilenameRole) # type: str

            menu = Q.QMenu(self)

            menu.addAction(self._actions[self.Edit])
            menu.setDefaultAction(self._actions[self.Edit])
            menu.addSeparator()

            if file_name:
                self._actions[self.OpenWith].setFileName(file_name)
                self._actions[self.PostProcess].setFileName(file_name)
                menu.addAction(self._actions[self.OpenWith])
                menu.addAction(self._actions[self.PostProcess])
                menu.addAction(self._actions[self.BrowseDirectory])

            menu.popup(self._tree.viewport().mapToGlobal(pos))

    @Q.pyqtSlot()
    def _edit(self):
        """Open editor for selection file item."""
        index = self._tree.currentIndex()  # type: Q.QModelIndex
        index = index.sibling(index.row(), 1) # get model index for second column
        if index.flags() & Q.Qt.ItemIsEditable:
            self._tree.edit(index) # activate in-place editing

    @Q.pyqtSlot()
    def _browse_directory(self):
        """
        Launch file manager on the selected file's directory.
        """
        index = self._tree.currentIndex()  # type: Q.QModelIndex
        file_name = index.data(FilenameRole) # type: str
        directory = osp.dirname(file_name)
        Q.QDesktopServices.openUrl(Q.QUrl(directory))

class DataFileItem(Q.QStandardItem):
    """
    Item representing a data file.
    """

    def __init__(self, stage=None, unit=None):
        """
        Create item.

        Arguments:
            file (asterstudy.gui.datafiles.objects.File): File helper.
        """
        super().__init__()
        self.stage = stage
        self.unit = unit

    def __eq__(self, other):
        return isinstance(other, DataFileItem) and \
            self.stage.uid == other.stage.uid and self.unit == other.unit

    @classmethod
    def clone(cls, other):
        """Create a copy of this item."""
        item = cls(other.stage, other.unit)
        item.setEditable(other.isEditable())
        return item

    def copy_from(self, other):
        """Update this item from reference one.."""
        self.stage = other.stage
        self.unit = other.unit
        self.setEditable(other.isEditable())
        self.emitDataChanged()

    def data(self, role=Q.Qt.UserRole+1):
        """
        Provide readable filename as item's text, and full details as tooltip.
        """
        info = self.stage.handle2info.get(self.unit)
        path = info is not None and info.filename

        if role in (Q.Qt.DisplayRole, SortRole,):
            return _file_title(path, info and info.embedded)

        if role in (Q.Qt.ToolTipRole,):
            if not path:
                path = "<{}>".format(translate("DataFiles", "undefined"))
            if self.stage and path:
                tooltip = []
                tooltip.append(bold(NodeType.value2str(NodeType.Stage)) + ": " + self.stage.name)
                tooltip.append(bold(NodeType.value2str(NodeType.Unit)) + ": " + path)
                return preformat('<br/>'.join(tooltip))

        if role in (Q.Qt.BackgroundRole,):
            if not path:
                return Q.QBrush(Q.Qt.yellow)

        if role in (Q.Qt.ForegroundRole,):
            if not path:
                return Q.QBrush(Q.Qt.red)

        if role in (Q.Qt.SizeHintRole,):
            fm = Q.QFontMetrics(self.font())
            return Q.QSize(fm.width(self.text()) + 50, 18)

        if role in (UnitRole,): # unit
            return self.unit

        if role in (StageRole,): # stage
            return self.stage

        if role in (FilenameRole,): # filename
            return info and info.filename

        return super().data(role)

class _DataFilesSummaryTreeData:
    """
    Implementation of `tree_data` for `synchronize()`.
    """

    def is_equal(self, src, dst): # pragma pylint: disable=no-self-use
        """
        Arguments:
            src (Q.QStandardItem)
            dst (DataFileItem)

        Returns:
            bool
        """
        if isinstance(src, DataFileItem):
            return src == dst
        return True # for invisible root items

    def create_item(self, src): # pragma pylint: disable=no-self-use
        """
        Arguments:
            src (Q.QStandardItem)

        Returns:
            DataFileItem
        """
        if isinstance(src, DataFileItem):
            return DataFileItem.clone(src)
        return DataFileItem() # should not get here

    def update_item(self, src, dst): # pragma pylint: disable=no-self-use
        """
        Arguments:
            src (DataFileItem)
            dst (DataFileItem)
        """
        if isinstance(src, DataFileItem) and isinstance(dst, DataFileItem):
            dst.copy_from(src)

    def get_src_children(self, src): # pragma pylint: disable=no-self-use
        """
        Arguments:
            src (DataFileItem)

        Returns:
            list[DataFileItem]
        """
        return [src.child(i) for i in range(src.rowCount())]

    def get_dst_children(self, dst): # pragma pylint: disable=no-self-use
        """
        Arguments:
            dst (DataFileItem)

        Returns:
            list[DataFileItem]
        """
        return [dst.child(i, 1) for i in range(dst.rowCount())]

    def replace_dst_children(self, dst, children): # pragma pylint: disable=no-self-use
        """
        Arguments:
            dst (DataFileItem)
            children (list[DataFileItem])
        """
        # Remove items that are not present in new list
        # Also, remember which ones are present
        existing_children = []
        for i in reversed(range(dst.rowCount())):
            child = dst.child(i, 1)
            if child not in children:
                dst.removeRow(i)
            else:
                existing_children.append(child)

        # Add items from new list that weren't present in list before
        for i, child in enumerate(children):
            if child not in existing_children:
                unit = child.unit
                unit_item = Q.QStandardItem(str(unit))
                unit_item.setData(unit, SortRole)
                stage = child.stage
                stage_item = Q.QStandardItem(stage.name)
                stage_item.setData(stage.name, SortRole)
                attr = stage.handle2info[unit].attr if unit in stage.handle2info else FileAttr.No
                attr_item = Q.QStandardItem(FileAttr.value2str(attr))
                attr_item.setData(FileAttr.value2str(attr), SortRole)
                dst.insertRow(i, [unit_item, child, stage_item, attr_item])

class _EditingWidget(Q.QWidget):
    """
    Simple widget for selecting a file.

    User can either select an existing file from combobox or open a dialog for choosing a new one.
    Actual logic is implemented in ``__EditingDelegate``.
    """
    def __init__(self, parent):
        """
        Arguments:
            parent (Q.QWidget)
        """
        super().__init__(parent)
        self.setObjectName('DataFilesSummary_EditingWidget')

        layout = Q.QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        self.setLayout(layout)

        self.combobox = Q.QComboBox(self)
        self.combobox.setObjectName('DataFilesSummary_EditingWidget_ComboBox')
        self.combobox.setFocusPolicy(Q.Qt.NoFocus)
        layout.addWidget(self.combobox, 1)

        self.button = Q.QToolButton(self)
        self.button.setObjectName('DataFilesSummary_EditingWidget_Button')
        self.button.setText('...')
        self.button.setFocusPolicy(Q.Qt.NoFocus)
        layout.addWidget(self.button, 0)

    def keyPressEvent(self, event):
        """Redefined from *QWidget*."""
        if not getattr(self, "in_key_press", False):
            setattr(self, "in_key_press", True)
            Q.QApplication.instance().sendEvent(self.combobox, event)
            delattr(self, "in_key_press")

class _EditingDelegate(Q.QStyledItemDelegate):
    """
    Uses ``_EditingWidget`` for inline editing of a ``File``.
    When user selects a new file (with Open File dialog),
    this delegate adds the file to the datamodel.
    """
    def __init__(self, tree, astergui):
        """
        Arguments:
            tree (Q.QTreeView)
            astergui (asterstudy.gui.astergui.AsterGui)
        """
        super().__init__()
        self._tree = tree
        self._astergui = astergui

    def createEditor(self, parent, option, index): # pylint: disable=unused-argument,no-self-use
        """
        Arguments:
            parent (Q.QWidget)
            option (Q.QStyleOptionViewItem)
            index (Q.QModelIndex)

        Returns:
            _EditingWidget
        """
        stage = index.data(StageRole)  # type: Stage
        case = stage.parent_case

        widget = _EditingWidget(parent)
        widget.button.clicked.connect(self._onButtonClicked)

        model = _ComboModel(widget)

        # Add all current known files
        for stage in case.stages:
            for info in stage.handle2info.values():
                model.addItem(info.filename, _file_title(info.filename, info.embedded))

        # Add all known external files
        for uid in external_files():
            model.addItem(uid, external_file(uid))

        widget.combobox.setModel(model)
        return widget

    def setEditorData(self, widget, index): # pylint: disable=no-self-use
        """
        Arguments:
            widget (_EditingWidget)
            index (Q.QModelIndex)
        """
        stage = index.data(StageRole)  # type: Stage
        unit = index.data(UnitRole)  # type: int
        info = stage.handle2info[unit]
        widget.combobox.attr = info.attr
        for i in range(widget.combobox.count()):
            path = widget.combobox.itemData(i, Q.Qt.ToolTipRole)  # type: str
            if path == info.filename:
                widget.combobox.setCurrentIndex(i)
                break
        else:
            widget.combobox.setCurrentIndex(-1) # undefined filename

    def setModelData(self, widget, model, index): # pylint: disable=no-self-use,unused-argument
        """
        Arguments:
            widget (_EditingWidget)
            model (PyQt5.QtCore.QStandardItemModel.QStandardItemModel)
            index (PyQt5.QtCore.QModelIndex.QModelIndex)
        """
        path = widget.combobox.currentData(Q.Qt.ToolTipRole)  # type: str
        stage = index.data(StageRole)  # type: Stage
        unit = index.data(UnitRole)  # type: int
        info = stage.handle2info[unit]
        study_dir = stage.model.tmpdir

        if path and path != info.filename:
            if self._astergui.prepare_for_new_operation():
                operation = translate("DataFiles", "Change file path")
                ctr = Controller(operation, self._astergui)
                # Update actual file
                is_embedded = not is_reference(path) and osp.dirname(path) == study_dir
                current_file = File(stage, unit)
                current_file.filename = path
                current_file.embedded = is_embedded
                # Commit changes to data model
                ctr.controllerCommit()
                self._astergui.study().commit(operation)
                # Update GUI
                self._astergui.update()

    @Q.pyqtSlot()
    def _onButtonClicked(self):
        """
        Open file dialog and select a new file.
        Put its name to the combobox.
        """
        widget = self.sender().parent()  # type: _EditingWidget
        path = widget.combobox.currentData(Q.Qt.ToolTipRole)  # type: str
        attr = widget.combobox.attr  # type: str

        # Show file selection dialog
        title = translate("DataFiles", "Select File")
        file_name = path if not is_reference(path) else None
        mode = 0 if (attr & ~FileAttr.Named) == 2 else 1
        filters = common_filters()
        file_name = get_file_name(mode, widget, title, file_name, filters)

        if file_name:
            # Add an item to combo box if necessary
            widget.combobox.model().addItem(file_name)
            # Select an item in combo box
            for i in range(widget.combobox.count()):
                path = widget.combobox.itemData(i, Q.Qt.ToolTipRole)  # type: str
                if path == file_name:
                    widget.combobox.setCurrentIndex(i)
                    break
            self.commitData.emit(widget)
            self.closeEditor.emit(widget, Q.QAbstractItemDelegate.SubmitModelCache)

class _FilesModel(Q.QStandardItemModel):
    """Helper model to manage list of data files in the *Data Files Summary* panel."""

    def headerData(self, section, orientation, role=Q.Qt.DisplayRole):
        """Redefined from *QStandardItemModel*."""
        headers = {0: translate("DataFiles", "Unit"),
                   1: translate("DataFiles", "Filename"),
                   2: translate("DataFiles", "Stage"),
                   3: translate("DataFiles", "Mode")}

        if orientation == Q.Qt.Horizontal and role in (Q.Qt.DisplayRole,):
            return headers.get(section)
        return super().headerData(section, orientation, role)

class _ComboModel(Q.QSortFilterProxyModel):
    """Helper model to show list of unique file names in selection combo."""

    def __init__(self, parent):
        """Create model."""
        super().__init__(parent)
        source_model = Q.QStandardItemModel(parent)
        self.setSourceModel(source_model)
        self.sort(0, Q.Qt.AscendingOrder)

    def addItem(self, path, title=None):
        """Add item to model."""
        source_model = self.sourceModel()
        if path and not source_model.findItems(path, Q.Qt.MatchExactly, 1):
            if not title:
                title = osp.basename(path)
            item0 = Q.QStandardItem(title)
            item0.setData(path, Q.Qt.ToolTipRole) # tooltip
            item1 = Q.QStandardItem(path) # for search
            source_model.appendRow([item0, item1])

def _file_title(file_name, is_embedded=False):
    """
    Returns nice title for the file's item in a list, such as a tree or a combobox.

    For a normal file, renders its basename.
    For an embedded file, renders its external name and adds "(embedded)" after the name.
    For an empty filename, renders <undefined>.

    Arguments:
        file (File):

    Returns:
        str
    """
    if file_name:
        if is_reference(file_name):
            title = external_file(file_name)
        else:
            title = osp.basename(file_name)
    else:
        title = '<{}>'.format(translate("DataFiles", "undefined"))

    if is_embedded:
        title += ' ({})'.format(translate("DataFiles", "embedded"))

    return title


import threading

from constants import *
from controller.Bot import Bot
from controller.InputController import InputController
from controller.ScreenCaptureController import ScreenCaptureController
from controller.SettingsController import SettingsController

class MainActivity(object):
    _execute = True
    _pause = True
    _viewPause = False
    _cursorValueVisible = False

    def __init__(self, view):
        self.view = view
        self.capture = ScreenCaptureController(self.view)
        self.settingsController = SettingsController(self.view)
        self.inputController = InputController()
        self.bot = Bot()

        self.bot.setView(self.view)
        self.bot.setCapture(self.capture)
        self.bot.setInputController(self.inputController)

        self.settingsController.loadData()
        self.bot.setSettings(self.settingsController.settings)

    def runBot(self):
        botThread = threading.Thread(target=self._runner, daemon=True)
        self.view.getByKey(VIEW_BUTTON_GO_KEY).update(button_color=("#CCCCCC", "#CCCCCC"), disabled=True)

        botThread.start()

    def _runner(self):
        """
            bot thread runner
        """
        self.capture.addViewArea(self.settingsController.settings.rodPresentViewArea)
        self.capture.addViewArea(self.settingsController.settings.baitViewArea)
        self.capture.addViewArea(self.settingsController.settings.catchingBoxPresentViewArea)
        self.capture.addViewArea(self.settingsController.settings.fishFightingViewArea)

        self.bot.initializeStates()

        while self._execute:
            if self._cursorValueVisible:
                self.capture.captureCursor()

            if self._viewPause:
                continue

            self.bot.execute()

    def onSaveClick(self, values):
        """
            onSaveClick(values)

            save settings and update areas
        """
        self.settingsController.updateData(values)
        self.settingsController.saveData()
        self.capture.updateArea(self.settingsController.settings.rodPresentViewArea)
        self.capture.updateArea(self.settingsController.settings.baitViewArea)
        self.capture.updateArea(self.settingsController.settings.catchingBoxPresentViewArea)
        self.capture.updateArea(self.settingsController.settings.fishFightingViewArea)

    def onCursorValueClick(self):
        """
            toggle cursor capture
        """
        if self._cursorValueVisible:
            self.view.logMessage("Cursor value pause", "#FF0000")
        else:
            self.view.logMessage("Cursor value play", "#FF0000")

        self._cursorValueVisible ^= True

    def onBotPauseClick(self):
        """
            toggle Bot state transition
        """
        if self._pause:
            self.view.logMessage("play", "#FF0000")
        else:
            self.view.logMessage("pause", "#FF0000")

        self._pause ^= True
        self.bot.setPause(self._pause)

    def onViewPauseClick(self):
        """
            toggle View capture preview
        """
        if self._viewPause:
            self.view.logMessage("View pause", "#FF0000")
        else:
            self.view.logMessage("View play", "#FF0000")

        self._viewPause ^= True


import sys
import numpy as np

# Path of Python SCN scripts generator
scn_generator_class = '/home/jorisp/GitHub/Verilog/scripts/scn_generator'
sys.path.append(scn_generator_class)

# Import Class
import scn_class

# Import Model
max7219_model_class = "/home/linux-jp/Documents/GitHub/VHDL_code/MAX7219/scripts/MAX7219_models"
sys.path.append(max7219_model_class)
import max7219_models_class

# Import HTML
sys.path.append("/home/linux-jp/Documents/GitHub/Python/Tools/yattag_debug")
import html_blocs_class

class macros_max_controller_class:

    def __init__(self, scn):
        self.scn = scn

        # == TB ALIAS ==
        self.clk_alias = "CLK"

        self.spi_frame_received_alias = "SPI_FRAME_RECEIVED"
        self.spi_load_received_alias  = "SPI_LOAD_RECEIVED"
        self.spi_data_alias           = "SPI_DATA"
        self.config_done_alias        = "O_CONFIG_DONE"

        self.i_decode_mode_alias  = "I_DECOD_MODE"
        self.i_intensity_alias    = "I_INTENSITY"
        self.i_scan_limit_alias   = "I_SCAN_LIMIT"
        self.i_shutdown_alias     = "I_SHUTDOWN"
        self.i_display_test_alias = "I_DISPLAY_TEST"

        self.i_new_config_val_alias = "I_NEW_CONFIG_VAL"

        self.i_static_dyn_alias       = "I_STATIC_DYN"
        self.i_new_display_alias      = "I_NEW_DISPLAY"
        self.i_en_static_alias        = "I_EN_STATIC"
        self.i_me_static_alias        = "I_ME_STATIC"
        self.i_we_static_alias        = "I_WE_STATIC"
        self.i_addr_static_alias      = "I_ADDR_STATIC"
        self.i_wdata_static_alias     = "I_WDATA_STATIC"
        self.i_start_ptr_static_alias = "I_START_PTR_STATIC"
        self.i_last_ptr_static_alias  = "I_LAST_PTR_STATIC"
        self.i_loop_static_alias      = "I_LOOP_STATIC"
        self.o_rdata_static_alias     = "O_RDATA_STATIC"
        self.o_ptr_equality_static_alias = "O_PTR_EQUALITY_STATIC"

        self.sel_display_screen_matrix_alias = "DISPLAY_SCREEN_SEL"
        self.display_screen_matrix_alias     = "DISPLAY_SCREEN_MATRIX"
        self.i_me_scroller_alias            = "I_ME_SCROLLER"
        self.i_we_scroller_alias            = "I_WE_SCROLLER"
        self.i_addr_scroller_alias          = "I_ADDR_SCROLLER"
        self.i_wdata_scroller_alias         = "I_WDATA_SCROLLER"
        self.i_ram_start_ptr_scroller_alias = "I_RAM_START_PTR_SCROLLER"
        self.i_msg_length_scroller_alias    = "I_MSG_LENGTH_SCROLLER"
        self.i_max_tempo_cnt_scroller_alias = "I_MAX_TEMPO_CNT_SCROLLER"
        self.o_rdata_scroller_alias         = "O_RDATA_SCROLLER"
        self.o_scroller_busy_alias          = "O_SCROLLER_BUSY"

        # == Default Value from TB ==
        self.decode_mode_init_data  = 0xAA
        self.intensity_init_data    = 0xBB
        self.scan_limit_init_data   = 0xCC
        self.shutdown_init_data     = 0xDD
        self.display_test_init_data = 0

        # == Create MAX7219 MODELS CLASS ==
        self.max7219_models_class     = max7219_models_class.max7219_models_class(scn                      = self.scn,
                                                                                spi_frame_received_alias = self.spi_frame_received_alias,
                                                                                spi_load_received_alias  = self.spi_load_received_alias,
                                                                                spi_data_alias           = self.spi_data_alias)

    # Check SPI INIT Config after Release of reset
    def check_init_config(self):

        self.scn.print_comment("Check Initial SPI Configuration and wait for Config Done")
        self.max7219_models_class.check_spi_config(self.decode_mode_init_data,
                                                   self.intensity_init_data,
                                                   self.scan_limit_init_data,
                                                   self.shutdown_init_data,
                                                   self.display_test_init_data)

        self.scn.WTRS(self.config_done_alias, 100, "us")

    # Check a SPI Config
    def check_config(self,
                     decode_mode_data,
                     intensity_data,
                     scan_limit_data,
                     shutdown_data,
                     display_test_data):

        self.scn.print_comment("Check a SPI Configuration and wait for Config Done")
        self.max7219_models_class.check_spi_config(decode_mode_data,
                                                   intensity_data,
                                                   scan_limit_data,
                                                   shutdown_data,
                                                   display_test_data)

        self.scn.WTRS(self.config_done_alias, 100, "us")
        self.scn.CHK(self.config_done_alias, 1, "OK")

    # Set a new Config and check it
    def set_new_config_and_check(self,
                                 decode_mode_data,
                                 intensity_data,
                                 scan_limit_data,
                                 shutdown_data,
                                 display_test_data,
                                 bypass_set_config = False):

        self.scn.print_comment("Set a New config and check it")

        # Check that A last config was already done
        self.scn.CHK(self.config_done_alias, 1, "OK")

        self.scn.WTFS(self.clk_alias)

        # Set config
        self.scn.SET(self.i_decode_mode_alias,  decode_mode_data)
        self.scn.SET(self.i_intensity_alias,    intensity_data)
        self.scn.SET(self.i_scan_limit_alias,   scan_limit_data)
        self.scn.SET(self.i_shutdown_alias,     shutdown_data)
        self.scn.SET(self.i_display_test_alias, display_test_data)

        self.scn.WTFS(self.clk_alias)

        if(bypass_set_config == False):
            self.scn.WTFS(self.clk_alias)
            self.scn.SET(self.i_new_config_val_alias, 1)
            self.scn.WTFS(self.clk_alias)
            self.scn.SET(self.i_new_config_val_alias, 0)

        # Wait for Falling edge of status
        self.scn.WTFS(self.config_done_alias, 100, "us")

        # Check Config
        self.check_config(decode_mode_data,
                          intensity_data,
                          scan_limit_data,
                          shutdown_data,
                          display_test_data)

    # Write Data in Static RAM
    def write_static_data_in_ram(self, ram_data_list):
        self.max7219_models_class.write_data_in_ram(ram_data_list,
                                                    self.i_me_static_alias,
                                                    self.i_we_static_alias,
                                                    self.i_addr_static_alias,
                                                    self.i_wdata_static_alias,
                                                    self.clk_alias)

    def read_static_data_in_ram(self, ram_data_list):
        self.max7219_models_class.read_data_in_ram(ram_data_list,
                                                   self.i_me_static_alias,
                                                   self.i_we_static_alias,
                                                   self.i_addr_static_alias,
                                                   self.o_rdata_static_alias,
                                                   self.clk_alias)

    # Send One SPI Request and check it - STATIC Mode
    def send_one_spi_request_and_check_static(self, ram_addr, ram_data):

        spi_check_expected = "OK"

        self.max7219_models_class.send_one_spi_request_and_check(ram_addr,
                                                                 ram_data,
                                                                 spi_check_expected,
                                                                 self.i_start_ptr_static_alias,
                                                                 self.i_last_ptr_static_alias,
                                                                 self.i_static_dyn_alias,
                                                                 self.i_new_display_alias,
                                                                 self.o_ptr_equality_static_alias,
                                                                 self.clk_alias)
    # Send Multiple SPI REQUEST and check it - Static Mode
    def send_multiple_spi_request_and_check_static(self,
                                                   ram_start_ptr,
                                                   ram_stop_ptr):
        spi_check_expected = "OK"
        self.max7219_models_class.send_multiple_spi_request_and_check(ram_start_ptr,
                                                                      ram_stop_ptr,
                                                                      spi_check_expected,
                                                                      self.i_start_ptr_static_alias,
                                                                      self.i_last_ptr_static_alias,
                                                                      self.i_static_dyn_alias,
                                                                      self.i_new_display_alias,
                                                                      self.o_ptr_equality_static_alias,
                                                                      self.clk_alias)

    # Write Data in Scroller RAM
    def write_scroller_data_in_ram(self, ram_data_list):
        self.max7219_models_class.write_data_in_ram(ram_data_list,
                                                    self.i_me_scroller_alias,
                                                    self.i_we_scroller_alias,
                                                    self.i_addr_scroller_alias,
                                                    self.i_wdata_scroller_alias,
                                                    self.clk_alias,
                                                    ram_sel = "SCROLLER")
    # Read Data in Scroller RAM
    def read_scroller_data_in_ram(self, ram_data_list):
        self.max7219_models_class.read_data_in_ram(ram_data_list,
                                                   self.i_me_scroller_alias,
                                                   self.i_we_scroller_alias,
                                                   self.i_addr_scroller_alias,
                                                   self.o_rdata_scroller_alias,
                                                   self.clk_alias)

    # Set Scroller Config and check spi transaction
    # RAM Scroller must be previously written
    def send_scroller_pattern_and_check(self,
                                        ram_start_ptr,
                                        msg_length,
                                        timeout_value   = 10,
                                        timeout_unity   = "ms",
                                        html_debug_en   = False,
                                        html_debug_name = ""):

        self.max7219_models_class.send_scroller_pattern_and_check(
            ram_start_addr      = ram_start_ptr,
            msg_length          = msg_length,
            static_dyn_alias    = self.i_static_dyn_alias,
            ram_start_ptr_alias = self.i_ram_start_ptr_scroller_alias,
            msg_length_alias    = self.i_msg_length_scroller_alias,
            new_display_alias   = self.i_new_display_alias,
            scroller_busy_alias = self.o_scroller_busy_alias,
            clk_alias           = self.clk_alias,
            timeout_value       = timeout_value,
            timeout_unity       = timeout_unity,
            html_debug_en       = html_debug_en,
            html_debug_name     = html_debug_name)

    # Send Commandes from a list and check
    # Commandes : "CONFIG" "STATIC" or "SCROLLER"
    def send_multile_pattern_and_check(self, commandes_list):

        # Write commands
        for cmd in range(0, len(commandes_list)):
            if(commandes_list[cmd] == "CONFIG"):
                self.scn.WTFS(self.clk_alias)
                self.scn.SET(self.i_new_config_val_alias, 1)
                self.scn.WTFS(self.clk_alias)
                self.scn.SET(self.i_new_config_val_alias, 0)

            elif(commandes_list[cmd] == "STATIC"):
                self.scn.WTFS(self.clk_alias)
                self.scn.SET(self.i_static_dyn_alias, 0)
                self.scn.SET(self.i_new_display_alias, 1)
                self.scn.WTFS(self.clk_alias)
                self.scn.SET(self.i_static_dyn_alias, 0)
                self.scn.SET(self.i_new_display_alias, 0)

            elif(commandes_list[cmd] == "SCROLLER"):
                self.scn.WTFS(self.clk_alias)
                self.scn.SET(self.i_static_dyn_alias, 1)
                self.scn.SET(self.i_new_display_alias, 1)
                self.scn.WTFS(self.clk_alias)
                self.scn.SET(self.i_static_dyn_alias, 0)
                self.scn.SET(self.i_new_display_alias, 0)

            else:
                print("Error: Commands not recognized")

        # Check Status for each commands (MAX 10)
        if(len(commandes_list) <= 10):
            max_cmd = len(commandes_list)
        else:
            max_cmd = 10

        for cmd in range(0, max_cmd):
            if(commandes_list[cmd] == "CONFIG"):
                self.scn.WTRS(self.config_done_alias, 10, "ms")

            elif(commandes_list[cmd] == "STATIC"):
                self.scn.WTRS(self.o_ptr_equality_static_alias, 10, "ms")

            elif(commandes_list[cmd] == "SCROLLER"):
                self.scn.WTFS(self.o_scroller_busy_alias, 10, "ms")


from typing import Dict, List

class Solution:
    def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:
        def get_dictionary_of_char_occuring(word: str):
            ret = {}
            for w in word:
                ret[w] = word.count(w)

            return ret

        def isASubString(word: str, dictionary: Dict):
            for d in dictionary:
                if word.count(d) < dictionary[d]:
                    return False

            return True

        count_of_str = {}

        for w in set(words2):
            dictionary = get_dictionary_of_char_occuring(w)
            for k in dictionary:
                count_of_str[k] = max(count_of_str[k], dictionary[k]) if count_of_str.get(k) else dictionary[k]

        #  for w in words1:
            #  if

        return list(filter(lambda x: isASubString(x, count_of_str), words1))

if __name__ == "__main__":
    s = Solution()

    words1 = ["amazon", "apple", "facebook", "google", "leetcode"]
    words2 = ["e", "o"]

    print(s.wordSubsets(words1, words2))

    words1 = ["amazon", "apple", "facebook", "google", "leetcode"]
    words2 = ["lo", "eo"]

    print(s.wordSubsets(words1, words2))

    words1 = ["facebok"]
    words2 = ["e", "oo"]

    print(s.wordSubsets(words1, words2))

    words1 = ["amazon","apple","facebook","google","leetcode"]
    #  words2 = ["ec", "oc", "ceo"]
    words2 = ["ee", "oo", "ceo"]

    print(s.wordSubsets(words1, words2))


# http://www.checkio.org/mission/hamming-distance2/
__author__ = 'ken'

def checkio(n, m):
    ns = bin(n)[2:]
    ms = bin(m)[2:]
    ns = ("0" * max(0, len(ms)-len(ns))) + ns
    ms = ("0" * max(0, len(ns)-len(ms))) + ms
    cnt = [1 for a, b in zip(ns, ms) if a != b]
    return len(cnt)

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio(117, 17) == 3, "First example"
    assert checkio(1, 2) == 2, "Second example"
    assert checkio(16, 15) == 5, "Third example"