import copy
import collections as col
import math
import os
import random
import time

import gym
from gym import spaces
import numpy as np

from .snakeoil3 import Client as snakeoil3

HOST = os.environ.get('TORCS_HOST', 'localhost')
PORT = int(os.environ.get('TORCS_PORT', '3101'))
FILEDIR = os.path.dirname(os.path.realpath(__file__))

class Torcs:
    # Speed limit is applied after this step
    terminal_judge_start = 50

    # [km/h], episode terminates if car is running slower than this limit
    termination_limit_progress = 1

    def __init__(self):
        self.initial_reset = True
        self.initial_run = True
        self.client = None

        # Action Space
        low = [-1., -1.]  # steering, throttle/brake
        high = [1., 1.]  # steering, throttle/brake
        self.action_space = spaces.Box(np.array(low), np.array(high))

        # Observation Space
        low = ([0.] +  # angle
               [0.] * 19 +  # track sensors,
               [-np.inf] +  # trackPos
               [-np.inf, -np.inf, -np.inf] +  # speedX, speedY, speedZ
               [-np.inf] * 4 +  # wheelSpinVel
               [-np.inf])  # rpm
        high = ([1.] +  # angle
                [1.] * 19 +  # track sensors
                [np.inf] +  # trackPos
                [np.inf, np.inf, np.inf] +  # speedX, speedY, speedZ
                [np.inf] * 4 +  # wheelSpinVel
                [np.inf])  # rpm
        self.observation_space = spaces.Box(np.array(low), np.array(high))

    def step(self, action):
        client = self.client

        # Apply Action
        action_torcs = client.R.d
        action_torcs['steer'] = np.clip(action[0], -1, 1)
        throttle = np.clip(np.abs(action[1]), 0, 1)
        if action[1] > 0:
            action_torcs['accel'] = throttle
            action_torcs['brake'] = 0
        else:
            action_torcs['accel'] = 0
            action_torcs['brake'] = throttle

        # Automatic gear shifting
        action_torcs['gear'] = 1
        if client.S.d['speedX'] > 50:
            action_torcs['gear'] = 2
        if client.S.d['speedX'] > 80:
            action_torcs['gear'] = 3
        if client.S.d['speedX'] > 110:
            action_torcs['gear'] = 4
        if client.S.d['speedX'] > 140:
            action_torcs['gear'] = 5
        if client.S.d['speedX'] > 170:
            action_torcs['gear'] = 6

        # Save the previous full-obs from torcs for the reward calculation
        obs_pre = copy.deepcopy(client.S.d)

        try:
            client.respond_to_server()  # Apply the Agent's action into torcs
            client.get_servers_input()  # Get the response of TORCS
        except ConnectionError:
            self.initial_reset = True
            raise EnvironmentError('Torcs server went away.')

        # Get the current full-observation from torcs
        obs = client.S.d

        # Make an observation from a raw observation vector from TORCS
        self.observation = self.make_observation(obs)

        # Compute reward.
        # TODO: Make plugable
        speed = np.array(obs['speedX'])
        progress = speed * np.cos(obs['angle'])  # forward progress
        reward = speed * (np.cos(obs['angle']) -  # encourage forward
                          np.abs(np.sin(obs['angle'])) -  # discourage sideways
                          np.abs(obs['trackPos']))  # discourage off-center

        # Collision detection.
        if obs['damage'] - obs_pre['damage'] > 0:
            reward = -1
            print('terminated due to damage taken')
            episode_terminate = True
            client.R.d['meta'] = True

        # Termination judgement
        episode_terminate = False

        # Episode is terminated if the car is out of track
        if np.min(obs['track']) < 0:
            print('terminated due to out of track')
            reward = -1
            episode_terminate = True
            client.R.d['meta'] = True

        print('progress {:.2f}, reward {:.2f}'.format(progress, reward))

        # Episode terminates if the progress of agent is small
        if self.terminal_judge_start < self.time_step and progress < 1:
            reward = -1
            episode_terminate = True
            client.R.d['meta'] = True
        # if self.terminal_judge_start < self.time_step and speed < 10:
        #     print('terminated due to speed')
        #     episode_terminate = True
        #     client.R.d['meta'] = True

        # Episode is terminated if the agent runs backward
        # if np.cos(obs['angle']) < 0:
        #     print('terminated due to angle')
        #     episode_terminate = True
        #     client.R.d['meta'] = True

        # Send a reset signal
        if client.R.d['meta'] is True:
            self.initial_run = False
            try:
                client.respond_to_server()
            except ConnectionError:
                self.initial_reset = True
                raise EnvironmentError('Torcs server went away.')

        # if episode_terminate:
        #     reward = -1

        self.time_step += 1
        return self.observation, reward, client.R.d['meta'], {}

    def reset(self):
        self.time_step = 0

        if self.initial_reset is not True:
            self.client.R.d['meta'] = True
            try:
                self.client.respond_to_server()
            except ConnectionError:
                self.initial_reset = True
                return self.reset()

        # Modify here if you use multiple tracks in the environment
        # Open new UDP in vtorcs
        self.client = snakeoil3(H=HOST, p=PORT, vision=False)
        self.client.MAX_STEPS = np.inf

        try:
            self.client.get_servers_input()  # Get the initial input from torcs
        except ConnectionError:
            self.initial_reset = True
            return self.reset()
        obs = self.client.S.d  # Get the current full-observation from torcs
        self.observation = self.make_observation(obs)
        self.last_u = None
        self.initial_reset = False
        return self.observation

    def make_observation(self, obs):
        """
        angle, track sensors, trackPos, speedX, speedY, speedZ,
        wheelSpinVel, rpm
        """
        sensors = [('angle', 3.1416),
                   ('track', 200),
                   ('trackPos', 1),
                   ('speedX', 300),
                   ('speedY', 300),
                   ('speedZ', 300),
                   ('wheelSpinVel', 1),
                   ('rpm', 10000)]
        data = [np.array(obs[sensor]) / div for sensor, div in sensors]
        return np.hstack(data)

    def render(self, close=False):
        """Not implemented. Always renders."""
        ...

    def close(self):
        """Shutdown connection to torcs server."""
        self.client.shutdown()


import logging
import argparse
import numpy as np

from bokeh.plotting import figure, show
from bokeh.layouts import column

logger = logging.getLogger(__name__)

def fill_ascii():
    ascii_map = dict()

    start_index = int('C0', 16) - 1
    for symb in range(ord('А'), ord('я') + 1):
        ascii_map[chr(symb)] = start_index = start_index + 1

    ascii_map['.'] = int('2C', 16)
    ascii_map[' '] = int('20', 16)

    return ascii_map

def text2bin(text):
    ascii_map = fill_ascii()

    bin_code = ''
    for symb in text:
        cur_symb_bin = bin(ascii_map[symb])[2:].zfill(8)
        bin_code += cur_symb_bin
        logger.info(f'Symbol {symb} -> {cur_symb_bin}')
    return bin_code

def nrz(bin_code):
    x = np.array( list(range(len(bin_code))) )
    y = np.array( list(map(int, bin_code)) )

    return (x, y)

def rz(bin_code):
    x = range(len(bin_code))
    y = map(int, bin_code)

    x = np.array( [ [i, i+0.5] for i in x] ).flatten()
    y = np.array( [ [-1, 0] if i == 0 else [1, 0] for i in y]).flatten()

    return (x, y)

def manchester(bin_code):
    x = range(len(bin_code))
    y = map(int, bin_code)

    x = np.array( [ [i, i+0.5] for i in x] ).flatten()
    y = np.array( [ [1, 0] if i == 0 else [0, 1] for i in y]).flatten()

    return (x, y)

def ami(bin_code):
    x, y = nrz(bin_code)

    is_even = False
    for i in range(len(y)):
        if (y[i] == 1) and is_even:
            y[i] = -1
            is_even = False
        elif y[i] == 1:
            is_even = True

    return (x, y)

def nrzi(bin_code):
    x = range(len(bin_code))
    y = list(map(int, bin_code))

    for i in range(1, len(y)):
        if y[i] == 1:
            y[i] = abs(y[i-1] - 1)
        else:
            y[i] = y[i-1]

    return (x, y)

def plot_discrete(encodings):
    for ((x, y), title) in encodings:
        delta = x[1] - x[0]

        x = np.array( [ [i, i+delta] for i in x] ).flatten()
        y = np.array( [ [i, i] for i in y] ).flatten()

        p = figure(width=1300, height=300, title=title)
        p.xgrid.minor_grid_line_color = 'navy'
        p.xgrid.minor_grid_line_alpha = 0.1
        p.line(x, y)

        yield p

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Pretty useful (no) program')
    parser.add_argument('--log', type=str, default='ERROR')

    args = parser.parse_args()

    loglevel = getattr(logging, args.log)
    logging.basicConfig(level=loglevel)

    bin_code = text2bin('Волков Ю.В.')
    print(bin_code)

    nrz_plot = nrz(bin_code[:4 * 8])
    rz_plot = rz(bin_code[:4 * 8])
    man_plot = manchester(bin_code[:4 * 8])
    ami_plot = ami(bin_code[:4 * 8])
    nrzi_plot = nrzi(bin_code[:4 * 8])

    plots = list(plot_discrete([(rz_plot, 'RZ'),
                                (nrz_plot, 'NRZ'),
                                (man_plot, 'Manchester'),
                                (ami_plot, 'AMI'),
                                (nrzi_plot, 'NRZI')]))

    show(column(*plots))


import cv2
import numpy as np

imagem = cv2.imread('../images/logo-if.jpg')

def ajuste_brilho(img, br):
    brilho = [br, br, br]
    res = np.zeros(img.shape, np.uint8)
    for y in range(0, img.shape[0]):
        for x in range(0, img.shape[1]):
            res[y, x] = np.minimum(brilho + img[y, x], [255, 255, 255])
    return res

def ajuste_contraste(img, cont):
    contraste = [cont, cont, cont]
    res = np.zeros(img.shape, np.uint8)
    for i in range(0, img.shape[0]):
        for j in range(0, img.shape[1]):
                res[i,j] = np.minimum(contraste * img[i,j], [255, 255, 255])
    return res

def ajuste_negativo(img, negative):
    negativo = [negative, negative, negative]
    res = np.zeros(img.shape, np.uint8)
    for i in range(0, img.shape[0]):
        for j in range(0, img.shape[1]):
                res[i,j] = np.minimum(negativo - img[i,j], [255, 255, 255])
    return res

cv2.namedWindow('MOD')
brilho = 0
contraste = 1
negativo = 255
result = imagem

while(True):
    cv2.imshow('MOD', result)
    key = cv2.waitKey(20)
    if key == 27:
        break
    elif key == ord('a'):
        brilho = min(brilho + 50, 255)
        result = ajuste_brilho(imagem, brilho)
    elif key == ord('z'):
        brilho = max(brilho - 50, 0)
        result = ajuste_brilho(imagem, brilho)
    elif key == ord('s'):
        contraste = min(contraste + 0.20, 255)
        if(contraste > 1):
            result = ajuste_contraste(imagem, contraste)
    elif key == ord('x'):
        contraste = max(contraste - 0.20, 0)
        if(contraste > 0 and contraste < 1):
            result = ajuste_contraste(imagem, contraste)
    elif key == ord('n'):
        negativo = max(negativo, 0)
        result = ajuste_negativo(imagem, negativo)

cv2.destroyAllWindows()


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

plt.xlabel('X')
plt.ylabel('Y')
data = {'a': np.arange(50),
        'c': np.random.randint(0, 50, 50),
        'd': np.random.randn(50)}
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100

plt.scatter('a', 'b', c='c', s='d', data=data)
plt.plot('a', 'b', data=data)

plt.show()


import itertools

import chainer
import numpy as np

class OperableMovingMNISTDataset(chainer.dataset.DatasetMixin):

    """Operable Moving MNIST Dataset

    Args:
        n_frames (int): The number of frames which the returned array has in channel dimension.
        rows (int): The height of the returned video.
        cols (int): The width of the returned video.
        n_digits (int or tuple): The number of digits the returned array contains.
            If tuple is specified, it randomly outputs multiple digits within a range of t[0] and t[1].
        label (bool): If True, it also outputs an array with length 10 where
            each element denotes the number of the digit.

    Returns:
        ndarray: The shape is (N, n_frames, rows, cols)

    """

    def __init__(self, n_frames=16, rows=64, cols=64, n_digits=2, label=False, train=True):
        self.n_frames = n_frames
        self.rows = rows
        self.cols = cols
        train, test = chainer.datasets.mnist.get_mnist()
        if train:
            self.dset = train
        else:
            self.dset = test
        self.n = len(self.dset)
        self.n_digits = n_digits
        self.label = label
        if label:
            digits = set(tuple(sorted(t)) for t in itertools.product(range(10), repeat=n_digits))
            self._digits_to_label = {x: i for i, x in enumerate(sorted(digits))}

    def __len__(self):
        return self.n

    @property
    def n_channels(self):
        return 1

    @property
    def n_classes(self):
        return len(self._digits_to_label)

    def make_trajectory(self, length, rows=36, cols=36):

        def make_position(rows, cols):
            return np.array([rows, cols]) * np.random.rand(2)

        def make_velocity():
            theta = (2 * np.pi) * np.random.rand()
            s_min, s_max = 1.0, 5.0
            s = s_min + (s_max - s_min) * np.random.rand()
            return np.array([s * np.cos(theta), s * np.sin(theta)])

        def proceed(p0, v0, size):
            p1 = p0 + v0
            v1 = v0
            if p1 < 0:
                p1 = -p1
                v1 = -v0
            elif size < p1:
                p1 = size - (p1 - size)
                v1 = -v0
            return p1, v1

        pos, vel = make_position(rows, cols), make_velocity()

        positions = np.zeros((length, 2), dtype=np.float64)
        velocities = np.zeros((length, 2), dtype=np.float64)
        positions[0] = pos
        velocities[0] = vel
        for t in range(1, length):
            p0, v0 = positions[t - 1], velocities[t - 1]
            p1y, v1y = proceed(p0[0], v0[0], rows)
            p1x, v1x = proceed(p0[1], v0[1], cols)
            positions[t, :] = [p1y, p1x]
            velocities[t, :] = [v1y, v1x]
        return positions.astype(np.int32), velocities.astype(np.int32)

    def draw(self, mov, pic):
        p, _ = self.make_trajectory(
            self.n_frames,
            rows=(self.rows - 28),
            cols=(self.cols - 28))
        for t in range(self.n_frames):
            y, x = p[t]
            mov[0, t, y:(y + 28), x:(x + 28)] += pic.reshape(28, 28)

    def make_label(self, labels):
        return self._digits_to_label[tuple(sorted(labels))]

    def get_example(self, i):
        mov = np.zeros((1, self.n_frames, self.rows, self.cols), dtype=np.float32)
        labels = []
        if isinstance(self.n_digits, int):
            n_digits = self.n_digits
        else:
            n_digits = np.random.randint(self.n_digits[0], self.n_digits[1])

        for s in range(n_digits):
            if s == 0:
                d, t = self.dset[i]
            else:
                j = np.random.randint(self.n)
                d, t = self.dset[j]
            self.draw(mov, d)
            labels.append(t)
        mov[mov > 1] = 1
        mov = 2 * mov - 1
        if self.label:
            return mov, self.make_label(labels)
        else:
            return mov


import os
from src.config import CONFIGURATION
import subprocess
import wget
import zipfile

def get_fastqc(fastqc_dir):
    '''If FastQC is not already downloaded, function downloads FastQC version 0.11.7 zip file, unzips file, and
    makes fastqc executable.
    '''
    file_dir = os.path.dirname(__file__)
    if not os.path.isdir(os.path.join(file_dir, os.path.basename('FastQC'))):
        fastqc_url = 'https://www.bioinformatics.babraham.ac.uk/projects/fastqc/fastqc_v0.11.7.zip'
        wget.download(fastqc_url, out=file_dir)
        with zipfile.ZipFile(os.path.join(file_dir, os.path.basename('fastqc_v0.11.7.zip')), "r") as zip_obj:
            zip_obj.extractall(file_dir)
        os.remove(os.path.join(file_dir, os.path.basename('fastqc_v0.11.7.zip')))
        os.chmod(fastqc_dir, 0o755)

def exec_fastqc(fastq_dir, fastqc_dir, fastqc_output_dir):
    '''Runs FastQC to analyze RNA read quality
    '''
    file_dir = os.path.dirname(__file__)
    if not os.path.isdir(fastqc_output_dir):
        os.mkdir(fastqc_output_dir)
    os.chmod('{}/gen_fastqc.sh'.format(file_dir), 0o755)
    subprocess.call(['{}/gen_fastqc.sh'.format(file_dir), fastq_dir, fastqc_dir, fastqc_output_dir])

def run():
    '''
    Runs the fastqc script.
    '''
    fastqc_dir = CONFIGURATION['fastqc_dir']
    fastqc_output_dir = CONFIGURATION['fastqc_output_dir']
    fastq_dir = CONFIGURATION['fastq_dir']
    get_fastqc(fastqc_dir)
    exec_fastqc(fastq_dir, fastqc_dir, fastqc_output_dir)

if __name__ == '__main__':
    run()


from datetime import datetime
from openpyxl import load_workbook
workbook = load_workbook(filename="Attendance.xlsx")
#print(workbook.sheetnames)

sheet = workbook["Class-1A"]
#print(sheet.title)
#print(sheet["A1"].value)

# Returns last row with data
#print(sheet.max_row)

lastRow = sheet.max_row
#print(sheet.cell(lastRow,1).value)

#gets today date
print(datetime.today().strftime('%d-%m-%Y'))

# Gets all data of row
# names = [i.value for i in sheet[lastRow] ]
# print(names)

names = [i.value for i in sheet[1] ]

sheet.cell(lastRow+1,1).value = datetime.today().strftime('%d-%m-%Y')
indexVal = names.index("Bill Gates")
sheet.cell(lastRow+1,indexVal+1).value = True
workbook.save(filename="Attendance.xlsx")


# Paul Arzberger
# 00311430
# Informatics 2 - SS19
# Gruppe A

import numpy
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import cartopy.crs as ccrs

def animate(i, data, text, coast, grid):

    """
    function that creates all the singe frames of the animation
    :param i:
    :param data: ndim numpy array - with gravity data for 12 months
    :param text: text that should be printed in a little box on a certain position and holds time stamp information
    :param coast: matplotlib object that visualizes the coastline of the earth
    :param grid: matpolotlib meshgrid object - for each time stamp an own pcolormesh is created and returned
    :return: text, coast, grid - have to be returned in the exact same way they have handed to the function
    """
    print("create frame for month ", i)
    print("max: ", numpy.nanmax(gravity_data[i]), " min: ", numpy.nanmin(gravity_data[i]))
    grid.set_array(data[i].flatten())
    text.set_text("Month: 2008-{}".format(i+1))
    return text, coast, grid

def animate_via_function(input_data, input_lon, input_lat):
    """
    creates the animation for the gravity field
    :param input_data: ndim numpy array with gravity data for 12 months
    :param input_lon: list - with longitude information as float ( same size as input_lat)
    :param input_lat: list - with latitude information as fload ( same size as input_lon)
    :return:
    """
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection=ccrs.Robinson())
    ax.set_global()
    ax.set_title("Seasonal gravity ignal from GRACE")

    grid = ax.pcolormesh(input_lon, input_lat, input_data[0], cmap='RdBu', vmin=numpy.nanmin(input_data),
                         vmax=numpy.nanmax(input_data), transform=ccrs.PlateCarree())
    coast = ax.coastlines()

    # print a field with the date information
    text = ax.text(-180,-25, "", transform=ccrs.PlateCarree(), bbox=dict(facecolor='white', pad=4))
    cbar = fig.colorbar(grid, orientation='horizontal', extend='both', extendfrac=0.025, aspect=50, pad=0.025,
                        shrink=0.75)

    cbar.set_label("Equivalent water height [cm]")

    # create an FuncAnimation object
    anim = animation.FuncAnimation(fig,  # figure object
                                   animate,  # callable animation function
                                   gravity_data.shape[0],  # number of frames
                                   fargs=(gravity_data, text, coast, grid),  # parameters passed to function
                                   interval=500,  # delay between frames [ms]
                                   blit=True)  # blitting (only redraw changes)

    # save created animation to disc
    anim.save("animation_gravity.mp4")

if __name__ == "__main__":

    # 1.) Sat Orbit
    # =======================

    # read out satdata and skip header information
    sat_data = numpy.loadtxt(r"orbit.txt", skiprows=2)
    blue_mrbl_jpg = plt.imread(r"bluemarble01.jpg")

    # calc the norm of the vector for each epoch
    r = numpy.linalg.norm(sat_data[:, 1:], axis=1)
    phi = numpy.arcsin(sat_data[:, 3] / r) * 180/numpy.pi
    lam = numpy.arctan2(sat_data[:, 2], sat_data[:, 1]) * (180 / numpy.pi)

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection=ccrs.Robinson())
    ax.imshow(blue_mrbl_jpg, origin='upper', transform=ccrs.PlateCarree())
    ax.plot(lam, phi, color='red', transform=ccrs.Geodetic())       # transform plot
    ax.set_title("Grace orbit on 2008-01-01")

    plt.show()

    # 2.) Animation
    # =======================

    gravity_data = numpy.load(r"gravityField.npy")
    lon_gravity_field_plot = numpy.arange(-180, 181, 1)
    lat_gravity_field_plot = numpy.arange(90, -91, -1)

    animate_via_function(gravity_data, lon_gravity_field_plot, lat_gravity_field_plot)

    print("Programm ENDE")


from django.urls import path, include
from rest_framework.urlpatterns import format_suffix_patterns
from crim.views.observation import ObservationList, ObservationDetail, api_root, ObservationListJSON, ObservationDetailJSON
from crim.views.relationship import RelationshipList, RelationshipDetail, RelationshipListJSON, RelationshipDetailJSON
from crim.views.definition import DefinitionList, DefinitionDetail, DefinitionListJSON, DefinitionDetailJSON
from crim.views.relationship_form import get_relationship
from crim.views.observation_form import get_observation
urlpatterns = [
    path('observations/', ObservationList.as_view(), name='crimobservation-list'),
    path('observations/<int:pk>/', ObservationDetail.as_view(), name='crimobservation-detail'),
    path('data/observations/', ObservationListJSON.as_view(), name='crimobservation-list-json'),
    path('data/observations/<int:pk>/', ObservationDetailJSON.as_view(), name='crimobservation-detail-json'),

    path('relationships/', RelationshipList.as_view(), name='crimrelationship-list'),
    path('relationships/<int:pk>/', RelationshipDetail.as_view(), name='crimrelationship-detail'),
    path('data/relationships/', RelationshipListJSON.as_view(), name='crimrelationship-list-json'),
    path('data/relationships/<int:pk>/', RelationshipDetailJSON.as_view(), name='crimrelationship-detail-json'),

    path('definitions/', DefinitionList.as_view(), name='crimdefinition-list'),
    path('definitions/<int:pk>/', DefinitionDetail.as_view(), name='crimdefinition-detail'),
    path('data/definitions/', DefinitionListJSON.as_view(), name='crimdefinition-list-json'),
    path('data/definitions/<int:pk>/', DefinitionDetailJSON.as_view(), name='crimdefinition-detail-json'),

    path('forms/relationship/', get_relationship, name='relationship-form'),
    path('forms/observation/', get_observation, name='observation-form'),
    path('', api_root, name='home-view'),

    path("select2/", include("django_select2.urls")),
]

urlpatterns = format_suffix_patterns(urlpatterns)


import numpy as np
from numba import jit
import random

@jit
def categorical_sample(p):
    threshold = np.random.rand()
    current = 0
    for i in range(p.shape[0]):
        current += p[i]
        if current > threshold:
            return i
@jit
#def threshold_exponential(mean):
#    return 1 + np.round(np.random.exponential(mean-1))
def threshold_exponential(mean):
    return np.round(np.random.exponential(mean))

@jit
def threshold_log_normal(mean, sigma):
    x = np.random.lognormal(mean, sigma)
    if x <= 0:
        return 1
    else:
        return np.round(x)


#!/usr/bin/python3

from flask import Flask, render_template
import markdown, codecs
from bs4 import BeautifulSoup

app = Flask(__name__)

@app.route("/")
def show_cv():
    # Load file
    input_file = codecs.open("cv_text.md", mode="r", encoding="utf-8")
    text = input_file.read()

    # text markdown to html
    md = markdown.Markdown(extensions = ['extra'])
    html = md.convert(text)

    # Extract points (divs) of the html text
    soup = BeautifulSoup(html, 'html.parser')
    per_brd = soup.find(id="personal_brand")
    per_dta = soup.find(id="personal_data")
    pre = soup.find(id="presentation")
    cmp = soup.find(id="competences")
    exp = soup.find(id="expirience")
    formt = soup.find(id="formation")
    others = soup.find(id="others")

    return render_template('index.html', personal_brand = per_brd, personal_data = per_dta, presentation = pre, competences = cmp, expirience = exp, formation = formt, others = others)


#!/usr/bin/env python

import sys
sys.path.append('./')
sys.path.append('../../')

from lab_utils import (
    tf, os, np, plt, logger, ap, BooleanAction,
    debug, toc, auto_increment
)

ap.add_argument('--epochs', type=int, default=10, help='number of epochs: 10*')
ap.add_argument('--batch', type=int, default=32, help='batch size: 32*')
args, extra_args = ap.parse_known_args()
logger.info(args)
# logger.info(extra_args)

if args.all:
    args.step = 0 # forced to 0

if args.debug:
    import pdb
    import rlcompleter
    pdb.Pdb.complete=rlcompleter.Completer(locals()).complete
    # import code
    # code.interact(local=locals())
    debug = breakpoint

# if args.steps:
#     args.steps = [int(n) for n in args.steps.split(",")]
#     logger.info(f'Step #{args.steps} will be run')
# else:
#     args.steps = []

import time
import pandas as pd
import itertools
import pathlib
import re

from tensorflow.keras import Sequential, Model, Input
from tensorflow.keras.layers import Flatten, Dense, Dropout, Concatenate
from tensorflow.keras.layers.experimental import preprocessing

### TOC
if args.step == 0:
    toc(__file__)

args.step = auto_increment(args.step, args.all)
### Step #1 - Load the dataset
if args.step >= 1:
    print("\n### Step #1 - Load the dataset")

    titanic = pd.read_csv("https://storage.googleapis.com/tf-datasets/titanic/train.csv")
    logger.debug(f'\n{titanic.head()}')

    titanic_features = titanic.copy()
    titanic_labels = titanic_features.pop('survived')

### Step #2 - Construct multi-input layers
if args.step >= 2:
    print("\n### Step #2 - Construct input layers")

    print("\n### Step #2 - Construct input layers")
    inputs = {}
    for name, column in titanic_features.items():
        dtype = column.dtype
        if dtype == object:
            dtype = tf.string
        else:
            dtype = tf.float32
        inputs[name] = Input(shape=(1,), name=name, dtype=dtype)

    if args.verbose: print(*inputs.items(), sep='\n')

### Step #3 - Collect numeic columns
if args.step >= 3:
    print("\n### Step #3 - Collect numeic columns")

    numeric_inputs = dict(filter(lambda ele: ele[1].dtype == tf.float32, inputs.items()))
    # = numeric_inputs = {
    #     name: input for (name, input) in inputs.items() if input.dtype==tf.float32
    # }
    if args.verbose: print(*numeric_inputs.items(), sep='\n')

### Step #4 - Prepare normalization
if args.step >= 4:
    print("\n### Step #4 - Prepare normalization")

    norm = preprocessing.Normalization()

    titanic_numeric_features = titanic_features[numeric_inputs.keys()]
    # = titanic_numeric_features = titanic_features[['age', 'n_siblings_spouses', 'fare', 'parch']]

    norm.adapt(titanic_numeric_features)
    # = norm.adapt(np.array(titanic_numeric_features))
    # = norm.adapt(titanic_numeric_features.values)

    sample = titanic_numeric_features[0:2]
    normalized = norm(sample)
    logger.debug(f'norm(sample):\n{normalized}')

### Step #5 - Build the model: for numeric inputs
if args.step >= 5:
    print("\n### Step #5 - Build the model: for numeric inputs")

    x = Concatenate()(numeric_inputs.values())
    # = inputs.values() == [input_age, input_siblings, input_fare, input_parch]
    # ? Concatenate()(list(numeric_inputs.values()))
    x_numeric = norm(x)

    x = Dense(64)(x_numeric)
    output = Dense(1)(x)

    model_numeric = Model(inputs=numeric_inputs, outputs=[output])

    model_numeric.compile(
        loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
        optimizer=tf.keras.optimizers.Adam()
    )
    if args.verbose:
        model_numeric.summary()

### Step #6 - Data preparation: for numeric inputs
if args.step in [6, 7]:
    print("\n### Step #6 - Data preparation: for numeric inputs")

    logger.info('1. primitively')
    data_primitive = {
        'age': titanic_features['age'].values,
        'n_siblings_spouses': titanic_features['n_siblings_spouses'].values,
        'fare': titanic_features['fare'].values,
        'parch': titanic_features['parch'].values,
    }

    logger.info('2. using dict()')
    for name, column in titanic_features.items():
        dtype = column.dtype
        if dtype == object:
            dtype = tf.string
            _ = titanic_features.pop(name)
        else:
            dtype = tf.float32
    # = titanic_features.pop('deck')
    # = titanic_features.pop('embark_town')
    # = titanic_features.pop('alone')
    # = titanic_features.pop('class')
    # = titanic_features.pop('sex')
    data_dict = dict(titanic_features)

    logger.info('3. dataset form')
    data_ds = tf.data.Dataset.from_tensor_slices(
        (dict(titanic_features), titanic_labels)
    )
    data_ds = data_ds.shuffle(len(titanic_labels)).batch(32)

### Step #7 - Train the numeric-only model
if args.step == 7:
    print("\n### Step #7 - Train the numeric-only model")

    logger.info('with data_primitive')
    model_numeric.fit(
        data_primitive,
        titanic_labels,
        # = titanic_lables.values,
        epochs=args.epochs,
        verbose=2 if args.verbose else 0
    )
    print()

    logger.info('with data_dict')
    model_numeric.fit(
        x=data_dict,
        y=titanic_labels,
        epochs=args.epochs,
        verbose=2 if args.verbose else 0
    )
    print()

    logger.info('with data_ds')
    model_numeric.fit(
        data_ds,
        epochs=args.epochs,
        verbose=2 if args.verbose else 0
    )

### Step #8 - Preprocessing layer: including categorical inputs
if args.step >= 8:
    print("\n### Step #8 - Preprocessing layer: including categorical inputs")

    preprocessed_inputs = [x_numeric]
    # for categorical feature
    for name, input in inputs.items():
        if input.dtype == tf.float32:
            continue

        # string to number
        lookup = preprocessing.StringLookup(vocabulary=np.unique(titanic[name]))
        # number to onehot
        one_hot = preprocessing.CategoryEncoding(num_tokens=lookup.vocabulary_size())

        x_categorical = lookup(input)
        x_categorical = one_hot(x_categorical)
        preprocessed_inputs.append(x_categorical)

    preprocessed_output = Concatenate()(preprocessed_inputs)

    preprocessing_model = Model(inputs, preprocessed_output)

    if args.verbose:
        preprocessing_model.summary()

### Step #9 - Build the model
if args.step >= 9:
    print("\n### Step #9 - Build the model")

    def titanic_model(preprocessing_head, inputs):
        body = Sequential([
            Dense(64),
            Dense(1)
        ])

        preprocessed_inputs = preprocessing_head(inputs)
        result = body(preprocessed_inputs)
        model = Model(inputs, result)

        model.compile(
            loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
            optimizer=tf.keras.optimizers.Adam()
        )
        return model

    # = x = preprocessing_model(inputs)
    # x = Dense(64)(x)
    # output = Dense(1)(x)
    #
    # model = Model(inputs, output)
    #
    # model.compile(
    #     loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
    #     optimizer=tf.keras.optimizers.Adam()
    # )

    model = titanic_model(preprocessing_model, inputs)
    if args.verbose:
        model.summary()

### Step #10 - Data preparation: for all inputs
if args.step in [10, 11]:
    print("\n### Step #10 - Data preparation: for all inputs")

    titanic_features = titanic.copy()
    titanic_labels = titanic_features.pop('survived')

    logger.info('1. using dict()')
    data_dict = dict(titanic_features)
    # = data_dict = {
    #     name: np.array(value) for name, value in titanic_features.items()
    # }

    logger.info('2. dataset form')
    data_ds = tf.data.Dataset.from_tensor_slices(
        (dict(titanic_features), titanic_labels)
    )
    data_ds = data_ds.shuffle(len(titanic_labels)).batch(32)

### Step #11 - Train the model: for all inputs
if args.step == 11:
    print("\n### Step #11 - Train the model: for all inputs")

    model = titanic_model(preprocessing_model, inputs)

    logger.info('with data_dict')
    model.fit(
        x=data_dict,
        y=titanic_labels,
        epochs=args.epochs,
        verbose=2 if args.verbose else 0
    )
    print()

    logger.info('with data_ds')
    model.fit(
        data_ds,
        epochs=args.epochs,
        verbose=2 if args.verbose else 0
    )

### Step #12 - Dataset from disk
if args.step >= 12:
    print("\n### Step #12 - Dataset from disk")

    titanic_file_path = f'{os.getenv("HOME")}/.keras/datasets/train.csv'

    titanic_csv_ds = tf.data.experimental.make_csv_dataset(
        titanic_file_path,
        batch_size=32,
        label_name='survived',
        num_epochs=1,
        ignore_errors=True,
    )

    if args.verbose:
        for batch, label in titanic_csv_ds.unbatch().batch(5).take(1):
            for key, value in batch.items():
                print(f"{key:20s}: {value}")
                print(f"{'label':20s}: {label}")
            print()

### Step #13 - Train the model
if args.step >= 13:
    print("\n### Step #13 - Train the model")

    model = titanic_model(preprocessing_model, inputs)

    model.fit(
        titanic_csv_ds,
        epochs=args.epochs,
        verbose=2 if args.verbose else 0
    )

### End of File
print()
if args.plot:
    plt.show()
debug()


"""
setup.py is a module used to build and distribute Python packages.
It typically contains information about the package, such as its name, version, and dependencies,
as well as instructions for building and installing the package.
"""

import setuptools

# reading README file so that it will help to write the description automatically if we publish this project as a module /package
with open("README.md","r",encoding='utf-8') as f:
    long_description = f.read()

__version__ = "0.0.0"
REPO_NAME = "Text-Summarization"
AUTHOR_USER_NAME = "Avinav09"
SRC_REPO = "Text_Summarizer"
AUTHOR_EMAIL = "avinav.sharma500@gmail.com"

setuptools.setup(
    name=SRC_REPO,
    version=__version__,
    author=AUTHOR_USER_NAME,
    author_email=AUTHOR_EMAIL,
    description="Text Summarization end to end project implementation.",
    long_description=long_description,
    long_description_content = "text/markdown",
    url=f"https://github.com/{AUTHOR_USER_NAME}/{REPO_NAME}",
    project_urls={
        "Bug Tracker":f"https://github.com/{AUTHOR_USER_NAME}/{REPO_NAME}/issues"
    },
    package_dir={"":"src"},
    packages=setuptools.find_packages(where="src")

)


import asyncio
import inspect
import json
import logging
import types
import typing as T

import aiohttp
import numpy as np
import websockets

import pupil_labs  # noqa: F401

from .base import DeviceBase
from .models import (
    APIPath,
    Component,
    Event,
    Status,
    UnknownComponentError,
    parse_component,
)

logger = logging.getLogger(__name__)

UpdateCallbackSync = T.Callable[["pupil_labs.realtime_api.models.Component"], None]
"""Type annotation for synchronous update callbacks"""

UpdateCallbackAsync = T.Callable[
    ["pupil_labs.realtime_api.models.Component"], T.Awaitable[None]
]
"""Type annotation for asynchronous update callbacks"""

UpdateCallback = T.Union[UpdateCallbackSync, UpdateCallbackAsync]
"""Type annotation for synchronous and asynchronous callbacks"""

class DeviceError(Exception):
    pass

class Device(DeviceBase):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self._create_client_session()

    async def get_status(self) -> Status:
        """
        :raises pupil_labs.realtime_api.device.DeviceError: if the request fails
        """
        async with self.session.get(self.api_url(APIPath.STATUS)) as response:
            confirmation = await response.json()
            if response.status != 200:
                raise DeviceError(response.status, confirmation["message"])
            result = confirmation["result"]
            logger.debug(f"[{self}.get_status] Received status: {result}")
            return Status.from_dict(result)

    async def status_updates(self) -> T.AsyncIterator[Component]:
        # Auto-reconnect, see
        # https://websockets.readthedocs.io/en/stable/reference/client.html#websockets.client.connect
        websocket_status_endpoint = self.api_url(APIPath.STATUS, protocol="ws")
        async for websocket in websockets.connect(websocket_status_endpoint):
            try:
                async for message_raw in websocket:
                    message_json = json.loads(message_raw)
                    try:
                        component = parse_component(message_json)
                    except UnknownComponentError:
                        logger.warning(f"Dropping unknown component: {component}")
                        continue
                    yield component
            except websockets.ConnectionClosed:
                logger.debug("Websocket connection closed. Reconnecting...")
                continue
            except asyncio.CancelledError:
                logger.debug("status_updates() cancelled")
                break

    async def recording_start(self) -> str:
        """
        :raises pupil_labs.realtime_api.device.DeviceError:
            if the recording could not be started. Possible reasons include
            - Recording already running
            - Template has required fields
            - Low battery
            - Low storage
            - No wearer selected
            - No workspace selected
            - Setup bottom sheets not completed
        """
        async with self.session.post(self.api_url(APIPath.RECORDING_START)) as response:
            confirmation = await response.json()
            logger.debug(f"[{self}.start_recording] Received response: {confirmation}")
            if response.status != 200:
                raise DeviceError(response.status, confirmation["message"])
            return confirmation["result"]["id"]

    async def recording_stop_and_save(self):
        """
        :raises pupil_labs.realtime_api.device.DeviceError:
            if the recording could not be started
            Possible reasons include
            - Recording not running
            - template has required fields
        """
        async with self.session.post(
            self.api_url(APIPath.RECORDING_STOP_AND_SAVE)
        ) as response:
            confirmation = await response.json()
            logger.debug(f"[{self}.stop_recording] Received response: {confirmation}")
            if response.status != 200:
                raise DeviceError(response.status, confirmation["message"])

    async def recording_cancel(self):
        """
        :raises pupil_labs.realtime_api.device.DeviceError:
            if the recording could not be started
            Possible reasons include
            - Recording not running
        """
        async with self.session.post(
            self.api_url(APIPath.RECORDING_CANCEL)
        ) as response:
            confirmation = await response.json()
            logger.debug(f"[{self}.stop_recording] Received response: {confirmation}")
            if response.status != 200:
                raise DeviceError(response.status, confirmation["message"])

    async def send_event(
        self, event_name: str, event_timestamp_unix_ns: T.Optional[int] = None
    ) -> Event:
        """
        :raises pupil_labs.realtime_api.device.DeviceError: if sending the event fails
        """
        event: T.Dict[str, T.Any] = {"name": event_name}
        if event_timestamp_unix_ns is not None:
            event["timestamp"] = event_timestamp_unix_ns

        async with self.session.post(
            self.api_url(APIPath.EVENT), json=event
        ) as response:
            confirmation = await response.json()
            logger.debug(f"[{self}.send_event] Received response: {confirmation}")
            if response.status != 200:
                raise DeviceError(response.status, confirmation["message"])
            return Event.from_dict(confirmation["result"])

    async def close(self):
        await self.session.close()
        self.session = None

    async def __aenter__(self) -> "Device":
        if self.session is None:
            self._create_client_session()
        return self

    async def __aexit__(
        self,
        exc_type: T.Optional[T.Type[BaseException]],
        exc_val: T.Optional[BaseException],
        exc_tb: T.Optional[types.TracebackType],
    ) -> None:
        await self.close()

    def _create_client_session(self):
        self.session = aiohttp.ClientSession()

    async def get_calibration(self) -> np.ndarray:
        """
        :raises pupil_labs.realtime_api.device.DeviceError: if the request fails
        """
        async with self.session.get(self.api_url(APIPath.CALIBRATION)) as response:
            if response.status != 200:
                raise DeviceError(response.status, "Failed to fetch calibration")

            raw_data = await response.read()
            return np.frombuffer(
                raw_data,
                np.dtype(
                    [
                        ("version", "u1"),
                        ("serial", "6a"),
                        ("scene_camera_matrix", "(3,3)d"),
                        ("scene_distortion_coefficients", "8d"),
                        ("scene_extrinsics_affine_matrix", "(4,4)d"),
                        ("right_camera_matrix", "(3,3)d"),
                        ("right_distortion_coefficients", "8d"),
                        ("right_extrinsics_affine_matrix", "(4,4)d"),
                        ("left_camera_matrix", "(3,3)d"),
                        ("left_distortion_coefficients", "8d"),
                        ("left_extrinsics_affine_matrix", "(4,4)d"),
                        ("crc", "u4"),
                    ]
                ),
            )

class StatusUpdateNotifier:
    def __init__(self, device: Device, callbacks: T.List[UpdateCallback]) -> None:
        self._auto_update_task: T.Optional[asyncio.Task] = None
        self._device = device
        self._callbacks = callbacks

    async def receive_updates_start(self) -> None:
        if self._auto_update_task is not None:
            logger.debug("Auto-update already started!")
            return
        self._auto_update_task = asyncio.create_task(self._auto_update())

    async def receive_updates_stop(self):
        if self._auto_update_task is None:
            logger.debug("Auto-update is not running!")
            return
        self._auto_update_task.cancel()
        try:
            # wait for the task to be cancelled
            await self._auto_update_task
        except asyncio.CancelledError:
            pass  # task has been successfully cancelled
        self._auto_update_task = None

    async def __aenter__(self):
        await self.receive_updates_start()

    async def __aexit__(
        self,
        exc_type: T.Optional[T.Type[BaseException]],
        exc_val: T.Optional[BaseException],
        exc_tb: T.Optional[types.TracebackType],
    ):
        await self.receive_updates_stop()

    async def _auto_update(self) -> None:
        async for changed in self._device.status_updates():
            for callback in self._callbacks:
                result = callback(changed)
                if inspect.isawaitable(result):
                    await result


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import json
import progressbar
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

SIGTAP_TABLES = (
    ('tb_procedimento', 'co_procedimento'),
    ('tb_grupo', 'co_grupo'),
    ('tb_sub_grupo', 'co_sub_grupo'),
    ('tb_forma_organizacao', 'co_forma_organizacao'),
    ('tb_cid', 'co_cid'),
    ('tb_descricao', 'co_procedimento'),
    ('tb_descricao_detalhe', 'co_detalhe'),
    ('tb_detalhe', 'co_detalhe'),
    ('tb_financiamento', 'co_financiamento'),
    ('tb_grupo_habilitacao', 'nu_grupo_habilitacao'),
    ('tb_habilitacao', 'co_habilitacao'),
    ('tb_modalidade', 'co_modalidade'),
    ('tb_ocupacao', 'co_ocupacao'),
    ('tb_registro', 'co_registro'),
    ('tb_regra_condicionada', 'co_regra_condicionada'),
    ('tb_renases', 'co_renases'),
    ('tb_rubrica', 'co_rubrica'),
    ('tb_servico', 'co_servico'),
    ('tb_servico_classificacao', 'co_servico'),
    ('tb_sia_sih', 'co_procedimento_sia_sih'),
    ('tb_tipo_leito', 'co_tipo_leito'),
    ('tb_componente_rede', 'co_componente_rede'),
    ('tb_rede_atencao', 'co_rede_atencao'),
    ('tb_tuss', 'co_tuss')
)

if __name__ == '__main__':
    current_dir = os.getcwd()
    json_dir = os.path.join(current_dir, 'Sigtap', 'json')

    cred = credentials.Certificate('comments-devjc-ad243d4d5314.json')
    firebase_admin.initialize_app(cred)
    db = firestore.client()

    total_regs = 0

    for table in SIGTAP_TABLES:
        # print(table[0], ' - ', table[1])
        filename = os.path.join(json_dir, '{}.json'.format(table[0]))

        try:
            f = open(filename, 'r', encoding='utf-8')
        except IOError as ex:
            print('Não foi possivel abrir o arquivo {}. Erro: {}'.format(filename, ex))
            sys.exit(-1)

        r = json.loads(f.read())
        table_json = r[table[0]]
        qtd = len(table_json)

        total_regs = total_regs + qtd
        print(table[0], ': ', qtd)

        # pbar_widgets = [
        #     progressbar.FormatLabel('Reg: %(value)d / '),
        #     progressbar.Percentage(),
        #     progressbar.Bar()
        # ]
        # pbar = progressbar.ProgressBar(widgets=pbar_widgets, maxval=qtd)
        # pbar.start()

        batch = db.batch()
        i = 0

        for num, reg in enumerate(table_json):
            key = reg[table[1]]
            # doc_ref = db.collection(table[0]).document(key)
            # batch.set(doc_ref, reg)
            i += 1
            # pbar.update(num)
            if i == 500:
                i = 0
                # batch.commit()

        # batch.commit()
        # pbar.finish()

    print('TOTAL: ', total_regs)


from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_POST
from mainsite.models import Product, coupon
from orders.models import order
from .cart import Cart
from .forms import CartAddProductForm
from django.http import HttpResponse

# Create your views here.

@require_POST
def cart_add(request, product_id):
	if request.session.get('total_cost') is not None:
		del request.session['total_cost']
	cart = Cart(request)
	product = get_object_or_404(Product, id=product_id)
	form = CartAddProductForm(request.POST)
	if form.is_valid():
		cd = form.cleaned_data
		cart.add(product=product, quantity=cd['quantity'], update_quantity=cd['update'])
	return redirect('cart:cart_detail')

def cart_remove(request, product_id):
	if request.session.get('total_cost') is not None:
		del request.session['total_cost']
	cart = Cart(request)
	product = get_object_or_404(Product, id=product_id)
	cart.remove(product)
	return redirect('cart:cart_detail')

def cart_detail(request):
	r = request.user
	cart = Cart(request)
	price_after_discount = None

	for item in cart:
		item['update_quantity_form'] = CartAddProductForm(initial={'quantity': item['quantity'], 'update': True})

	context = {
		'cart': cart,
		'r': r,
		'price_after_discount': price_after_discount
	}

	return render(request, 'cart-page.html', context)


import tensorflow as tf
import numpy as np
import os
from scipy.signal import convolve as conv1d
from sklearn.svm import OneClassSVM
from sklearn.ensemble import IsolationForest
from sklearn.metrics import roc_auc_score
from typing import Union

from protocols.packet_protocols import KitsuneProtocol

def get_base_data():
    dataset_folder = r"E:\Users\Degva\Documents\_PhD\Tensorflow\datasets\kitsune\Active Wiretap"
    packets = np.load(os.path.join(dataset_folder, "dataset.npy"))
    train_packets = packets[:1000000]
    test_packets = packets[1000000:]

    labels = np.load(os.path.join(dataset_folder, "labels.npy"))
    labels = labels[1000000:]

    return train_packets, test_packets, labels

@tf.function
def interpolation_error(latent_codes: tf.Tensor) -> tf.Tensor:
    step_count = latent_codes.shape[1]
    first_latent_code = latent_codes[:, 0]
    last_latent_code = latent_codes[:, -1]

    target_latent_codes = temporal_interpolation(first_latent_code, last_latent_code, step_count)

    latent_codes = latent_codes[:, 1:-1]
    target_latent_codes = target_latent_codes[:, 1:-1]

    error = tf.square(latent_codes - target_latent_codes)
    # error = tf.abs(latent_codes - target_latent_codes)

    return error

@tf.function
def temporal_interpolation(first_latent_code: tf.Tensor,
                           last_latent_code: tf.Tensor,
                           step_count: Union[tf.Tensor, int]
                           ) -> tf.Tensor:
    first_latent_code = tile_latent_code(first_latent_code, step_count, stop_gradient=True)
    last_latent_code = tile_latent_code(last_latent_code, step_count, stop_gradient=True)

    weights_shape = [1, step_count] + [1] * (len(first_latent_code.shape) - 2)
    weights = tf.range(start=0, limit=step_count, dtype=tf.int32)
    weights = tf.cast(weights / (step_count - 1), tf.float32)
    weights = tf.reshape(weights, weights_shape)

    latent_codes = first_latent_code * (1.0 - weights) + last_latent_code * weights
    return latent_codes

@tf.function
def tile_latent_code(latent_code: tf.Tensor,
                     step_count: int,
                     stop_gradient: bool):
    if stop_gradient:
        latent_code = tf.stop_gradient(latent_code)

    tile_multiples = [1, step_count] + [1] * (len(latent_code.shape) - 1)
    latent_code = tf.expand_dims(latent_code, axis=1)
    latent_code = tf.tile(latent_code, tile_multiples)

    return latent_code

def compute_labels(input_length):
    dataset_folder = r"E:\Users\Degva\Documents\_PhD\Tensorflow\datasets\kitsune\Active Wiretap"
    labels = np.load(os.path.join(dataset_folder, "labels.npy"))
    labels = labels[1000000:]

    kernel = np.ones([input_length], dtype=np.float32)
    labels = (conv1d(labels, kernel, mode="valid") >= 1.0).astype(np.float32)

    np.save(r"C:\Users\Degva\Desktop\tmp\work\LTM\labels.npy", labels)
    return labels

def compute_base_predictions():
    protocol = KitsuneProtocol(base_log_dir="../logs/AEA", epoch=128, kitsune_dataset="Active Wiretap")
    protocol.load_weights(128)
    model = protocol.model

    batch_size = 1024

    pattern = protocol.get_network_packet_pattern()
    train_dataset = protocol.dataset_loader.train_subset.make_base_tf_dataset(pattern, 1, None, False, batch_size)
    test_dataset = protocol.dataset_loader.test_subset.make_base_tf_dataset(pattern, 1, None, False, batch_size)

    train_predictions = model.predict(train_dataset, verbose=1)
    test_predictions = model.predict(test_dataset, verbose=1)

    np.save(r"C:\Users\Degva\Desktop\tmp\work\LTM\train_predictions.npy", train_predictions)
    np.save(r"C:\Users\Degva\Desktop\tmp\work\LTM\test_predictions.npy", test_predictions)

    return train_predictions, test_predictions

def compute_initial():
    train_predictions, test_predictions = compute_base_predictions()
    labels = compute_labels(input_length=64)
    return train_predictions, test_predictions, labels

def reload():
    train_predictions = np.load(r"C:\Users\Degva\Desktop\tmp\work\LTM\train_predictions.npy")
    test_predictions = np.load(r"C:\Users\Degva\Desktop\tmp\work\LTM\test_predictions.npy")
    labels = np.load(r"C:\Users\Degva\Desktop\tmp\work\LTM\labels.npy")
    return train_predictions, test_predictions, labels

def random_choice_nd(array: np.ndarray, size: int) -> np.ndarray:
    indices = np.arange(array.shape[0])
    indices = np.random.choice(indices, size)
    return array[indices]

def normalize_sklearn_predictions(predictions):
    predictions = - predictions
    predictions = (predictions - predictions.min()) / (predictions.max() - predictions.min())
    return predictions

def run_isolation_forest(train_predictions: np.ndarray, test_predictions: np.ndarray) -> np.ndarray:
    isolation_forest = IsolationForest(max_samples=0.8, n_jobs=-1)
    print("IF - Fitting...")
    isolation_forest.fit(train_predictions)

    print("IF - Predicting...")
    predictions = isolation_forest.score_samples(test_predictions)
    predictions = normalize_sklearn_predictions(predictions)
    return predictions

def run_svm(train_predictions: np.ndarray, test_predictions: np.ndarray) -> np.ndarray:
    svm = OneClassSVM(kernel="rbf", nu=0.03, cache_size=8000)
    train_predictions = random_choice_nd(train_predictions, size=10000)

    print("SVM - Fitting...")
    svm.fit(train_predictions)

    print("SVM - Predicting...")
    predictions = svm.score_samples(test_predictions)
    predictions = normalize_sklearn_predictions(predictions)
    return predictions

def main():
    # train_packets, test_packets, base_labels = get_base_data()
    # svm_predictions = run_svm(train_packets, test_packets)
    # svm_result = roc_auc_score(base_labels.astype("bool"), svm_predictions)
    # print("[Base Data] SVM Result :", round(svm_result, 3))

    # forest_predictions = run_isolation_forest(train_packets, test_packets)
    # forest_result = roc_auc_score(base_labels.astype("bool"), forest_predictions)
    # print("[Base Data] Isolation Forest Result :", round(forest_result, 3))

    train_predictions, test_predictions, labels = compute_initial()
    # train_predictions, test_predictions, labels = reload()
    # train_predictions = interpolation_error(tf.convert_to_tensor(train_predictions)).numpy()
    # test_predictions = interpolation_error(tf.convert_to_tensor(test_predictions)).numpy()
    train_predictions = np.reshape(train_predictions, [train_predictions.shape[0], -1])
    test_predictions = np.reshape(test_predictions, [test_predictions.shape[0], -1])

    # svm_predictions = run_svm(train_predictions, test_predictions)
    # np.save(r"C:\Users\Degva\Desktop\tmp\work\LTM\svm_predictions.npy", svm_predictions)
    # svm_result = roc_auc_score(labels.astype("bool"), svm_predictions)
    # print("[LTM Data] SVM Result :", round(svm_result, 3))

    forest_predictions = run_isolation_forest(train_predictions, test_predictions)
    np.save(r"C:\Users\Degva\Desktop\tmp\work\LTM\forest_predictions.npy", forest_predictions)
    forest_result = roc_auc_score(labels.astype("bool"), forest_predictions)
    print("[LTM Data] Isolation Forest Result :", round(forest_result, 3))

if __name__ == "__main__":
    main()


from django.contrib.auth.models import User
from django.db import models

class Project(models.Model):
    name = models.CharField(max_length=100, unique=True)
    prefix = models.CharField(max_length=4, null=True, blank=True)
    is_active = models.BooleanField(default=True)
    description = models.TextField(null=True, blank=True)
    scope = models.CharField(
        max_length=30,
        choices=(
            ("Sandbox", "Sandbox"),
            ("R&D", "R&D"),
            ("Internal", "Internal"),
            ("Public Product", "Public Product"),
            ("Private Product", "Private Product"),
        ),
        default="R&D",
    )
    members = models.ManyToManyField(User, blank=True, related_name="project_members")
    manager = models.ForeignKey(
        User,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="project_manager",
    )

    class Meta:
        ordering = ("name",)

    def __str__(self):
        try:
            title = f"{self.id } / {self.name} / {self.prefix}"
        except AttributeError:
            title = f"{self.id } / {self.name}"
        return title


import torch
from torch.nn import functional as F
import math

class Ops:
    @staticmethod
    def resample(input, size, align_corners=True):
        n, c, h, w = input.shape
        dh, dw = size

        input = input.view([n * c, 1, h, w])

        if dh < h:
            kernel_h = Ops.lanczos(Ops.ramp(dh / h, 2), 2).to(input.device, input.dtype)
            pad_h = (kernel_h.shape[0] - 1) // 2
            input = F.pad(input, (0, 0, pad_h, pad_h), 'reflect')
            input = F.conv2d(input, kernel_h[None, None, :, None])

        if dw < w:
            kernel_w = Ops.lanczos(Ops.ramp(dw / w, 2), 2).to(input.device, input.dtype)
            pad_w = (kernel_w.shape[0] - 1) // 2
            input = F.pad(input, (pad_w, pad_w, 0, 0), 'reflect')
            input = F.conv2d(input, kernel_w[None, None, None, :])

        input = input.view([n, c, h, w])
        return F.interpolate(input, size, mode='bicubic', align_corners=align_corners)

    @staticmethod
    def lanczos(x, a):
        cond = torch.logical_and(-a < x, x < a)
        out = torch.where(cond, Ops.sinc(x) * Ops.sinc(x/a), x.new_zeros([]))
        return out / out.sum()

    @staticmethod
    def sinc(x):
        return torch.where(x != 0, torch.sin(math.pi * x) / (math.pi * x), x.new_ones([]))

    @staticmethod
    def ramp(ratio, width):
        n = math.ceil(width / ratio + 1)
        out = torch.empty([n])
        cur = 0
        for i in range(out.shape[0]):
            out[i] = cur
            cur += ratio
        return torch.cat([-out[1:].flip([0]), out])[1:-1]

    @staticmethod
    def vector_quantize(x, codebook):
        d = x.pow(2).sum(dim=-1, keepdim=True) + codebook.pow(2).sum(dim=1) - 2 * x @ codebook.T
        indices = d.argmin(-1)
        x_q = F.one_hot(indices, codebook.shape[0]).to(d.dtype) @ codebook
        return ReplaceGrad.apply(x_q, x)

class ClampWithGrad(torch.autograd.Function):
    @staticmethod
    def forward(ctx, input, min, max):
        ctx.min = min
        ctx.max = max
        ctx.save_for_backward(input)
        return input.clamp(min, max)

    @staticmethod
    def backward(ctx, grad_in):
        input, = ctx.saved_tensors
        return grad_in * (grad_in * (input - input.clamp(ctx.min, ctx.max)) >= 0), None, None

class ReplaceGrad(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x_forward, x_backward):
        ctx.shape = x_backward.shape
        return x_forward

    @staticmethod
    def backward(ctx, grad_in):
        return None, grad_in.sum_to_size(ctx.shape)


import sys
#sys.path.insert(0, '/usr/lib/python2.7/dist-packages')
import cv2
import imutils
import numpy
import time
from Queue import Queue
import urllib
import threading
from threading import Thread, Event, ThreadError

#print sys.path

searchMode = 0
forward = 1
forward_left = 2
forward_right = 3
left = 4
right = 5
bananaDance = 6

def printData(data):
    return data*data

def openCV(data):

    cap = cv2.VideoCapture('http://192.168.1.130:8080/videoView')
    #cap = cv2.VideoCapture('http://192.168.8.148:8080/videoView')
    ret, frame = cap.read()
    frame = imutils.rotate(frame, -90)
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower_range = numpy.array([20,100,100])
    upper_range = numpy.array([30, 255, 255])

    mask = cv2.inRange(hsv, lower_range, upper_range)

    result = cv2.bitwise_and(frame, frame, mask=mask)

    blurred = cv2.GaussianBlur(mask, (5, 5), 0)
    thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]

    area2 = 0
    for c in cnts:
        M = cv2.moments(c)
        area = cv2.contourArea(c)
        if(M["m00"] != 0 and area > 250):
            cX = int(M["m10"] / M["m00"])
            cY = int(M["m01"] / M["m00"])

            #cv2.drawContours(result, [c], -1, (0,255,0), 2)
            #cv2.circle(result, (cX, cY), 7, (255, 255, 255), -1)
            #cv2.putText(result, "Possible banana!", (cX - 20, cY - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
            if(area > area2):
                area2 = area
                c2X = cX
                c2Y = cY

    res = 0

    if(area2 > 400):
        if((c2X > 145 and c2X < 190) and (c2Y > 75 and c2Y < 110)):
            res = 6
        elif((c2X > 145 and c2X < 190) and (c2Y < 75)):
            res = 1
        elif((c2X < 145) and (c2Y < 75)):
            res = 2
        elif((c2X > 190) and c2Y < 75):
            res = 3
        elif((c2X < 145) and (c2Y > 75 and c2Y < 110)):
            res = 4
        elif((c2X > 190) and (c2Y > 75 and c2Y < 110)):
            res = 5
    else:
        res = 0

    #cv2.imshow('Result', result)

    return res


"""Demo DAG for using Bash and Python Operators"""

from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator
from airflow.models import Variable
from datetime import datetime
from airflow.utils.dates import days_ago

args = {
    'owner': 'Jake Klein',
    'email': 'JakeKlein94@gmail.com',
    'retries': 3,
    'depends_on_past': True,
}

dag = DAG(
    dag_id='demo_bash_and_python_dag',
    default_args=args,
    schedule_interval='0 0 * * *',
    start_date=days_ago(2),
    tags=['bash', 'python', 'lab8', 'demo'],
    max_active_runs=1
)

task1 = BashOperator(
    dag=dag,
    task_id='task1',
    bash_command='echo "This is a BashOperator"'
)

def task2_callable():
    print("This is a PythonOperator")

task2 = PythonOperator(
    dag=dag,
    task_id='task2',
    python_callable=task2_callable
)

task3 = BashOperator(
    dag=dag,
    task_id='task3',
    bash_command='echo "The execution date is: {{ ds }}"'
)

def task4_callable(ts):
    print(f'The execution timestamp is: {ts}')

task4 = PythonOperator(
    dag=dag,
    task_id='task4',
    provide_context=True,
    python_callable=task4_callable
)

def print_lyrics(singer):
    singers = ['meatloaf', 'hall & oates']
    if singer.lower() not in singers:
        raise ValueError(f'Expecting value in {singers} but received {singer}')
    elif singer.lower() == 'meatloaf':
        str = """
            I would do anything for love.
            But I won't do that.
            No I won't do that.
        """
    else:
        str = """
            I'll do almost anything.
            But I can't go for that.
            No can do.
        """
    print(str)

task5 = PythonOperator(
    dag=dag,
    task_id='task5',
    python_callable=print_lyrics,
    op_kwargs={'singer': 'Meatloaf'}
)

task6 = PythonOperator(
    dag=dag,
    task_id='task6',
    python_callable=print_lyrics,
    op_kwargs={'singer': 'Hall & Oates'}
)

task7 = PythonOperator(
    dag=dag,
    task_id='task7',
    python_callable=print_lyrics,
    op_kwargs={'singer': 'Simon & Garfunkel'}
)

task8 = BashOperator(
    dag=dag,
    task_id='task8',
    bash_command='echo "Our GCS Bucket is: $BUCKET"',
    env={'BUCKET': Variable.get('bucket')}
)

task1 >> task2 >> [task3, task4]
task3 >> [task5, task6, task7]
task4 >> [task5, task6, task7]


import torch
import torch.nn as nn
from torch.nn.utils import rnn

import bi_lstm_crf as blc

import numpy as np

import api.utils as utils

class TransformerBiLSTM_CRF(nn.Module):
    """
        Credit
        - pytorch advanced tutorial: https://pytorch.org/tutorials/beginner/nlp/advanced_tutorial.html
        - bi-lstm-crf: https://github.com/jidasheng/bi-lstm-crf
    """

    def __init__(self,
                 transformer_model: str,
                 tag_to_ix,
                 embedding_dim: int = 32,
                 hidden_dim: int = 256,
                 dropout_ratio: int = 0.5
                 ):

        super(TransformerBiLSTM_CRF, self).__init__()

        self.tag_to_ix = tag_to_ix
        self.tagset_size = len(tag_to_ix)

        self.hidden_dim = hidden_dim
        self.dropout_ratio = dropout_ratio

        self.embeddings, self.tokenizer = utils.transformer_from_pretrained(transformer_model)

        self.lstm = nn.LSTM(embedding_dim, hidden_dim // 2, num_layers=1, bidirectional=True)

        self.dropout = nn.Dropout(dropout_ratio)

        self.crf = blc.CRF(hidden_dim, self.tagset_size)

    def _get_lstm_features(self, sentences):

        masks = np.array([
            [tok == utils.PAD_TOK for tok in sent]
            for sent in sentences
        ])

        masks = torch.from_numpy(masks)

        sentences = [' '.join(tok for tok in sent) for sent in sentences]

        tok_sentences = self.tokenizer(sentences, return_tensors="pt", padding=True)

        embeds, _ = self.embeddings(**tok_sentences)

        seq_length = masks.sum(1)

        print("Seq length: ", seq_length)

        pack_sequence = rnn.pack_padded_sequence(embeds, lengths=seq_length, batch_first=True, enforce_sorted=False)
        packed_output, _ = self.lstm(pack_sequence)
        lstm_out, _ = rnn.pad_packed_sequence(packed_output, batch_first=True)
        lstm_out = self.dropout(lstm_out)

        return lstm_out, masks

    def loss(self, sentence, tags):

        features, masks = self._get_lstm_features(sentence)
        loss = self.crf.loss(features, tags, masks=masks)

        return loss

    def forward(self, sentence):
        lstm_feats, masks = self._get_lstm_features(sentence)
        scores, tag_seq = self.crf(lstm_feats, masks)

        return scores, tag_seq


from enum import Enum
import cv2
from PIL import Image

class TroopType(Enum):
    NORMAL = 1
    SPELL = 2
    SIEGE_MACHINE = 3

class Troop:
    def __init__(self, name, troop_type: TroopType = TroopType.NORMAL, swipe: bool = False):
        self.name = name
        # self.request = fr"request/{name}.png"
        self.donate = fr"donate/{name}.png"
        self.queue = fr"queue/{name}.png"
        # self.request_image = cv2.imread(self.request, 0)
        self.donate_image = cv2.imread(self.donate, 0)
        self.queue_image = cv2.imread(self.queue, 0)
        self.troop_type = troop_type
        self.swipe = swipe

    def __str__(self):
        return self.name

barbarian = Troop("barbarian")
giant = Troop("giant")
wall_breaker = Troop("wall_breaker")
balloon = Troop("balloon")
super_wizard = Troop("super_wizard")
dragon = Troop("dragon")
super_dragon = Troop("super_dragon")
baby_dragon = Troop("baby_dragon")
electro_dragon = Troop("electro_dragon")
minion = Troop("minion", swipe=True)
head_hunter = Troop("head_hunter", swipe=True)

archer = Troop("archer")
super_archer = Troop("super_archer")
goblin = Troop("goblin")
super_wall_breaker = Troop("super_wall_breaker")
super_giant = Troop("super_giant")
wizard = Troop("wizard")
healer = Troop("healer")
pekka = Troop("pekka")
miner = Troop("miner")
yeti = Troop("yeti")
bowler = Troop("bowler", swipe=True)
valkyrie = Troop("valkyrie", swipe=True)
witch = Troop("witch", swipe=True)
golem = Troop("golem", swipe=True)
ice_golem = Troop("ice_golem", swipe=True)
lava_hound = Troop("lava_hound", swipe=True)
hog_rider = Troop("hog_rider", swipe=True)

freeze_spell = Troop("freeze_spell", TroopType.SPELL)
rage_spell = Troop("rage_spell", TroopType.SPELL)
invisibility_spell = Troop("invisibility_spell", TroopType.SPELL)
lightning_spell = Troop("lightning_spell", TroopType.SPELL)
poision_spell = Troop("poision_spell", TroopType.SPELL)
hast_spell = Troop("hast_spell", TroopType.SPELL)
bat_spell = Troop("bat_spell", TroopType.SPELL)
heal_spell = Troop("heal_spell", TroopType.SPELL)
jump_spell = Troop("jump_spell", TroopType.SPELL)
clone_spell = Troop("clone_spell", TroopType.SPELL)
earthquake_spell = Troop("earthquake_spell", TroopType.SPELL)
skeleton_spell = Troop("skeleton_spell", TroopType.SPELL)
siege_barracks = Troop("siege_barracks", TroopType.SIEGE_MACHINE)
stone_slammer = Troop("stone_slammer", TroopType.SIEGE_MACHINE)
wall_wrecker = Troop("wall_wrecker", TroopType.SIEGE_MACHINE)
battle_blimp = Troop("battle_blimp", TroopType.SIEGE_MACHINE)
log_launcher = Troop("log_launcher", TroopType.SIEGE_MACHINE)
flame_flinger = Troop("flame_flinger", TroopType.SIEGE_MACHINE, swipe=True)
unknown = Troop("unknown")

# target_images
OPEN_CHAT_IMG = cv2.imread(r"buttons/open_chat.png", 0)
ClOSE_CHAT_IMG = cv2.imread(r"buttons/close_chat.png", 0)
DONATE_IMG = cv2.imread(r"buttons/donate_button.png", 0)
DONATE_PADDING_IMG = cv2.imread(r"buttons/donate_padding.png", 0)
DRAG_CANCEL = cv2.imread(r"buttons/drag_cancel.png", 0)
CLOSE_BUTTON = cv2.imread(r"buttons/close_button.png", 0)
ZERO_TROOP = cv2.imread(r"buttons/zero_troop.png", 0)
JUMP_UP = cv2.imread(r"buttons/unread_up.png", 0)
JUMP_DOWN = cv2.imread(r"buttons/unread_down.png", 0)

TRAIN_BUTTON = cv2.imread(r"buttons/train_button.png", 0)
TRAIN_TROOPS = cv2.imread(r"buttons/train_troops.png", 0)
BREW_SPELLS = cv2.imread(r"buttons/brew_spells.png", 0)
BUILD_SIEGE_MACHINES = cv2.imread(r"buttons/build_siege_machines.png", 0)

def is_grey_scale(img_path):
    img = Image.open(img_path).convert('RGB')
    w, h = img.size
    for i in range(w):
        for j in range(h):
            r, g, b = img.getpixel((i, j))
            # print([r,g, b])
            color_range = range(g - 20, g + 20)
            if (not (r in color_range and g in color_range and b in color_range)) and r != 255:
                # print([r, g, b])
                return False
    return True

def is_grey_scale_img(im):
    img = im.convert('RGB')

    w, h = img.size
    for i in range(w):
        for j in range(h):
            r, g, b = img.getpixel((i, j))
            # print([r,g, b])
            color_range = range(g - 50, g + 50)
            if (not (r in color_range and g in color_range and b in color_range)) and r != 255:
                # if r != g != b:
                return False
    return True

def foreach(items, function):
    for i in items:
        function(i)

# print(is_grey_scale(r"donate/rage_spell.png"))
# print(is_grey_scale(r"donate/balloon.png"))
# print(is_grey_scale(r"slammer_train.png"))
# print(is_grey_scale(r"temp.png"))


import datetime

from django.db import models, transaction

class PollManager(models.Manager):
    def create_with_questions(self, data, creator):
        with transaction.atomic():
            questions = data.pop("questions")
            instance = self.create(**data, creator=creator)
            instance.questions.create_questions(questions, creator)
        return instance

    def get_with_question(self):
        return self.prefetch_related("question").order_by("-id")

    def get_active_polls(self):
        current_time = datetime.datetime.now()
        return self.filter(
            start_at__lte=current_time, end_at__gte=current_time
        ).order_by("-id")

class QuestionManager(models.Manager):
    def create_questions(self, questions, creator):
        instances = []
        for question in questions:
            question[self.field.name] = self.instance
            question["creator"] = creator
            instances.append(self.model(**question))
        self.bulk_create(instances)

class VoteManager(models.Manager):
    def create_with_answers(self, data, user):
        if not user.is_authenticated:
            user = None
        with transaction.atomic():
            questions = data.pop("answers")
            instance = self.create(**data, user=user)
            instance.answers.create_answers(questions)
        return instance

    def get_with_answers(self):
        return self.select_related("poll").order_by("-id")

class AnswerManager(models.Manager):
    def create_answers(self, answers):
        instances = []
        for answer in answers:
            answer[self.field.name] = self.instance
            instances.append(self.model(**answer))
        self.bulk_create(instances)


import json

path = 'contacts.json'

with open(path, 'r') as file:
    text = file.read()

def load_contacts(contacts):
    with open('contacts.json', 'r') as file:
        text = file.read()
    contacts = json.loads(text)
    contacts = contacts['contacts']
    return contacts

def save_contacts(path, contacts):
    contacts = {'contacts': contacts} # put the list of contacts in to a dictionary
    with open(path, 'w') as file: # open the file
        text = json.dumps(contacts, indent=4, sort_keys=True) # convert our list of dictionaries into json
        file.write(text) # write the json to the file

contacts = load_contacts(path)
print(contacts)

# print(contacts[0]['name'])

while True:
    command = input('\nWelcome to the Contact Dominator 9001!\n \nWhat operation would you like to perform? ')
    if command in ['done', 'finished', 'quit', 'exit']:
        break
    elif command == 'create':
        name = input('What is the contact\'s name? ')
        age = input('What is the contact\'s age? ')
        email = input('What is the contact\'s email? ')
        color = input('What is the contact\'s favorite color? ')
        contacts.append({'name':name, 'age':age, 'email':email, 'favorite color':color})
        print(f'Contact {name} has been added!')
        print(contacts)
    elif command == 'retrieve':
        name = input('Who do you want to look up? ')
        print(name)
    elif command == 'list':
        name = input('What is the contact\'s name')
        found_contact = False
        for contact in contacts:
            if name.lower() in contact['name'].lower():
                print_contact(contact)
                found_contact = True
    elif command == 'update':
        name = input('what is the contact\'s name which you want to update? ')
        field = input('what field would you like to update? (name, age, email, favorite color)? ')
        value = input('what is the new value? ')
        for contact in contacts:
            if contact['name'] == name:
                contact[field] == 'name'
    else:
        print('command not recognized')
        break

save_contacts(path, contacts)


import numpy as np
import sortednp
import pyeda.inter
import scipy

# sympy import takes forever
# but for some cases, to_dnf method performs better than the one of pyeda
# this flag therefore enables sympy use instead of pyeda
USE_SYMPY = False

class Model:

    def __init__(self, bnet_filename):
        self.model = self.readBNet(bnet_filename)

    def readBNet(self, filename):
        model = {}
        with open(filename, 'r') as f:
            lines = f.readlines()
            del lines[0]
            for line in lines:
                species, formula = [value.strip() for value in line.split(",")]
                model.update({species: formula})

        return model

class TransitionTable:

    def __init__(self, model: Model):
        self.model = model.model

    def to_dnf(self, formula_str):

        if not USE_SYMPY:
            formula = pyeda.inter.expr(
                formula_str.replace('!', '~')).simplify()
            formula = formula.to_dnf()
            _, _, clauses_tmp = formula.encode_dnf()

            clauses = []
            for clause in clauses_tmp:
                clause_map = {}
                for arg in clause:
                    clause_map.update(
                        {str(formula.inputs[abs(arg) - 1]): True if arg > 0 else False})

                clauses.append(clause_map)

            return clauses
        else:
            import sympy

            formula = sympy.parsing.sympy_parser.parse_expr(
                formula_str.replace('!', '~')).simplify()

            dnf = sympy.logic.boolalg.to_dnf(formula)

            if not isinstance(dnf, sympy.logic.boolalg.Or):
                clauses_tmp = dnf,
            else:
                clauses_tmp = dnf.args

            clauses = []
            for clause in clauses_tmp:
                if not isinstance(clause, sympy.logic.boolalg.And):
                    args = clause,
                else:
                    args = clause.args

                clause_map = {}
                for arg in args:
                    clause_map.update(
                        {str(arg.free_symbols.pop()): not isinstance(arg, sympy.logic.boolalg.Not)})

                clauses.append(clause_map)

            return clauses

    def get_node_transitions(self, node_idx, nodes, node_values):
        def get_transitions(formula, up_down):
            node_transitions = [np.empty((0), dtype=int)]

            for clause in formula:
                # no change
                if nodes[node_idx] in clause.keys() and clause[nodes[node_idx]] == up_down:
                    continue

                contributing_nodes_idxs = []
                contributing_nodes_vals = []
                for arg in clause.keys():
                    contributing_nodes_idxs.append(nodes.index(arg))
                    contributing_nodes_vals.append(clause[arg])

                if nodes[node_idx] not in clause.keys():
                    contributing_nodes_idxs.append(node_idx)
                    contributing_nodes_vals.append(not up_down)

                static_val = np.sum(
                    node_values[contributing_nodes_idxs] * contributing_nodes_vals)

                not_contributing_count = len(
                    nodes) - len(contributing_nodes_idxs)
                clause_transitions = np.empty(
                    (2 ** not_contributing_count), dtype=int)
                clause_transitions[0] = static_val

                states_len = 1
                for not_contributing_idx in range(len(nodes)):
                    if not_contributing_idx in contributing_nodes_idxs:
                        continue

                    clause_transitions[states_len: 2 *
                                       states_len] = clause_transitions[: states_len] + 2 ** not_contributing_idx

                    states_len *= 2

                node_transitions.append(clause_transitions)

            node_transitions = sortednp.kway_merge(
                *node_transitions, duplicates=sortednp.DROP)

            return node_transitions

        formula = self.model[nodes[node_idx]]

        dnf_clauses = self.to_dnf(formula)
        up_transitions_src = get_transitions(dnf_clauses, True)

        dnf_clauses = self.to_dnf(f"!({formula})")
        down_transitions_src = get_transitions(dnf_clauses, False)

        up_trans_src_len = up_transitions_src.shape[0]
        transitions_src = np.concatenate(
            [up_transitions_src, down_transitions_src])

        transitions_dst = np.empty((transitions_src.shape[0]))
        transitions_dst[:up_trans_src_len] = transitions_src[:
                                                             up_trans_src_len] + 2 ** node_idx
        transitions_dst[up_trans_src_len:] = transitions_src[up_trans_src_len:] - 2 ** node_idx

        return transitions_src, transitions_dst

    def build_transition_table(self):
        nodes = list(self.model.keys())
        n = len(nodes)
        states_count = 2 ** n

        node_values = 2 ** np.arange(n, dtype=int)

        transitions_src = []
        transitions_dst = []

        for node_idx in range(n):
            node_tr_src, node_tr_dst = self.get_node_transitions(
                node_idx, nodes, node_values)

            transitions_src.append(node_tr_src)
            transitions_dst.append(node_tr_dst)

        transitions_src = np.concatenate(transitions_src)
        transitions_dst = np.concatenate(transitions_dst)

        self.transition_table = scipy.sparse.csr_matrix(
            (
                np.ones((len(transitions_dst))),
                (transitions_dst,
                 transitions_src)
            ),
            shape=(states_count, states_count)
        )

class InitialState:

    def __init__(self, model: Model, fixed_nodes=[], fixed_values=[], fixed_probability=1):
        nodes = list(model.model.keys())
        n = len(nodes)
        states_count = 2 ** n
        states = np.empty((states_count, n), dtype=bool)
        for i in range(n):
            a = np.full((2 ** (i + 1)), True)
            a[:2 ** i] = False

            states[:, i] = np.tile(a, (2 ** (n - i - 1)))

        fixed_indices = [nodes.index(f) for f in fixed_nodes]
        fixed_state = [v == 1 for v in fixed_values]

        fixed_state_mask = np.sum(
            np.array(fixed_state) == states[:, fixed_indices], axis=1) == len(fixed_nodes)

        fixed_state_indices_count = np.sum(fixed_state_mask)
        nonfixed_state_indices_count = states_count - fixed_state_indices_count

        self.x_0 = np.empty((states_count))

        self.x_0[fixed_state_mask] = fixed_probability / \
            fixed_state_indices_count

        self.x_0[~fixed_state_mask] = (
            1 - fixed_probability) / nonfixed_state_indices_count

class TransitionGraph:

    def __init__(self, table: TransitionTable, initial_state: InitialState):
        self.table = table.transition_table
        self.sorted_subgraphs = []
        self.initial_state = initial_state.x_0

    def toposort(self, table):
        orig_table = table
        ordering = np.empty((table.shape[0]), dtype=int)
        mask = np.full((table.shape[0]), False)
        terminals_idx = None
        fill_idx = table.shape[0]
        while table.shape[0] != 0:
            new_mask = np.array(table.sum(axis=0) == 0).flatten()

            indices = np.argwhere(new_mask ^ mask).flatten()
            fill_idx -= indices.shape[0]
            ordering[fill_idx: fill_idx + indices.shape[0]] = indices

            mask = new_mask
            table = orig_table[~mask, :]

            if terminals_idx is None:
                terminals_idx = fill_idx

        return ordering, terminals_idx

    def create_metagraph(self, table):
        count, labels = scipy.sparse.csgraph.connected_components(
            table, directed=True, connection='strong')

        # All SCCs are single vertices => metagraph = graph
        if count == table.shape[0]:
            sccomponents = np.arange(count, dtype=int).reshape((count, 1))
            return sccomponents, table

        sccomponents = np.empty(shape=(count), dtype=object)

        sort_idx = np.argsort(labels)
        labels_sorted = labels[sort_idx]
        unq_first = np.concatenate(
            ([True], labels_sorted[1:] != labels_sorted[:-1]))
        unq_count = np.diff(np.nonzero(unq_first)[0])
        sccomponents = np.array(
            np.split(sort_idx, np.cumsum(unq_count)), dtype=object)

        rows, columns = table.nonzero()

        sccs_transitions_mask = labels[rows] != labels[columns]

        row_transitions = rows[sccs_transitions_mask]
        col_transitions = columns[sccs_transitions_mask]

        metagraph = scipy.sparse.csr_matrix(
            ([1] * len(row_transitions),
             (labels[row_transitions], labels[col_transitions])),
            shape=(count, count)
        )

        metagraph.data = np.ones((metagraph.data.shape))

        return sccomponents, metagraph

    def build_sorted_transition_table(self, original_table, sccs, sorted_metavertices):

        sorted_vertices = np.concatenate(sccs[sorted_metavertices])

        return original_table[sorted_vertices, :][:, sorted_vertices], sorted_vertices

    def sort(self):
        count, disconnected_subgraphs = scipy.sparse.csgraph.connected_components(
            self.table, directed=True, connection='weak')

        for i in range(count):
            subgraph_indices = np.argwhere(
                disconnected_subgraphs == i).flatten()

            # whole component is within empty part of initial state => skip
            if np.all(self.initial_state[subgraph_indices] == 0):
                continue

            subtable = self.table[subgraph_indices, :][:, subgraph_indices]

            subnodes_count = len(subgraph_indices)

            if (subnodes_count == 1):
                self.sorted_subgraphs.append((1, [[0]], [0, 1], [0]))
                continue

            sccs, metagraph = self.create_metagraph(subtable)

            if len(sccs) == 1:
                self.sorted_subgraphs.append(
                    (subgraph_indices, subtable, [0, subnodes_count], list(range(subnodes_count))))
                continue

            sccs_ordering, terminal_start_idx = self.toposort(metagraph)

            sorted_subtable, sorted_subvertices = self.build_sorted_transition_table(subtable,
                                                                                sccs, sccs_ordering)

            terminal_offsets = [0]

            for i in range(terminal_start_idx, len(sccs_ordering)):
                terminal_offsets.append(
                    terminal_offsets[-1] + len(sccs[sccs_ordering[i]]))

            terminal_offsets = [
                x + (subnodes_count - terminal_offsets[-1]) for x in terminal_offsets]

            self.sorted_subgraphs.append(
                (subgraph_indices, sorted_subtable, terminal_offsets, sorted_subvertices))

class Solution:

    def __init__(self, graph: TransitionGraph, initial_state: InitialState, nodes_count):
        self.subgraphs = graph.sorted_subgraphs
        self.initial_state = initial_state.x_0
        self.n = nodes_count

    def create_kinetic(self, trans_table):
        uni_weights = 1/(2*self.n)
        K = uni_weights * trans_table
        return K - scipy.sparse.diags(np.array(K.sum(axis=0))[0])

    def compute_scc_kernel(self, table, terminal_offsets, terminal_index):

        state_start = terminal_offsets[terminal_index]
        state_end = terminal_offsets[terminal_index + 1]

        scc_transition_table = table[state_start:
                                     state_end, state_start:state_end]

        nodes_count = scc_transition_table.shape[0]

        K = self.create_kinetic(scc_transition_table).todense()
        K = np.delete(K, 0, axis=0)

        kernel = np.empty((nodes_count))

        sign = -1 ** (nodes_count - 1)
        for i in range(nodes_count):
            minor = np.delete(K, i, axis=1)
            kernel[i] = sign * scipy.linalg.det(minor)
            sign *= -1

        return kernel / np.sum(kernel)

    def compute_column_nullspace(self, table, terminal_offsets):

        terminals_count = len(terminal_offsets) - 1
        terminal_start = terminal_offsets[0]

        data = np.empty((table.shape[0] - terminal_start))
        indices = []
        indptrs = [0] * (terminal_start + 1)

        for i in range(terminals_count):
            scc_size = terminal_offsets[i+1] - terminal_offsets[i]

            for _ in range(scc_size):
                indptrs.append(indptrs[-1] + 1)
            indices += [i] * scc_size
            if scc_size == 1:
                data[i] = 1
            else:
                data[i: i + scc_size] = self.compute_scc_kernel(
                    table, terminal_offsets, i)

        return scipy.sparse.csr_matrix((data, indices, indptrs), shape=(table.shape[0], terminals_count))

    def compute_row_nullspace(self, table, terminal_offsets, column_nullspace):

        terminal_start = terminal_offsets[0]

        U = column_nullspace[terminal_start:, :].transpose()
        U.data = np.array([1] * len(U.data))

        if terminal_start == 0:
            return U

        K = self.create_kinetic(table)
        N = K[:terminal_start, :][:, :terminal_start]
        B = K[terminal_start:, :][:, :terminal_start]

        X = -U @ B

        scipy.sparse.linalg.use_solver(
            useUmfpack=True, assumeSortedIndices=False)
        X = scipy.sparse.linalg.spsolve(N.conj().transpose(
        ), X.conj().transpose(), use_umfpack=True).conj().transpose()

        return scipy.sparse.hstack((X, U))

    def compute_final_states(self):

        for subgraph_indices, subtable, terminal_offsets, sorted_subvertices in self.subgraphs:

            # just a single disconnected state -> probability of final state does not change
            if len(subgraph_indices) == 1:
                continue

            R = self.compute_column_nullspace(subtable, terminal_offsets)
            L = self.compute_row_nullspace(subtable, terminal_offsets, R)

            self.initial_state[subgraph_indices[sorted_subvertices]
                               ] = R @ L @ self.initial_state[subgraph_indices[sorted_subvertices]]

        return self.initial_state

def state_to_human_readable(state, node_names):
    nonzero_indices = state.nonzero()[0]
    probabilities = np.empty((nonzero_indices.shape[0], 2), dtype=object)
    probabilities[:, 0] = state[nonzero_indices]

    for i, idx in enumerate(nonzero_indices):

        present_nodes = []

        node_i = 0
        while idx != 0:
            if idx % 2:
                present_nodes.append(node_i)
            idx = idx >> 1
            node_i += 1

        if len(present_nodes) > 0:
            nodes = [node_names[ind] for ind in present_nodes]
            probabilities[i, 1] = " -- ".join(nodes)

        else:
            probabilities[i, 1] = "<nil>"

    return probabilities