import argparse
import logging

import PIL
import torch

from . import datasets, decoder, network, transforms, visualizer

LOG = logging.getLogger(__name__)

class Predictor:
    """Convenience class to predict from various inputs with a common configuration."""

    batch_size = 1  #: batch size
    device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')  #: device
    fast_rescaling = True  #: fast rescaling
    loader_workers = None  #: loader workers
    long_edge = None  #: long edge

    def __init__(self, checkpoint=None, head_metas=None, *,
                 json_data=False,
                 visualize_image=False,
                 visualize_processed_image=False):
        if checkpoint is not None:
            network.Factory.checkpoint = checkpoint
        self.json_data = json_data
        self.visualize_image = visualize_image
        self.visualize_processed_image = visualize_processed_image

        self.model_cpu, _ = network.Factory().factory(head_metas=head_metas)
        self.model = self.model_cpu.to(self.device)
        if self.device.type == 'cuda' and torch.cuda.device_count() > 1:
            LOG.info('Using multiple GPUs: %d', torch.cuda.device_count())
            self.model = torch.nn.DataParallel(self.model)
            self.model.base_net = self.model_cpu.base_net
            self.model.head_nets = self.model_cpu.head_nets

        self.preprocess = self._preprocess_factory()
        self.processor = decoder.factory(self.model_cpu.head_metas)

        self.last_decoder_time = 0.0
        self.last_nn_time = 0.0
        self.total_nn_time = 0.0
        self.total_decoder_time = 0.0
        self.total_images = 0

        LOG.info('neural network device: %s (CUDA available: %s, count: %d)',
                 self.device, torch.cuda.is_available(), torch.cuda.device_count())

    @classmethod
    def cli(cls, parser: argparse.ArgumentParser, *,
            skip_batch_size=False, skip_loader_workers=False):
        """Add command line arguments.

        When using this class together with datasets (e.g. in eval),
        skip the cli arguments for batch size and loader workers as those
        will be provided via the datasets module.
        """
        group = parser.add_argument_group('Predictor')

        if not skip_batch_size:
            group.add_argument('--batch-size', default=cls.batch_size, type=int,
                               help='processing batch size')

        if not skip_loader_workers:
            group.add_argument('--loader-workers', default=cls.loader_workers, type=int,
                               help='number of workers for data loading')

        group.add_argument('--long-edge', default=cls.long_edge, type=int,
                           help='rescale the long side of the image (aspect ratio maintained)')
        group.add_argument('--precise-rescaling', dest='fast_rescaling',
                           default=True, action='store_false',
                           help='use more exact image rescaling (requires scipy)')

    @classmethod
    def configure(cls, args: argparse.Namespace):
        """Configure from command line parser."""
        cls.batch_size = args.batch_size
        cls.device = args.device
        cls.fast_rescaling = args.fast_rescaling
        cls.loader_workers = args.loader_workers
        cls.long_edge = args.long_edge

    def _preprocess_factory(self):
        rescale_t = None
        if self.long_edge:
            rescale_t = transforms.RescaleAbsolute(self.long_edge, fast=self.fast_rescaling)

        pad_t = None
        if self.batch_size > 1:
            assert self.long_edge, '--long-edge must be provided for batch size > 1'
            pad_t = transforms.CenterPad(self.long_edge)
        else:
            pad_t = transforms.CenterPadTight(16)

        return transforms.Compose([
            transforms.NormalizeAnnotations(),
            rescale_t,
            pad_t,
            transforms.EVAL_TRANSFORM,
        ])

    def dataset(self, data):
        """Predict from a dataset."""
        loader_workers = self.loader_workers
        if loader_workers is None:
            loader_workers = self.batch_size if len(data) > 1 else 0

        dataloader = torch.utils.data.DataLoader(
            data, batch_size=self.batch_size, shuffle=False,
            pin_memory=self.device.type != 'cpu',
            num_workers=loader_workers,
            collate_fn=datasets.collate_images_anns_meta)

        yield from self.dataloader(dataloader)

    def enumerated_dataloader(self, enumerated_dataloader):
        """Predict from an enumerated dataloader."""
        for batch_i, item in enumerated_dataloader:
            if len(item) == 3:
                processed_image_batch, gt_anns_batch, meta_batch = item
                image_batch = [None for _ in processed_image_batch]
            elif len(item) == 4:
                image_batch, processed_image_batch, gt_anns_batch, meta_batch = item
            if self.visualize_processed_image:
                visualizer.Base.processed_image(processed_image_batch[0])

            pred_batch = self.processor.batch(self.model, processed_image_batch, device=self.device)
            self.last_decoder_time = self.processor.last_decoder_time
            self.last_nn_time = self.processor.last_nn_time
            self.total_decoder_time += self.processor.last_decoder_time
            self.total_nn_time += self.processor.last_nn_time
            self.total_images += len(processed_image_batch)

            # un-batch
            for image, pred, gt_anns, meta in \
                    zip(image_batch, pred_batch, gt_anns_batch, meta_batch):
                LOG.info('batch %d: %s', batch_i, meta.get('file_name', 'no-file-name'))

                # load the original image if necessary
                if self.visualize_image:
                    visualizer.Base.image(image, meta=meta)

                pred = [ann.inverse_transform(meta) for ann in pred]
                gt_anns = [ann.inverse_transform(meta) for ann in gt_anns]

                if self.json_data:
                    pred = [ann.json_data() for ann in pred]

                yield pred, gt_anns, meta

    def dataloader(self, dataloader):
        """Predict from a dataloader."""
        yield from self.enumerated_dataloader(enumerate(dataloader))

    def image(self, file_name):
        """Predict from an image file name."""
        return next(iter(self.images([file_name])))

    def images(self, file_names, **kwargs):
        """Predict from image file names."""
        data = datasets.ImageList(
            file_names, preprocess=self.preprocess, with_raw_image=True)
        yield from self.dataset(data, **kwargs)

    def pil_image(self, image):
        """Predict from a Pillow image."""
        return next(iter(self.pil_images([image])))

    def pil_images(self, pil_images, **kwargs):
        """Predict from Pillow images."""
        data = datasets.PilImageList(
            pil_images, preprocess=self.preprocess, with_raw_image=True)
        yield from self.dataset(data, **kwargs)

    def numpy_image(self, image):
        """Predict from a numpy image."""
        return next(iter(self.numpy_images([image])))

    def numpy_images(self, numpy_images, **kwargs):
        """Predict from numpy images."""
        data = datasets.NumpyImageList(
            numpy_images, preprocess=self.preprocess, with_raw_image=True)
        yield from self.dataset(data, **kwargs)

    def image_file(self, file_pointer):
        """Predict from an opened image file pointer."""
        pil_image = PIL.Image.open(file_pointer).convert('RGB')
        return self.pil_image(pil_image)


#!/usr/bin/python3

import rospy
from geometry_msgs.msg import PoseStamped
from std_msgs.msg import Int64, String, Bool
import random
from itertools import combinations

num_robots = 4
robots = [f"robot{num}" for num in range(1,num_robots+1)]
bomb_duration = 10
bomb_range = 1
thrower_length_advantage = 1

class Robot:
    def __init__(self, name):
        self.name = name

        self.kill_sub = rospy.Subscriber(f"/{name}/kill", Bool, self.kill)
        self.pose_sub = rospy.Subscriber(f"/{name}/pose", PoseStamped, self.set_pose)
        self.stop_pub = rospy.Publisher(f"/{name}/stop", Int64, queue_size=1)
        self.target_pub = rospy.Publisher(f"/{name}/bomb_target", String, queue_size=1)
        self.pose_stamped = PoseStamped()
        self.bomb_available = True
        self.is_killed = False

    def kill(self, _):
        self.is_killed = True

    def reset_bomb(self, _):
        self.bomb_available = True

    def distance_to(self, other):
        # Euclidean distance
        a = self.pose_stamped
        try:
            b = other.pose_stamped
        except AttributeError:
            b = other
        return ((a.pose.position.x - b.pose.position.x)**2 + (a.pose.position.y - b.pose.position.y)**2) ** 0.5

    def set_pose(self, pose):
        self.pose_stamped = pose

    def throw_bomb_at(self, target):
        if self.is_killed or target.is_killed:
            return
        target_msg = String()
        target_msg.data = target.name
        self.target_pub.publish(target_msg)

        stop_duration = Int64()
        stop_duration.data = bomb_duration
        self.stop_pub.publish(stop_duration)

        self.bomb_available = False
        target.bomb_available = False
        rospy.Timer(rospy.Duration(bomb_duration) * 1.25, self.reset_bomb, oneshot=True)
        rospy.Timer(rospy.Duration(bomb_duration) * 1.25, target.reset_bomb, oneshot=True)

class Planner:
    def __init__(self):
        self.robots = [Robot(robot) for robot in robots]
        self.rate = rospy.Rate(2)

    def bomb_calculation(self):
        for robot1, robot2 in list(combinations(self.robots, 2)):
            if robot1.bomb_available and robot1.distance_to(robot2) <= bomb_range + thrower_length_advantage:
                if random.random() < 0.5:
                    robot1.throw_bomb_at(robot2)
                else:
                    robot2.throw_bomb_at(robot1)

    def run(self):
        while not rospy.is_shutdown():
            self.bomb_calculation()
            for robot in self.robots:
                if robot.is_killed:
                    self.robots.remove(robot)
            self.rate.sleep()

if __name__=="__main__":
    rospy.init_node('path_planner')
    planner = Planner()
    planner.run()


from flask import Flask, render_template, redirect, url_for, request, session

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/assignment9', methods=["GET", "POST"])
def as9():
    curr_method = request.method
    users = {"ASAF": "BOY", "D": "S", "R": "REEE"}
    user_name = ''
    user_search = ''

    if curr_method == "GET":
        if 'user_search' in request.args:
            user_search = request.args['user_search']
        else:
            user_search = ''

    if curr_method == "POST":
        if 'user_name' in request.form:
            user_name = request.form['user_name']
        else:
            user_name = ''
        if user_name in users:
            session['nickname'] = users[user_name]
        else:
            session['nickname'] = ''

    return render_template("assignment9.html",
                           users=users,
                           user_name=user_name,
                           curr_method=curr_method,
                           user_search=user_search)

@app.route("/logout")
def logout():
    session['nickname'] = ''
    return redirect('/assignment9')

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


''' create basic config file '''
from sys import argv
from os import path
from configparser import ConfigParser

def __get_args__() -> dict:
    ''' get arguments '''
    token, chat, *admin = argv[1:]
    return {'token': token, 'chat': chat, 'admin': admin}

def __get_path__() -> str:
    '''return path to settings'''
    return path.dirname(path.abspath(__file__)) + '\\settings.ini'

def __create_settings__(token: str, chat: str, admin: str) -> None:
    '''create a config file'''
    token, chat = str(token), str(chat)
    admin = '['+', '.join(map(str, admin))+']' if isinstance(admin, list) else admin
    data_path = __get_path__()
    config = ConfigParser()
    config.add_section('requestsSettings')
    config.set('requestsSettings',
               'token', token)
    config.set('requestsSettings',
               'chat', chat)
    config.set('requestsSettings',
               'admin', admin)
    with open(data_path, 'w') as config_file:
        config.write(config_file)

def read_settings():
    '''read a config file'''
    data_path = __get_path__()
    if not path.isfile(data_path):
        token = input('Telegram bot token needed')
        chat = input('Telegram chatid needed')
        admin = input('Telegram admin list needed')
        __create_settings__(token, chat, admin)
    config = ConfigParser()
    config.read(data_path)
    return config

if __name__ == '__main__':
    __create_settings__(**__get_args__())


import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset

from matplotlib import pyplot as plt
import numpy as np

import random

from dataset import get_data

X_train = get_data('dataset')
X_train = torch.from_numpy(X_train)
X_dataset = TensorDataset(X_train)
X_dataloader = DataLoader(X_dataset, batch_size=64, shuffle=True)

lcode = 64

device = "cpu"

class VAE(nn.Module):
    def __init__(self):
        super().__init__()
        self.shrink = nn.Sequential(
            nn.Conv2d(3, 16, (3, 3),  padding=1),
            nn.MaxPool2d((2, 2)),
            nn.ReLU(),
            nn.Conv2d(16, 64, (3, 3), padding=1),
            nn.MaxPool2d((2, 2)),
            nn.ReLU(),
            nn.Flatten(),
            nn.Linear(64*8*8, 64*8),
            nn.ReLU()
        )
        self.mean = nn.Linear(64*8, lcode)
        self.logvar = nn.Linear(64*8, lcode)
        self.decode = nn.Sequential(
            nn.Linear(lcode, 64*8),
            nn.ReLU(),
            nn.Linear(64*8, 64*8*8),
            nn.ReLU(),
            nn.Unflatten(1, (64, 8, 8)),
            nn.ConvTranspose2d(64, 16, (3, 3), stride=2, padding=1),
            nn.ReLU(),
            nn.ConvTranspose2d(16, 3, (4, 4), stride=2),
            nn.Sigmoid()
        )

    def sample(self, mean, logvar):
        return mean + torch.exp(logvar / 2) * torch.randn_like(mean)

    def encode(self, x):
        y = self.shrink(x)
        return self.sample(self.mean(y), self.logvar(y))

    def forward(self, x):
        y = self.shrink(x)
        m = self.mean(y)
        v = self.logvar(y)
        return (m, v, self.decode(self.sample(m, v)))

model = VAE().to(device)
print(model)

def kl_std(mean, logvar):
    return torch.sum(torch.exp(logvar) + mean * mean - 1 - logvar)

mse = nn.MSELoss(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(), lr=5e-6, momentum=0.9)

def train(model, optimizer, beta):
    size = len(X_dataloader.dataset)
    model.train()
    for batch, X in enumerate(X_dataloader):
        X = X[0].to(device)

        (mean, logvar, img) = model(X)
        loss_mse = mse(img, X)
        loss_kl = kl_std(mean, logvar)
        loss = loss_mse + beta * loss_kl

        loss.backward()
        optimizer.step()
        optimizer.zero_grad()

def test(model):
    size = len(X_dataloader.dataset)
    num_batches = len(X_dataloader)
    model.eval()
    test_loss_mse, test_loss_kl, correct = 0, 0, 0
    with torch.no_grad():
        for X in X_dataloader:
            X = X[0].to(device)
            (mean, logvar, img) = model(X)
            test_loss_mse += mse(img, X).item()
            test_loss_kl += kl_std(mean, logvar).item()
    test_loss_mse /= num_batches
    test_loss_kl /= num_batches
    correct /= size
    print(f"Test Error: \n Avg MSE loss: {test_loss_mse:>8f} \n Avg KL loss: {test_loss_kl:>8f} \n")

epochs = 800
ncycle = 2 # see "Cyclical Annealing Schedule: A Simple Approach to Mitigating KL Vanishing"
def beta_of(t):
    n = epochs / (2 * ncycle)
    i = t / n
    if i % 2 == 0:
        return i / n
    else:
        return 1

for t in range(epochs):
    print(f"Epoch {t+1}\n-------------------------------")
    train(model, optimizer, beta_of(t))
    test(model)
print("Done!")

def show(rgb):
    img = np.empty([32, 32, 3])
    for i in range(32):
        for j in range(32):
            for k in range(3):
                img[i, j, k] = rgb[k, i, j]
    plt.axis('off')
    plt.xticks([])
    plt.yticks([])
    plt.imshow(img)

def gen(code):
    with torch.no_grad():
        show(model.decode(code).numpy()[0])

def compare(i):
    show(X_dataset[i][0])
    gen(model.encode(torch.unsqueeze(X_dataset[i][0], 0)))

def fake():
    gen(torch.randn([1, lcode]))

for i in range(8*8):
    plt.subplot(8, 8, i+1)
    fake()
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

for i in range(32):
    t = random.randint(a=0, b=len(X_train) - 1)
    plt.subplot(8, 8, 2 * i + 1)
    show(X_dataset[t][0])
    plt.subplot(8, 8, 2 * i + 2)
    gen(model.encode(torch.unsqueeze(X_dataset[t][0], 0)))
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

for i in range(8):
    t1 = random.randint(a=0, b=len(X_train) - 1)
    t2 = random.randint(a=0, b=len(X_train) - 1)
    c1 = model.encode(torch.unsqueeze(X_dataset[t1][0], 0))
    c2 = model.encode(torch.unsqueeze(X_dataset[t2][0], 0))
    for a in range(11):
        plt.subplot(8, 11, i * 11 + a + 1)
        a /= 10
        gen(a * c1 + (1 - a) * c2)
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()


import requests, sys, os
from utils import get_result, escape

wsdl_file = 'http://nlptools.info.uaic.ro/WebFdgRo/FdgParserRoWS?wsdl'
headers = {'content-type': 'text/xml'}

rqt_parse_pos_tagged_text = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webFdgRo.uaic/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:parsePosTaggedXML>
         <posTaggedXML>{xml}</posTaggedXML>
      </web:parsePosTaggedXML>
   </soapenv:Body>
</soapenv:Envelope>"""

rqt_parse_text = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webFdgRo.uaic/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:parseText>
         <txt>{text}</txt>
      </web:parseText>
   </soapenv:Body>
</soapenv:Envelope>"""

def parse_pos_tagget_text(text):
    print("Creating request")
    body = rqt_parse_pos_tagged_text.format(xml=text).encode("utf-8")
    print("Request sent to server")
    response = requests.post(wsdl_file, data=body, headers=headers)
    print("Response received")
    return get_result(response)

def parse_text(text):
    try:
        text = escape(text)
        print("Creating request")
        body = rqt_parse_text.format(text=text).encode("utf-8")
        print("Request sent to server")
        response = requests.post(wsdl_file, data=body, headers=headers)
        print("Response received")
        return get_result(response)
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print("Error at calling fdg_parser. Reason:", str(e), "File:",fname , "Line:", exc_tb.tb_lineno)


import os, sys
from setuptools import setup
try:
    README = open(os.path.join(os.path.dirname(__file__), 'README.rst')).read()
except:
    README = ''

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

requirements = [
    'Django>=1.11',
    'googlemaps>=2.5.0',
    'unidecode'
]

try:
    import django
    if django.VERSION >= (3,1,0):
        pass
    else:
        requirements.append('jsonfield')
except ImportError:
    if sys.version_info.major >= (3,8,0):
        requirements[0] = 'Django>=3.1'
    else:
        requirements.append('jsonfield')

setup(
    name='django-google-maps-services',
    version='0.1.0',
    packages=['dj_gmap'],
    include_package_data=True,
    license='BSD License',
    description='Google maps caching functions for django. + helpers',
    long_description=README,
    author='Gerasev Kirill',
    author_email='gerasev.kirill@gmail.com',
    install_requires=requirements,
    classifiers=[
        'Environment :: Web Environment',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
)


from __future__ import annotations
from typing import TYPE_CHECKING
from data_structures.observer import Observer
from entity_base.listeners.tick_listener import TickLambda
from root_container.field_container.node.arc_curve_node import ArcCurveNode
from root_container.field_container.node.bezier_lines import BezierLines
from root_container.field_container.node.bezier_theta_node import BezierThetaNode
from root_container.field_container.path_element import PathElement

from root_container.field_container.segment.segment_type import PathSegmentType
if TYPE_CHECKING:
    from root_container.path import Path

from entity_base.listeners.drag_listener import DragLambda
from root_container.field_container.segment.segment_direction import SegmentDirection
from utility.math_functions import isInsideBox
if TYPE_CHECKING:
    from root_container.field_container.node.path_node_entity import PathNodeEntity

from abc import ABC, abstractmethod
from enum import Enum
from common.reference_frame import PointRef, Ref, VectorRef

from entity_base.listeners.click_listener import ClickLambda
from entity_base.listeners.select_listener import SelectLambda, SelectorType
from entity_base.entity import Entity

from root_container.field_container.segment.path_segment_state import PathSegmentState
from root_container.field_container.segment.PathSegmentStates.bezier_segment_state import BezierSegmentState
from root_container.field_container.segment.PathSegmentStates.arc_segment_state import ArcSegmentState
from root_container.field_container.segment.PathSegmentStates.straight_segment_state import StraightSegmentState

from adapter.path_adapter import PathAdapter, AdapterInterface

from data_structures.linked_list import LinkedListNode

from common.draw_order import DrawOrder
from utility.pygame_functions import shade
import pygame

"""
Acts as a "context" class in the state design pattern. Owns PathSegmentState objects
that define behavior for straight/arc/bezier shapes. Easy to switch between states
We also define the constants that apply across all segment types here, like color and thickness
"""

class PathSegmentEntity(Entity, AdapterInterface, Observer, PathElement['PathNodeEntity']):
    def __init__(self, parent: Entity, path: Path) -> None:

        super().__init__(parent = parent,
                         select = SelectLambda(self, "segment", type = SelectorType.SOLO),
                         drag = DragLambda(self,
                                           FonStartDrag = self.onStartDrag,
                                           FcanDrag = self.canDrag,
                                           FonDrag = self.onDrag,
                                           FonStopDrag = self.onStopDrag
                                           ),
                         tick = TickLambda(self, self.tick),
                         drawOrder = DrawOrder.SEGMENT)

        LinkedListNode.__init__(self)

        self.path = path

        self.states: dict[PathSegmentType, PathSegmentState] = {
            PathSegmentType.STRAIGHT: StraightSegmentState(self),
            PathSegmentType.ARC: ArcSegmentState(self),
            PathSegmentType.BEZIER: BezierSegmentState(self)
        }
        # on state update, recompute itself
        for stateID in self.states:
            self.states[stateID].subscribe(self, onNotify = self.recomputeEntity)

        self.currentState: PathSegmentType = PathSegmentType.STRAIGHT

        self.direction: SegmentDirection = SegmentDirection.FORWARD

        self.colorForward = [122, 210, 118]
        self.colorForwardH = shade(self.colorForward, 0.92)
        self.colorForwardA = shade(self.colorForward, 0.7)
        self.colorReversed = [191, 118, 118]
        self.colorReversedH = shade(self.colorReversed, 0.92)
        self.colorReversedA = shade(self.colorReversed, 0.7)

        # Since the construction of this segment is not complete, with
        # having both nodes not guaranteed, initialization of ArcCurveEntity
        # is delayed until both nodes are set at updateAdapter()
        self.arcNode: ArcCurveNode = None

        self.isFullyInitialized = False

        self.updateAdapter()

    def tick(self):
        return
        print(self.getPrevious(), self.getNext())

    def getState(self) -> PathSegmentState:
        return self.states[self.currentState]

    def getStateType(self) -> PathSegmentType:
        return self.currentState

    def setState(self, newState: PathSegmentType) -> None:
        oldState = self.currentState
        self.currentState = newState
        self.path.onSegmentTypeChange(self, oldState, newState)

        self.getState().onStateChange()
        self.updateAdapter()

        self.getPrevious().onAngleChange()
        self.getNext().onAngleChange()

    def onStartDrag(self, mouse: tuple):
        self.mouseStartDrag = PointRef(Ref.SCREEN, mouse)
        self.nodeStartPosition = []
        for node in [self.getPrevious(), self.getNext()]:
            self.nodeStartPosition.append(node.position)
            node.constraints.showPosition()

    def canDrag(self, mouseTuple: tuple) -> bool:
        mouse = PointRef(Ref.SCREEN, mouseTuple)

        self.mouseDelta = mouse - self.mouseStartDrag
        self.nodeGoalPosition = []
        for i, node in enumerate([self.getPrevious(), self.getNext()]):
            newPos = self.nodeStartPosition[i] + self.mouseDelta
            self.nodeGoalPosition.append(newPos)
            if not isInsideBox(*newPos.fieldRef, 0, 0, 144, 144):
                return False
        return True

    # When dragging, determine if either node is snappable. If so, do it
    def onDrag(self, mouseTuple: PointRef):
        for i, node in enumerate([self.getPrevious(), self.getNext()]):
            node.position = self.nodeGoalPosition[i]
            node.constraints.resetPositionConstraints(node.position)

        delta = VectorRef(Ref.FIELD, (0,0))
        for node in [self.getPrevious(), self.getNext()]:
            node.constrainPosition()
            if node.constraints.snappable():
                delta = node.constraints.getPosition() - node.position
                break # can only snap one node at a time

        for node in [self.getPrevious(), self.getNext()]:
            node.position += delta
            node.onNodeMove()

    def onStopDrag(self):
        for node in [self.getPrevious(), self.getNext()]:
            node.constraints.hidePosition()

    # callback when a node attached to this segment has stopped dragging
    # Pass callback on to segment states if they need to do something
    def onNodeStopDrag(self):
        self.getState().onNodeStopDrag()

    # a segment is temporary if the nodes on either ends are temporary
    def isTemporary(self) -> bool:
        if self.getPrevious() is not None and self.getPrevious().isTemporary():
            return True
        if self.getNext() is not None and self.getNext().isTemporary():
            return True
        return False

    def onNodeMove(self, node: Entity):
        self.updateAdapter()
        self.recomputeEntity()
        self.arcNode.recomputePositionRef()
        if node is self.getPrevious():
            self.getNext().onAngleChange()
        else:
            self.getPrevious().onAngleChange()

    def onReshape(self):
        self.updateAdapter()
        self.recomputeEntity()
        self.getNext().onAngleChange()
        self.getPrevious().onAngleChange()

    def getAdapter(self) -> PathAdapter:
        return self.getState().getAdapter()

    def getAllAdapters(self) -> list[PathAdapter]:
        adapters = []
        for state in self.states:
            adapters.append(self.states[state].getAdapter())
        return adapters

    def updateAdapter(self) -> None:

        # initailize arc node
        if not self.isFullyInitialized and self.getNext() is not None and self.getPrevious() is not None:
            self.arcNode = ArcCurveNode(self, self.states[PathSegmentType.ARC])
            self.bezierTheta1 = BezierThetaNode(self, self.states[PathSegmentType.BEZIER], self.getPrevious, True)
            self.bezierTheta2 = BezierThetaNode(self, self.states[PathSegmentType.BEZIER], self.getNext, False)

            # must call this after initilizing arcNode and bezierThetas,
            # so that position recomputation happens before drawing lines
            BezierLines(self)

            self.isFullyInitialized = True

        self.getState().updateAdapter()
        self.recomputeEntity()

    def toggleDirection(self):
        if self.direction == SegmentDirection.FORWARD:
            self.direction = SegmentDirection.REVERSE
        else:
            self.direction = SegmentDirection.FORWARD
        self.updateAdapter()
        self.getPrevious().onAngleChange()
        self.getNext().onAngleChange()

    def getDirection(self):
        return self.direction

    def getSegmentType(self) -> PathSegmentType:
        return self.currentState

    def getOther(self, node: PathNodeEntity):
        if self.getPrevious() is node:
            return self.getNext()
        elif self.getNext() is node:
            return self.getPrevious()
        else:
            raise ValueError("Node is not connected to this segment")

    def getColor(self, isActive: bool = False, isHovered: bool = False):

        if self.direction == SegmentDirection.REVERSE:
            if isActive:
                return self.colorReversedA
            elif isHovered:
                return self.colorReversedH
            else:
                return self.colorReversed
        else:
            if isActive:
                return self.colorForwardA
            elif isHovered:
                return self.colorForwardH
            else:
                return self.colorForward

    def getStartTheta(self) -> float:
        return self.getState().getStartTheta()

    def getEndTheta(self) -> float:
        return self.getState().getEndTheta()

    def getLinearDistance(self, ref: Ref):
        return (self.getPrevious().position - self.getNext().position).magnitude(ref)

    def getThetaAtNode(self, node: PathNodeEntity):
        if self.getPrevious() is node:
            return self.getStartTheta()
        elif self.getNext() is node:
            return self.getEndTheta()
        else:
            raise Exception("Node is not connected to this segment")

    def isTouching(self, position: PointRef) -> bool:
        return self.getState().isTouching(position)

    def defineCenter(self) -> tuple:
        return self.getState().getCenter()

    def getThickness(self, isForMouseDetection: bool = False) -> int:

        thickness = 6 if isForMouseDetection else 3

        return int(thickness * self.dimensions.RESOLUTION_RATIO)

    def draw(self, screen: pygame.Surface, isActive: bool, isHovered: bool) -> bool:
        return self.getState().draw(screen, isActive, isHovered)

    def isSelfOrNodesSelected(self):
        if self.select.isSelected:
            return True
        elif self.getPrevious().select.isSelected:
            return True
        elif self.getNext().select.isSelected:
            return True
        return False


"""Tweet quote images with hashtags in perpetuity (as long as terminal
is open)."""
import numpy as np
import pandas as pd
import threading
from datetime import datetime, timedelta
import helper.tweetwrapper as twrap
import helper.images as images

# ----------------------------------------------------
# Local functions.
# ----------------------------------------------------
def new_id(df,track_id):
    return_flag = False
    while return_flag is False:
        quote_id = np.random.randint(df.index[0], df.index[-1]+1)
        if quote_id not in track_id[-10:]:
            return_flag = True
    return quote_id

def console_message(text):
    print("""{time}: I tweeted:\n{tweet}\n""".format(
        time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        tweet=text))

def tweet_quote(track_id):
    # read quotes data:
    df = pd.read_csv("bookquotes.csv", comment="#")
    df.loc[:,"title"] = df.title.fillna("unknown")
    df.loc[:,"author"] = df.author.fillna("unknown")

    # choose a quote that fits the conditions present
    # in new_id():
    quote_id = new_id(df,track_id)
    auth = df.loc[quote_id,"author"]
    book = df.loc[quote_id,"title"]
    quote = df.loc[quote_id, "quote"]
    topics = df.loc[quote_id, "topics"]
    poetry_flag = df.loc[quote_id, "poetry"]

    # add quotes around the quote text and book text:
    quote = "'%s'" %quote
    if book!="unknown":
        book="'%s'" %book

    # create tweet hashtags:
    tweet_text = twrap.generate_tweet(auth,book,quote,topics)

    # create tweet image:
    images.imake(
        auth,
        book,
        quote,
        imagec="black",
        textc="white",
        borderx=80,
        text_anchor=(80,-10),
        fontsize=60,
        poetry_flag=poetry_flag)

    # upload tweet image:
    upload_stdout = twrap.upload_media("bin/quote_of_day.png")
    media_id = upload_stdout["media_id"]

    # tweet the quote:
    twrap.tweetme(tweet_text,media_id=media_id)

    # print to console:
    console_message(quote)

    # track the quote id:
    track_id.append(quote_id)

def when_last_tweet(verbose=False):
    stdout = twrap.get_last_tweet()
    datetime_str = stdout[0]["created_at"]
    aware_utc = datetime.strptime(
        datetime_str,
        "%a %b %d %H:%M:%S %z %Y")

    # the Twitter API returns UTC times, but datetime.now()
    # returns local time (Central US for me). I can't figure
    # out how to intelligently assign timezones to the
    # objects, so I'll just strip the Twitter API's timezone
    # info, and manually convert it to CT by subtracting
    # 6 hours.
    naive_utc = aware_utc.replace(tzinfo=None)
    dt = timedelta(hours=6)
    naive_ct = naive_utc - dt

    # print when the last tweet occurred:
    if verbose is True:
        print("The last tweet occurred at: %s" %(
            naive_ct.strftime("%Y-%m-%d %H:%M:%S")))
    return naive_ct

# ----------------------------------------------------
# Execute command.
# ----------------------------------------------------
# define interval:
interval_hours = 1
interval_seconds = interval_hours*60*60

# define hours when the bot can tweet:
hi = 7 #first hour; 7am
he = 22 #last hour; 10pm

# track previous quotes to reduce repeats:
track_id = []

# state when the last tweet occurred:
last_tweet_time = when_last_tweet(verbose=True)

# tweet immediately if the time between now and the last
# tweet is larger than some tolerance:
start_tolerance = timedelta(hours=1)
now=datetime.now()
time_elapsed = now-last_tweet_time
if time_elapsed > start_tolerance:
    tweet_quote(track_id)

# create event and execute once every interval:
ticker = threading.Event()
while ticker.wait(interval_seconds) is False:
    now = datetime.now() #returns datetime in CT; UTC-6hours
    ti = datetime(
        year=now.year,
        month=now.month,
        day=now.day,
        hour=hi)
    te = datetime(
        year=now.year,
        month=now.month,
        day=now.day,
        hour=he)
    if now >= ti and now <= te:
        tweet_quote(track_id)
    else:
        print("no tweet; outside acceptable time.")


# Usubstractbedgraphs v3

import argparse
import sys
import os
import numpy
from array import *

# get arguments from command line
parser = argparse.ArgumentParser(description='Uaveragebedgraphs_args')
parser.add_argument('-o','--output', help='name of output file', required=True, type=str)
parser.add_argument('-f1','--file1', help='file1', required=True, type=str)
parser.add_argument('-f2','--file2', help='file2', required=True, type=str)
parser.add_argument('-d','--delete', help='delete all overlap with file 2 if set to "Y" ', required=False, type=str, default = "N")

args = vars(parser.parse_args())
file1 = args['file1']
file2 = args['file2']
output_file = args['output']
deleteall = args['delete']

try: os.remove(output_file)
except: pass

def substract (file1, file2, newfile):
# make list of chromosomes
    chromosomes = []
    files = [file1, file2]
    for file in files:
        with open(file) as f:
            for line in f:
                line_split = line.split("\t")
                if line_split[0][0:3] == "chr": # account for different formats with and without "chr"
                    line_split[0] = line_split[0][3:]
                if line_split[0] not in chromosomes:
                    chromosomes.append(line_split[0])
    done = "no"
    while done == "no":
        try:
            current_chromosome = chromosomes.pop(0)
        except:
            break

# get length of current chromosome
        stop = 0
        for file in files: # go through all files
            with open(file) as f:
                for line in f:
                    line_split = line.split("\t")
                    if line_split[0][0:3] == "chr": # account for different formats with and without "chr"
                        line_split[0] = line_split[0][3:]
                    if line_split[0] == current_chromosome:
                        if int(line_split[2]) > stop:
                            stop = int(line_split[2])
        if stop > 0:  # continue only if values in chromosome
            output = array('f', [0] * stop)

#substract
            for x, file in enumerate(files):
                with open(file) as f:
                    for line in f:
                        line_split = line.split("\t")
                        line_split[3] = line_split[3].replace("\n", "")
                        if line_split[0][0:3] == "chr":  # account for different formats with and without "chr"
                            line_split[0] = line_split[0][3:]
                        if line_split[0] == current_chromosome:
                            for o in range(int(line_split[2]) - int(line_split[1])):
                                coord = o + int(line_split[1])
                                if x == 0:
                                    output[coord] = float(line_split[3])
                                elif x == 1:
                                    if deleteall == "N":
                                        output[coord] = output[coord] - float(line_split[3])
                                    elif deleteall == "Y":
                                        output[coord] = 0
                                    else:
                                        print("Error: -d has to be N or Y")
                                        sys.exit()

# concatenate entries with same value
            output2 = []
            temp_entry = []
            for x, value in enumerate(output):
                if value != 0:
                    if temp_entry == []: temp_entry = [current_chromosome, x, x + 1, value]
                    elif temp_entry[3] == value: temp_entry[2] += 1
                    elif temp_entry[3] != value:
                        output2.append(temp_entry)
                        temp_entry = [current_chromosome, x, x + 1, value]
                else: temp_entry == []

            if temp_entry != []:
                output2.append(temp_entry)

            with open(newfile, "a") as f:
                for row in output2:
                    f.write(str(row[0]) + "\t" + str(row[1]) + "\t" + str(row[2]) + "\t" + str(row[3]) + "\n")

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

substract(file1, file2, output_file)
print("Success :)")


### Early column mapper script ###

import pandas as pd
import pickle
import keras
from sklearn.preprocessing import LabelEncoder
from keras.preprocessing import sequence

threshold = 0.75

CSV = r'C:\Users\mail\Downloads\data\bisgaard NOOS data.csv'
df = pd.read_csv(CSV, error_bad_lines=False, engine='c', encoding='ISO-8859-1', low_memory=False)
df = df.astype(str)
df = df.sample(1000, random_state=7)
df = df.reset_index(drop=True)

model = keras.models.load_model(r'C:\Users\mail\PycharmProjects\MLDM\Miscellaneous Code\Models\organized\6_class_MVP.h5')
tokenizer = pickle.load(open(r'C:\Users\mail\PycharmProjects\MLDM\Miscellaneous Code\Models\organized\6_class_MVP.pkl', 'rb'))
le = LabelEncoder()
print('Model, Tokenizer and LabelEncoder loaded.')

def predictClass(text, tok, model):
    text_pad = sequence.pad_sequences(tok.texts_to_sequences([text]), maxlen=300)
    score = le.inverse_transform(model.predict_classes([text_pad]))
    return score[0]

col_dict = {df.columns.get_loc(c): c for idx, c in enumerate(df.columns)}
manual_map = {0: 'Amount', 1: 'Unknown', 2: 'Name', 3: 'EAN', 4: 'EAN',
              5: 'Amount', 6: 'Price', 7: 'Price', 8: 'Price', 9: 'Price'}

map_list = []
correct = 0

lists = list(map(list, col_dict.items()))

for n in (range(len(col_dict))):
    df_select = df[col_dict[n]]
    df_select = df_select.astype(str)
    class_map = []
    for m in (range(len(df_select))):
        class_predictions = []
        targets = ['Name', 'Description', 'Category', 'Price', 'Amount', 'EAN']
        targets = le.fit_transform(targets)
        text = df_select[m]
        score = predictClass(text, tokenizer, model)
        lists[n].append(score)
        print('data:', text, ', prediction:', score)
    x = pd.Series(lists[n])
    y = pd.Series(x.value_counts())
    print('_____________________________')
    if y.iloc[0] > (len(df_select)) * threshold:
        print(y.index[0], 'Is the Majority Predicted Class.')
        for i in range(len(y) - 2):
            print('Majority Predicted classes:', y.index[i], ', predictions:', y.iloc[i], '/', len(df_select))
            class_predictions.append('Pred: {class_pred}: {num_pred} / {class_len}. Original: {origin}'
                                     .format(class_pred=y.index[i], num_pred=y.iloc[i], class_len=len(df_select),
                                             origin=col_dict[n], actual=manual_map[n]))
            if y.index[i] == manual_map[n]:
                correct += 1

    else:
        print('There is no Majority Predicted Class above the threshold.')
        for i in range(len(y) - 2):
            print('Predicted classes:', y.index[i], ', predictions:', y.iloc[i], '/', len(df_select))
            class_predictions.append(
                'Pred {num}: {class_pred}: {num_pred} / {class_len}. Original: {origin}, Actual: {actual}'
                .format(num=(i + 1), class_pred=y.index[i], num_pred=y.iloc[i], class_len=len(df_select),
                        origin=col_dict[n], actual=manual_map[n]))
    print('Original Class:', col_dict[n])
    print('__________________________________________')
    map_list.append(class_predictions)

print('Correct Maps: {correct}/{total}'.format(correct = correct, total = len(col_dict)))


import sys
import random
from constants import N_OPTION
from print import o

from sym import Sym
from utils import per

class Num(Sym):
  def __init__(self, c = 0, s = ""):
    super().__init__(c, s)
    self.n = 0
    self.at = c or 0
    self.name = s or ""
    self._has = []
    self.lo = sys.maxsize
    self.hi = -sys.maxsize
    self.isSorted = True
    self.w = -1 if (s or "").find("$") == -1 else 1

  def __repr__(self):
    return o({"at": self.at, "hi": self.hi, "isSorted": self.isSorted, "lo": self.lo, "n": self.n,
    "name": self.name, "w": self.w})

  def nums(self):
    if not self.isSorted:
      self._has.sort()
      self.isSorted = True
    return self._has

  # v is a number -> value like key, value
  def add(self, v, nums, rand: random.Random):
    if v!="?":
      self.n = self.n +1
      self.lo = min(v, self.lo)
      self.hi = max(v, self.hi)

      if len(self._has) < nums:
        self.isSorted = False
        self._has.append(float(v))

      elif rand.random() < nums/self.n:
        # generate random int b/w 1, len of _has. As mentioned in csv.luna
        self.isSorted = False
        pos = rand.randint(0, len(self._has)-1)
        self._has[pos] = float(v)

  def div(self):
    a = self.nums()
    return (per(a,.9) - per(a,0.1))/2.58

  def mid(self):
    return per(self.nums(),.5)


from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from rest_framework import status
from django.shortcuts import get_object_or_404
from rest_framework.permissions import IsAuthenticated
from SoftDesk.models import Projects, Issues, Comments, Contributors
from SoftDesk.serializers import (ProjectsSerializer, ContributorsSerializer,
                                    IssuesSerializer, CommentsSerializer)
from SoftDesk.permissions import (PermissionsViewContributor, PermissionsProjectAuthor,
                                    PermissionsCommentAuthor,
                                    PermissionsContributorAuthorProjet,
                                    PermissionsIssueAuthor)

@api_view(['GET', 'POST'])
@permission_classes([PermissionsViewContributor, IsAuthenticated])
def project_list(request):
    """
    List all projects in which the utilisateur is an author or was affected as a contributor, or create a new projet.
    Permissions: Only authenticated users can access.
    """
    if request.method == 'GET':
        projects = Projects.objects.filter(author_user_id=request.user.id) | Projects.objects.filter(contributor__user_id=request.user.id)
        serializer = ProjectsSerializer(projects, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'POST':
        data = request.data.copy()
        data['author_user_id'] = request.user.id

        serializer = ProjectsSerializer(data=data)
        if serializer.is_valid():
            project = serializer.save()
            Contributors.objects.create(user_id=request.user, project_id=project, role='L\'AUTEUR')
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'PUT', 'DELETE'])
@permission_classes([PermissionsProjectAuthor, IsAuthenticated])
def project_detail(request, project_id):
    """
    Retrieve, update or delete issues.
    Permissions: Only authenticated users can access.
                 Only the author of the project can delete or modify the project
    """
    project = get_object_or_404(Projects, id=project_id)

    if request.method == 'GET':
        serializer = ProjectsSerializer(project)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'PUT':
        data = request.data.copy()
        data['author_user_id'] = project.author_user_id.id

        serializer = ProjectsSerializer(project, data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        project.delete()
        return Response('Project deleted successfully!', status=status.HTTP_204_NO_CONTENT)

@api_view(['GET', 'POST'])
@permission_classes([IsAuthenticated])
def contributor_list(request, project_id):
    """
    List all contributors, or create a new contributor.
    Permissions: Only authenticated users can access.
                 Only the project author can attach new contributors to the project
    """
    project = get_object_or_404(Projects, id=project_id)

    if request.method == 'GET':
        contributors = Contributors.objects.filter(project_id=project)
        serializer = ContributorsSerializer(contributors, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'POST':
        #seul l'auteur du projet peut ajouter des contributeurs
        if request.user.id == project.author_user_id.id:
            data = request.data.copy()
            data['project_id'] = project.id

            serializer = ContributorsSerializer(data=data)
            if serializer.is_valid(raise_exception=True):
                serializer.save()
                return Response(serializer.data, status=status.HTTP_201_CREATED)
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response('You do not have permission to add contributors to the project. Only the author of the project has that right.')

@api_view(['DELETE'])
@permission_classes([PermissionsContributorAuthorProjet, IsAuthenticated])
def contributor_detail(request, contributor_id, project_id):
    """
    Delete contributors
    Permissions: Only authenticated users can access.
                 Only the project author can remove project contributors
    """
    contributor = get_object_or_404(Contributors, id=contributor_id)
    project = get_object_or_404(Projects, id=project_id)

    if request.method == 'DELETE':
        contributor.delete()
        return Response('Contributor deleted successfully!', status=status.HTTP_204_NO_CONTENT)

@api_view(['GET', 'POST'])
@permission_classes([IsAuthenticated])
def issue_list(request, project_id):
    """
    List all issues, or create a new issue.
    Permissions: Only authenticated users can access.
    """
    project = get_object_or_404(Projects, id=project_id)

    if request.method == 'GET':
        issues = Issues.objects.filter(project_id=project_id)
        serializer = IssuesSerializer(issues, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'POST':
        data = request.data.copy()
        data['project_id'] = project_id
        data['author_user_id'] = request.user.id

        serializer = IssuesSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['PUT', 'DELETE'])
@permission_classes([PermissionsIssueAuthor, IsAuthenticated])
def issue_detail(request, project_id, issue_id):
    """
    Retrieve, update or delete issues.
    Permissions: Only authenticated users can access.
                 Only the author of the problem can modify or delete it.
    """
    project = get_object_or_404(Projects, id=project_id)
    issue = get_object_or_404(Issues, id=issue_id)

    if request.method == 'PUT':
        data = request.data.copy()
        data['project_id'] = project.id
        data['author_user_id'] = issue.author_user_id.id

        serializer = IssuesSerializer(issue, data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        issue.delete()
        return Response('Issue successfully deleted.', status=status.HTTP_204_NO_CONTENT)

@api_view(['GET', 'POST'])
@permission_classes([IsAuthenticated])
def comments_list(request, project_id, issue_id):
    """
    List all comments, or create a new comment.
    Permissions: Only authenticated users can access.
    """
    get_object_or_404(Projects, id=project_id)
    issue = get_object_or_404(Issues, id=issue_id)

    if request.method == 'GET':
        comment = Comments.objects.filter(issues_id=issue)
        serializer = CommentsSerializer(comment, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'POST':
        data = request.data.copy()
        data['issue'] = issue.id
        data['author'] = request.user.id

        serializer = CommentsSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'PUT', 'DELETE'])
@permission_classes([PermissionsCommentAuthor, IsAuthenticated])
def comment_detail(request, project_id, issue_id, comment_id):
    """
    Retrieve, update or delete comments.
    Permissions: Only authenticated users can access.
                 Only the author of the comment can modify or delete it.
    """
    get_object_or_404(Projects, id=project_id)
    issue = get_object_or_404(Issues, id=issue_id)
    comment = get_object_or_404(Comments, id=comment_id)

    if request.method == 'GET':
        serializer = CommentsSerializer(comment)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'PUT':
        data = request.data.copy()
        data['issue'] = issue.id
        data['author'] = comment.author_user_id.id

        serializer = CommentsSerializer(comment, data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        comment.delete()
        return Response('Comment successfully deleted.', status=status.HTTP_204_NO_CONTENT)


import torch
import torch.nn as nn
import torch.nn.functional as F
from sparsebit.sparse.modules import SparseOpr, register_smodule

@register_smodule(sources=[nn.Conv2d])
class SConv2d(SparseOpr):
    def __init__(self, org_module, config=None):
        assert isinstance(org_module, nn.Conv2d)
        super().__init__()
        self.fwd_kwargs = dict(
            stride=org_module.stride,
            padding=org_module.padding,
            dilation=org_module.dilation,
            groups=org_module.groups,
        )
        self.weight = org_module.weight
        self.bias = org_module.bias
        w_mask = torch.ones_like(self.weight)
        b_mask = torch.ones_like(self.bias) if self.bias is not None else None
        self.register_buffer("w_mask", w_mask)
        self.register_buffer("b_mask", b_mask)
        self._repr_info = "S" + org_module.__repr__()

    def calc_mask(self, pre_mask=None):
        self.w_mask = self.sparser.calc_mask(self.weight)

        if self.sparser.type == "structed":
            mask = self.w_mask[:, 0, 0, 0]
            if self.bias is not None:
                self.b_mask.data.copy_(mask.data)

            if self.sparser.strategy == "l1norm":
                return mask

        return None

    def forward(self, x_in: torch.Tensor):
        weight = self.weight * self.w_mask
        bias = self.bias * self.b_mask if self.bias is not None else self.bias
        out = F.conv2d(x_in, weight, bias, **self.fwd_kwargs)
        return out


import logging

from django.shortcuts import render
from django import forms
from django.core.mail import send_mail
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic.edit import FormView
from django.contrib import messages
from django.utils.translation import ugettext as _

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

from studentsdb.settings import ADMIN_EMAIL

class ContactForm(forms.Form):
    def __init__(self, *args, **kwards):
        # call original initializator
        super(ContactForm, self).__init__(*args, **kwards)

        # this helper object allows us to customize form
        self.helper = FormHelper()

        # form tag attributes
        # self.helper.form_class = 'form-horizontal'
        self.form_method = 'post'
        self.helper.form_tag = False
        self.form_action = reverse('contact_admin')

        # twitter bootstrap styles
        self.helper.help_text_inline = True
        self.helper.html5_required = True
        self.helper.label_class = 'col-sm-2 control-label'
        self.helper.field_class = 'col-sm-10'

        # form buttons
        self.helper.add_input(Submit('send_button', _(u'Send')))

    from_email = forms.EmailField(
        label=_(u"Your Email Address")
    )

    subject = forms.CharField(
        label=_(u"Email Subject"),
        max_length=128
    )
    message = forms.CharField(
        label=_(u"Email Body"),
        max_length=2560,
        widget=forms.Textarea
    )

def contact_admin(request):
    # check it form was posted
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = ContactForm(request.POST)

        # check whether user data is valid:
        if form.is_valid():
            # send email
            subject = form.cleaned_data['subject']
            message = form.cleaned_data['message']
            from_email = form.cleaned_data['from_email']

            try:
                send_mail(subject, message, from_email, [ADMIN_EMAIL])
            except Exception:
                message = _(u"An error occured during email transfer. Please, "
                            u"try again later.")
                messages.warning(request, message)
                logger = logging.getLogger(__name__)
                logger.exception(message)
            else:
                message = _(u'Message sent successfully!')
                messages.success(request, message)

            # redirect to same contact page with success message
            return HttpResponseRedirect(reverse('contact_admin'))

    # if there was not POST render blank form
    else:
        form = ContactForm()

    return render(request, 'contact_admin/form.html', {'form': form})

class ContactView(FormView):
    template_name = 'contact_admin/form.html'
    form_class = ContactForm
    # success_url = '/email-sent/'
    success_url = '/contact_admin/'

    def get_success_url(self):
        return reverse('contact_admin')

    def form_valid(self, form):
        """This method is called for valid data"""
        subject = form.cleaned_data['subject']
        message = form.cleaned_data['message']
        from_email = form.cleaned_data['from_email']

        try:
            send_mail(subject, message, from_email, [ADMIN_EMAIL])
        except Exception:
            message = _(u"An error occured during email transfer. Please, "
                            u"try again later.")
            messages.warning(self.request, message)
            logger = logging.getLogger(__name__)
            logger.exception(message)
        else:
            message = _(u'Message sent successfully!')
            messages.success(self.request, message)

        return super(ContactView, self).form_valid(form)


import os
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from kafka import SimpleProducer, KafkaClient
from dotenv import load_dotenv, find_dotenv

load_dotenv(find_dotenv())

access_token = os.getenv('ACCESS_TOKEN')
access_token_secret = os.getenv('ACCESS_TOKEN_SECRET')
consumer_key = os.getenv('CONSUMER_KEY')
consumer_secret = os.getenv('CONSUMER_SECRET')

class StdOutListener(StreamListener):
    def on_data(self, data):
        producer.send_messages('twitter', data.encode('utf-8'))
        return True

    def on_error(self, status):
        print (status)

kafka = KafkaClient('localhost:9092')
producer = SimpleProducer(kafka)
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(languages=["en"], track=['basketball', 'nba', 'ncaa'])


from discord.ext import commands
from aurora_secrets import *
from aurora_lists import *
import asyncio
import discord
import random

class Games(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(aliases=['mini_games'])
    async def games(self, ctx):
        bbo = discord.Embed(title="Mini Games", description="Choose which one you want to play!", color=0xa3ff61)
        bbo.add_field(name="🏀 ``.bbgame``", value="basketball, simple shooting game", inline=False)
        bbo.add_field(name=":soccer: ``.fbgame``", value="football, simple kicking game", inline=False)
        bbo.add_field(name="<:greenball:830467077058854922> ``.blgame``", value="bowling, simple bowling game", inline=False)
        bbo.set_thumbnail(url="https://media.discordapp.net/attachments/807511480878497806/832282579050823711/aurora_games.png")

        await ctx.send(embed=bbo)

    @commands.command()
    async def bbgame(self, ctx):
        bbem = discord.Embed(title="Game Info", description="**React** to the appropriate power level to make a shot based on your intuition", color=random.choice(colors))
        bbem.add_field(name="Start Game: ``.shoot``", value="_ _", inline=False)
        bbem.add_field(name="Power levels (correspond to the reactions):", value="_ _", inline=False)
        bbem.set_image(url='https://media.discordapp.net/attachments/807511480878497806/819109874897911848/brown_square_red_square_orange_square_yellow_square_green_square_blue_square_white_large_square_.png')

        await ctx.send(embed=bbem)

    @commands.command(aliases=["basketball"])
    @commands.cooldown(1, 3, commands.BucketType.user)
    async def shoot(self, ctx):
        x = random.randint(1, 8)
        coloremojis = {"🟫": 7, "🟥": 6, "🟧": 5, "🟨": 4, "🟩": 3, "🟦": 2, "⬜": 1}
        b = await ctx.reply("<:bbball:830475864172789800>" + ("<:transparent:830476759463231498>" * x) + ':wastebasket:', mention_author=False)
        for color in coloremojis.keys():
            await b.add_reaction(color)

        def check(reaction, user):
            return user == ctx.author and str(reaction.emoji) in coloremojis.keys()

        reaction, user = await self.bot.wait_for("reaction_add", check=check)

        a = int(x - coloremojis[str(reaction.emoji)])

        await b.edit(content="<:bbball:830475864172789800>" + "<:transparent:830476759463231498>" * a + ':wastebasket:')

        if a == 0:
            await b.edit(content="<:yay:830336856415666187>")
            await ctx.send(f"{ctx.author.mention}, Nice Shot! <a:jedipepe:796237771990761532> type ``.shoot`` to play again")

        elif a > 0:
            await b.edit(content="<:bbball:830475864172789800>" + "<:transparent:830476759463231498>" * a + ":wastebasket:")
            await ctx.send(f"{ctx.author.mention}, oh no! you're too short <a:wutpepe:796237684170424351> type ``.shoot`` to play again")

        elif a < 0:
            await b.edit(content=':wastebasket:' + "<:transparent:830476759463231498>" + "<:bbball:830475864172789800>")
            await ctx.send(f"{ctx.author.mention}, oh no! you went past the basket <a:wutpepe:796237684170424351> type ``.shoot`` to play again")

    @commands.command()
    async def fbgame(self, ctx):
        fb = discord.Embed(title="Game Info", description="the goal keeper can  move anywhere! use your luck...\nlist of choices to decide where the ball goes (use after command):\n ``left`` ``middle`` ``right``", color=random.choice(colors))
        fb.add_field(name="Start Game: ``.kickball``", value="_ _")
        await ctx.send(embed=fb)

    @commands.command(aliases=["kb"])
    @commands.cooldown(1, 3, commands.BucketType.user)
    async def kickball(self, ctx):
        x = random.randint(0, 2)
        goalie = ":man_standing:\n"
        goal = ":goal::goal::goal:\n"
        goal2 = ":goal::goal:"
        goalmid = ":goal::soccer::goal:\n"
        ball = ":soccer:"
        plane = goal+"<:transparent:830476759463231498>"*x+goalie+ball
        f = await ctx.reply(plane, mention_author=False)

        def check(message):

            return ctx.author == message.author and ctx.channel == message.channel

        ans = await self.bot.wait_for('message', check=check)

        if ans.author == ctx.message.author:

            if ans.content.lower() == "right":
                y = random.randint(1, 3)
                r = random.randint(1, 2)
                if y == 3:
                    newplane = goal + "<:transparent:830476759463231498>" * 2 + goalie + "<:transparent:830476759463231498>"*2 + ball

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} "+ww + " lol the goalie blocked your kick")
                    return

                elif y == 1:
                    newplane = goal2+ball + "\n" + "<:transparent:830476759463231498>" + goalie

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} "+vb + " you made it into the goal")
                    return

                elif y == 2:
                    newplane = goal2 + ball + "\n" + "<:transparent:830476759463231498>" * 1 + goalie

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} " + vb + " you made it into the goal")
                    return

                if r == 1:
                    newplane = goal + "<:transparent:830476759463231498>" * 2 + goalie + "<:transparent:830476759463231498>" * 2 + ball

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} " + ww + " lol the goalie blocked your kick")
                    return

                if x == 2:
                    newplane = goal + "<:transparent:830476759463231498>" * 2 + goalie + "<:transparent:830476759463231498>"*2 + ball
                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} "+ww + " lol the goalie blocked your kick")

                    return

                return

            if ans.content.lower() == "left":
                y = random.randint(1, 3)
                r = random.randint(1, 2)
                if y == 1:
                    newplane = goal + goalie + ball

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} "+ww + " lol the goalie blocked your kick")
                    return

                elif y == 3:
                    newplane = ball+goal2 + "\n" + "<:transparent:830476759463231498>" * 2 + goalie

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} "+vb + " you made it into the goal")
                    return

                elif y == 2:
                    newplane = ball+goal2 + "\n" + "<:transparent:830476759463231498>" * 1 + goalie

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} "+vb + " you made it into the goal")
                    return

                if r == 1:
                    newplane = goal + goalie + ball
                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} "+ww + " lol the goalie blocked your kick")
                    return

                if x == 0:
                    newplane = goal + goalie + ball

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} "+ww + " lol the goalie blocked your kick")
                    return

                return

            if ans.content.lower() == "middle":
                y = random.randint(1, 3)
                r = random.randint(1, 2)
                if y == 1:
                    newplane = goalmid + goalie

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} " + vb + " you made it into the goal")
                    return

                elif y == 3:
                    newplane = goalmid + "<:transparent:830476759463231498>"*2 + goalie

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} " + vb + " you made it into the goal")
                    return

                elif y == 2:
                    newplane = goal + "<:transparent:830476759463231498>"*1 + goalie + "<:transparent:830476759463231498>"*1 + ball

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} "+ww + " lol the goalie blocked your kick")
                    return

                if r == 1:
                    newplane = goal + goalie + ball

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} " + ww + " lol the goalie blocked your kick")
                    return

                if x == 1:
                    newplane = goal + "<:transparent:830476759463231498>" + goalie + "<:transparent:830476759463231498>" + ball

                    await f.edit(content=newplane)
                    await ctx.send(f"{ctx.author.mention} "+ww + " lol the goalie blocked your kick")

                return

            if ans.content.lower() == ".kickball":
                em = discord.Embed(title="", description="", colour=0xffb12b)
                em.add_field(name="ERROR", value="Please respond with the following options next time\n``left``, ``middle``, ``right``\n Support Server: [Join](https://discord.gg/G9vTrsV4aG)")

                await ctx.send(embed=em)
                return

            else:
                em = discord.Embed(title="", description="", colour=0xffb12b)
                em.add_field(name="ERROR",value="Please respond with the following options next time\n``left``, ``middle``, ``right`` & ``.kickball`` to run the command again\n Support Server: [Join](https://discord.gg/G9vTrsV4aG)")

                await ctx.send(embed=em)
                return

    @commands.command()
    async def blgame(self, ctx):
        bl = discord.Embed(title="Game Info", description="Choose an appropriate power level to knock down all the pins\n(type the number listed below)", color=random.choice(colors))
        bl.add_field(name="Start Game: ``.bowl``", value="_ _", inline=False)
        bl.add_field(name="power levels list:", value="1, 2, 3, 4, 5, 6", inline=False)

        await ctx.send(embed=bl)

    @commands.command()
    async def bowl(self, ctx):
        powerlevel = "<:12:831762862124171275><:34:831762862304526356><:56:831762862388936755>"
        win = f"{ctx.author.mention} <a:4919pepelaugh:830345716437352448> You knocked all the pins! "
        lose = f"{ctx.author.mention} <a:2813pepeexit:830345717478326273> lol your too short!"
        bounds = f"{ctx.author.mention} " + ww + "yo slow down the ball went out of bounds"
        emoji = ["<:purpleball:830467076291821638>", "<:redball:830467075575119962>","<:greenball:830467077058854922>", "<:blueball:830467076455137321>"]
        em = random.choice(emoji)
        plane = "<:pin:830469397465792512><:pin:830469397465792512><:pin:830469397465792512>\n      <:pin:830469397465792512><:pin:830469397465792512>\n             <:pin:830469397465792512>"
        broken = f"<:pin:830469397465792512><:pin:830469397465792512><:pin:830469397465792512>\n      <:pin:830469397465792512><:pin:830469397465792512>\n             <:fallenpin3:830469397671182346>{em}"
        broken2 = f"<:pin:830469397465792512><:pin:830469397465792512><:pin:830469397465792512>\n      <:fallenpin2:830469397000486963><:fallenpin1:830469397092892742>{em}\n             <:fallenpin3:830469397671182346>"
        broken3 = "<:fallenpin2:830469397000486963><:fallenpin3:830469397671182346><:fallenpin1:830469397092892742>\n      <:fallenpin2:830469397000486963><:fallenpin1:830469397092892742>\n             <:fallenpin3:830469397671182346>"
        x = random.randint(1, 6)
        newplane = plane + "\n<:transparent:830476759463231498>" * x + "\n" + "             "+em+"\n"*2+powerlevel
        b = await ctx.reply(newplane, mention_author=False)

        def check(message):

            return ctx.author == message.author and ctx.channel == message.channel

        ans = await self.bot.wait_for('message', check=check)

        if ans.author == ctx.message.author:
            if ans.content.lower() == "1":
                if 1 == x:
                    await b.edit(content=plane + "\n"*2 + "             " + em)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken2)
                    await asyncio.sleep(0.5)
                    await b.edit(content="<:transparent:830476759463231498>" + em + "\n" + broken3)
                    await ctx.send(win)
                elif 1 < x:
                    await b.edit(content=plane + "\n"*4 + "             " + em)
                    await ctx.send(lose)

            elif ans.content.lower() == "2":
                if 2 == x:
                    await b.edit(content=plane + "\n"*3 + "             " + em)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken2)
                    await asyncio.sleep(0.5)
                    await b.edit(content="<:transparent:830476759463231498>" + em + "\n" + broken3)
                    await ctx.send(win)

                elif 2 < x:
                    await b.edit(content=plane + "\n"*2 + "             " + em)
                    await ctx.send(lose)

            elif ans.content.lower() == "3":
                if 3 == x:
                    await b.edit(content=plane + "\n"*3 + "             " + em)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken2)
                    await asyncio.sleep(0.5)
                    await b.edit(content="<:transparent:830476759463231498>" + em + "\n" + broken3)
                    await ctx.send(win)

                elif 3 < x:
                    await b.edit(content=plane + "\n"*3 + "             " + em)
                    await ctx.send(lose)

                elif 3 > x:
                    await b.edit(content="<:transparent:830476759463231498>"+em+"\n"*2+plane)
                    await ctx.send(bounds)

            elif ans.content.lower() == "4":
                if 4 == x:
                    await b.edit(content=plane + "\n"*3 + "             " + em)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken2)
                    await asyncio.sleep(0.5)
                    await b.edit(content="<:transparent:830476759463231498>" + em + "\n" + broken3)
                    await ctx.send(win)

                elif 4 < x:
                    await b.edit(content=plane + "\n"*3 + "             " + em)
                    await ctx.send(lose)

                elif 4 > x:
                    await b.edit(content="<:transparent:830476759463231498>"+em+"\n"*3+plane)
                    await ctx.send(bounds)

            elif ans.content.lower() == "5":
                if 5 == x:
                    await b.edit(content=plane + "\n"*3 + "             " + em)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken2)
                    await asyncio.sleep(0.5)
                    await b.edit(content="<:transparent:830476759463231498>" + em + "\n" + broken3)
                    await ctx.send(win)

                elif 5 < x:
                    await b.edit(content=plane + "\n"*3 + "             " + em)
                    await ctx.send(lose)

                elif 5 > x:
                    await b.edit(content="<:transparent:830476759463231498>"+em+"\n"*4+plane)
                    await ctx.send(bounds)

            elif ans.content.lower() == "6":
                if 6 == x:
                    await b.edit(content=plane + "\n"*3 + "             " + em)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken)
                    await asyncio.sleep(0.5)
                    await b.edit(content=broken2)
                    await asyncio.sleep(0.5)
                    await b.edit(content="<:transparent:830476759463231498>" + em + "\n" + broken3)
                    await ctx.send(win)

                elif 6 > x:
                    await b.edit(content="<:transparent:830476759463231498>"+em+"\n"*6+plane)
                    await ctx.send(bounds)
            else:
                if ans.content.lower() == ".bowl":
                    em = discord.Embed(title="", description="", colour=0xffb12b)
                    em.add_field(name="ERROR", value="Please respond with the following options next time\n``1``, ``2``, ``3``, ``4``, ``5``, ``6``\nrun ``.blgame`` for more info\nSupport Server: [Join](https://discord.gg/G9vTrsV4aG)")
                    await ctx.send(embed=em)
                    return
                else:
                    em = discord.Embed(title="", description="", colour=0xffb12b)
                    em.add_field(name="ERROR", value="Please respond with the following options next time\n``1``, ``2``, ``3``, ``4``, ``5``, ``6`` & ``.bowl`` to run the command again\nrun ``.blgame`` for more info\n Support Server: [Join](https://discord.gg/G9vTrsV4aG)")
                    await ctx.send(embed=em)
                    return

async def setup(bot):
    await bot.add_cog(Games(bot))


import collections

import cv2
import gym
import gym_super_mario_bros
import numpy as np
import torch
from gym.core import Env
from nes_py.wrappers import JoypadSpace

from src.constants import (
    AGENT_ACTIONS,
    ENV_HEIGHT,
    ENV_WIDTH,
    SKIP_AND_STACK_AMOUNT,
    WORLD,
)

class MaxAndSkipEnv(gym.Wrapper):
    def __init__(self, env=None, skip=SKIP_AND_STACK_AMOUNT):
        """Return only every `skip`-th frame"""
        super(MaxAndSkipEnv, self).__init__(env)
        # most recent raw observations (for max pooling across time steps)
        self._obs_buffer = collections.deque(maxlen=2)
        self._skip = skip

    def step(self, action):
        total_reward = 0.0
        done = None
        for _ in range(self._skip):
            obs, reward, done, info = self.env.step(action)
            self._obs_buffer.append(obs)
            total_reward += reward
            if done:
                break
        max_frame = np.max(np.stack(self._obs_buffer), axis=0)
        # Save the original state to allow fetching when creating video
        self.frame = max_frame.copy()
        return max_frame, total_reward, done, info

    def reset(self):
        """Clear past frame buffer and init to first obs"""
        self._obs_buffer.clear()
        obs = self.env.reset()
        self._obs_buffer.append(obs)
        return obs

class ProcessFrame84(gym.ObservationWrapper):
    """
    Downsamples image to 84x84
    Greyscales image

    Returns numpy array
    """

    def __init__(self, env=None):
        super(ProcessFrame84, self).__init__(env)
        self.observation_space = gym.spaces.Box(
            low=0, high=255, shape=(ENV_WIDTH, ENV_HEIGHT, 1), dtype=np.uint8
        )

    def observation(self, obs):
        return ProcessFrame84.process(obs)

    @staticmethod
    def process(frame):
        if frame.size == 240 * 256 * 3:
            img = np.reshape(frame, [240, 256, 3]).astype(np.float32)
        else:
            assert False, "Unknown resolution."
        img = img[:, :, 0] * 0.299 + img[:, :, 1] * 0.587 + img[:, :, 2] * 0.114
        resized_screen = cv2.resize(img, (84, 110), interpolation=cv2.INTER_AREA)
        x_t = resized_screen[18:102, :]
        x_t = np.reshape(x_t, [84, 84, 1])
        return x_t.astype(np.uint8)

class ImageToPyTorch(gym.ObservationWrapper):
    """Class to convert a state image to a pytorch tensor
    """

    def __init__(self, env):
        super(ImageToPyTorch, self).__init__(env)
        old_shape = self.observation_space.shape
        self.observation_space = gym.spaces.Box(
            low=0.0,
            high=1.0,
            shape=(old_shape[-1], old_shape[0], old_shape[1]),
            dtype=np.float32,
        )

    def observation(self, observation):
        return np.moveaxis(observation, 2, 0)

class ScaledFloatFrame(gym.ObservationWrapper):
    """Normalize pixel values in frame --> 0 to 1"""

    def observation(self, obs):
        return np.array(obs).astype(np.float32) / 255.0

class BufferWrapper(gym.ObservationWrapper):
    """Class to wrapper observation in a buffer"""

    def __init__(self, env, n_steps, dtype=np.float32):
        super(BufferWrapper, self).__init__(env)
        self.dtype = dtype
        old_space = env.observation_space
        self.observation_space = gym.spaces.Box(
            old_space.low.repeat(n_steps, axis=0),
            old_space.high.repeat(n_steps, axis=0),
            dtype=dtype,
        )

    def reset(self):
        self.buffer = np.zeros_like(self.observation_space.low, dtype=self.dtype)
        return self.observation(self.env.reset())

    def observation(self, observation):
        self.buffer[:-1] = self.buffer[1:]
        self.buffer[-1] = observation
        return self.buffer

def create_mario_env() -> Env:
    """Function to create a Super Mario Bros environment wrapped by our custom  wrappers

    Returns:
        Env: the wrapped environment
    """
    env = gym_super_mario_bros.make(WORLD)
    env = MaxAndSkipEnv(env)
    env = ProcessFrame84(env)
    env = ImageToPyTorch(env)
    env = BufferWrapper(env, 4)
    env = ScaledFloatFrame(env)
    env.seed(42)
    env.action_space.seed(42)
    torch.manual_seed(42)
    torch.random.manual_seed(42)
    np.random.seed(42)
    return JoypadSpace(env, AGENT_ACTIONS)

IDATT2502 Anvendt maskinlæring med prosjekt/project/src/environment.py


# Import required libraries
import matplotlib.pyplot as plt
import cv2
import numpy as np
import os

# Read source and mask (if exists) for a given id
def Read(id, path = ""):
    source = plt.imread(path + "image_" + id + ".jpg")
    maskPath = path + "mask_" + id + ".jpg"

    if os.path.isfile(maskPath):
        mask = plt.imread(maskPath)
        assert(mask.shape == source.shape), 'size of mask and image does not match'
        mask = (mask > 128)[:, :, 0].astype(int)
    else:
        mask = np.zeros_like(source)[:, :, 0].astype(int)

    return source, mask

def compute_energy_matrix(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    dx = cv2.Sobel(gray, cv2.CV_64F, 1, 0)
    dy = cv2.Sobel(gray, cv2.CV_64F, 0, 1)
    energy = np.abs(dx) + np.abs(dy)

    return energy

def compute_optimal_seam(E):
    # Make the scoring matrix M
    M = E.copy()

    # Set the values of every entry in the matrix except for the first row
    for i in range(1, M.shape[0]):
        for j in range(M.shape[1]):
            # Need to compensate for bounds
            if j == 0:
                M[i][j] = E[i][j] + min(M[i - 1][j], M[i - 1][j + 1])
            elif j == M.shape[1] - 1:
                M[i][j] = E[i][j] + min(M[i - 1][j - 1], M[i - 1][j])
            else:
                M[i][j] = E[i][j] + min(M[i - 1][j - 1], M[i - 1][j], M[i - 1][j + 1])
    return M

def find_min_value(M):
    to_list = list(M)
    return to_list.index(min(to_list))

def compute_seam_path(M):
    h, w = M.shape
    min_val_bot_rpw = find_min_value(M[h - 1, :])
    seam = [min_val_bot_rpw]
    i = 0

    for y in range(h - 1, 0, -1):
        x = seam[i]

        if (x == 0):
            min_val = find_min_value(M[y - 1, x : x + 2])
            seam.append(x + min_val)
        elif(x == w - 1):
            min_val = find_min_value(M[y - 1, x - 1: x + 1])
            seam.append(x + min_val - 1)
        else:
            min_val = find_min_value(M[y - 1, x - 1: x + 2])
            seam.append(x + min_val - 1)
        i += 1

    return seam

def seam_removal(img, seam):
    h, w, _ = img.shape
    output = cv2.resize(img, (w - 1, h), interpolation=cv2.INTER_LINEAR)
    col = h - 1

    for i in range(img.shape[0]):
        row = seam[i]
        output[col, : row] = img[col, : row]
        output[col, row:] = img[col, row + 1:]
        col = col - 1

    return output

def scale_column(input, scale_c):
    target_width = int(input.shape[1] * scale_c)

    for i in range(input.shape[1] - target_width):
        E = compute_energy_matrix(input)
        M = compute_optimal_seam(E)
        seam = compute_seam_path(M)
        input = seam_removal(input, seam)

    return input

def scale_row(input, scale_r):
    input = np.rot90(input, 1)
    input = scale_column(input, scale_r)

    return np.rot90(input, 3)

def SeamCarve(input, widthFac, heightFac, mask):

    # Main seam carving function. This is done in three main parts: 1)
    # computing the energy function, 2) finding optimal seam, and 3) removing
    # the seam. The three parts are repeated until the desired size is reached.

    assert (widthFac == 1 or heightFac == 1), 'Changing both width and height is not supported!'
    assert (widthFac <= 1 and heightFac <= 1), 'Increasing the size is not supported!'

    if heightFac != 1:
        input = scale_row(input, heightFac)
    else:
        input = scale_column(input, widthFac)

    return input, (input.shape[1], input.shape[0])

# Setting up the input output paths
inputDir = '../Images/'
outputDir = '../Results/'

widthFac = 1; # To reduce the width, set this parameter to a value less than 1
heightFac = 0.5;  # To reduce the height, set this parameter to a value less than 1
N = 4 # number of images

for index in range(N, N + 1):

    input, mask = Read(str(index).zfill(2), inputDir)

    # Performing seam carving. This is the part that you have to implement.
    output, size = SeamCarve(input, widthFac, heightFac, mask)

    # Writing the result
    plt.imsave("{}/result_{}_{}x{}.jpg".format(outputDir,
                                            str(index).zfill(2),
                                            str(size[0]).zfill(2),
                                            str(size[1]).zfill(2)), output)


import os
import numpy as np
import datetime
import pickle
import json
import nibabel as nib
from pathlib import Path

def get_base_path(training):
    base_path = str(Path(__file__).parent.parent.parent) + "/"

    if training:
        checkpoint_path = base_path + "ckp_" + datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + "/"
        os.mkdir(checkpoint_path)
        return base_path, checkpoint_path
    else:
        return base_path

def get_data_folder_path(base_path, shape, samples):
    folder_path = base_path + "shape" + str(shape) + "_ex" + str(samples) + "_" + str(datetime.datetime.now().strftime("%Y%m%d_%H%M%S")) + "/"
    os.mkdir(folder_path)
    return folder_path

def save_pickle(path, array):
    with open(path, 'wb') as handle:
        pickle.dump(array, handle, protocol=pickle.HIGHEST_PROTOCOL)

def load_pickle(path):
    with open(path, 'rb') as handle:
        return pickle.load(handle)

def save_json(path, array):
    with open(path, 'w') as f:
        json.dump(array, f)

def load_json(path):
    with open(path) as json_file:
        return json.load(json_file)

def save_nii(volume, path):
    nib.save(nib.Nifti1Image(volume, np.eye(4)), path)

def load_nii(path):
    return nib.load(path).get_data()

def add_padding(volumes, pad_size):
    assert (len(volumes.shape) == 5 and len(pad_size) == 3)
    padded_volumes = []
    shape = volumes.shape[1:]
    for volume in volumes:
        # Add one if shape is not EVEN
        padded = np.pad(volume[:, :, :, 0], [(int(shape[0] % 2 != 0), 0), (int(shape[1] % 2 != 0), 0), (int(shape[2] % 2 != 0), 0)], 'constant', constant_values=(0.0))

        # Calculate how much padding to give
        val_x = (pad_size[0] - padded.shape[0]) // 2
        val_y = (pad_size[1] - padded.shape[1]) // 2
        val_z = (pad_size[2] - padded.shape[2]) // 2

        # Append padded volume
        padded_volumes.append(np.pad(padded, [(val_x,), (val_y,), (val_z,)], 'constant', constant_values=(0.0)))

    padded_volumes = np.array(padded_volumes)
    assert (padded_volumes.shape[1] == pad_size[0] and padded_volumes.shape[2] == pad_size[1] and padded_volumes.shape[3] == pad_size[2])

    return np.expand_dims(padded_volumes, -1), np.array(shape[:-1]), np.array([val_x, val_y, val_z])

def remove_padding(volumes, orig_shape, values):
    assert (len(volumes.shape) == 5 and len(orig_shape) == 3 and len(values) == 3)
    unpadded_volumes = []
    for volume in volumes:
        # Remove padding
        removed = volume[values[0]:-values[0], values[1]:-values[1], values[2]:-values[2]]

        # Append volume
        unpadded_volumes.append(removed[int(orig_shape[0] % 2 != 0):, int(orig_shape[1] % 2 != 0):, int(orig_shape[2] % 2 != 0):])

    unpadded_volumes = np.array(unpadded_volumes)
    shape = unpadded_volumes.shape
    assert (shape[1] == orig_shape[0] and shape[2] == orig_shape[1] and shape[3] == orig_shape[2])

    return unpadded_volumes

def apply_mask(volumes, masks):
    assert (len(volumes.shape) == 5 and len(masks.shape) == 5)
    return np.array([volumes[i] * masks[i] for i in range(len(volumes))])

def add_noise(noise_typ, volume, param):
    """
        'gauss'     : Gaussian-distributed additive noise.
        'poisson'   : Poisson-distributed noise generated from the data.
        's&p'       : Replaces random pixels with 0 or 1.
        'speckle'   : Multiplicative noise using out = image + n*image, where n is uniform noise with specified mean & variance.
    """
    assert (len(volume.shape) == 5)
    if noise_typ == "gauss":
        batch, row, col, dep, ch = volume.shape
        mean = 0
        var = param
        sigma = var ** 0.5
        gauss = np.random.normal(mean, sigma, (batch, row, col, dep, ch))
        return volume + gauss
    elif noise_typ == "SP":
        assert (volume.shape[0] == 1 and volume.shape[-1] == 1)
        volume = volume[0, :, :, :, 0]
        s_vs_p = 0.5
        amount = param
        out = np.copy(volume)

        # Salt mode
        """num_salt = np.ceil(amount * volume.size * s_vs_p)
        coords = [np.random.randint(0, i - 1, int(num_salt)) for i in volume.shape]
        out[tuple(coords)] = 1"""

        # Pepper mode
        num_pepper = np.ceil(amount * volume.size * (1. - s_vs_p))
        coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in volume.shape]
        out[tuple(coords)] = 0

        return np.expand_dims(np.array([out]), -1)
    elif noise_typ == "speckle":
        batch, row, col, dep, ch = volume.shape
        mean = 0
        var = param
        sigma = var ** 0.5
        gauss = sigma * np.random.randn(batch, row, col, dep, ch)
        return volume + volume * gauss
    else:
        raise NotImplementedError("Noise type not found.")


import logging,json,findspark
findspark.init()
import pyspark
from time import sleep
from pathlib import Path
from pyspark.sql import SparkSession,Window
import pyspark.sql.functions as F
from pyspark.sql.types import *
import matplotlib.pyplot as plt
import seaborn as sns
#%matplotlib inline

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
prefix_check = ["GB","US","CA","FR","JP","KR","RU","DE","BR"]
output_dir = f"../output/"
data_dir = f"../data/"

def create_spark_session(description):
    logger.info("Reading Spark object")
    spark = SparkSession.builder.appName(description).getOrCreate()
    logger.info("Spark read ok")

    return spark

def process_csv(spark,csv):

    prefix  = Path(csv).name[0:2]

    csv_schema = StructType([StructField("video_id",StringType(),False),
                            StructField("title",StringType(),True),
                            StructField("publishedAt",DateType(),True),
                            StructField("channelId",StringType(),True),
                            StructField("channelTitle",StringType(),True),
                            StructField("categoryId",ShortType(),True),
                            StructField("trending_date",DateType(),True),
                            StructField("tags",StringType(),True),
                            StructField("view_count",LongType(),True),
                            StructField("likes",LongType(),True),
                            StructField("dislikes",LongType(),True),
                            StructField("comment_count",LongType(),True),
                            StructField("comments_disabled",BooleanType(),True),
                            StructField("ratings_disabled",BooleanType(),True),
                            StructField("description",StringType(),True),
                            StructField("country",StringType(),True)]
                        )

    logger.info("Reading csv to dataframe using csv_schema")
    df_pyspark = spark.read.csv(csv, header=True,schema=csv_schema)

    logger.info("transforming dataframe")

    ############################################################
    #add trend count to df
    ############################################################
    trending_count = df_pyspark.groupby("video_id").count().withColumnRenamed("count","trendCount")

    df_pyspark = df_pyspark.join(trending_count,"video_id").select(df_pyspark["*"],trending_count["trendCount"])

    #use window function to drop all duplicate row
    win = Window.partitionBy("video_id").orderBy("trending_date")

    df_pyspark = df_pyspark.withColumn('rank', F.row_number().over(Window.partitionBy(df_pyspark.video_id).orderBy(df_pyspark.trending_date.desc())))\
                        .filter(F.col('rank') == 1)\
                        .drop("rank")

    ############################################################
    #read category name from json and add to df
    ############################################################
    logger.info("read category json")
    category_id = {}
    try:
        with open("../data/"+prefix+"_category_id.json","r") as f:
            category = json.load(f)
    except:
        logger.info("Error at loading/reading json, check if json file exist")
        return
    else:
        for cat in category["items"]:
            category_id[int(cat["id"])] = cat["snippet"]["title"]

    cat = list(map(list, category_id.items()))

    category = spark.createDataFrame(cat, ["categoryId", "categoryName"])

    logger.info("add category name to df")

    df_pyspark = df_pyspark.join(category,"categoryId").select(df_pyspark["*"], category["categoryName"])

    logger.info("create cat like")

    cat_like = df_pyspark.groupby("categoryName")\
                        .sum("likes","dislikes")\
                        .withColumnRenamed("sum(likes)","totalLikes")\
                        .withColumnRenamed("sum(dislikes)","totalDislikes")\
                        .withColumn("totalDislikes",F.col("totalDislikes")+1)\
                        .withColumn("liketoDislike",((F.col("totalLikes"))/(F.col("totalDislikes"))).cast(IntegerType()))\
                        .orderBy("totalLikes",ascending=False)

    logger.info("to pandas")

    spark.conf.set("spark.sql.execution.arrow.pyspark.enabled", "true")

    ############################################################
    #create graph for like to dislike vidoe category
    ############################################################
    logger.info("start plotting most_liked_category")

    plt.figure(figsize=(16, 14))
    plt.title("Like to dislike ratio for each category in " + prefix,fontsize = 24,fontweight = "bold")
    plt.ticklabel_format(style="plain",useOffset=False)
    sns.color_palette("light:b", as_cmap=True)
    ax = sns.barplot(x="liketoDislike", y="categoryName", data=cat_like.toPandas(), color="c")
    ax.set_xlabel ("Likes-Dislikes Ratio",fontsize = 16)
    ax.set_ylabel ("Category Name",fontsize = 16)

    plt.savefig(output_dir+prefix+"_most_liked_category.png")

    ############################################################
    #create graph for most trending category base on view count
    ############################################################
    logger.info("start plotting most_trending _category")

    cat_trend = df_pyspark.groupby("categoryName").count().orderBy("count",ascending=False)
    plt.clf()
    plt.figure(figsize=(16, 14))
    plt.title("Most trending video category in " + prefix,fontsize = 24,fontweight = "bold")
    plt.ticklabel_format(style='plain',useOffset=False)
    sns.color_palette("flare", as_cmap=True)
    ax = sns.barplot(x="count", y="categoryName", data=cat_trend.toPandas(),orient='h')
    ax.set_xlabel ("Number of trending video",fontsize = 16)
    ax.set_ylabel ("Category Name",fontsize = 16)

    plt.savefig(output_dir+prefix+"_most_trending_category.png")

if __name__ == "__main__":

    csv_list = [i for i in (Path(data_dir).glob("*.csv"))]

    session = create_spark_session("nlp word cloud generation")
    for csv in csv_list:
        if csv.name[0:2] not in prefix_check:
            logger.info("Inappropriate file name in folder, abort now")
            break
        else:
            process_csv(session,str(csv))
            sleep(5)


from absl.testing import absltest
from learned_optimization.optimizers import test_utils as opt_test_utils
from learned_optimization.research.general_lopt import prefab

class PrefabLOptTest(absltest.TestCase):

  def test_lopt(self):
    opt_test_utils.smoketest_optimizer(prefab.LearnedOptimizer(100))

  def test_lopt_accum(self):
    opt = prefab.LearnedOptimizer(100, max_training_steps=10)
    self.assertEqual(opt.opt.num_average, 10)
    opt_test_utils.smoketest_optimizer(opt)

  def test_lopt_wd(self):
    opt = prefab.LearnedOptimizer(100, weight_decay=1e-6)
    opt_test_utils.smoketest_optimizer(opt)

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


#https://github.com/python-visualization/folium

import folium
import pandas

df = pandas.read_csv("Volcanoes-USA.txt")

map = folium.Map(location=[df['LAT'].mean(), df['LON'].mean()], zoom_start=12, tiles="Stamen Terrain") #latitude and longitude

def color(elev):
    min_color = int(min(df['ELEV']))
    step = int((max(df['ELEV']) - min_color) / 3)
    if elev in range(min_color, min_color + step):
        return 'green'
    elif elev in range(min_color + step, min_color + 2 * step):
        return 'orange'
    else:
        return 'red'

for lat, lon, name, elev in zip(df['LAT'], df['LON'], df['NAME'], df['ELEV']):
    folium.Marker(location=[lat, lon], popup=name, icon=folium.Icon(color=color(elev))).add_to(map)

map.save("test_map_2.html")