from typing import Sequence, Dict, Union
import numpy as np

from .evaluator import Evaluator
from .labeled_tensor import LabeledTensor

class LabelMapEvaluator(Evaluator):
    """ Computes statistics related to volume and shape of the structures in a label map.

    A table with per-subject stats will be included in the output dictionary
    under the key ``'subject_stats'``. The table is a ``pd.DataFrame`` where the
    first column header is ``"subject_name"`` and the other columns headers
    are ``"stat_name.label_name"``.

    A dictionary containing summary statistics will be included in the output dictionary
    under the key ``'summary_stats'``. It has the following structure:
    ``{summary_stat_name: {stat_name: {label_name: value}}}``

    The label map must have a ``Dict[str, int]`` property ``'label_values'``,
    which maps a label's name to its value.

    The supported output statistics are the following:
    ``('volume')``

    Summary statistics for any of the above can also be computed and output from the evaluation
    The following are supported:
    ``('mean', 'median', 'mode', 'std', 'min', 'max')``

    Args:
        label_map_name: Key to a ``tio.LabelMap`` in each ``tio.Subject``.
        curve_params: Dictionary with keys for label_names and values that contain params for curve
        curve_attribute: Subject key attribute that is passed into the curve, an example is 'age'.
        stats_to_output: A sequence of statistic names that are output from the evaluation
        summary_stats_to_output: A sequence of summary statistic names that are output from the evaluation.
    """
    def __init__(
            self,
            label_map_name: str,
            curve_params: Union[Dict[str, np.ndarray], None] = None,
            curve_attribute: Union[str, None] = None,
            stats_to_output: Sequence[str] = ('volume',),
            summary_stats_to_output: Sequence[str] = ('mean', 'std', 'min', 'max'),
    ):
        self.label_map_name = label_map_name
        self.curve_params = curve_params
        self.curve_attribute = curve_attribute
        self.stats_to_output = stats_to_output
        self.summary_stats_to_output = summary_stats_to_output

        # arguments check
        curve_stats = ['error', 'absolute_error', 'squared_error', 'percent_diff']
        if any(stat in curve_stats for stat in self.stats_to_output):
            if curve_params is None:
                raise ValueError("curve_params must be provided")

            if curve_attribute is None:
                raise ValueError("curve_attribute must be provided")

        if curve_params is not None and curve_attribute is not None:
            self.poly_func = {label: np.poly1d(param) for label, param in curve_params.items()}
        else:
            self.poly_func = None

    def __call__(self, subjects):
        label_values = subjects[0][self.label_map_name]['label_values']
        label_names = list(label_values.keys())
        subject_names = [subject['name'] for subject in subjects]

        subject_stats = LabeledTensor(dim_names=['subject', 'label', 'stat'],
                                      dim_keys=[subject_names, label_names, self.stats_to_output])
        for subject in subjects:
            data = subject[self.label_map_name].data

            for label_name, label_value in label_values.items():
                label = (data == label_value)

                # Compute tensors for each statistic. Each element corresponds to one label.
                spatial_dims = (1, 2, 3)
                volume = label.sum(dim=spatial_dims)

                stats = {
                    'volume': volume,
                }

                if self.poly_func is not None:

                    curve_predicted = self.poly_func[label_name](subject[self.curve_attribute])
                    error = volume - curve_predicted
                    curve_stats = {
                        'error': error,
                        'absolute_error': abs(error),
                        'squared_error': error**2,
                        'percent_diff': (error / curve_predicted) * 100
                    }
                    stats.update(curve_stats)

                for stat_name in self.stats_to_output:
                    value = stats[stat_name].item()
                    subject_stats[subject['name'], label_name, stat_name] = value

        summary_stats = subject_stats.compute_summary_stats(self.summary_stats_to_output)
        out_dict = {
            'subject_stats': subject_stats.to_dataframe(),
            'summary_stats': summary_stats
        }

        return out_dict


from typing import Dict, Iterator, NewType, Tuple

FILEPATH = "data/data_05.txt"
OverlapedPoints = NewType('OverlapedPoints', Dict[Tuple[int, int], int])

def read_lines(filepath: str) -> Iterator:
    with open(filepath, 'r') as fp:
        for line in fp.readlines():
            start, end = line.split(' -> ')
            x1, y1 = [int(i) for i in start.split(',')]
            x2, y2 = [int(i) for i in end.split(',')]
            yield x1, y1, x2, y2

def add_points(matrix: OverlapedPoints, x1: int, y1: int, x2: int, y2: int) -> OverlapedPoints:
    if x1 != x2 and y1 == y2:
        for iter_x in range(min(x1, x2), max(x1, x2) + 1):
            matrix[(iter_x, y1)] = matrix.get((iter_x, y1), 0) + 1
    elif y1 != y2 and x1 == x2:
        for iter_y in range(min(y1, y2), max(y1, y2) + 1):
            matrix[(x1, iter_y)] = matrix.get((x1, iter_y), 0) + 1
    else:
        # Deals with diagonals for star 2
        x_range = list(range(min(x1, x2), max(x1, x2) + 1))
        y_range = list(range(min(y1, y2), max(y1, y2) + 1))
        if x1 > x2:
            x_range.reverse()
        if y1 > y2:
            y_range.reverse()
        for (x, y) in zip(x_range, y_range):
            matrix[(x, y)] = matrix.get((x, y), 0) + 1
    return matrix

if __name__ == "__main__":
    overlap: OverlapedPoints = OverlapedPoints({})
    for points in read_lines(FILEPATH):
        overlap = add_points(overlap, *points)

    counter = len([item for item in overlap.values() if item >= 2])
    print(counter)


"""Script for picking certain number of sampels."""

from argparse import ArgumentParser

from streaming import StreamingDataset, MDSWriter

def parse_args():
    args = ArgumentParser()
    args.add_argument('--input_dir', type=str, required=True)
    args.add_argument('--output_dir', type=str, required=True)
    args.add_argument('--compression', type=str, default='zstd:16')
    args.add_argument('--hashes', type=str, default='sha1,xxh3_64')
    args.add_argument('--size_limit', type=int, default=1 << 26)
    args.add_argument('--num_examples_to_pick', type=int, default=10000)
    return args.parse_args()

def main(args):
    dataset = StreamingDataset(local=args.input_dir, shuffle=False)
    columns = {
        'input_ids': 'bytes',
        'input_mask': 'bytes',
        'segment_ids': 'bytes',
        'masked_lm_positions': 'bytes',
        'masked_lm_ids': 'bytes',
        'masked_lm_weights': 'bytes',
        'next_sentence_labels': 'bytes',
    }
    hashes = args.hashes.split(',') if args.hashes else []
    with MDSWriter(args.output_dir, columns, args.compression, hashes, args.size_limit) as writer:
        pick_ratio = dataset.index.total_samples / args.num_examples_to_pick
        for i in range(args.num_examples_to_pick):
            sample = dataset[int(i * pick_ratio)]
            writer.write(sample)

if __name__ ==  '__main__':
    main(parse_args())


import math
import os
import pygame
import random
from pygame.locals import *
import subprocess

directory = os.getcwd()

#window
pygame.init()
WIDTH = 800
HEIGHT = 500
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tic Tac Toe")

#button vars
RADIUS = 20
GAP = 20
letters = []
startx = round((WIDTH - (RADIUS * 2 + GAP) * 13) / 2)
starty = 370

#font
LETTER_FONT = pygame.font.SysFont("comicsans", 30)
WORD_FONT = pygame.font.SysFont("comicsans", 50)
TITLE_FONT = pygame.font.SysFont("comicsans", 60)

#colors
WHITE = (255,255,255)
BLACK = (0, 0, 0,)
GREEN = (0, 255, 0)

#game loop
FPS = 60
clock = pygame.time.Clock()
run = True

#game vars
turn_count = -1
board = [["h", "h", "h"],
        ["h", "h", "h"],
        ["h", "h", "h"]]

class Button:
    def __init__(self, x, y, width, height, color, text):
        self.rect = pygame.Rect(x, y, width, height)
        self.color = color
        self.text = text

    def drawButton(self, screen):
        # Check if the mouse is hovering over the button
        if self.rect.collidepoint(pygame.mouse.get_pos()):
            # Use a lighter color when the mouse is hovering
            hover_color = tuple(min(c + 0, 200) for c in self.color)
            pygame.draw.rect(screen, hover_color, self.rect)
        else:
            pygame.draw.rect(screen, self.color, self.rect)

        font = pygame.font.Font(None, 80)
        text = font.render(str(self.text), True, BLACK)
        text_rect = text.get_rect(center=self.rect.center)
        screen.blit(text, text_rect)

    def setText(self, screen, msg):
        self.text = msg
        font = pygame.font.Font(None, 80)
        text = font.render(str(msg), True, BLACK)
        text_rect = text.get_rect(center=self.rect.center)
        screen.blit(text, text_rect)

    def getText(self):
        return self.text

#buttons
b_1 = Button(190, 110, 120, 120, WHITE, "")
b_2 = Button(190+120+5, 110, 120, 120, WHITE, "")
b_3 = Button(190+240, 110, 120, 120, WHITE, "")

b_4 = Button(190, 110+120+5, 120, 120, WHITE, "")
b_5 = Button(190+120+5, 110+120+5, 120, 120, WHITE, "")
b_6 = Button(190+240, 110+120+5, 120, 120, WHITE, "")

b_7 = Button(190, 110+240, 120, 120, WHITE, "")
b_8 = Button(190+120+5, 110+240, 120, 120, WHITE, "")
b_9 = Button(190+240, 110+240, 120, 120, WHITE, "")

buttons = [b_1, b_2, b_3, b_4, b_5, b_6, b_7, b_8, b_9]

#example funcs
def start_game():
    print("Starting the game!")

def quit_game():
    print("Quitting the game!")
    pygame.quit()

def draw():
    win.fill(WHITE)

    #draw title
    text = TITLE_FONT.render("TIC TAC TOE", 1, BLACK)
    win.blit(text, (WIDTH/2 - text.get_width()/2, 20))

    #draw player turn
    text = WORD_FONT.render("TURN:", 1, BLACK)
    win.blit(text, (600, 155))

    if turn_count == -1:
        text = LETTER_FONT.render("Player 1", 1, BLACK)

    elif turn_count % 2 != 0: #even
        text = LETTER_FONT.render("Player 1", 1, BLACK)
    else:
        text = LETTER_FONT.render("Player 2", 1, BLACK)
    win.blit(text, (610, 230))

    #draw lines
    pygame.draw.rect(win, BLACK, Rect(220, 94, 375, 5)) #underline
    #draw buttons
    for button in buttons:
        button.drawButton(win)
        if button.text:
            button.setText(win, button.text)
    #draw board
    pygame.draw.rect(win, BLACK, Rect(310, 110, 5, 120*3))
    pygame.draw.rect(win, BLACK, Rect(310+120, 110, 5, 120*3))
    pygame.draw.rect(win, BLACK, Rect(310-120, 110+120, 120*3, 5))
    pygame.draw.rect(win, BLACK, Rect(310-120, 110+120*2, 120*3, 5))

    pygame.display.update()

def button_clicked(button):
    if button == b_1:
        if not button.text:
            if turn_count % 2 == 0: #even:
                button.setText(win, "X")
                board[0][0] = "x"
            else:
                button.setText(win, "O")
                board[0][0] = "o"
    elif button == b_2:
        if not button.text:
            if turn_count % 2 == 0: #even:
                button.setText(win, "X")
                board[0][1] = "x"
            else:
                button.setText(win, "O")
                board[0][1] = "o"
    elif button == b_3:
        if not button.text:
            if turn_count % 2 == 0: #even:
                button.setText(win, "X")
                board[0][2] = "x"
            else:
                button.setText(win, "O")
                board[0][2] = "o"
    elif button == b_4:
        if not button.text:
            if turn_count % 2 == 0: #even:
                button.setText(win, "X")
                board[1][0] = "x"
            else:
                button.setText(win, "O")
                board[1][0] = "o"
    elif button == b_5:
        if not button.text:
            if turn_count % 2 == 0: #even:
                button.setText(win, "X")
                board[1][1] = "x"
            else:
                button.setText(win, "O")
                board[1][1] = "o"
    elif button == b_6:
        if not button.text:
            if turn_count % 2 == 0: #even:
                button.setText(win, "X")
                board[1][2] = "x"
            else:
                button.setText(win, "O")
                board[1][2] = "o"
    elif button == b_7:
        if not button.text:
            if turn_count % 2 == 0: #even:
                button.setText(win, "X")
                board[2][0] = "x"
            else:
                button.setText(win, "O")
                board[2][0] = "o"
    elif button == b_8:
        if not button.text:
            if turn_count % 2 == 0: #even:
                button.setText(win, "X")
                board[2][1] = "x"
            else:
                button.setText(win, "O")
                board[2][1] = "o"
    elif button == b_9:
        if not button.text:
            if turn_count % 2 == 0: #even:
                button.setText(win, "X")
                board[2][2] = "x"
            else:
                button.setText(win, "O")
                board[2][2] = "o"
    pygame.display.update()

def display_message(message):
    pygame.time.delay(1200) #wait 1 second
    win.fill(WHITE) #fill screen with blank white
    text = WORD_FONT.render(message, 1, BLACK) #render param 'message'
    win.blit(text, (WIDTH/2 - text.get_width()/2, HEIGHT/2 - text.get_height()/2)) # blit/put text in middle of screen
    pygame.display.update() #update screen
    pygame.time.delay(3000) #wait 3 seconds

def winCheckX(matrix):
    # Win conditions: Horizontal, Vertical, Main Diagonal, Other Diagonal
    win_conditions = [
        matrix[0] == ['x', 'x', 'x'],
        matrix[1] == ['x', 'x', 'x'],
        matrix[2] == ['x', 'x', 'x'],
        [matrix[i][0] for i in range(3)] == ['x', 'x', 'x'],
        [matrix[i][1] for i in range(3)] == ['x', 'x', 'x'],
        [matrix[i][2] for i in range(3)] == ['x', 'x', 'x'],
        [matrix[i][i] for i in range(3)] == ['x', 'x', 'x'],
        [matrix[i][2-i] for i in range(3)] == ['x', 'x', 'x']
    ]

    return any(win_conditions)

def winCheckO(matrix):
    # Win conditions: Horizontal, Vertical, Main Diagonal, Other Diagonal
    win_conditions = [
        matrix[0] == ['o', 'o', 'o'],
        matrix[1] == ['o', 'o', 'o'],
        matrix[2] == ['o', 'o', 'o'],
        [matrix[i][0] for i in range(3)] == ['o', 'o', 'o'],
        [matrix[i][1] for i in range(3)] == ['o', 'o', 'o'],
        [matrix[i][2] for i in range(3)] == ['o', 'o', 'o'],
        [matrix[i][i] for i in range(3)] == ['o', 'o', 'o'],
        [matrix[i][2-i] for i in range(3)] == ['o', 'o', 'o']
    ]

    return any(win_conditions)

def checkNotH (matrix):
    # Win conditions: Horizontal, Vertical, Main Diagonal, Other Diagonal
    win_conditions = [
        matrix[0] == ['h', 'h', 'h'],
        matrix[1] == ['h', 'h', 'h'],
        matrix[2] == ['h', 'h', 'h'],
        [matrix[i][0] for i in range(3)] == ['h', 'h', 'h'],
        [matrix[i][1] for i in range(3)] == ['h', 'h', 'h'],
        [matrix[i][2] for i in range(3)] == ['h', 'h', 'h'],
        [matrix[i][i] for i in range(3)] == ['h', 'h', 'h'],
        [matrix[i][2-i] for i in range(3)] == ['h', 'h', 'h']
    ]

    return any(win_conditions)

def display_message(message):
    pygame.time.delay(1000) #wait 1 second
    win.fill(WHITE) #fill screen with blank white
    text = WORD_FONT.render(message, 1, BLACK) #render param 'message'
    win.blit(text, (WIDTH/2 - text.get_width()/2, HEIGHT/2 - text.get_height()/2)) # blit/put text in middle of screen
    pygame.display.update() #update screen
    pygame.time.delay(3000) #wait 3 seconds

while run:
    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            for button in buttons:
                if button.rect.collidepoint(pos):
                    # Button clicked
                    turn_count += 1
                    #print(turn_count)
                    button_clicked(button)
            print(pygame.mouse.get_pos())
            print(board)
    #pygame.draw.rect(win, BLACK, Rect(425, 130, 300, 90)) # top right

    draw()

    if winCheckX(board):
        display_message('Player 1 wins!')
        break
    if winCheckO(board):
        display_message('Player 2 wins!')
        break

pygame.quit()


# TODO merge naive and weighted loss.
import torch
import torch.nn.functional as F

def weighted_nll_loss(pred, label, weight, avg_factor=None):
    if avg_factor is None:
        avg_factor = max(torch.sum(weight > 0).float().item(), 1.)
    raw = F.nll_loss(pred, label, reduction='none')
    return torch.sum(raw * weight)[None] / avg_factor

def weighted_cross_entropy(pred, label, weight, avg_factor=None, reduce=True):
    if avg_factor is None:
        avg_factor = max(torch.sum(weight > 0).float().item(), 1.)
    raw = F.cross_entropy(pred, label, reduction='none')
    if reduce:
        return torch.sum(raw * weight)[None] / avg_factor
    else:
        return raw * weight / avg_factor

def weighted_binary_cross_entropy(pred, label, weight, avg_factor=None):
    if avg_factor is None:
        avg_factor = max(torch.sum(weight > 0).float().item(), 1.)
    return F.binary_cross_entropy_with_logits(
        pred, label.float(), weight.float(),
        reduction='sum')[None] / avg_factor

def sigmoid_focal_loss(pred,
                       target,
                       weight,
                       gamma=2.0,
                       alpha=0.25,
                       reduction='mean'):
    pred_sigmoid = pred.sigmoid()
    target = target.type_as(pred)
    pt = (1 - pred_sigmoid) * target + pred_sigmoid * (1 - target)
    weight = (alpha * target + (1 - alpha) * (1 - target)) * weight
    weight = weight * pt.pow(gamma)
    loss = F.binary_cross_entropy_with_logits(
        pred, target, reduction='none') * weight
    reduction_enum = F._Reduction.get_enum(reduction)
    # none: 0, mean:1, sum: 2
    if reduction_enum == 0:
        return loss
    elif reduction_enum == 1:
        return loss.mean()
    elif reduction_enum == 2:
        return loss.sum()

def weighted_sigmoid_focal_loss(pred,
                                target,
                                weight,
                                gamma=2.0,
                                alpha=0.25,
                                avg_factor=None,
                                num_classes=80):
    if avg_factor is None:
        avg_factor = torch.sum(weight > 0).float().item() / num_classes + 1e-6
    return sigmoid_focal_loss(
        pred, target, weight, gamma=gamma, alpha=alpha,
        reduction='sum')[None] / avg_factor

def mask_cross_entropy(pred, target, label):
    num_rois = pred.size()[0]
    inds = torch.arange(0, num_rois, dtype=torch.long, device=pred.device)
    pred_slice = pred[inds, label].squeeze(1)
    return F.binary_cross_entropy_with_logits(
        pred_slice, target, reduction='mean')[None]

def smooth_l1_loss(pred, target, beta=1.0, reduction='mean'):
    assert beta > 0
    assert pred.size() == target.size() and target.numel() > 0
    diff = torch.abs(pred - target)
    loss = torch.where(diff < beta, 0.5 * diff * diff / beta,
                       diff - 0.5 * beta)
    reduction_enum = F._Reduction.get_enum(reduction)
    # none: 0, mean:1, sum: 2
    if reduction_enum == 0:
        return loss
    elif reduction_enum == 1:
        return loss.sum() / pred.numel()
    elif reduction_enum == 2:
        return loss.sum()

def weighted_smoothl1(pred, target, weight, beta=1.0, avg_factor=None):
    if avg_factor is None:
        avg_factor = torch.sum(weight > 0).float().item() / 4 + 1e-6
    loss = smooth_l1_loss(pred, target, beta, reduction='none')
    return torch.sum(loss * weight)[None] / avg_factor

def accuracy(pred, target, topk=1):
    if isinstance(topk, int):
        topk = (topk, )
        return_single = True
    else:
        return_single = False

    maxk = max(topk)
    _, pred_label = pred.topk(maxk, 1, True, True)
    pred_label = pred_label.t()
    correct = pred_label.eq(target.view(1, -1).expand_as(pred_label))

    res = []
    for k in topk:
        correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
        res.append(correct_k.mul_(100.0 / pred.size(0)))
    return res[0] if return_single else res


import pytest

def Fibonacci(n):
    # Check if input is 0 then it will print incorrect input
    if n < 0:
        print("Incorrect input")

    # Check if n is 0 then it will return 0
    elif n == 0:
        return 0

    # Check if n is 1,2 it will return 1
    elif n == 1 or n == 2:
        return 1

    else:
        return Fibonacci(n-1) + Fibonacci(n-2)

@pytest.mark.parametrize("n, ans", [(2,1), (3,2)])
def test_Fibonacci(n, ans):
    assert Fibonacci(n) == ans


#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" Fetching user description and subscriptions given a channel id.

Usage: python 11_scrape_youtube_subscriptions.py
Input data files: data/mbfc/to_crawl_users.csv
Output data files: data/mbfc/active_user_subscription.json.bz2
"""

import up  # go to root folder

import os, json, bz2, time

from utils.helper import Timer
from utils.crawlers import get_subscriptions_from_channel

def main():
    timer = Timer()
    timer.start()

    input_filepath = 'data/mbfc/to_crawl_users.csv'
    output_filepath = 'data/mbfc/active_user_subscription.json.bz2'

    visited_channel_set = set()
    if os.path.exists(output_filepath):
        with bz2.BZ2File(output_filepath, 'r') as fin:
            for line in fin:
                line = line.decode('utf-8')
                channel_id = json.loads(line.rstrip())['channel_id']
                visited_channel_set.add(channel_id)
    print('visited {0} channels in the past, continue...'.format(len(visited_channel_set)))

    num_user = len(visited_channel_set)
    with bz2.open(output_filepath, 'at') as fout:
        with open(input_filepath, 'r') as fin:
            for line in fin:
                user_id = line.rstrip().split(',')[0]
                if user_id not in visited_channel_set:
                    num_request = 0
                    found = False
                    print('get description and subscriptions for user {0}'.format(user_id))
                    while num_request < 5:
                        try:
                            profile_json = get_subscriptions_from_channel(user_id, target='subscription')
                            found = True
                        except:
                            num_request += 1

                        if found:
                            fout.write('{0}\n'.format(json.dumps(profile_json)))
                            num_user += 1
                            print('{0} subscriptions are obtained for user {1}: {2}\n'.format(len(profile_json['subscriptions']), num_user, user_id))
                            time.sleep(1)
                            break

    timer.stop()

if __name__ == '__main__':
    main()


import pandas as pd
import numpy as np
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import LinearSVC
from sklearn.metrics import confusion_matrix

#Function importing data from .txt file
def gatherData(directory):
    with open(directory, "rb") as myFile:
        data_table = pd.read_csv(myFile, sep=" ", header=None)
    return data_table.values

#Data importing

train_features = gatherData("train/X_train.txt")
train_labels = gatherData("train/Y_train.txt")
test_features = gatherData("test/X_test.txt")
test_labels = gatherData("test/Y_test.txt")
print("Data loaded")

#Model training

model = GaussianNB()
model.fit(train_features, train_labels.ravel())
print("Trained model 1")
model2 = LinearSVC(multi_class="ovr") #I'm using One vs Rest method for multi-class classification
model2.fit(train_features, train_labels.ravel())
print("Trained model 2")

#Making predictions

predictions = model.predict(test_features)
confusion = confusion_matrix(test_labels, predictions)
counter = 0
print("Predicted for model 1")

predictions2 = model2.predict(test_features)
confusion2 = confusion_matrix(test_labels, predictions2)
counter2 = 0
print("Predicted for model 2")

#Checking accuracy

for i in range(np.size(confusion, 0)):
    counter += confusion[i][i]
    counter2 += confusion2[i][i]
print("{0:.2f} percent of test values predicted well with GaussianNB".format(float(counter/len(predictions))*100))
print("{0:.2f} percent of test values predicted well with LinearSVC".format(float(counter2/len(predictions))*100))


#!/usr/bin/python3
# 文件名：client.py

# 导入 socket、sys 模块
import socket
from time import ctime
import sys
import os
import time

# host = socket.gethostname()
host = "172.16.1.14"
port = 6666
BUFSIZ = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

while True:
    data = input(">>").strip()
    if not data:
        break
    data = str(len(data)) + '\n' + data
    print(data)
    s.send(data.encode('utf-8'))

    loadabel_file = open('inception_v1-nv_large-direct-quant.nvdla', 'rb')
    data = loadabel_file.read()
    data = str(len(data)) + '\n' + str(data)
    s.send(data.encode('utf-8'))

    recv_data = s.recv(BUFSIZ)
    if not recv_data:
        break
    data_len = int(recv_data)
    print(data_len)
    data = s.recv(data_len)
    print(data)

# msg = 'GET_WELCOME'
# msg_len = len(msg)
# print(msg_len)
# msg = str(msg_len) + '\n' + msg

# s.send(msg.encode('utf-8'))

# # 接收小于 1024 字节的数据
# msg = s.recv()

# time.sleep(10)
# s.close()

# print (msg.decode('utf-8'))


import numpy as np
from numpy import genfromtxt
from functools import reduce
#--- przerabianie grzybow ---#
mushroom = genfromtxt('dane/mushroom.csv', delimiter=',' ,dtype = str)

classes = mushroom[:,0].copy()
last = mushroom[:,-1].copy()
training_set= mushroom
training_set[:,0]= last
training_set[:,-1]=classes

def f(x):
    if x=='p':
        return 1
    if x =='e':
        return 0

binary_classes = [f(x) for x in classes]
b  = np.reshape(binary_classes,(8124, 1))

def toBinaryFeatures(features):
    COLUMNS = features.shape[1]
    v = [x + str(i % COLUMNS) for i, x in enumerate(features.flatten())]
    l = features.tolist()
    uv = list(set(v)) # unique values of all features

    mv = {} # mapping to unique powers of 2
    for i,x in enumerate(uv):
        mv[x] = 2**i

    as_numbers = [reduce((lambda x, y: x | y), [mv[x + str(i)] for i, x in enumerate(row)]) for row in l]
    TO_BIN = "{0:0" + str(len(mv)) +"b}"
    flattened_features = [[int(char) for char in TO_BIN.format(number)] for number in as_numbers]

    return np.array(flattened_features)
a= toBinaryFeatures(training_set)
grzyby_zestaw_uczacy = np.append(a,b,axis=1)
#--- koniec przerabiania grzybow ---#
#--- pobieranie danych ---#
training_set = np.array([[0, 1, 0, 1, 0, 1],[0, 0, 0, 1, 0, 0],[0, 0, 0, 0, 1, 0],[1, 0, 1, 0, 1, 0],[0, 1, 1, 1, 0, 1],[0, 1, 0, 0, 1, 1],[1, 1, 1, 0, 0, 0],[1, 1, 1, 1, 0, 1],[0, 1, 1, 0, 1, 0],[1, 1, 0, 0, 0, 1],[1, 0, 0, 0, 1, 0]])
test1 = genfromtxt('dane/test1.csv', delimiter=',')
dane1 = genfromtxt('dane/dane1.csv', delimiter=',')
udacity_set1 = np.array(
        [[1,1,1,0],
         [1,0,1,0],
         [0,1,0,1],
         [1,0,0,1]])
#--- koniec pobierania danych ---#

#--- liczenie entropii ---#
def H(X):
    if X.shape[0] == 0:
        return 0 #nie wiem jaka jest entropia zrobic jak macierz jest pusta
    fx = X[:,-1]
    zeros = len([j for j in fx if j == 0])
    ones = len([j for j in fx if j == 1])
    p0 = zeros/ len(fx)
    p1 = 1-p0
    if p0 == 0 or p0 == 1:
        return 0
    return -p0*np.log2(p0)-p1*np.log2(p1)

def select_attribute(X,i,v):
    list = X[np.where(X[:,i] == v)]
    return list

def Q(X,i,v):
    return select_attribute(X,i,v).shape[0]/X.shape[0]

def IG(X,i):
    return H(X)-Q(X,i,0)*H(select_attribute(X,i,0))-Q(X,i,1)*H(select_attribute(X,i,1))

def ID3(S, recursion = 1, tree = {}):
    result = np.array([])
    rows = S.shape[0]
    columns = S.shape[1]

    decision_column = S[:,columns-1]
    if np.all(decision_column==0):
        tree[recursion] = int(0)
        return
    if np.all(decision_column==1):
        tree[recursion] = int(1)
        return
    for i in range(columns-1):
        ig = IG(S,i)
        result = np.append(result, ig)
    j = result.argmax()
    leaf_label2 = str(int(j))
    tree[recursion] = leaf_label2
    S0 = select_attribute(S,j,0)
    S1 = select_attribute(S,j,1)
    ID3(S0,recursion*2,tree)
    ID3(S1,recursion*2+1,tree)
    return tree

def zwroc_wierzcholki(X):
    drzewo_lista = list(X.keys())
    wynik = {}
    for i in range(0, len(drzewo_lista)):
        index = drzewo_lista[i]
        if X[index] != 1 and X[index] != 0:
            wynik[index] = X[index]
    return sorted(list(wynik.keys()))

def poddrzewa(drzewo):
    wszystkie_drzewa = []
    wierzcholki = zwroc_wierzcholki(drzewo)
    drzewo_klucze = sorted(list(drzewo.keys()))
    for wierzcholek in wierzcholki[1:]:
        pojedyncze = {}
        for j in range(0,drzewo_klucze.index(wierzcholek)):
            wartosc = drzewo_klucze[j]
            pojedyncze[wartosc]=drzewo[wartosc]
        wszystkie_drzewa.append(pojedyncze)
    wszystkie_drzewa.append(drzewo)
    return wszystkie_drzewa

def zapis2(drzewo):
    a = [None for i in range(max(drzewo.keys()))]
    for i in drzewo.keys():
        a[i-1]=drzewo[i]
    return a

def drzewa_zapisy(drzewo):
    return list(drzewo.items()), list(drzewo.keys()), list(drzewo.values()), zapis2(drzewo)

def poddrzewa2(drzewo):
    wszystkie = []
    chwilowe= []
    drzewo = zapis2(drzewo)
    for i in drzewo:
        chwilowe.append(i)
        if type(i) == int:
            wszystkie.append(chwilowe.copy())
    return wszystkie

def blad_drugiego_rodzaju(drzewa, training_set):
    wynik = []
    iloscbledow=0
    for drzewo in drzewa:
        test_set = training_set.copy()

        for i in range(0,len(drzewo)-1):
            if i%2 == 1: #negatywne
                if type(drzewo[i]) == str:
                    atrybut = int(drzewo[i])
                    test_set = select_attribute(test_set,atrybut,1)

                elif drzewo[i] == 1:
                    iloscbledow = len([j for j in test_set[:,-1] if j==1])
                    print(test_set)
                    print(iloscbledow)
                elif drzewo[i] == 0:
                    iloscbledow = len([j for j in test_set[:,-1] if j==0])
                    print(test_set)
                    print(iloscbledow)
            else: #pozytywne
                if type(drzewo[i]) == str:
                    atrybut = int(drzewo[i])
                    test_set = select_attribute(test_set,atrybut,0)

                elif drzewo[i] == 1:
                    iloscbledow = len([j for j in test_set[:,-1] if j==1])
                    print(test_set)
                    print(iloscbledow)
                elif drzewo[i] == 0:
                    iloscbledow = len([j for j in test_set[:,-1] if j==0])
                    print(test_set)
                    print(iloscbledow)
                wynik.append(iloscbledow)
    return wynik

#def treetoarray(tree):
#    a=0
#    array = max(tree.keys())
#    treearray =  np.empty([2,len])
#    for i in tree.keys():
#        treearray[1]
#        a = a+1
#
#

#
#def liczbalisci(drzewo):
#    z = list(drzewo.values())
#    f = z.count(1)
#    g = z.count(0)
#    return f+g
#
#def blad(drzewo,S):
#

#def alfai(T,Ti,S):
#    return blad(Ti,S)-blad(T,S)/liczbalisci(T)-liczbalisci(Ti)
#
#def wybierzdrzewo(drzewa,S):
#    wyniki= []
#    glowne_drzewo = drzewa[len(drzewa)-1]
#    for drzewo in drzewa:
#        wyniki.append(alfai(glowne_drzewo,drzewo,S))
#    indexdrzewa= wyniki.index(max(wyniki))
#    return drzewa[indexdrzewa]
#
#

#from sklearn.feature_extraction import DictVectorizer
#dvec = DictVectorizer(sparse=False)
#
#X = dvec.fit_transform(mushroom.transpose().to_dict().values())
#
#data = pd.DataFrame({'0': ['u']})
#res = pd.get_dummies(mushroom)
#res.to_csv('output.csv')
#print(res)


"""
Functions - Lab
Check your code: https://judge.softuni.bg/Contests/Practice/Index/1727#0

SUPyF2 Functions-Lab - 01. Grades

Problem:
1.	Grades
Write a function that receives a grade between 2.00 and 6.00 and prints the corresponding grade in words
•	2.00 – 2.99 - "Fail"
•	3.00 – 3.49 - "Poor"
•	3.50 – 4.49 - "Good"
•	4.50 – 5.49 - "Very Good"
•	5.50 – 6.00 - "Excellent"

Examples:
Input:	Output:
3.33	Poor
4.50	Very Good
2.99	Fail
"""

def grades(score):
    if 2.00 <= score <= 2.99:
        return "Fail"
    elif 3.00 <= score <= 3.49:
        return "Poor"
    elif 3.50 <= score <= 4.49:
        return "Good"
    elif 4.50 <= score <= 5.49:
        return "Very Good"
    elif 5.50 <= score <= 6.00:
        return "Excellent"

print(grades(float(input())))

SoftUni Lessons/Python Development/Python Fundamentals September 2019/Problems And Files/12 FUNCTIONS - Дата 9-ти октомври, 1830 - 2130/01. Grades.py


from bson import ObjectId
from pydantic import BaseModel, Field
from typing import Optional

class PyObjectId(ObjectId):
    @classmethod
    def __get_validators__(cls):
        yield cls.validate

    @classmethod
    def validate(cls, v):
        if not ObjectId.is_valid(v):
            raise ValueError("Invalid objectid")
        return ObjectId(v)

    @classmethod
    def __modify_schema__(cls, field_schema):
        field_schema.update(type="string")

class AddressModel(BaseModel):
    id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
    userId: str = Field(...)
    addresses: Optional[tuple]

    class Config:
        allow_population_by_field_name = True
        arbitrary_types_allowed = True
        json_encoders = {ObjectId: str}
        schema_extra = {
            "example": {
                "id": "abc123",
                "userId": "dansId123",
                "addresses": [
                    {
                      "firstLine": "1 dan road",
                      "lastLine": "1 second line",
                      "townCity": "Huddersfield",
                      "postcode": "HD1 111",
                      "country": "England"
                    }
                ]
            }
        }

class AddAddressModel(BaseModel):
    firstLine: str = Field(...)
    secondLine: str = Field(...)
    TownCity: str = Field(...)
    Postcode: str = Field(...)
    Country: str = Field(...)

    class Config:
        allow_population_by_field_name = True
        arbitrary_types_allowed = True
        json_encoders = {ObjectId: str}
        schema_extra = {
            "example": {
                "id": "abc123",
                "userId": "dansId123",
                "addresses": [
                    {
                      "firstLine": "1 dan road",
                      "lastLine": "1 second line",
                      "townCity": "Huddersfield",
                      "postcode": "HD1 111",
                      "country": "England"
                    }
                ]
            }
        }


import os
import csv
File = os.path.join("Resources","election_data.csv")
count_khan = 0
count_correy = 0
count_votes = 0
count_Li = 0
count_tooley = 0
candidates = []
with open(File) as csvfile:
    csvreader = csv.reader(csvfile, delimiter=",")
    next(csvreader, None)

    for row in csvreader:
        count_votes += 1
        if row[2] not in candidates:
            candidates.append(row[2])

        if row[2] == "Khan":
            count_khan = count_khan +1
        if row[2] == "Correy":
            count_correy = count_correy +1
        if row[2] == "Li":
            count_Li = count_Li +1
        if row[2] == "O'Tooley":
            count_tooley = count_tooley +1

print(f'Total votes cast: {count_votes}')
print(f'Posible candidates {candidates}')
print("-------------------------------------------------------------------")
k=(count_khan/count_votes)
print(f'{candidates[0]} percentage {"{:.2%}".format(k)} votes {count_khan}')
c=(count_correy/count_votes)
print(f'{candidates[1]} percentage {"{:.2%}".format(c)} votes {count_correy}')
L=(count_Li/count_votes)
print(f'{candidates[2]} percentage {"{:.2%}".format(L)} votes {count_Li}')
t=(count_tooley/count_votes)
print(f"{candidates[3]} percentage {'{:.2%}'.format(t)} votes {count_tooley}")
print("-------------------------------------------------------------------")
if count_khan > count_Li and count_khan > count_correy and count_khan > count_tooley:
    print("Winner: Khan")
if count_Li > count_khan and count_Li > count_correy and count_Li > count_tooley:
    print("Winner: Li")
if count_correy > count_khan and count_correy > count_Li and count_correy > count_tooley:
    print("Winner: Correy")
if count_tooley > count_khan and count_tooley > count_Li and count_tooley > count_correy:
    print("Winner: Correy")


"""
PySynth Output Classes

We contain the PySynth OutputHandler,
and OutputControl.

Both classes handle and manage output from synth chains,
and send them to output modules attached to the Control class.

The Output class is the engine of this process,
pulling audio info and passing it along.
The OutputControl class manages the synth chains,
adding and removing them as necessary.

The design of the output class is heavily structured for sequencer use,
but you could script synth output using certain features located in OutputControl.
"""

import threading
import time
from concurrent.futures import ThreadPoolExecutor

from pysynth.utils import BaseModule, AudioCollection, get_time
from pysynth.output.modules import BaseOutput
from pysynth.osc import ZeroOscillator

class OutputControl(BaseModule):

    """
    Output Control - Controls adding and removing synths from the OutputHandler.

    We offer an easy to use interface for adding synths to the OutputHandler.

    We act as any other synth module,
    but when we are started, we add ourselves to the OutputHandler.
    This starts the other modules attached to us, and audio information is consumed by the OutputHandler.

    Because of this, we should be the LAST module in a synth chain.
    If not, then some information could be lost,
    and errors could occur when outputting information.

    We offer the ability to add ourselves to the OutputHandler until we are stopped
    (Great for sequencer use).
    We also offer an internal scheduling system that allows users and sequencers to specify
    when we should add and remove ourselves from the OutputHandler.
    This allows us to control ourselves, and makes audio scheduling easy!

    Point is, if you iterate over us,
    or call our start method,
    then we will add ourselves to the OutputHandler until we are stopped.

    You shouldn't create this module directly.
    Instead, you should receive your very own OutputControl module
    when you bind a synth to the OutputHandler.
    """

    OUT = []  # Reference to OutputHandler

    def __init__(self):

        super(OutputControl, self).__init__()

        self.item_written = 0  # Number of items to write. If 0, then we don't keep track

        self.time_events = []  # List of time events

        self.started = False  # Value determining if we are added to the OutputHandler
        self.finishing = False  # Value determining if we are finishing
        self.wait = False  # Value determining if we are started, but are wating on a time event

    def start(self):

        """
        We simply prepare the object for iteration.

        '__iter__()' does all the dirty work!
        """

        # Check if we are not started

        if not self.started:

            print("Starting...")

            return iter(self)

        # Check if we are finishing:

        if self.finishing:

            # We are just finishing, restart the modules:

            print("Resetting modules...")

            self.input.start_modules()

            self.finishing = False

            self.info.done = 0

    def stop(self):

        """
        Stop method - A polite way of stopping this control instance!

        This function prepares this instance to stop,
        but does not actually do so until all modules are ready to stop.

        This allows features like fade outs, echos, and amplitude envelopes
        to continue to operate even after this instance is asked to stop.

        Once all modules have reported that they are ready to stop,
        and there are no other time events to process,
        then we will automatically call 'abs_stop()' which
        will complete the process of removing us from the OutputHandler.
        """

        # Tell the chain that we are done:

        self.done()

        # Have the modules finish up:

        self.input.finish_modules()

        # Update our status:

        self.finishing = True

    def abs_stop(self):

        """
        Absolute stop - Completely stops this control instance!

        When called, we remove ourselves from the OutputHandler,
        reset our state, and stop the synth chain.

        This function ignores time events and finish status!
        Thats great for immediately stopping this synth chain,
        but not that great if you are expecting modules to finish up,
        and it might interfear with the sequencer,
        and it's ability to properly manage the notes attached to this object.

        This function is called by us automatically,
        when the synth chain is finished,
        and we don't have any more time events.
        So worry not! You do not have to call this function manually,
        as we will automatically do so.

        This removes us from the OutputHandler,
        meaning that this chain will NOT be in an event loop.
        This means that time events and the modules will NOT be sampled
        or worked with until our 'start()' method is called by an external entity.

        Point is - It is probably in your best intrest to NOT call this function!
        We should be able to automatically handle the process of stopping ourselves.
        It is better to politely stop this module chain by calling 'stop()',
        so we can ensure that the modules are completely ready to stop.
        """

        if self.info.running:

            # Remove ourselves from the OutputHandler:

            print("Removing ourselves from output hand")

            self.OUT[0]._remove_synth(self)

            # Reset our values:

            self.item_written = 0
            self.time_events.clear()

            # Stop the chain:

            self.input.stop_modules()

            # Update our status:

            self.started = False
            self.finishing = False

    def join(self):

        """
        Blocks until the OutputControl is removed from OutputHandler.
        """

        while self.started:

            continue

        return

    def get_next(self):

        """
        We simply return values from the synth chain attached to us.

        We also do some checks to determine if we should stop.
        If we do stop, we will call our 'stop()' method,
        which will remove us from the OutputHandler.
        """

        time_now = get_time()

        # Lets see if we are ready to start:

        if self.time_events and self.time_events[0][0] > time_now:

            # We are not ready to start, lets wait:

            return None

        if self.time_events and self.time_events[0][0] < time_now:

            # We are ready, lets start the synth chain:

            self.wait = False

            self.start()

        # Lets see if we are ready to stop:

        if self.time_events and self.time_events[0][1] < time_now and self.time_events[0][0] >= 0:

            print("Ready to stop")

            # We are done! Lets remove the time event:

            self.time_events.pop(0)

            # Prepare the synth chain for stopping:

            self.stop()

        # Lets see if we have written enough:

        if self.item_written != 0 and self.item_written > self.index:

            # We have written everything we can, lets prepare for stopping:

            self.stop()

        # State of the object has been changed, let's see if we are ready to stop:

        if not self.info.running or self.info.done == self.info.connected:

            # Chain has stopped, or all modules ready to stop:

            print("Removing synth chain...")

            self._event_done()

            self.info.done = 0

            return None

        # Otherwise, lets just return!

        return self.get_input()

    def write_num(self, num):

        """
        Registers ourselves to the OutputHandler for a number of iterations.

        :param num: Number of values to write
        :type num: int
        """

        # Set the max write limit:

        self.item_written = num

        # Start ourselves:

        self.start()

    def time_event(self, start, stop):

        """
        Schedules a time event for this OutputControl instance.

        Time events allows us to add and remove ourselves during certain intervals.

        'start' determines the time to start this object.
        We will not return audio values until the value from 'get_time()'
        is less than or equal to this value.
        If you want this object to start immediately, then supply 0 for this value.

        'stop' determines the time to stop this object.
        We will remove ourselves from the OutputHandler when the value from 'get_time()'
        is less than or equal to this value.
        If you want this object to continue indefinitely, then supply -1 for this value.

        You can schedule multiple time events, and we will handle them as necessary.
        Once a time event has been completed,
        (Added and removed ourselves during the given interval),
        then we will remove the time event.
        If there are no other time events, then we will remove ourselves from the OutputHandler.
        """

        # Add the time event to this object:

        self.time_events.append([start, stop])

    def _event_done(self):

        """
        Method called when we have processed a time event
        (Passed start and stop times).

        We will determine if we should remove ourselves completely from OutputHandler.
        If these are more events in the 'time_events' list,
        then we will remain added, as we will eventually start ourselves.
        """

        if self.time_events:

            # We are good to continue

            return

        # No events scheduled, lets exit:

        self.abs_stop()

    def __iter__(self):

        """
        We do the same as BaseModule,
        except we add ourselves to the OutputHandler,
        and we don't call 'start()'!
        """

        # Reset the index value:

        self.index = 0

        # Prepare the sub-modules:

        self.input.start_modules()

        # Set our started value:

        self.started = True

        # Add ourselves to the OutputHandler:

        self.OUT[0]._add_synth(self)

        # Return ourselves:

        return self

class OutputHandler:

    """
    OutputHandler - Handles and coordinates sending audio data to certain locations

    We handle to addition and subtraction of synths,
    allowing them to be added and removed on the fly.
    This is useful for systems such as the sequencer.

    We also allow for outputting info to multiple sources,
    so one could configure the output to send data to speakers
    and a wave file at the same time.

    When a synth is added, an 'OutputControl' is returned.
    This class allows for the control of the synth added,
    and allows for the developer to add the synth for a certain amount of time.
    It also integrates will with the sequencer.

    We are a 'reactive' output system,
    meaning we only sample the synths when the modules request us too.
    This allows for a balance of speed and accuracy,
    so we don't sample more frames then we should.
    This also means that we will only sample as quickly as our slowest module.
    Most of the time this is ideal,
    but if not then you should take care to only load modules you need!
    """

    def __init__(self, rate=44100):

        self._output = []  # Output modules to send information
        self._work = ThreadPoolExecutor()  # Thread pool executor to put our output modules in
        self._input = AudioCollection()  # Audio Collection to mix sound
        self.rate = rate  # Rate to output audio
        self.futures = []
        self.thread = []

        self.run = False  # Value determining if we are running
        self._pause = threading.Event()  # Event object determining if we are paused

        self._pause.set()

        self._input.add_module(ZeroOscillator())

    def add_output(self, out):

        """
        Adds an output module to this class.

        We ensure it inherits BaseModule, and then we add it.

        If we are currently running,
        then we start it and add it to the thread pool executor.

        If we are not started, then we will wait to add the modules until we have been.

        :param out: Output modules to add
        :type out: BaseOutput
        """

        # Ensure object is output module:

        assert isinstance(out, BaseOutput), "Class must inherit BaseOutput!"

        # Add ourselves to the module:

        out.out = self

        # Check if we are running:

        if self.run:

            # Start and add the module:

            self._submit_module(out)

        # Otherwise, add it to the collection and start it later:

        self._output.append(out)

    def bind_synth(self, synth):

        """
        Binds a synth chain to the Output class.

        We return an OutputControl class to manage adding the synth to this class.
        This allows synths to be managed by a sequencer,
        or for them to be added for a specified amount of time.

        We will bind the synth chain to the OutputControl class,
        and return it.

        We also set the sampling rate of the synth chain to our own,
        so all synths can maintain a similar sampling rate.

        :param synth: Synth chain to add to output
        :type synth: BaseModule
        :return: OutputControl with the synth chain bound to it
        :rtype: OutputControl
        """

        # Create an output control:

        out = OutputControl()
        out.OUT.append(self)

        # Bind the synth to the output control:

        out.bind(synth)

        # Bind our sampling rate to the synth chain:

        synth._info.samp = self.rate

        # Return the output control:

        return out

    def start(self):

        """
        Starts the OutputHandler.

        This entails starting the output modules we have added,
        and creating a barrier to ensure all modules are synchronized in getting their values.

        We will start to consume audio information until we are stopped or paused.
        """

        # Set the run value:

        self.run = True

        # Create the barrier:

        self.barrier = threading.Barrier(len(self._output))

        # Start all the modules in our collection:

        for mod in self._output:

            # Start the module:

            self._submit_module(mod)

    def stop(self):

        """
        Stops the OutputHandler.

        This entails stopping all the output modules in our collection,
        and stopping the control thread that sends audio to the modules.

        Once stopped, the OutputHandler can be started again.
        However, some modules can't be restarted.
        So be prepared for errors, or certain output modules not working.
        """

        # Set the run value:

        self.run = False

        # Stop all output modules:

        for mod in self._output:

            # Stop the module:

            self._stop_module(mod)

    def pause(self):

        """
        Pauses the OutputHandler.

        We clear our pause event,
        which will cause the control thread to pause until it is unset.

        Audio information will not be consumed when paused!
        """

        # Clear the event:

        self._pause.clear()

    def resume(self):

        """
        Resumes the OutputHandler.

        We set out pause event,
        which will cause the control thread to continue.

        Audio information will be consumed when resumed!
        """

        # Set the event:

        self._pause.set()

    def gen_value(self):

        """
        Gets and sends the input from the synths to each output module.

        This allows audio information to be sampled only when it is necessary!
        Because modules block before getting information,
        all modules will be ready to receive information when this method is called.
        """

        # Iterate until we ge something valid:

        while True:

            # Pause if necessary:

            self._pause.wait()

            # Get some audio information:

            inp = next(self._input)

            if inp is None:

                continue

            # Iterate over our modules:

            for mod in self._output:

                # Add the input to the module:

                if mod.special:

                    # Ignore and continue:

                    continue

                mod.add_input(inp)

            return inp

    def _add_synth(self, synth):

        """
        Adds a synth to the AudioCollection.

        This should really only be called by OutputControl,
        as they have the ability to fine-tune the operation.

        :param synth: Synth to be added to the Output class
        :type synth: BaseModule
        """

        # Add the synth to our collection:

        print("Added synth: {}".format(synth))

        self._input.add_module(synth)

    def _remove_synth(self, synth):

        """
        Removes a synth from the AudioCollection.

        This should really only be called by OutputControl,
        as they have the ability to fine-tune the operation.

        :param synth: Synth to be removed from the Output class
        :type synth: BaseModule
        """

        # Remove the synth from our collection:

        self._input.remove_module(synth)

    def _submit_module(self, mod):

        """
        We do the dirty work of starting and submitting a module to the ThreadPoolExecutor.

        We assume the module inherits BaseOutput,
        and that it has been added to the module collection.

        If you wish to add a module, you should really use 'add_output'.

        :param mod: Output module to start
        :type mod: BaseOutput
        """

        # Set the run value:

        mod.running = True

        # Start the module:

        mod.start()

        # Add it to the collection:

        #self._work.submit(mod.run)

        thread = threading.Thread(target=mod.run)
        thread.daemon = True
        thread.start()
        self.thread.append(thread)

    def _stop_module(self, mod):

        """
        Stops the given module.

        We call the 'stop' method, and set the running value to False.
        We also add None to the input queue.

        :param mod: Module to stop
        :type mod: BaseOutput
        """

        # Set the run value:

        mod.running = False

        # Call the stop method:

        mod.stop()

        # Add 'None' to the input queue:

        mod.add_input(None)


from queue import PriorityQueue

class Graph:

    # edges is a matrix and the size of the matrix depends on the number of vertices
    def __init__(self, numOfVertices):
        self.v = numOfVertices
        self.visited = []
        self.edges = [[-1 for i in range(numOfVertices)] for j in range(numOfVertices)]

    def addEdge(self, u, v, weight):
        self.edges[u][v] = weight
        self.edges[v][u] = weight

    def dijkstra(self, start_vertex):
        D = {v:float('inf') for v in range(self.v)}
        D[start_vertex] = 0

        pq = PriorityQueue()
        pq.put((0, start_vertex))

        while not pq.empty():
            (dist, current_vertex) = pq.get()
            self.visited.append(current_vertex)


import requests
import pandas as pd
import numpy as np
import seaborn as sns
from bs4 import BeautifulSoup
import warnings
import nltk
#import surprise
import scipy as sp
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import stopwords
from nltk import word_tokenize, RegexpTokenizer
from nltk.stem import SnowballStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from datetime import datetime, time

class Tags():

    def __init__(self):
         self.cargaDocumentos()

    def cargaDocumentos(self):
        self.df_usuaarioO = pd.read_csv('csv/Usuario_0.csv', sep=';')

        self.df_usuaarioO = self.df_usuaarioO.drop(columns=["title"])

        for usuario_nuevo in range(len(self.df_usuaarioO["movieId"])):
            self.df_usuaarioO["userId"] = 0
            self.df_usuaarioO["timestamp"] = datetime.now()

        self.df_movies = pd.read_csv('csv/movies.csv')

        self.df_movies = self.df_movies.dropna()
        self.df_ratings = pd.read_csv('csv/ratings.csv')
        self.df_ratings = self.df_ratings.dropna()
        self.df_tags = pd.read_csv('csv/tags.csv')
        self.df_tags = self.df_tags.dropna()

        self.df_movies_ratings_old = self.df_ratings.merge(self.df_movies)[
            ['userId', 'movieId', 'title', 'rating', 'genres']]

        self.df_movies_ratings_tags_old = pd.merge(self.df_movies_ratings_old, self.df_tags, how='outer')[
            ['userId', 'movieId', 'title', 'rating', 'genres', 'tag']]
        self.df_movies_ratings_tags_old["tag"] = self.df_movies_ratings_tags_old["tag"].str.lower()

        self.df_ratings = pd.concat([self.df_usuaarioO, self.df_ratings], axis=0)

        self.df_movies_ratings = self.df_ratings.merge(self.df_movies)[
            ['userId', 'movieId', 'title', 'rating', 'genres']]

        self.df_movies_ratings_tags = pd.merge(self.df_movies_ratings, self.df_tags, how='outer')[
            ['userId', 'movieId', 'title', 'rating', 'genres', 'tag']]
        self.df_movies_ratings_tags["tag"] = self.df_movies_ratings_tags["tag"].str.lower()
        # self.df_movies_ratings_tags.fillna("vacio", inplace = True)

        self.ratings_table = self.df_movies_ratings.pivot_table(index='userId', columns='title', values='rating')
        # para cambiar los NAN por 0:
        self.ratings_table.fillna(0, inplace=True)

    def recomedacionPorTags(self, nombrePelicula, n_similares):
        listaPeliculasMostrar = []
        try:
            n_similares=int(n_similares)
            count_matrix = self.df_movies_ratings_tags.pivot_table(index='movieId', columns='tag', values='userId')
            #count_matrix = self.df_movies_ratings_tags.pivot_table(index='movieId', columns='tag', values='rating')
            count_matrix.fillna(0, inplace=True)
            sparse_rating = sp.sparse.csr_matrix(count_matrix)
            selected_movie = self.df_movies[self.df_movies["title"] == nombrePelicula]["movieId"].values[0]
            #encontramos el id de la pelicula en la matriz
            selected_movie_index = count_matrix.index.get_loc(selected_movie)

            similarities = cosine_similarity(sparse_rating, sparse_rating[selected_movie_index])

            movie_list = [(index, similarity) for index, similarity in enumerate(similarities)]
            movie_list.sort(key=lambda x: x[1], reverse=True)

            if(n_similares>len(movie_list)):
                n_similares=len(movie_list)-1

            bandera=False

            contador = 1
            for movie in movie_list[0:n_similares]:
                if(nombrePelicula != self.df_movies.iloc[movie[0]]["title"]):

                    listaPeliculasMostrar.append(self.df_movies.iloc[movie[0]]["title"])
                    contador+=1
                else:
                    bandera=True
            if(bandera):
                mov=movie_list[n_similares][0]

                listaPeliculasMostrar.append(self.df_movies.iloc[mov]["title"])
        except Exception as e:
            print("")
        return listaPeliculasMostrar

    def predecirRatingDeUserAPeliculaPorSusTags(self, nombrePelicula, user_id):
        user_id=int(user_id)
        yaVotado = self.df_movies_ratings[(self.df_movies_ratings['title']==nombrePelicula) & (self.df_movies_ratings['userId']==user_id)]["rating"].unique()
        if(len(yaVotado)!=0):
            prediction = yaVotado[0]

            return str(prediction)
        else:
            # obtener tags de la película a predecir
            tagsPeli = []
            movie_tags = self.df_movies_ratings_tags[self.df_movies_ratings_tags['title']==nombrePelicula]["tag"].unique()
            for m in movie_tags:
                tagsPeli.append(m)

            filtroMergeandoTags = self.df_movies_ratings_tags[['userId','movieId','title', 'rating', 'tag']]
            filtroEnBaseUserId = filtroMergeandoTags[filtroMergeandoTags['userId']==user_id]

            #similitud = [distance.cosine(tagsPeli, j['tag']) for i,j in filtroEnBaseUserId.iterrows()]

            user_ratings = filtroEnBaseUserId[filtroEnBaseUserId['tag'].isin(tagsPeli)]
            #si el usuario a creado un tag de alguna peli que sea igual a alguno de el de la pelicula buscada filtramos mas el df quitando los nulos
            #si no a hecho ningun tag y todos sus tag son nan dejamos el df como esta ya que si hacemos dropna eliminamos el df entero
            if user_ratings.dropna().size != 0:
                user_ratings = user_ratings.dropna()

            # calcular la media de valoraciones del usuario para las peliculas con generos en comun
            if user_ratings.empty:
                print()
                return "Vacia"
            else:
                #prediction = user_ratings_ID['rating'].mean()
                prediction = format(user_ratings['rating'].mean(), '.3f')

                return str(prediction)

    def recomedacionPorTagsUser(self, user_id, n_similares):
        user_id=int(user_id)
        n_similares=int(n_similares)
        df_movies_rating_user = self.df_movies_ratings[self.df_movies_ratings['userId']==user_id]
        self.df_movies[~self.df_movies.movieId.isin(df_movies_rating_user["movieId"])]
        df = pd.DataFrame()
        movies = self.df_movies[~self.df_movies.movieId.isin(df_movies_rating_user["movieId"])]
        df["movieId"] = movies["movieId"]
        df["title"] = movies["title"]
        df["ratingPredict"] = [self.predecirRatingDeUserAPeliculaPorSusTags(j["title"], user_id) for i,j in df.iterrows()]
        df = df.sort_values(by='ratingPredict', ascending = False)

        df_resultados = df["title"].head(n_similares)

        listaPeliculasRecomendadas = []

        for i in df_resultados:
            listaPeliculasRecomendadas.append(i)

        return listaPeliculasRecomendadas


from __future__ import unicode_literals

from django.utils.translation import ugettext_lazy as _
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
import Image as PILImage

from uds.core.util.stats import charts
from uds.core.auths.auth import webLoginRequired
from uds.core.util.decorators import denyBrowsers

import io
import six

from geraldo.generators.pdf import PDFGenerator
from geraldo import Report, landscape, ReportBand, ObjectValue, SystemField, BAND_WIDTH, Label
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.lib.enums import TA_RIGHT, TA_CENTER

import logging

logger = logging.getLogger(__name__)

__updated__ = '2015-04-27'

class TestReport(Report):
    title = 'Test report'
    author = 'UDS Enterprise'

    print_if_empty = True
    page_size = A4
    margin_left = 2 * cm
    margin_top = 0.5 * cm
    margin_right = 0.5 * cm
    margin_bottom = 0.5 * cm

    class band_detail(ReportBand):
        height = 0.5 * cm
        elements = (
            ObjectValue(attribute_name='name', left=0.5 * cm),
            ObjectValue(attribute_name='age', left=3 * cm,
                        get_value=lambda instance: six.text_type(instance['age'])),
        )

    class band_page_header(ReportBand):
        height = 1.3 * cm
        elements = [
            SystemField(expression='%(report_title)s', top=0.1 * cm, left=0, width=BAND_WIDTH,
                        style={'fontName': 'Helvetica-Bold', 'fontSize': 14, 'alignment': TA_CENTER}),
            Label(text="ID", top=0.8 * cm, left=0.5 * cm),
            Label(text=u"Creation Date", top=0.8 * cm, left=3 * cm),
            SystemField(expression=_('Page %(page_number)d of %(page_count)d'), top=0.1 * cm,
                        width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
        ]
        borders = {'bottom': True}

    class band_page_footer(ReportBand):
        height = 0.5 * cm
        elements = [
            Label(text='Geraldo Reports', top=0.1 * cm),
            SystemField(expression=_('Printed in %(now:%Y, %b %d)s at %(now:%H:%M)s'), top=0.1 * cm,
                        width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
        ]
        borders = {'top': True}

@denyBrowsers(browsers=['ie<9'])
@webLoginRequired(admin='admin')
def usage(request):
    resp = HttpResponse(content_type='application/pdf')

    family = [
        {'name': 'Leticia', 'age': 29, 'weight': 55.7, 'genre': 'female', 'status': 'parent'},
        {'name': 'Marinho', 'age': 28, 'weight': 76, 'genre': 'male', 'status': 'parent'},
        {'name': 'Tarsila', 'age': 4, 'weight': 16.2, 'genre': 'female', 'status': 'child'},
        {'name': 'Linus', 'age': 0, 'weight': 1.5, 'genre': 'male', 'status': 'child'},
        {'name': 'Mychelle', 'age': 19, 'weight': 50, 'genre': 'female', 'status': 'nephew'},
        {'name': 'Mychell', 'age': 17, 'weight': 55, 'genre': 'male', 'status': 'niece'},
    ]

    report = TestReport(queryset=family)
    report.generate_by(PDFGenerator, filename=resp)
    return resp
    # return HttpResponse(pdf, content_type='application/pdf')


# Qus:https://leetcode.com/problems/maximal-rectangle/

# time complexity is O(n*2n)
# 2*n for calculating max Rectangle for a row
# n for number of rows in the matrix
def largestRectangleArea(h):
        """
        :type heights: List[int]
        :rtype: int
        """
        # try to draw a graph first
        # remember it is similar to brute force approack
        # formulat to calculate area is (i-stack[-1]-1)*h[pop] after poping out the               # element
        # in case of empty stack , means it is the smallest element till now so
        # formula for it is (i)*h[pop]
        stack=[]
        i=0
        max1=0

        while(i<len(h)):
            if(stack==[] or h[stack[-1]]<=h[i]):
                stack.append(i)
                i+=1
            else:
                li=stack.pop()
                if(stack==[]):
                    max1=max(max1,(i)*h[li])
                else:

                    max1=max(max1,(i-stack[-1]-1)*h[li])

        while(stack):
            li=stack.pop()
            if(stack==[]):
                max1=max(max1,(i)*h[li])
            else:
                max1=max(max1,(i-stack[-1]-1)*h[li])

        return max1

class Solution(object):
    def maximalRectangle(self, matrix):
        """
        :type matrix: List[List[str]]
        :rtype: int
        """
        if(len(matrix)==0 or len(matrix[0])==0):
            return 0

        max1=0
        h=[0 for i in range(len(matrix[0]))]

        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                # make sure the matrix elemnts is string

                if(matrix[i][j]=='1'):

                    h[j]+=1
                else:
                    h[j]=0
            # we have a calculated height arr for row i
            # now can can pass to largestRectangleArea to calculate what is the
            # largest rectangle possible
            # same histrogram approach to calculate matrix rectangle area
            max1=max(max1,largestRectangleArea(h))

        return max1


#!/usr/bin/env python3
# coding  : utf-8
# @author : Francis.zz
# @date   : 2021-06-30 15:25
# @desc   : 创建线程池

from multiprocessing import Pool
from concurrent.futures import ThreadPoolExecutor
import os, time, random

def call_func(name):
    print('Run task %s (%s)...' % (name, os.getpid()))

# 调用cmd命令的任务函数
def call_api(filename):
    cmd_str = f'Call xx.exe -o {filename} -r "xx.py"'
    print(cmd_str)
    time.sleep(2)

if __name__ == '__main__':
    # 创建线程池
    # p = Pool(4)
    # for i in range(5):
    #     p.apply_async(call_func, args=(i,))
    # p.close()
    # p.join()
    # print('All subprocesses done.')
    #
    # # 调用cmd命令
    # port = 443
    # ipconfig = os.popen(f"netstat -ant|findstr :{port}")
    # info = ipconfig.readlines()
    # for line in info:  # 按行遍历
    #     line = line.strip('\r\n')
    #     print(line)
    #
    # exe = "D://cam.exe"
    # file = "x.qjp"
    # cmd = f'Call {exe} -o "{file}" -r "x.py"'
    # print(cmd)

    # 开启线程池，参数可以指定线程的数量
    pool = ThreadPoolExecutor(5)

    # 遍历文件目录
    g = os.walk('D:\python_code\config1')

    for i in range(10):
        pool.submit(call_api, i)

    # wait 为 True时会等待所有的任务完成才会返回
    pool.shutdown(wait=True)

    print("执行完成...")


def projectionArea(grid: list[list[int]]) -> int:
    projection_x = sum(max(i) for i in grid)
    projection_z = sum(j > 0 for i in grid for j in i)
    projection_y = sum(max(s[i] for s in grid) for i in range(len(grid)))

    return sum([projection_x, projection_y, projection_z])

grid = [[1, 2], [3, 4]]
# grid = [[1, 0], [0, 2]]
print(projectionArea(grid))


# Opus/UrbanSim urban simulation software.
# Copyright (C) 2010-2011 University of California, Berkeley, 2005-2009 University of Washington
# See opus_core/LICENSE

from urbansim.datasets.dataset import Dataset as UrbansimDataset
from opus_core.datasets.dataset_factory import DatasetFactory
from opus_core.storage_factory import StorageFactory
from numpy import array, allclose
from opus_core.misc import unique

class FazPersonsDataset(UrbansimDataset):

    id_name_default = 'dummy_id'
    in_table_name_default = "faz_persons"
    out_table_name_default = "faz_persons"
    dataset_name = "faz_persons"

    def __init__(self, **kwargs):
        fazes, households = self._try_load_datasets(**kwargs)
        if fazes is not None:
            faz_id = fazes.get_id_attribute()
            persons = unique( households.get_attribute("persons") )
            combine_array = array( [[x, y]  for x in faz_id for y in persons] )
            storage = StorageFactory().get_storage('dict_storage')
            storage.write_table(table_name=self.in_table_name_default,
                                table_data={
                                            'dummy_id'   :combine_array[:, 0] * 100 + combine_array[:,1],
                                            'faz_id'     :combine_array[:, 0],
                                            'persons'    :combine_array[:, 1],
                                            }
                                )

            UrbansimDataset.__init__(self, in_storage=storage,
                                     #in_table_name=self.in_table_name_default,
                                     #id_name=self.id_name_default,
                                     #dataset_name=self.dataset_name
                                 )
        else:
            UrbansimDataset.__init__(self, **kwargs)

    def _try_load_datasets(self, **kwargs):
        try:
            fazes = DatasetFactory().get_dataset("faz",
                                                     package='urbansim',
                                                     arguments=kwargs)
            households = DatasetFactory().get_dataset("household",
                                                     package='urbansim',
                                                     arguments=kwargs)

            fazes.load_dataset()
            households.load_dataset(attributes='*')
        except:
            fazes = None
            households = None
        return (fazes, households)

from opus_core.tests import opus_unittest
class DistrictCommuteDatasetTests(opus_unittest.OpusTestCase):
    def test_input(self):
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(table_name='fazes',
                            table_data={
                                        'faz_id'     :array([1,3,4]),
                                        }
                            )
        storage.write_table(table_name='households',
                            table_data={
                                        'household_id'  :array([1,2,3]),
                                        'persons'       :array([1,3,4]),
                                        }
                            )

        dc = DatasetFactory().get_dataset('faz_persons',
                                          package='psrc_parcel',
                                          arguments={'in_storage':storage}
                                          )

        self.assertEqual(dc.get_id_name(), ['dummy_id'])
        self.assertTrue( "faz_id" in dc.get_known_attribute_names())
        self.assertTrue( "persons" in dc.get_known_attribute_names())
        self.assertEqual(dc.size(), 9)
        self.assertTrue(allclose(dc.get_id_attribute(), array([
                                                       101,
                                                       103,
                                                       104,
                                                       301,
                                                       303,
                                                       304,
                                                       401,
                                                       403,
                                                       404])
                                    ))

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


# in aridity_comparison2

def IA_two(T):
    if np.isnan(T):
        return np.nan
    elif ((T == 1) or (T == 2) or (T == 3) or (T == 4)):
        return 10
    else:
        return 20

def RA_two(T):
    if np.isnan(T):
        return np.nan
    elif (T == 1) or ((T == 2) or (T == 3) or (T == 4) or (T == 5)):
        return 10
    else:
        return 20

def RAIA(Tra, Tia):
    if (np.isnan(Tra)) or (np.isnan(Tia)):
        return np.nan
    elif (Tra == 1) and (Tia == 1):
        return 10
    elif (Tra == 2) and (Tia == 2):
        return 20
    elif (Tra == 1) and (Tia == 2):
        return 30
    elif (Tra == 2) and (Tia == 1):
        return 40

# in aridity_changes

def similar_subtypes(x):
    if np.std(x) == 0:
        value = 0
    else:
        value = np.max(x) - np.min(x)

    if value >= 2:
        value = 2

    return value

def change_of_RA(present, future):
    if np.isnan(present) or np.isnan(future):
        response = np.nan
    else:
        present = int(present)
        future = int(future)
        if present == future:
            value = 9
        elif (present == 1) and (future == 2):
            value = 1
        elif (present == 2) and (future == 3):
            value = 2
        elif (present == 3) and (future == 4):
            value = 3
        elif (present == 4) and (future == 5):
            value = 4
        elif (present == 5) and (future == 6):
            value = 5
        elif (present == 6) and (future == 7):
            value = 6
        elif (present == 7) and (future == 8):
            value = 7
        elif (present == 8) and (future == 9):#
            value = 8
        elif (present == 2) and (future == 1):
            value = 10
        elif (present == 3) and (future == 2):
            value = 11
        elif (present == 4) and (future == 3):
            value = 12
        elif (present == 5) and (future == 4):
            value = 13
        elif (present == 6) and (future == 5):
            value = 14
        elif (present == 7) and (future == 6):
            value = 15
        elif (present == 8) and (future == 7):
            value = 16
        elif (present == 9) and (future == 8):
            value = 17
        elif (present-future) <= -2:
            value = 0
        else:
            value = 18

        response = value

    return response