import sys
import pygame
from settings import Settings
from Player import player
from Bullet import bullet
import soundeffects as se
from Zombie import zombie

class ZombieShooter:
    """Overall class to manage game assets and behavior."""

    def __init__(self):
        """Initialize the game, defines and creates the game's resources."""
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height ))#, pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        pygame.display.set_caption("Zombie Shooter")

        self.Player = player(self)
        self.Zombie = zombie(self)
        self.bullets = pygame.sprite.Group()
        self.zombies = pygame.sprite.Group()
        self.Players = pygame.sprite.Group()
        self.angle = 90
        self.angle_bullet = self.angle
        self.mouse_click_x, self.mouse_click_y = 0, 0
        self.curr_player_x, self.curr_player_y = 0, 0

        self.zombie_health = 3
        self.zombie1_health = self.zombie_health

        self.clock = pygame.time.Clock()
        self.base_font = pygame.font.Font('28 Days Later.ttf', 45)
        self.user_text = '*Space to Survive the Zombie*'
        self.extra_text = '*Shoot it as many times as you can before it catches up*'
        self.input_rect = pygame.Rect(200, 360, 380, 0)
        self.color_active = pygame.Color('lightskyblue3')
        self.color_passive = pygame.Color('chartreuse4')
        self.color = self.color_passive
        self.active = False
        self.s = 0
        self.bullet_zombie_collision = False

        self.score = 0

        se.backgroundsound.play(loops=-1)

    def run_game(self):
        """Charlie Ross helped create start button and start screen"""
        """Makes a home screen so that the game only begins after the spacebar is pressed"""
        """Calls all of the functions defined below to make a playable game"""
        while True:
            if self.s == 0:
                for event in pygame.event.get():
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_SPACE:
                            self.s = 1
                            self.active = True
                        else:
                            pygame.quit()
                            sys.exit()
                self.screen.fill((255, 0, 0))
                if self.active:
                    self.color = self.color_active
                    self.s = 1
                else:
                    self.color = self.color_passive
                pygame.draw.rect(self.screen, self.color, self.input_rect)

                text_surface1 = self.base_font.render(self.user_text, True, (255, 255, 255))
                self.screen.blit(text_surface1, (self.input_rect.x + 20, self.input_rect.y + 5))
                self.input_rect.w = max(100, text_surface1.get_width() + 10)

                text_surface2 = self.base_font.render(self.extra_text, True, (255, 255, 255))
                self.screen.blit(text_surface2, (self.input_rect.x - 150, self.input_rect.y + 60))
                self.input_rect.w = max(100, text_surface2.get_width() + 10)

                pygame.display.flip()
                self.clock.tick(60)
            elif self.s == 1:
                self._check_events()
                self._check_bullet_zombie_collisions()
                self._check_zombie_player_collisions()
                self.Player.update()
                self._update_screen()
                self.Zombie.rotate_zombie(self.Player)
                self.Zombie.update(self)
                self._update_bullets()
                self.Player.rotate_player()

    def _check_events(self):
        """Defines keydown events and mouse clicks."""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                self.mouse_click_x, self.mouse_click_y = pygame.mouse.get_pos()
                self.curr_player_x, self.curr_player_y = self.Player.rect.x, self.Player.rect.y
                self._fire_bullet()
                se.bulletsound.play()

    def _check_keydown_events(self, event):
        """Defines the arrow keys as player movement, as well as making the q key a quit"""
        if event.key == pygame.K_RIGHT:
            self.Player.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.Player.moving_left = True
        elif event.key == pygame.K_UP:
              self.Player.moving_up = True
        elif event.key == pygame.K_DOWN:
              self.Player.moving_down = True
        elif event.key == pygame.K_q:
            sys.exit()

    def _check_keyup_events(self, event):
        """Restricts the code to movement only while a key is pressed down"""
        if event.key == pygame.K_RIGHT:
            self.Player.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.Player.moving_left = False
        elif event.key == pygame.K_UP:
            self.Player.moving_up = False
        elif event.key == pygame.K_DOWN:
            self.Player.moving_down = False

    def _fire_bullet(self):
        """Used to create a new bullet and call the function from bullet"""
        if len(self.bullets) < self.settings.bullets_allowed:
            new_bullet = bullet(self)
            self.bullets.add(new_bullet)

    def _update_bullets(self):
        """Used to call the update from the bullet class as well as define the edge of the screen as boundaries"""
        self.bullets.update()
        for bullet in self.bullets.copy():
            mouse_x, mouse_y = pygame.mouse.get_pos()
            if bullet.rect.centerx >= self.settings.screen_width or bullet.rect.centerx < 0:
                self.bullets.remove(bullet)
            if bullet.rect.centery >= self.settings.screen_height or bullet.rect.centery < 0:
                    self.bullets.remove(bullet)

    def _check_bullet_zombie_collisions(self):
        """Code used to detect the collision between a bullet and a zombie"""
        self.zombies.add(self.Zombie)
        collisions = pygame.sprite.groupcollide(self.bullets, self.zombies, True, True)
        if collisions:
            self.bullet_zombie_collision = True
            self.score += 1
            self.settings.zombie_speed += .05

    def _check_zombie_player_collisions(self):
        """Define the Player on Zombie collision and cause you to lose if the zombie touches you."""
        self.zombies.add(self.Zombie)
        collision = pygame.sprite.collide_rect(self.Player, self.Zombie)
        if collision:
            print("Game Over")
            sys.exit()

    def show_score(self):
        """Code used to construst the score/hit board"""
        """I was helped by Eli Watson"""
        score_txt = self.base_font.render("Hits*" + str(self.score), True, (133,133,133))
        self.screen.blit(score_txt, (80,80))

    def _update_screen(self):
        """This is used to update all of the moving objects on the screen"""
        self.screen.fill(self.settings.bg_color)
        self.Player.blitme()
        self.Zombie.blitme()
        self.show_score()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        pygame.display.flip()

if __name__ == '__main__':
    """Makes a game instance, and runs the game."""
    ai = ZombieShooter()
    ai.run_game()


import pandas as pd
import numpy as np

from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif

df = pd.read_csv(
	filepath_or_buffer='adult.data',
	header=None,
	sep=',')

df.columns=['Idade', 'Emprego', 'Peso final', 'Educação', 'Anos de Estudo', 'Estado Civil', 'Ocupação', 'Relacionamento', 'Raça', 'Sexo', 'Ganhos de Capital', 'Perdas de Capital', 'Horas de trabalho por semana', 'País de Nascimento', 'classe']
df.dropna(how="all", inplace=True)
# drops the empty line at file-end

df2=df.drop(columns=['Emprego', 'Educação', 'Estado Civil', 'Ocupação', 'Relacionamento', 'Raça', 'Sexo', 'País de Nascimento'])

atributos=df2[['Idade', 'Peso final', 'Anos de Estudo', 'Ganhos de Capital', 'Perdas de Capital', 'Horas de trabalho por semana']]
classe=df2[['classe']]

X, y = atributos.values, classe.values
##print(X.shape)

selector = SelectKBest(f_classif, k=2)
X_new = selector.fit_transform(X, y)

#print(X_new.shape)

mask = selector.get_support()
new_features = atributos.columns[mask]

print(df2.columns)
print(new_features)
print(selector.scores_)


import json
import torch
from torch.utils import data
import numpy as np

class LolDataset(data.Dataset):
    ''' Lol dataset crawled from op.gg'''

    def __init__(self, json_file, normal=True):
        with open(json_file, 'r') as f:
            self.lol_data = json.load(f)
        self.normal = normal

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

    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()

        data = self.lol_data[idx]
        features = (data['team_1'] + data['team_2'])

        return {
            'draft': torch.from_numpy(np.asarray(features)/148) if self.normal else torch.from_numpy(np.asarray(features)),
            'result': torch.tensor(data['result'])}

def data_loader(source):
    with open(source, 'r') as f:
        data = json.load(f)
        print(data)

if __name__ == '__main__':
    source = 'data/dataset/processed.json'
    lol_data = LolDataset(source)
    dataloader = data.DataLoader(lol_data, batch_size=4, shuffle=True)
    for i_batch, sample_batched in enumerate(dataloader):
        print(i_batch, sample_batched['draft'], sample_batched['result'])
        break


# Instalam request si beautifulsoup cu pip install in terminal
import requests

# Definim URL-ul paginii pe care dorim sa o analizam
url = "https://animax.ro/collections/pasari"
base_url=url
#Preluam html-ul si folosim dictionar headers pt a nu ni se bloca accesul de catre server
Headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0;  Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
response = requests.get(url, headers=Headers)
html = response.content

#Verificam daca solicitarea a fost realizata cu succes
if response.status_code == 200:
    print("Solicitarea a fost realizata cu succes")
else:
   print("Solicitarea are buba")

from bs4 import BeautifulSoup

#Cream liste in care vom stoca datele obtinute pentru scrierea in fisier
BRAND=[]
NUME=[]
CODARTICOL=[]
PRET=[]
PRET_Redus=[]

#definim variabila pentru parcurgerea paginilor
page_num=2

while requests.get(url, headers=Headers).status_code==200  :

    soup = BeautifulSoup(html, 'html.parser')
    #Gasim toate elementele de tip div cu clasa necesara
    products = soup.find_all('div', class_='col-6 col-sm-6 col-md-4 col-lg-4 col-xl-3')

    #cream noul url pt pagina urmatoare
    url=f'{base_url}?page={page_num}'
    page_num=page_num+1

    #html pentru noua pagina
    html = requests.get(url, headers=Headers).content

    #extrasem din pagina informatiile referitoare la butonul de "Urmatoarea pagina"
    next_page_link = soup.find("a", {"class": "pagination__item pagination__item--prev pagination__item-arrow btn"})

    #Pentru fiecare produs gasim anterior, luam brandul, numele, id-ul, pretul si pretul redus(daca exista)
    #Preturile vor fi transformate prin cast in date de tip float

    for product in products:
        #Luam brandul, numele, id-ul si pretul produsului
        brand = product.find('div', class_='product-collection__more-info mb-3').text.strip()
        BRAND.append(brand)
        nume= product.find('h4', class_='m-0').text.strip()
        NUME.append(nume)
        codArticol=product.find('p', class_='m-0').text.strip().replace('Cod articol: ','')
        CODARTICOL.append(codArticol)
        pret = product.find('span', class_ = 'price' ).text.strip().replace(' lei',' ')
        Pret=[]
        for x in pret:
            if x!=" ":
                Pret.append(x)
            else:
                break
        pret="".join(Pret)

        # pret_redus=product.find('span', class_ = 'price price--sale')
        # Daca exista, luam si pretul redus

        if product.find('span', class_ = 'price price--sale') is None:
            pret_redus = 'NaN'
            #pret = product.find('span', class_ = 'price').text.strip().replace(' lei','')
        else:
            pret_redus=product.find('span', class_= 'price price--sale').text.strip().replace(' lei','').replace(pret, "")
            pret_redus=pret_redus.replace(",",".")
            pret_redus=float(pret_redus)
            #print("s a facut strip", pret_redus)

        #cast la float
        #tratam cazul in care pretul este mai mare de 999.99 si vor aparea 2 puncte in float-> va produce eroare
        if len(pret)<7:
            pret=pret.replace(",",".")
        else:
            pret=pret.replace(",","", 1).replace(",",".")
        pret=float(pret)

        PRET.append(pret)
        PRET_Redus.append(pret_redus)

        #afisam si pe ecran
        print(brand, nume, codArticol, pret, pret_redus)

    #daca nu avem butonul de "Urmatoarea pagina "  in pagina curenta, inseamna ca este ultima pagina si iesim din while/
    # am terminat de extras produse din categoria respectiva
    if  next_page_link is None:
       break

#importam csv pentru scrierea datelor in fisier csv
import csv

CapTabel=["Cod_Articol","Brand","Nume","Pret", "Pret_Redus"]

#Cu fisierul deschis, scriem capul de tabel si informatiile obtinute, pe fiecare rand
with open('Produse_Animax.csv', 'w',encoding='utf-8-sig', newline='') as file:
    writer = csv.writer(file)
    #scriem capul de tabel
    writer.writerow(CapTabel)

    #scriem datele in fisier
    for i in range(len(BRAND)):

        writer.writerow([CODARTICOL[i],BRAND[i],NUME[i],PRET[i],PRET_Redus[i]])


import numpy as np
import cv2
from scipy.ndimage.filters import gaussian_filter
from scipy.signal import convolve2d

def main():
    # load and prepare image
    image_rgb = cv2.imread('../data/rectangles.jpeg')
    show_image(image_rgb, 'original image', destroy_windows=False)

    image_gray = convert2gray(image_rgb)
    show_image(image_gray, 'gray-scale image')

    J = compute_structure_tensor(image_gray, sigma=0.5, rho=0.5, show=True)

    eigenvalues = compute_eigenvalues(J)
    c, e, f = generate_feature_masks(eigenvalues, thresh=0.003)
    show_feature_masks(c, e, f)

# convert to gray scale and normalize for float
# (OpenCV treats color pixels as BGR)
def convert2gray(image_rgb):
    temp = cv2.cvtColor(image_rgb, cv2.COLOR_BGR2GRAY)
    image_gray = temp.astype(np.float32) / 255.0

    return image_gray

# compute smooth version of image with parameter sigma (zero-padding)
def filter_gauss(image, sigma):
    # TODO: filter image using sigma and zero padding (filter mode 'constant')
    img_filtered = gaussian_filter(image, sigma, mode='constant')  # TODO: replace None by your result
    return img_filtered

# compute the structure tensor for an image
def compute_structure_tensor(image, sigma=0.5, rho=0.5, show=False):
    # perform gaussian filtering before computing the gradient (parameter is sigma)
    image = filter_gauss(image, sigma)

    # compute the gradient image
    img_gradient = compute_gradient(image)
    if show:
        show_gradient(img_gradient)

    # create and compute the structure tensor (dimY x dimX x 2 x 2)
    # local 2x2-tensor J = [[f_x ^ 2 f_x * f_y], [f_x * f_y f_y ^ 2]]
    J = np.empty((np.shape(image)[0], np.shape(image)[1], 2, 2))
    # print(J.shape)
    # f_x squared
    # TODO
    J[:, :, 0, 0] = img_gradient[:, :, 0] * img_gradient[:, :, 0]
    # f_y squared
    # TODO
    J[:, :, 1, 1] = img_gradient[:, :, 1] * img_gradient[:, :, 1]
    # f_x * f_y
    J[:, :, 0, 1] = img_gradient[:, :, 0] * img_gradient[:, :, 1]
    J[:, :, 1, 0] = J[:, :, 0, 1]

    # TODO
    # relaxation step (parameter is rho)
    # TODO: use filter_gauss to filter the tensor components
    # print(J.shape)
    J = filter_gauss(J, rho)
    return J

# compute gradients
def compute_gradient(image):
    # create two channel image for the gradients (dimY x dimX x 2)
    img_gradient = np.empty((np.shape(image)[0], np.shape(image)[1], 2))
    # print(image.shape)

    # the filter kernel and convolution for forward differences in x direction
    x_kernel = np.asarray([[1, -1]], dtype=np.float32)  # TODO: fix the kernel for forward differences
    # print(x_kernel)
    img_gradient[:, :, 0] = convolve2d(image, x_kernel, mode='same')  # TODO: apply x_kernel
    # print(img_gradient[:,:,0])

    # the filter kernel and convolution for forward differences in y direction
    y_kernel = np.asarray([[1], [-1]], dtype=np.float32)  # TODO: fix the kernel for forward differences
    img_gradient[:, :, 1] = convolve2d(image, y_kernel, mode='same')  # TODO: apply y_kernel

    return img_gradient

# create array for the eigenvalues and compute them
def compute_eigenvalues(tensor):
    evs = np.empty((np.shape(tensor)[0], np.shape(tensor)[1], 2))
    print('Computing eigenvalues, this may take a while...')

    # TODO: implement the computation of the eigenvalues
    # TODO (Hint): make use of np.linalg.eig(...)
    evs, _ = np.linalg.eig(tensor)

    return evs

# generate masks for corners, straight edges, and flat areas
def generate_feature_masks(evs, thresh=0.005):
    corners = np.zeros(np.shape(evs[:, :, 0]))
    straight_edges = np.zeros(np.shape(evs[:, :, 0]))
    flat_areas = np.zeros(np.shape(evs[:, :, 0]))

    for i in range(0, np.shape(evs)[0]):
        for j in range(0, np.shape(evs)[1]):

            # TODO: analyze the eigenvalues evs, and assign corners, edges, and flat areas accordingly
            lam1 = evs[i, j, 0]
            lam2 = evs[i, j, 1]
            if lam1 < thresh:
                if lam2 < thresh:
                    flat_areas[i, j] = 1
            if lam1 > thresh:
                if lam2 < thresh:
                    straight_edges[i, j] = 1
            if lam2 > thresh:
                if lam1 < thresh:
                    straight_edges[i, j] = 1
            if lam1 > thresh:
                if lam2 > thresh:
                    corners[i, j] = 1

    return corners, straight_edges, flat_areas

# show feature masks
def show_feature_masks(c, e, f, destroy_windows=True):
    print('[| Features are indicated by white. |]')

    show_image(c, 'corners', False)
    show_image(e, 'straight edges', False)
    show_image(f, 'flat areas', destroy_windows)

# visualize the gradient components and the gradient norm
def show_gradient(img_gradient, destroy_windows=True):
    # compute the norm for display purposes
    img_gradient_norm = np.sqrt(
        img_gradient[:, :, 0] * img_gradient[:, :, 0] + img_gradient[:, :, 1] * img_gradient[:, :, 1])

    # the gradients are in the range of [-1, +1], so we rescale for display purposes
    show_image(img_gradient[:, :, 0] / 2.0 + 0.5, 'x gradients', False)
    show_image(img_gradient[:, :, 1] / 2.0 + 0.5, 'y gradients', False)
    show_image(img_gradient_norm, 'gradient L2-norm', destroy_windows)

# function for displaying an image and waiting for user input
def show_image(i, t, destroy_windows=True):
    cv2.imshow(t, i)

    print('Press a key to continue...')
    cv2.waitKey(0)

    if destroy_windows:
        cv2.destroyAllWindows()

if __name__ == '__main__':
    main()

P1 - Structure Tensor/code/structureTensorExercise.py


import numpy as np
from matplotlib import pyplot as plt

# Area setting
xmin, xmax = 0, 10
ymin, ymax = -5, 5
xpoints, ypoints = 200, 200
x = np.linspace(xmin, xmax, xpoints)
y = np.linspace(ymin, ymax, ypoints)

# meshgrid function can create a rectangular grid out of two given one-dimensional arrays
xx, yy = np.meshgrid(x, y, sparse = False)

# Join these arrays along the axis
points = np.concatenate([xx.reshape(-1, 1), yy.reshape(-1, 1)], axis=-1)

# Defining two wave sources
source1=np.array([0, 0.5])
source2=np.array([0, -0.5])
points1 = points - source1
points2 = points - source2

# Parameters
A1= 5
A2= 5
k = 30

# Same frequency for both wave sources
wave1 = A1*(np.sin( k * (points1[:, 0]**2 + points1[:, 1]**2)**0.5))
wave2 = A2*(np.sin( k * (points2[:, 0]**2 + points2[:, 1]**2)**0.5))

# Intensities
A = (wave1 + wave2)
intensity = A**1

# Size of graph
plt.figure(figsize=(7, 7))
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)

# Spreading through the whole graph
plt.scatter(points[:, 0], points[:, 1], c = intensity, cmap=plt.cm.binary)
plt.scatter(*source1, c='blue')
plt.scatter(*source2, c='blue')

# Values of movements
interval = 0.5
radius = np.linspace(xmin, xmax, int((xmax-xmin)/interval))
velocity = 2

# Python interactive display
from IPython.display import display, clear_output
plt.style.use('dark_background')

fig, ax = plt.subplots(figsize=(7, 7))
plt.xlim(xmin, xmax-1)
plt.ylim(ymin, ymax)

cs1, cs2 = [], []

# Setting Wave 1 and Wave 2
for r in radius:
  circle1=plt.Circle(source1, r, facecolor=(1, 0, 0, 0), edgecolor='red')
  cs1.append(ax.add_artist(circle1))

  circle2=plt.Circle(source2, r, facecolor=(1, 0, 0, 0), edgecolor='yellow')
  cs2.append(ax.add_artist(circle2))

plt.scatter(*source1, c='white')
plt.scatter(*source2, c='white')

# Animation setting
for i in range(10000):
  [c.set_radius(radius[i]) for i, c in enumerate(cs1)]
  [c.set_radius(radius[i]) for i, c in enumerate(cs2)]
  radius = (radius + velocity)%(xmax-xmin)
  clear_output(wait=True)
  display(fig)

# Python simulation designed by Sayan Dey, check out his work on https://sayan1999.github.io/


import unittest
from enum import Enum
from random import shuffle
from copy import deepcopy
from itertools import count

from zombiaki_karty import *
from zombiaki_plansza import *

#################################################################################
class Actions(Enum):
	Noop = 0
	PlaceCard = 1
	MoveCards = 2
	RemoveCards = 3
	DiscardCard = 4
	TakeCards = 5

class Action:
	def __init__(self, action, args=None, position=None):
		self.action = action
		self.args = args
		self.position = position

""" Przebieg gry:
1 runda
	zombiaki
		sprzątanie
		przesuwanie kota
		przesuwanie psa
		przesuwanie zombie
		dobieranie
		wyrzucanie
		zagranie kart
2 runda
	Ludzie
		sprzątanie
		przesuwanie
		dobieranie
		wyrzucanie
		zagranie kart
2 runda
	zombiaki
		...
	ludzie
		...
3 runda """

class Phase(Enum):
	RemoveExpiredCards=0
	CatMovement=1   #skipped if no cat in play
	DogMovement=2   #skipped if no dog in play
	OtherCardMovement=3
	TakeNewCards=4
	DiscardCard=5
	Play1stCard=6
	Play2ndCard=7
	Play3rdCard=8
	NumberOfPhases=9

	@staticmethod
	def next(currentPhase, currentPlayer, useCat, useDog):
		vn = currentPhase.value + 1
		#need two if's as both can be skipped
		if vn==Phase.CatMovement.value\
			and (not useCat or currentPlayer == Player.Humans):
			vn += 1
		if vn==Phase.DogMovement.value\
			and (not useDog or currentPlayer == Player.Humans):
			vn += 1
		nextRonud=False
		if vn==Phase.NumberOfPhases.value:
			vn = Phase.RemoveExpiredCards
			nextRonud=True
		return Phase(vn), nextRonud

class GameState:
	@classmethod
	def init(cls):
		cls.PhaseToLegalMoves = { Phase.RemoveExpiredCards  : cls.getLegalMoves_in_removeExpiredCards,
								  Phase.CatMovement         : cls.getLegalMoves_in_CatMovement,
								  Phase.DogMovement         : cls.getLegalMoves_in_DogMovement,
								  Phase.OtherCardMovement   : cls.getLegalMoves_in_Movement,
								  Phase.TakeNewCards        : cls.getLegalMoves_in_takeNewCards,
								  Phase.DiscardCard         : cls.getLegalMoves_in_DiscardCard,
								  Phase.Play1stCard         : cls.getLegalMoves_in_PlayCard,
								  Phase.Play2ndCard         : cls.getLegalMoves_in_PlayCard,
								  Phase.Play3rdCard         : cls.getLegalMoves_in_PlayCard }
		cls.PhaseToExec       = { Phase.RemoveExpiredCards  : cls.execMove_in_removeExpiredCards,
								  Phase.CatMovement         : cls.execMove_in_Movement,
								  Phase.DogMovement         : cls.execMove_in_Movement,
								  Phase.OtherCardMovement   : cls.execMove_in_Movement,
								  Phase.TakeNewCards        : cls.execMove_in_takeNewCards,
								  Phase.DiscardCard         : cls.execMove_in_DiscardCard,
								  Phase.Play1stCard         : cls.execMove_in_PlayCard,
								  Phase.Play2ndCard         : cls.execMove_in_PlayCard,
								  Phase.Play3rdCard         : cls.execMove_in_PlayCard }
	@staticmethod
	def createRandom():
		zombiesDeck = list(deepcopy(ZombiesCards))
		shuffle(zombiesDeck)
		humansDeck  = list(deepcopy(HumansCards))
		shuffle(humansDeck)
		return GameState.create(zombiesDeck, humansDeck)

	@staticmethod
	def create(zombiesDeck, humansDeck):
		gs = GameState()
		gs.board = Board()
		gs.isDogInPlay = False
		gs.isCatInPlay = False
		gs.cemetary = []
		gs.barricade = []
		gs.thrash = []
		gs.zombiesDeck = zombiesDeck
		gs.humansDeck = humansDeck
		gs.player = Player.Zombies
		gs.phase = Phase.TakeNewCards
		gs.roundNo = 1
		gs.deck = gs.zombiesDeck
		gs.hand = gs.cemetary
		return gs

	def nextPhase(self):
		self.phase,nextPlayer = Phase.next(self.phase, self.player, self.isCatInPlay, self.isDogInPlay)
		if nextPlayer:
			self.roundNo += 1
			self.player = Turn.next(self.player)
			if self.player == Player.Zombies:
				self.deck = self.zombiesDeck
				self.hand = self.cemetary
			else:
				self.deck =  self.humansDeck
				self.hand = self.barricade

	def getLegalMoves(self, player):
		if player != self.player: return [Action(Actions.Noop)]
		return GameState.PhaseToLegalMoves[self.phase](self, player)

	def getNextState(self, player, move):
		ns = deepcopy(self)
		GameState.PhaseToExec[ns.phase](ns, player,move)
		ns.nextPhase()
		return ns

	def getLegalMoves_in_removeExpiredCards(self, player):
		expired = self.board.findExpiredCards(player, self.roundNo)
		return [ Action(Actions.RemoveCards,expired) ]

	def getLegalMoves_in_takeNewCards(self, player):
		numCardsToTake = min( 4 - len(self.hand), len(self.deck) )
		return [ Action(Actions.TakeCards,numCardsToTake) ]

	def getLegalMoves_in_CatMovement(self, player):
		kot = self.board.findCardByType(Kot)
		assert(kot is not None)
		positions = kot.getLegalPositionsAfterMove(self.board)
		return [ Action(Actions.MoveCards, kot, pos)  for pos in positions ]

	def getLegalMoves_in_DogMovement(self, player):
		pies = self.board.findCardByType(Pies)
		assert(pies is not None)
		positions = pies.getLegalPositionsAfterMove(self.board)
		return [ Action(Actions.MoveCards, pies, pos)  for pos in positions ]

	def getLegalMoves_in_Movement(self, player):
		return [ Action(Actions.MoveCards, moveable) ]

	def getLegalMoves_in_DiscardCard(self, player):
		assert(self.hand is not None)
		return [ Action(Actions.DiscardCard, card) for card in self.hand ]

	def getLegalMoves_in_PlayCard(self, player):
		Mv = [ Action(Action.Noop) ]
		for card in self.hand:
			ValidPositions = self.board.getCardValidPositions(card)
			Mv.extend( [ Action(Actions.PlaceCard, c, vp) for vp in ValidPositions] )
		return Mv

	def execMove_in_removeExpiredCards(self, player, move):
		assert(move.action == Actions.RemoveCards)
		Bc = [self.board.removeCard(pos) for pos in move.args]
		for bc in Bc:
			self.thrash.extend( bd.cards )

	def execMove_in_Movement(self, player, move):
		assert(move.action == Actions.MoveCards)
		for pos in move.args:
			self.board.moveCard(pos)

	def execMove_in_takeNewCards(self, player, move):
		self.hand.extend( self.deck[0 : move.args] )
		del self.deck[0 : move.args]

	def execMove_in_DiscardCard(self, player, move):
		 self.hand.remove(move.args)
		 self.thrash.append(move.args)

	def execMove_in_PlayCard(self, player, move):
		pass

class CardTests(unittest.TestCase):
	def test_BulletProofZombie(self):
		z = BulletProofZombie(3)
		self.assertEqual( z.owner, Player.Zombies )
		self.assertTrue( z.isZombie )
		self.assertTrue( z.isMovable )

	def test_Kot(self):
		z = Kot()
		self.assertEqual( z.owner, Player.Zombies )
		self.assertTrue( z.isZombie )
		self.assertTrue( z.isMovable )
		self.assertEqual(z.wounds, 1)

	def test_Czlowiek(self):
		z = Czlowiek()
		self.assertEqual( z.owner, Player.Zombies )
		self.assertFalse( z.canAttachTo( Kot() ))
		self.assertFalse( z.canAttachTo( Pies() ))
		self.assertTrue( z.canAttachTo( BulletProofZombie(3) ))

	def test_compare_zombie_cards(self):
		b1 = BulletProofZombie(3)
		b2 = BulletProofZombie(3)
		self.assertEqual(b1,b2)

	def test_kot_initial_position(self):
		b = Board.create()
		kot =Kot()
		b.placeCard( Zombie(2), Position(0,0), 1 )
		validPositions = kot.getLegalInitialPositions(b)
		expvp = [ Position(0,1), Position(0,2) ]
		for p in validPositions:self.assertIn( p, expvp )
		for p in expvp:	 		self.assertIn( p, validPositions )

	def test_kot_movement(self):
		b = Board.create()
		kot = Kot()
		b.placeCard( kot, 		Position(0,1), 1 )
		b.placeCard( Zombie(2), Position(0,0), 1 )
		b.placeCard( Zombie(2), Position(1,1), 1 )
		b.placeCard( Zapora(), 	Position(4,0), 1 )
		b.placeCard( Zapora(), 	Position(4,1), 1 )
		b.placeCard( Zapora(), 	Position(4,2), 1 )
		vp = list(kot.getLegalPositionsAfterMove(b))
		exp_vp = [ Position(0,0), Position(0,1), Position(0,2), Position(1,1), Position(2,1) ]
		for p in vp: 		self.assertTrue( p in exp_vp )
		for p in exp_vp:	self.assertTrue( p in vp )

class TurnsTests(unittest.TestCase):
	def test1(self):
		p = Phase.RemoveExpiredCards
		pn,nextPlayer = Phase.next(p, Player.Zombies, False,False)
		self.assertEqual(pn, Phase.OtherCardMovement)
		self.assertFalse(nextPlayer)
		p = Phase.Play3rdCard
		pn,nextPlayer = Phase.next(p, Player.Zombies, False,False)
		self.assertEqual(pn, Phase.RemoveExpiredCards)
		self.assertTrue(nextPlayer)

	def test2(self):
		p = Phase.RemoveExpiredCards
		pn,nextPlayer = Phase.next(p,Player.Zombies, True,False)
		self.assertEqual(pn, Phase.CatMovement)
		self.assertFalse(nextPlayer)

		pn,nextPlayer = Phase.next(pn,Player.Zombies, True,False)
		self.assertEqual(pn, Phase.OtherCardMovement)
		self.assertFalse(nextPlayer)

		p = Phase.CatMovement
		pn,nextPlayer = Phase.next(p,Player.Zombies, True,True)
		self.assertEqual(pn, Phase.DogMovement)
		self.assertFalse(nextPlayer)
		pn,nextPlayer = Phase.next(pn,Player.Zombies, False, False)
		self.assertEqual(pn, Phase.OtherCardMovement)
		self.assertFalse(nextPlayer)

	def test3(self):
		p = Phase.RemoveExpiredCards
		pn,nextPlayer = Phase.next(p,Player.Humans, True,True)
		self.assertEqual(pn, Phase.OtherCardMovement)
		self.assertFalse(nextPlayer)

class BoardTests(unittest.TestCase):
	def test1(self):
		c1 = Kot()
		c2 = Kot()
		c3 = Kot()
		b = Board.create()
		b.placeCard( c1, Position(0,0), 1)
		b.placeCard( c2, Position(1,1), 1)
		b.placeCard( c3, Position(2,2), 2)
		e = b.findExpiredCards(1)
		self.assertEqual(e, [])
		e = b.findExpiredCards(2)
		self.assertEqual(e, [])
		e = b.findExpiredCards(3)
		self.assertEqual(e, [Position(0,0), Position(1,1)])
		e = b.findExpiredCards(4)
		self.assertEqual(e, [Position(0,0), Position(1,1), Position(2,2)])
		c = b.removeCard(Position(2,2))
		self.assertEqual(c.cards[0],c3)
		c = b.removeCard(Position(1,1))
		self.assertEqual(c.cards[0],c2)
		c = b.removeCard(Position(0,0))
		self.assertEqual(c.cards[0],c1)
		e = b.findExpiredCards(4)
		self.assertEqual(e, [])

	def test2(self):
		b = Board.create()
		b.placeCard(Kot(), Position(4,2), 1)
		c = b.findCardByType(Kot)
		self.assertTrue(c is not None)
		self.assertEqual(type(c),Kot)

		b.removeCard(Position(4,2))
		c = b.findCardByType(Kot)
		self.assertTrue(c is None)

class GameStateTests(unittest.TestCase):
	@classmethod
	def setUpClass(cls):
		GameState.init()

	def test1(self):
		#1st Zombie action after create
		s = GameState.createRandom()
		Mv = s.getLegalMoves(Player.Zombies)
		self.assertEqual( len(Mv), 1 )
		self.assertEqual( Mv[0].action, Actions.TakeCards )
		self.assertEqual( Mv[0].args, 4 )

	def test2(self):
		#1st human action after create
		s = GameState.createRandom()
		Mv = s.getLegalMoves(Player.Humans)
		self.assertEqual( len(Mv), 1 )
		self.assertEqual( Mv[0].action, Actions.Noop )

	def test3(self):
		#TakeCard action
		s = GameState.createRandom()
		self.assertEqual(s.hand,[])
		nc = len(s.deck)
		tod = s.deck[0:4]
		Mv = s.getLegalMoves(Player.Zombies)
		self.assertEqual(Mv[0].action, Actions.TakeCards)
		ns = s.getNextState(Player.Zombies, Mv[0])
		#check if s does not changes
		self.assertEqual(s.phase, Phase.TakeNewCards)
		self.assertEqual(s.hand, [])
		self.assertEqual(len(s.deck), nc)
		#check ifns is next phase
		self.assertEqual(ns.hand, tod)
		self.assertEqual(len(ns.deck), nc-4)
		self.assertEqual(ns.phase, Phase.DiscardCard)

	def test4(self):
		#DiscardCard action
		s = GameState.createRandom()
		Mv = s.getLegalMoves(Player.Zombies)
		self.assertEqual(Mv[0].action, Actions.TakeCards)
		s = s.getNextState(Player.Zombies, Mv[0])
		Mv = s.getLegalMoves(Player.Zombies)
		self.assertEqual(len(Mv),4)
		for m,i in zip(Mv,count(0)):
			self.assertEqual( m.action, Actions.DiscardCard )
			self.assertEqual( m.args, s.hand[i] )
		ns = s.getNextState(Player.Zombies, Mv[0])
		self.assertTrue( Mv[0].args not in ns.hand )
		self.assertTrue( Mv[0].args in ns.thrash )
		for i in range(1,4):
			c = Mv[i].args
			self.assertTrue( c in ns.hand )
			self.assertTrue( c not in ns.thrash )
		self.assertEqual(ns.phase, Phase.Play1stCard)

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


"""Example to show how to plot spectrum of Fermi/LAT sources.
"""
import matplotlib.pyplot as plt
from gammapy.catalog import (
    SourceCatalog3FGL,
    SourceCatalog2FHL,
    SourceCatalog1FHL,
    SourceCatalog3FHL,
)

def plot_source_spectrum(source, label, color):
    opts = dict(energy_power=2, flux_unit="erg-1 cm-2 s-1")
    source.spectral_model.plot(
        energy_range=source.energy_range, label=label, color=color, **opts
    )
    source.spectral_model.plot_error(
        energy_range=source.energy_range, facecolor=color, **opts
    )
    source.flux_points.plot(sed_type="dnde", color=color, **opts)

def plot_source_spectra(name):
    plot_source_spectrum(
        source=SourceCatalog3FGL()[name], label="Fermi 3FGL", color="r"
    )
    plot_source_spectrum(
        source=SourceCatalog2FHL()[name], label="Fermi 2FHL", color="g"
    )
    plot_source_spectrum(
        source=SourceCatalog1FHL()[name], label="Fermi 1FHL", color="c"
    )
    plot_source_spectrum(
        source=SourceCatalog3FHL()[name], label="Fermi 3FHL", color="b"
    )

    ax = plt.gca()
    ax.set_ylim(1.0e-12, 7.0e-11)
    ax.set_xlim(1.0e-4, 2.0)
    ax.set_xlabel("Energy (TeV)")
    ax.set_ylabel("E^2 dN/dE (erg cm-2 s-1])")
    plt.legend(loc=0)

if __name__ == "__main__":
    # Select your favourite source
    # (must be named like this in ASSOC columns of all catalogs)
    name = "PKS 2155-304"
    plot_source_spectra(name)
    plt.show()


from typing import Any, Dict, List, Optional

import httpx
from fastapi import APIRouter, Query
from pydantic import BaseModel, Field

from .config import settings
from .models import *
from .utils import get_association_all, get_pub_info

BASE_API_URL = settings.monarch_api_url

router = APIRouter()

############################
### Gene -> Disease endpoint
############################

@router.get(
    "/gene-diseases",
    response_model=DiseaseAssociations,
    description="Get a list of diseases associated with a gene",
    summary="Get a list of diseases associated with a gene",
    response_description="A DiseaseAssociations object containing a list of DiseaseAssociation objects",
    operation_id="get_gene_disease_associations",
)
async def get_gene_disease_associations(
    gene_id: str = Query(..., description="The identifier of the gene.", example="HGNC:1884"),
    limit: Optional[int] = Query(10, description="The maximum number of associations to return."),
    offset: Optional[int] = Query(0, description="Offset for pagination of results"),
) -> DiseaseAssociations:

    causalAssociations = await get_association_all(
        category="biolink:CausalGeneToDiseaseAssociation",
        entity=gene_id,
        limit=limit,
        offset=offset,
    )

    correlatedAssociations = await get_association_all(
        category="biolink:CorrelatedGeneToDiseaseAssociation",
        entity=gene_id,
        limit=limit,
        offset=offset,
    )

    associations = []
    for item in causalAssociations.get("items", []):
        disease = Disease(disease_id=item.get("object"), label=item.get("object_label"), type="causal")
        assoc = DiseaseAssociation(disease=disease, metadata={"relationship": "causal"})
        associations.append(assoc)

    for item in correlatedAssociations.get("items", []):
        disease = Disease(disease_id=item.get("object"), label=item.get("object_label"))
        assoc = DiseaseAssociation(disease=disease, metadata={"relationship": "correlated"})

        for pub in item.get("publications", []):
            assoc.publications.append(get_pub_info(pub))

        associations.append(assoc)

    return DiseaseAssociations(associations=associations,
                               total=causalAssociations.get("total", 0) + correlatedAssociations.get("total", 0),
                               disease_url_template = settings.monarch_ui_url + "/disease/{disease_id}")


import sys

sys.path.append("..")  # Allows imports from sibling directories

import pytest
from raw_data_manager import models
from raw_data_manager.raw_data_splitter import RawDataSplitter
from sklearn.model_selection import train_test_split
import pandas as pd
import pathlib
import shutil
from unittest.mock import patch, MagicMock

dataset_version = "10103"

class TestRawDataSplitter:
    @pytest.fixture
    def mock_metadata_table(self):
        metadata = {
            "path": [
                "path1",
                "path2",
                "path3",
                "path4",
                "path5",
                "path6",
                "path7",
                "path8",
            ],
            "source": [
                models.EventSource.REAL,
                models.EventSource.SIMULATED,
                models.EventSource.HAND_DRAWN,
                models.EventSource.REAL,
                models.EventSource.REAL,
                models.EventSource.SIMULATED,
                models.EventSource.HAND_DRAWN,
                models.EventSource.REAL,
            ],
            "class_type": [
                models.EventClassType.ABRUPT_INCREASE_BSW,
                models.EventClassType.FLOW_INSTABILITY,
                models.EventClassType.FLOW_INSTABILITY,
                models.EventClassType.SEVERE_SLUGGING,
                models.EventClassType.ABRUPT_INCREASE_BSW,
                models.EventClassType.FLOW_INSTABILITY,
                models.EventClassType.FLOW_INSTABILITY,
                models.EventClassType.SEVERE_SLUGGING,
            ],
        }
        return pd.DataFrame(metadata)

    @pytest.fixture
    def raw_data_splitter(self, mock_metadata_table):
        return RawDataSplitter(mock_metadata_table, dataset_version)

    @pytest.mark.parametrize(
        "sources, expected",
        [
            ([models.EventSource.REAL], "r"),
            ([models.EventSource.SIMULATED, models.EventSource.HAND_DRAWN], "s-d"),
            ([models.EventSource.HAND_DRAWN, models.EventSource.SIMULATED], "s-d"),
            (
                [
                    models.EventSource.HAND_DRAWN,
                    models.EventSource.REAL,
                    models.EventSource.SIMULATED,
                ],
                "all",
            ),
            (None, "all"),
        ],
    )
    def test_get_source_name(self, raw_data_splitter, sources, expected):
        print("sources", sources)
        print("expected", expected)
        print(raw_data_splitter)

        # = raw_data_splitter()
        assert raw_data_splitter._RawDataSplitter__get_source_name(sources) == expected

    @pytest.mark.parametrize(
        "class_types, expected",
        [
            (
                [models.EventClassType.ABRUPT_INCREASE_BSW],
                str(models.EventClassType.ABRUPT_INCREASE_BSW.value),
            ),
            (
                [models.EventClassType.SEVERE_SLUGGING],
                str(models.EventClassType.SEVERE_SLUGGING.value),
            ),
            (
                [models.EventClassType.FLOW_INSTABILITY, models.EventClassType.NORMAL],
                str(models.EventClassType.NORMAL.value)
                + "-"
                + str(models.EventClassType.FLOW_INSTABILITY.value),
            ),
            (None, "all"),
        ],
    )
    def test_get_class_type_name(self, raw_data_splitter, class_types, expected):
        assert (
            raw_data_splitter._RawDataSplitter__get_class_type_name(class_types)
            == expected
        )

    @pytest.mark.parametrize(
        "well_ids, expected",
        [([6], "6"), ([1, 2], "1-2"), ([3, 12, 1], "1-3-12"), (None, "all")],
    )
    def test_get_well_ids_name(self, raw_data_splitter, well_ids, expected):
        assert (
            raw_data_splitter._RawDataSplitter__get_well_ids_name(well_ids) == expected
        )

    @pytest.mark.parametrize(
        "test_size, class_types, sources, well_ids, expected_train, expected_test",
        [
            (
                0.2,
                [models.EventClassType.NORMAL],
                [models.EventSource.REAL],
                [1, 2],
                f"dataset_converted_v{dataset_version}_split-20_source-r_class-0_well-1-2_train",
                f"dataset_converted_v{dataset_version}_split-20_source-r_class-0_well-1-2_test",
            ),
            (
                0.25,
                [
                    models.EventClassType.NORMAL,
                    models.EventClassType.ABRUPT_INCREASE_BSW,
                ],
                [models.EventSource.REAL, models.EventSource.SIMULATED],
                [1, 2],
                f"dataset_converted_v{dataset_version}_split-25_source-r-s_class-0-1_well-1-2_train",
                f"dataset_converted_v{dataset_version}_split-25_source-r-s_class-0-1_well-1-2_test",
            ),
            (
                0.3,
                None,
                None,
                None,
                f"dataset_converted_v{dataset_version}_split-30_source-all_class-all_well-all_train",
                f"dataset_converted_v{dataset_version}_split-30_source-all_class-all_well-all_test",
            ),
        ],
    )
    def test_get_split_name(
        self,
        raw_data_splitter,
        test_size,
        class_types,
        sources,
        well_ids,
        expected_train,
        expected_test,
    ):
        train_name, test_name = raw_data_splitter.get_split_name(
            test_size, class_types, sources, well_ids
        )
        assert train_name == expected_train
        assert test_name == expected_test

    @patch("shutil.copy")
    def test_copy_file(self, mock_copy, raw_data_splitter):
        raw_data_splitter.copy_file(
            "path/0/to_file.feather", pathlib.Path("output_dir")
        )
        mock_copy.assert_called_once_with(
            pathlib.Path("path/0/to_file.feather"),
            pathlib.Path("output_dir/0/to_file.feather"),
        )


import logging
import time
from scapy.all import *

logging.basicConfig(filename='arp_poisoning.log',
					format='[%(asctime)s] %(message)s',
					datefmt='%d/%m/%Y %I:%M:%S %p',
					level=logging.DEBUG)

ip_to_mac = {}

def arp_monitor_callback(pkt):
	if ARP in pkt and pkt[ARP].op in (1,2): #ARP request or reply
		if pkt[ARP].psrc in ip_to_mac: #known IP
			if ip_to_mac[pkt[ARP].psrc] != pkt[ARP].hwsrc: #MAC address mismatch
				print('\n[!] ARP Poisoning detected!')
				print('Victim IP: ' + pkt[ARP].psrc)
				print('Victim MAC: ' + ip_to_mac[pkt[ARP].psrc])
				print('Attacker IP: ' + pkt[ARP].pdst)
				print('Attacker MAC: ' + pkt[ARP].hwsrc)
				logging.warning('ARP Poisoning detected! Victim IP: ' + pkt[ARP].psrc + ', Victim MAC: ' + ip_to_mac[pkt[ARP].psrc] + ', Attacker IP: ' + pkt[ARP].pdst + ', Attacker MAC: ' + pkt[ARP].hwsrc)
		else:
			ip_to_mac[pkt[ARP].psrc] = pkt[ARP].hwsrc

print('[*] Starting ARP Monitor...')
sniff(prn=arp_monitor_callback, filter='arp', store=0)


# Import argparse for command line automation processes

import time

# starting time
start = time.time()

import argparse

# Now, you should create a parser instance using ArgumentParser()
parser = argparse.ArgumentParser()

parser.add_argument("directory_path",
                    help="enter directory_path")
# Now, we will add a optional argument ( remember linux style )

# action :: --> behavior of script when using this option
# for example :: we are using here a "store_true" action, this tell the script that we well store a default
# true of false value and this default value is passed in the default argument
# dest :: the name of the attribute that will be used in the rest of our code
parser.add_argument("-u",action="store_true", dest="unixformat", default=False,help="maintain unix time format")

args = parser.parse_args()

import json
import re
import pandas as pd
records = [json.loads(line) for line in open(args.directory_path)]
# Print the data frame
df = pd.DataFrame(records)

df= df[['a', 'r','u','cy','ll','tz','t','hc']]
df = df.dropna()
df = df.reset_index(drop=True)

def get_browser(s):
    x=re.search("\w*/\d\.\d",str(s))
    if x:
        return x.group()
    else :
        return None

df['web_browser'] = df['a'].apply(get_browser)

df['operating_sys']= df.a.str.extract(r'\(([^)]+)\)', expand=True)
df['operating_sys']= df.operating_sys.str.extract(r'^([\w\-]+)', expand=True)

#def get_os(s):
#    if s is not None:
#        e=str(s).split()
#        if len(e) > 1:
#            return e[1][1:].replace(";", "")
#        else:
#            return None

#df['operating_sys'] = df['a'].apply(get_os)

df['from_url']= df.r.str.extract(r'([\w_-]+(?:(?:\.[\w_-]+)+))', expand=True)

df['to_url']= df.u.str.extract(r'([\w_-]+(?:(?:\.[\w_-]+)+))', expand=True)

df['city'] = df['cy']

def get_longitude(s):
        e=str(s).replace('[','').split(', ')[0]
        return e

df['longitude'] = df['ll'].apply(get_longitude)

def get_latitude(s):
        e=str(s).replace(']','').split(', ')[1]
        return e

df['latitude'] = df['ll'].apply(get_latitude)

df['time_zone']=df['tz']

df['time_in']=df['t']

time_in = []
for i, row in df.iterrows():
    stamp = pd.to_datetime(row['time_in'], unit = 's').tz_localize(row['time_zone']).tz_convert('UTC')
    time_in.append(stamp)

df['time_in'] =time_in

df['time_out']=df['hc']

time_out = []
for i, row in df.iterrows():
    stamp2 = pd.to_datetime(row['time_out'], unit = 's').tz_localize(row['time_zone']).tz_convert('UTC')
    time_out.append(stamp2)

df['time_out'] =time_out

#    print(x)

#for x in df['time_zone']:
#        print(x)

if args.unixformat:
    df['time_in']=df['t']
    df['time_out']=df['hc']

#df.head(50)

df_final=df[['web_browser','operating_sys','from_url','to_url','city','longitude','latitude','time_zone','time_in','time_out']]

df_final = df_final.dropna()
df_final = df_final.reset_index(drop=True)

if args.directory_path=="/home/nourhan/Downloads/ITI_Python/task2/Task2/usa.gov_click_data_1.json" :
	target_file="/home/nourhan/Downloads/ITI_Python/task2/Task2/target/newfile1.csv"
elif args.directory_path=="/home/nourhan/Downloads/ITI_Python/task2/Task2/usa.gov_click_data_2.json" :
	target_file="/home/nourhan/Downloads/ITI_Python/task2/Task2/target/newfile2.csv"
else :
	target_file="/home/nourhan/Downloads/ITI_Python/task2/Task2/target/newfile3.csv"

df_final.to_csv (target_file, index = True, header=True)

#df_final.head(20)

print('done inshallaaaaa')

print('number of rows transformed',df_final.shape[0])
print('target file is',target_file)

checksums = {}
duplicates = []

# Import subprocess
from subprocess import PIPE, Popen
# Import os
from os import listdir
# Get all files in the current directory
from os.path import isfile, join
files = [item for item in listdir('.') if isfile(join('.', item ))]

# Iterate over the list of files filenames
for filename in files:
    # Use Popen to call the md5sum utility
    with Popen(["md5sum", filename], stdout=PIPE) as proc:
        checksum, _ = proc.stdout.read().split()

        # Append duplicate to a list if the checksum is found
        if checksum in checksums:
            duplicates.append(filename)
        checksums[checksum] = filename

print(f"Found Duplicates: {duplicates}")

# program body ends

# end time
end = time.time()

# total time taken
print(f"Runtime of the program is {end - start}")


def mars_scrape():
	"""
		From Mars related sites scrape content for eventual
		insert into mongodb and display on web page
	"""
	import pandas as pd
	import requests
	import time
	from bs4 import BeautifulSoup
	from splinter import Browser
	executable_path = {'executable_path': 'chromedriver.exe'}
	return_list = []
	# ### NASA Mars News
	with  Browser('chrome', **executable_path, headless=True) as browser:
		url = 'https://mars.nasa.gov/news/'
		# open web page
		browser.visit(url)
		# scrape web page using beautiful soup
		html = browser.html
		soup = BeautifulSoup(html, 'html.parser')
		# find latest title and paragraph
		news_title = soup.find('div', class_='content_title').a.text
		return_list.append(news_title)
		time.sleep(3)
		news_p = soup.find('div', class_='article_teaser_body').text
		return_list.append(news_p)
		time.sleep(3)

	print('News title --> ' + news_title)
	print('News paragraph --> \n' + news_p)

	# ### JPL Mars Space Images - Featured Image
	with  Browser('chrome', **executable_path, headless=True) as browser:
		jpl_url = 'https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars'
		# open web page
		browser.visit(jpl_url)
		# scrape web page using beautiful soup
		html = browser.html
		soup = BeautifulSoup(html, 'html.parser')
		# Get url for image jpg
		jpl_url = soup.find('div', class_= 'jpl_logo').a['href']
		url_last = soup.find('div', class_='default floating_text_area ms-layer').footer.a['data-fancybox-href']
		image_url = jpl_url[:-1] + url_last
		return_list.append(image_url)
	print('image url --> ' + image_url)

	# ### Mars Weather

	twitter_url = 'https://twitter.com/marswxreport?lang=en'
	response = requests.get(twitter_url)
	soup = BeautifulSoup(response.text, 'html.parser')
	mars_weather = soup.find('p', class_="TweetTextSize TweetTextSize--normal js-tweet-text tweet-text").text
	print('mars_weather = ' + mars_weather)
	return_list.append([mars_weather])
	# ### Mars Facts

	url = 'http://space-facts.com/mars/'
	# Use pandas to read all tables from the web page into an array of dataframes
	df = pd.read_html(url)
	df[0].columns = ['description','value']
	# Assign a dataframe with an html table
	mars_html_table = df[0].to_html(index=False)
	print(mars_html_table)
	return_list.append(mars_html_table)
	# ### Mars Hemisperes

	with  Browser('chrome', **executable_path, headless=True) as browser:
		astro_url = 'https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars'
		# open web page
		browser.visit(astro_url)
		# scrape web page using beautiful soup
		html = browser.html
		soup = BeautifulSoup(html, 'html.parser')
		results = soup.find_all('div', class_='item')
		mars_images = []
		# Read through items to get images from second page
		count = 1
		for first_page in results:
			# Get image title from first page
			title_text = first_page.h3.text
			# Click on title's link on first page and go to second page
			browser.click_link_by_partial_text(first_page.h3.text)
			# Wait for second page processing
			time.sleep(2)
			# Get second page's url
			page2_url = browser.url
			# Visit second page so parsing can begin
			browser.visit(page2_url)
			# Get second page's html for parsing
			html2 = browser.html
			# Begin parse of second page using beautifulsoup
			soup2 = BeautifulSoup(html2, 'html.parser')
			# Get Sample image's url on second page
			img_url = soup2.find('a',href=True, text='Sample')['href']
			# Save title and image url to list of dicts
			title_desc = "title" + str(count)
			img_desc = "img_url" + str(count)
			mars_images.append({"title" : title_text,"img_url" : img_url})
			# Go back to first page
			browser.back()
		return_list.append(mars_images)
	# print('mars_images = ')
	# print(mars_images[0])
	# print(mars_images[1])
	# print(mars_images[2])
	# print(mars_images[3])
	title_text1 = mars_images[0]["title"]
	title_text2 = mars_images[1]["title"]
	title_text3 = mars_images[2]["title"]
	title_text4 = mars_images[3]["title"]
	image_url1 = mars_images[0]["img_url"]
	image_url2 = mars_images[1]["img_url"]
	image_url3 = mars_images[2]["img_url"]
	image_url4 = mars_images[3]["img_url"]
	return {"news_title": news_title,
			"news_p": news_p,
			"images_url": image_url,
			"mars_weather": mars_weather,
			"mars_html_table": mars_html_table,
			"title1": title_text1,
			"img_url1": image_url1,
			"title2": title_text2,
			"img_url2": image_url2,
			"title3": title_text3,
			"img_url3": image_url3,
			"title4": title_text4,
			"img_url4": image_url4
			}


from os import path
from datetime import datetime
from itertools import chain

from ..source_code.get_process_SRS_data_02 import parse_SRS_data, \
    hale_class_count, macintosh_class_count, HALE_CLASSES, MACINTOSH_CLASSES

class TestAnalyseSRS(object):
    def test_parse_SRS(self):
        with open(path.join('tests', 'data', '20120105SRS.txt')) as file:
            data = file.read()
            res = parse_SRS_data(data)

            assert res[0] == [datetime(2012, 1, 5, 0, 30), '1386', 's17w87', '150', '0030', 'hsx',
                              '03', '01', 'alpha']

            assert len(res) == 6

    def test_hale_class_count(self):
        # Create test data each class occurs i time for i 1 ... 7
        test_classes = [[cls for j in range(i+1)] for i, cls in enumerate(HALE_CLASSES)]
        # Flatten nested lists
        test_classes = list(chain.from_iterable(test_classes))
        # Add incorrect class which should be dropped
        test_classes.append('wrong')

        count = hale_class_count(test_classes)

        assert len(count) == len(HALE_CLASSES)
        assert sorted(list(count.values())) == list(range(1, len(HALE_CLASSES) + 1))

    def test_macintosh_class_count(self):
        # Create test data each class occurs i time for i 1 ... 7
        test_classes = [[cls for j in range(i+1)] for i, cls in enumerate(MACINTOSH_CLASSES)]
        # Flatten nested lists
        test_classes = list(chain.from_iterable(test_classes))
        # Add incorrect class which should be dropped
        test_classes.append('wrong')

        count = macintosh_class_count(test_classes)

        assert len(count) == len(MACINTOSH_CLASSES)
        assert sorted(list(count.values())) == list(range(1, len(MACINTOSH_CLASSES) + 1))


#!/usr/bin/python3

#   Authors: Wesley Ferreira de Ferreira and Willian Ferreira de Ferreira
#   Date: 6/9/2018
#   All Rights Reserved
#   V0.1

import sys, socket
from multiprocessing import Process
from treatment.mycrypt import key_exchange
from treatment.request import recebe_requisicao
from treatment.respose import envia_resposta

def recieve_message(source, cliente):
    print("connection from: {}".format(cliente))

    #DIFFIE-HELMAN
    secrete_key = key_exchange(source)
    print("Troca de chaves concluida")

    #FICA ESCUTANDO AS REQUISIÇÕES DO CLIENTE
    try:
        while True:
            msg, valid = recebe_requisicao(secrete_key, source)
            print("Requisição recebida de {}".format(cliente))
            envia_resposta(secrete_key, msg, valid, source)
    except:
        print("Conexão encerrada")

    source.close()

def main():

    if(len(sys.argv) < 2):
        print("Porfavor execute como: ./server.py <numero da porta>")
        exit()

    #DEFINE OS PARAMETROS DE CONEXÃO
    HOST = ""
    PORT = int(sys.argv[1])
    ORIGIN = (HOST, PORT)

    #INICIA A CONEXÃO
    connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    connection.bind(ORIGIN)
    connection.listen(5)

    #LOOP DE REQUISIÇÕES DO SERVIDOR
    while True:
        source, cliente = connection.accept()
        p = Process(target=recieve_message, args=(source, cliente)) #DISPARA O PROCESSO PRA A REQUISIÇÃO
        p.start() #INICIA A EXECUÇÃO

    connection.close() #FECHA A CONEXÃO

if __name__ == "__main__":
    main()


import numpy as np
import pandas as pd
from regressionML import RegressionML
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from statistics import stdev
from statistics import mean
import winsound
from sklearn.feature_selection import f_regression
import matplotlib.pyplot as plt
import seaborn as sns
from model_setting import Model_Setting

class SensitivityAnalysisExperiment(Model_Setting):
    def __init__(self, regression=True):
        super().__init__()

        self.cf['targets'] = ['OV (Oily value)']
        # self.cf['targets'] = ['OS (Oil separation)']
        # self.cf['targets'] = ['Turbidity']

        self.size_experiments = 10
        self.ml = RegressionML()
        for clf in self.ml.regressors:
            name = type(clf).__name__
            self.excel[name + '-MAE'] = []

        for clf in self.ml.regressors:
            name = type(clf).__name__
            self.excel[name + '-MAE-STD'] = []

    def show_heatmap_matrix(self, df, columns):
        cm = np.corrcoef(df[columns].values.T)
        sns.set(font_scale=0.8)
        # hm = sns.heatmap(cm, char=True, annot=True, square=True, fmt='.2f', annot_kws={'size':15}, yticklabels=columns, xticklabels=columns)
        hm = sns.heatmap(cm, annot=True, square=True, fmt='.2f', annot_kws={'size': 15}, yticklabels=columns,
                         xticklabels=columns)
        plt.show()
        plt.close()

    def get_f_regression(self, X_train, y_train):
        f_test, _ = f_regression(X_train, y_train)

        results_f_dict = {}
        for i in range(X_train.shape[1]):
            results_f_dict[X_train.columns.values[i]] = f_test[i]

        results_f_dict = {k: v for k, v in sorted(results_f_dict.items(), key=lambda item: item[1], reverse=True)}

        print("======================")
        print("=       F-test       =")
        f_results = ''
        for k, v in results_f_dict.items():
            f_results += f'{k}\t{v}\r'
            print(f'{k}\t{v}')

        return f_results

    def run_ml_removing_sensitivity_analysis(self):
        # 1. Load data from xlsx
        excel_data = pd.read_excel(self.cf['file'], self.cf['sheet'])

        for X in self.cf['selected_Xs']:
            selected_Xs = self.cf['selected_Xs'].copy()
            selected_Xs.remove(X)
            print(f'******************** removed {X} ********************')
            for target in self.cf['targets']:
                print('target: ', target)
                self.cf['target'] = target

                # 2. Select variables
                data = excel_data[selected_Xs + [self.cf['target']]]

                # 3. Remove data if it contains 'nan'
                data = data[~data[self.cf['target']].isna()]
                if len(data) == 0:
                    continue

                # 4. Surfactant is converted to a numeric variable
                if 'Surfactant name' in data.columns.values:
                    data['Surfactant name'] = LabelEncoder().fit_transform(data['Surfactant name'])

                # 5. Normalization is performed
                scaled_df = MinMaxScaler(feature_range=(0, 1)).fit_transform(data)
                data = pd.DataFrame(scaled_df, index=data.index, columns=data.columns)

                # 6. Separate Xs and y
                df_X, df_y = data[selected_Xs], data[self.cf['target']]

                # 7. Convert y to a categorical variable for classification
                # df_y = self.convert_to_categorical_variable(df_y)

                # 9. Perform several ML experiments
                sum_results = None
                all_results = {}
                for i in range(self.size_experiments):
                    # 9. Split into training and test part
                    X_train, X_test, y_train, y_test = train_test_split(df_X, df_y, test_size=.2)

                    # 13. Set data X and y for ML
                    self.ml.set_train_test_data(X_train, X_test, y_train, y_test)

                    # 14. Perform ML
                    results = self.ml.perform_ML()

                    if len(all_results) == 0:
                        all_results = {x: [v] for x, v in results.items()}
                    else:
                        for x, v in all_results.items():
                            for x2, v2 in results.items():
                                if x2 == x:
                                    v.append(v2)

                    if sum_results is None:
                        sum_results = results
                    else:
                        sum_results = {x: v + v2 for x, v in sum_results.items() for x2, v2 in results.items() if x2 == x}

                # 15. Set all results for the excel output
                for clf in self.ml.regressors:
                    name = type(clf).__name__
                    # self.excel[name + '-MAE'].append(avg_results[name])
                    self.excel[name + '-MAE'].append(round(mean(all_results[name]), 4))
                    self.excel[name + '-MAE-STD'].append(round(stdev(all_results[name]), 4))

                self.excel['Train Size'].append(len(X_train))
                self.excel['Test Size'].append(len(X_test))
                self.excel['Target'].append(target)
                self.excel['X Removed'].append(X)
                self.excel['F Results'].append('')

        self.save_excel_file()

    def run_experiment(self, excel_data):
        for target in self.cf['targets']:
            print('target: ', target)
            self.cf['target'] = target

            # 2. Select variables
            data = excel_data[self.cf['selected_Xs'] + [self.cf['target']]]

            # 3. Remove data if it contains 'nan'
            data = data[~data[self.cf['target']].isna()]

            # 5. Normalization is performed
            scaled_df = MinMaxScaler(feature_range=(0, 1)).fit_transform(data)
            data = pd.DataFrame(scaled_df, index=data.index, columns=data.columns)

            # 6. Separate Xs and y
            df_X, df_y = data[self.cf['selected_Xs']], data[self.cf['target']]

            # 8. Perform Sensitivity Analysis
            f_results = ''
            # columns = df_X.columns.copy()
            # columns.append(self.cf['target'])
            # self.show_heatmap_matrix(data, columns)
            f_results = self.get_f_regression(df_X, df_y)

    def run(self):
        # 1. Load data from xlsx
        excel_data = pd.read_excel(self.cf['file'], self.cf['sheet'])

        self.run_experiment(excel_data)

        self.save_excel_file()

# 1. looks at F-score
# ex = SensitivityAnalysisExperiment().run()

# 2. performs one variable removal test
SensitivityAnalysisExperiment().run_ml_removing_sensitivity_analysis()

winsound.Beep(1000, 440)


from collections import deque
import common

lines = common.read_file('2020/24/data.txt').splitlines()
directions = []

for line in lines:
    curr_i = 0
    directions_acc = []
    while curr_i < len(line):
        l = 2 if line[curr_i] in {'n', 's'} else 1
        directions_acc.append(line[curr_i:curr_i+l])
        curr_i += l
    directions.append(directions_acc)

# part 1
field = set()
for line in directions:
    x, y = 0, 0
    for d in line:
        if d == 'nw':
            y -= 1
            x -= 1
        elif d == 'ne':
            y -= 1
        elif d == 'w':
            x -= 1
        elif d == 'e':
            x += 1
        elif d == 'sw':
            y += 1
        elif d == 'se':
            y += 1
            x += 1
        else:
            raise 'unknown direction'
    pos = x, y
    if pos in field:
        field.remove(pos)
    else:
        field.add(pos)

print(len(field))

# part 2
neighbors = [
    (-1, -1), (0, -1),
    (-1, 0), (1, 0),
    (0, 1), (1, 1)
]
def perform_step(field):
    result = set()
    seen = set()
    q = deque(field)
    while len(q) > 0:
        pos = q.popleft()
        if pos in seen:
            continue
        seen.add(pos)
        x, y = pos
        flipped = pos in field
        flipped_neighbors = 0
        for ndx, ndy in neighbors:
            nx = x + ndx
            ny = y + ndy
            npos = nx, ny
            if npos in field:
                flipped_neighbors += 1
            elif flipped:
                q.append(npos)

        if flipped and (flipped_neighbors == 1 or flipped_neighbors == 2):
            result.add(pos)
        if not flipped and flipped_neighbors == 2:
            result.add(pos)
    return result

for _ in range(100):
    field = perform_step(field)

print(len(field))


import os

import click
import sentry_sdk
from click.core import Context
from dotenv import load_dotenv
from sentry_sdk.integrations.atexit import AtexitIntegration

from controllers import auth, client, contract, employee, event
from db import get_session
from tools import atexit_callback

load_dotenv()
SENTRY_DSN = os.getenv("SENTRY_DSN")
TRACES_SAMPLE_RATE = float(os.getenv("TRACES_SAMPLE_RATE"))
PROFILES_SAMPLE_RATE = float(os.getenv("PROFILES_SAMPLE_RATE"))
DEBUG_MODE = os.getenv("DEBUG_MODE").lower() in ("1", "true")

@click.group(
    cls=click.CommandCollection,
    sources=[
        auth.auth_group,
        employee.employee_group,
        client.client_group,
        contract.contract_group,
        event.event_group,
    ],
)
@click.pass_context
def cli(ctx: Context):
    if not DEBUG_MODE:
        sentry_sdk.init(
            dsn=SENTRY_DSN,
            traces_sample_rate=TRACES_SAMPLE_RATE,
            profiles_sample_rate=PROFILES_SAMPLE_RATE,
            debug=DEBUG_MODE,
            integrations=[AtexitIntegration(callback=atexit_callback)],
        )
    ctx.meta["SESSION"] = ctx.with_resource(get_session().begin())
    ctx.meta["SENTRY"] = ctx.with_resource(
        sentry_sdk.start_transaction(name=ctx.invoked_subcommand)
    )

if __name__ == "__main__":
    cli()


import sys
input = sys.stdin.readline
from collections import deque

t = int(input())

for i in range(t):
    n = int(input())
    chass = [[0]* n for _ in range(n)]
    a,b = map(int, input().split())
    A,B = map(int, input().split())
    if a==A and b ==B:
        print(0)
        continue
    dx = [-2,-2,-1,-1,1,1,2,2]
    dy = [-1,1,-2,2,-2,2,-1,1]
    que = deque([])
    que.append([a,b])
    chass[a][b] = 1

    while que:
        x, y = que.popleft()
        if x == A and y == B:
            print(chass[x][y]-1)
            break

        for j in range(8):
            nx = x + dx[j]
            ny = y + dy[j]
            if nx >= 0 and nx < n and ny >= 0 and ny< n:
                if not chass[nx][ny]:
                    chass[nx][ny] = chass[x][y] + 1
                    que.append([nx,ny])


import socket
import logging
import struct
import select
import threading
import base64

def encryption(data):
    return base64.b64encode(data)

def decryption(data):
    return base64.b64decode(data)

def send_all(sock,data):
    send_bytes = 0
    while True:
        n = sock.send(data[send_bytes:])
        if n < 0:
            return n
        send_bytes += n
        if send_bytes == len(data):
            return send_bytes

def handle(client,remote):
    try:
        fdset = [client, remote]
        while True:
            r,w,e = select.select(fdset,[],[])

            if client in r:
                data = client.recv(1024)
                if len(data) <= 0:
                    break
                #data = decryption(data)
                result = send_all(remote,data)
                if result < len(data):
                    raise Exception('fail to send all data')

            if remote in r:
                data = remote.recv(1024)
                if len(data) <= 0:
                    break
                #data = encryption(data)
                result = send_all(client,data)
                if result < len(data):
                    raise Exception('fail to send all data')

    except Exception as e:
        raise(e)
    finally:
        client.close()
        remote.close()

def handle_connect(client,client_addr):
    data = client.recv(4)
    if data[1] != 1:
        client.close()
        return
    if data[3] == 1:
        addr_ip = client.recv(4)
        remote_addr = socket.inet_ntop(socket.AF_INET, addr_ip)
    elif data[3] == 3:
        blen = client.recv(1)
        addr_len = int.from_bytes(blen, byteorder = 'big')
        remote_addr = client.recv(addr_len)
    elif data[3] == 4:
        addr_ip = client.recv(16)
        remote_addr = socket.inet_ntop(socket.AF_INET6, addr_ip)
    else:
        client.close()
        return
    bport = client.recv(2)
    remote_addr_port = struct.unpack('>H',bport)
    try:
        remote = socket.create_connection((remote_addr,remote_addr_port[0]))
        print("connect form ",remote_addr,remote_addr_port[0])
    except socket.error as e:
        client.close()
        logging.error(e)
        return

    handle(client,remote)

def main():
    agency_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    agency_server.bind(('',1234))
    agency_server.listen(15)
    agency_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    try:
        while True:
            c, addr = agency_server.accept()
            t = threading.Thread(target = handle_connect,args =(c,addr))
            t.start()
    except socket.error as e:
        logging.error(e)
    except KeyboardInterrupt:
        agency_server.close()

main()


from PIL import Image
from PyQt5 import QtWidgets, uic
import sys
import cv2
import numpy as np
import matplotlib.pyplot as plt
import torch
from train_res50 import get_train_val_test_data , Res50_CD
from torchsummary import summary
import torchvision.models as models
import pickle

test_dataset = get_train_val_test_data(only_test_dataset=True)

class Q5(QtWidgets.QMainWindow, QtWidgets.QDialog):
    def __init__(self):
        super(Q5, self).__init__()

        uic.loadUi('Q5.ui', self)

        # load model & record
        self.load_model_and_record(
            record_path="tensorboard.png", model_path="pretrained.pt")

        # buttons
        self.showModelStructureButton.clicked.connect(self.showModelStructure)
        self.showTensorboardButton.clicked.connect(
            self.showTensorboard)
        self.TestButton.clicked.connect(self.Test)
        self.DataArgumentButton.clicked.connect(self.DataArgument)

        self.show()

    def load_model_and_record(self, record_path, model_path):

        # read train/test record
        self.tensorboard_img = cv2.imread(record_path)
        # load model
        self.model = Res50_CD()
        self.model.load_state_dict(torch.load(
            model_path, map_location=torch.device("cpu")))
        self.model.eval()

    def showModelStructure(self):
        #summary(self.model, (3, 32, 32))
        print(self.model)
    def showTensorboard(self):
        cv2.imshow("Q5-2" , self.tensorboard_img)

    def Test(self):
        idx = self.imgIndex.value()-1
        with torch.no_grad():
            img = cv2.imread(test_dataset.samples[idx][0])
            inp ,_ = test_dataset[idx]
            inp = inp.unsqueeze(0)
            try:
                pred = torch.argmax(self.model( inp ) , dim = -1)
                print(pred)
                if pred.item() == 0:
                    cv2.imshow(  "cat",img )
                else:
                    cv2.imshow(  "dog",img )
            except:
                print(f"Image Error! {test_dataset.samples[idx]}")
                pass

    def DataArgument(self):
        plt.bar(["Before Random-Erasing" , "After Random-Erasing"], [0.727,0.742],width=0.5 )
        plt.ylabel('Acc')
        plt.show()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Q5()
    app.exec_()


"""
# > Various modules for handling data
"""
from __future__ import division
from __future__ import absolute_import
import os
import random
import fnmatch
import numpy as np
from PIL import Image

def deprocess(x, np_uint8=True):
    # [-1,1] -> [0, 255]
    x = (x+1.0)*127.5
    return np.uint8(x) if np_uint8 else x

def preprocess(x):
    # [0,255] -> [-1, 1]
    return (x/127.5)-1.0

def augment(a_img, b_img):
    """
       Augment images - a is distorted
    """
    # randomly interpolate
    a = random.random()
    a_img = a_img*(1-a) + b_img*a
    # flip image left right
    if (random.random() < 0.25):
        a_img = np.fliplr(a_img)
        b_img = np.fliplr(b_img)
    # flip image up down
    if (random.random() < 0.25):
        a_img = np.flipud(a_img)
        b_img = np.flipud(b_img)
    return a_img, b_img

def getPaths(data_dir):
    exts = ['*.png','*.PNG','*.jpg','*.JPG', '*.JPEG']
    image_paths = []
    for pattern in exts:
        for d, s, fList in os.walk(data_dir):
            for filename in fList:
                if (fnmatch.fnmatch(filename, pattern)):
                    fname_ = os.path.join(d,filename)
                    image_paths.append(fname_)
    return np.asarray(image_paths)

def read_and_resize(path, img_res):
    im = Image.open(path).resize(img_res)
    if im.mode=='L':
        copy = np.zeros((res[1], res[0], 3))
        copy[:, :, 0] = im
        copy[:, :, 1] = im
        copy[:, :, 2] = im
        im = copy
    return np.array(im).astype(np.float32)

def read_and_resize_pair(pathA, pathB, img_res):
    img_A = read_and_resize(pathA, img_res)
    img_B = read_and_resize(pathB, img_res)
    return img_A, img_B

def get_local_test_data(data_dir, img_res=(256, 256)):
    assert os.path.exists(data_dir), "local image path doesnt exist"
    imgs = []
    for p in getPaths(data_dir):
        img = read_and_resize(p, img_res)
        imgs.append(img)
    imgs = preprocess(np.array(imgs))
    return imgs

class DataLoader():
    def __init__(self, data_dir, dataset_name, img_res=(256, 256), test_only=False):
        self.img_res = img_res
        self.DATA = dataset_name
        self.data_dir = data_dir
        if not test_only:
            self.trainA_paths = getPaths(os.path.join(self.data_dir, "trainA")) # distorted
            self.trainB_paths = getPaths(os.path.join(self.data_dir, "trainB")) # enhanced
            if (len(self.trainA_paths)<len(self.trainB_paths)):
                self.trainB_paths = self.trainB_paths[:len(self.trainA_paths)]
            elif (len(self.trainA_paths)>len(self.trainB_paths)):
                self.trainA_paths = self.trainA_paths[:len(self.trainB_paths)]
            else: pass
            self.val_paths = getPaths(os.path.join(self.data_dir, "validation"))
            self.num_train, self.num_val = len(self.trainA_paths), len(self.val_paths)
            print ("{0} training pairs\n".format(self.num_train))
        else:
            self.test_paths    = getPaths(os.path.join(self.data_dir, "test"))
            print ("{0} test images\n".format(len(self.test_paths)))

    def get_test_data(self, batch_size=1):
        idx = np.random.choice(np.arange(len(self.test_paths)), batch_size, replace=False)
        paths = self.test_paths[idx]
        imgs = []
        for p in paths:
            img = read_and_resize(p, self.img_res)
            imgs.append(img)
        imgs = preprocess(np.array(imgs))
        return imgs

    def load_val_data(self, batch_size=1):
        idx = np.random.choice(np.arange(self.num_val), batch_size, replace=False)
        pathsA = self.trainA_paths[idx]
        pathsB = self.trainB_paths[idx]
        imgs_A, imgs_B = [], []
        for idx in range(len(pathsB)):
            img_A, img_B = read_and_resize_pair(pathsA[idx], pathsB[idx], self.img_res)
            imgs_A.append(img_A)
            imgs_B.append(img_B)
        imgs_A = preprocess(np.array(imgs_A))
        imgs_B = preprocess(np.array(imgs_B))
        return imgs_A, imgs_B

    def load_batch(self, batch_size=1, data_augment=True):
        self.n_batches = self.num_train//batch_size
        for i in range(self.n_batches-1):
            batch_A = self.trainA_paths[i*batch_size:(i+1)*batch_size]
            batch_B = self.trainB_paths[i*batch_size:(i+1)*batch_size]
            imgs_A, imgs_B = [], []
            for idx in range(len(batch_A)):
                img_A, img_B = read_and_resize_pair(batch_A[idx], batch_B[idx], self.img_res)
                if (data_augment):
                    img_A, img_B = augment(img_A, img_B)
                imgs_A.append(img_A)
                imgs_B.append(img_B)
            imgs_A = preprocess(np.array(imgs_A))
            imgs_B = preprocess(np.array(imgs_B))
            yield imgs_A, imgs_B


# ceaser_cipher
import art

alphabets = ['a', 'b', 'c', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
             'u', 'v', 'w', 'x', 'y', 'z']

numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

symbols = ['$', '(', ')', '#', '%', '&']

print(art.cipher_art)

restart = True

def start(re):
    while re:

        direction = input("Type 'encrypt' to encode , 'decrypt to decode : \n")

        if direction == 'encrypt':
            inputs(direction)
        elif direction == 'decrypt':
            inputs(direction)
        else:
            print("Invalid Input")

        start_again = input("Type 'yes' if you want to go again otherwise type 'no' ")
        if start_again == 'yes':
            re = True
        else:
            re = False

def ceasar(shift, text, direction):
    encoded_or_decoded_text = ''
    for text_letter in text:

        if text_letter == ' ':
            encoded_or_decoded_text += ' '

        for number in numbers:
            if text_letter == number:
                encoded_or_decoded_text += number

        for symbol in symbols:
            if text_letter == symbol:
                encoded_or_decoded_text += text_letter

        for alphabet_index in range(len(alphabets) - 1):
            if text_letter == alphabets[alphabet_index]:
                if direction == 'encrypt':
                    encoded_or_decoded_text += alphabets[(alphabet_index + shift) % 26]
                else:
                    encoded_or_decoded_text += alphabets[alphabet_index - shift]

    print(f"The changed text is {encoded_or_decoded_text}")

def inputs(direct):
    text = input("Type your message to encrypt").lower()
    shift = int(input("Type the shift number"))
    ceasar(shift, text, direct)

start(restart)


import csv
import logging
import transaction

DIRNAME = "/opt/Plone-4.3/zeocluster/Extensions/"

FILENAME = "ccdet.dat"

MAXROWS = 100000000
TRANSSIZE = 50
TRANSSIZE_FOR_READING = 5000
FOLDERID = 'cbrf-folder'
logger = logging.getLogger('ccdet_mem')

html_escape_table = {
    "&": "&",
    }

def html_escape(text):
    """Produce entities within text."""
    return "".join(html_escape_table.get(c,c) for c in text)

def ccdet_mem(self, readonly):
    allrecs = {}
    readonly = (readonly == 1 or readonly == True)

    def ccdet_read(self):
        transcount = 0
        totalcount = 0
        with open('%s/%s' % (DIRNAME, FILENAME), 'rb') as csvfile:
            linereader = csv.DictReader(csvfile, dialect=csv.excel_tab)

            current_person_id = ''
            current_person_object = None
            last_person_object = None
            last_unique_key = ''

            for row in linereader:
                (
                    unique_key,
                    last_name,
                    first_name,
                    middle_initial,
                    last_4,
                    birthdate,
                    phone,
                    email,
                    employment_status,
                    class_name,
                    start_date,
                    city,
                    state,
                    trainer_name,
                    trainer_approval_number
                    ) = (
                    row['P_Unique_Key'],
                    row['Last_Name'],
                    row['First_Name'],
                    row['Middle_Initial'],
                    row['Last_4'],
                    row['P_Birthdate'],
                    row['P_Phone'],
                    row['Email'],
                    row['Employment_Status'],
                    row['Class_Name'],
                    row['Start_Date'],
                    row['C_City'],
                    row['C_State'],
                    row['P_Trainer'],
                    row['P_Trainer_Approval_#']
                )
                # strip out time from start_date
                start_date = start_date.split(' ')[0]
                if employment_status == 'S' and start_date != '':
                    current_person_id_base = '%s-%s-%s-%s' % (last_name, first_name, middle_initial, unique_key)
                    current_person_id_base = current_person_id_base.translate(None, "':\"/&")
                    current_person_id = current_person_id_base
                    if unique_key != last_unique_key:
                        # we are dealing with a new person so close out the previous person record
                        if last_unique_key != '':
                            # do not do this if we are just starting
                            #last_person_object.setText(last_person_object.getText() + '
')
                            pass
                        # check if need to create a new person object or can edit an existing person object
                        if not allrecs.has_key(current_person_id):
                            allrecs[current_person_id] = {}
                            #logger.info('created new person id %s' % current_person_id)
                            current_person_object = allrecs[current_person_id]
                            current_person_object['title'] = '%s, %s %s %s' % (last_name, first_name, middle_initial, unique_key)
                            current_person_object['certs'] = []
                        else:
                            #logger.info('skipping existing person ID %s' % current_person_id)
                            current_person_object = allrecs[current_person_id]
                    else:
                        # we are adding a cert to an existing person object
                        current_person_object = allrecs[current_person_id]

                    transcount += 1
                    totalcount += 1
                    current_person_object['certs'].append({'class_name':class_name, 'start_date':start_date, 'city':city, 'state':state, 'trainer_name':trainer_name, 'trainer_approval_number':trainer_approval_number})
                    last_person_object = current_person_object
                    last_unique_key = unique_key
                    transcount += 1
                    if transcount == TRANSSIZE_FOR_READING:
                        transaction.commit()
                        transcount = 0
                        logger.info("read extract line %s" % totalcount)
                    if totalcount == MAXROWS:
                        #logger.error('reached max number of rows to process; bailing now')
                        message = 'reached max number of rows to process (%s); bailing now' % MAXROWS
                        logger.warn(message)
                        return message

    def ccdet_write(self, readonly):
        transcount = 0
        totalcount = 0
        updatecount = 0
        newcount = 0
        identicalcount = 0
        if self.checkIdAvailable(FOLDERID):
            if readonly:
                logger.info('would create folder %s' % id)
            else:
                self.invokeFactory(type_name='Folder', id=FOLDERID)
        folder = getattr(self, FOLDERID, None)
        for current_person_id in allrecs.keys():
            current_person = allrecs[current_person_id]
            if not folder:
                # must be in readonly mode
                logger.info('would create person %s' % current_person_id)
                newcount += 1
            else:
                if folder.checkIdAvailable(current_person_id):
                    if readonly:
                        logger.info('would create person %s' % current_person_id)
                    else:
                        folder.invokeFactory(type_name='CBRFPersonSimple', id=current_person_id)
                        #logger.info('created new person id %s' % current_person_id)
                current_person_object = getattr(folder, current_person_id, None)
                if not readonly:
                    current_person_object.setTitle(current_person['title'])
                    #current_person_object.reindexObject(idxs=["Title"]) # will reindex below
                new_text = '

  Class Name 	  Date 	  City 	  Trainer 	  Trainer #
\n'
                for cert in current_person['certs']:
                    new_text += '
 %s	 %s	 %s %s	 %s	 %s
\n' % (cert['class_name'], cert['start_date'], cert['city'], cert['state'], cert['trainer_name'], cert['trainer_approval_number'])
                new_text += '
'
                if current_person_object:
                    current_text = current_person_object.getText()
                else:
                    # we are in readonly mode and this would be a new person
                    current_text = ''
                if current_text != html_escape(new_text):
                    if len(current_text) < 10: # arbitrary number
                        logger.info('setting %s' % current_person_id)
                        newcount += 1
                    else:
                        logger.info('updating %s' % current_person_id)
                        #logger.info('OLD: %s\nNEW: %s' % (current_text, html_escape(new_text)))
                        updatecount += 1
                    if not readonly:
                        current_person_object.setText(new_text)
                        current_person_object.reindexObject()
                else:
                    #logger.info('no change to %s' % current_person_id)
                    identicalcount += 1
                transcount += 1
                totalcount += 1
                if transcount == TRANSSIZE:
                    transaction.commit()
                    transcount = 0
                    logger.info("committed transaction %s" % totalcount)
        logger.info('Created %s records. Updated %s records. %s records were unchanged.' % (newcount, updatecount, identicalcount))

    ccdet_read(self)
    if readonly:
        logger.info('running in readonly mode')
    ccdet_write(self, readonly)
    return "Done."


import csv
import os
import os.path
import re
from prettytable import PrettyTable
from datetime import datetime

now = datetime.now()
current_date = now.strftime("%d.%m.%y")
current_time = now.strftime("%H:%M")
header = ['Id', 'Name', 'Date', 'Time', 'Note']
data = ['none', current_date, current_time, 'test']

table = PrettyTable()

#  InputDate()
pattern = r'\d{1,2}\.\d{2}\.\d{2}'  # Setting a pattern for input
regex = re.compile(pattern)
#

# SearchV()
selected_date = []  # Creating a list for confirmed notes(rows)
empty = 'No notes were created on this date.'  # Error messages if no notes(rows) for entered date

# Creating notes.csv if it doesn't exist
def Create():
    if os.path.isfile('notes.csv') == False:
        with open('notes.csv', 'w', encoding='UTF8', newline='') as f:
            writer = csv.writer(f)

            writer.writerow(header)

# Append notes.csv by adding new note(row)
def AddNote():
    r = csv.reader(open('notes.csv'))
    note_name = input('Note name:  ')
    note_text = input('Note text:  ')
    lines = list(r)
    note_id = len(lines)
    # Create the dictionary (=row)
    row = {'Id': note_id, 'Name': note_name, 'Date': current_date, 'Time': current_time, 'Note': note_text}

    # Open the CSV file in "append" mode
    with open('notes.csv', 'a', newline='') as f:
        # Create a dictionary writer with the dict keys as column fieldnames
        writer = csv.DictWriter(f, fieldnames=row.keys())

        # Append single row to CSV
        writer.writerow(row)

# Function for printing out existing notes
def ViewNotes():
    r = csv.reader(open('notes.csv'))
    lines = list(r)
    table = PrettyTable()
    table.field_names = lines[0]
    table.add_rows(lines[1:])
    print(table)

# Function for editing selected note by id
def EditNote():
    r = csv.reader(open('notes.csv'))
    lines = list(r)
    print(lines)
    e1 = int(input('Enter id: '))
    edited_note = str(input('Enter edited note: '))
    lines[e1][4] = edited_note
    lines[e1][2] = current_date
    lines[e1][3] = current_time
    writer = csv.writer(open('notes.csv', 'w', encoding='UTF8', newline=''))
    writer.writerows(lines)

# Function for deleting selected by id note
def DeleteNote():
    r = csv.reader(open('notes.csv'))
    lines = list(r)
    print(lines)
    e1 = int(input('Enter id: '))
    lines.pop(e1)
    writer = csv.writer(open('notes.csv', 'w', encoding='UTF8', newline=''))
    writer.writerows(lines)

def InputDate():
    date = input('Enter the date in the format xx.xx.xx or x.xx.xx: ')
    match = regex.search(date)
    if match:
        SearchV(date)
    else:
        print("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              "\nWrong format! Pls enter date in the format xx.xx.xx or x.xx.xx"
              "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
        InputDate()

def SearchV(date):
    r = csv.reader(open('notes.csv'))
    lines = list(r)
    table = PrettyTable()
    ec = 0
    for item in list(lines):
        if date in item:
            selected_date.append(item)
            ec += 1
    if ec != 0:
        table.add_rows(selected_date)
        print(table)

    else:
        print(empty)

def OneNote():
    r = csv.reader(open('notes.csv'))
    lines = list(r)
    table = PrettyTable()
    on = int(input('Enter note ID to view it: '))
    print(lines)
    table.field_names = lines[0]
    table.add_row(lines[on])
    print(table)


"""empty message

Revision ID: 499b26071da4
Revises: 1ff181210213
Create Date: 2023-09-12 22:24:50.922919

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '499b26071da4'
down_revision = '1ff181210213'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('threads',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('subject', sa.String(), nullable=True),
    sa.Column('author_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['author_id'], ['users.id'], name=op.f('fk_threads_author_id_users')),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_threads'))
    )
    with op.batch_alter_table('friendships', schema=None) as batch_op:
        batch_op.add_column(sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True))
        batch_op.add_column(sa.Column('updated_at', sa.DateTime(), nullable=True))

    with op.batch_alter_table('messages', schema=None) as batch_op:
        batch_op.add_column(sa.Column('thread_id', sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column('user_id', sa.Integer(), nullable=True))
        batch_op.create_foreign_key(batch_op.f('fk_messages_user_id_users'), 'users', ['user_id'], ['id'])
        batch_op.create_foreign_key(batch_op.f('fk_messages_thread_id_threads'), 'threads', ['thread_id'], ['id'])
        batch_op.drop_column('updated_at')
        batch_op.drop_column('created_at')

    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('messages', schema=None) as batch_op:
        batch_op.add_column(sa.Column('created_at', sa.DATETIME(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True))
        batch_op.add_column(sa.Column('updated_at', sa.DATETIME(), nullable=True))
        batch_op.drop_constraint(batch_op.f('fk_messages_thread_id_threads'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_messages_user_id_users'), type_='foreignkey')
        batch_op.drop_column('user_id')
        batch_op.drop_column('thread_id')

    with op.batch_alter_table('friendships', schema=None) as batch_op:
        batch_op.drop_column('updated_at')
        batch_op.drop_column('created_at')

    op.drop_table('threads')
    # ### end Alembic commands ###


# CODE BLOCK: 1
import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
from tqdm import tqdm
import numpy as np

# Code Block: 8

# Data to plot accuracy and loss graphs
train_losses = []
test_losses = []
train_acc = []
test_acc = []

test_incorrect_pred = {'images': [], 'ground_truths': [], 'predicted_vals': []}

# Code Block: 9

def GetCorrectPredCount(pPrediction, pLabels):
  return pPrediction.argmax(dim=1).eq(pLabels).sum().item()

def train(model, device, train_loader, optimizer):
  model.train()
  pbar = tqdm(train_loader)

  train_loss = 0
  correct = 0
  processed = 0

  for batch_idx, (data, target) in enumerate(pbar):
    data, target = data.to(device), target.to(device)
    optimizer.zero_grad()

    # Predict
    pred = model(data)

    # Calculate loss
    loss = F.nll_loss(pred, target)
    train_loss+=loss.item()

    # Backpropagation
    loss.backward()
    optimizer.step()

    correct += GetCorrectPredCount(pred, target)
    processed += len(data)

    pbar.set_description(desc= f'Train: Loss={loss.item():0.4f} Batch_id={batch_idx} Accuracy={100*correct/processed:0.2f}')

  train_acc.append(100*correct/processed)
  train_losses.append(train_loss/len(train_loader))

def test(model, device, test_loader):
    model.eval()

    test_loss = 0
    correct = 0

    with torch.no_grad():
        for batch_idx, (data, target) in enumerate(test_loader):
            data, target = data.to(device), target.to(device)

            output = model(data)
            test_loss += F.nll_loss(output, target, reduction='sum').item()  # sum up batch loss

            correct += GetCorrectPredCount(output, target)

    test_loss /= len(test_loader.dataset)
    test_acc.append(100. * correct / len(test_loader.dataset))
    test_losses.append(test_loss)

    print('Test set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))

# Code Block 11:

def plot_curves():
  fig, axs = plt.subplots(2,2,figsize=(15,10))
  axs[0, 0].plot(train_losses)
  axs[0, 0].set_title("Training Loss")
  axs[1, 0].plot(train_acc)
  axs[1, 0].set_title("Training Accuracy")
  axs[0, 1].plot(test_losses)
  axs[0, 1].set_title("Test Loss")
  axs[1, 1].plot(test_acc)
  axs[1, 1].set_title("Test Accuracy")
  plt.show()

def plot_incorrect_prediction(mismatch, n=10 ):
    classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
    display_images = mismatch[:n]
    index = 0
    fig = plt.figure(figsize=(10,5))
    for img in display_images:
        image = img[0].squeeze().to('cpu').numpy()
        pred = classes[img[1]]
        actual = classes[img[2]]
        ax = fig.add_subplot(2, 5, index+1)
        ax.axis('off')
        ax.set_title(f'\n Predicted Label : {pred} \n Actual Label : {actual}',fontsize=10)
        ax.imshow(np.transpose(image, (1, 2, 0)))
        index = index + 1
    plt.show()

def get_all_predictions(model, loader, device):
    """Get All predictions for model

    Args:
        model (Net): Trained Model
        loader (Dataloader): instance of dataloader
        device (str): Which device to use cuda/cpu

    Returns:
        tuple: all predicted values and their targets
    """
    model.eval()
    all_preds = torch.tensor([]).to(device)
    all_targets = torch.tensor([]).to(device)
    with torch.no_grad():
        for data, target in loader:
            data, targets = data.to(device), target.to(device)
            all_targets = torch.cat(
                (all_targets, targets),
                dim=0
            )
            output = model(data)
            preds = output.argmax(dim=1)
            all_preds = torch.cat(
                (all_preds, preds),
                dim=0
            )

    return all_preds, all_targets

def get_incorrect_predictions(model, loader, device):
    """Get all incorrect predictions

    Args:
        model (Net): Trained model
        loader (DataLoader): instance of data loader
        device (str): Which device to use cuda/cpu

    Returns:
        list: list of all incorrect predictions and their corresponding details
    """
    model.eval()
    incorrect = []
    with torch.no_grad():
        for data, target in loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            loss = F.nll_loss(output, target)
            pred = output.argmax(dim=1)
            for d, t, p, o in zip(data, target, pred, output):
                if p.eq(t.view_as(p)).item() == False:
                    incorrect.append(
                        [d.cpu(), t.cpu(), p.cpu(), o[p.item()].cpu()])

    return incorrect

def plot_incorrect_predictions(predictions, class_map, count=10):
    """Plot Incorrect predictions

    Args:
        predictions (list): List of all incorrect predictions
        class_map (list): Lable mapping
        count (int, optional): Number of samples to print, multiple of 5. Defaults to 10.
    """
    print(f'Total Incorrect Predictions {len(predictions)}')

    if not count % 5 == 0:
        print("Count should be multiple of 10")
        return

    #classes = list(class_map.values())
    classes = class_map
    fig = plt.figure(figsize=(10, 5))
    for i, (d, t, p, o) in enumerate(predictions):
        ax = fig.add_subplot(int(count/5), 5, i + 1, xticks=[], yticks=[])
        ax.set_title(f'{classes[t.item()]}/{classes[p.item()]}')
        plt.imshow(d.cpu().numpy().transpose(1, 2, 0))
        if i+1 == 5*(count/5):
            break

def prepare_confusion_matrix(all_preds, all_targets, class_map):
    """Prepare Confusion matrix

    Args:
        all_preds (list): List of all predictions
        all_targets (list): List of all actule labels
        class_map (list): Class names

    Returns:
        tensor: confusion matrix for size number of classes * number of classes
    """
    stacked = torch.stack((
        all_targets, all_preds
    ),
        dim=1
    ).type(torch.int64)

    no_classes = len(class_map)

    # Create temp confusion matrix
    confusion_matrix = torch.zeros(no_classes, no_classes, dtype=torch.int64)

    # Fill up confusion matrix with actual values
    for p in stacked:
        tl, pl = p.tolist()
        confusion_matrix[tl, pl] = confusion_matrix[tl, pl] + 1

    return confusion_matrix