###############
# Authored by Weisheng Jiang
# Book 4  |  From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############

import plotly.graph_objects as go
import numpy as np
import streamlit as st
from scipy.stats import multivariate_normal

st.latex(r'''{\displaystyle f_{\mathbf {X} }(x_{1},\ldots ,x_{k})={\frac {\exp \left(-{\frac {1}{2}}({\mathbf {x} }-{\boldsymbol {\mu }})^{\mathrm {T} }{\boldsymbol {\Sigma }}^{-1}({\mathbf {x} }-{\boldsymbol {\mu }})\right)}{\sqrt {(2\pi )^{k}|{\boldsymbol {\Sigma }}|}}}}''')

def bmatrix(a):
    """Returns a LaTeX bmatrix

    :a: numpy array
    :returns: LaTeX bmatrix as a string
    """
    if len(a.shape) > 2:
        raise ValueError('bmatrix can at most display two dimensions')
    lines = str(a).replace('[', '').replace(']', '').splitlines()
    rv = [r'\begin{bmatrix}']
    rv += ['  ' + ' & '.join(l.split()) + r'\\' for l in lines]
    rv +=  [r'\end{bmatrix}']
    return '\n'.join(rv)

# x1_array = np.linspace(-3,3,301)
# x2_array = np.linspace(-3,3,301)
# x3_array = np.linspace(-3,3,301)

xxx1,xxx2,xxx3 = np.mgrid[-3:3:0.2,-3:3:0.2,-3:3:0.2]

with st.sidebar:

    sigma_1 = st.slider('sigma_1', min_value=0.5, max_value=3.0, value=1.0, step=0.1)
    sigma_2 = st.slider('sigma_2', min_value=0.5, max_value=3.0, value=1.0, step=0.1)
    sigma_3 = st.slider('sigma_3', min_value=0.5, max_value=3.0, value=1.0, step=0.1)

    rho_1_2 = st.slider('rho_1_2', min_value=-0.95, max_value=0.95, value=0.0, step=0.05)
    rho_1_3 = st.slider('rho_1_3', min_value=-0.95, max_value=0.95, value=0.0, step=0.05)
    rho_2_3 = st.slider('rho_2_3', min_value=-0.95, max_value=0.95, value=0.0, step=0.05)

SIGMA = np.array([[sigma_1**2, rho_1_2*sigma_1*sigma_2, rho_1_3*sigma_1*sigma_3],
                  [rho_1_2*sigma_1*sigma_2, sigma_2**2, rho_2_3*sigma_2*sigma_3],
                  [rho_1_3*sigma_1*sigma_3, rho_2_3*sigma_2*sigma_3, sigma_3**2]])

st.latex(r'\Sigma = ' + bmatrix(SIGMA))

MU = np.array([0, 0, 0])

# st.write(xxx1.shape)
pos = np.dstack((xxx1.ravel(),xxx2.ravel(),xxx3.ravel()))

# st.write(pos.shape)
rv  = multivariate_normal(MU, SIGMA)
PDF = rv.pdf(pos)

fig = go.Figure(data=go.Volume(
    x=xxx1.flatten(),
    y=xxx2.flatten(),
    z=xxx3.flatten(),
    value=PDF.flatten(),
    isomin=0,
    isomax=PDF.max(),
    colorscale = 'RdYlBu_r',
    opacity=0.1,
    surface_count=11,
    ))

fig.update_layout(scene = dict(
                    xaxis_title=r'x_1',
                    yaxis_title=r'x_2',
                    zaxis_title=r'x_3'),
                    width=1000,
                    margin=dict(r=20, b=10, l=10, t=10))

st.plotly_chart(fig, theme=None, use_container_width=True)


import pygame
from collections import namedtuple, defaultdict
from progress.pathfind import MinHeap, Graph, is_edge, maze_to_graph

BLACK = (0,0,0)
WHITE = (255, 255, 255)
RED = (255,0,0)
BLUE = (5,21,104)

pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 50)
WINDOW_WIDTH = 500
WINDOW_HEIGHT = 600
GAME_WIDTH = 500
GAME_HEIGHT = 500
top_offset = (WINDOW_HEIGHT - GAME_HEIGHT) / 2
gameDisplay = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('pacman')

Coord = namedtuple('Coord', 'x, y')
Move = namedtuple('Move', 'dest, cost')

# Maze
maze_arr = []
with open('mazes/maze_01.txt', 'r') as maze:
    m = maze.readlines()
    for i in m:
        line = list(i.rstrip())
        maze_arr.append(line)

target_score = sum(row.count('@') for row in maze_arr)
# print(target_score)

maze_height = len(maze_arr)
maze_width = len(maze_arr[0])
pixel_height = GAME_HEIGHT / maze_height
pixel_width = GAME_WIDTH / maze_width

# Pacman
obj_width = 18
obj_height = 18
pacman_orig = pygame.image.load('img/pacman.png')
pacman_orig = pygame.transform.scale(pacman_orig, (obj_width, obj_height))
pacman_right = pygame.transform.rotate(pacman_orig, 0)
pacman_left = pygame.transform.rotate(pacman_orig, 180)
pacman_up = pygame.transform.rotate(pacman_orig, 90)
pacman_down = pygame.transform.rotate(pacman_orig, 270)
obj_x = 1
obj_y = 1
score = 0

# Pacman movement
MOVE_UP = 1
MOVE_DOWN = 2
MOVE_RIGHT = 3
MOVE_LEFT = 4
next_move = 0
change_x = 0
change_y = 0

# Ghost
ghost_width = 16
ghost_height = 16
ghost = pygame.image.load('img/red.png')
ghost = pygame.transform.scale(ghost, (ghost_width, ghost_height))
ghost_x = 23
ghost_y = 23
ghost_change_x = 0
ghost_change_y = 0

node_list = maze_to_graph(maze_arr).return_node_list()

ghost_recalculate_counter = 0
graph = maze_to_graph(maze_arr)
start = Coord(ghost_x,ghost_y)
end = Coord(obj_x,obj_y)
path = graph.dijkstra(start, end)
target_index = 0
def ghost_recalculate():
    global start
    global end
    global path
    global graph
    global node_list
    start = Coord(ghost_x,ghost_y)
    end = Coord(obj_x,obj_y)
    if start not in node_list:
        dist = 1 #UP
        while(True):
            if maze_arr[start.y-dist][start.x] == '#':
                break
            if is_edge(maze_arr, start.y-dist, start.x):
                dest = Coord(start.x, start.y-dist)
                graph.add_edge(start, dest, dist) #src, dest, cost
                break
            dist += 1
        dist = 1    #DOWN
        while(True):
            if maze_arr[start.y+dist][start.x] == '#':
                break
            if is_edge(maze_arr, start.y+dist, start.x):
                dest = Coord(start.x, start.y+dist)
                graph.add_edge(start, dest, dist) #src, dest, cost
                break
            dist += 1
        dist = 1    #LEFT
        while(True):
            if maze_arr[start.y][start.x-dist] == '#':
                break
            if is_edge(maze_arr, start.y, start.x-dist):
                dest = Coord(start.x-dist, start.y)
                graph.add_edge(start, dest, dist) #src, dest, cost
                break
            dist += 1
        dist = 1    #RIGHT
        while(True):
            if maze_arr[start.y][start.x+dist] == '#':
                break
            if is_edge(maze_arr, start.y, start.x+dist):
                dest = Coord(start.x+dist, start.y)
                graph.add_edge(start, dest, dist) #src, dest, cost
                break
            dist += 1

    if end not in node_list:
        dist = 1 #UP
        while(True):
            if maze_arr[end.y-dist][end.x] == '#':
                break
            if is_edge(maze_arr, end.y-dist, end.x):
                dest = Coord(end.x, end.y-dist)
                graph.add_edge(end, dest, dist) #src, dest, cost
                break
            dist += 1
        dist = 1    #DOWN
        while(True):
            if maze_arr[end.y+dist][end.x] == '#':
                break
            if is_edge(maze_arr, end.y+dist, end.x):
                dest = Coord(end.x, end.y+dist)
                graph.add_edge(end, dest, dist) #src, dest, cost
                break
            dist += 1
        dist = 1    #LEFT
        while(True):
            if maze_arr[end.y][end.x-dist] == '#':
                break
            if is_edge(maze_arr, end.y, end.x-dist):
                dest = Coord(end.x-dist, end.y)
                graph.add_edge(end, dest, dist) #src, dest, cost
                break
            dist += 1
        dist = 1    #RIGHT
        while(True):
            if maze_arr[end.y][end.x+dist] == '#':
                break
            if is_edge(maze_arr, end.y, end.x+dist):
                dest = Coord(end.x+dist, end.y)
                graph.add_edge(end, dest, dist) #src, dest, cost
                break
            dist += 1

    path = graph.dijkstra(start, end)
    if start not in node_list:
        graph.remove_edge(start)
    if end not in node_list:
        graph.remove_edge(end)

keepGoing = True
pause = False
clock = pygame.time.Clock()
pacman = pacman_orig

while keepGoing:
    for event in pygame.event.get():    #event handler
        if event.type == pygame.QUIT:
            keepGoing = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                if maze_arr[obj_y-1][obj_x] != '#':
                    change_x = 0
                    change_y = -1
                    next_move = 0
                else:
                    next_move = MOVE_UP
            elif event.key == pygame.K_DOWN:
                if maze_arr[obj_y+1][obj_x] != '#':
                    change_x = 0
                    change_y = 1
                    next_move = 0
                else:
                    next_move = MOVE_DOWN
            elif event.key == pygame.K_LEFT:
                if maze_arr[obj_y][obj_x-1] != '#':
                    change_x = -1
                    change_y = 0
                    next_move = 0
                else:
                    next_move = MOVE_LEFT
            elif event.key == pygame.K_RIGHT:
                if maze_arr[obj_y][obj_x+1] != '#':
                    change_x = 1
                    change_y = 0
                    next_move = 0
                else:
                    next_move = MOVE_RIGHT

    if ghost_x == obj_x and ghost_y == obj_y:
        print('CAUGHT!')
        gameDisplay.fill(BLACK)
        final_font = pygame.font.SysFont('Comic Sans MS', 50)
        stop_text = final_font.render('GAME OVER...', True, WHITE)
        gameDisplay.blit(stop_text,(0,0))
        pygame.display.update()
        pygame.time.delay(1000)
        keepGoing = False
        continue

    if next_move > 0:
        if next_move == MOVE_UP and maze_arr[obj_y-1][obj_x] != '#':
            change_x = 0
            change_y = -1
        elif next_move == MOVE_DOWN and maze_arr[obj_y+1][obj_x] != '#':
            change_x = 0
            change_y = 1
        elif next_move == MOVE_LEFT and maze_arr[obj_y][obj_x-1] != '#':
            change_x = -1
            change_y = 0
        elif next_move == MOVE_RIGHT and maze_arr[obj_y][obj_x+1] != '#':
            change_x = 1
            change_y = 0
    if maze_arr[obj_y+change_y][obj_x+change_x] == '#':
        change_x = 0
        change_y = 0
    elif maze_arr[obj_y+change_y][obj_x+change_x] == '@':
        maze_arr[obj_y+change_y][obj_x+change_x] = ' '
        score += 1
        if score == target_score:
            keepGoing = False

    if change_x == 1: #right
        pacman = pacman_right
    elif change_x == -1: #left
        pacman = pacman_left
    elif change_y == -1: #up
        pacman = pacman_up
    elif change_y == 1: #down
        pacman = pacman_down

    obj_x += change_x
    obj_y += change_y

    #GHOST MOVEMENT
    target_x = path[target_index].x
    target_y = path[target_index].y
    if target_x > ghost_x:
        ghost_change_x = 1
        ghost_change_y = 0
    elif target_x < ghost_x:
        ghost_change_x = -1
        ghost_change_y = 0
    elif target_y > ghost_y:
        ghost_change_y = 1
        ghost_change_x = 0
    elif target_y < ghost_y:
        ghost_change_y = -1
        ghost_change_x = 0

    ghost_x += ghost_change_x
    ghost_y += ghost_change_y

    ghost_recalculate_counter += 1
    if target_x == ghost_x and target_y == ghost_y:
        target_index += 1
    if target_index == len(path) or ghost_recalculate_counter == 5:
        ghost_recalculate_counter = 0
        ghost_recalculate()
        target_index = 0
    # print('path_length:', len(path))
    # print('start:', start)
    # print('end:', end)
    # print('path:', path)
    # print('target_index:', target_index, '\n')

    #Draw maze
    gameDisplay.fill(BLACK)
    lead_x = 0
    lead_y = 0
    for row in maze_arr:
        for col in row:
            if col == '#':
                pygame.draw.rect(gameDisplay, BLUE, (lead_x, top_offset+lead_y, pixel_width, pixel_height))
            elif col == '@':
                pygame.draw.circle(gameDisplay, WHITE, (int(lead_x + pixel_width/2), int(top_offset + lead_y + pixel_height/2)), 3, 0)
            lead_x += pixel_width
        lead_y += pixel_height
        lead_x = 0

    gameDisplay.blit(pacman, (obj_x*pixel_width+1,top_offset+obj_y*pixel_height+1))
    gameDisplay.blit(ghost, (ghost_x*pixel_width+2,top_offset+ghost_y*pixel_height+2))
    #pygame.draw.rect(gameDisplay, RED, (ghost_x*pixel_width+5, top_offset+ghost_y*pixel_height+5, 10, 10))
    score_text = myfont.render('score: '+ str(score), True, WHITE)
    gameDisplay.blit(score_text,(WINDOW_WIDTH/30, top_offset/5))
    pygame.display.update()
    clock.tick(5)

pygame.quit()
quit()


import numpy as np
import progressbar
import gym
from tensorflow.python.keras.optimizer_v2.adam import Adam

from rl.agent import Agent

# note: for 08.10.2022 requires a stable_baselines3 package to be installed
if __name__ == '__main__':
    # prepare an environment
    environment = gym.make("Taxi-v3")
    environment.reset()
    environment.render()

    print('Number of states: {}'.format(environment.observation_space.n))
    action_space = environment.action_space
    print('Number of actions: {}'.format(action_space.n))

    optimizer = Adam(learning_rate=0.01)
    agent = Agent(environment, optimizer)

    batch_size = 2000  # 2000
    num_of_episodes = 20  # 20
    time_steps_per_episode = 100  # 100
    agent.q_network.summary()

    # training (could take 30m+, increase batch_size/num_of_episodes, decrease time_steps_per_episode to speed up)
    for e in range(0, num_of_episodes):
        # Reset the environment
        state = environment.reset()
        state = np.reshape(state, [1, 1])

        # Initialize variables
        reward = 0
        terminated = False

        bar = progressbar.ProgressBar(maxval=time_steps_per_episode / 10,
                                      widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
        bar.start()

        for timestep in range(time_steps_per_episode):
            # Run Action
            action = agent.act(environment, state)

            # Take action
            next_state, reward, terminated, info = environment.step(action)
            next_state = np.reshape(next_state, [1, 1])
            agent.store(state, action, reward, next_state, terminated)

            state = next_state

            # average termination is on the 20th step
            if terminated:
                agent.align_target_model()
                break

            if len(agent.experience_replay) > batch_size:
                agent.retrain(batch_size)

            if timestep % 10 == 0:
                bar.update(timestep / 10 + 1)

        bar.finish()
        if (e + 1) % 10 == 0:
            print("**********************************")
            print("Episode: {}".format(e + 1))
            environment.render()
            print("**********************************")
    print("Model trained!")

    # model evaluation (could take 30m+, decrease test_num_of_episodes to speed up)
    test_total_epochs = 0
    test_total_penalties = 0
    test_num_of_episodes = 15  # 100

    for _ in range(test_num_of_episodes):
        print(f'Episode {_}')
        state = environment.reset()
        state = np.reshape(state, [1, 1])
        epochs = 0
        penalties = 0
        reward = 0

        terminated = False

        while not terminated:
            if epochs % 10 == 0:
                print(f'Timestep {epochs}')
            action = np.argmax(agent.q_network.predict(state)[0])
            state, reward, terminated, info = environment.step(action)
            state = np.reshape(state, [1, 1])

            if reward == -10:
                penalties += 1

            epochs += 1

        test_total_penalties += penalties
        test_total_epochs += epochs

    print("**********************************")
    print("Results")
    print("**********************************")
    print("Epochs per episode: {}".format(test_total_epochs / test_num_of_episodes))
    print("Penalties per episode: {}".format(test_total_penalties / test_num_of_episodes))
    # fixme model is walking on the field for too long and makes a lot of wrong pickups/drops


import math
from PIL import Image

suites = ['Heart', 'Club', 'Diamond', 'Spade']
values = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']

class Card:
    suit = None
    value = None

    image_dimensions = 71, 96
    image_pad = 2

    def __init__ (self, suit, value):
        self.suit = int(suit)
        self.value = int(value)

    def __str__ (self):
        return "%s of %ss (%s)" % (values[self.value], suites[self.suit], self.get_color())

    def get_color (self):
        return 'Black' if (self.suit % 2) else 'Red'

    def get_card_image (self):
        suit_translation = [2,0,3,1][self.suit]

        im = Image.open('cards.png')
        x = self.image_pad * (self.value + 1) + self.value * self.image_dimensions[0]
        y = 1 + self.image_pad * suit_translation + suit_translation * self.image_dimensions[1]
        return im.crop((
            x,
            y,
            x + self.image_dimensions[0],
            y + self.image_dimensions[1]
        ))

def build_new_deck ():
    deck = []
    for i in range(0, 52):
        suit = math.floor(i/13)
        value = i % 13
        if i >= 26:
            value = 12 - value

        deck.append(Card(
            suit,
            value
        ))
    return deck

def faro_shuffle (deck):
    half_count = len(deck)/2
    left_cut = deck[:half_count]
    right_cut = deck[half_count:]
    new_deck = []
    for i in range(0, half_count):
        new_deck.append(right_cut[i])
        new_deck.append(left_cut[i])
    return new_deck

def longest_sequences (deck):
    max_suite_sequence = 0
    max_value_sequence = 0
    max_color_sequence = 0

    previous_suite = None
    previous_value = None
    previous_color = None
    current_value_direction = None # -1 descending, 0 duplicates, 1 ascending

    current_suite_sequence = 0
    current_value_sequence = 0
    current_color_sequence = 0

    for card in deck:
        if card.suit != previous_suite:
            max_suite_sequence = max(max_suite_sequence, current_suite_sequence)
            current_suite_sequence = 0

        if card.value == previous_value:
            if current_value_direction != 0:
                max_value_sequence = max(max_value_sequence, current_value_sequence)
                current_value_direction = 0
                current_value_sequence = 1
        elif card.value - 1 == previous_value:
            if current_value_direction != 1:
                max_value_sequence = max(max_value_sequence, current_value_sequence)
                current_value_direction = 1
                current_value_sequence = 1
        elif card.value + 1 == previous_value:
            if current_value_direction != -1:
                max_value_sequence = max(max_value_sequence, current_value_sequence)
                current_value_direction = -1
                current_value_sequence = 1
        else:
            max_value_sequence = max(max_value_sequence, current_value_sequence)
            current_value_direction = None
            current_value_sequence = 0

        if card.get_color() != previous_color:
            max_color_sequence = max(max_color_sequence, current_color_sequence)
            current_color_sequence = 0

        current_suite_sequence += 1
        current_value_sequence += 1
        current_color_sequence += 1

        previous_suite = card.suit
        previous_value = card.value
        previous_color = card.get_color()

    max_suite_sequence = max(max_suite_sequence, current_suite_sequence)
    max_value_sequence = max(max_value_sequence, current_value_sequence)
    max_color_sequence = max(max_color_sequence, current_color_sequence)

    return max_suite_sequence, max_value_sequence, max_color_sequence

def print_deck (deck):
    for card in deck:
        print(card)

def save_deck_image (deck, name):
    til = Image.new("RGB",(834,95))
    x = 0
    for card in deck:
        til.paste(card.get_card_image(),(x,0))
        x += 15
    til.save("images/%s.png" % name)

deck = build_new_deck()
for i in range(0, 100):
    max_suite_sequence, max_value_sequence, max_color_sequence = longest_sequences(deck)
    save_deck_image(deck, '%s - %s, %s, %s' % (i, max_suite_sequence, max_value_sequence, max_color_sequence))
    deck = faro_shuffle(deck)


"""This is a module to extract loops from structures. It uses matlab to find
all loops and then will save them in the correct location as specificed by
'locations'. It also stores the the loop information into the database.
"""

import os
import functools as ft

from pymotifs import core
from pymotifs.utils import matlab
from pymotifs import models as mod
from pymotifs.utils.correct_units import Correcter

from pymotifs.pdbs.info import Loader as PdbLoader
from pymotifs.units.info import Loader as UnitLoader
from pymotifs.mat_files import Loader as MatLoader
from pymotifs.interactions.loader import Loader as InteractionLoader

class Loader(core.SimpleLoader):
    loop_types = ['IL', 'HL', 'J3']
    merge_data = True
    allow_no_data = True
    dependencies = set([PdbLoader, MatLoader, InteractionLoader, UnitLoader])
    save_loops = True

    def to_process(self, pdbs, **kwargs):

        with self.session() as session:
            query = session.query(mod.LoopInfo.pdb_id).\
                distinct()
            known = [r.pdb_id for r in query]

        to_use = sorted(set(pdbs).difference(known))  #We want to process ONLY the pdbs that are NOT in loop_info

        self.logger.info("Extracting loops from %s" % to_use)

        if not to_use:
            raise core.Skip("no new PDB ids that need loops extracted")
        return to_use

    def query(self, session, pdb):
        return session.query(mod.LoopInfo).filter_by(pdb_id=pdb)

    def remove(self, *args, **kwargs):
        """Does not actually remove from the DB. We always want the loop ids to
        be consistent so we do not automatically remove loops.
        """

        self.logger.info("We don't actually remove data for loop extractor")

    def normalizer(self, pdb):
        """Create a callable that will normalize a comma seperated string of
        unit ids for a particular structure.

        :param str pdb: The PDB id to use for normalization.
        :returns: A callable that will translate a comma seperated string of
        unit ids in the structure.
        """

        corrector = Correcter(self.config, self.session)
        mapping = corrector.normalized_mapping(pdb)

        def fn(unit_string):
            unit_ids = unit_string.split(',')
            units = [corrector.as_unit(uid) for uid in unit_ids]
            corrected = corrector.correct_structure(pdb, mapping, units,
                                                    require_all=True)
            return tuple(sorted(unit for unit in corrected))

        return fn

    def _next_loop_number_string(self, current):
        """Compute the next loop number string. This will pad to either 3 or 6
        characters with zeros. If the next number is over 999 we use 6,
        otherwise 3 as the length to pad to.

        :current: The current loop count.
        :returns: A string of the next loop id.
        """

        next_number = current + 1
        if next_number > 999:
            return str(next_number).rjust(6, '0')
        return str(next_number).rjust(3, '0')

    def _get_loop_id(self, units, pdb_id, loop_type, mapping):
        """Compute the loop id to use for the given unit string. This will
        build a string like IL_1S72_001 or IL_4V4Q_001000. In structures with
        over 999 loops, we will pad with zeros to 6 characters, but keep the
        stanadrd padding to 3 characters otherwise.

        :units: The concanated unit or nt id string.
        :pdb_id: The pdb id to use.
        :loop_type: The type of loop.
        :mapping: A mapping from unit string to known loop_id.
        :count: The current number of known loops.
        :returns: A string of the new loop id.
        """

        # format examples: IL_1S72_001, IL_4V4Q_001000
        if units not in mapping:
            str_number = self._next_loop_number_string(len(mapping))
            loop_id = '%s_%s_%s' % (loop_type, pdb_id, str_number)
            self.logger.info('Created new loop id %s, for nucleotides %s',
                             loop_id, units)
            mapping[units] = str(loop_id)

        return mapping[units]

    def _get_fake_loop_id(self, pdb_id, loop_type):

        loop_id = '%s_%s_%s' % (loop_type, pdb_id, '000')
        self.logger.info('Created new fake loop id %s for pdb_id %s', loop_id, pdb_id)

        return loop_id

    def _extract_loops(self, pdb, loop_type, mapping, normalize):
        """Uses matlab to extract the loops for a given structure of a specific
        type. This will also save the loop files into the correct place.

        :param str pdb: PDB file to process
        :param str loop_type: The type of loop (IL, HL, J3, ...) to extract
        loops for.
        :param dict mapping: A mapping of unit ids to known loop names.
        :returns: The extracted loops.
        """
        try:
            mlab = matlab.Matlab(self.config['locations']['fr3d_root'])
            [loops, count, err_msg] = mlab.extractLoops(pdb, loop_type, nout=3)
        except Exception as err:
            self.logger.exception(err)
            raise err

        if err_msg != '':
            raise core.MatlabFailed(err_msg)

        if loops == 0:
            self.logger.warning('No %s in %s', loop_type, pdb)
            loop_id = self._get_fake_loop_id(pdb, loop_type)
            return [mod.LoopInfo(loop_id=loop_id,
                type = 'NA',
                pdb_id=pdb,
                sequential_id='000',
                length=0,
                seq='',
                r_seq='',
                nwc_seq='',
                r_nwc_seq='',
                unit_ids='',
                loop_name='')]

        self.logger.info('Found %i %s loops', count, loop_type)

        data = []
        for index in xrange(count):
            loop = loops[index].AllLoops_table
            full_id = normalize(loop.full_id)
            loop_id = self._get_loop_id(full_id, pdb, loop_type, mapping)
            loops[index].Filename = loop_id

            data.append(mod.LoopInfo(
                loop_id=loop_id,
                type=loop_type,
                pdb_id=pdb,
                sequential_id=loop_id.split("_")[-1],
                length=int(loops[index].NumNT[0][0]),
                seq=loop.seq,
                r_seq=loop.r_seq,
                nwc_seq=loop.nwc,
                r_nwc_seq=loop.r_nwc,
                unit_ids=','.join(full_id),
                loop_name=loop.loop_name))

        if self.save_loops:
            self.__save__(loops, self.config['locations']['loops_mat_files'])

        return data

    def __save__(self, loops, location):
        """Save the loops to a file.

        :loops: The loops matlab proxy object to save.
        :param str location: The location to saave to.
        """

        if not os.path.isdir(location):
            os.makedirs(location)

        try:
            mlab = matlab.Matlab(self.config['locations']['fr3d_root'])
            [status, err_msg] = mlab.aSaveLoops(loops, location, nout=2)
        except Exception as err:
            self.logger.exception(err)
            raise err

        if status != 0:
            self.logger.error(mlab.last_stdout)
            raise matlab.MatlabFailed("Could not save all loop mat files")

        self.logger.debug("Saved loop mat files")

    def _mapping(self, pdb_id, loop_type, normalizer):
        """Compute a mapping from the nts to the loop id.  This is used
        for setting ids by either looking up the old known id or creating a new
        one if no one is found.

        :pdb_id: The pdb id to search.
        :loop_type: The loop type.
        :returns: A dictonary mapping from the nts to the loop id. If there are
        no loops it returns an empty dictonary.
        """

        mapping = {}
        with self.session() as session:
            query = self.query(session, pdb_id).filter_by(type=loop_type)
            for result in query:
                unit_ids = normalizer(result.unit_ids)
                if unit_ids in mapping:
                    self.logger.error("Loop %s duplicates %s",
                                      result.loop_id, mapping[unit_ids])
                    continue
                mapping[unit_ids] = result.loop_id
        return mapping

    def data(self, pdb, **kwargs):
        """Compute the loops in the given structure.

        :param str pdb: The structure to get loops for.
        :returns: A list of all the loops.
        """

        data = []
        normalizer = self.normalizer(pdb)
        for loop_type in self.loop_types:
            mapping = self._mapping(pdb, loop_type, normalizer)
            data.extend(self._extract_loops(pdb, loop_type, mapping, normalizer))
        return data


import requests
from requests.structures import CaseInsensitiveDict

from http.server import BaseHTTPRequestHandler, HTTPServer
import json

class MyRequestHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length)

        # decodificar el mensaje HTTP POST
        post_data = post_data.decode('utf-8')

        # analizar el contenido JSON del mensaje HTTP POST
        post_json = json.loads(post_data)

        # imprimir el contenido del mensaje HTTP POST
        print(f"Mensaje HTTP POST recibido: {post_json}")

        ######################
        # Envia el mensaje al whatsapp

        url = "https://graph.facebook.com/v15.0/101042539575372/messages"

        headers = CaseInsensitiveDict()
        headers["Authorization"] = "Bearer EAAJd7y5KaZA8BAN0t0ZC8VVVAhNzmtmpaESVrJlLEDwLwAqVCMg9z5ImZCWX1LLGCVOhDrbXM03VoE9oS2vrSWWbav5oRrw6djnVs1nPoWKZAT6IJSxPKsWuy697jPkfeLtq7PtQIj8VyjZCHZBoGUrjchZB3EmSosLdZAsfL5LelNV92DcVz653oN6RrRETjGQX3RMXCURxggZDZD"
        headers["Content-Type"] = "application/json"

        data = '{ "messaging_product": "whatsapp", "to": "50256327243", "type": "template", "template": { "name": "hello_world", "language": { "code": "en_US" } } }'

        resp = requests.post(url, headers=headers, data=data)

        print(resp.status_code)

        # enviar una respuesta al cliente
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        response = "Mensaje HTTP POST recibido"
        self.wfile.write(response.encode('utf-8'))

def run_server():
    print("Ip: ")
    Ip_k = str(input())
    print("Puerto: ")
    Puerto_k = int(input())
    server_address = (Ip_k, Puerto_k)
    httpd = HTTPServer(server_address, MyRequestHandler)
    print(f'Servidor en ejecución: http://{server_address[0]}:{server_address[1]}')
    httpd.serve_forever()

if __name__ == '__main__':
    run_server()


import spritesheet_handler, pygame
pygame.init()

#Base class for all beings in the game.
class Olio():
    def __init__(self,posX,posY,Sprite, SpriteWidth, SpriteHeight):
        #Each character has a position, and a sprite sheet.
        self.__x = posX
        self.__y = posY
        self.__sprites = spritesheet_handler.spritesheet(Sprite)
        #self.__n = 0
        self.__spriteWidth = SpriteWidth
        self.__spriteHeight = SpriteHeight
        #self.__runner = SpriteStripAnim(Sprite,pygame.Rect(216,2,41,351),5)
        self.__leftruns = [(216,0,41,65),
                           (216,72,41,65),
                           (216,144,41,67),
                           (216,215,37,68),
                            (216,287,41,64)]

    def __get_anims(SpritePosList):
        for column in SpritePosList:
            self.__anims.append

    #The function for drawing the character.
    def __draw__(self, gameDisplay,moveX,moveY):
        image = self.__sprites.image_at((0,0,41,65))
        if moveY > 0:
            try:
                image = self.__sprites.image_at(self.__leftruns[self.__n])
                self.__n += 1
            except IndexError:
                self.__n = 0
            #image = self.__runner.
        #elif moveY != 0:
        gameDisplay.blit(image,(self.__x,self.__y))
        #gameDisplay.blit(self.__sprites.image_at(pygame.Rect(1,2,40,64)),(self.__x,self.__y))

    #Move character on the X-axis.
    def __move_x_axis__(self, moveAmount):
        self.__x += moveAmount

    #Move character on the Y-axis.
    def __move_y_axis__(self, moveAmount):
        self.__y += moveAmount

    #Function for handling the animation. Work in progress.
    def __animation(self):
        self.__runner

#Class of the Player character. Derived from the base character class Olio.
class main_dude(Olio):
    def __nothing(self):
        pass
    '''
    def __init__(self,posX,posY,Sprite):
        self.__x = posX
        self.__y = posY
        self.__sprites = spritesheet_handler.spritesheet(Sprite)'''

#class enemy(olio):


import os
import platform
from pathlib import Path
import subprocess

IS_WINDOWS = (platform.system() == 'Windows')

if IS_WINDOWS:
    UNRAR_EXE = str(Path(__file__).parent / 'bin' / 'UnRAR.exe')
else:
    UNRAR_EXE = 'unrar'

def unrar(filepath, folder, unrar_executable=UNRAR_EXE):
    # UnRAR wants its folder argument with '/'
    folder = "{}{}".format(folder, os.sep)
    tokens = [unrar_executable, 'e', str(filepath), folder, '-y']
    try:
        return subprocess.check_call(tokens)
    except subprocess.CalledProcessError as e:
        raise e

class DocFiles:
    def __init__(self, folder):
        self.folder = Path(folder)

    @property
    def paths(self):
        return [str(x) for x in self.folder.glob('*.doc*')]

    def exist(self):
        return len(self.paths) > 0

    def __str__(self):
        return ' ' * 4 + ('\n' + ' ' * 4).join(self.paths)

class Unpacker:
    def __init__(self, archive_filepath,
                 destination_folder):
        self.path = archive_filepath
        if not Path(self.path).exists():
            raise FileNotFoundError(self.path)
        self.folder = destination_folder
        self.status = (f'Already unpacked:\n{self.docs}'
                       if self.docs.exist()
                       else None)

    @property
    def docs(self):
        return DocFiles(self.folder)

    def run(self):
        res = unrar(self.path, self.folder)
        if res == 0:
            self.status = f'UnRARed {self.path}'
            return True
        return False


import pygame, random, sys
pygame.init()
screen = pygame.display.set_mode((800,600))

draw_on = False
last_pos = (0, 0)
color = (255, 128, 0)
radius = 10

def roundline(srf, color, start, end, radius=1):
    dx = end[0]-start[0]
    dy = end[1]-start[1]
    distance = max(abs(dx), abs(dy))
    for i in range(distance):
        x = int( start[0]+float(i)/distance*dx)
        y = int( start[1]+float(i)/distance*dy)
        pygame.draw.circle(srf, color, (x, y), radius)

try:
    while True:
        e = pygame.event.wait()
        print(e)

        if e.type == pygame.QUIT:
            raise StopIteration
        if e.type == pygame.MOUSEBUTTONDOWN:
            if e.button == 3:
                print('\nSaved my bit!')
                fname = "hml.png"
                pg.image.save(win, fname)
                pygame.quit()
            color = (random.randrange(256), random.randrange(256), random.randrange(256))
            pygame.draw.circle(screen, color, e.pos, radius)
            draw_on = True
        if e.type == pygame.MOUSEBUTTONUP:
            draw_on = False
        if e.type == pygame.MOUSEMOTION:
            if draw_on:
                pygame.draw.circle(screen, color, e.pos, radius)
                roundline(screen, color, e.pos, last_pos,  radius)
            last_pos = e.pos
        pygame.display.flip()
        pygame.event.pump()
except StopIteration:
    pass

pygame.quit()


import os
import warnings

import matplotlib.pyplot as plt
import missingno as msno
import numpy as np
import pandas as pd
import seaborn as sns
from plotnine import *

warnings.simplefilter(action='ignore', category=FutureWarning)

def plot_matches_year(data: pd.DataFrame):
    """Plot year-match_count stats.

     Data structure:
    ['date', 'team1', 'team1Text', 'team2', 'team2Text', 'resText',
       'statText', 'venue', 'IdCupSeason', 'CupName', 'team1Score',
       'team2Score', 'team1PenScore', 'team2PenScore']
    """
    g = ggplot(mapping=aes('date.dt.year'), data=data) + \
        geom_bar(mapping=aes(fill="CupName"), width=1, color="black") + \
        theme(legend_position='bottom', legend_direction='vertical') + \
        ggtitle("Matches played by Year")

    print(g)

def plot_mean_goals_year(data: pd.DataFrame):
    """Plot year-mean_goals stats.

     Data structure:
    ['date', 'team1', 'team1Text', 'team2', 'team2Text', 'resText',
       'statText', 'venue', 'IdCupSeason', 'CupName', 'team1Score',
       'team2Score', 'team1PenScore', 'team2PenScore']
    """
    year = sorted(data.date.dt.year.drop_duplicates())
    goals_team_sum = data['team1Score'] + data['team2Score']
    goals_per_game = [goals_team_sum[data.date.dt.year == y].sum() / sum(data.date.dt.year == y) for y in year]
    new_data = pd.DataFrame({'year': year, 'goals_per_game': goals_per_game})

    g = ggplot(mapping=aes(x='year', y='goals_per_game'), data=new_data) + \
        geom_point() + \
        geom_smooth(method="loses") + ggtitle("Goals scored per game, over time")

    print(g)

def plot_missing_map(data: pd.DataFrame):
    """Plot missing map of the data.

     Data structure:
    ['date', 'team1', 'team1Text', 'team2', 'team2Text', 'resText',
       'statText', 'venue', 'IdCupSeason', 'CupName', 'team1Score',
       'team2Score', 'team1PenScore', 'team2PenScore']
    """
    msno.matrix(data)
    plt.show()

def plot_win_percent(team_perf: pd.DataFrame, min_games: int = 100):
    """Plot win percentage diagram.

    Data structure:
    ['match_id': index, 'date', 'name', 'opponentName', 'homeVenue',
    'neutralVenue', 'gs', 'ga', 'gd', 'win', 'loss', 'draw',
    'friendly', 'qualifier', 'finaltourn']
    """

    def win_percent(wins, draws, total):
        return (wins + 0.5 * draws) / total

    # Group by team name.
    groups = team_perf.groupby('name')

    names, win_percentage, total_games = [], [], []
    for name, group in groups:
        count = len(group)
        if count >= min_games:
            names.append(name)
            win_percentage.append(
                win_percent(sum(group.win), sum(group.draw), count))
            total_games.append(count)

    new_data = pd.DataFrame({'name': names, 'win_percentage': win_percentage,
                             'total_games': total_games})

    g = ggplot(mapping=aes(x='win_percentage', y='total_games'), data=new_data) + \
        geom_point(size=1.5) + \
        geom_text(aes(label='name'), ha='left', va='bottom') + \
        geom_vline(xintercept=.5, linetype='solid', color="red") + \
        ggtitle("Winning Percentage vs Games Played") + \
        expand_limits(x=(0, 1))

    print(g)

def plot_goal_sum_per_match(data: pd.DataFrame):
    """Plot goals scored per match distribution.

    Data structure.
    ['gameSum','count','freq']
    """
    data = data[data.freq >= 0.01]
    g = ggplot(mapping=aes(x='goalScore', y='freq'), data=data) + \
        geom_bar(stat="identity") + \
        geom_text(aes(label='round(freq, 2)'), va='top', nudge_y=0.01) + \
        ggtitle('Goals scored per match distribution')

    print(g)

def plot_goal_diff_per_match(data: pd.DataFrame):
    """Plot goals scored per match distribution.

    Data structure.
    ['goalDiff','count','freq']
    """
    data = data[abs(data.goalDiff) <= 4]
    g = ggplot(mapping=aes(x='goalDiff', y='freq'), data=data) + \
        geom_bar(stat="identity") + \
        geom_text(aes(label='round(freq, 2)'), va='top', nudge_y=0.01) + \
        ggtitle('Goal differential distribution')
    print(g)

def plot_features_corr(data: pd.DataFrame):
    """Plot data correlation.

    Data structure.
    ['user_id', 'id', 'label', 'cargo_type', 'handling_type', 'truck_count',
    'highway', 'lcl_cargo', 'truck_len_match', 'regular_subscribe_match',
    'line_match', 'line30_match', 'line60_match', 'scan_cargo_level_3',
    'scan_cargo_level_7', 'scan_cargo_level_14', 'scan_cargo_level_30',
    'click_cargo_level_3', 'click_cargo_level_7', 'click_cargo_level_14',
    'click_cargo_level_30', 'call_cargo_level_3', 'call_cargo_level_7',
    'call_cargo_level_14', 'call_cargo_level_30]
    """
    data = data.drop(columns=['user_id', 'id'])
    # Compute the correlation matrix
    corr = data.corr()

    # Generate a mask for the upper triangle
    mask = np.zeros_like(corr, dtype=np.bool)
    mask[np.triu_indices_from(mask)] = True

    # Set up the matplotlib figure
    plt.subplots(figsize=(11, 9))

    # Generate a custom diverging colormap
    cmap = sns.diverging_palette(220, 10, as_cmap=True)

    # Draw the heatmap with the mask and correct aspect ratio
    sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
                square=True, linewidths=.5, cbar_kws={"shrink": .5})

    plt.show()

def plot_scatters(data: list, x_title, y_title, title=None):
    data = pd.DataFrame({x_title: data.index, y_title: data})
    g = ggplot(mapping=aes(x=x_title, y=y_title), data=data) + \
        geom_point(shape='o', size=2, fill='', color='black') + \
        ggtitle(title)
    print(g)

def plot_wins_per_team(sim_results: pd.DataFrame):
    new_data = sim_results.groupby(by='winner').size().reset_index(name='wins')
    new_data = new_data.sort_values(by='wins', ascending=False)
    new_data = new_data.reset_index(drop=True)

    new_data['winner'] = pd.Categorical(new_data['winner'], categories=reversed(new_data.winner))

    g = ggplot(mapping=aes(x='winner', y='wins'), data=new_data) + \
        geom_bar(stat="identity", ) + \
        coord_flip() + \
        geom_text(aes(label="wins/100", va=0.3, ha=0.5, size=3)) + \
        ggtitle("Tournament simulation winners (10,000 iterations)")
    print(g)

if __name__ == '__main__':
    preprocess_dir = os.path.join('data', 'preprocess')

    train_features = pd.read_csv(os.path.join(preprocess_dir, 'train_features.csv'))
    plot_features_corr(train_features)


from django.core.management.base import BaseCommand

from foodgram.models import Ingredient
import csv
from pathlib import Path
import logging

logging.basicConfig(filename='default.log')

class Command(BaseCommand):
    help = 'Load data from csv'

    def handle(self, *args, **options):
        path_to_dir = f'{Path(__file__).resolve().parent.parent.parent}'
        final_path = path_to_dir + '/data/ingredients.csv'
        with open(final_path) as csv_file:
            file_reader = csv.reader(csv_file)
            for row in file_reader:
                title, unit = row
                Ingredient.objects.get_or_create(title=title, unit=unit)
        logging.info(f'Добавлено {Ingredient.objects.count()} игредиентов')


import RPi.GPIO as GPIO
import time

ledPin = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(ledPin, GPIO.OUT)

p = GPIO.PWM(ledPin, 100)
duty = 0 #(1~100)
p.start(duty)

try :
    # GPIO.output(ledPin, True)
    while duty < 100:
        duty += 1
        p.ChangeDutyCycle(duty)
        print("duty : " + str(duty))
        time.sleep(0.1)
    p.stop()
finally :
    GPIO.cleanup()


'''
Author: your name
Date: 2021-10-05 22:17:39
LastEditTime: 2021-10-07 01:47:21
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \show_trajectory\video2seq.py
'''
import multiprocessing as mp
import time
import cv2
import os

def Video2Image(videoFile, outputFile):
    vc = cv2.VideoCapture(videoFile)
    if vc.isOpened():
        rval, frame = vc.read()
    else:
        print('openerror!')
        rval = False

    frame_id = 1
    while rval:
        rval, frame = vc.read()
        frame_name = outputFile + str(frame_id).zfill(6) + '.jpg'
        if frame is None:
            print(frame_id)
            continue
        cv2.imwrite(frame_name, frame)
        frame_id += 1

    vc.release()

if __name__ == "__main__":
    video_dir = "figures/video"
    output_dir = "figures/trajectory"

    for seq_name in os.listdir(video_dir):
        st = time.time()
        name = os.path.splitext(seq_name)[0]
        videoFile = os.path.join(video_dir, seq_name)
        outputFile = os.path.join(output_dir, name+"/")
        if not os.path.exists(outputFile):
            os.makedirs(outputFile, exist_ok=True)

        Video2Image(videoFile, outputFile)

        print("[%.2f sec] Processed %s." % (time.time()-st, name))


import os
from flask import Flask, Blueprint,request, render_template, redirect, url_for
from werkzeug.utils import secure_filename
from blueprints import *
import cv2
import base64
import sql
import tensorflow as tf
import pathlib as p

model = tf.keras.models.load_model('models/3_96_25k.h5')

upload_api = Blueprint('upload_api', __name__)

@upload_api.route('/uploader', methods = ['GET', 'POST'])
def upload_file():
   if request.method == 'POST':
      try:
        f = request.files['file']
        f.save(os.path.join(str(p.Path.cwd() /'static'/'images'),secure_filename(f.filename)))
        preprocess_img = sql.load_and_preprocess_image(os.path.join(str(p.Path.cwd() /'static'/'images' / str(f.filename))))
        preprocess_img = tf.reshape(preprocess_img,[1,192,192,3])
        predict = model.predict(preprocess_img)
        d = {0: 'cat',
                1: 'dog'}
        predict = d[predict.argmax()]
        return render_template('predict.html', img=f.filename, predict = predict)
      except:
        data = request.json
        print('1' not in data)
        img_name = data.split(" ")[0]
        path = os.path.join(str(p.Path.cwd() /'static'/'images' / img_name))
        if '1' not in data:
          label = {"cat":1,"dog":0}.get(data.split(" ")[1])
          sql.save_into_db(path,label)
        os.remove(path)
        return render_template('predict.html', img="", predict = "")
if __name__ == '__main__':
   app.run(debug = True)


from collections import Counter
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
import pandas as pd
import random
import warnings

style.use('fivethirtyeight')

def k_nearest_neighbours(data, predict, k=3):
    """
    :param data: Dictionary of labels and set of features for each label
    :param predict: The feature for we need to classify
    :param k: The number of neighbours
    :return: The classified label
    """
    if len(data) >= k:  # If number of labels is >= number of neighbours to test
        warnings.warn('K is set to a value less than total voting groups!')  # We haven't tested for all possible labels
    distances = []
    for label in data:
        for featureList in data[label]:
            # The below line gives most intuitive way for what we are calculating with data and test point (predict)
            # euclidean_distance = sqrt( (feature[0]-predict[0])**2 + (feature[1]-predict[1])**2 )
            euclidean_distance = np.linalg.norm(np.array(featureList) - np.array(predict))  # Built-in fast version of above
            distances.append((euclidean_distance, label))

    # Sort all distances and choose top k elements, get those labels (tup[1])
    votes = [tup[1] for tup in sorted(distances)[:k]]
    vote_result = Counter(votes).most_common(1)[0][0]
    '''
    Counter.most_common: Given a list of values, the Counter class analyses it to produce a list of tuples.
    Each tuple consists of (value, count) from the list.
    most_common() takes an argument n and returns the list of top n tuples. If n < length of analysed data, returns
    available data.
    '''
    return vote_result

'''Trial dataset to test K nearest neighbuours with custom data'''
dataset = {    # The keys represent the labels of classification and sub-list represnets list of features for that label
    'k': [[1, 2], [2, 3], [3, 1]],
    'r': [[6, 5], [7, 7], [8, 6]]
    }
# for label in dataset:   # To show dataset
#     for feature in dataset[label]:
#         plt.scatter(feature[0], feature[1], s=30, color=label)      # s argument is size of points
# plt.show()

# new_feature = [5, 7]    # The new feature we want to classify and attach a label from existing label in dataset
# result = k_nearest_neighbours(dataset, new_feature, k=3)
# print("Classification of feature: label ", result)

'''Testing our K nearest neighbours algorithm with the breast cancer data to compare with sci-kit learn'''
df = pd.read_csv('breast-cancer-wisconsin.txt')
df.replace('?', -99999, inplace=True)
df.drop(['id'], 1, inplace=True)
# Convert all data to float, full_data is list of lists (each is a row of data from input file, labels and features)
full_data = df.astype(float).values.tolist()
random.shuffle(full_data)   # Shuffles order of lists and stores in same variable

test_size = 0.2
train_set = {2: [], 4: []}
test_set = {2: [], 4: []}
train_data = full_data[:-int(test_size * len(full_data))]    # Split the data, first 80% train
test_data = full_data[-int(test_size * len(full_data)):]     # Split the data, last 20% train

# Populate the train_set and test_set from train_data and set_data
for dataRow in train_data:
    label = dataRow[-1]  # Get label attached to each row of data and use this as key in training dictionary
    train_set[label].append(dataRow[:-1])   # Append row of data without label to training dictionary

# Do same as above for train_set
[test_set[dataRow[-1]].append(dataRow[:-1]) for dataRow in test_data]

correct = 0
total = 0
for label in test_set:
    for featureList in test_set[label]:
        # The train_set is the bunch of features with attached labels and for each time we want to classify new features
        # we simply pass this as an argument. Training is just having labelled data points to compare test point to!
        vote = k_nearest_neighbours(train_set, featureList, k=5)
        if label == vote:
            correct += 1
        total += 1
print("Accuracy: ", correct / total)


import os
import joblib
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis

def countLabels(set):
    count = {}
    for label in set:
        if label in count:
            count[label] += 1
        else:
            count[label] = 1

    return count

file = open('version', 'r')
version = file.read()
print(version)

file = open('version', 'w')
numbers = version.split('.')
file.write(f'{numbers[0]}.{numbers[1]}.{int(numbers[2]) + 1}')

dataPath = '../balanced/classification_dataset.csv'
classifierPath = f'../Classifiers/{version}/'

if not os.path.exists(classifierPath):
    os.mkdir(classifierPath)

# read training file
file = open(dataPath, 'r')
data = file.read().split('\n')
dataset = []
labels = []
for row in data:
    xy, label = row.split(',')
    xy = xy[:-1]
    xy = xy.split(' ')

    coordinates = []
    for pair in xy:
        x, y = pair.split(':')
        coordinates.append(float(x))
        coordinates.append(float(y))
    dataset.append(coordinates)
    labels.append(label)

dataset = np.array(dataset).astype(np.float32)

X_train, X_test, y_train, y_test = train_test_split(dataset, labels, test_size=0.2, stratify=labels)

names = [
    "kNN",
    "SVM",
    "NN",
    "NB",
    "QDA",
]

classifiers = [
    KNeighborsClassifier(3),
    SVC(gamma=2, C=1),
    MLPClassifier(alpha=1, max_iter=1000),
    GaussianNB(),
    QuadraticDiscriminantAnalysis(),
]

for i in range(len(classifiers)):
    classifier = classifiers[i]
    classifier.fit(X_train, y_train)
    score = classifier.score(X_test, y_test)
    print(f'{names[i]} : {score}')
    joblib.dump(classifier, f'{classifierPath}{names[i]}' + version + '.pkl')


from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from .models import Tour, Category, Types, Comment, Entry, Like

class CommentSerializer(serializers.ModelSerializer):
    user = serializers.CharField(source="user.username")
    tour = serializers.CharField(source="tour.title")

    class Meta:
        model = Comment
        fields = ('id', 'text', 'image', 'user', 'tour')

class TourSerializer(serializers.ModelSerializer):
    category = serializers.CharField(source="category.title")
    types = serializers.CharField(source="types.title")
    comments = CommentSerializer(many=True)

    class Meta:
        model = Tour
        fields = ('id', 'category', 'types', 'title', 'image', 'price', 'start_day', 'end_day', 'comments',
                  'likes_count')

        def get(self, request):
            serializer = TourSerializer(Tour.objects.all(), many=True)

class CategorySerializer(serializers.ModelSerializer):
    tours = serializers.SerializerMethodField()

    def get_tours(self, obj):
        return TourSerializer(obj.tours.all(), many=True).data

    class Meta:
        model = Category
        fields = ('id', 'title', 'tours')

class TypesSerializer(serializers.ModelSerializer):
    tours = serializers.SerializerMethodField()

    def get_tours(self, obj):
        return TourSerializer(obj.tours.all(), many=True).data

    class Meta:
        model = Types
        fields = ('id', 'title', 'tours')

class TourCrudSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tour
        fields = "__all__"

class EntrySerializer(serializers.ModelSerializer):
    """Запись на тур,Если мест нету выдаст ошибку"""

    def validate(self, attrs):
        print(attrs)
        quantity = attrs['tour'].quantity_of_seats
        quantity2 = Entry.objects.filter(tour=attrs['tour']).count()
        if quantity2 >= quantity:
            raise ValidationError(
                {
                    'quantity_of_seats': 'Извините, Все места забронированы!'
                                         'Вы можете записаться на другую дату!'
                }
            )
        return attrs

    class Meta:
        model = Entry
        fields = "__all__"

class DeleteCommentSerializer(serializers.ModelSerializer):
    '''Удаление коментариев Под Турами.
    Коментарий может удалить только тот человек кто его написал'''

    class Meta:
        model = Comment
        fields = ('id', 'user')

class LikeSerializer(serializers.ModelSerializer):
    likes_count = serializers.IntegerField(read_only=True)

    def to_representation(self, instance):
        data = super().to_representation(instance)
        if isinstance(instance, Like):
            data['likes_count'] = instance.like
        elif isinstance(instance, Tour):
            likes = Like.objects.filter(tour=instance)
            data['likes_count'] = likes.count()
        return data

    class Meta:
        model = Like
        fields = ('id', 'like', 'user', 'tour', 'likes_count')

class LikeDeleteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Like
        fields = ('id', 'user')


import numpy as np
import scipy as sp
from scipy import optimize
from scipy import stats
import matplotlib.pyplot as plt

def dilser(low=0.001, limit=100., dilfactor=2.):
    '''returns a numpy array dilution series from low to limit by dilfactor'''
    #replace this with a generator/iterator someday
    a = [low]
    while a[-1] <= limit:
        a.append(a[len(a)-1]*dilfactor)
    return np.array(a)

def k1mod(params,ligs,noise=None):
    '''
    returns tuple of ligand concentrations, saturation, params

    params is array of binding constants
    ligs is array of ligand concentrations
    noise is relative percent of gaussian noise (e.g., 0.0-1.) applied to data
    '''

    k1 = params[0]
    sat = (k1*ligs)/(1 + k1*ligs)

    if noise is None:
        return (ligs,sat)
    else:
        noised = np.random.normal(sat,noise*sat)
        return (ligs,noised,params)

def k2mod(params,ligs,noise=None):
    '''
    returns tuple of ligand concentrations, saturation, params

    params is array of binding constants
    ligs is array of ligand concentrations
    noise is relative percent of gaussian noise (e.g., 0.0-1.) applied to data
    '''

    k1 = params[0]
    k2 = params[1]
    sat = (1/2)*((k1*ligs + 2*k1*k2*(ligs**2))/(1 + k1*ligs + k1*k2*(ligs**2)))

    if noise is None:
        return (ligs,sat)
    else:
        noised = np.random.normal(sat,noise*sat)
        return (ligs,noised,params)

def k2mod_fix(params,ligs,noise=None):
    '''
    returns tuple of ligand concentrations, saturation, params

    params is array of binding constants
    ligs is array of ligand concentrations
    noise is relative percent of gaussian noise (e.g., 0.0-1.) applied to data
    '''

    k1 = params[0]
    k2 = params[0]/4
    sat = (1/2)*((k1*ligs + 2*k1*k2*(ligs**2))/(1 + k1*ligs + k1*k2*(ligs**2)))

    if noise is None:
        return (ligs,sat)
    else:
        noised = np.random.normal(sat,noise*sat)
        return (ligs,noised,params)

def obj(guess,model,data):
    return model(guess,data[0])[1] - data[1]

def fit(data,model,guess):
        return optimize.least_squares(obj,guess,args=(model,data),ftol=1e-14,gtol=1e-14,xtol=1e-14)

def ftest(fit1,fit2):
    '''
    fit1 is fit from simpler model
    '''

    SS1 = np.sum(np.square(fit1.fun))
    SS2 = np.sum(np.square(fit2.fun))

    DF1 = fit1.fun.shape[0] - fit1.x.shape[0]
    DF2 = fit2.fun.shape[0] - fit2.x.shape[0]

    F = ((SS1-SS2)/(DF1-DF2))/(SS2/DF2)

    p = stats.f.sf(F,(DF1-DF2),DF2)

    return (SS1,SS2,DF1,DF2,F,p)


"""
A custom OAuth2 application that allows wildcard for redirect_uris

https://github.com/jazzband/django-oauth-toolkit/issues/443#issuecomment-420255286
"""
import re
from urllib.parse import parse_qsl
from urllib.parse import urlparse

from django.core.exceptions import ValidationError

from oauth2_provider.models import AbstractApplication
from oauth2_provider.settings import oauth2_settings

def validate_uris(value):
    """ Ensure that `value` contains valid blank-separated URIs. """
    urls = value.split()
    for url in urls:
        obj = urlparse(url)
        if obj.fragment:
            raise ValidationError('Redirect URIs must not contain fragments')
        if obj.scheme.lower() not in oauth2_settings.ALLOWED_REDIRECT_URI_SCHEMES:
            raise ValidationError('Redirect URI scheme is not allowed.')
        if not obj.netloc:
            raise ValidationError('Redirect URI must contain a domain.')

class Application(AbstractApplication):
    """ Subclass of application to allow for regular expressions for the redirect uri. """

    @staticmethod
    def _uri_is_allowed(allowed_uri, uri):
        """ Check that the URI conforms to these rules. """
        schemes_match = allowed_uri.scheme == uri.scheme
        netloc_matches_pattern = re.fullmatch(allowed_uri.netloc, uri.netloc)
        paths_match = allowed_uri.path == uri.path

        return all([schemes_match, netloc_matches_pattern, paths_match])

    def __init__(self, *args, **kwargs):
        """ Relax the validator to allow for uris with regular expressions. """
        self._meta.get_field('redirect_uris').validators = [validate_uris, ]
        super(). __init__(*args, **kwargs)

    def redirect_uri_allowed(self, uri):
        """
        Check if given url is one of the items in :attr:`redirect_uris` string.
        A Redirect uri domain may be a regular expression e.g. `^(.*).example.com$` will
        match all subdomains of example.com.
        A Redirect uri may be `https://(.*).example.com/some/path/?q=x`
        :param uri: Url to check
        """
        for allowed_uri in self.redirect_uris.split():
            parsed_allowed_uri = urlparse(allowed_uri)
            parsed_uri = urlparse(uri)

            if self._uri_is_allowed(parsed_allowed_uri, parsed_uri):
                aqs_set = set(parse_qsl(parsed_allowed_uri.query))
                uqs_set = set(parse_qsl(parsed_uri.query))

                if aqs_set.issubset(uqs_set):
                    return True

        return False

    class Meta:
        db_table = 'oauth2_provider_application'


import TranslationAndCleaning
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
from statsmodels.formula.api import ols

data = TranslationAndCleaning.data.reset_index()

# ------------------------
# Define used column names
# ------------------------
cluster_vars = ['person', 'household_id', 'home_id', 'conglomerado', 'city']

indep_vars = ['sex', 'age', 'income_pc', 'poverty', 'has_received_free_school_uniform']
dep_vars = ['level_of_education']

# Is this a numerical proxy for level of education? Perhaps better to use the level directly.

# ---------------------------------------
# Study Completeness for Different Models
# ---------------------------------------

# Count per-variable completeness.
#  Note: level_of_education is 92% complete, years of schooling only 47.6%.
#  Note: has_received_free_school_uniform is 11.48% complete.
#  Note: All other variables are >99% complete.
#  Note: Missingness in income_pc seems to correspond precisely with missingness in poverty.
total_entries = data.shape[0]
for var in dep_vars + indep_vars + ['level_of_education']:
    complete_entries = data[var].dropna().shape[0]
    print("missing values for " + var, ":", total_entries - complete_entries, "/", total_entries, "|| Completeness",
          100 * complete_entries / total_entries, "%")

# Count completeness by complete series using these data
model_1_vars = dep_vars + indep_vars
print("Completeness for " + ", ".join(model_1_vars) + ":",
      1 - data[model_1_vars].notnull().all(axis=1).value_counts()[False] / total_entries)
# 0% complete.

model_2_vars = ['level_of_education'] + indep_vars
print("Completeness for " + ", ".join(model_2_vars) + ":",
      1 - data[model_2_vars].notnull().all(axis=1).value_counts()[False] / total_entries)
# Exchange years of schooling for level of education: 11% complete.

model_3_vars = ['level_of_education'] + indep_vars
model_3_vars.remove('has_received_free_school_uniform')
print("Completeness for " + ", ".join(model_3_vars) + ":",
      1 - data[model_3_vars].notnull().all(axis=1).value_counts()[False] / total_entries)
# Exchange years of schooling for level of education & remove has_received_free_school_uniform: 91% complete.
# Completeness is 99% without level of education\.

model_4_vars = ['level_of_education'] + indep_vars
model_4_vars.remove('has_received_free_school_uniform')
model_4_assumptions = data['age'] > 4
print("Completeness for " + ", ".join(model_4_vars) + ":",
      1 - data.loc[model_4_assumptions, model_4_vars].notnull().all(axis=1).value_counts()[False] / total_entries)
print("Assumption: age > 4")

total_entries_filt = data.loc[model_4_assumptions, :].shape[0]
for var in model_4_vars:
    complete_entries = data.loc[model_4_assumptions, var].dropna().shape[0]
    print("missing values for " + var, ":", total_entries_filt - complete_entries, "/", total_entries_filt,
          "|| Completeness", 100 * complete_entries / total_entries_filt, "%")
# 0% is missing in level of education, sex, and age.
# 1.11% is missing in income_pc, poverty

# -----------------------------------------
# Study Completeness for Level of Education
# -----------------------------------------
data['missing_level_of_education'] = [1 if pd.isnull(x) else 0 for x in data['level_of_education']]

X = pd.get_dummies(data=data[['sex', 'age', 'income_pc', 'poverty']], drop_first=True).dropna()
y = data.iloc[X.index]['missing_level_of_education']

for i in range(0, X.shape[1]):
    plt.scatter(X.iloc[:, i], y)
    plt.xlabel(X.columns[i])
    plt.show()
# Age almost fully separates the data, with no missing values existing for ages above 8 years old or so.
# Note: this way of plotting does not work for sex and poverty. Need a value count. See below.

print(data[data['missing_level_of_education'] == 1]['age'].value_counts())
age_for_non_missing_data = data[data['missing_level_of_education'] == 0]['age']
print(min(age_for_non_missing_data), max(age_for_non_missing_data))
# Level of education is missing only for ages 0-4, and exists for above 5.

data[data['age'] > 4].missing_level_of_education.value_counts()
# Conclusion: When filtering for ages above 4 all missing data points are removed.

# -----------------------------------------------------
# Study Amount of Missingness for poverty and income_pc
# -----------------------------------------------------
total_measurements_per_year = data.iloc[data.index[data['person'].notnull()]]['year'].value_counts().reset_index()

data['has_received_human_dev_bond'].notnull().value_counts()
data['poverty'].notnull().value_counts()

missing_pov_idx = data.index[data['poverty'].isnull()]
missing_inc_idx = data.index[data['income_pc'].isnull()]
present_inc_idx = data.index[data['income_pc'].notnull()]

missing_pov_idx.equals(missing_inc_idx)
# Returns True: Precisely the same values are missing here. Strange.
# TODO: What does it mean for our model results that missingness in poverty and income_pc is perfectly correlated?

pov_count_per_year = data[data['poverty'] == 'pobre']['year'].value_counts().reset_index()
missing_per_year = data[data['poverty'].isnull()]['year'].value_counts().reset_index()
missing_per_year = (missing_per_year.merge(total_measurements_per_year, on='index')
                    .merge(pov_count_per_year, on='index')
                    .rename({'year': 'count_pov', 'index': 'year', 'year_x': 'count_missing', 'year_y': 'count_full'},
                            axis=1)
                    .sort_values(by='year'))
missing_per_year['percentage_miss'] = missing_per_year['count_missing'] / missing_per_year['count_full']
missing_per_year['percentage_pov'] = missing_per_year['count_pov'] / missing_per_year['count_full']
missing_per_year['factor'] = missing_per_year['percentage_miss'] / missing_per_year['percentage_pov']
# Summary of created tables:
# Missingness fluctuates from 1.9% to 0.05% per year for this variable. If removing the data does not induce significant
# bias, it is probably safe to remove it: In no single year is the percentage of values missing more than 5% of the
# amount of people self-reporting poverty. It is unlikely we are removing important dynamics.

# ------------------------------------------------------------------------------------------
# Study Type of Missingness for poverty and income_pc (e.g. at random, completely at random)
# ------------------------------------------------------------------------------------------

data['poverty_num'] = pd.factorize(data['poverty'])[0]
data[['poverty_num', 'income_pc']].corr(method='spearman')
# Poverty and income_pc are very strongly correlated; -0.8.

data['missing_poverty'] = [1 if pd.isnull(x) else 0 for x in data['poverty']]

X = pd.get_dummies(data=data[['sex', 'age']], drop_first=True)
y = data['missing_poverty']

for i in range(0, X.shape[1]):
    plt.scatter(X.iloc[:, i], y)
    plt.xlabel(X.columns[i])
    plt.show()
# On first sight it appears evenly distributed.

missingness_by_level_of_ed = data.loc[data['missing_poverty'] == 1, 'level_of_education'].value_counts()
missingness_pct_by_level_of_ed = (data.loc[data['missing_poverty'] == 1, 'level_of_education']
                                  .value_counts().div(data['level_of_education'].value_counts()))
missingness_by_level_of_ed.plot(kind='bar')
plt.show()
missingness_pct_by_level_of_ed.plot(kind='bar')
plt.show()
# More values are missing in the bracket for higher educated people, i.e. post-grado and universitario.

X = pd.get_dummies(data.loc[model_4_assumptions, 'level_of_education'], drop_first=True)
y = data.loc[model_4_assumptions, 'missing_poverty']
model = sm.OLS(y, sm.add_constant(X)).fit()
print(model.summary())
# ANOVA shows that coefficients for post-grado, secundaria, and superior universitario are all statistically significant.
# Hence data missingness is related to our dependent variable. As such, it may even be related to income levels.

model_for_anova = ols(formula='missing_poverty ~ level_of_education', data=data[model_4_assumptions]).fit()
sm.stats.anova_lm(model_for_anova)
# Corroborated by statistical test: F-test says we can reject the null hypothesis that all means are the same.
# --> Data is definitely not MCAR, and unlikely to be MAR, since level of education is weakly correlated to income.

data.loc[model_4_assumptions, ['income_pc', 'level_of_education_num']].corr(method='spearman')
# corr is .23

# Conclusion: To prevent any bias data should be imputed with some model.
# TODO: Create data imputation model.

# ------------------------------------------------
# Study Possible Proxies for poverty and income_pc
# ------------------------------------------------
missing_pov_idx = data.index[data['poverty'].isnull()]

income_cols = ['income_self_employed', 'income_wage_and_domestic', 'income_capital', 'income_capital_transactions',
               'income_retirement_pension', 'income_gift_donation', 'income_from_abroad', 'income_labour']
hh_income_cols = ['hh_income_total', 'hh_size', 'hh_income_pc']  # can impute based on hh wealth?
pov_proxy_1 = ['number_of_jobs']  # perhaps strongly related to poverty? where more jobs = less money per job = poorer?
pov_proxy_2 = ['hours_worked1', 'place_of_work']  # use in nn imputation?

def count_missing_values_for_selection(idx, df, mask, cols):
    for col in cols:
        print(df.iloc[idx].loc[mask, col].isnull().value_counts())

count_missing_values_for_selection(missing_pov_idx, data, model_4_assumptions, income_cols)
# wage and domestic covers all but 634 missing values!
# capital and capital transactions cover all but one missing value!

count_missing_values_for_selection(missing_pov_idx, data, model_4_assumptions, pov_proxy_1)
# covers all but 446 values.

count_missing_values_for_selection(missing_pov_idx, data, model_4_assumptions, pov_proxy_2)
# cover a bit more than half of missing values; 5499

count_missing_values_for_selection(missing_pov_idx, data, model_4_assumptions, hh_income_cols)
# hh_inc_total and hh_income_pc
# present for all values! Size missing, though.

data['income_tot'] = data[income_cols].sum(axis=1)
data['income_pct_change'] = (data['income_pc'] - data['income_tot']) / data['income_pc']
(data.loc[(-3 < data['income_pct_change']) & (data['income_pct_change'] < 101), 'income_pct_change']
 .value_counts(bins=50)
 .sort_index(ascending=True))
(data.loc[(-101 < data['income_pct_change']) & (data['income_pct_change'] < 101), 'income_pct_change']
 .value_counts()
 .sum())
# For 615k entries the change is almost 100%; i.e. income_tot is near 0 for these entries. Changes are around 0% for only 22k
# entries: Very big discrepancies exist.

# TODO: Contact statistical office to ask why this discrepancy exists.
# TODO: Further decide on what proxy to use.

# Impute income data with total data. NOTE: This is far from perfect, but produces a complete dataset for now, until
# I can get some information from the producer of the dataset.
# So: Right now income_pc is leading, and imputed with the sum of incomes where necessary.
data['income_imp'] = data.loc[:, 'income_pc']
data.loc[missing_inc_idx, 'income_imp'] = data.loc[:, 'income_tot']

# Assert that no values are missing.
data['income_imp'].isnull().value_counts()[False] == len(data)

# Assert values have been imputed
data.loc[present_inc_idx, 'income_imp'].equals(data.loc[present_inc_idx, 'income_pc'])
data.loc[missing_inc_idx, 'income_imp'].equals(data.loc[missing_inc_idx, 'income_tot'])


from django.urls import path
from .views import PostsList, PostDetail, PostCreate, PostUpdate, PostDelete, PostSearch, NewsCreate, ArticleCreate, ArticlesPost, NewsPost 	# Импортируем созданные нами представления

urlpatterns = [
    path('', PostsList.as_view(), name='post_list'),   # ???либо name='news_list' либо name='post_list'??? В данном случае путь ко всем постам у нас останется пустым(urlpatterns = [path('cheburashka/', ListPost.as_view())] # так ничего не выводится на страницу)(???)
    path('<int:pk>', PostDetail.as_view(), name='post_detail'),
    path('create/', PostCreate.as_view(), name='post_create'),
    path('news/create/', NewsCreate.as_view(), name='news_create'),
    path('articles/create/', ArticleCreate.as_view(), name='articles_create'),
    path('<int:pk>/update/', PostUpdate.as_view(), name='post_update'),          #??????????????????????
    path('<int:pk>/edit/', PostUpdate.as_view(), name='post_edit'),              #??????????????????????
    path('<int:pk>/delete/', PostDelete.as_view(), name='post_delete'),
    path('search/', PostSearch.as_view(), name='post_search'),
]
	# path — означает путь.
	# Т.к. наше объявленное представление является классом, а Django ожидает функцию, нам надо представить этот класс в виде view.
	# Для этого вызываем метод as_view.
    # pk — это первичный ключ публикации, который будет выводиться у нас в шаблон
    # int — указывает на то, что принимаются только целочисленные значения


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random
import sys
from Bio import SeqIO

"""

Code here adapted from:

https://2-bitbio.com/2018/06/one-hot-encode-dna-sequence-using.html

Code to help read in and parse sequence formats specific to this assignment.

"""

#Fully display all rows in dataframe: optional
#pd.set_option('display.max_rows', None)

#Parse and return a specified sampling of sequences of length 17 from the negative
#fasta file.

def return_negs (path = './data/yeast-upstream-1k-negative.fa', number_to_return = 137, length = 17):
	#set a seed
	random.seed(1)
	#parse fasta file
	seqs = SeqIO.parse(path, "fasta")
	full_negs = []
	#For each sequence, append the raw DNA sequence to a list
	for seq in seqs:
		full_negs.append(seq.seq)
	sampled_negs = []
	#Sample one of these DNA sequences from the full list
	for i in range(number_to_return):
		sequence = str(random.sample(full_negs, 1)[0])
		end = len(sequence)
		#In the range of the sequence length, choose to start somewhere anywhere
		#in the sequence (assumption: it's ALL negative )
		begin = random.randrange(1, end - length)
		to = begin + length
		#Subsequence is begin, to begin + length
		subseq = sequence[begin:to]
		#append sampled subsequence and then return the full list of sampled negs
		sampled_negs.append(subseq)
	return sampled_negs

#return seqs from a text file of DNA sequences
def return_seqs (path = './data/rap1-lieb-positives.txt'):
	with open(path) as f:
		#Split and strip off new lines
		split_file = f.readlines()
		seqs = [i.strip() for i in split_file]
	return seqs

#Return a balanced sample of data. Always will have 137 positive sequences, and
#however many specified negative sequences.
def return_training_data (length = 137):
	test_negs = return_negs()
	test_pos = return_seqs()
	#Label vectors
	neg_df = pd.DataFrame({'seq': test_negs, 'class': ['0'] * length})
	pos_df = pd.DataFrame({'seq': test_pos, 'class': ['1'] * length})
	#concat data and return as dataframe
	full_data = pd.concat ([neg_df, pos_df])
	return full_data

#one-hot encoding logic: given a dictionary, return array of values (vectors of 0/1)
def one_hot_encode (string, dictionary):
	new_list = []
	#Each nucleotide gets a different encoding
	for nucl in string:
		value = dictionary[nucl]
		new_list.append(value)
	#Return an array with shape 17 * 4
	return np.array(new_list)

#unpack an input string by calling above 'one_hot_encode', and flattens list
def unpack (input, dict):
	input = one_hot_encode(input, dict)
	#List flattening
	input = [item for sublist in input for item in sublist]
	return input

#Helper function to plot mostly error vs epochs plots. Weird '1.02' limit is
#so to help see CV accuracies of 1
def plot_utility (keys, values, name, title, ylab, xticks, ylim = (0, 1.02)):
	plt.plot(keys, values, linewidth = 3)
	plt.xticks(rotation=90)
	#Setting xticks so that I don't get a mess on the x-axis of each epoch
	plt.xticks(xticks)
	plt.title(title)
	plt.ylim(ylim)
	plt.ylabel(ylab)
	plt.savefig(name)
	plt.clf()


#!C:\Python\Python

def getDepth(currPage, options, depth):
    if isinstance(options, list):
        depth += 1
        for i in options:
            if i not in dataTree:
                dataTree[i] = None
            getDepth(i, data[i-1], depth)
    else:
        print(currPage, " is an ending page!")
        print("depth is: ", depth)
        if currPage not in dataTree:
            dataTree[currPage] = depth
        else:
            lastPath = dataTree[currPage]
            if lastPath is None or lastPath > depth:
                dataTree[currPage] = depth

#==================================
# Data Init
#==================================
length = int(input())
data = list()
for i in range(length):
    inputStr = input()
    currData = list(map(int, inputStr.split(" ")))
    if len(currData) >  1:
        data.append(currData[1:])
    else:
        data.append(currData[0])

print(data)

#==================================
# Calculating...
#==================================
dataTree = dict()
if isinstance(data[0], list):
    dataTree[1] = None
    getDepth(1, data[0], 1)
else:
    dataTree[1] = 1
    if length == 1:
        print("Y")
    else:
        print("N")
    print("1")
    exit()

print(dataTree)


import turtle
import math
from math import pi
sc=turtle.Screen()
sk=turtle.Turtle()
sk.speed(50)
sk.pensize(3)

side = 3
radius = int(turtle.numinput('title', 'Radius Lenght'))
size = int(turtle.numinput('title', 'Polygon size'))
# color = ["brown", "green", "red"]
for i in range(3):
		# sk.pencolor(color[i])
		for j in range(side):
			x = radius*math.cos(2*pi/side * j)
			turtle.goto(x, y=None)
		side += 3
turtle.done()
sc.mainloop()


import sys
import os

try:
    import pytest
except ImportError:
    fname = os.__file__
    if fname.endswith('.pyc'):
        fname = fname[:-1]

    if not os.path.islink(fname):
        raise

    real_prefix = os.path.dirname(os.path.realpath(fname))
    site_packages = os.path.join(real_prefix, 'site-packages')
    old_path = sys.path
    sys.path = old_path + [site_packages]
    try:
        import pytest
    finally:
        sys.path = old_path

class Collector(object):
    def __init__(self, send):
        self.send = send

    def extract_trace(self, excinfo):
        """:type excinfo: py._code.code.ReprExceptionInfo"""

        result = []
        for entry in excinfo.reprtraceback.reprentries:
            result.append((entry.reprfileloc.path, entry.reprfileloc.lineno))

        return result

    def extract_output(self, report):
        result = str(report.longrepr)
        for s, d in report.sections:
            result += '\n\n===================== ' + s + '=======================\n' + d

        return result

    def pytest_runtest_logreport(self, report):
        """:type report: _pytest.runner.TestReport()"""

        if report.when != 'call':
            return

        if report.passed:
            self.send(('PASS', report.nodeid))
        elif report.failed:
            if report.when != "call":
                self.send(('ERROR', report.nodeid, self.extract_output(report),
                    self.extract_trace(report.longrepr)))
            else:
                self.send(('FAIL', report.nodeid, self.extract_output(report),
                    self.extract_trace(report.longrepr)))
        elif report.skipped:
            self.send(('SKIP', report.nodeid))

    def pytest_runtest_call(self, item):
        self.send(('ITEM_CALL', item.nodeid))

    def pytest_collectreport(self, report):
        """:type report: _pytest.runner.CollectReport()"""
        if report.failed:
            self.send(('FAILED_COLLECT', report.nodeid, self.extract_output(report),
                self.extract_trace(report.longrepr)))

    def pytest_internalerror(self, excrepr):
        self.send(('INTERNAL_ERROR', excrepr))

    def pytest_sessionstart(self, session):
        self.send(('START', str(session.fspath)))

    def pytest_sessionfinish(self, session, exitstatus):
        self.send(('END', ))

    def pytest_collection_finish(self, session):
        self.send(('COLLECTED_TESTS', [t.nodeid for t in session.items]))

#class Collector(object):
#    def __init__(self, *args, **kwargs):
#        self.f = open('/tmp/result.txt', 'w')
#
#    def __getattr__(self, name):
#        if not name.startswith('pytest_'):
#            raise AttributeError(name)
#
#        from _pytest import hookspec
#        import inspect
#
#        space = {'f':self.f}
#        exec 'def {0}{1}:\n    print >>f, "{0}{1}", locals()'.format(
#            name, inspect.formatargspec(*inspect.getargspec(getattr(hookspec, name)))) in space
#
#        return space[name]

if __name__ == '__main__':
    from multiprocessing.connection import Listener
    listener = Listener(sys.argv[1])
    conn = listener.accept()

    if sys.version_info[0] == 3:
        from pickle import dumps
        def sender(data):
            return conn.send_bytes(dumps(data, 2))
    else:
        sender = conn.send

    pytest.main(sys.argv[2:], plugins=[Collector(sender)])


from os import system

class Board():
    def __init__(self):
        self.board = [[" "," "," "] for i in range(3)]

    def add_move(self,row,column,player_symbol):
        try:
            self.row = int(row)
            self.column = int(column)
            self.player_symbol = player_symbol.upper()
        except ValueError:
            print("Please enter an integer for row and/or column")
            return
        if self.row > len(self.board) - 1 or self.column > len(self.board) - 1:
            print("Please enter an integer between 0 and 2")
            return
        if self.board[self.row][self.column] != " ":
            print("Move already selected, please pick an open space")
            return
        self.board[self.row][self.column] = self.player_symbol
        return

    def draw_board(self):
        horz_line = "-" * 16
        output = ["","",""]
        for index_row, row in enumerate(self.board):
            for index_square, square in enumerate(row):
                if index_square != 0:
                    output[index_row] += "|"
                output[index_row] += f"  {square}  "
        output.append(horz_line)
        output = [output[0],output[3],output[1],output[3],output[2]]
        system("cls")
        for row in output:
            print(row)

    def check_winner(self):
        for index_row in range(3):
            place_holder = self.board[index_row][0]
            if place_holder == " ":
                continue
            for index_column in range(3):
                if self.board[index_row][index_column] != place_holder:
                    break
                if index_column == 2:
                    return True, place_holder

        for index_column in range(3):
            place_holder = self.board[0][index_column]
            if place_holder == " ":
                continue
            for index_row in range(3):
                if self.board[index_row][index_column] != place_holder:
                    break
                if index_row == 2:
                    return True, place_holder

        if self.board[0][0] == self.board[1][1] == self.board[2][2] != " ":
            return True, self.board[1][1]
        if self.board[0][2] == self.board[1][1] == self.board[2][0] != " ":
            return True, self.board[1][1]

        return False, " "

def create_board():
    a = Board()
    a.board[0] = ["X","O"," "]
    a.board[1] = ["O"," "," "]
    a.board[2] = ["O"," ","X"]
    return a


# flake8: noqa
# pylint: disable=invalid-name
# pylint: disable=line-too-long
# pylint: disable=missing-docstring
# pylint: disable=too-many-arguments
# pylint: disable=too-many-locals
# pylint: disable=too-many-statements
# pylint: disable=too-many-branches
"""
Find String
===============================================================================

Finds a string in the terms of a thesaurus.

>>> from techminer2.refine.words import find_string
>>> find_string(
...     #
...     # SEARCH PARAMS:
...     contains='ARTIFICIAL_INTELLIGENCE',
...     startswith=None,
...     endswith=None,
...     #
...     # DATABASE PARAMS:
...     root_dir="example/",
... )
--INFO-- The file data/regtech/words.txt has been reordered.

"""
import os.path
import re

import pandas as pd

from ...._common.thesaurus_lib import load_system_thesaurus_as_dict

THESAURUS_FILE = "words.txt"

def find_string(
    #
    # SEARCH PARAMS:
    contains=None,
    startswith=None,
    endswith=None,
    #
    # DATABASE PARAMS:
    root_dir="./",
):
    """
    :meta private:
    """

    th_file = os.path.join(root_dir, THESAURUS_FILE)
    if not os.path.isfile(th_file):
        raise FileNotFoundError(f"The file {th_file} does not exist.")

    th_dict = load_system_thesaurus_as_dict(th_file)

    reversed_th = {value: key for key, values in th_dict.items() for value in values}

    pdf = pd.DataFrame(
        {
            "text": reversed_th.keys(),
            "key": reversed_th.values(),
        }
    )

    result = []
    if contains is not None:
        if isinstance(contains, str):
            contains = [contains]
        for word in contains:
            #
            # startswith:
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile("^" + word + "_"),
                        regex=True,
                    )
                ]
            )
            #
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile("^" + word + r"\b"),
                        regex=True,
                    )
                ]
            )
            #
            # endswith:
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile("_" + word + "$"),
                        regex=True,
                    )
                ]
            )
            #
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile(r"\b" + word + "$"),
                        regex=True,
                    )
                ]
            )
            #
            # contains:
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile("_" + word + "_"),
                        regex=True,
                    )
                ]
            )
            #
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile(r"\b" + word + "_"),
                        regex=True,
                    )
                ]
            )
            #
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile("_" + word + r"\b"),
                        regex=True,
                    )
                ]
            )
            #
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile(r"\b" + word + r"\b"),
                        regex=True,
                    )
                ]
            )
            #
    elif startswith is not None:
        if isinstance(startswith, str):
            startswith = [startswith]
        for word in startswith:
            #
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile("^" + word + "_"),
                        regex=True,
                    )
                ]
            )
            #
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile("^" + word + r"\b"),
                        regex=True,
                    )
                ]
            )
            #
    elif endswith is not None:
        if isinstance(endswith, str):
            endswith = [endswith]
        for word in endswith:
            #
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile("_" + word + "$"),
                        regex=True,
                    )
                ]
            )
            #
            result.append(
                pdf[
                    pdf.text.str.contains(
                        re.compile(r"\b" + word + "$"),
                        regex=True,
                    )
                ]
            )
            #

    else:
        raise ValueError("No filter provided")
    pdf = pd.concat(result)

    keys = pdf.key.drop_duplicates()

    findings = {key: th_dict[key] for key in sorted(keys)}

    # reorder the thesaurus to reflect the search
    for key in findings.keys():
        th_dict.pop(key)

    with open(th_file, "w", encoding="utf-8") as file:
        for key in sorted(findings.keys()):
            file.write(key + "\n")
            for item in findings[key]:
                file.write("    " + item + "\n")

        for key in sorted(th_dict.keys()):
            file.write(key + "\n")
            for item in th_dict[key]:
                file.write("    " + item + "\n")

    print(f"--INFO-- The file {th_file} has been reordered.")

# def find_string(
#     #
#     # SEARCH PARAMS:
#     contains=None,
#     startswith=None,
#     endswith=None,
#     #
#     # DATABASE PARAMS:
#     root_dir="./",
# ):
#     """Find the specified keyword and reorder the thesaurus file.

#     :meta private:
#     """

#     return _find_string(
#         #
#         # THESAURUS FILE:
#         thesaurus_file=THESAURUS_FILE,
#         #
#         # SEARCH PARAMS:
#         contains=contains,
#         startswith=startswith,
#         endswith=endswith,
#         #
#         # DATABASE PARAMS:
#         root_dir=root_dir,
#     )


# This is a Rock, Paper, Scissors game done as part of the "100 Days of Code - The Complete Pyton Pro Bootcamp for 2021" course by Dr. Angela Yu.
# When writing code as part of courses I am taking, I only upload those excercises/projects I can fully understand and finish without checking the solutions.
# Martin Ignacio Lupa - 11/03/2021.

import random

rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''
game_images = [rock, paper, scissors]

user_choice = int(input("What do you choose? Type 0 for Rock, 1 for Paper and 2 for Scissors.\n"))
print(game_images[user_choice])

print("Computer chose:")
computer_choice = random.randint(0, 2)
print(game_images[computer_choice])

#ROCK, PAPER, SCISSORS RULES:

if user_choice == computer_choice:
  print("Draw.")
if user_choice == 0 and computer_choice == 1:
  print("You lose.")
if user_choice == 0 and computer_choice == 2:
  print("You win.")
if user_choice == 1 and computer_choice == 0:
  print("You win.")
if user_choice == 1 and computer_choice == 2:
  print("You lose.")
if user_choice == 2 and computer_choice == 0:
  print("You lose.")
if user_choice == 2 and computer_choice == 1:
  print("You win.")
else:
  print("You chose an invalid input. You lose!.")


from django.urls import path
from . import views

urlpatterns = [
    path('user_profile/',views.SetUserProfile,name='UserProfile'),
    path('get_services/',views.get_services,name='get_services'),
    path('me/',views.me,name='me'),
    path('add_handler/',views.addHandler,name='AddHandler'),
    path('remove_handler/',views.removeHandler,name='removeHandler'),
    path('events/<str:id>',views.UserEvents,name='UserEvents'),
    path('add_event/',views.addEvent,name='addEvent'),
    path('remove_event/',views.removeEvent,name='removeEvent'),
]


def make_average():
    count = 0
    total = 0

    def average(new_value):
        nonlocal count, total
        total += new_value
        count += 1
        return total / count

    return average

avg = make_average()
avg(10)
avg(11)
print(avg(12))

第七章/闭包/demo求平均数.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Yuande Liu <miraclecome (at) gmail.com>

import time
from collections import defaultdict

def delete_buy_sell_price_from_recorder(buy_price, buy_prices, sell_price, sell_prices, enobs):
    buy_key = int(buy_price * 10 ** (enobs - 1))
    buy_value = int(buy_price * 10 ** enobs)
    sell_key = int(sell_price * 10 ** (enobs - 1))
    sell_value = int(sell_price * 10 ** enobs)

    buy_prices[buy_key].remove(buy_value)
    sell_prices[sell_key].remove(sell_value)

def best_buy_sell_price_duplicate(buy_price, buy_prices, sell_price, sell_prices, enobs):
    """
    buy_prices: {186: [1861, 1865, 1868], 191: [1911, 1913]}
    """
    buy_key = int(buy_price * 10 ** (enobs - 1))
    buy_value = int(buy_price * 10 ** enobs)
    sell_key = int(sell_price * 10 ** (enobs - 1))
    sell_value = int(sell_price * 10 ** enobs)

    if buy_value in buy_prices[buy_key] or sell_value in sell_prices[sell_key]:
        return True
    else:
        buy_prices[buy_key].add(buy_value)
        sell_prices[sell_key].add(sell_value)
        return False

def parse_price(buy_sell_pair, buy_prices, sell_prices, enobs):
    # after filled or timeout
    remove_pair = []

    for k, v in buy_sell_pair.items():
        buy_price, sell_price = v
        if buy_price - 1.73 < 1e-5:
            pass
        else:
            remove_pair.append(k)
            break

    for i in remove_pair:
        buy_price, sell_price = buy_sell_pair.pop(i)
        delete_buy_sell_price_from_recorder(buy_price, buy_prices, sell_price, sell_prices, enobs)

        print('del: ', buy_price, buy_prices, sell_price, sell_prices)

def main(enobs=3):
    buy_prices = defaultdict(set)
    sell_prices = defaultdict(set)
    buy_sell_pair = defaultdict(tuple)

    prices = [1.732, 1.734, 1.735, 1.736, 1.732, 1.734, 1.735]
    for p in prices:
        parse_price(buy_sell_pair, buy_prices, sell_prices, enobs)

        buy_price = round(p - 2 * 10 ** -enobs, enobs)
        sell_price = round(p + 2 * 10 ** -enobs, enobs)

        if best_buy_sell_price_duplicate(buy_price, buy_prices, sell_price, sell_prices, enobs):
            print('continue: ', buy_price, buy_prices, sell_price, sell_prices)
            continue
        else:
            print('add: ', buy_price, buy_prices, sell_price, sell_prices)
            buy_sell_pair[int(time.time())] = (buy_price, sell_price)
            time.sleep(1)

main()


"""total_number_of_projects_added

Revision ID: c1e908241401
Revises: 879b99e7f212
Create Date: 2023-05-29 11:27:10.843043

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = "c1e908241401"
down_revision = "879b99e7f212"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("reporting", sa.Column("total_project_count", sa.Integer(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("reporting", "total_project_count")
    # ### end Alembic commands ###