'''
Created on 09.03.2019

@author: Nicco
'''

from logger.logger import logger
from ego.simple_ego import simple_ego
import math

class path_base():
    def __init__(self):
        '''
        Constructor
        '''
        self.grid = []
        self.grid_size=[]
        self.heuristic = []
        self.open = []

    def set_grid_size(self, size):
        self.grid_size = size
        for i in range(self.grid_size[0]):
            self.grid.append([])
            for j in range(self.grid_size[1]):
                self.grid[i].append(0)

    def set_heuristic(self,init,target):
        """
        set up the heuristic for the search pattern
        """
        self.logger.write_log("init the heuristic function")
        for i in range(self.grid_size[0]):
            self.heuristic.append([])
            for j in range(self.grid_size[1]):
                distance = 0
                distance = (target[0]-init[0])+(target[1]-init[1])
                distance = distance - i - j
                self.heuristic[i].append(abs(distance))

        for line in self.heuristic:
            msg = ""
            for ele in line:
                if ele <= 9:
                    msg = msg+",  " + str(ele)
                else:
                    msg = msg+", " + str(ele)
            self.logger.write_log(msg[2:])

    def set_obsticals(self, static, dynamic):
        """
        set the obsticals both static an dynamic
        static and dynamic obsticals have these attributes in common

        array [ y1, x1, y2, x2, id]
        """
        for  obj in static:
            if obj[0]<0 or obj[0]>len(self.grid):
                self.logger.write_log("object out of boundarys: " + str(obj))
            elif obj[1]<0 or obj[1]>len(self.grid[0]):
                self.logger.write_log("object out of boundarys: " + str(obj))
            elif obj[2]<0 or obj[2]>len(self.grid):
                self.logger.write_log("object out of boundarys: " + str(obj))
            elif obj[3]<0 or obj[3]>len(self.grid[0]):
                self.logger.write_log("object out of boundarys: " + str(obj))
            else:
                for i in range(obj[2]-obj[0]):
                    for j in range(obj[3]-obj[1]):
                        y = obj[0] + i
                        x = obj[1] + j
                        self.grid[y][x] = -1

        for  obj in dynamic:
            if obj[0]<0 or obj[0]>len(self.grid):
                self.logger.write_log("object out of boundarys: " + str(obj))
            elif obj[1]<0 or obj[1]>len(self.grid[0]):
                self.logger.write_log("object out of boundarys: " + str(obj))
            elif obj[2]<0 or obj[2]>len(self.grid):
                self.logger.write_log("object out of boundarys: " + str(obj))
            elif obj[3]<0 or obj[3]>len(self.grid[0]):
                self.logger.write_log("object out of boundarys: " + str(obj))
            else:
                for i in range(obj[2]-obj[0]):
                    for j in range(obj[3]-obj[1]):
                        y = obj[0] + i
                        x = obj[1] + j
                        self.grid[y][x] = -1
    def get_grid(self):
        return self.grid

class path_dynamic_prog(path_base):
    '''
    classdocs
    '''

    def __init__(self):
        '''
        Constructor
        '''
        path_base.__init__(self)
        self.logger = logger("path_dynamic_prog")
        self.value_grid = []
        self.ego = simple_ego()

    def compute_value(self,goal,cost):
        # ----------------------------------------
        # compute the values of the single grid cells
        # ----------------------------------------

        delta = [[-1, 0 ], # go up
                 [ 0, -1], # go left
                 [ 1, 0 ], # go down
                 [ 0, 1 ]] # go right
        delta_name = ['^', '<', 'v', '>']

        value = [[0 for row in range(len(self.grid[0]))] for col in range(len(self.grid))]
        closed = [[0 for row in range(len(self.grid[0]))] for col in range(len(self.grid))]
        policy = [[" " for row in range(len(self.grid[0]))] for col in range(len(self.grid))]

        x = goal[0]
        y = goal[1]
        self.ego.set_position(x,y,"N")
        closed[x][y]=1
        g = 1
        found = False
        resign = False
        count = 1
        option = [[g,x,y]]
        max_iter = len(self.grid[0])*len(self.grid)+10
        while found == False and resign == False and count <max_iter :

            count = count + 1
            if len(option)== 0:
                resign = True
            option.sort()
            option.reverse()
            if len(option) == 0:
                resign = True
                break
            var = option.pop()
            x = var[1]
            y = var[2]
            if x==0 and y==0:
                found = True
            else:
                for i in range(len(delta)):
                    x2 = x + delta[i][0]
                    y2 = y + delta[i][1]
                    if x2 >= 0 and x2 < len(self.grid) and y2 >=0 and y2 < len(self.grid[0]):

                        if closed[x2][y2] == 0:
                            if self.grid[x2][y2]!=0:
                                value[x2][y2] = 999
                            else:
                                g = value[x][y] +cost[i]
                                value[x2][y2] = g
                                option.append([g,x2,y2])
                            closed[x2][y2] = 1

        #get the policy for the movements
        for x in range(len(self.grid)):
            for y in range(len(self.grid[0])):
                if value[x][y] < 999 and value[x][y] >0:
                    #policy[x][y] = "x"
                    lowest_val2 = 999
                    target = 0
                    # update the ego position to geht the correct movement options
                    #delta, delta_name = self.ego.get_move_options()

                    for i in range(len(delta)):
                        #self.ego.update_position([x,y])
                        x2 = x + delta[i][0]
                        y2 = y + delta[i][1]
                        if x2 >= 0 and x2 < len(self.grid) and y2 >=0 and y2 < len(self.grid[0]):
                            if value[x2][y2] < lowest_val2:
                                lowest_val2 = value[x2][y2]
                                if value[x][y] > lowest_val2:
                                    target = i
                                    t_x = x2
                                    t_y = y2
                    policy[x][y]= delta_name[target]
                    #self.ego.update_position([x + delta[i][0],y + delta[i][1]])

        policy[goal[0]][goal[1]] ="*"

        self.value_grid = value

        for i in range(len(value)):
            for j in range(len(value[0])):
                if value[i][j] == 999:
                    value[i][j] = "###"
                    policy[i][j] = "###"
                elif value[i][j] == 0:
                    if i == goal[0] and j == goal[1]:
                        value[i][j] = "GOA"
                        policy[i][j] = "GOA"
                    else:
                        value[i][j] = "###"
                        policy[i][j] = "###"

        return value,policy

class path_a_star(path_base):
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
        path_base.__init__(self)
        self.logger = logger("path_a_star")
        self.logger.write_log("init search A*")

    def search(self, init, goal, cost):
        # ----------------------------------------
        # modify the code below
        # init is the current position
        # ----------------------------------------
        delta_name = ['^', '<', 'v', '>']
        delta = [[-1, 0 ], # go up
                 [ 0, -1], # go left
                 [ 1, 0 ], # go down
                 [ 0, 1 ]] # go right
        closed = [[0 for col in range(len(self.grid[0]))] for row in range(len(self.grid))]
        closed[init[0]][init[1]] = 1

        expand = [[-1 for col in range(len(self.grid[0]))] for row in range(len(self.grid))]
        action = [[-1 for col in range(len(self.grid[0]))] for row in range(len(self.grid))]

        x = init[0]
        y = init[1]
        g = 0 #+ self.heuristic[x][y]
        f = g + self.heuristic[x][y]

        open = [[f, g, x, y]]

        found = False  # flag that is set when search is complete
        resign = False # flag set if we can't find expand
        count = 0

        while not found and not resign:
            #self.logger.write_log("iteration:" +str(count))
            if len(open) == 0:
                resign = True
                #return "Fail"
                return expand,action
            else:
                open.sort()
                open.reverse()
                next = open.pop()
                x = next[2]
                y = next[3]
                g = next[1]#+heuristic[x][y]
                f = next[0]
                expand[x][y] = count
                count += 1

                if x == goal[0] and y == goal[1]:
                    found = True
                    action[x][y]=" X "
                else:
                    for i in range(len(delta)):
                        x2 = x + delta[i][0]
                        y2 = y + delta[i][1]
                        if x2 >= 0 and x2 < len(self.grid) and y2 >=0 and y2 < len(self.grid[0]):
                            if closed[x2][y2] == 0 and self.grid[x2][y2] == 0:
                                f = g + cost[i] + self.heuristic[x2][y2]
                                open.append([f,g+1, x2, y2])
                                closed[x2][y2] = 1
                                action[x][y] = delta_name[i]
        return expand,action

class path_hyprid_a_star():
    '''
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.ego = simple_ego()

    def get_new_xy(self,x,y, v,delta_t, phi,str_angle):
        t = 1
        delta_w = (v/self.ego.length)*math.tan(str_angle)

        x_new = x + v * (delta_t/t)*math.cos(phi)
        y_new = y + v * (delta_t/t)*math.sin(phi)
        phi_new = phi + (delta_t/t)*delta_w

        return x_new, y_new, phi_new


import os
import torch

from xml.etree import ElementTree
from xml.dom import minidom
import collections
import shutil
from random import randrange, sample
from PIL import Image
from tqdm import tqdm
import numpy as np

classes = ["D00", "D10", "D20", "D40"]
idx_to_class = {i:j for i, j in enumerate(classes)}
class_to_idx = {value:key for key,value in idx_to_class.items()}

chosen_countries = ["Norway", "Japan", "United_States"]

dataset_path = "../../../projects/vc/courses/TDT17/2022/open/RDD2022/"

folder_name = "RDD_YOLO_PREPROCESSING"

not_labels = 0
labels = 0

if not os.path.exists(f"{folder_name}"):
   os.makedirs(f"{folder_name}")
   os.makedirs(f"{folder_name}/train/images")
   os.makedirs(f"{folder_name}/train/labels")
   os.makedirs(f"{folder_name}/val/images")
   os.makedirs(f"{folder_name}/val/labels")

count = 0
for country in tqdm(os.listdir(dataset_path), desc=f"Country: "):
  if country.endswith(".zip"):
    continue
  if country not in chosen_countries:
    continue
  print(f"Creating annotations for country {country}")
  for image in tqdm(os.listdir(f"{dataset_path}/{country}/train/images"), desc=f"Image in {country}: "):
    image_name = image.split(".")[0]
    annotation_path = f"{dataset_path}/{country}/train/annotations/xmls/{image_name}.xml"

    try:
      infile_xml = open(annotation_path)
    except FileNotFoundError:
      continue
    tree = ElementTree.parse(infile_xml)
    root = tree.getroot()

    print_buffer = []

    im = Image.open(f"{dataset_path}/{country}/train/images/{image}").convert('RGB')
    im_w, im_h = im.size
    for obj in root.iter('object'):
      damage_type = obj.find("name").text
      bbox = obj.find("bndbox")
      xmin = int(float(bbox.find("xmin").text))
      ymin = int(float(bbox.find("ymin").text))
      xmax = int(float(bbox.find("xmax").text))
      ymax = int(float(bbox.find("ymax").text))

      center_x = (xmin + xmax) / 2
      center_y = (ymin + ymax) / 2
      width = xmax - xmin
      height = ymax - ymin

      if damage_type in class_to_idx:
        class_id = class_to_idx[damage_type]

        print_buffer.append("{} {} {} {} {}".format(class_id, center_x/im_w, center_y/im_h, width/im_w, height/im_h))
    if len(print_buffer) > 0:
      labels += 1
    else:
      not_labels += 1
    if len(print_buffer) > 0 or not_labels < 200:
      options = ["train", "val"]
      weights = [0.8, 0.2]

      data_type = np.random.choice(options, p=weights)
      annotation_file = open(f"{folder_name}/{data_type}/labels/{image_name}.txt", "w")
      annotation_file.write("\n".join(print_buffer))
      annotation_file.close()
      im.save(f"{folder_name}/{data_type}/images/{image_name}.jpg", "JPEG")
      count += 1
print("Done annotating file")

TDT17 - Visual Intelligence/TDT17-Mini-Project/yolo_annotations.py


# def myPrint(st:str):
#     print(st.center(20,"#"))
# myPrint("hello green")

# def add(a,b:int=10):
#     return a+b
# print(add(10,20))
# print(add(a=30)) #可以传参指定名称

# def multi(a,b):
#     return a+b,a*b #可以return多个值
# c,d=multi(1,2)
# print(c,d)

t=4
def double(a):
    global t  #声明全局变量 相当于在全局外部声明了t=2
    t=2
    return t*a
print(double(23),t)

k=30
def addk(t):
    k=10+t
    return(k)
print(addk(7),k)


import numpy as np
from mxnet.gluon.loss import SoftmaxCrossEntropyLoss

__all__ = ["RingLoss"]

numeric_types = (float, int, np.generic)

def _apply_weighting(F, loss, weight=None, sample_weight=None):
    """Apply weighting to loss.

    Parameters
    ----------
    loss : Symbol
        The loss to be weighted.
    weight : float or None
        Global scalar weight for loss.
    sample_weight : Symbol or None
        Per sample weighting. Must be broadcastable to
        the same shape as loss. For example, if loss has
        shape (64, 10) and you want to weight each sample
        in the batch separately, `sample_weight` should have
        shape (64, 1).

    Returns
    -------
    loss : Symbol
        Weighted loss
    """
    if sample_weight is not None:
        loss = F.broadcast_mul(loss, sample_weight)

    if weight is not None:
        assert isinstance(weight, numeric_types), "weight must be a number"
        loss = loss * weight

    return loss

class RingLoss(SoftmaxCrossEntropyLoss):
    """Computes the Ring Loss from
    `"Ring loss: Convex Feature Normalization for Face Recognition"
    <https://arxiv.org/abs/1803.00130>`_paper.

    .. math::
        p = \softmax({pred})

        L_SM = -\sum_i \log p_{i,{label}_i}

        L_R = \frac{\lambda}{2m} \sum_{i=1}^{m} (\Vert \mathcal{F}({x}_i)\Vert_2 - R )^2

    Parameters
    ----------
    lamda: float
        The loss weight enforcing a trade-off between the softmax loss and ring loss.

    Outputs:
        - **loss**: loss tensor with shape (batch_size,). Dimensions other than
          batch_axis are averaged out.

    """

    def __init__(self, lamda, weight_initializer=None, dtype='float32',
                 axis=-1, sparse_label=True, weight=None, batch_axis=0, **kwargs):
        super().__init__(axis=axis, sparse_label=sparse_label, weight=weight, batch_axis=batch_axis, **kwargs)

        self._lamda = lamda
        self.R = self.params.get('R', shape=(1,), init=weight_initializer,
                                 dtype=dtype, allow_deferred_init=True)

    def hybrid_forward(self, F, pred, label, embedding, R, sample_weight=None):
        # RingLoss
        emb_norm = F.norm(embedding, axis=1)
        loss_r = F.square(F.broadcast_sub(emb_norm, R))
        loss_r = loss_r * 0.5
        loss_r = _apply_weighting(F, loss_r, self._weight, sample_weight)

        # Softmax
        loss_sm = super().hybrid_forward(F, pred, label, sample_weight)

        return loss_sm + self._lamda * loss_r


from oort.shared.utils import find_first_in_list

def test_find_item_in_valid_list():
    calibrations = [{'uuid': '1', 'name': 'Biases', 'datasets': '10'},
                    {'uuid': '2', 'name': 'Biases', 'datasets': '20'}]
    assert (find_first_in_list(calibrations, name='Biases', uuid='2') == calibrations[1])

def test_find_observation_skipping_name_success():
    observations = [{'uuid': '38c1391f-f8a4-4000-b688-e5bc12a3b463', 'index': 0,
                     'night_log': '07dd5758-cebe-46fb-a888-2fb068747a51', 'target_name': '',
                     'target_class': 'AstronomicalObject',
                     'dataset': 'b4bbd718-93b6-4efe-8aa6-69c6be9e8845'}]

    kwargs = {'name': '[HD 5980] V', 'night_log': '07dd5758-cebe-46fb-a888-2fb068747a51'}

    # Name is not being part of the keys of the observation and must not be used in filtering.
    # However, if name is skipped, one must ensure tests is still effective, that is,
    # remaining provided keys in kwargs are enough to get a meaning ful result.
    assert (find_first_in_list(observations, **kwargs) == observations[0])

def test_find_observation_skipping_keys_failure():
    observations = [{'uuid': '38c1391f-f8a4-4000-b688-e5bc12a3b463', 'index': 0,
                     'night_log': '07dd5758-cebe-46fb-a888-2fb068747a51', 'target_name': '',
                     'target_class': 'AstronomicalObject',
                     'dataset': 'b4bbd718-93b6-4efe-8aa6-69c6be9e8845'}]

    kwargs = {'name': '[HD 5980] V'}
    # Name will be skipped because it doesn't exists in observation. But no keys are left
    # for comparison, hence the result must be None.
    assert find_first_in_list(observations, **kwargs) is None


#Program to capture a video from the default camera (0), compute the 2D FFT
#on the Green component, take the magnitude (phase) and display it live on the screen
#Gerald Schuller, Nov. 2014
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
#Get size of frame:
[retval, frame] = cap.read()
[r,c,d]=frame.shape
print(r,c)

#Mask to set to zero the 3/4 highest frequencies,
#only kep the 1/4 lowest frequencies in each direction:

#For rows:
Mr=np.ones((r,1))
Mr[(r/8.0):(r-r/8.0),0]=np.zeros((3.0/4.0*r))
#For columns:
Mc=np.ones((1,c))
Mc[0,(c/8.0):(c-c/8)]=np.zeros((3.0/4.0*c));
#Together:
M=np.dot(Mr,Mc)

while(True):
    # Capture frame-by-frame
    [retval, frame] = cap.read()
    cv2.imshow('Original Video, Gruen Komponente',frame[:,:,1])

    #compute magnitude of 2D FFT of green component
    #with suitable normalization for the display:
    X=np.fft.fft2(frame[:,:,1]/255.0)
    #Set to zero the 7/8 highest spacial frequencies in each direction:
    X=X*M
    frame=np.abs(X)/512.0
    #angle/phase:
    #frame=(3.14+np.angle(np.fft.fft2(frame[:,:,1]/255.0)))/6.28
    # Display the resulting frame
    cv2.imshow('2D-FFT mit Null Setzen der hoechsten Ortsfrequenzen',frame)
    #Inverse FFT, with abs to turn complex into float numbers:
    x=np.abs(np.fft.ifft2(X))
    cv2.imshow('Inverse 2D FFT ohne die hoechsten Ortsfrequenzen', x)

    #Keep window open until key 'q' is pressed:
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()


# coding=utf-8
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import math
import argparse
from scipy.linalg import block_diag
from mpl_toolkits.mplot3d import Axes3D

# Define Function u

def u(x, y):
    res = (np.sinh(np.pi * x) / np.sinh(np.pi)) * np.sin(np.pi * y)
    return res

def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("n", nargs='?', type=int)
    args = parser.parse_args()

    n = args.n

    x = np.linspace(0, 1, n + 1)
    y = np.linspace(0, 1, n + 1)

    G = np.zeros((n - 1) ** 2)
    # 右辺の行列式の作成
    for i in range(n - 1):
        G[i] += 0
        G[(n-2) * (n-1) + i] += 0
        G[(n-1) * i] += 0
        G[(n-1) * i + (n-2)] = (1 / 4) * np.sin(np.pi * (y[i + 1]))
    #　係数行列式の作成
    Coef = np.eye((n - 1) ** 2)

    for j in range(n - 1):
        for k in range(1, n - 1):
            Coef[j * (n - 1) + k][j * (n - 1) + k - 1] = -1/4
            Coef[j * (n - 1) + k - 1][j * (n - 1) + k] = -1/4

    for j in range(1, n - 1):
        for k in range(n - 1):
            Coef[j * (n - 1) + k][(j - 1) * (n - 1) + k] = -1/4
            Coef[(j - 1) * (n - 1) + k][j * (n - 1) + k] = -1/4

    # X = A-1 * G
    res = np.linalg.inv(Coef).dot(G.T)
    res = res.reshape(n - 1, n - 1)
    res = block_diag(0.0, res, 0.0)
    # 境界条件を代入
    res[:, n] = np.sin(np.pi * y)
    print("n = %d 差分法の結果" % (n))
    print(res)

    Acc = np.zeros([(n + 1), (n + 1)])

    for i in range(1, n):
        for j in range(1, n):
            Acc[j, i] = u(x[i], y[j])
    # 境界条件を代入
    Acc[:, n] = np.sin(np.pi * y)
    print("n = %d 精確値" % (n))
    print(Acc)

    fig = plt.figure()
    ax = fig.add_subplot(1, 2, 1, projection='3d')
    x, y = np.meshgrid(x, y)

    plot1 = ax.plot_surface(x, y, res, label="差分法",
                            cmap="rainbow")
    fig.colorbar(plot1, shrink=0.5)
    ax.text2D(0.05, 0.95, "FDM", transform=ax.transAxes)

    ax = fig.add_subplot(1, 2, 2, projection='3d')
    plot2 = ax.plot_surface(x, y, Acc, label="精確値",
                            cmap="rainbow")
    fig.colorbar(plot2, shrink=0.5)

    ax.text2D(0.05, 0.95, "Exact Solution", transform=ax.transAxes)
    plt.show()

    plt.subplot(121)
    plt.title("FDM")
    plt.imshow(res,
               cmap="Blues",
               interpolation="nearest")

    plt.subplot(122)
    plt.title("Exact Solution")
    plt.imshow(Acc,
               cmap="Blues",
               interpolation="nearest")
    plt.show()

if __name__ == "__main__":
    main()


import csv

class GetUniqueEmailPatterns:
    """
    Returns all unique email conventions from data.csv
    """

    @staticmethod
    def get_unique_email_patterns():
        patterns = set()
        with open('data.csv', newline='') as file:
            csv_reader = csv.reader(file)
            for row in csv_reader:
                patterns.add(row[2].split('@')[0])
        return patterns


# 다리 놓기
# 23.05.30
# https://www.acmicpc.net/problem/1010

import sys
input = sys.stdin.readline

def dp(N, M):
    num = [1] * M
    for i in range(N - 1):
        prefix = [0]
        for n in num[:-1]:
            prefix.append(prefix[-1] + n)
        num = prefix
    return sum(num)

T = int(input())
for _ in range(T):
    N, M = map(int, input().split())
    print(dp(N, M))


from http.client import InvalidURL
from flask import Blueprint, render_template, request, flash
from urllib.request import urlopen
from urllib.error import *
import requests
from bs4 import BeautifulSoup

views = Blueprint('views', __name__)

@views.route('/', methods=['GET', 'POST'])
def home():
    result_list = []
    if request.method == 'POST':
        search = request.form.get('search')
        result_list = getSearch(search)

    return render_template("home.html", results=result_list)

@views.route('/choose-cities', methods=['GET', 'POST', 'DELETE'])
def cities():
    if request.method == 'POST':
        city = request.form.get('city')

        # check if the city exists and add it to list
        city = city.replace(" ", "")
        url = "https://" + city + ".craigslist.org/"
        try:
            html = urlopen(url)
        except HTTPError as e:
            print("City does not exist", e)
        except URLError as e:
            print("City does not exist", e)
        except InvalidURL as e:
            print("City does not exist", e)
        else:
            cityList.append(city)
            print('City added')

    # delete cities, not functioning currently
    if request.method == 'DELETE':
        deleteCities()
        print("delete")

    return render_template("cities.html", cities=getCities())

# city list stuff
cityList = []

def getCities():
    return cityList

def deleteCities():
    cityList.clear()
    print("clearing list")

class Result:
    title = ""
    price = ""
    location = ""
    photo = ""
    link = ""

    def __init__(self, title, price, location, photo, link):
        self.title = title
        self.price = price
        self.location = location
        self.photo = photo
        self.link = link

    def make_result(title,  price, location, photo, link):
        result = Result(title,  price, location, photo, link)
        return result

# function to search craigslist
def getSearch(search):
    result_list = []
    for city in cityList:
        print(city)
        URL = "https://" + city + ".craigslist.org/search/sss?query=" + search
        page = requests.get(URL)
        soup = BeautifulSoup(page.content, "html.parser")
        results = soup.findAll('li', class_='result-row')

        for result in results:
            # get the metadata for an ad
            title = result.find('h3').text
            price = result.find('span').text
            try:
                location = result.find('span', class_='result-hood').text
            except AttributeError as e:
                location = ""
            link = result.find('a').attrs['href']
            rpage = requests.get(result.find('a').attrs['href'])
            rsoup = BeautifulSoup(rpage.content, "html.parser")
            try:
                photo = rsoup.find('img').attrs['src']
            except AttributeError as e:
                photo = "No Image"
            result = Result(title, price, location, photo, link)
            result_list.append(result)

    return result_list


import sys
sys.path.insert(0, '..')
from BackEnd import data, protocol, broadcaster, listener, sender, receiver
import os

def recon():
	ans = input('Do you want to receive ' + Data.fileName + ' sent from ' + Data.reqName + ' ?(y/n)')
	accept = False
	if ans == 'y':
		accept = True
	receiver.finishRecon(accept, restartListen)

def restartListen():
	receiver.startListen(recon)

def choose():
	print \
		('''
0. Send File
1. Check the receiver list
2. Change username
3. exit
''')
	ans = input('input the number: ')
	if ans == '0':
		return 0
	if ans == '1':
		return 1
	if ans == '2':
		return 2
	if ans == '3':
		return 3

def choosesend():
	count = 0
	IPList = []
	for item in Data.receiverList:
		print (str(count) + ' ' + Data.receiverList[item][0])
		IPList.append(item)
		count += 1
	ans = input('input the number you want to send to: ')
	num = int(ans)
	if num >= 0 and num < count:
		filename = input('input the file URI you want to send: ')
		if os.path.exists(filename):
			sender.startSend(IPList[num], filename, sender.sendFile)
		else:
			print("file doesn't exists")

def main():
	while True:
		ans = choose()
		if ans == 3:
			broadcaster.stopBroadcast()
			listener.stopListen()
			Data.save()
			print('exit')
			exit(0)
			break
		if ans == 2:
			newName = input('input new name: ')
			Data.setNewName(newName)
		if ans == 1:
			for item in Data.receiverList:
				print (Data.receiverList[item][0])
		if ans == 0:
			choosesend()

if __name__ == '__main__':
	Data = data.FrogDropData()
	Data.initial()
	broadcaster.startBroadcast()
	listener.startListen()
	receiver.startListen(recon)
	main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec  5 16:45:13 2019

@author: eleanorezimah
"""
#Ashwini Python Presentation

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
import seaborn as sns
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder,StandardScaler

#Scatter plot outlier analysis
def oplot(df):
    fig, ax = plt.subplots()
    ax.scatter(x = data['GrLivArea'], y = data['SalePrice'])
    plt.xlabel('GrLivArea', fontsize=13)
    plt.show()

#Distribution plot for target variable analysis
def distributionplot(df):
    plt.subplots(figsize=(12,9))
    sns.distplot(data['SalePrice'], bins=100, hist_kws={'alpha': 0.4})
    # Get the fitted parameters used by the function
    (mu, sigma) = stats.norm.fit(data['SalePrice'])
    # plot with the distribution
    plt.legend(['Normal dist. ($\mu=$ {:.2f} and $\sigma=$ {:.2f} )'.format(mu, sigma)], loc='best')
    plt.ylabel('Frequency')

#Filling Nan with random normal distirubtion values
def fillNaN_with_distribution(df):
    a = df.values
    m = np.isnan(a) # mask of NaNs
    mu, sigma = df.mean(), df.std()
    print(mu)
    print(sigma)
    a[m] = np.random.normal(mu, sigma, size=m.sum())
    return df

with open('Housing.txt') as fp:
        #open the file and read it into a list
        mydata = [line.strip().split(",") for line in fp]

#Data manupulation - To perform dataprofiling
Header = mydata.pop(0)
datan=np.asarray(mydata)
data = pd.DataFrame(datan,columns=Header)

cols = ['LotArea', 'HouseAge', 'TotalBsmtSF','1stFlrSF','2ndFlrSF','GrLivArea','SalePrice','NoBedroom']
data[cols] = data[cols].apply(pd.to_numeric, errors='coerce', axis=1)

#Step 1: Data profiling is concerned with summarizing your dataset through descriptive statistics
print(data['SalePrice'].describe())
print(data['GrLivArea'].describe())
print(data['HouseStyle'].describe())

#Step2 : Missing values
print(data.columns[data.isna().any()])
data = data.apply(lambda x: x.str.strip() if isinstance(x, str) else x).replace('', np.nan)
print(data.columns[data.isna().any()])
print('***************-- Final list')
X=data.values

#Handling missing data - Stragegy 1
imputer = SimpleImputer(missing_values= np.nan, strategy='mean')
imputer=imputer.fit(X[:, 2:3])
X[:, 2:3] = imputer.transform(X[:, 2:3])

dataf = pd.DataFrame(X,columns=Header)
print('***************')
print(dataf.columns[dataf.isna().any()])
print('***************')

#Handling missing data - Stragegy 2
dataf['LotArea']=fillNaN_with_distribution(data['LotArea'])
print('***************')
print(dataf.columns[dataf.isna().any()])
print('***************')

#handling missing data - Stragegy 3
permutation = np.random.permutation(dataf['RoofStyle'])

#erase the empty values
empty_is = np.where(permutation == "")
permutation = np.delete(permutation, empty_is)

#replace all empty values of the dataframe[field]
end = len(permutation)
dataf['RoofStyle'] = dataf['RoofStyle'].apply(lambda x: permutation[np.random.randint(end)] if pd.isnull(x) else x)

print('***************')
print(dataf.columns[dataf.isna().any()])
print('***************')

#Step 2 : Performing Visualization to understand data
del data['Id']
corr = data.corr()
plt.subplots(figsize=(20,9))
sns.heatmap(corr, annot=True)
plt.show()

dataf['HouseStyle'].value_counts().plot(kind='bar',title ="VISUALIZATION OF DIFFERENT HOUSESTYLE",figsize=(15,10),legend=True, fontsize=12)
plt.xlabel('House Style')
plt.ylabel('COUNT')
plt.show()

dataplot = data[['LotArea', 'HouseAge', 'TotalBsmtSF','1stFlrSF','2ndFlrSF','GrLivArea','SalePrice','NoBedroom']].copy()
for i in range(0, len(dataplot.columns), 5):
    sns.pairplot(data=dataplot,
                x_vars=dataplot.columns[i:i+5],
                y_vars=['SalePrice'])

#Step 3: Handling categorical variables
datacategorical = dataf[['HouseStyle', 'RoofStyle', 'BsmtQual','SaleCondition']].copy()
Xa=datacategorical.values
onehotencoder=OneHotEncoder()
Xa = onehotencoder.fit_transform(Xa).toarray()

#Step 4: Feature scaling
numericategory = dataf[['LotArea', 'HouseAge', 'TotalBsmtSF','1stFlrSF','2ndFlrSF','GrLivArea','NoBedroom']].copy()
Xb=numericategory.values
sc_Xb=StandardScaler()
Xb = sc_Xb.fit_transform(Xb)

#Step 5 : Handling Outliers
sns.boxplot(x=data['GrLivArea'])
oplot(data['GrLivArea'])
#Deleting outliers
data = data.drop(data[(data['GrLivArea']>4000) & (data['SalePrice']<300000)].index)
oplot(data['GrLivArea'])

#Target variable analysis
distributionplot(data['SalePrice'])
#Skewness is asymmetry in a statistical distribution, in which the curve appears distorted or skewed either to the left or to the right
#we use log for target variable to make more normal distribution , Outliers handled using transformation
data['SalePrice'] = np.log1p(data['SalePrice'])
distributionplot(data['SalePrice'])


import numpy as np
def ngramMaxDimKnown(Xd, gramsize, maxlen):
    X = list(map(str, Xd))
    numele = len(X)
    reqdim = maxlen - gramsize +1

    Y = []
    for i in X:
        tempy = []
        paddedi = i + "0"*(maxlen-len(i))
        for j in range(0,len(paddedi)-gramsize+1):
            tempy.append(paddedi[j:j+gramsize])
        if len(tempy) == 0:
            tempy = [paddedi]
        templeny = len(tempy)

        Y.append(tempy)
    return Y

def createNgramsPerSStable(thisssTableData, alltemptableData, mingram, maxgram):
    # mingram = 6
    # maxgram = 6
    initialDimension = alltemptableData.shape[1]

    extrapolatedData = thisssTableData
    findimlist = []
    for realFeature in range(initialDimension):
    #     not taking care of negative features
        thisFeatureData = thisssTableData[:,realFeature]
        maxlen = len(str(np.max(alltemptableData[:,realFeature])))
        dimlist = []
        for gramsize in range(mingram, min(maxgram,maxlen)+1):
#             print(thisFeatureData[0], gramsize, maxlen)
#             print(gramsize)
            thisFeatureDataOfGramSize = np.array(ngramMaxDimKnown(thisFeatureData, gramsize, maxlen),dtype=np.float)
#             print(thisFeatureDataOfGramSize[0])
#             print(thisFeatureDataOfGramSize.shape)
            reqdimForLater = thisFeatureDataOfGramSize.shape[1]
#             print(reqdimForLater)
            dimlist.append(reqdimForLater)
            extrapolatedData = np.append(extrapolatedData, thisFeatureDataOfGramSize, axis=1)
        findimlist += dimlist
    return (extrapolatedData, findimlist)
def createNgrams(ssTableData, alltemptableData, mingram, maxgram):
    ngramData = {}
    dimlists = []
    for i in range(len(ssTableData)):
        (bigData, dimlist)=createNgramsPerSStable(ssTableData[i], alltemptableData, mingram, maxgram)
        ngramData[i] = bigData
        dimlists.append(dimlist)
    return (ngramData, dimlists)


import cv2 as cv
import imutils
import time
from configparser import ConfigParser
from utils.bounding_box import BoundingBox

# Detect objects in a video file and write back out as .avi video
def main():
    # create parser instance
    config = ConfigParser()

    # Read detection.cfg
    config.read('config/detection.cfg')
    crop = config.get('blob', 'crop')
    swap_rb = config.get('blob', 'swap_rb')
    scale_factor = config.get('blob', 'scale_factor')
    output_width = config.get('blob', 'output_width')
    output_length = config.get('blob', 'output_length')
    confidence = config.get('blob', 'confidence')
    threshold = config.get('blob', 'threshold')
    yolov_labels = config.get('yolov3', 'yolov3_labels')
    yolov_weights = config.get('yolov3', 'yolov3_test_weights_file')
    yolov_config = config.get('yolov3', 'yolov3_config_file')
    input_video = config.get('video', 'input_video')
    output_video = config.get('video', 'output_video')
    frames_per_second = int(config.get('video', 'frames_per_second'))

    # convert string to boolean
    if config.get('video', 'color') == 'True':
        color=True
    else:
        color=False

    bounding_box = BoundingBox(scale_factor, output_length, output_width, swap_rb, crop,
                               yolov_labels, yolov_config, yolov_weights, confidence, threshold)

    video_stream = cv.VideoCapture(input_video)
    writer = None

    # Obtain video frame metadata
    try:
        if imutils.is_cv2():
            prop = cv.cv.CV_CAP_PROP_FRAME_COUNT
        else :
            prop = cv.CAP_PROP_FRAME_COUNT
        total = int(video_stream.get(prop))
        print("Total frames in video: {}".format(total))
    except:
        print("Could not determine # of frames in video")
        total = -1

    frame_count = 1
    while True:
        # Read frame
        ret_val, frame = video_stream.read()
        if not ret_val:
            break
        start_time = time.time()
        input_height, input_width = frame.shape[:2]

        # Get and draw bounding boxes
        blob = bounding_box.get_blob(frame)
        output_layers = bounding_box.get_output_layers(blob)
        boxes, confidences, class_identifiers = bounding_box.get_bounding_boxes(output_layers, input_height, input_width)
        suppression = bounding_box.suppress_weak_overlapping_boxes(boxes, confidences)
        frame_with_boxes = bounding_box.draw_boxes_labels(frame, boxes, confidences, class_identifiers, suppression)
        end_time = time.time()

        # Write frame
        if writer is None:
            four_cc = cv.VideoWriter_fourcc(*"MJPG")
            writer = cv.VideoWriter(output_video, four_cc, frames_per_second, (frame.shape[1], frame.shape[0]), color)
            if total > 0:
                elapsed_time = (end_time - start_time)
                print("Single frame took:  {:.4f} seconds".format(elapsed_time))
                print("Estimated total time to finish: {:.4f} seconds".format(elapsed_time * total))

        if frame_count % 10 == 0:
            print("Total frames written: {}".format(frame_count))

        frame_count += 1
        writer.write(frame_with_boxes)

    print("Wrote video to: {}".format(output_video))

    # Cleanup
    video_stream.release()
    cv.destroyAllWindows()

if __name__ == "__main__":
    main()


from keras.models import Sequential
from keras.layers.core import Dense, Dropout
from keras.optimizers import SGD
from keras.layers import Dense, Dropout, Activation, Flatten

nb_classes = 10

from keras.datasets import mnist
from keras.utils import np_utils

# FC@512+relu -> DropOut(0.2) -> FC@512+relu -> DropOut(0.2) -> FC@nb_classes+softmax
def make_model(nb_classes=10, input_size=784):

    model = Sequential()

    model.add(Dense(512, input_shape=(input_size,)))

    model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(Dense(512))

    model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(Dense(nb_classes))

    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy',
                  optimizer=SGD(),
                  metrics=['accuracy'])
    '''
    Yam Peleg Solution:
    model = Sequential()
    model.add(Dense(512, activation='relu', input_shape=(784,)))
    model.add(Dropout(0.2))
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(10, activation='softmax'))

    '''
    return model

(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype("float32")
X_test = X_test.astype("float32")
X_train /= 255
X_test /= 255

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)
model=make_model()
model.summary()
network_history = model.fit(X_train, Y_train, batch_size=128,
                            epochs=200, verbose=1, validation_data=(X_test, Y_test))
import matplotlib.pyplot as plt

plt.figure()
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.plot(network_history.history['loss'])
plt.plot(network_history.history['val_loss'])
plt.legend(['Training', 'Validation'])

plt.figure()
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.plot(network_history.history['acc'])
plt.plot(network_history.history['val_acc'])
plt.legend(['Training', 'Validation'], loc='lower right')


#! /usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
import os
import shutil
import os.path
import tempfile

from rdiff_trimmer.rdiff_api import RdiffAPI
from rdiff_trimmer.rdiff_api import Increment

DIR_PATH = os.path.abspath(os.path.dirname(__file__))

class TestIncrement(object):
    def test_increment_from_string(self):
        inc_line = "   1524336818 directory   "

        inc = Increment.from_string(inc_line)

        assert inc.timestamp == 1524336818.0

class TestRdiffAPI(object):
    def setup(self):
        self.ra = RdiffAPI(os.path.join(DIR_PATH, "test_data"))
        self.tmp_dir = tempfile.mkdtemp()
        self.out_ra = RdiffAPI(self.tmp_dir)

    def teardown(self):
        shutil.rmtree(self.tmp_dir)

    def test_yield_increments(self):
        increments = list(self.ra.yield_increments())

        assert len(increments) == 3

        assert increments[1].timestamp == 1524342591.0
        assert increments[2].timestamp == 1524342605.0

    def test_restore_into(self):
        increments = list(self.ra.yield_increments())

        self.ra.restore_into(self.tmp_dir, increments_to_keep=[increments[0]])

        kept_increments = list(self.out_ra.yield_increments())

        assert len(kept_increments) == 1
        assert kept_increments[0].timestamp == increments[0].timestamp

        assert os.path.exists(os.path.join(self.tmp_dir, "content"))
        assert not os.path.exists(os.path.join(self.tmp_dir, "and_more_content"))


"""
	Funciones secundarias
"""

def ingreso(diccionario, palabra):
	try:
		diccionario[palabra] = diccionario[palabra] + 1	#Si la clave existe, se aumenta la frecuencia
	except:
		diccionario[palabra] = 1						#Sino, se crea un nuevo espacio con frecuencia 1

def depuracion_texto(texto):

	texto = texto.lower()
	a_eliminar = ['.', ',', ';', ':', '\"', '\'', '(', ')']	#arreglo con elementos a eliminar

	for i in a_eliminar:
		texto = texto.replace(i, '')	#Eliminacion de caracteres usando el arreglo
	texto = texto.replace('\n', ' ')
	texto = texto.replace('\t', ' ')

	return texto

"""
	Ejecucion del programa
"""

archivo = open("funes.txt", "r", encoding='utf-8')	#Apertura
texto = archivo.read()								#Lectura
archivo.close()										#Cierre
texto = depuracion_texto(texto)						#Limpieza (tener un texto sin mayusculas ni signos)
lista = texto.split()								#Cada palabra se lleva a una lista
dict_frecuencias = {}								#Diccionario con las frecuencias, se inicia vacio

for pal_lista in lista:
	ingreso(dict_frecuencias, pal_lista)			#Ingreso de palabras

frecuencia = max(dict_frecuencias.values())			#Buscamos mayor frecuencia

while frecuencia > 0:								#Impresion de datos desde la mayor frecuencia
	for i in dict_frecuencias:
		if dict_frecuencias[i] == frecuencia:
			print (i + ": " + str(frecuencia))
	frecuencia = frecuencia - 1


import re

def getTerms(query):
  m = re.compile('\w+\([\w,*\s*]+\)')
  return m.findall(query)

def getOperators(query):
  m = re.compile('[&&=>]+')
  return m.findall(query)

def getPredicate(x):
  return x[0:x.find('(')]

def unknownPos(arguments):
  positions = []
  for i in xrange(len(arguments)):
    if len(arguments[i]) == 1:
      positions.append(i)
  return positions

def sameKnown(args1, args2, positions):
  if (len(args1) != len(args2)): return False
  if (len(args1) == 0): return True
  for p in positions:
    if args1[p] != args2[p]: return False
  return True

def lastReversedSub(subs):
  if len(subs) == 0: return []
  last = subs[len(subs)-1]
  last = (last[1], last[0])
  return [last]

def allButLastSub(subs):
  if len(subs) <= 1: return []
  return subs[0: len(subs)-1]

def knownPos(arguments):
  positions = []
  for i in xrange(len(arguments)):
    if len(arguments[i]) > 1:
      positions.append(i)
  return positions

def hasUnknown(x):
  unknown = False
  for i in x:
    if (len(i) == 1): return True
  return False

def getArguments(x):
  l = x.find('(')+1
  u = x.find(')')
  args = x[l:u].split(",")
  final = []
  for i in args:
    final.append(i.strip())
  return final

def replaceArgument(term, x, y):
  l = term.find('(')+1
  u = term.find(')')
  return term[0:l] + term[l:u].replace(x, y) + term[u:len(term)]

class Atomic(object):
  def __init__(self, sentence):
    self.sentence = sentence
    self.pred = getPredicate(self.sentence)
    self.args = getArguments(self.sentence)
    self.known = not hasUnknown(self.args)

  def subInArgs(self, sub):
    for a in self.args:
      if (sub[1] == a):
        return True
    return False

  def adjustSubs(self, subs):
    for i in xrange(len(subs)):
      s = subs[i]
      if self.subInArgs(s):
        temp = (s[0], s[1]+"1")
        # subs[i] = temp
        # print('would have changed: ' + str(s) +" into " + str(temp))

  def applySubs(self, subs):
    self.adjustSubs(subs)
    for s in subs:
      for i in xrange(len(self.args)):
        a = self.args[i]
        if (a == s[0]): self.args[i] = s[1]
    self.update()

  def applySubsUnknown(self, subs):
    self.adjustSubs(subs)
    for s in subs:
      for i in xrange(len(self.args)):
        a = self.args[i]
        if (a == s[0] and len(s[0]) == 1):
          self.args[i] = s[1]
    self.update()

  def update(self):
    nargs = len(self.args)
    self.sentence = self.pred + "("
    for i in xrange(nargs-1):
      self.sentence = self.sentence + self.args[i] + ","
    self.sentence = self.sentence + self.args[nargs-1] + ")"

class Sentence(object):
  def __init__(self, sentence):
    self.extractSentence(sentence)

  def extractSentence(self, sentence):
    self.sentence = sentence.replace(" ", "")
    self.ops = getOperators(self.sentence)
    self.parts = []
    self.terms = getTerms(self.sentence)
    self.variables = self.getVariables()
    for t in self.terms:
      self.parts.append(Atomic(t))

    self.known = True
    for i in self.parts:
      if hasUnknown(i.args): self.known = False

    self.implication = self.sentence.find("=>") is not -1
    self.atomic = len(self.terms) is 1

  def getVariables(self):
    variables = set()
    for p in self.parts:
      for a in p.args:
        variables.add(a)
    return variables

  def getConsequent(self):
    return self.sentence.split("=>")[1]

  def keyValue(self):
    x = self.sentence

    if self.implication:
      y = x.split("=>")
      z = y[0].split("&&")

      keyTerm = y[1].strip()
      key = getPredicate(keyTerm)
      antecedent = y[0].strip()
      variables = getArguments(keyTerm)

      return key, (antecedent, variables)

    else:
      key = getPredicate(x)
      if self.known: return key, getArguments(x)
      else: return key, None

  def updateSentence(self):
    # nTerms = len(self.terms)
    sentence = self.terms[0]
    for i in xrange(len(self.ops)):
      sentence = sentence + self.ops[i] + self.terms[i+1]

    self.extractSentence(sentence)

  def applySubs(self, subs):
    for i in xrange(len(self.parts)):
      p = self.parts[i]
      p.applySubs(subs)
      self.terms[i] = p.sentence
    self.updateSentence()

  def subUnknown(self, subs):
    for i in xrange(len(self.parts)):
      p = self.parts[i]
      p.applySubsUnknown(subs)
      self.terms[i] = p.sentence
    self.updateSentence()

  def cleanUnknown(self):
    for i in xrange(len(self.terms)):
      t = self.terms[i]
      args = getArguments(t)
      for a in args:
        if (len(a) == 1):
          self.terms[i]= replaceArgument(self.terms[i], a, "_")
    self.updateSentence()

  def __str__(self):
    return self.sentence


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tg import TGController, expose, tmpl_context
import transaction
from tgext.crud import EasyCrudRestController
from .base import CrudTest, Movie, DBSession, metadata, Actor, Genre

class TestFilteredCrud(CrudTest):
    def controller_factory(self):
        class MovieController(EasyCrudRestController):
            model = Movie
            filters = {
                'genre': lambda: DBSession.query(Genre).filter_by(name='action').first()
            }

        class CrudHTMLController(TGController):
            movies = MovieController(DBSession)

        return CrudHTMLController()

    def setUp(self):
        super(TestFilteredCrud, self).setUp()

        genre1 = Genre(name='action')
        DBSession.add(genre1)
        DBSession.add(Movie(title='Action Blockbuster', genre=genre1))

        genre2 = Genre(name='comedy')
        DBSession.add(genre2)
        DBSession.add(Movie(title='Comedy Blockbuster', genre=genre2))
        DBSession.add(Movie(title='Blockbuster Test', genre=genre2))
        DBSession.flush()
        transaction.commit()

    def test_filtered_table(self):
        result = self.app.get('/movies/')
        result = result.text
        assert result.find('Action Blockbuster') != -1, result
        assert result.count('Blockbuster') == 1, result

    def test_post(self):
        action_genre = DBSession.query(Genre).filter_by(name='action').first()

        result = self.app.post('/movies.json',
                               params={'title': 'Blockbuster Test'})
        result = result.json
        assert result['value']['genre_id'] == action_genre.genre_id, result

    def test_post_html(self):
        self.app.post('/movies/', params={'title': 'Blockbuster Test'}, status=302)
        result = self.app.get('/movies/')
        result = result.text
        assert result.count('Blockbuster') == 2, result
        assert result.find('Action Blockbuster') != -1, result
        assert result.find('Blockbuster Test') != -1, result

    def test_search(self):
        self.app.post('/movies.json', params={'title': 'Blockbuster Test'})
        result = self.app.get('/movies/?title=Blockbuster%20Test')
        result = result.text.split('<tbody')[-1]
        assert result.count('Blockbuster') == 1, result

    def test_put(self):
        movie = DBSession.query(Movie).filter_by(title='Action Blockbuster').first()
        result = self.app.put('/movies.json',
                               params={'movie_id': movie.movie_id,
                                       'title': 'RENAMED'})
        assert result.json['value']['title'] == 'RENAMED', result

    def test_put_restriction(self):
        movie = DBSession.query(Movie).filter_by(title='Comedy Blockbuster').first()

        result = self.app.put('/movies.json',
                               params={'movie_id': movie.movie_id,
                                       'title': 'RENAMED'}, status=404)

        result = self.app.put('/movies',
                               params={'movie_id': movie.movie_id,
                                       'title': 'RENAMED'}, status=404)

    def test_get(self):
        movie = DBSession.query(Movie).filter_by(title='Comedy Blockbuster').first()
        result = self.app.get('/movies/%s.json' % movie.movie_id, status=404)
        result = self.app.get('/movies/%s' % movie.movie_id, status=404)

        movie = DBSession.query(Movie).filter_by(title='Action Blockbuster').first()
        result = self.app.get('/movies/%s.json' % movie.movie_id)
        assert result.json['value']['title'] == 'Action Blockbuster', result
        result = self.app.get('/movies/%s' % movie.movie_id)
        assert 'Action Blockbuster' in result.text, result

    def test_delete(self):
        movie = DBSession.query(Movie).filter_by(title='Comedy Blockbuster').first()
        result = self.app.delete('/movies/%s.json' % movie.movie_id)
        DBSession.remove()
        movie = DBSession.query(Movie).filter_by(title='Comedy Blockbuster').first()
        assert movie is not None

        movie = DBSession.query(Movie).filter_by(title='Action Blockbuster').first()
        result = self.app.delete('/movies/%s.json' % movie.movie_id)
        DBSession.remove()
        movie = DBSession.query(Movie).filter_by(title='Action Blockbuster').first()
        assert movie is None


import telebot

from config import *
from extensions import Converter, APIException

bot = telebot.TeleBot(TOKEN)

@bot.message_handler(commands=['start', 'help'])
def start(message: telebot.types.Message):
    text = "Приветствую! Какую информацию по текущему курсу валют вы хотите узнать?"
    bot.send_message(message.chat.id, text)

@bot.message_handler(commands=['values'])
def values(message: telebot.types.Message):
    text = 'Доступные валюты:'
    for i in exchanges.keys():
        text = '\n'.join((text, i))
    bot.reply_to(message, text)

@bot.message_handler(content_types=['text'])
def converter(message: telebot.types.Message):
    try:
        comands = message.text.split()
        if len(comands) != 3:
            raise ValueError('Неверное количество параметров')
        base, sym, amount = comands
        new_price = Converter.get_price(base, sym, amount)
        bot.reply_to(message, f"Цена {amount} {base} в {sym} : {new_price}")
    except APIException as e:
        bot.reply_to(message, f"Ошибка в команде:\n{e}" )

bot.polling()


import numpy as np
from matplotlib import image
from matplotlib import pyplot as plt
import cv2

image = cv2.imread("P5/internet_censorship_map.png",cv2.IMREAD_UNCHANGED)
transparent_area = len(np.where(image[:,:,3] == 0)[0])
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
water_color = (176,224,230)

water_idxs = np.where((image == water_color).all(axis = 2))
image[water_idxs] = (100,100,100)

plt.imshow(image)
plt.show()

percentage = len(water_idxs[0])/(image.shape[0] * image.shape[1] - transparent_area) * 100

print(" percentage of water on earth:", percentage)


"""
Some common utilities.
"""

import yaml
from funcy import *
from munch import Munch
import cytoolz as tz

def difference(*colls):
    """
    Find the keys that have different values in an arbitrary number of (nested) collections. Any key
    that differs in at least 2 collections is considered to fit this criterion.
    """

    # Get all the leaf paths for each collection: make each path a tuple
    leaf_paths_by_coll = list(map(lambda c: list(map(tuple, get_all_leaf_paths(c))), colls))

    # Find the union of all leaf paths: merge all the paths and keep only the unique paths
    union_leaf_paths = list(distinct(concat(*leaf_paths_by_coll)))

    # Get the values corresponding to these leaf paths in every collection: if a leaf path doesn't exist, assumes None
    values_by_coll = list(map(lambda lp: list(map(lambda coll: tz.get_in(lp, coll), colls)), union_leaf_paths))

    # Filter out the leaf paths that have identical values across the collections
    keep_leaf_paths = list(map(0, filter(lambda t: not allequal(t[1]), zip(union_leaf_paths, values_by_coll))))
    keep_values = list(map(1, filter(lambda t: not allequal(t[1]), zip(union_leaf_paths, values_by_coll))))

    # Rearrange to construct a list of dictionaries -- one per original collection.
    # Each of these dictionaries maps a 'kept' leaf path to its corresponding
    # value in the collection
    differences = list(map(lambda vals: dict(zip(keep_leaf_paths, vals)), list(zip(*keep_values))))

    return differences

def rmerge(*colls):
    """
    Recursively merge an arbitrary number of collections.
    For conflicting values, later collections to the right are given priority.
    Note that this function treats sequences as a normal value and sequences are not merged.

    Uses:
    - merging config files
    """
    if isinstance(colls, tuple) and len(colls) == 1:
        # A squeeze operation since merge_with generates tuple(list_of_objs,)
        colls = colls[0]
    if all(is_mapping, colls):
        # Merges all the collections, recursively applies merging to the combined values
        return merge_with(rmerge, *colls)
    else:
        # If colls does not contain mappings, simply pick the last one
        return last(colls)

def prettyprint(s):
    if hasattr(s, '__dict__'):
        print(yaml.dump(s.__dict__))
    elif isinstance(s, dict):
        print(yaml.dump(s))
    else:
        print(s)

def allequal(seq):
    return len(set(seq)) <= 1

@autocurry
def listmap(fn, seq):
    return list(map(fn, seq))

@autocurry
def prefix(s, p):
    if isinstance(s, str):
        return f'{p}{s}'
    elif isinstance(s, list):
        return list(map(prefix(p=p), s))
    else:
        raise NotImplementedError

@autocurry
def postfix(s, p):
    if isinstance(s, str):
        return f'{s}{p}'
    elif isinstance(s, list):
        return list(map(postfix(p=p), s))
    else:
        raise NotImplementedError

@autocurry
def surround(s, pre, post):
    return postfix(prefix(s, pre), post)

def nested_map(f, *args):
    """ Recursively transpose a nested structure of tuples, lists, and dicts """
    assert len(args) > 0, 'Must have at least one argument.'

    arg = args[0]
    if isinstance(arg, tuple) or isinstance(arg, list):
        return [nested_map(f, *a) for a in zip(*args)]
    elif isinstance(arg, dict):
        return {
            k: nested_map(f, *[a[k] for a in args])
            for k in arg
        }
    else:
        return f(*args)

@autocurry
def walk_values_rec(f, coll):
    """
    Similar to funcy's walk_values, but does so recursively, including mapping f over lists.
    """
    if is_mapping(coll):
        return f(walk_values(walk_values_rec(f), coll))
    elif is_list(coll):
        return f(list(map(walk_values_rec(f), coll)))
    else:
        return f(coll)

@autocurry
def nested_dict_walker(fn, coll):
    """
    Apply a function over the mappings contained in coll.
    """
    return walk_values_rec(iffy(is_mapping, fn), coll)

def get_all_leaf_paths(coll):
    """
    Returns a list of paths to all leaf nodes in a nested dict.
    Paths can travel through lists and the index is inserted into the path.
    """
    if isinstance(coll, dict) or isinstance(coll, Munch):
        return list(cat(map(lambda t: list(map(lambda p: [t[0]] + p,
                                               get_all_leaf_paths(t[1])
                                               )),
                            iteritems(coll)))
                    )

    elif isinstance(coll, list):
        return list(cat(map(lambda t: list(map(lambda p: [t[0]] + p,
                                               get_all_leaf_paths(t[1])
                                               )),
                            enumerate(coll)))
                    )
    else:
        return [[]]

def get_all_paths(coll, prefix_path=(), stop_at=None, stop_below=None):
    """
    Given a collection, by default returns paths to all the leaf nodes.
    Use stop_at to truncate paths at the given key.
    Use stop_below to truncate paths one level below the given key.
    """
    assert stop_at is None or stop_below is None, 'Only one of stop_at or stop_below can be used.'
    if stop_below is not None and stop_below in str(last(butlast(prefix_path))):
        return [[]]
    if stop_at is not None and stop_at in str(last(prefix_path)):
        return [[]]
    if isinstance(coll, dict) or isinstance(coll, Munch) or isinstance(coll, list):
        if isinstance(coll, dict) or isinstance(coll, Munch):
            items = iteritems(coll)
        else:
            items = enumerate(coll)

        return list(cat(map(lambda t: list(map(lambda p: [t[0]] + p,
                                               get_all_paths(t[1],
                                                             prefix_path=list(prefix_path) + [t[0]],
                                                             stop_at=stop_at,
                                                             stop_below=stop_below)
                                               )),
                            items))
                    )
    else:
        return [[]]

def get_only_paths(coll, pred, prefix_path=(), stop_at=None, stop_below=None):
    """
    Get all paths that satisfy the predicate fn pred.
    First gets all paths and then filters them based on pred.
    """
    all_paths = get_all_paths(coll, prefix_path=prefix_path, stop_at=stop_at, stop_below=stop_below)
    return list(filter(pred, all_paths))

if __name__ == '__main__':
    coll1 = {'a': 1,
             'b': 2,
             'c': {'d': 12}}
    coll2 = {'a': 1,
             'b': 2,
             'c': {'d': 13}}
    coll3 = {'a': 1,
             'b': 3,
             'c': {'d': 14},
             'e': 4}

    difference(coll1, coll2, coll3)


# A CSP is modeled as a list of three elements:
# X = [X_1, X_2, ... , X_n] variables
# D = [D_1, D_2, ... , D_n] domains for each variable
# C = [C_1, C_2, ..., C_m] constraints

# Example. Map Coloring.
# Suppose we have we have the simple map drawn here:
# |------------------------------|
# |       A        |             |
# |----------------|             |
# |                |      B      |
# |       C        |             |
# |                |---|---------|
# |                |   |         |
# |----------------|   |         |
# |                    |   E     |
# |        D           |         |
# |------------------------------|

# Possible solution: {A=red, B=blue, C=green, D=orange, E=red}
# Where X = [A, B, C, D, E]
# Where D = [[red,blue,green,orange], ..., ..]
# And constraints are implicitly defined as A.color != B.color
# Explicitly: (A,B) in {(red,green), (red,blue), ... }

# Given: two variables and their shared domain as a list
# Output: a tuple containing a tuple of the variables and
# a list of constraints represented as tuples
# Example: diff_constraint('A', 'B', ['red','green']
# Output: ( ('A','B'), [('red','green'), ('green','red')] )
def diff_constraint(var1, var2, shared_domain):
    constraints = []
    for i in range(len(shared_domain)):
        for j in range(len(shared_domain)):
            if i != j:
                x = shared_domain[i]
                y = shared_domain[j]
                constraints.append((x,y))

    return ( (var1, var2), constraints )

class CSP:
    def __init__(self, Xlist, Dlist, Clist):
        # assumes that len(Xlist) == len(Dlist)
        self.domains = {}
        self.variables = Xlist
        for i in zip(Xlist, Dlist):
            var,d = i
            self.domains[var] = d
        self.constraints = {}
        for constr in Clist:
            var1,var2 = constr[0]
            lst = constr[1]
            if var1 not in self.constraints:
                self.constraints[var1] = {}
            self.constraints[var1][var2] = lst
            if var2 not in self.constraints:
                self.constraints[var2] = {}
            self.constraints[var2][var1] = lst

    def get_vars(self):
        return list(self.variables)
    def get_domains(self):
        return dict(self.domains)
    def get_constraints(self):
        return dict(self.constraints)

# Given a dictionary of assignments, determine if it is partial
def is_partial(assignment):
    return (None in list(assignment.values()))

def is_valid(assignment, constraints):
    if is_partial(assignment): return False
    for var in assignment:
        d = constraints[var]
        for var2 in d:
            lst = d[var2]
            a1,a2 = assignment[var],assignment[var2]
            if (a1,a2) not in lst: return False
    return True

# Given a CSP, returns a dictionary of a valid assignment
def solve_csp(csp):
    variables = csp.get_vars()
    domains = csp.get_domains()
    constraints = csp.get_constraints()

    assignment = {}
    for v in csp.get_vars():
        assignment[v] = None

    # dfs
    def dfs(depth):
        curr = variables[depth]
        for val in domains[curr]:
            assignment[curr] = val
            if is_valid(assignment, constraints):
                return assignment
            if 1+depth < len(variables):
                rec = dfs(1+depth)
                if rec:
                    return rec
    return dfs(0)

# small test example
# a-b-c-a in a triangle map
map_variables = ['a','b','c']
map_domains = [['red','green','blue'] for _ in range(3)]
map_constraints = []
for i in range(len(map_variables)):
    for j in range(len(map_variables)):
        if i!=j:
            v1,v2 = map_variables[i],map_variables[j]
            domain = ['red','green','blue']
            map_constraints.append(diff_constraint(v1,v2,domain))
map_csp = CSP(map_variables, map_domains, map_constraints)
map_sol = {'a': 'green', 'b': 'red', 'c': 'blue'}
# print(is_valid(map_sol, map_csp.get_constraints()))

map2X = ['A','B','C','D','E']
map2D = [['red','green','orange','blue'] for _ in range(5)]
map2_constr = []
map2_neighbors = {
    'A': ['B','C'],
    'B': ['A','C','D','E'],
    'C': ['A','B','D'],
    'D': ['B','C','E'],
    'E': ['B','D'] }
for v in map2X:
    for nei in map2_neighbors[v]:
        domain=['red','green','blue','orange']
        map2_constr.append(diff_constraint(v,nei,domain))

map2_csp = CSP(map2X, map2D, map2_constr)


from scenerf.data.bundlefusion.bundlefusion_dm import BundlefusionDM
import torch
import numpy as np
import os
from tqdm import tqdm
import click
import pickle

torch.set_grad_enabled(False)

@click.command()
@click.option('--n_gpus', default=1, help='number of GPUs')
@click.option('--bs', default=1, help='Batch size')
@click.option('--n_workers_per_gpu', default=3, help='number of workers per GPU')
@click.option('--root', default="", help='path to dataset folder')
@click.option('--eval_save_dir', default="")
def main(
    root,
    bs, n_gpus, n_workers_per_gpu,
    eval_save_dir
    ):

    data_module = BundlefusionDM(
        root=root,
        batch_size=int(bs / n_gpus),
        num_workers=int(n_workers_per_gpu),
    )
    data_module.setup_val_ds()
    data_loader = data_module.val_dataloader()

    cnt = 0

    agg_depth_errors = {}
    agg_n_frames = {}
    for batch in tqdm(data_loader):
        cnt += 1

        for i in range(bs):

            frame_id = batch['frame_id'][i]
            sequence = batch['sequence'][i]

            save_dir = os.path.join("{}/depth_metrics".format(eval_save_dir), sequence)

            save_filepath = os.path.join(save_dir,"{}.npy".format(frame_id))

            with open(save_filepath, "rb") as handle:
                data = pickle.load(handle)
            depth_errors = data["depth_errors"]
            n_frames = data["n_frames"]

            for k in depth_errors:
                if k not in agg_depth_errors:
                    agg_depth_errors[k] = depth_errors[k]
                    agg_n_frames[k] = n_frames[k]
                else:
                    agg_depth_errors[k] += depth_errors[k]
                    agg_n_frames[k] += n_frames[k]

            if cnt % 20 == 0:
                print("=================")
                print("==== batch {} ====".format(cnt))
                print("=================")
                print_metrics(agg_depth_errors, agg_n_frames)
    print("=================")
    print("====== End ======")
    print("=================")
    print(model_name)
    print_metrics(agg_depth_errors, agg_n_frames)

def print_metrics(agg_depth_errors, n_frames):
    print("|distance|abs_rel |sq_rel  |rmse     |rmse_log|a1      |a2      |a3      |n_frames|")

    total_depth_errors = None
    total_frame = 0
    for distance in sorted(agg_depth_errors):
        if total_depth_errors is None:
            total_depth_errors = np.copy(agg_depth_errors[distance])
        else:
            total_depth_errors = total_depth_errors + agg_depth_errors[distance]
        print("|{:08d}|{:02.6f}|{:.6f}|{:.6f}|{:.6f}|{:.6f}|{:.6f}|{:.6f}|{:08d}|".format(
            distance,
            agg_depth_errors[distance][0]/n_frames[distance],
            agg_depth_errors[distance][1]/n_frames[distance],
            agg_depth_errors[distance][2]/n_frames[distance],
            agg_depth_errors[distance][3]/n_frames[distance],
            agg_depth_errors[distance][4]/n_frames[distance],
            agg_depth_errors[distance][5]/n_frames[distance],
            agg_depth_errors[distance][6]/n_frames[distance],
            n_frames[distance]
        ))
        total_frame += n_frames[distance]
    print("|{}|{:02.6f}|{:.6f}|{:.6f}|{:.6f}|{:.6f}|{:.6f}|{:.6f}|{:08d}|".format(
            "All     ",
            total_depth_errors[0]/total_frame,
            total_depth_errors[1]/total_frame,
            total_depth_errors[2]/total_frame,
            total_depth_errors[3]/total_frame,
            total_depth_errors[4]/total_frame,
            total_depth_errors[5]/total_frame,
            total_depth_errors[6]/total_frame,
            total_frame
        ))

if __name__ == "__main__":
    main()


import asyncio
from datetime import datetime
from typing import List, Optional

import tabulate
import typer
from mognet.cli.cli_state import state
from mognet.cli.models import OutputFormat
from mognet.cli.run_in_loop import run_in_loop
from mognet.model.result import Result
from mognet.primitives.queries import StatusResponseMessage
from mognet.tools.dates import now_utc
from pydantic import BaseModel, Field

group = typer.Typer()

@group.command("status")
@run_in_loop
async def status(
    format: OutputFormat = typer.Option(OutputFormat.TEXT, metavar="format"),
    text_label_format: str = typer.Option(
        "{name}(id={id!r}, state={state!r})",
        metavar="text-label-format",
        help="Label format for text format",
    ),
    json_indent: int = typer.Option(2, metavar="json-indent"),
    poll: Optional[int] = typer.Option(
        None,
        metavar="poll",
        help="Polling interval, in seconds (default=None)",
    ),
    timeout: int = typer.Option(
        30,
        help="Timeout for querying nodes",
    ),
):
    """Query each node for their status"""

    async with state["app_instance"] as app:
        while True:
            each_node_status: List[StatusResponseMessage] = []

            async def read_status():
                async for node_status in app.get_current_status_of_nodes():
                    each_node_status.append(node_status)

            try:
                await asyncio.wait_for(read_status(), timeout=timeout)
            except asyncio.TimeoutError:
                pass

            all_result_ids = set()

            for node_status in each_node_status:
                all_result_ids.update(node_status.payload.running_request_ids)

            all_results_by_id = {
                r.id: r
                for r in await app.result_backend.get_many(
                    *all_result_ids,
                )
                if r is not None
            }

            report = _CliStatusReport()

            for node_status in each_node_status:
                running_requests = [
                    all_results_by_id[r]
                    for r in node_status.payload.running_request_ids
                    if r in all_results_by_id
                ]
                running_requests.sort(key=lambda r: r.created or now_utc())

                report.node_status.append(
                    _CliStatusReport.NodeStatus(
                        node_id=node_status.node_id, running_requests=running_requests
                    )
                )

            if poll:
                typer.clear()

            if format == "text":
                table_headers = ("Node name", "Running requests")

                table_data = [
                    (
                        n.node_id,
                        "\n".join(
                            text_label_format.format(**r.dict())
                            for r in n.running_requests
                        )
                        or "(Empty)",
                    )
                    for n in report.node_status
                ]

                typer.echo(
                    f"{len(report.node_status)} nodes replied as of {datetime.now()}:"
                )

                typer.echo(tabulate.tabulate(table_data, headers=table_headers))

            elif format == "json":
                typer.echo(report.json(indent=json_indent, ensure_ascii=False))

            if not poll:
                break

            await asyncio.sleep(poll)

class _CliStatusReport(BaseModel):
    class NodeStatus(BaseModel):
        node_id: str
        running_requests: List[Result]

    node_status: List[NodeStatus] = Field(default_factory=list)


#%%
from selenium import webdriver
#%%
# Acesso ao site da How
driver = webdriver.Chrome()
driver.get('https://howedu.com.br')
# %%
# clicando no botão
element = driver.find_element('xpath', '//*[@id="post-11648"]/div/div/section[2]/div/div[1]/div/div[5]/div/div/a/span/span[2]')
element.click()
# %%
# Acessando o site de busca de cep dos correios
driver.get('https://buscacepinter.correios.com.br/app/endereco/index.php')
elem_cep = driver.find_element('name', 'endereco')
elem_cep.click()

# %%
# preenchendo o formulário
elem_cep.clear()
elem_cep.send_keys('80420130')
# %%
# selecionando o tipo de cep
elem_cod = driver.find_element('name', 'tipoCEP')
elem_cod.click()
elem_cod = driver.find_element('xpath', '//*[@id="tipoCEP"]/optgroup/option[6]')
# %%
# clicando em pesquisar
elem_cod = driver.find_element('id', 'btn_pesquisar')
elem_cod.click()
# %%
# capturando os dados da pesquisa
logradouro = driver.find_element('xpath', '/html/body/main/form/div[1]/div[2]/div/div[4]/table/tbody/tr/td[1]')
logradouro.text
bairro = driver.find_element('xpath', '//*[@id="resultado-DNEC"]/tbody/tr/td[2]')
bairro.text
localidade = driver.find_element('xpath', '//*[@id="resultado-DNEC"]/tbody/tr/td[3]')
localidade.text
cep = driver.find_element('xpath', '//*[@id="resultado-DNEC"]/tbody/tr/td[4]')
cep.text

driver.close()
# %%
print(f"""
      Para o CEP: {cep.text}
      Endereço: {logradouro.text}
      Bairro: {bairro.text}
      Localidade: {localidade.text}
""")

# %%


#coding=utf8

########################################################################
###                                                                  ###
### Created by Martin Genet, 2012-2023                               ###
###                                                                  ###
### University of California at San Francisco (UCSF), USA            ###
### Swiss Federal Institute of Technology (ETH), Zurich, Switzerland ###
### École Polytechnique, Palaiseau, France                           ###
###                                                                  ###
########################################################################

import vtk

import myPythonLibrary    as mypy
import myVTKPythonLibrary as myvtk

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

def writeUGrid(
        ugrid,
        filename,
        verbose=0):

    mypy.my_print(verbose, "*** writeUGrid: "+filename+" ***")

    if (filename.endswith("vtk")):
        ugrid_writer = vtk.vtkUnstructuredGridWriter()
    elif (filename.endswith("vtu")):
        ugrid_writer = vtk.vtkXMLUnstructuredGridWriter()
    else:
        assert 0, "File must be .vtk or .vtu. Aborting."

    ugrid_writer.SetFileName(filename)
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        ugrid_writer.SetInputData(ugrid)
    else:
        ugrid_writer.SetInput(ugrid)
    ugrid_writer.Update()
    ugrid_writer.Write()


#python3

import os
import sys

###################################################
## Translate negative interaction sets to OG ids
###################################################

if len(sys.argv) != 4:
    print("Need 3 arguments: [Negative interaction set] [Ortholgous group file] [negative interaction in OGs out file]")
    sys.exit()

neg_set = sys.argv[1] #negative interaction dataset
OG_file = sys.argv[2] #Orthologous group file
neg_out_file = sys.argv[3] #out fle

try:
    open(sys.argv[1])
    open(sys.argv[2])
except IOError:
    print("No such input file"); sys.exit()

#Check if file is not empty
for file in (sys.argv[1], sys.argv[2]):
    if os.path.getsize(file) <= 1:
        print(file, "file is empty"); sys.exit()

#eukarya to og depends on orthology used.
#Translate eukarya to OGs
og_file = open(OG_file, "r") #.TXT HAS NO HEADER
euk_to_og_d = {}
for lines in og_file:
    line = lines.rstrip().split(":")
    OG_id = line[0]
    seq_list = line[1].strip().split(" ")
    #if OG_id in leca_d:
    for seq in seq_list:
        if seq not in euk_to_og_d:
            euk_to_og_d[seq] = OG_id

print("Length euk to og: ", len(euk_to_og_d))

neg_set  = open(neg_set, "r")
neg_out_file = open(neg_out_file, "w")

for lines in neg_set:
    line = lines.rstrip().split('\t')
    eukA = line[-2]
    eukB = line[-1]
    if eukA in euk_to_og_d and eukB in euk_to_og_d:
        line += [euk_to_og_d[eukA]]
        line += [euk_to_og_d[eukB]]
        line += ["\n"]
        neg_out_file.write("\t".join(line))
    else:
        if eukA in euk_to_og_d:
            line += [euk_to_og_d[eukA],"-", "\n"]
            neg_out_file.write("\t".join(line))
        elif eukB in euk_to_og_d:
            line += ["-",euk_to_og_d[eukB], "\n"]
            neg_out_file.write("\t".join(line))
        else:
            line += ["-","-", "\n"]
            neg_out_file.write("\t".join(line))


def cubic_checker(check_num):
    """Cubic number checker.

    Check if digits of your number raised to the power of three are equal to that number.
    """
    check_num = str(check_num)
    sum_of_cubs = 0
    for x in check_num:
        x = int(x)
        sum_of_cubs += x**3
    if sum_of_cubs == int(check_num):
        return f'Yes, {check_num} is a cubic number.'

if __name__ == "__main__":
    print(cubic_checker(153))

Daily Challenges/Daily Challenge #11 - Cubic Numbers/cub.py


"""Test input file handle."""

from benchtmpl.io.files.base import FileHandle, InputFile

class TestInputFile(object):
    def test_source_and_target_path(self):
        """Test source and target path methods for input file handle."""
        fh = FileHandle(filepath='/home/user/files/myfile.txt')
        # Input file handle without target path
        f = InputFile(f_handle=fh)
        assert f.source() == '/home/user/files/myfile.txt'
        assert f.target() == 'myfile.txt'
        # Input file handle with target path
        f = InputFile(f_handle=fh, target_path='data/names.txt')
        assert f.source() == '/home/user/files/myfile.txt'
        assert f.target() == 'data/names.txt'


# 1 - importer biblioteker
import random as r
import pygame as pg
from pygame.locals import *
import sys
import math

# 2 - definer constanter
BACKGROUND = (135, 206, 235) # definerer bakgrunns farge som himmel blå
Farger = {
  "Blå": (0,0,255),
  "Rød": (255,0,0),
  "Grønn": (0,255,0),
  "gress" : (150,255,100)
}
WINDOW_WIDTH = 640 # definerer bredde på vinduet
WINDOW_HEIGHT = 480 # definerer høyde på vinduet
FRAMES_PER_SECOND = 60 # definerer hvor mange frames (bilder) som vises hvert sekund
speed = 200/FRAMES_PER_SECOND # definerer farten til spiller
keys = [K_a,K_b,K_c,K_d,K_e,K_f,K_g,K_h,K_i,K_j,K_k,K_l,K_m,K_n,K_o,K_p,K_q,K_r,K_s,K_t,K_u,K_v,K_w,K_x,K_y,K_z,K_SPACE]
keys_alt = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"," "]
for i in range(len(keys_alt)):
    keys_alt[i] = keys_alt[i].upper()

# 2.5 definer klasser
class spiller:
    def __init__(self, x, y, hp, penger, dir):
        self.x = x
        self.y = y
        self.hp = hp
        self.penger = penger
        self.dir = dir
class felt:
    def __init__(self, a, b, forklaring, hotkey, funk):
        self.a = a
        self.b = b
        self.forklaring = forklaring
        self.hotkey = hotkey
        self.funk = funk
class level:
    def __init__(self, alle_felt, oppdater, tegn, start):
        self.alle_felt = alle_felt
        self.oppdater = oppdater
        self.tegn = tegn
        self.start = start

# 2.5.5 definer funksjoner
def _quit(): # avslutter spillet ved å lukke vinduet og slutte programmet
    pg.quit()
    sys.exit()
def tast_trykket(t): # tester om tast a er trykket ned denne framen
    return trykkede_taster[t] and not trykkede_taster_f[t] # returnerer True hvis en tast a er trykket denne framen men ikke forige. Returnerer ellers false
def spiller_i_felt(f):
    return _spiller.x > f.a and _spiller.x < f.b
def ny_snakkeboble(a):
    while (font.size(a)[0] > bobble_lengde):
        txt = ""
        for i in range(len(a)):
            if (font.size(txt+a[i])[0] < bobble_lengde):
                txt += a[i]
            else:
                a = a[i:]
                alle_snakkebobbler.append(txt)
                break
    alle_snakkebobbler.append(a)

    # til test level
def funk1():
    ny_snakkeboble("Bla blabla")
def funk2():
    print("2")
def levelstart1():
    pass
def leveltick1():
    pass
def leveltegn1():
    pg.draw.rect(window,Farger["gress"],(0,WINDOW_HEIGHT-200,WINDOW_WIDTH,200)) # gress

# 3 - Initialize verden
pg.init()
window = pg.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
clock = pg.time.Clock()

# 4 - lag fonter
font = pg.font.SysFont("Arial", 24)
font_big = pg.font.SysFont("Arial", 50)
font_small = pg.font.SysFont("Arial", 18)

# 5 - definer variabeler
tid = 0
komando = ""
alle_snakkebobbler = []
_spiller = spiller(WINDOW_WIDTH/2,400,100,0,1)
alle_leveler = [
    level( # test level
    [
        felt(100,200,"interact","X", funk1),
        felt(100,300,"interact","Y", funk2)
    ],
    leveltick1,
    leveltegn1,
    levelstart1),
    level( # test level 2 med samme funk som level 1
    [
        felt(100,200,"interact","x", funk1),
        felt(100,300,"interact","y", funk2)
    ],
    leveltick1,
    leveltegn1,
    levelstart1)
]
_level = alle_leveler[0]
_level.start()
aktiv_felt = []
    # variabler til rendering
step_speed = 10
scale = 5
l = 5
r = 10
border = 3
bobble_lengde = WINDOW_WIDTH-border*4-200

# 6 - Loop for alltid
while True:

    # 7 - test etter og håndter events
    for event in pg.event.get():
        if event.type == pg.QUIT:
            _quit() # lukker programmet om vinduet krysses ut av bruker

    # 8 - gjør alle "per frame" handlinger
        # henter ny input fra bruker
    trykkede_taster = pg.key.get_pressed()
    trykkede_musetaster = pg.mouse.get_pressed()
        # oppdater tid
    tid += 1/FRAMES_PER_SECOND
        # beveg spiller
    if (len(alle_snakkebobbler)==0): # --------------------------------------------------------
        if (trykkede_taster[K_LEFT]): _spiller.x -= speed; _spiller.dir=-1
        if (trykkede_taster[K_RIGHT]): _spiller.x += speed; _spiller.dir=1
        if (not (trykkede_taster[K_LEFT] ^ trykkede_taster[K_RIGHT])):
            _spiller.dir = 0
    t = alle_leveler.index(_level)
    if (_spiller.x>WINDOW_WIDTH):
        if (t < len(alle_leveler)-1):
            _level = alle_leveler[t+1]
            _level.start()
            _spiller.x = 0
            print("Ny level:", t+1)
        else:
            _spiller.x = WINDOW_WIDTH
    if (_spiller.x<0):
        if (t > 0):
            _level = alle_leveler[t-1]
            _level.start()
            _spiller.x = WINDOW_WIDTH
            print("Ny level:", t-1)
        else:
            _spiller.x = 0
        # oppdater felt
    aktiv_felt = []
    for i in _level.alle_felt:
        if (spiller_i_felt(i)):
            aktiv_felt.append(i)
        # oppdater komando
    for i in range(len(keys)):
        if (tast_trykket(keys[i])):
            komando += str(keys_alt[i])
    if (tast_trykket(K_BACKSPACE)):
        komando = komando[:-1]
    if (tast_trykket(K_RETURN)):
        for i in aktiv_felt:
            if (komando == i.hotkey.upper()):
                i.funk()
        komando = ""
        # oppdater snakkeboble
    if (tast_trykket(K_TAB)):
        alle_snakkebobbler = alle_snakkebobbler[1:]

        # oppdater levelen
    _level.oppdater()

        # lagrer denne framens input til neste frame. Vi bruker dette for å teste etter endringer
    trykkede_taster_f = trykkede_taster
    trykkede_musetaster_f = trykkede_musetaster

    # 9 - Clear the window
    window.fill(BACKGROUND)

    # 10 - tegn alle elementene
        # tegn level
    _level.tegn()
        # tegn spiller
    if (_spiller.dir == 1 or _spiller.dir == -1):
        step_d = (math.sin(tid*step_speed)+1)/2
    else: step_d = 0
    if (_spiller.dir == 1):
        pg.draw.line(window, (50, 50, 50), (_spiller.x, _spiller.y-(30)*scale), (_spiller.x, _spiller.y-(10)*scale), l*scale) # kropp
        pg.draw.rect(window, (139, 69, 19), (_spiller.x+(-10-l/2)*scale, _spiller.y-(30+step_d)*scale, 10*scale, 15*scale)) # sekk
        pg.draw.ellipse(window, (50, 50, 50), (_spiller.x-r/2*scale, _spiller.y-(30+r)*scale, r*scale, r*scale)) # hode
        pg.draw.line(window, (50, 50, 50), (_spiller.x-7*scale, _spiller.y-(30+r/2)*scale), (_spiller.x+7*scale, _spiller.y-(30+r/2)*scale), int(round(l*scale/5,0))) # hatt
    if (_spiller.dir == -1):
        pg.draw.line(window, (50, 50, 50), (_spiller.x, _spiller.y-(30)*scale), (_spiller.x, _spiller.y-(10)*scale), l*scale) # kropp
        pg.draw.rect(window, (139, 69, 19), (_spiller.x+l/2*scale, _spiller.y-(30+step_d)*scale, 10*scale, 15*scale)) # sekk
        pg.draw.ellipse(window, (50, 50, 50), (_spiller.x-r/2*scale, _spiller.y-(30+r)*scale, r*scale, r*scale)) # hode
        pg.draw.line(window, (50, 50, 50), (_spiller.x-7*scale, _spiller.y-(30+r/2)*scale), (_spiller.x+7*scale, _spiller.y-(30+r/2)*scale), int(round(l*scale/5,0))) # hatt
    if (_spiller.dir == 0):
        pg.draw.line(window, (50, 50, 50), (_spiller.x, _spiller.y-(30)*scale), (_spiller.x, _spiller.y-(10)*scale), l*scale) # kropp
        pg.draw.rect(window, (139, 69, 19), (_spiller.x+(-5)*scale, _spiller.y-(30+step_d)*scale, 10*scale, 15*scale)) # sekk
        pg.draw.ellipse(window, (50, 50, 50), (_spiller.x-r/2*scale, _spiller.y-(30+r)*scale, r*scale, r*scale)) # hode
        pg.draw.line(window, (50, 50, 50), (_spiller.x-7*scale, _spiller.y-(30+r/2)*scale), (_spiller.x+7*scale, _spiller.y-(30+r/2)*scale), int(round(l*scale/5,0))) # hatt
        # tegn komando boks
    pg.draw.rect(window, (0, 0, 0), (0,WINDOW_HEIGHT-50,WINDOW_WIDTH,50))
    txt = "> " + komando
    if (math.sin(tid*5)<=0): txt += "_"
    bilde = font.render(txt, True, (255, 255, 255))
    window.blit(bilde, (10, WINDOW_HEIGHT+(50-font.size(txt)[1])/2-50))
        # tegn tips linje
    t = 10
    for i in aktiv_felt:
        txt = "[" + i.hotkey + "] " + i.forklaring
        bilde = font.render(txt, True, (255, 255, 255))
        window.blit(bilde, (10, t))
        t += font.size(txt)[1] + 5
        # tegn sankke boble
    if (alle_snakkebobbler != []):
        pg.draw.rect(window, (100, 100, 100), (0,WINDOW_HEIGHT-100,WINDOW_WIDTH,50))
        pg.draw.rect(window, (255, 255, 255), (border,WINDOW_HEIGHT-(100-border),WINDOW_WIDTH-border*2,50-border*2))
        txt = alle_snakkebobbler[0]
        if (len(alle_snakkebobbler) >= 2): txt += " ..."
        bilde = font.render(txt, True, (0, 0, 0))
        window.blit(bilde, (20, WINDOW_HEIGHT-100+(50-font.size(txt)[1])/2))
        txt = "Trykk TAB for neste"
        bilde = font_small.render(txt, True, (0, 0, 0))
        window.blit(bilde, (WINDOW_WIDTH-font_small.size(txt)[0]-border*2, WINDOW_HEIGHT-50-font_small.size(txt)[1]-border*2))
        txt = "(" + str(len(alle_snakkebobbler)-1) +  ")"
        bilde = font_small.render(txt, True, (0, 0, 0))
        window.blit(bilde, (WINDOW_WIDTH-font_small.size(txt)[0]-border*2, WINDOW_HEIGHT-100+border*2))

    # 11 - Update the window
    pg.display.update()

    # 12 - Slow things down a bit
    clock.tick(FRAMES_PER_SECOND)  # make pygame wait


import numpy as np

from sklearn import datasets

from sklearn.neighbors import KNeighborsRegressor
from cvxopt import solvers, matrix
from sklearn.metrics import mean_squared_error

def read_libsvm(filename):
    """ Read libsvm data from '.txt' file and split to training data and test data
    :param filename: a string indicates the file name
    :return: training instances (x), training labels (y)
    """

    x, y = datasets.load_svmlight_file(filename)
    x = x.toarray()

    return x, y

def split(train_x, train_y, n_labels=10):

    """ Split labeled data and unlabeled data for SSL task
    :param train_x: all training instances
    :param train_y: all training labels
    :param n_labels: numer of labeled instances
    :return: supervised data (x_sup, y_sup) and unsupervised data (x_unsup, y_unsup) where y_unsup can be used to
    test model performance.
    """

    np.random.seed(5)

    n = train_x.shape[0]
    rand_index = np.random.permutation(np.arange(n))

    labels_index = rand_index[:n_labels]
    unlabels_index = rand_index[n_labels:]

    x_sup = train_x[labels_index, :]
    y_sup = train_y[labels_index]

    x_unsup = train_x[unlabels_index, :]
    y_unsup = train_y[unlabels_index]

    return x_sup, y_sup, x_unsup, y_unsup

def baseline_predict(sup_x, sup_y, unsup_x):
    """This is a  1NN regressor with euclidean distance measure.

        sup_x : array-like
            laeled data matrix with [n_samples, n_features]

        y : array-like
            label  matrix with [n_sample, ]

        unsup_x: array-like
            unlabeled data matrix with [n_samples, n_features]
    """

    knn = KNeighborsRegressor(n_neighbors=1)
    knn.fit(sup_x, sup_y)
    [dist, idx] = knn.kneighbors(unsup_x)

    baseline_prediction = np.zeros((len(unsup_x), 1))
    for t in range(0, len(unsup_x)):
        baseline_prediction[t] = np.mean(
            np.array([sup_y[i] for i in idx[t]]).flatten())

    return baseline_prediction

def fit_estimator(sup_x, sup_y, unsup_x, n_neighbors=3, metric='minkowski'):
    """Provide ssl methods according to self-training

    """
    knn = KNeighborsRegressor(n_neighbors=n_neighbors, metric=metric)

    knn.fit(sup_x, sup_y)

    [dist, idx] = knn.kneighbors(unsup_x)

    label_l = sup_y

    label_u = np.zeros((len(unsup_x),))
    for t in range(0, len(unsup_x)):
        label_u[t] = np.mean(np.array([sup_y[i] for i in idx[t]]).flatten())

    y = np.hstack((label_l, label_u))
    [dist, idx] = knn.kneighbors(unsup_x)

    for i in range(0, 5):
        label_u = np.zeros((len(unsup_x),))
        for t in range(0, len(unsup_x)):
            label_u[t] = np.mean(np.array([y[i] for i in idx[t]]).flatten())
        y = np.hstack((label_l, label_u))

    return label_u

def fit_pred(candidate_prediction=None, baseline_prediction=None):
    """

    Parameters
    ----------
    candidate_prediction :array-like, optical(default=None)
        a matrix with size instance_num * candidate_num . Each
        column vector of candidate_prediction is a candidate regression
        result.

    baseline_prediction :array-like, optical(default=None)
        a column vector with length instance_num. It is the regression
        result of the baseline method.

    Return
    ------
    Safe_prediction : array-like
        a predictive result
    x_values: array-like
        weight of each base learner

    """

    if candidate_prediction is None:
        raise ValueError("Please provide candidate prediction or "
                         "call the function that generates the prediction "
                         "result in this algorithm.")
    else:
        if baseline_prediction is None and baseline_prediction is None:
            raise ValueError("Please provide candidate prediction or "
                             "call the function that generates the prediction result "
                             "in this algorithm.")

    candidate_num = candidate_prediction.shape[1]

    H = np.dot(candidate_prediction.T, candidate_prediction) * 2
    f = -2 * np.dot(candidate_prediction.T, baseline_prediction)
    Aeq = np.ones((1, candidate_num))
    beq = 1.0

    lb = np.zeros((candidate_num, 1))
    ub = np.ones((candidate_num, 1))
    h = np.vstack((lb, ub))
    G_lb = -1 * np.eye(candidate_num, candidate_num)
    G_ub = np.eye(candidate_num, candidate_num)
    G = np.vstack((G_lb, G_ub))

    sln = solvers.qp(matrix(H), matrix(f), matrix(G), matrix(h),
                     matrix(Aeq), matrix(beq))

    x_value = sln['x'] # weights for base learners

    safe_prediction = np.zeros((baseline_prediction.shape[0], 1))
    for i in range(0, candidate_num):
        safe_prediction[:, 0] = safe_prediction[:,
                                0] + x_value[i] * candidate_prediction[:, i]

    return safe_prediction, x_value

def cal_mse(y_pred, y_truth):
    return mean_squared_error(y_truth, y_pred)

if __name__ == "__main__":

    #construct dataset
    train_x, train_y = read_libsvm("data/abalone.txt")
    sup_x, sup_y, unsup_x, unsup_y = split(train_x, train_y)

    #base predictions
    candidate_prediction1 = fit_estimator(
        sup_x, sup_y, unsup_x, n_neighbors=3, metric='euclidean')
    candidate_prediction2 = fit_estimator(
        sup_x, sup_y, unsup_x, n_neighbors=3, metric='cosine')
    candidate_prediction3 = fit_estimator(
        sup_x, sup_y, unsup_x, n_neighbors=3)

    candidate_prediction = np.stack(
        [candidate_prediction1, candidate_prediction2, candidate_prediction3], axis=1)
    baseline_prediction = baseline_predict(sup_x, sup_y, unsup_x)

    #safe prediction
    safe_prediction, weights = fit_pred(candidate_prediction, baseline_prediction)

    print("Mse of baseline prediction: ", cal_mse(baseline_prediction, unsup_y))
    print("Mse of candidate prediction1: ", cal_mse(candidate_prediction1, unsup_y))
    print("Mse of candidate prediction2: ", cal_mse(candidate_prediction2, unsup_y))
    print("Mse of candidate prediction3: ", cal_mse(candidate_prediction3, unsup_y))
    print("Mse of safe prediction: ", cal_mse(safe_prediction, unsup_y))
    print("Weights of base learners: ", weights)