from collections import deque
from threading import Lock
from time import perf_counter
import asyncio
from typing import Optional, Tuple

# Third-party modules
import orjson
from typing import Union, Iterable, List, Dict, Any

# NOC modules
from noc.config import config
from noc.core.comp import smart_text

class TopicQueue(object):
    def __init__(self, topic: str, loop: Optional[asyncio.BaseEventLoop] = None) -> None:
        self.topic = topic
        self.lock = Lock()
        self.waiter: Optional[asyncio.Event] = None
        self.shutdown_waiter: Optional[asyncio.Event] = None
        self.queue: deque = deque()
        self.queue_size = 0
        self.to_shutdown = False
        self.last_get: Optional[float] = None
        self.loop = loop
        # Metrics
        self.msg_put = 0
        self.msg_get = 0
        self.msg_put_size = 0
        self.msg_get_size = 0
        self.msg_requeued = 0
        self.msg_requeued_size = 0

    @staticmethod
    def iter_encode_chunks(
        message: Union[str, object], limit: int = config.nsqd.mpub_size - 8
    ) -> Iterable[str]:
        """
        Encode data to iterable atomic chunks of up to limit size

        :param message: Input data
        :param limit: Chunk limit
        :return: Yields JSON-encoded chunks
        :raises ValueError: If message cannot be encoded or too big
        """
        if isinstance(message, str):
            if len(message) > limit:
                raise ValueError("Message too big")
            yield message
        else:
            data = smart_text(orjson.dumps(message))
            if len(data) <= limit:
                yield data
            elif isinstance(message, (list, tuple)):
                # Try to split in parts
                n_chunks = len(data) // limit + 1
                chunk_size = len(message) // n_chunks
                while message:
                    chunk, message = message[:chunk_size], message[chunk_size:]
                    chunk_data = smart_text(orjson.dumps(chunk))
                    if len(chunk_data) > limit:
                        raise ValueError("Message too big")
                    yield chunk_data
            else:
                raise ValueError("Message too big")

    def put(self, message: object, fifo: bool = True) -> None:
        """
        Put message into queue. Block if queue is full

        :param message: Message of any json-serializable type
        :param fifo: Boolean. Append message to the start of queue (LIFO) if False.
            Append message to the end of queue (FIFO) if True.
        :return:
        """
        if not isinstance(message, str):
            message = smart_text(orjson.dumps(message))
        with self.lock:
            if self.to_shutdown:
                raise RuntimeError("put() after shutdown")
            if fifo:
                self.queue.append(message)
            else:
                self.queue.appendleft(message)
            m_size = len(message)
            self.queue_size += m_size
            self.msg_put += 1
            self.msg_put_size += m_size
            self._notify_waiters()

    def _notify_waiters(self):
        """
        Notify waiting coroutine.
        Must be called within lock
        :return:
        """
        if not self.waiter:
            return
        waiter = self.waiter
        if self.loop:
            self.loop.call_soon_threadsafe(waiter.set)
        else:
            waiter.set()

    async def _wait_for_data(self, timeout: Optional[float] = None):
        if self.waiter:
            raise RuntimeError("Incomplete wait")
        with self.lock:
            self.waiter = asyncio.Event()
        try:
            if timeout:
                await asyncio.wait_for(self.waiter.wait(), timeout)
            else:
                await self.waiter.wait()
        except asyncio.TimeoutError:
            pass
        finally:
            with self.lock:
                self.waiter = None

    def return_messages(self, messages: List[str]) -> None:
        """
        Return messages to the start of the queue

        :param messages: List of messages
        :return:
        """
        with self.lock:
            if self.to_shutdown:
                raise RuntimeError("return_messages() after shutdown")
            for msg in reversed(messages):
                self.queue.appendleft(msg)
                self.msg_requeued += 1
                self.msg_requeued_size += len(msg)
            self._notify_waiters()

    def iter_get(
        self, n: int = 1, size: int = None, total_overhead: int = 0, message_overhead: int = 0
    ) -> Iterable[str]:
        """
        Get up to `n` items up to `size` size.

        Warning queue will be locked until the end of function call.

        :param n: Amount of items returned
        :param size: None - unlimited, integer - upper size limit
        :param total_overhead: Adjust total size to `total_overhead` octets.
        :param message_overhead: Adjust total size to `message_overhead` per each returned message.
        :return: Yields items
        """
        total = 0
        if size and total_overhead:
            total += total_overhead
        with self.lock:
            for _i in range(n):
                try:
                    msg = self.queue.popleft()
                    m_size = len(msg)
                    total += m_size
                    if size and message_overhead:
                        total += message_overhead
                    if size and total > size:
                        # Size limit exceeded. Return message to queue
                        self.queue.appendleft(msg)
                        break
                    self.queue_size -= m_size
                    self.msg_get += 1
                    self.msg_get_size += m_size
                    yield msg
                except IndexError:
                    break
            self.last_get = perf_counter()

    def is_empty(self) -> bool:
        """
        Check if queue is empty

        :return: True if queue is empty, False otherwise
        """
        return not self.queue_size

    def qsize(self) -> Tuple[int, int]:
        """
        Returns amount of messages and size of queue

        :return: messages, total size
        """
        with self.lock:
            return len(self.queue), self.queue_size

    def shutdown(self) -> None:
        """
        Begin shutdown sequence. Disable queue writes

        :return:
        """
        if self.to_shutdown:
            raise RuntimeError("Already in shutdown")
        self.to_shutdown = True
        self.shutdown_waiter = asyncio.Event()
        with self.lock:
            self._notify_waiters()  # Leave publisher waiting state

    async def wait_for_shutdown(self, timeout: Optional[float] = None) -> None:
        """
        Get publisher a chance to drain collected items
        :param timeout:
        :return:
        """
        if not self.shutdown_waiter:
            return
        try:
            await asyncio.wait_for(self.shutdown_waiter.wait(), timeout)
        except asyncio.TimeoutError:
            pass

    def notify_shutdown(self):
        if self.shutdown_waiter:
            self.shutdown_waiter.set()

    async def wait(self, timeout: Optional[float] = None, rate: Optional[int] = None):
        """
        Block and wait up to `timeout`
        :param timeout: Max. wait in seconds
        :param rate: Max. rate of publishing in messages per second
        :return:
        """
        # Sleep to throttle rate
        if rate and self.last_get and not self.to_shutdown:
            now = perf_counter()
            delta = max(self.last_get + 1.0 / rate - now, 0)
            if delta > 0:
                await asyncio.sleep(delta)
                # Adjust remaining timeout
                if timeout:
                    # Adjust timeout
                    timeout -= delta
                    if timeout <= 0:
                        # Timeout expired
                        return
        # Check if queue already contains messages
        if not self.queue_size and not self.to_shutdown:
            # No messages, wait
            await self._wait_for_data(timeout)

    def apply_metrics(self, data: Dict[str, Any]) -> None:
        data.update(
            {
                ("nsq_msg_put", ("topic", self.topic)): self.msg_put,
                ("nsq_msg_put_size", ("topic", self.topic)): self.msg_put_size,
                ("nsq_msg_get", ("topic", self.topic)): self.msg_get,
                ("nsq_msg_get_size", ("topic", self.topic)): self.msg_get_size,
                ("nsq_msg_requeued", ("topic", self.topic)): self.msg_requeued,
                ("nsq_msg_requeued_size", ("topic", self.topic)): self.msg_requeued_size,
            }
        )


import pickle as pkl
import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

import numpy as np
import matplotlib.pyplot as plt

# Hyper-parameters
input_size = 784
hidden_size = 500
num_classes = 10
num_epochs = 5
batch_size = 100
learning_rate = 0.001

file_name_og = "Layer_surface_code\\cnn_mnist_models\\model-original-2.ckpt"
file_name_rob = "Layer_surface_code\\cnn_mnist_models\\model-robust_2.ckpt"

# Fully connected neural network with one hidden layer
class NeuralNet(nn.Module):
    def __init__(self):
        super(NeuralNet, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 32, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(32, 64, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.drop_out = nn.Dropout()
        self.fc1 = nn.Linear(7 * 7 * 64, 1000)
        self.fc2 = nn.Linear(1000, 10)

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.reshape(out.size(0), -1)
        out = self.drop_out(out)
        out = self.fc1(out)
        out = self.fc2(out)

        return out

def train(device, train_loader):
    model = NeuralNet().to(device)

    # Loss and optimizer
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

    # Train the model
    total_step = len(train_loader)
    for epoch in range(num_epochs):
        for i, (images, labels) in enumerate(train_loader):
            # Move tensors to the configured device
            images = images.to(device)
            labels = labels.to(device)

            # Forward pass
            outputs = model(images)
            loss = criterion(outputs, labels)

            # Backward and optimize
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if (i + 1) % 100 == 0:
                print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
                      .format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))

        # Save the model checkpoint
        torch.save(model.state_dict(), 'Layer_surface_code/cnn_mnist_models/model-{}.ckpt'.format(epoch))

    return model

def test(device, test_loader, eps, file_name):
    # Test the model
    # In test phase, we don't need to compute gradients (for memory efficiency)
    criterion = nn.CrossEntropyLoss()

    model = NeuralNet().to(device)
    model.load_state_dict(torch.load(file_name))

    correct = 0
    adv_correct = 0
    misclassified = 0
    total = 0
    noises = []
    y_preds = []
    y_preds_adv = []

    for images, labels in test_loader:
        images = Variable(images.to(device), requires_grad=True)
        labels = Variable(labels.to(device))

        outputs = model(images)
        loss = criterion(outputs, labels)
        loss.backward()

        # Add perturbation
        grad = torch.sign(images.grad.data)
        imgs_adv = torch.clamp(images.data + (eps * grad), 0, 1)

        adv_outputs = model(Variable(imgs_adv))

        _, predicted = torch.max(outputs.data, 1)
        _, adv_preds = torch.max(adv_outputs.data, 1)

        total += labels.size(0)
        correct += (predicted == labels).sum().item()
        adv_correct += (adv_preds == labels).sum().item()
        misclassified += (predicted != adv_preds).sum().item()

        noises.extend((images - imgs_adv).data.numpy())
        y_preds.extend(predicted.data.numpy())
        y_preds_adv.extend(adv_preds.data.numpy())

    print('Accuracy of the network w/o adversarial attack on the 10000 test images: {} %'.format(100 * correct / total))
    print('Accuracy of the network with adversarial attack on the 10000 test images: {} %'.format(
        100 * adv_correct / total))
    print('Number of misclassified examples (as compared to clean predictions): {}/{}'.format(misclassified, total))
    print("\n")

    return 100*(correct/total), 100*(adv_correct/total), (misclassified, total)

def main(flag):
    # Device configuration
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    # MNIST dataset
    train_dataset = torchvision.datasets.MNIST(root='./data',
                                               train=True,
                                               transform=transforms.ToTensor(),
                                               download=True)

    test_dataset = torchvision.datasets.MNIST(root='./data',
                                              train=False,
                                              transform=transforms.ToTensor())

    # Data loader
    train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)

    test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)

    if flag == "train":
        model = train(device, train_loader)
    elif flag == "test":
        pertub = np.linspace(0.001, 0.3, 10)
        no_attack_og = []
        attack_og = []
        no_attack_rob = []
        attack_rob = []

        for i in range(len(pertub)):
            og = test(device, test_loader, pertub[i], file_name=file_name_og)
            rob = test(device, test_loader, pertub[i], file_name=file_name_rob)

            no_attack_og.append(og[0])
            attack_og.append(og[1])

            no_attack_rob.append(rob[0])
            attack_rob.append(rob[1])

        plt.figure(1)
        plt.plot(pertub, attack_og, 'r*')
        plt.plot(pertub, attack_og, 'r', label='Original Model')

        # plt.plot(pertub, no_attack_og, 'g*')
        # plt.plot(pertub, no_attack_og, 'g', label='Original Model')

        plt.plot(pertub, attack_rob, 'b*')
        plt.plot(pertub, attack_rob, 'b', label='Robust Model')

        # plt.plot(pertub, no_attack_rob, 'y', label='Robust Model')

        plt.ylabel('Model Accuracy (%)')
        plt.xlabel('Perturbation magnitude')
        plt.legend()
        plt.show()

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


import channels
import django
import graphene
import pytest

import channels_graphql_ws
import channels_graphql_ws.testing

@pytest.fixture
def gql():
    """PyTest fixture for testing GraphQL WebSocket backends.

    The fixture provides a method to setup GraphQL testing backend for
    the given GraphQL schema (query, mutation, and subscription). In
    particular: it sets up an instance of `GraphqlWsConsumer` and an
    instance of `GraphqlWsCommunicator`. The former one is returned from
    the function.

    Syntax:
        gql(
            *,
            query=None,
            mutation=None,
            subscription=None,
            consumer_attrs=None,
            communicator_kwds=None
        ):

    Args:
        query: Root GraphQL query. Optional.
        mutation: Root GraphQL subscription. Optional.
        subscription: Root GraphQL mutation. Optional.
        consumer_attrs: `GraphqlWsConsumer` attributes dict. Optional.
        communicator_kwds: Extra keyword arguments for the Channels
            `channels.testing.WebsocketCommunicator`. Optional.

    Returns:
        An instance of the `GraphqlWsCommunicator` class which has many
        useful GraphQL-related methods, see the `GraphqlWsCommunicator`
        class docstrings for details.

    Use like this:
    ```
    def test_something(gql):
        comm = gql(
            # GraphQl schema.
            query=MyQuery,
            mutation=MyMutation,
            subscription=MySubscription,
            # `GraphqlWsConsumer` settings.
            consumer_attrs={"strict_ordering": True},
            # `channels.testing.WebsocketCommunicator` settings.
            communicator_kwds={"headers": [...]}
        )
        ...
    ```

    """

    def communicator_constructor(
        *,
        query=None,
        mutation=None,
        subscription=None,
        consumer_attrs=None,
        communicator_kwds=None,
    ):
        """Setup GraphQL consumer and the communicator for tests."""

        class ChannelsConsumer(channels_graphql_ws.GraphqlWsConsumer):
            """Channels WebSocket consumer for GraphQL API."""

            schema = graphene.Schema(
                query=query,
                mutation=mutation,
                subscription=subscription,
                auto_camelcase=False,
            )

        # Set additional attributes to the `ChannelsConsumer`.
        if consumer_attrs is not None:
            for attr, val in consumer_attrs.items():
                setattr(ChannelsConsumer, attr, val)

        application = channels.routing.ProtocolTypeRouter(
            {
                "websocket": channels.routing.URLRouter(
                    [django.urls.path("graphql/", ChannelsConsumer)]
                )
            }
        )

        graphql_ws_communicator = channels_graphql_ws.testing.GraphqlWsCommunicator(
            application=application, path="graphql/", **(communicator_kwds or {})
        )

        return graphql_ws_communicator

    return communicator_constructor


# -*- coding: utf-8 -*-

from __future__ import unicode_literals, print_function, division, absolute_import
from builtins import str

import os
from ..indexed_enums import Enum, EnumArray

def assert_near(value, target_value, absolute_error_margin = None, message = '', relative_error_margin = None):
    '''

      :param value: Value returned by the test
      :param target_value: Value that the test should return to pass
      :param absolute_error_margin: Absolute error margin authorized
      :param message: Error message to be displayed if the test fails
      :param relative_error_margin: Relative error margin authorized

      Limit : This function cannot be used to assert near dates or periods.

    '''

    import numpy as np

    if absolute_error_margin is None and relative_error_margin is None:
        absolute_error_margin = 0
    if isinstance(value, (list, tuple)):
        value = np.array(value)
    if isinstance(target_value, (list, tuple)):
        target_value = np.array(target_value)
    if isinstance(message, str):
        message = message.encode('utf-8')
    if isinstance(value, np.ndarray):
        if isinstance(target_value, Enum) or (isinstance(target_value, np.ndarray) and target_value.dtype == object):
            if not isinstance(value, EnumArray):
                assert False, "Expected an Enum, got {} of dtype {}".format(value, value.dtype)
            else:
                assert (target_value == value.decode()).all(), "Expected {}, got {}".format(target_value, value)
        else:
            target_value = np.array(target_value).astype(np.float32)
            value = np.array(value).astype(np.float32)
            diff = abs(target_value - value)

            if absolute_error_margin is not None:
                assert (diff <= absolute_error_margin).all(), \
                    '{}{} differs from {} with an absolute margin {} > {}'.format(message, value, target_value,
                        diff, absolute_error_margin)
            if relative_error_margin is not None:
                assert (diff <= abs(relative_error_margin * target_value)).all(), \
                    '{}{} differs from {} with a relative margin {} > {}'.format(message, value, target_value,
                        diff, abs(relative_error_margin * target_value))
    else:
        if absolute_error_margin is not None:
            assert abs(target_value - value) <= absolute_error_margin, \
                '{}{} differs from {} with an absolute margin {} > {}'.format(message, value, target_value,
                    abs(target_value - value), absolute_error_margin)
        if relative_error_margin is not None:
            assert abs(target_value - value) <= abs(relative_error_margin * target_value), \
                '{}{} differs from {} with a relative margin {} > {}'.format(message, value, target_value,
                    abs(target_value - value), abs(relative_error_margin * target_value))

def indent(text):
    return "  {}".format(text.replace(os.linesep, "{}  ".format(os.linesep)))

def get_trace_tool_link(scenario, variables, api_url, trace_tool_url):
    import json
    import urllib

    scenario_json = scenario.to_json()
    simulation_json = {
        'scenarios': [scenario_json],
        'variables': variables,
        }
    url = trace_tool_url + '?' + urllib.urlencode({
        'simulation': json.dumps(simulation_json),
        'api_url': api_url,
        })
    return url


from collections import defaultdict
import gzip
import random
import math
import scipy
from scipy import optimize
import numpy
import nltk
from nltk.tokenize import word_tokenize
import string
from nltk.stem.porter import *
from sklearn import linear_model
from scipy import spatial
from random import shuffle
from nltk.corpus import stopwords
import itertools
from sklearn.metrics import mean_squared_error
import statistics

# read data
data = []
requiredEntrySet = {"fit", "bust size", "weight", "rating", "rented for", "review_text", "body type", "review_summary", "category", "height", "size", "age"}
def readJSON(path):
	for l in gzip.open(path, 'rt'):
		try:
			d = eval(l)
			curEntries = d.keys()
			if requiredEntrySet.issubset(curEntries):
				yield d
		except Exception as e:
			pass

for d in readJSON("renttherunway_final_data.json.gz"):
	data.append(d)

data = data[:10000]

# process the data to parse the height and the weight
import re

def parse_height(height):
    pattern = re.compile(r'(\d+)\' (\d+)\"')
    match = pattern.match(height)

    if match:
        feet = int(match.group(1))
        inch = int(match.group(2))

        return feet*12 + inch
    else:
        return -1

def parse_weight(weight):
    pattern = re.compile(r'(\d+)lbs')
    match = pattern.match(weight)
    if match:
        return int(match.group(1))
    else:
        return -1

def parse_data(data):
    for d in data:
        height = parse_height(d['height'])
        d['height'] = height

        weight = parse_weight(d['weight'])
        d['weight'] = weight

parse_data(data)

# extract the min and max for regulization
def extract_max(data):
    maxAge = -1
    maxSize = -1
    maxWeight = -1
    maxHeight = -1

    for d in data:
        if int(d["age"]) > maxAge:
            maxAge = int(d["age"])
        if d["size"] > maxSize:
            maxSize = d["size"]
        if d["weight"] > maxWeight:
            maxWeight = d["weight"]
        if d["height"] > maxHeight:
            maxHeight = d["height"]

    return maxAge, maxSize, maxWeight, maxHeight

maxAge, maxSize, maxWeight, maxHeight = extract_max(data)

def extract_min(data):
    minAge = maxAge
    minSize = maxSize
    minWeight = maxWeight
    minHeight = maxHeight

    for d in data:
        if int(d["age"]) < minAge:
            minAge = int(d["age"])
        if d["size"] < minSize:
            minSize = d["size"]
        if d["weight"] < minWeight:
            minWeight = d["weight"]
        if d["height"] < minHeight:
            minHeight = d["height"]

    return minAge, minSize, minWeight, minHeight

minAge, minSize, minWeight, minHeight = extract_min(data)

# extract the regulized features
def regulize(d, minD, maxD):
    return (d - minD)/(maxD - minD)

def age(datum, minAge, maxAge):
    return regulize(int(datum["age"]), minAge, maxAge)

def size(datum, minSize, maxSize):
    return regulize(datum["size"], minSize, maxSize)

def weight(datum, minWeight, maxWeight):
    return regulize(datum["weight"], minWeight, maxWeight)

def height(datum, minHeight, maxHeight):
    return regulize(datum["height"], minHeight, maxHeight)

# One hot encoding features
Entry = ["fit", "rented for", "body type", "category", "bust size"]

def extract_onehot(data, Entry):
	Maps = {Entry[i]:defaultdict(int) for i in range(len(Entry))}
	for d in data:
		for f in Entry:
			mp = Maps[f]
			if d[f] not in mp:
				mp[d[f]] = len(mp)
	return Maps

# fit: 3; rented for: 9; body type: 7; category: 68; bust size: 101

Maps = extract_onehot(data, Entry)

# Combined single vector
def feature_onehot_combined(datum):
	featureVectors = [[0]*len(Maps[f]) for f in Entry]
	for i in range(len(Entry)):
		f = Entry[i]
		featureVectors[i][Maps[f][d[f]]] = 1
	singleVector = list(itertools.chain.from_iterable(featureVectors))
	singleVector.append(1)
	return singleVector

# separate features (all 5)
def feature_onehot_separated(datum):
	featureVectors = [[0]*len(Maps[f]) for f in Entry]
	for i in range(len(Entry)):
		f = Entry[i]
		featureVectors[i][Maps[f][d[f]]] = 1
	for v in featureVectors:
		v.append(1)
	return featureVectors

# print(feature_onehot_separated(data[22]))

# fit feature
def featureFit(datum):
	feat = [0] * len(Maps['fit'])
	feat[Maps['fit'][d['fit']]] = 1
	feat.append(1)
	return feat

def featureRentedFor(datum):
	feat = [0] * len(Maps['rented for'])
	feat[Maps['rented for'][d['rented for']]] = 1
	feat.append(1)
	return feat

def featureBodyType(datum):
	feat = [0] * len(Maps['body type'])
	feat[Maps['body type'][d['body type']]] = 1
	feat.append(1)
	return feat

def featureCategory(datum):
	feat = [0] * len(Maps['category'])
	feat[Maps['category'][d['category']]] = 1
	feat.append(1)
	return feat

def featureBustSize(datum):
	feat = [0] * len(Maps['bust size'])
	feat[Maps['bust size'][d['bust size']]] = 1
	feat.append(1)
	return feat

#import nltk
#nltk.download('stopwords')

# unigram and bigram
uniWordCount = defaultdict(int)
biWordCount = defaultdict(int)

uniWordCount_rmPunc = defaultdict(int)
biWordCount_rmPunc = defaultdict(int)

uniWordCount_rmPunc_Stop = defaultdict(int)
biWordCount_rmPunc_Stop = defaultdict(int)

punctuation = set(string.punctuation)
stopwords = set(stopwords.words('english'))
punctuation.add('\n')
stemmer = PorterStemmer()

for d in data:
	text = ''.join([c for c in d['review_summary'].lower()])
	for token in text.split():
		uniWordCount[token] += 1
	text = ''.join([c for c in d['review_summary'].lower() if c not in punctuation ])
	for token in text.split():
		uniWordCount_rmPunc[token] += 1
	text = ''.join([c for c in d['review_summary'].lower() if c not in punctuation and c not in stopwords])
	for token in text.split():
		uniWordCount_rmPunc_Stop[token] += 1

	text = ''.join([c for c in d['review_text'].lower() if c not in punctuation ])
	bigrm = nltk.bigrams(nltk.word_tokenize(text))
	for token in bigrm:
		biWordCount_rmPunc[token] += 1
	text = ''.join([c for c in d['review_text'].lower()])
	bigrm = nltk.bigrams(nltk.word_tokenize(text))
	for token in bigrm:
		biWordCount[token] += 1
	text = ''.join([c for c in d['review_text'].lower() if c not in punctuation and c not in stopwords])
	bigrm = nltk.bigrams(nltk.word_tokenize(text))
	for token in bigrm:
		biWordCount_rmPunc_Stop[token] += 1

top_k_uni = 40
top_k_bi = 40

unicounts = [(uniWordCount[w], w) for w in uniWordCount]
unicounts.sort()
unicounts.reverse()
uniwords = [x[1] for x in unicounts[:top_k_uni]]
uniwordId = dict(zip(uniwords, range(len(uniwords))))
uniwordSet = set(uniwords)

unicounts_rmPunc = [(uniWordCount_rmPunc[w], w) for w in uniWordCount_rmPunc]
unicounts_rmPunc.sort()
unicounts_rmPunc.reverse()
uniwords_rmPunc = [x[1] for x in unicounts_rmPunc[:top_k_uni]]
uniwordId_rmPunc = dict(zip(uniwords_rmPunc, range(len(uniwords_rmPunc))))
uniwordSet_rmPunc = set(uniwords_rmPunc)

uniWordCount_rmPunc_Stop = [(uniWordCount_rmPunc_Stop[w], w) for w in uniWordCount]
uniWordCount_rmPunc_Stop.sort()
uniWordCount_rmPunc_Stop.reverse()
uniwords_rmPunc_Stop = [x[1] for x in uniWordCount_rmPunc_Stop[:top_k_uni]]
uniwordId_rmPunc_Stop = dict(zip(uniwords_rmPunc_Stop, range(len(uniwords_rmPunc_Stop))))
uniwordSet_rmPunc_Stop = set(uniwords_rmPunc_Stop)

bicounts = [(biWordCount[w], w) for w in biWordCount]
bicounts.sort()
bicounts.reverse()
biwords = [x[1] for x in bicounts[:top_k_bi]]
biwordId = dict(zip(biwords, range(len(biwords))))
biwordSet = set(biwords)

bicounts_rmPunc = [(biWordCount_rmPunc[w], w) for w in biWordCount_rmPunc]
bicounts_rmPunc.sort()
bicounts_rmPunc.reverse()
biwords_rmPunc = [x[1] for x in bicounts_rmPunc[:top_k_bi]]
biwordId_rmPunc = dict(zip(biwords_rmPunc, range(len(biwords_rmPunc))))
biwordSet_rmPunc = set(biwords_rmPunc)

bicounts_rmPunc_Stop = [(biWordCount_rmPunc_Stop[w], w) for w in biWordCount_rmPunc_Stop]
bicounts_rmPunc_Stop.sort()
bicounts_rmPunc_Stop.reverse()
biwords_rmPunc_Stop = [x[1] for x in bicounts_rmPunc_Stop[:top_k_bi]]
biwordId_rmPunc_Stop = dict(zip(biwords_rmPunc_Stop, range(len(biwords_rmPunc_Stop))))
biwordSet_rmPunc_Stop = set(biwords_rmPunc_Stop)

def featureSummary(datum, rmPunc, rmPuncStop):
	feat = [0]*len(uniwords)
	if rmPunc:
		r = ''.join([c for c in datum['review_summary'].lower() if c not in punctuation])
	elif rmPuncStop:
		r = ''.join([c for c in datum['review_summary'].lower() if c not in punctuation and c not in stopwords])
	else:
		r = ''.join([c for c in datum['review_summary'].lower()])
	for token in r.split():
		if rmPunc:
			if token in uniwords_rmPunc:
				feat[uniwordId_rmPunc[token]] += 1
		elif rmPuncStop:
			if token in uniwords_rmPunc_Stop:
				feat[uniwordId_rmPunc_Stop[token]] += 1
		else:
			if token in uniwords:
				feat[uniwordId[token]] += 1
	feat.append(1)
	return feat

def featureText(datum, rmPunc, rmPuncStop):
	feat = [0]*len(uniwords)
	if rmPunc:
		r = ''.join([c for c in datum['review_text'].lower() if c not in punctuation])
	elif rmPuncStop:
		r = ''.join([c for c in datum['review_text'].lower() if c not in punctuation and c not in stopwords])
	else:
		r = ''.join([c for c in datum['review_text'].lower()])

	for token in r.split():
		if rmPunc:
			if token in biwords_rmPunc:
				feat[biwordId_rmPunc[token]] += 1
		elif rmPuncStop:
			if token in biwords_rmPunc_Stop:
				feat[biwordId_rmPunc_Stop[token]] += 1
		else:
			if token in uniwords:
				feat[biwordId[token]] += 1
	feat.append(1)
	return feat

clf = linear_model.Ridge(1.0, fit_intercept=False) # MSE + 1.0 l2
clf.fit(X, y)
predictions = clf.predict(X_test)
print("mse of prediction is "+ str(mean_squared_error(y_test ,predictions)))


import os
import cv2
from time import sleep
folder = os.listdir(r'C:\Users\islom\PycharmProjects\Hakaton\Data\dasturlash')
print(folder[0])
list_videos = fr'C:\Users\islom\PycharmProjects\Hakaton\Data\dasturlash\{folder[0]}'
cap = cv2.VideoCapture(list_videos)
counter = 0
while True:
    success, img = cap.read()
    cv2.imshow("Image", img)
    print(counter)
    counter += 1
    sleep(0.08)
    key = cv2.waitKey(1)
    if key == ord("q"):
        break

cap.release()
cv2.destroyAllWindows()


# coding=utf-8
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation
import pandas as pd
from os import chdir, makedirs
import datetime
# my modules
import calc
import convert
import fields
import c
import info

# Make new folder for each run
name = str(datetime.datetime.now()).split('.')[0]
makedirs(c.path + name)
chdir(c.path + name)

# Constants
case = [1, -1] # directions of currents in the [shock wave, current sheet]. 1 — on us, -1 — from us. => directions of B.
num = [400, 0, 200]
min_x = -5
max_x = 5
min_z = -5
max_z = 5
x = np.linspace(min_x, max_x, num=num[0])
z = np.linspace(min_z, max_z, num=num[2])

n_steps = 200
n_part = 1000  # > 1
t_koeff = 0.5 # time of particle movement for one step. Bigger — larger trajectories and more rude results
delta = 0.245
theta = 1.0 # ion mass / proton mass
dh = 0.0423
dz = 0.489
angle = np.pi/3
x_axis = -1.0
z_axis = 0.0

# Write constants to constants.txt
info.write_constants(case=case, num=num, min_x=min_x, max_x=max_x, min_z=min_z, max_z=max_z,
                     n_steps=n_steps, n_part=n_part, t_koeff=t_koeff, delta=delta, theta=theta,
                     dh=dh, dz=dz, angle=angle, x_axis=x_axis, z_axis=z_axis)

# Calc, show and save png of total field of shock wave and rotated plasmoid
u, v, m = fields.main(x, z, case, dh, dz, angle, x_axis, z_axis)

# Calc, show and save png of trajectories
sol = calc.traces(n_steps, n_part, t_koeff, delta, theta, dh, dz,
                  angle, x_axis, z_axis, case)
xyz = convert.sol_to_xyz(sol, n_steps, n_part)

# Animation
t = np.array([np.ones(n_part)*i for i in range(n_steps)]).flatten()
df = pd.DataFrame({'time': t , 'x': xyz[:, 0], 'y': xyz[:, 1], 'z': xyz[:, 2]})

def update_graph(num):
    data = df[df['time'] == num]
    graph.set_data(data.x, data.y)
    graph.set_3d_properties(data.z)
    title.set_text('step={}'.format(num))
    return title, graph

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
title = ax.set_title('')

data = df[df['time'] == 0]
graph, = ax.plot(data.x, data.y, data.z, linestyle="", marker="o")

anim = matplotlib.animation.FuncAnimation(fig, update_graph, n_steps,
                               interval=1, blit=False)

xx, yy, zz = convert.field_to_surface(num, x, z, m)
ax.contourf(xx, yy, zz, zdir='y', offset=0, alpha=0.25, cmap=cm.coolwarm)

ax.view_init(elev=0., azim=-90)
ax.set_xlabel('X')
ax.set_xlim(-20, 20)
ax.set_ylabel('Y')
ax.set_ylim(-10, 10)
ax.set_zlabel('Z')
ax.set_zlim(-10, 10)

plt.show()


import sys
import os
import glob
import cv2
import argparse
import json
import numpy as np
import pprint
import traceback

# classes_file is a text file which contains all classes(labels)
# Example classes.txt
"""
Bicycles_Only
Bumpy_Road
Buses_Priority
Centre_Line
Closed_To_Pedestrians
Crossroads
Dangerous_Wind _Gusts
Directions_Indicator
Falling_Rocks
Keep_Left
...
"""

class YOLO2COCOConverter:

  def __init__(self, classes_file):
    self.classes = []
    with open(classes_file, "r") as f:
      all_class_names = f.readlines()
      for class_name in all_class_names:
        class_name = class_name.strip()
        if class_name.startswith("#") ==False:
          self.classes.append(class_name)
    print("==== classes {}".format(self.classes))

  def run(self, dataset_dir, coco_json):
    print("=== YOLO2COCOConverter.run() start")
    NL = "\n"
    coco_annotation = {}
    info = {
      "description": "COCO 2022 Dataset",
      "url": "http://cocodataset.org",
      "version": 1.0,
      "year": 2022,
      "contributor": "antillia.com",
      "date_created": "2022/04/05"
    }
    license = [
        {
          "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/",
          "id":  1,
          "name": "Attribution-NonCommercial-ShareAlike License"
        },
    ]
    coco_annotation["info"]        = info
    coco_annotation["license"]     = license

    images, annotations = self.create_images_and_annotations_sections(dataset_dir)

    coco_annotation['images']      = images
    coco_annotation['annotations'] = annotations
    coco_annotation['categories']  = []

    for index, label in enumerate(self.classes):
      category = {
        "supercategory": "None",
        "id": index + 1,
        "name": label
      }
      coco_annotation['categories'].append(category)

    with open(coco_json, 'w') as outfile:
      json.dump(coco_annotation, outfile, indent=4)

    print("=== YOLO2COCOConverter.run() end")

  def create_images_and_annotations_sections(self, dataset_dir):
    # The dataset_dir(train, valid, or test) contains both something_image.jpg and something_image.txt , and so on.

    # We assume the following folder structure:
    # dataset_dir/train/
    #  +- image1.jpg
    #  +- image1.txt  (YOLO format annotation file to image1.jpg)
    #  ...
    #  ...
    #  +- imageN.jpg
    #  +- imageN.txt  (YOLO format annotation file to imageN.jpg)
    #
    annotations = []
    images      = []
    pattern     = dataset_dir + "/*.jpg"
    all_jpg_files   = glob.glob(pattern)

    image_id      = 0
    annotation_id = 1   # In COCO dataset format, you must start annotation id with '1'

    for jpg_file in all_jpg_files:
        # Check how many items have progressed
        print("==== YOLOtoCOCOConverter.run() processing  annotation_id {} filename {}".format(annotation_id, jpg_file))

        image = cv2.imread(jpg_file)

        annotation_file = jpg_file.replace(".jpg", ".txt")
        if os.path.exists(annotation_file) == False:
          raise Exception("Not found annotation file {}".format(annotation_file))

        annotation_f = open(annotation_file, "r")
        all_annotations  = annotation_f.readlines()
        annotation_f.close()

        image_height, image_width, _ = image.shape

        jpg_base_filename = os.path.basename(jpg_file)

        image = self.create_image_section(jpg_base_filename, image_width, image_height, image_id)
        images.append(image)

        image_height = float(image_height)
        image_width  = float(image_width)

        # YOLO format - (class_id, x_center, y_center, width, height)
        # where class_id starts from 0, and x_center, y_center, width, height are float, and in range [0.0, 1.0]

        # COCO format - (annotation_id, x_upper_left, y_upper_left, width, height)
        # where annotation_id starts from 1, and x_upper_left, y_upper_left, width, height are integer, and real coordinates of the bbox rectangle.

        for annotation in all_annotations:

            class_id, x_center, y_center, width, height = annotation.split(" ")

            category_id   = int(class_id) + 1
            x_center      = float(x_center)
            y_center      = float(y_center)
            width         = float(width)
            height        = float(height)

            real_x_center = int(x_center * image_width)
            real_y_center = int(y_center * image_height)
            real_width    = int(width    * image_width)
            real_height   = int(height   * image_height)

            real_x        = real_x_center - real_width/2
            real_y        = real_y_center - real_height/2

            annotation    = self.create_annotation_section(real_x, real_y, real_width, real_height, image_id, category_id, annotation_id)
            annotations.append(annotation)
            annotation_id += 1

        image_id += 1

    return images, annotations

  def create_image_section(self, file_name, width, height, image_id):
    image_section = {
        'file_name': file_name,
        'height':    height,
        'width':     width,
        'id':        image_id
    }
    return image_section

  def create_annotation_section(self, x, y, width, height, image_id, category_id, annotation_id):
    bbox = (x, y, width, height)
    area = width * height

    annotation_section = {
        'id': annotation_id,
        'image_id': image_id,
        'bbox': bbox,
        'area': area,
        'iscrowd': 0,
        'category_id': category_id,
        'segmentation': []
    }

    return annotation_section

# python YOLO2COCOConverter.py ./classes.txt ./train ./train/annotation.json
#
# python YOLO2COCOConverter.py ./classes.txt ./valid ./valid/annotation.json

if __name__ == '__main__':
  classes_file     = ""
  dataset_dir      = ""
  output_coco_json = ""
  try:
    if len(sys.argv) == 4:
      classes_file     = sys.argv[1]
      dataset_dir      = sys.argv[2]
      output_coco_json = sys.argv[3]
    else:
      raise Exception("Invalid argment")

    if os.path.exists(classes_file) == False:
      raise Exception("Not found classes files:{}".format(classes_file))
    if os.path.exists(dataset_dir) == False:
      raise Exception("Not found dataset_dir  :{}".format(dataset_dir))

    converter = YOLO2COCOConverter(classes_file)
    converter.run(dataset_dir, output_coco_json)

  except:
    traceback.print_exc()


from json import dumps, loads
from os import listdir

def get_json_files():
    """
    Function to get json files
    :return: json files from directory
    """
    return lambda filename: filename[-5:] == '.json', listdir("jsons/")

def read_json(file):
    """
    Function to read json data
    :param: file - json file to read
    :return: data - data from json file
    """
    with open("/jsons/" + file, "r") as json_file:
        data = json_file.read()
    try:
        data = dumps(loads(data))
    except ValueError as ve:
        print("Unable to parse file, json expected!")

    return data


#!/usr/bin/python3

import argparse
import datetime
import json
import logging
import os
import sdr.scanner

def config_logger(verbose, dir):
    params = {}

    levels = [logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG]
    level = levels[min(len(levels) - 1, verbose)]

    params["format"] = "[%(asctime)s][%(levelname)7s][%(name)6s] %(message)s"
    params["level"] = level
    params["datefmt"] = "%Y-%m-%d %H:%M:%S"

    if dir:
        now = datetime.datetime.now()
        os.makedirs("%s/%04d-%02d-%02d" % (dir, now.year, now.month, now.day), exist_ok=True)
        filename = "%s/%04d-%02d-%02d/%02d_%02d_%02d.txt" % (dir, now.year, now.month, now.day, now.hour, now.minute, now.second)
        params["filename"] = filename
    logging.basicConfig(**params)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("config", help="path to config file", type=str, metavar="file")
    parser.add_argument("-ld", "--log_directory", help="store output log in directory", type=str, metavar="dir")
    parser.add_argument("-wd", "--wav_directory", help="store output wav in directory", type=str, metavar="dir", default="wav")
    parser.add_argument("-dr", "--disable_recording", help="disable recording, only scannig", action="store_true")
    parser.add_argument("-pbf", "--print_best_frequencies", help="print n best frequencies", type=int, default=1, metavar="n")
    parser.add_argument("-fbf", "--filter_best_frequencies", help="filter printed n best frequencies", action="store_true")
    parser.add_argument("-v", "--verbose", action="count", default=0)
    args = parser.parse_args()

    config_logger(args.verbose, args.log_directory)
    with open(args.config) as f:
        config = json.load(f)
        ignored_frequencies_ranges = []
        if "ignored_frequencies_ranges" in config:
            ignored_frequencies_ranges = config["ignored_frequencies_ranges"]
        sdr.scanner.run(
            frequencies_ranges=config["frequencies_ranges"],
            ignored_frequencies_ranges=ignored_frequencies_ranges,
            ppm_error=int(config["device"]["ppm_error"]),
            tuner_gain=config["device"]["tuner_gain"],
            squelch=config["recording"]["squelch"],
            noise_level=config["scanning"]["noise_level"],
            bandwidth=config["scanning"]["bandwidth"],
            samples=config["scanning"]["samples"],
            fft=config["scanning"]["fft"],
            min_recording_time=config["recording"]["min_recording_time"],
            max_recording_time=config["recording"]["max_recording_time"],
            max_silence_time=config["recording"]["max_silence_time"],
            samples_rate=config["recording"]["samples_rate"],
            wav_directory=args.wav_directory,
            disable_recording=args.disable_recording,
            print_best_frequencies=args.print_best_frequencies,
            filter_best_frequencies=args.filter_best_frequencies,
        )


self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.send(
    """

            <title>Explorer</title>
            <link rel="stylesheet" href="/css" media="all">

        """)

try:
    import numpy as np
    from sklearn import metrics

    from sklearn.preprocessing import label_binarize
    from sklearn.multiclass import OneVsRestClassifier

    from sklearn.metrics import precision_recall_curve
    from sklearn.metrics import roc_curve
    from sklearn.metrics import average_precision_score

    import matplotlib.pyplot as plt

    import copy

    import debug

    for change_request in change_request_list:
        self.send('
%s
' % change_request.get_issue_map().get_universe_name())

        ml_debug_results = {}
        try:
            for key, value in change_request.get_machine_learning_model().calc_score().items():
                ml_debug_results[key] = value
        except Exception as e:
            self.send(debug.exception_html(e))
            continue

        model = change_request.get_machine_learning_model()

        X_train, y_train, X_test, y_test = ([],[],[],[])
        for X, y in model.get_dataset_training():
            X_train += X
            y_train += y

        for X, y in model.get_dataset_test():
            X_test += X
            y_test += y

        X_train, y_train, X_test, y_test = ( np.array(X_train), np.array(y_train), np.array(X_test), np.array(y_test))

        feature_names = model.get_feature_names_list()

        for result in sorted([{'name': key, **value} for key, value in ml_debug_results.items()], key=lambda x: 0 if x['average_proportional_score'] != x['average_proportional_score'] else -(x['average_proportional_score'] + x['interestingness'] / 4))[:2]:
            self.send('
%s
' % result['name'])

            classes_ = result['classes']
            n_classes = len(classes_)

            class_padding =  ['#PADDING#1#'] if n_classes == 2 else [] # when there are two classes, there will only be one column in the one-hot encoding. That breaks things.

            y_test_binarized = label_binarize(y_test, classes=classes_ + class_padding)

            if n_classes == 2:
                y_test_binarized = y_test_binarized[:,0:2]

            DV = result['DV']

            X_train_ = DV.transform(X_train)
            X_test_ = DV.transform(X_test)
            y_train_ = y_train

            if not result['scaler'] is None:
                X_train_ = result['scaler'].transform(X_train_)
                X_test_ = result['scaler'].transform(X_test_)

            if not result['sampler'] is None:
                X_train_, y_train_ = result['sampler'].fit_resample(X_train_, y_train)
                y_train_binarized = label_binarize(y_train_, classes=classes_)
            else:
                y_train_binarized = label_binarize(y_train_, classes=classes_)

            if not result['selector'] is None:
                X_train_ = result['selector'].transform(X_train_)
                X_test_ = result['selector'].transform(X_test_)

            if not result['reducer'] is None:
                X_train_ = result['reducer'].transform(X_train_)
                X_test_ = result['reducer'].transform(X_test_)

            y_pred = result['classifier'].predict(X_test_)

            cm = result['cm']
            report = metrics.classification_report(y_test, y_pred)

            self.send('Classes: %s
' % str(list(enumerate(classes_))))
            self.send('Interestingness: %s
' % str(result['interestingness']))
            self.send('Proportional score: %s%%
' % str(list(zip(classes_, result['proportional_score']))))
            self.send('Average proportional score: %s%%
' % str(result['average_proportional_score']))

            classifier = result['classifier']

            score = classifier.score(X_test_, y_test) * 100.0
            accuracy = metrics.accuracy_score(y_test, y_pred) * 100

            dimensionality = None
            d = None

            roc_score_ovr = -1
            roc_score_ovo = -1

            figfilename = TEMP_DIR + change_request.get_issue_map().get_universe_name() + result['name'] + '_1.png'
            figfilename_roc = TEMP_DIR + change_request.get_issue_map().get_universe_name() + result['name'] + '_roc.png'
            figfilename_3 = TEMP_DIR + change_request.get_issue_map().get_universe_name() + result['name'] + '_prec_recall.png'
            figfilename_4 = TEMP_DIR + change_request.get_issue_map().get_universe_name() + result['name'] + '_multi.png'

            try:
                y_prob = classifier.predict_proba(X_test_)

                try:
                    roc_score_ovr = metrics.roc_auc_score(y_test_binarized, y_prob, average='weighted', multi_class='ovr')
                    roc_score_ovo = metrics.roc_auc_score(y_test_binarized, y_prob, average='weighted', multi_class='ovo')
                except Exception as e:
                    self.send(debug.exception_html(e))

                try:
                    ovr = OneVsRestClassifier(copy.deepcopy(classifier))
                    ovr.fit(X_train_, y_train_)

                    y_score = np.nan_to_num(ovr.predict_proba(X_test_))

                    precision = dict()
                    recall = dict()
                    plt.figure()
                    for i in range(n_classes):
                        precision[i], recall[i], _ = precision_recall_curve(y_test_binarized[:, i], y_score[:, i])
                        plt.plot(recall[i], precision[i], lw=2, label=classes_[i])

                    plt.xlabel("recall")
                    plt.ylabel("precision")
                    plt.legend(loc="best")
                    plt.title("precision vs. recall curve")
                    plt.savefig(figfilename)

                    #
                    fpr = dict()
                    tpr = dict()
                    plt.figure()
                    for i in range(n_classes):
                        fpr[i], tpr[i], _ = roc_curve(y_test_binarized[:, i], y_score[:, i])
                        plt.plot(fpr[i], tpr[i], lw=2, label=classes_[i])

                    plt.xlabel("false positive rate")
                    plt.ylabel("true positive rate")
                    plt.legend(loc="best")
                    plt.title("ROC curve")
                    plt.savefig(figfilename_roc)

                except Exception as e:
                    self.send(debug.exception_html(e))

                try:
                    #
                    #try:
                    #    y_score = np.nan_to_num(ovr.decision_function(X_test_))
                    #except:
                    #    y_score = np.nan_to_num(ovr.predict_proba(X_test_))

                    precision = dict()
                    recall = dict()
                    average_precision = dict()

                    plt.figure()
                    for i in range(n_classes):
                        precision[i], recall[i], _ = precision_recall_curve(y_test_binarized[:, i], y_score[:, i])
                        average_precision[i] = average_precision_score(y_test_binarized[:, i], y_score[:, i])

                    # A "micro-average": quantifying score on all classes jointly
                    precision["micro"], recall["micro"], _ = precision_recall_curve(y_test_binarized.ravel(), y_score.ravel())
                    average_precision["micro"] = average_precision_score(y_test_binarized, y_score, average="micro")

                    plt.step(recall['micro'], precision['micro'], where='post')

                    plt.xlabel('Recall')
                    plt.ylabel('Precision')
                    plt.ylim([0.0, 1.05])
                    plt.xlim([0.0, 1.0])
                    plt.title(
                        'Average precision score, micro-averaged over all classes: AP={0:0.2f}'
                        .format(average_precision["micro"]))

                    plt.savefig(figfilename_3)

                    #
                    from itertools import cycle
                    # setup plot details
                    colors = cycle(['navy', 'turquoise', 'darkorange', 'cornflowerblue', 'teal'])

                    plt.figure(figsize=(7, 8))
                    f_scores = np.linspace(0.2, 0.8, num=4)
                    lines = []
                    labels = []
                    for f_score in f_scores:
                        x = np.linspace(0.01, 1)
                        y = f_score * x / (2 * x - f_score)
                        l, = plt.plot(x[y >= 0], y[y >= 0], color='gray', alpha=0.2)
                        plt.annotate('f1={0:0.1f}'.format(f_score), xy=(0.9, y[45] + 0.02))

                    lines.append(l)
                    labels.append('iso-f1 curves')
                    l, = plt.plot(recall["micro"], precision["micro"], color='gold', lw=2)
                    lines.append(l)
                    labels.append('micro-average Precision-recall (area = {0:0.2f})'.format(average_precision["micro"]))

                    for i, color in zip(range(n_classes), colors):
                        l, = plt.plot(recall[i], precision[i], color=color, lw=2)
                        lines.append(l)
                        labels.append('Precision-recall for class {0} (area = {1:0.2f})'
                                    ''.format(classes_[i], average_precision[i]))

                    fig = plt.gcf()
                    fig.subplots_adjust(bottom=0.25)
                    plt.xlim([0.0, 1.0])
                    plt.ylim([0.0, 1.05])
                    plt.xlabel('Recall')
                    plt.ylabel('Precision')
                    plt.title('Extension of Precision-Recall curve to multi-class')
                    plt.legend(lines, labels, loc=(0, -.38), prop=dict(size=14))
                    plt.savefig(figfilename_4)

                except Exception as e:
                    self.send(debug.exception_html(e))

            except Exception as e:
                    self.send(debug.exception_html(e))

            try:
                if 'coef_' in dir(classifier):
                    dimensionality = classifier.coef_.shape[1]
                    d = density(classifier.coef_)
            except Exception as e:
                dimensionality = None
                d = None

            self.send('''
                Score: {score:.2f}%

                ROC Area-Under-Curve (ovr): {roc_score_ovr:.2f}%

                ROC Area-Under-Curve (ovo): {roc_score_ovo:.2f}%

                Density: {density}

                Dimensionality: {dimensionality}

                Report:

{report}

                Confusion Matrix:

{cm}

                '''.format(
                    score = score,
                    roc_score_ovr = roc_score_ovr * 100,
                    roc_score_ovo = roc_score_ovo * 100,
                    density = html.escape(str(d)),
                    dimensionality = html.escape(str(dimensionality)),
                    report = html.escape(str(report)),
                    cm = html.escape(str(cm)),
                    figfilename = figfilename,
                    figfilename_roc = figfilename_roc,
                    figfilename_3 = figfilename_3,
                    figfilename_4 = figfilename_4
                )
            )

except Exception as e:
    self.send(debug.exception_html(e))

self.send('')

exports = {
}


from utils.CUI import cui
from services._neo4j import neo4j

def main():
    while True:
        choice = cui.neo_menu()

        if choice == 1:
            tags = input("Enter a tags, separated by a comma: ")
            users_with_tagged_messages = neo4j.get_users_with_tagged_messages(tags)
            cui.print_list("Users: ", users_with_tagged_messages)

        elif choice == 2:
            n = int(input("Enter the 'N' value: "))
            users_with_n_long_relations = neo4j.get_users_with_n_long_relations(n)
            cui.print_list("Users: ", users_with_n_long_relations)

        elif choice == 3:
            username1 = input("Enter username of the first user")
            username2 = input("Enter username of the second user")
            way = neo4j.shortest_way_between_users(username1, username2)
            cui.show_way(way)

        elif choice == 4:
            users_which_have_only_spam_conversation = neo4j.get_users_which_have_only_spam_conversation()
            cui.print_list("Users: ", users_which_have_only_spam_conversation)

        elif choice == 5:
            tags = input("Enter a tags, separated by a comma: ")
            unrelated_users_with_tagged_messages = neo4j.get_unrelated_users_with_tagged_messages(tags)
            cui.print_list("Users: ", unrelated_users_with_tagged_messages)

        elif choice == 6:
            break

        else:
            print("Wrong option selection. Enter any key to try again..")

if __name__ == '__main__':
    main()


import csv
from nltk import FreqDist, bigrams
from nltk.corpus import stopwords
import os
import pprint
import re
import string

# from
# www.datasciencebytes.com/bytes/
# 2014/11/04/filter-common-words-from-documents/
stopword_list = stopwords.words('english')
stopword_set = set(stopword_list)

def clean_input(input):
    input = re.sub("\n+", " ", input)
    input = re.sub(' +', " ", input)
    clean_input = list()
    input = input.split(' ')
    for item in input:
        item = item.strip(string.punctuation)
        if len(item) > 1 or (item.lower() == 'a' or item.lower() == 'i'):
            if item.lower() not in stopword_set:
                clean_input.append(item)
    return clean_input

def analyze_data(running_string):
    cleaned_input = clean_input(running_string)
    bigrams_output = bigrams(cleaned_input)
    bigrams_dist = FreqDist(bigrams_output)
    return bigrams_dist

def concat_csv():
    running_string = ""
    for fname in os.listdir():
        if 'csv' in fname:
            print("Found {0}".format(fname))
            with open(fname, newline='') as csvfile:
                rdr = csv.reader(csvfile, delimiter=',', quotechar='"')
                for row in rdr:
                    try:
                        print(row[2])
                        running_string += " {0}".format(row[2])
                    except:
                        pass
    return running_string

if __name__ == "__main__":
    ppp = pprint.PrettyPrinter()
    running_string = concat_csv()
    print("Running String Length: {0}".format(len(running_string)))
    bigrams_dist = analyze_data(running_string)
    ppp.pprint(bigrams_dist.most_common(20))


#!/usr/bin/python3
from __future__ import print_function
import re, itertools

class Ingredient(object):
    def __init__(self, description):
        ingredient_re = r"(\w+): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)"
        parse = re.match(ingredient_re, description).groups()
        self.name       =     parse[0]
        self.capacity   = int(parse[1])
        self.durability = int(parse[2])
        self.flavor     = int(parse[3])
        self.texture    = int(parse[4])
        self.calories   = int(parse[5])

def getCalories(ingredients_list):
    return sum(l[0]*l[1].calories for l in ingredients_list)

def getScore(ingredients_list):
    capacity    = max(0, sum(l[0]*l[1].capacity     for l in ingredients_list))
    durability  = max(0, sum(l[0]*l[1].durability   for l in ingredients_list))
    flavor      = max(0, sum(l[0]*l[1].flavor       for l in ingredients_list))
    texture     = max(0, sum(l[0]*l[1].texture      for l in ingredients_list))

    return capacity * durability * flavor * texture

# That's handy, the Advent of Code gives unittests.
def UnitTest():
    ex = """Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3"""

    print("Unit test for Part One.")
    print("Test {inp} gives {res}".format(inp=ex, res=partOne(ex.strip())))

    print("")
    print("Unit test for Part Two.")
    print("Test {inp} gives {res}".format(inp=ex, res=partTwo(ex)))

def partOne(inp):
    ingredients = list(map(Ingredient, inp.split('\n')))
    def _genScores():
        for prod in itertools.product(*([range(100)]*(len(ingredients)-1))):
            if sum(prod) > 100:
                continue
            else:
                last_qty = 100-sum(prod)
                recipe = list(zip(list(prod)+[last_qty], ingredients))
                yield getScore(recipe)
    return max(_genScores())

def partTwo(inp):
    ingredients = list(map(Ingredient, inp.split('\n')))
    def _genScores():
        for prod in itertools.product(*([range(100)]*(len(ingredients)-1))):
            if sum(prod) > 100:
                continue
            else:
                last_qty = 100-sum(prod)
                recipe = list(zip(list(prod)+[last_qty], ingredients))
                if getCalories(recipe) == 500:
                    yield getScore(recipe)
    return max(_genScores())

if __name__ == '__main__':
    from argparse import ArgumentParser, FileType

    args = ArgumentParser()
    args.add_argument("-t", "--test", help='Unit tests', action='store_true')
    args.add_argument("-i", "--input", help='Your input file', type=FileType('r'))
    options = args.parse_args()

    if options.test:
        UnitTest()
    if options.input:
        inp = options.input.read().strip()
        print("Answer for part one is : {res}".format(res=partOne(inp)))
        print("Answer for part two is : {res}".format(res=partTwo(inp)))


import numpy as np
import scipy.io as scio
from .image_dataset import ImageDataset

class SynthDataset(ImageDataset):
    r'''Dataset reading from images.
    Args:
        Processes: A series of Callable object, which accept as parameter and return the data dict,
            typically inherrited the `DataProcess`(data/processes/data_process.py) class.
    '''

    def __init__(self, data_dir=None, data_list=None, cmd={}, **kwargs):
        self.load_all(**kwargs)
        self.data_dir = data_dir or self.data_dir
        self.data_list = data_list or self.data_list

        self.is_training = True

        self.debug = cmd.get('debug', False)

        self.image_paths = []
        self.targets = []
        self.get_all_samples()

    def get_all_samples(self):
        for i in range(len(self.data_dir)):
            data = scio.loadmat(self.data_list[i])

            img_paths = list(data['imnames'][0])
            gts = list(data['wordBB'][0])
            texts = list(data['txt'][0])

            img_paths=[self.data_dir[i]+'/img/'+timg[0].strip() for timg in img_paths]
            self.image_paths += img_paths

            for gt, text in zip(gts,texts):
                bboxes, words = self.load_ann(gt, text)

                lines = []
                for k in range(bboxes.shape[0]):
                    item = {
                        'poly':bboxes[k].tolist(),
                        'text':words[k]
                    }
                    lines.append(item)
                self.targets.append(lines)
        self.num_samples = len(self.image_paths)

    def load_ann(self, gt, text):
        bboxes = np.array(gt)
        bboxes = np.reshape(bboxes, (bboxes.shape[0], bboxes.shape[1], -1))
        bboxes = bboxes.transpose(2, 1, 0)
        bboxes = np.round(bboxes)

        words = []
        for text_ in text:
            text_ = text_.replace('\n', ' ').replace('\r', ' ')
            words.extend([w for w in text_.split(' ') if len(w) > 0])

        return bboxes, words


"""
Code for implementing a python controller for Texas Instruments DLPLCR6500.
This code is based on the following project (https://github.com/csi-dcsc/Pycrafter6500 , by Paolo Pozzi).

Here we define the hardware class to be integrated in a ScopeFoundry app.

-Michele Castriotta (@mikics on github), MSc at Politecnico di Milano.
-Andrea Bassi;
-Gianmaria Calisesi;

12/12/18
"""
from threading import Thread
from ScopeFoundry import HardwareComponent
from qtpy import QtCore, QtWidgets
from TexasInstrumentsDMD_ScopeFoundry.DMDDeviceHID import TexasInstrumentsDmdDeviceHID
import numpy
import os
import sys
import PIL.Image
import tifffile as tiff

class TexasInstrumentsDmdHW(HardwareComponent):

    name = "TexasInstrumentsDmdHW"

    def setup(self):

        self.mode = self.add_logged_quantity("mode", dtype = int, si = False, ro = 0,
                                             initial = 3, vmin = 0, vmax = 3)
        self.exposure = self.add_logged_quantity("exposure", dtype = int, si = False, ro = 0,
                                                 initial = 1000000, vmin = 1, spinbox_step = 50000, unit = "us") #the spinbox_step does not work since it only works with float
        self.dark_time = self.add_logged_quantity("dark_time", dtype = int, si = False, ro = 0,
                                                 initial = 0, vmin = 0, unit = "us")
        self.bit_depth = self.add_logged_quantity("bit_depth", dtype = int, ro = 1, initial = 1)
        self.trigger_input = self.add_logged_quantity("trigger_input", dtype = bool, si = False, ro = 0,
                                                      initial = False)
        self.trigger_output = self.add_logged_quantity("trigger_output", dtype = bool, si = False, ro = 0,
                                                      initial = True)
        self.pattern_num = self.settings.New(name='pattern_num', dtype=int, ro=False)
        self.select_frames = self.add_logged_quantity('select_frames', dtype=bool, initial=False)
        self.first_frame = self.settings.New(name='first_frame', dtype=int, initial=0,
                                             vmin=0, spinbox_step=1, ro=False, reread_from_hardware_after_write=True)
        self.last_frame = self.settings.New(name='last_frame', dtype=int, initial=0,
                                             vmin=0, spinbox_step=1, ro=False, reread_from_hardware_after_write=True)

        self.file_path = self.add_logged_quantity("file_path", dtype = 'file', is_dir = False,
                                               initial = "D:\\LabPrograms\\ScopeFoundry_POLIMI\\DMD_Pattern\\Calibration_pattern\\Periodic Pattern\\modulated_lightsheet_32.png")

        self.add_operation("browser", self.file_browser)
        self.add_operation("encode", self.encode_sequence)
        self.add_operation("load_pattern", self.load_sequence) #self.load_sequence_threaded_mode)
        self.add_operation("start_pattern", self.start_sequence)
        self.add_operation("pause_pattern", self.pause_sequence)
        self.add_operation("stop_pattern", self.stop_sequence)

    def connect(self):

        self.dmd = TexasInstrumentsDmdDeviceHID()
        self.dmd.stopsequence()

        self.mode.hardware_set_func = self.dmd.changemode(self.mode.val)
        t = Thread(target=self.load_start_stop)
        t.start()

    def disconnect(self):

        if hasattr(self, 'dmd'):
            #self.dmd.stopsequence()
            del self.dmd

        for lq in self.settings.as_list():
            lq.hardware_read_func = None
            lq.hardware_set_func = None

    @QtCore.Slot()
    def load_sequence_threaded_mode(self):

        t = Thread(target=self.load_sequence)
        t.start()

    def load_start_stop(self):

        self.load_sequence()
        self.stop_sequence()
        self.start_sequence()
        self.stop_sequence()

    def encode_sequence(self):
        filename = self.settings['file_path']
        images = list(tiff.imread(filename))
        self.dmd.save_encoded_sequence(images, filename)
        print("****************\n\nEncoded image has been saved\n\n****************")

    # def load_sequence(self):

    #     images=[]

    #     file_path = os.fsdecode(self.file_path.val)
    #     arr = numpy.array(PIL.Image.open(file_path), dtype = numpy.bool)
    #     images.append(arr)

    #     self.settings['pattern_num'] = len(images)
    #     print("Number of patterns: ", self.settings['pattern_num'])
    #     fframe = self.first_frame.val
    #     lframe = self.last_frame.val
    #     if self.settings['select_frames']:
    #         images = images[fframe:lframe+1]

    #     exposure=[self.exposure.val]*len(images)
    #     dark_time=[self.dark_time.val]*len(images)
    #     trigger_input=[self.trigger_input.val]*len(images)
    #     trigger_output=[self.trigger_output.val]*len(images)

    #     self.dmd.defsequence(images,exposure,trigger_input,dark_time,trigger_output,0)
    #     print("****************\n\nStop Loading sequence!\n\n****************")

# # Elena
#     def load_sequence(self):

#         filename = self.file_path.val
#         images = list(tiff.imread(filename)) # to load 3D stacks

#       # This works only for 2D patterns:
#       # images=[]
#       # file_path = os.fsdecode(self.file_path.val)
#       # arr = numpy.array(PIL.Image.open(file_path), dtype = numpy.bool)
#       # images.append(arr)

#         self.settings['pattern_num'] = len(images)
#         print("Number of patterns: ", self.settings['pattern_num'])
#         fframe = self.first_frame.val
#         lframe = self.last_frame.val
#         if self.settings['select_frames']:
#             images = images[fframe:lframe+1]

#         exposure=[self.exposure.val]*len(images)
#         dark_time=[self.dark_time.val]*len(images)
#         trigger_input=[self.trigger_input.val]*len(images)
#         trigger_output=[self.trigger_output.val]*len(images)

#         self.dmd.defsequence(images,exposure,trigger_input,dark_time,trigger_output,0)
#         print("****************\n\nStop Loading sequence!\n\n****************")

# #New:
    def load_sequence(self):

        filename = self.file_path.val

        file = os.fsdecode(filename)
        if file.endswith(".tif"):
            images = list(tiff.imread(filename)) # to load 3D stacks

            self.settings['pattern_num'] = len(images)
            print("Number of patterns: ", self.settings['pattern_num'])
            fframe = self.first_frame.val
            lframe = self.last_frame.val
            if self.settings['select_frames']:
                images = images[fframe:lframe+1]

            exposure=[self.exposure.val]*len(images)
            dark_time=[self.dark_time.val]*len(images)
            trigger_input=[self.trigger_input.val]*len(images)
            trigger_output=[self.trigger_output.val]*len(images)

            self.dmd.defsequence(images,exposure,trigger_input,dark_time,trigger_output,0)
            print("****************\n\nStop Loading sequence!\n\n****************")

        elif file.endswith(".encd"):
            exposure = [self.exposure.val]
            dark_time = [self.dark_time.val]
            trigger_input = [self.trigger_input.val]
            trigger_output = [self.trigger_output.val]

            self.dmd.def_sequence_by_file(filename,exposure,trigger_input,dark_time,trigger_output,0)
            print("****************\n\nStop Loading sequence!\n\n****************")

    #@QtCore.Slot()
    def start_sequence(self):

        self.dmd.startsequence()
        print("****************\n\nThe sequence starts!\n\n****************")

    #@QtCore.Slot()
    def pause_sequence(self):

        self.dmd.pausesequence()
        print("****************\n\nThe sequence pauses!\n\n****************")

    #@QtCore.Slot()
    def stop_sequence(self):

        self.dmd.stopsequence()
        print("****************\n\nThe sequence stops!\n\n****************")

    #@QtCore.Slot()
    def file_browser(self):
        """
        Opens a dialog when click on browser, and update the value of the directory
        from which fetching patterns.
        """

        if self.file_path.is_dir:
            fname = QtWidgets.QFileDialog.getExistingDirectory(directory = self.file_path.val)
        else:
            fname, _ = QtWidgets.QFileDialog.getOpenFileName(directory = self.file_path.val)

        self.file_path.log.debug(repr(fname))
        self.file_path.update_value(fname)


import socket  # funkcje gniazd sieciowych
import threading  # wątki
import datetime  # czas do zapisu w logach rozmów
import signal
import sys
import time

class FORMAT:  # formatowanie tekstu
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    IDK = '\033[90m'
    BOLD = '\033[1m'
    END = '\033[0m'
    NOCOLOR = '\x1b[0m'

class SRV:  # klasa serwera z definicjami metod uzywanych przez serwer
    clients = {}  # slownik klientów
    addresses = {}  # słownik adresów
    threads = []
    kill_threads = False
    HOST = ''
    PORT = 33000
    BUFSIZ = 1024
    ADDR = (HOST, PORT)
    SERVER = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # stworzenie gniazda IPv4 z protokołem TCP
    SERVER.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # ustawienie opcji gniazda
    print(FORMAT.GREEN, "Socket created!", FORMAT.END)

    def __init__(self):  # konstruktor do bindowania gniazda
        self.SERVER.bind(self.ADDR)
        print(FORMAT.GREEN, "Socket bounded!", FORMAT.END)

    def __del__(self):
        self.SERVER.close()  # zamykanie serwera
        for client in self.clients:  # kończenie połączenia z klientami

            client.close()

    def get_clients(self):  # metoda zwracająca klientów
        return self.clients

    def add_client(self, client, name):  # metoda przypisująca klientom nazwy
        self.clients[client] = name

    def accept_conn(self):  # akceptowanie nowych połączeń
        while True:
            client, client_address = self.SERVER.accept()
            # print(FORMAT.IDK, "%s:%s joined!" % client_address, FORMAT.END)
            print(FORMAT.IDK, "'{}' joined!".format(client_address), FORMAT.END)  # rozwiazanie błędu Too few arguments
            # for format string
            client.send(bytes("!@#Enter your username: ", "utf8"))
            self.addresses[client] = client_address  # przypisanie do klienta jego adresu IP
            # threading.Thread(target=self.handle_client, args=(client,)).start()  # wystartowanie wątku wywołującego
            t = threading.Thread(target=self.handle_client, args=(client, lambda: self.kill_threads))
            self.threads.append(t)
            t.start()
            # metodę handle_client i przekazującego mu argument client

    def handle_client(self, client, stop):  # obsluga calego polaczenia z klientem
        # poniższy fragment wykonuje się tylko raz po dolączeniu klienta do serwera
        name = client.recv(self.BUFSIZ).decode("utf8")  # wpisanie swojej nazwy przez użytkownika
        welcome = '#@!Hi %s! Type !q to quit.' % name
        client.send(bytes(welcome, "utf8"))
        msg = "%s joined the chat!" % name
        f.write("%s joined the chat\n" % name)  # zapisanie do txt dołączenia klienta
        print(FORMAT.BLUE, "%s joined the chat" % name, FORMAT.END)
        self.broadcast(bytes(msg, "utf8"))
        self.clients[client] = name

        while True:
            try:
                # wykonywanie ciągle w pętli
                client.settimeout(3.0)
                msg = client.recv(self.BUFSIZ)  # odebranie wiadomości
                if not msg:
                    break
                if msg.decode("utf8")[0] == '@':  # jezeli po zdekodowaniu pierwszym znakiem jest @ to wiadomość priv
                    self.send_to_user(msg, name + ": ")
                elif msg != bytes("!q", "utf8"):
                    self.broadcast(msg, name + ": ", client)
                else:
                    # pozostaly warunek to !q czyli wyjście klienta z serwera
                    self.broadcast(bytes("%s left the chat :c" % name, "utf8"))
                    client.close()
                    print(FORMAT.BLUE, "%s left the chat" % name, FORMAT.END)
                    f.write("%s left the chat\n" % name)
                    del self.clients[client]  # kasowanie klienta ze słownika
                    break
            except socket.timeout:
                if stop():
                    break
                else:
                    continue
            except Exception as inst:
                print(FORMAT.IDK + "Connection with client lost" + FORMAT.END)
                del self.clients[client]

    def broadcast(self, msg, prefix="", poster=None):  # prefix = nazwa + :
        for sock in self.clients:
            if sock != poster:  # dla wszystkich gniazd
                sock.send(bytes(prefix, "utf8") + msg)
                tmp1 = msg.decode("utf8")
                txt = (prefix.split(':', 1)[0] + " to all: " + tmp1 + "\n")
                if txt.split(":", 2)[0] != "to all":  # żeby nie wypisywało wiadomości n razy (n liczba klientów)
                    print(" " + prefix.split(':', 1)[0] + " to all: " + tmp1)
                    f.write(txt)

    def send_to_user(self, msg, prefix=""):  # wiadomość prywatna
        tmp = msg.decode().split(" ", 1)[0]  # dzieli wiadomość po spacjach i przypisuje do tmp @nazwa
        tmp = tmp.replace('@', '')  # zastępuje @ niczym
        for client in self.clients:
            if self.clients[client] == tmp:  # szuka klienta o uzytej nazwie
                client.send(bytes(prefix, "utf8") + msg)
                tmp1 = msg.decode("utf8")
                print(" " + prefix.split(':', 1)[0] + " to " + tmp + ": " + tmp1)
                txt = (prefix.split(':', 1)[0] + " to " + tmp + ": " + tmp1 + "\n")
                f.write(txt)

    def exit(self):
        self.kill_threads = True  # ubijanie wątków
        self.broadcast(bytes("!q", "utf-8"))
        for t in self.threads:
            t.join()  # dołączanie wątków pobocznych do głównego

srv = SRV()

def signal_handler(sig, frame):
    animation = "|/-\\"
    i = 0
    while i != 7:
        print(FORMAT.YELLOW + FORMAT.BOLD + "Exiting... " + FORMAT.END + animation[i % len(animation)], end="\r"),
        i = i + 1
        time.sleep(0.2)
    global srv  # definicja zmiennej globalnej srv
    srv.exit()
    del srv  # zamykanie serwera
    sys.exit(0)

if __name__ == "__main__":  # definicja zmiennych interpretera python (plik źródłowy jako program glówny)
    signal.signal(signal.SIGINT, signal_handler)
    srv.SERVER.listen(5)  # podłączenie maksymalnie 5 użytkowników do serwera
    f = open("chat_log.txt", "w")  # utworzenie txt z prawem zapisu do pliku
    now = datetime.datetime.now()  # ustawienie czasu na chwilę obecną
    f.write(now.strftime("%Y-%m-%d %H:%M:%S\n"))
    print(FORMAT.YELLOW, "Waiting for connection...", FORMAT.END)
    srv.accept_conn()
    # ACCEPT_THREAD = threading.Thread(target=srv.accept_conn())  # akceptacja i wystartowanie wątku z accept_conn
    # ACCEPT_THREAD.start()
    while input == "!quit":
        f.close()  # zamknięcie pliku
        del srv


from django.urls import path

from . import views

app_name = 'main'
urlpatterns = [
    # Homepage.
    path('', views.index, name='index'),
    # Assets
    path('assets/', views.assets, name='assets'),
    path('assets/<int:asset_id>/', views.asset, name='asset'),
    path('assets/<int:pk>/change', views.AssetUpdateView.as_view(), name='change_asset'),
    path('assets/add/', views.AssetCreateView.as_view(), name='add_asset'),
]


from aiogram import Dispatcher, types
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Command

from tgbot.misc.states import UserInfo

async def user_info(message: types.Message):
    await message.answer("Введи Имя")
    await UserInfo.Name.set()

async def answer_name(message: types.Message, state: FSMContext):
    answer = message.text
    # async with state.proxy() as data:
    #     data["answer1"] = answer
    await state.update_data(answer1=answer)
    await message.answer("Введи email")
    await UserInfo.Email.set()

async def answer_email(message: types.Message, state: FSMContext):
    answer = message.text
    # async with state.proxy() as data:
    #     data["answer1"] = answer
    await state.update_data(answer2=answer)
    await message.answer("Введи телефон")
    await UserInfo.Phone.set()

async def answer_phone(message: types.Message, state: FSMContext):
    data = await state.get_data()
    answer1 = data.get('answer1')
    answer2 = data.get('answer2')
    answer3 = message.text
    await message.answer(f'Привет! Ты ввел следующие данные:\n'
                         f'Имя: {answer1}\n'
                         f'Email: {answer2}\n'
                         f'Телефон: {answer3}')
    # await state.finish()
    await state.reset_state(with_data=True)

def register_info_user(dp: Dispatcher):
    dp.register_message_handler(user_info, Command("form"))
    dp.register_message_handler(answer_name, state=UserInfo.Name)
    dp.register_message_handler(answer_email, state=UserInfo.Email)
    dp.register_message_handler(answer_phone, state=UserInfo.Phone)


import pygame
import Assets.sounds as sounds

class PauseButton:
    def __init__(self, game):
        self.game       = game
        self.player     = game.player
        self.monsters   = game.monsters.sprites()

        self.image = pygame.image.load("Assets/Images/Buttons/pause.png")
        self.rect = self.image.get_rect()
        self.rect.x = game.SCREEN_WIDTH - self.rect.width - 30
        self.rect.y = 15

        self.clicked = False
        self.hide = False

    def handle_events(self, event):
        if not self.hide and not self.game.end_game:
            """
            Upon clicking the pause button we store prior velocities for certain sprites and set their velcoity to 0 or pause them
            """

            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                mouse_pos = pygame.mouse.get_pos()
                if self.rect.collidepoint(mouse_pos):

                    self.player.prior_y_velocity = self.player.velocity_y
                    self.player.velocity_y = 0
                    self.game.player.paused = True

                    #We must store the monster's speed so upon resuming the monster continues at the same rate prior to pausing
                    for monster in self.game.monsters:
                        monster.pause()

                    #simply pausing ufo
                    for ufo in self.game.UFOs:
                        ufo.paused = True

                    pygame.mixer.pause()
                    self.clicked = True

            elif event.type == pygame.MOUSEBUTTONUP and event.button == 1 and self.clicked:
                #When the user releases the left click we hide the button and show the resume button
                self.game.resume_button.hide = False
                self.hide = True
                self.clicked = False
                sounds.button.play()

    def draw(self, screen):
        if not self.hide:
            screen.blit(self.image, (self.rect.x, self.rect.y))


#!/usr/bin/python3
from __future__ import print_function
import re

#Syntax looks close to something you might do in python, so let's use exec!

def initVariables(instructions):
    """Get all variables in program, and return a dictionary of varname: 0."""
    get_var_names = r"^(\w+) \w+ [0-9+-]+ if (\w+)"
    all_used_vars = dict()
    for step in instructions:
        var1, var2 = re.match(get_var_names, step).groups()
        all_used_vars[var1] = 0
        all_used_vars[var2] = 0
    return all_used_vars

def formatInstructions(instructions):
    """Yield each instruction, so that they are suitable for exec.

    Variables are assumed to be hold in a dictionary named 'registers'.
    It will transform, for example,
        'a dec -10 if b != 5'
    by the instruction
        'registers['a'] -= -10 if registers['b'] != 5 else 0'
    """
    format_re = r"^(\w+) (\w+ [0-9+-]+ if) (\w+) (.*)"
    formatted_re = r"registers['\1'] \2 registers['\3'] \4"
    for step in instructions:
        registered_step = re.sub(format_re, formatted_re, step)
        registered_step = registered_step.replace(' inc ', ' += ') #Use spaces around inc, to avoid translate a register 'inc'.
        registered_step = registered_step.replace(' dec ', ' -= ')
        yield registered_step + " else 0"

def runInstructions(instructions):
    """Returns a dict of register:values after running the list of string instructions."""
    registers = initVariables(instructions)
    for line in formatInstructions(instructions):
        exec(line)
    return registers

def genHighestValues(instructions):
    """Yield highest value of registers at each step."""
    registers = initVariables(instructions)
    for line in formatInstructions(instructions):
        exec(line)
        yield max(registers.values())

# That's handy, the Advent of Code gives unittests.
def UnitTest():
    example="""b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10
"""

    print("Unit testing part one.")
    print("Highest register value is {res}".format(res=partOne(example)))
    print("All register values are {didi}".format(didi=runInstructions(example.strip().split('\n'))))

    print("")
    print("Unit testing part two.")
    print("Highest values at each step are {vals}".format(vals=list(genHighestValues(example.strip().split('\n')))))
    print("Highest ever value is {res}".format(res=partTwo(example)))

def partOne(inp):
    instructions = inp.strip().split('\n')
    registers = runInstructions(instructions)
    return max(registers.values())

def partTwo(inp):
    instructions = inp.strip().split('\n')
    return max(genHighestValues(instructions))

if __name__ == '__main__':
    from argparse import ArgumentParser, FileType

    args = ArgumentParser()
    args.add_argument("-t", "--test", help='Unit tests', action='store_true')
    args.add_argument("-i", "--input", help='Your input file', type=FileType('r'))
    options = args.parse_args()

    if options.test:
        UnitTest()
    if options.input:
        inp = options.input.read()
        print("Answer for part one is : {res}".format(res=partOne(inp)))
        print("Answer for part two is : {res}".format(res=partTwo(inp)))


import argparse
import logging
import os.path
import random
import re
import sys

from . import M209Error
from .converter import M209_ALPHABET_SET
from .data import KEY_WHEEL_DATA
from .keylist.generate import generate_key_list
from .keylist.key_list import valid_indicator, IndicatorIter
from .keylist.config import write as write_config, read_key_list
from .procedure import StdProcedure

DESC = "M-209 simulator and utility program"
DEFAULT_KEY_LIST = 'm209keys.cfg'
LOG_CHOICES = ['debug', 'info', 'warning', 'error', 'critical']
SYS_IND_RE = re.compile(r'^[A-Z]{1}$')
M209_ALPHABET_LOWER = set(c.lower() for c in M209_ALPHABET_SET)

def validate_key_list_indicator(s):
    """Validation/conversion function for validating the supplied starting key
    list indicator.

    Returns the string value if valid, otherwise raises an ArgumentTypeError.

    """
    if len(s) == 2:
        s = s.upper()
        if valid_indicator(s):
            return s

    raise argparse.ArgumentTypeError('must be in the range AA-ZZ')

def validate_num_key_lists(s):
    """Validation/conversion function for validating the number of key lists to
    generate.

    Returns the integer value if valid, otherwise raises an ArgumentTypeError

    """
    bounds = (1, 26 ** 2)
    msg = "value must be in range {}-{}".format(*bounds)
    try:
        val = int(s)
    except ValueError:
        raise argparse.ArgumentTypeError(msg)

    if not (bounds[0] <= val <= bounds[1]):
        raise argparse.ArgumentTypeError(msg)
    return val

def validate_ext_indicator(s):
    """Validation function for the external message indicator option.

    Returns the string value if valid, otherwise raises an ArgumentTypeError.

    """
    if len(s) == 6:
        s = s.upper()
        for n, c in enumerate(s):
            if c not in KEY_WHEEL_DATA[n][0]:
                break
        else:
            return s

    raise argparse.ArgumentTypeError(
        "{} is not a valid external message indicator".format(s))

def validate_sys_indicator(s):
    """Validation function for the system message indicator option.

    Returns the string value if valid, otherwise raises an ArgumentTypeError.

    """
    if len(s) == 1:
        s = s.upper()
        if s in M209_ALPHABET_SET:
            return s

    raise argparse.ArgumentTypeError('value must be 1 letter')

def plaintext_filter(fp):
    """Generator function to filter input plaintext.

    * upper case letters are passed as-is.
    * lower case letters are converted to upper case.
    * Whitespace characters are converted to 'Z'.
    * All other characters are dropped from the input.

    """
    for line in fp:
        for c in line:
            if c in M209_ALPHABET_SET:
                yield c
            elif c in M209_ALPHABET_LOWER:
                yield c.upper()
            elif c.isspace():
                yield 'Z'

def encrypt(args):
    """Encrypt subcommand processor"""
    logging.info("Encrypting using key file %s", args.key_file)

    if args.text and args.file:
        sys.exit("Please supply either -f/--file or -t/--text, not both\n")
    elif not args.text and not args.file:
        sys.exit("Please supply either -f/--file or -t/--text\n")

    # Get a key list from the key list file
    if not os.path.isfile(args.key_file):
        sys.exit("key list file not found: {}\n".format(args.key_file))

    key_list = read_key_list(args.key_file, args.key_list_ind)
    if not key_list:
        sys.exit("key list not found in file: {}\n".format(args.key_file))

    # Get the plaintext
    if args.text:
        plaintext = plaintext_filter(args.text)
    else:
        infile = open(args.file, 'r') if args.file != '-' else sys.stdin
        plaintext = plaintext_filter(infile)

    proc = StdProcedure(key_list=key_list)
    ct = proc.encrypt(plaintext, ext_msg_ind=args.ext_ind, sys_ind=args.sys_ind)
    print(ct)

def decrypt(args):
    """Decrypt subcommand processor"""
    logging.info("Decrypting using key file %s", args.key_file)

    if args.text and args.file:
        sys.exit("Please supply either -f/--file or -t/--text, not both\n")
    elif not args.text and not args.file:
        sys.exit("Please supply either -f/--file or -t/--text\n")

    # Check for key list file
    if not os.path.isfile(args.key_file):
        sys.exit("key list file not found: {}\n".format(args.key_file))

    # Get the ciphertext to decrypt
    if args.text:
        msg = args.text
    else:
        # Read contents of ciphertext file
        if args.file == '-':
            msg = sys.stdin.read()
        else:
            with open(args.file, 'r') as fp:
                msg = fp.read()
        msg = msg.strip()

    # Start the decrypt procedure
    proc = StdProcedure()
    params = proc.set_decrypt_message(msg)

    # Find a key list for the message
    key_list = read_key_list(args.key_file, params.key_list_ind)
    if key_list is None:
        sys.exit("Could not find key list {} in {}\n".format(
            params.key_list_ind, args.key_file))

    # Install the key list and perform the decrypt operation
    proc.set_key_list(key_list)
    plaintext = proc.decrypt()
    print(plaintext)

def keygen(args):
    """Key list generation subcommand processor"""
    logging.info("Creating key list file: %s", args.key_file)

    if not args.overwrite and os.path.exists(args.key_file):
        sys.exit("File '{}' exists. Use -o to overwrite\n".format(args.key_file))

    if args.start is None:   # random indicators
        indicators = random.sample([i for i in IndicatorIter()], args.number)
        indicators.sort()
    else:
        it = IndicatorIter(args.start)
        n = len(it)
        if n < args.number:
            sys.exit("Error: can only produce {} key lists when starting at {}\n".format(
                n, args.start))

        indicators = (next(it) for n in range(args.number))

    key_lists = (generate_key_list(indicator) for indicator in indicators)

    write_config(args.key_file, key_lists)

def main(argv=None):
    """Entry point for the m209 command-line utility."""

    # create the top-level parser
    parser = argparse.ArgumentParser(description=DESC)
    parser.add_argument('-l', '--log', choices=LOG_CHOICES, default='warning',
        help='set log level [default: %(default)s]')
    subparsers = parser.add_subparsers(title='list of commands',
        description='type %(prog)s {command} -h for help on {command}')

    # create the sub-parser for encrypt
    enc_parser = subparsers.add_parser('encrypt', aliases=['enc'],
        description='Encrypt text from a file or command-line',
        help='encrypt text from file or command-line',
        epilog='Either the -f/--file or -t/--text arguments must be supplied')
    enc_parser.add_argument('-z', '--key-file', default=DEFAULT_KEY_LIST,
        help='path to key list file [default: %(default)s]')
    enc_parser.add_argument('-f', '--file',
        help='path to plaintext file or - for stdin')
    enc_parser.add_argument('-t', '--text',
        help='text string to encrypt')
    enc_parser.add_argument('-k', '--key-list-ind', metavar='XX',
        type=validate_key_list_indicator,
        help='2-letter key list indicator; if omitted a random one is used')
    enc_parser.add_argument('-e', '--ext-ind', metavar='ABCDEF',
        type=validate_ext_indicator,
        help='6-letter external message indicator; if omitted a random one is used')
    enc_parser.add_argument('-s', '--sys-ind', metavar='S',
        type=validate_sys_indicator,
        help='1-letter system indicator; if omitted a random one is used')
    enc_parser.set_defaults(subcommand=encrypt)

    # create the sub-parser for decrypt
    dec_parser = subparsers.add_parser('decrypt', aliases=['dec'],
        description='Decrypt text from a file or command-line',
        help='decrypt text from file or command-line',
        epilog='Either the -f/--file or -t/--text arguments must be supplied')
    dec_parser.add_argument('-z', '--key-file', default=DEFAULT_KEY_LIST,
        help='path to key list file [default: %(default)s]')
    dec_parser.add_argument('-f', '--file',
        help='path to ciphertext file or - for stdin')
    dec_parser.add_argument('-t', '--text',
        help='text string to decrypt')
    dec_parser.set_defaults(subcommand=decrypt)

    # create the sub-parser for generating key lists

    kg_parser = subparsers.add_parser('keygen', aliases=['key'],
        description='Generate key list file',
        help='generate key list')
    kg_parser.add_argument('-z', '--key-file', default=DEFAULT_KEY_LIST,
        help='path to key list file [default: %(default)s]')
    kg_parser.add_argument('-o', '--overwrite', action='store_true',
        help='overwrite key list file if it exists')
    kg_parser.add_argument('-s', '--start', metavar='XX',
        type=validate_key_list_indicator,
        help='starting indicator; if omitted, random indicators are used')
    kg_parser.add_argument('-n', '--number', type=validate_num_key_lists, default=1,
        help='number of key lists to generate [default: %(default)s]')
    kg_parser.set_defaults(subcommand=keygen)

    args = parser.parse_args(args=argv)

    log_level = getattr(logging, args.log.upper())
    logging.basicConfig(level=log_level, format='%(message)s')

    try:
        args.subcommand(args)
    except EnvironmentError as ex:
        sys.exit('{}\n'.format(ex))
    except KeyboardInterrupt:
        sys.exit('Interrupted\n')
    except M209Error as ex:
        sys.exit('{}\n'.format(ex))

if __name__ == '__main__':
    main()