from textwrap import indent
from PySide2.QtCore import QObject, Signal, Slot, Property, QUrl, QAbstractListModel, QByteArray
from PySide2.QtGui import QGuiApplication
from PySide2.QtQuick import QQuickView
from PySide2.QtPositioning import QGeoCoordinate
from PySide2 import QtCore
from enum import Enum
import typing
import sys
import json

VIEW_URL = "superview.qml"
PUVODNI_LIST_FILE = "data_ukol1_znak.json"

#Class for maintaining list of tasks
class FiltrModel(QAbstractListModel):

    #Enum with added custom roles
    class Roles(Enum):
        LOCATION = QtCore.Qt.UserRole+0
        AREA = QtCore.Qt.UserRole+1
        POPULATION = QtCore.Qt.UserRole+2
        TYP = QtCore.Qt.UserRole+3
        KRAJ = QtCore.Qt.UserRole+4
        OKRES = QtCore.Qt.UserRole+5
        ZNAK = QtCore.Qt.UserRole+6

    #Initialize and load list from given file
    def __init__ (self,filename=None):
        QAbstractListModel.__init__(self)
        self._min_po = 0
        self._max_po = 1500000
        self._min_area = 0.0
        self._max_area = 500.0
        self._obce = False
        self._mesta = False
        self.kraj_filtr = ["všechny"]
        self.okres_filtr = ["všechny"]
        self.kraj_all = set()
        self.okres_all = set()
        self._output_file = ""
        self.city_list = []
        self.puvodni_list = []
        if filename:
            self.load_from_json(filename)

    #Property min_po
    def get_min_po(self):
        return self._min_po

    def set_min_po(self,val):
        if val != self.min_po:
            self._min_po = val
            self.min_po_changed.emit()

    min_po_changed = Signal()
    min_po = Property(int, get_min_po, set_min_po, notify=min_po_changed)

    #Property max_po
    def get_max_po(self):
        return self._max_po

    def set_max_po(self,val):
        if val != self.max_po:
            self._max_po = val
            self.max_po_changed.emit()

    max_po_changed = Signal()
    max_po = Property(int, get_max_po, set_max_po, notify=max_po_changed)

    #Property min_area
    def get_min_area(self):
        return self._min_area

    def set_min_area(self,val):
        if val != self.min_area:
            self._min_area = int(val)
            self.min_area_changed.emit()

    min_area_changed = Signal()
    min_area = Property(float, get_min_area, set_min_area, notify=min_area_changed)

    #Property max_area
    def get_max_area(self):
        return self._max_area

    def set_max_area(self,val):
        if val != self.max_area:
            self._max_area = int(val)
            self.max_area_changed.emit()

    max_area_changed = Signal()
    max_area = Property(float, get_max_area, set_max_area, notify=max_area_changed)

    #Property obce
    def get_obce(self):
        return self._obce

    def set_obce(self,val):
        if val != self.obce:
            self._obce = val
            self.obce_changed.emit()

    obce_changed = Signal()
    obce = Property(bool, get_obce, set_obce, notify=obce_changed)

    #Property mesta
    def get_mesta(self):
        return self._mesta

    def set_mesta(self,val):
        if val != self.mesta:
            self._mesta = val
            self.mesta_changed.emit()

    mesta_changed = Signal()
    mesta = Property(bool, get_mesta, set_mesta, notify=mesta_changed)

    #Property output_file
    def get_output_file(self):
        return self._output_file

    def set_output_file(self,val):
        if val != self.output_file:
            self._output_file = val
            self.output_file_changed.emit()

    output_file_changed = Signal()
    output_file = Property(str, get_output_file, set_output_file, notify=output_file_changed)

    #Load list of cities from given file
    def load_from_json(self,filename):
        with open(filename,encoding="utf-8") as f:
            self.puvodni_list = json.load(f)

            for c in self.puvodni_list:
                #Create QGeoCoordinate from the original JSON location
                pos = c['location']
                lon,lat = pos.split("(")[1].split(")")[0].split(" ")
                c['location'] = QGeoCoordinate(float(lat),float(lon))

                #Add value of 'krajLabel' and 'okresLabel' to set property
                self.kraj_all.add(c['krajLabel'])
                self.okres_all.add(c['okresLabel'])

                #Check if feature contains attribute 'mestoLabel' and set value to 'obec' if not
                if "mestoLabel" not in c:
                    c["mestoLabel"] = "obec"

                #Check if feature contains URL of its symbol, add URL of white rectangle if not
                if "znak" not in c:
                    c["znak"] = "https://www.meme-arsenal.com/memes/422425412debbe780318e9bfee9efdac.jpg" #bílý obdélník

    #Return number of cities in the list
    def rowCount(self, parent:QtCore.QModelIndex=...) -> int:
        """ Return number of cities in the list"""
        return len(self.city_list)

    #For given index and role return information of the city
    def data(self, index:QtCore.QModelIndex, role:int=...) -> typing.Any:
        if role == QtCore.Qt.DisplayRole: # On DisplayRole return name
            return self.city_list[index.row()]["obecLabel"]
        elif role == self.Roles.LOCATION.value: # On location role return coordinates
            return self.city_list[index.row()]["location"]
        elif role == self.Roles.AREA.value: # On area role return area
            return self.city_list[index.row()]["area"]
        elif role == self.Roles.POPULATION.value: # On population role return population
            return self.city_list[index.row()]["population"]
        elif role == self.Roles.TYP.value: # On typ role return city/municipality
            return self.city_list[index.row()]["mestoLabel"]
        elif role == self.Roles.KRAJ.value: # On kraj role return name of the region
            return self.city_list[index.row()]["krajLabel"]
        elif role == self.Roles.OKRES.value: # On okres role return name of the district
            return self.city_list[index.row()]["okresLabel"]
        elif role == self.Roles.ZNAK.value: # On znak role return URL of city's symbol
            return self.city_list[index.row()]["znak"]

    #Returns dict with role numbers and role names for default and custom roles together
    def roleNames(self) -> typing.Dict[int, QByteArray]:
        roles = super().roleNames()
        roles[self.Roles.LOCATION.value] = QByteArray(b'location')
        roles[self.Roles.AREA.value] = QByteArray(b'area')
        roles[self.Roles.POPULATION.value] = QByteArray(b'population')
        roles[self.Roles.TYP.value] = QByteArray(b'typ')
        roles[self.Roles.KRAJ.value] = QByteArray(b'kraj')
        roles[self.Roles.OKRES.value] = QByteArray(b'okres')
        roles[self.Roles.ZNAK.value] = QByteArray(b'znak')
        return roles

    #Sets the value of property kraj_filtr
    @Slot(str)
    def set_kraj(self, val: str):
        self.kraj_filtr = []
        self.kraj_filtr.append(val)
        print(self.kraj_filtr)

    #Sets the value of property okres_filtr
    @Slot(str)
    def set_okres(self, val: str):
        self.okres_filtr = []
        self.okres_filtr.append(val)
        print(self.okres_filtr)

    #Filters the original list of cities
    @Slot()
    def filtrovat(self):
        #Delete all rows from city_list
        self.beginRemoveRows(self.index(0).parent(), 0 , len(self.city_list)-1)
        self.city_list = []
        self.endRemoveRows()

        #Set values of kraj_filtr and okres_filtr if all menu items are chosen
        if "všechny" in self.kraj_filtr:
            self.kraj_filtr = self.kraj_all

        if "všechny" in self.okres_filtr:
            self.okres_filtr = self.okres_all

        #Filter the original list and add items to new one
        input_idx = 0
        for feature in self.puvodni_list:
            pocet_obyv = int(feature["population"])
            rozloha = float(feature["area"])

            #Municipalities
            if self.obce == True:
                if feature["mestoLabel"] == "obec"\
                    and pocet_obyv > self.min_po and pocet_obyv < self.max_po\
                    and rozloha > self.min_area and rozloha < self.max_area\
                    and feature["krajLabel"] in self.kraj_filtr\
                    and feature["okresLabel"] in self.okres_filtr:

                    self.beginInsertRows(self.index(0).parent(),input_idx,input_idx)
                    self.city_list.append(feature)
                    self.endInsertRows()
                    input_idx += 1

            #Towns
            if self.mesta == True:
                if feature["mestoLabel"] == "město v Česku"\
                    and pocet_obyv > self.min_po and pocet_obyv < self.max_po\
                    and rozloha > self.min_area and rozloha < self.max_area\
                    and feature["krajLabel"] in self.kraj_filtr\
                    and feature["okresLabel"] in self.okres_filtr:

                    self.beginInsertRows(self.index(0).parent(),input_idx,input_idx)
                    self.city_list.append(feature)
                    self.endInsertRows()
                    input_idx += 1

        print(f"Načteno {input_idx} obcí.")

    #Save filtered list of cities to a file
    @Slot()
    def save_to_file(self):
        with open(self.output_file, 'w', encoding ='utf8') as json_file:
            for feature in self.city_list:
                feature["location"] = QGeoCoordinate.toString(feature["location"])
            json.dump(self.city_list, json_file, ensure_ascii = False, indent=4)

app = QGuiApplication(sys.argv)
view = QQuickView()
url = QUrl(VIEW_URL)
puvodnilist_model = FiltrModel(PUVODNI_LIST_FILE)
ctxt = view.rootContext()
ctxt.setContextProperty('filtrModel',puvodnilist_model)
view.setSource(url)
view.show()
app.exec_()


"""
This script is used to get detailed data about experiments from the 'learning1'.

Unfortunately, it cannot be used as-is because it relies on the Smart Grid
simulator (located in `/Users/rchaput/PhD/Code`) and experiments data
(located in `../data/contribution1/remote/`).
It is provided just in case, to show how the experiments data were processed
to produce `agents_details.pkl.xz`.

Detailed data include:
- rewards per agent at each time step
- properties (comfort, equity, ...) about agents at each time step
- etc.
"""

import os
from pathlib import Path
from glob import glob
import sys
import traceback

import pandas as pd
from tqdm import tqdm

from scripts.common import get_run_id

sys.path.insert(0, '/Users/rchaput/PhD/Code')
from src.simulator import Simulator

# Paths to the different folders
data_dir = (Path(__file__).parent / '..' / 'data/contribution1').resolve()
print(f'Data dir = {data_dir}')
experiments_dir = data_dir / 'remote'
print(f'Experiments dir = {experiments_dir}')

# Variables that describe both our experiments and the file hierarchy
all_models = ['DDPG', 'MADDPG', 'Q-DSOM-1', 'Q-SOM-1']
all_profiles = ['annually']
all_sizes = ['small']
all_rewards = ['equity', 'overconsumption', 'multiobj-prod', 'multiobj-sum',
               'adaptability1', 'adaptability2']

def get_details():
    data = []
    for model in tqdm(all_models, position=0):
        for profile in tqdm(all_profiles, position=1):
            for size in tqdm(all_sizes, position=2):
                for reward in tqdm(all_rewards, position=3):
                    path = os.path.join(experiments_dir, model, profile, size, reward)
                    runs = glob(f'{path}/job_*/run_*/')
                    for run in tqdm(runs, position=4):
                        try:
                            run_id = get_run_id(run)
                            sim = Simulator.load(os.path.join(run, 'data.pkl.bz'))
                            for num_step, step in tqdm(enumerate(sim.env.history), position=5):
                                for agent in tqdm(sim.env.agents, position=6):
                                    agent_reward = step.rewards[agent]
                                    agent_comfort = step.agent_comfort[agent]
                                    data.append({
                                        'Model': model,
                                        'ConsumptionProfile': profile,
                                        'EnvironmentSize': size,
                                        'RewardFunction': reward,
                                        'Run': run_id,
                                        'Step': num_step,
                                        'Agent': agent.name,
                                        'Reward': agent_reward,
                                        'Comfort': agent_comfort,
                                    })
                            del sim
                        except:
                            traceback.print_exc()
    df = pd.DataFrame(data)
    return df

if __name__ == '__main__':
    df = get_details()
    result_file = data_dir / 'agents_details.pkl.xz'
    df.to_pickle(result_file, compression='xz')


import subprocess

class Executor(object):
    """
    Wrapper for subprocess
    """

    def __init__(self, command):
        self.cmd = command

    def execute(self):
        """
        Executes the given command

        Returns a tuple of (exit code, stdout+stderr)
        """
        ret = ''
        exit_code = 0
        try:
            ret = subprocess.check_output(self.cmd, stderr=subprocess.STDOUT, shell=True)
            ret = ret.rstrip()
        except subprocess.CalledProcessError as error:
            exit_code = error.returncode
            ret = error.output.rstrip()
        return exit_code, ret


import matplotlib.pyplot as plt
import numpy as np

x = [0.998175, 1.10364, 0.531075]

f = lambda t: x[0] + x[1]*t + x[2]*(t**2)
f_taylor = lambda t: 1 + t + ((t**2)/2)

t_range = np.linspace(-1.0, 1.0, 100)

yf = f(t_range)
yf_taylor = f_taylor(t_range)

fig = plt.figure(constrained_layout=True)
axis = fig.subplots(2,2)
axis[0,0].plot(t_range, yf, 'r', label="$f(t)$")
axis[0,0].legend()
axis[0,0].set_title("Funcao aproximada")

axis[0,1].plot(t_range, yf_taylor, 'b', label="$f_{taylor}(t)$")
axis[0,1].legend()
axis[0,1].set_title("Funcao taylor")

axis[1,0].plot(t_range, np.exp(t_range), 'g', label="$e^t$")
axis[1,0].legend()
axis[1,0].set_title("Funcao $e^t$")

axis[1,1].plot(t_range, yf, 'r', label="$f(t)$")
axis[1,1].plot(t_range, yf_taylor, 'b', label="$f_{taylor}(t)$")
axis[1,1].plot(t_range, np.exp(t_range), 'g', label="$e^t$")
axis[1,1].legend()
axis[1,1].set_title("As tres juntas")

plt.show()


from workshops.models import Workshop
import os
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'conference-site-django.settings'
django.setup()

workshop1 = Workshop(
    title="Microsoft Powerpoint",
    session=1,
    roomnumber=1,
    starttime='9:30AM',
    endtime='12:00PM',
)
workshop1.save()

workshop2 = Workshop(
    title="Microsoft Excel",
    session=1,
    roomnumber=2,
    starttime='9:30AM',
    endtime='12:00PM',
)
workshop2.save()

workshop3 = Workshop(
    title="Microsoft Word",
    session=1,
    roomnumber=3,
    starttime='9:30AM',
    endtime='12:00PM',
)
workshop3.save()

workshop4 = Workshop(
    title="Email Etiquette",
    session=2,
    roomnumber=1,
    starttime='1:30PM',
    endtime='4:00PM',
)
workshop4.save()

workshop5 = Workshop(
    title="One-on-one Meeting Etiquette",
    session=2,
    roomnumber=2,
    starttime='1:30PM',
    endtime='4:00PM',
)
workshop5.save()

workshop6 = Workshop(
    title="One-on-one Meeting Etiquette",
    session=2,
    roomnumber=3,
    starttime='1:30PM',
    endtime='4:00PM',
)
workshop6.save()

workshop7 = Workshop(
    title="Team Building",
    session=3,
    roomnumber=1,
    starttime='1:30PM',
    endtime='4:00PM',
)
workshop7.save()

workshop8 = Workshop(
    title="Strategies to Improve Focus",
    session=3,
    roomnumber=2,
    starttime='1:30PM',
    endtime='4:00PM',
)
workshop8.save()

workshop9 = Workshop(
    title="Effective Communication",
    session=3,
    roomnumber=3,
    starttime='1:30PM',
    endtime='4:00PM',
)
workshop9.save()


import socket
import thread
import xml.etree.ElementTree as ET
import emb

x = 0.0
y = 0.0
z = 0.0

posLock = thread.allocate_lock()
goLock = thread.allocate_lock()

host = '127.0.0.1'
port = 1000
BUF_SIZE = 1024

theID = 0
sock = None

def Parse(document):
    import xml.dom.minidom
    from xml.dom.minidom import Node
    dom = xml.dom.minidom.parseString(document)

    for name in dom.getElementsByTagName("user"):
        theID = name.getAttribute('id')
        children = name.getElementsByTagName("pos")
        for child in children:
            if child.nodeType == Node.ELEMENT_NODE:
                emb.update_user(
                    int(theID),
                    float(child.getAttribute("x")),
                    float(child.getAttribute("y")),
                    float(child.getAttribute("z"))
                    )

def SetPos(newx, newy, newz):
    global x, y, z, posLock
    posLock.acquire()
    x, y, z = newx, newy, newz
    posLock.release()

def Connect():
    global host, port, BUF_SIZE, sock, theID
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    theID = sock.recv( BUF_SIZE )
    return int(theID)

def Run():
    global sock, theID
    allinfo = sock.recv(2048)
    Parse(str(allinfo))
    return allinfo

def Send():
    global sock, theID
    pos = '<user id="' + theID + '">'
    posLock.acquire()
    pos += '<pos x="' + str(x) + '" y="' + str(y) + '" z="'+ str(z) + '"/>'
    posLock.release()
    pos += "</user>"
    sock.send(pos)

if __name__=="__main__":
    Connect()
    while 1:
        Run()
        Send()
#   locks = []
#
#    lock = thread.allocate_lock()
#    lock.acquire()
#    locks.append(lock)
#    thread.start_new_thread( mailLoop, (lock, ) )
#
#    for i in range( len(locks) ):
#        while locks[ i ].locked() : pass


import cv2
import time
import math
import os

import numpy as np
import pandas as pd
import holisticModule as hm

class Preprocessor:
    def __init__(self):
        self.matrix = np.zeros(42)

    def add_columns(self, feat_vector):
        feat_vector = np.array(feat_vector)
        self.matrix = np.column_stack((self.matrix, feat_vector))

    def cvt2csv(self, file_path, file):
        print("PRUEBA 2")
        file = os.path.basename(file)[0:-4]
        file_name = file_path+file+'.csv'
        print(file_name)
        df = pd.DataFrame(self.matrix)
        if self.matrix.shape[1] > 1:
            df = df.iloc[:, 1:]  # Drops first column
        print("PRUEBA")
        print(file_name)
        df.to_csv(file_name, index=False)

    def convert2csv(self, file_path, file):
        file = os.path.basename(file)[0:-4]
        file_name = file_path+"/"+file+'.csv'
        df = pd.DataFrame(self.matrix)
        if self.matrix.shape[1] > 1:
            df = df.iloc[:, 1:]  # Drops first column
        df.to_csv(file_name, index=False)

    # p2 is ref point, If you want the the angle between the line defined by these two points and the horizontal axis
    def get_angleX(self, p1, p2):
        return int(np.round(np.arctan2((p2[1] - p1[1]), (p2[0] - p1[0])) * 180 / np.pi))

    def get_distance(self, p1, p2):
        coordinate_distance = math.sqrt(((p1[0] - p2[0]) ** 2) + ((p1[1] - p2[1]) ** 2))
        return int(coordinate_distance)

    # Calculate distance matrix from an specific point(in this case NOSE) to the other points of interest
    def get_distMat(self, matrixHolistic, matrixLeft, matrixRight):
        distList = []
        NOSE = matrixHolistic[0]
        for i in matrixHolistic:
            distList.append([self.get_distance(NOSE, i), self.get_angleX(NOSE, i)])  #{} or []
        for i in matrixLeft:
            distList.append([self.get_distance(NOSE, i), self.get_angleX(NOSE, i)])
        for i in matrixRight:
            distList.append([self.get_distance(NOSE, i), self.get_angleX(NOSE, i)])
        # print('Matriz de Dist')
        # print(distList)
        # print('Matriz de Dist - NOSE')
        # print(distList[1:])
        # print(f"El tamaño de DISTANCIA: {len(distList)}")
        return distList[1:]

def main():
    # Check current directory
    cwd = os.getcwd()
    # Check and create data directory
    if not (os.path.exists(f'{cwd}/data')):
        os.mkdir(f'{cwd}/data')
    DATA = cwd+'/data/'
    print(DATA)
    # Iterate through videos
    DATASET_PATH = '/home/d3m1ur60/Desktop/LSBv2/'
    PATH1 = '/home/d3m1ur60/Desktop/LSBv2/Ayuda/ayuda_V1-0002.mp4'
    PATH2 = '/home/d3m1ur60/Desktop/LSBv2/Bolivia/bolivia_V1-0033.mp4'
    cap = cv2.VideoCapture(PATH2)
    pastTime = 0
    detector = hm.HolisticDetector()
    counter = 0
    # TEST
    prep = Preprocessor()
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        # Call Detection
        detector.find_pose(frame)  # Must be called for each detection
        detector.get_lm()
        detector.draw_pose(frame)  # Optional, just for drawing
        mat1, mat2, mat3 = detector.get_matrix()  # Returns holistic,left hand,right hand
        feat_vec = prep.get_distMat(mat1, mat2, mat3)
        prep.add_columns(feat_vec)
        # Get FPS
        counter = counter + 1
        currentTime = time.time()
        fps = int(1 / (currentTime - pastTime))
        pastTime = currentTime
        # Draw INFO
        cv2.putText(frame, f'FPS:{fps}', (50, 50), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
        cv2.putText(frame, f'COUNT:{counter}', (350, 50), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
        # Display
        cv2.imshow("original", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    print(f'TOTAL: {counter}')
    print('INTENTO ')
    print(prep.matrix)
    print(prep.matrix.shape)
    print(DATA)
    prep.cvt2csv(DATA, PATH1)

if __name__ == '__main__':
    main()


from sage.structure.sequence import Sequence
from sage.rings.polynomial.pbori.pbori import BooleanPolynomialRing

class AlgebraicModel:

    def __init__(self, cipher):
        self._cipher = cipher
        self.input_postfix = "x"
        self.output_postfix = "y"
        self._ring = None

    def connection_polynomials(self):
        """
        Return a list of polynomials that connects system of equations from each component.

        INPUT:

        - None

        EXAMPLES::

            sage: from claasp.ciphers.block_ciphers.fancy_block_cipher import FancyBlockCipher
            sage: from claasp.cipher_modules.models.algebraic.algebraic_model import AlgebraicModel
            sage: fancy = FancyBlockCipher(number_of_rounds=1)
            sage: connection = AlgebraicModel(fancy).connection_polynomials()
            sage: connection[:24]
            [plaintext_y0 + sbox_0_0_x0,
             plaintext_y1 + sbox_0_0_x1,
             plaintext_y2 + sbox_0_0_x2,
             ...
             plaintext_y21 + sbox_0_5_x1,
             plaintext_y22 + sbox_0_5_x2,
             plaintext_y23 + sbox_0_5_x3]
        """
        return sum([self.connection_polynomials_at_round(r) for r in range(self._cipher.number_of_rounds)], [])

    def connection_polynomials_at_round(self, r):
        """
        Return a list of connection polynomials at round `r`.

        INPUT:

        - ``r`` -- **integer**

        EXAMPLES::

            sage: from claasp.ciphers.block_ciphers.fancy_block_cipher import FancyBlockCipher
            sage: from claasp.cipher_modules.models.algebraic.algebraic_model import AlgebraicModel
            sage: fancy = FancyBlockCipher(number_of_rounds=1)
            sage: connection = AlgebraicModel(fancy).connection_polynomials_at_round(0)
            sage: connection[:24]
            [plaintext_y0 + sbox_0_0_x0,
             plaintext_y1 + sbox_0_0_x1,
             plaintext_y2 + sbox_0_0_x2,
             ...
             plaintext_y21 + sbox_0_5_x1,
             plaintext_y22 + sbox_0_5_x2,
             plaintext_y23 + sbox_0_5_x3]
        """
        polynomials = []
        R = self.ring()

        for component in self._cipher.get_components_in_round(r):

            if component.type == "constant":
                continue

            input_vars = [component.id + "_" + self.input_postfix + str(i) for i in range(component.input_bit_size)]
            input_vars = list(map(R, input_vars))

            input_links = component.input_id_links
            input_positions = component.input_bit_positions

            prev_input_vars = []
            for k in range(len(input_links)):
                prev_input_vars += [input_links[k] + "_" + self.output_postfix + str(i) for i in
                                    input_positions[k]]
            prev_input_vars = list(map(R, prev_input_vars))

            polynomials += [x + y for (x, y) in zip(input_vars, prev_input_vars)]

        return polynomials

    def is_algebraically_secure(self, timeout):
        """
        Return `True` if the cipher is resistant against algebraic attack.

        INPUT:

        - ``timeout`` -- **integer**; the timeout for the Grobner basis computation in seconds

        EXAMPLES::

            sage: from claasp.cipher_modules.models.algebraic.algebraic_model import AlgebraicModel
            sage: from claasp.ciphers.block_ciphers.identity_block_cipher import IdentityBlockCipher
            sage: identity = IdentityBlockCipher()
            sage: algebraic = AlgebraicModel(identity)
            sage: algebraic.is_algebraically_secure(120)
            False
        """
        from cysignals.alarm import alarm, cancel_alarm

        try:
            alarm(timeout)
            self.polynomial_system().groebner_basis()
            cancel_alarm()
            result = False
        except InterruptedError:
            result = True

        return result

    def nvars(self):
        """
        Return the number of variables.

        INPUT:

        - None

        EXAMPLES::

            sage: from claasp.ciphers.block_ciphers.fancy_block_cipher import FancyBlockCipher
            sage: from claasp.cipher_modules.models.algebraic.algebraic_model import AlgebraicModel
            sage: fancy = FancyBlockCipher(number_of_rounds=1)
            sage: AlgebraicModel(fancy).nvars()
            96
        """
        nvars = 0
        for cipher_round in self._cipher.rounds_as_list:
            for component in cipher_round.components:
                if component.type == "sbox" or component.type == "linear_layer":
                    nvars += component.input_bit_size + component.output_bit_size

        return nvars

    def polynomial_system(self):
        """
        Return a polynomial system for the cipher.

        INPUT:

        - None

        EXAMPLES::

            sage: from claasp.ciphers.block_ciphers.fancy_block_cipher import FancyBlockCipher
            sage: from claasp.cipher_modules.models.algebraic.algebraic_model import AlgebraicModel
            sage: fancy = FancyBlockCipher(number_of_rounds=1)
            sage: AlgebraicModel(fancy).polynomial_system()  # long time
            Polynomial Sequence with 468 Polynomials in 384 Variables
        """
        polynomials = sum([self.polynomial_system_at_round(r) for r in range(self._cipher.number_of_rounds)], [])
        polynomials += self.connection_polynomials()

        return Sequence(polynomials)

    def polynomial_system_at_round(self, r):
        """
        Return a polynomial system at round `r`.

        INPUT:

        - ``r`` -- **integer**; round index

        EXAMPLES::

            sage: from claasp.ciphers.block_ciphers.fancy_block_cipher import FancyBlockCipher
            sage: from claasp.cipher_modules.models.algebraic.algebraic_model import AlgebraicModel
            sage: fancy = FancyBlockCipher(number_of_rounds=1)
            sage: AlgebraicModel(fancy).polynomial_system_at_round(0) # long time
            Polynomial Sequence with 252 Polynomials in 288 Variables
        """
        if not 0 <= r < self._cipher.number_of_rounds:
            raise ValueError(f"r must be in the range 0 <= r < {self._cipher.number_of_rounds}")

        polynomials = []

        for component in self._cipher.get_components_in_round(r):
            component_type = component.type
            operation = component.description[0]
            component_types = ["sbox", "linear_layer", "mix_column", "constant"]
            operations = ["XOR", "AND", "OR", "SHIFT", "ROTATE", "NOT"]

            if component_type in component_types or (component_type == "word_operation" and operation in operations):
                polynomials += component.algebraic_polynomials(self)

        return Sequence(polynomials)

    def ring(self):
        """
        Return the polynomial ring for the system of equations.

        INPUT:

        - None

        EXAMPLES::

            sage: from claasp.ciphers.block_ciphers.fancy_block_cipher import FancyBlockCipher
            sage: from claasp.cipher_modules.models.algebraic.algebraic_model import AlgebraicModel
            sage: from claasp.cipher_modules.models.algebraic.boolean_polynomial_ring import is_boolean_polynomial_ring
            sage: fancy = FancyBlockCipher(number_of_rounds=1)
            sage: ring = AlgebraicModel(fancy).ring()
            sage: is_boolean_polynomial_ring(ring)
            True

            sage: ring.ngens()
            432
        """
        if self._ring is not None:
            return self._ring

        names = self.var_names()
        self._ring = BooleanPolynomialRing(len(names), names, order="degneglex")

        return self._ring

    def var_names(self):
        """
        Return a list of variable names in the polynomial ring.

        INPUT:

        - None

        EXAMPLES::

            sage: from claasp.ciphers.block_ciphers.fancy_block_cipher import FancyBlockCipher
            sage: from claasp.cipher_modules.models.algebraic.algebraic_model import AlgebraicModel
            sage: fancy = FancyBlockCipher(number_of_rounds=1)
            sage: var_names = AlgebraicModel(fancy).var_names()
            sage: var_names[0]
            'sbox_0_0_x0'
        """
        var_names = []

        for component in self._cipher.get_all_components():
            component_id = component.id
            input_size = component.input_bit_size
            output_size = component.output_bit_size

            if component.type != "constant":
                var_names += [component_id + "_" + self.input_postfix + str(i) for i in range(input_size)]
            var_names += [component_id + "_" + self.output_postfix + str(i) for i in range(output_size)]

            if component.type == "word_operation" and component.description[0].lower() == "modadd":
                ninput_words = component.description[1]
                nadditions = ninput_words - 1

                for n in range(nadditions):
                    # carry variables
                    var_names += [component_id + "_" + "c" + str(n) + "_" + str(i) for i in range(output_size)]
                    if n < nadditions - 1:
                        # aux output variables
                        var_names += \
                            [component_id + "_" + "o" + str(n) + "_" + str(i) for i in range(output_size)]

        for i in range(len(self._cipher.inputs)):
            var_names += [self._cipher.inputs[i] + "_" +
                          self.output_postfix + str(j) for j in range(self._cipher.inputs_bit_size[i])]

        return var_names


import pygame
from plane_sprite import *

pygame.init()

screen = pygame.display.set_mode((480, 650))

# initialize the game
bg = pygame.image.load('./images/background.png')
screen.blit(bg, (0, 0))

hero = pygame.image.load('./images/me1.png')
screen.blit(hero, (150, 300))

pygame.display.update()

# set clock
clock = pygame.time.Clock()

# set hero rect
hero_rect = pygame.Rect(150, 300, 102, 126)

# create sprites
enemy = GameSprite('./images/enemy1.png')
enemy1 = GameSprite('./images/enemy1.png', speed=2)

# create sprite group
enemy_group = pygame.sprite.Group(enemy, enemy1)

while True:
    clock.tick(60)
    hero_rect.y -= 1

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    if hero_rect.y <= -126:
        hero_rect.y = 650

    screen.blit(bg, (0, 0))
    screen.blit(hero, hero_rect)

    enemy_group.update()
    enemy_group.draw(screen)

    pygame.display.update()

pygame.quit()


i = 0
adet = int(input("Kaç adet ürün istiyorsunuz?: "))
urunler = []

while (i < adet):
    urunAdi = input("Ürün adı: ")
    urunFiyat = input("Ürün fiyat: ")
    urunler.append({
        "urunAdi" : urunAdi,
        "urunFiyat" : urunFiyat
    })

    i += 1

print(urunler)


import torch

class Dense(torch.nn.Module):
    """
    Fully-connected layer initialized directly with weights, rather than hyperparameters
    """

    def __init__(
        self,
        weight,
        bias,
        device=None,
        dtype=None
    ):
        """
        Parameters
        ----------
        weight : torch.Tensor or Tensor-like
            The weight matrix to use
        bias : torch.Tensor or Tensor-like
            The bias vector to use
        """
        factory_kwargs = {'device': device, 'dtype': dtype}
        super().__init__()
        self.w = torch.nn.Parameter(torch.Tensor(weight).to(**factory_kwargs))
        self.b = torch.nn.Parameter(torch.Tensor(bias).to(**factory_kwargs))

    def forward(self, inputs):
        """
        Call the layer on input data

        Parameters
        ----------
        inputs : torch.Tensor
            Inputs to call the layer's logic on

        Returns
        -------
        results : torch.Tensor
            The results of the layer's logic
        """
        out = torch.mm(inputs, self.w)
        out = torch.add(out, self.b)
        return out


msg = {3: "Fizz", 5: "Buzz"}

for i in range(1,101):
    activated = False
    for j in msg:
        if not i % j:
            activated = True
            print(msg[j], end="")

    end = "" if activated else i
    print(end)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: benoit.dufumier

"""
import sys; sys.path.append('../../')
import os, argparse
import numpy as np
import pandas as pd
import brainomics.image_preprocessing as preproc
import matplotlib
matplotlib.use('Agg')
import glob

def OUTPUT(dataset, output_path, modality='t1mri', mri_preproc='quasi_raw', type=None, ext=None):
    # type data64, or data32
    return os.path.join(output_path, dataset + "_" + modality+ "_" + mri_preproc +
                 ("" if type is None else "_" + type) + "." + ext)

def nii2npy(nii_path, phenotype_path, dataset_name, output_path, qc=None, sep='\t', id_type=str,
            check = dict(shape=(121, 145, 121), zooms=(1.5, 1.5, 1.5)), merge_ni_path=True):
    ########################################################################################################################

    phenotype = pd.read_csv(phenotype_path, sep=sep)
    qc = pd.read_csv(qc, sep=sep) if qc is not None else None

    if 'TIV' in phenotype:
        phenotype.rename(columns={'TIV': 'tiv'}, inplace=True)

    keys_required = ['participant_id', 'age', 'sex', 'tiv', 'diagnosis']

    assert set(keys_required) <= set(phenotype.columns), \
        "Missing keys in {} that are required to compute the npy array: {}".format(phenotype_path,
                                                                                set(keys_required)-set(phenotype.columns))

    ## TODO: change this condition according to session and run in phenotype.csv
    #assert len(set(phenotype.participant_id)) == len(phenotype), "Unexpected number of participant_id"

    null_or_nan_mask = [False for _ in range(len(phenotype))]
    for key in keys_required:
        null_or_nan_mask |= getattr(phenotype, key).isnull() | getattr(phenotype, key).isna()
    if null_or_nan_mask.sum() > 0:
        print('Warning: {} participant_id will not be considered because of missing required values:\n{}'. \
              format(null_or_nan_mask.sum(), list(phenotype[null_or_nan_mask].participant_id.values)))

    participants_df = phenotype[~null_or_nan_mask]

    ########################################################################################################################
    #  Neuroimaging niftii and TIV
    #  mwp1 files
      #  excpected image dimensions
    NI_filenames = glob.glob(nii_path)
    ########################################################################################################################
    #  Load images, intersect with pop and do preprocessing and dump 5d npy
    print("###########################################################################################################")
    print("#", dataset_name)

    print("# 1) Read images")
    scaling, harmo = 'raw', 'raw'
    print("## Load images")
    NI_arr, NI_participants_df, ref_img = preproc.load_images(NI_filenames,check=check)
    print('--> {} img loaded'.format(len(NI_participants_df)))
    print("## Merge nii's participant_id with participants.csv")
    NI_arr, NI_participants_df = preproc.merge_ni_df(NI_arr, NI_participants_df, participants_df,
                                                         qc=qc, id_type=id_type)
    print('--> Remaining samples: {} / {}'.format(len(NI_participants_df), len(participants_df)))

    print("## Save the new participants.csv")
    NI_participants_df.to_csv(OUTPUT(dataset_name, output_path, type="participants", ext="csv"),
                              index=False)
    print("## Save the raw npy file (with shape {})".format(NI_arr.shape))
    np.save(OUTPUT(dataset_name, output_path, type="data64", ext="npy"), NI_arr)

    ######################################################################################################################
    # Deallocate the memory
    del NI_arr

if __name__=="__main__":

    parser = argparse.ArgumentParser()

    parser.add_argument('--output_path', type=str, required=True)
    parser.add_argument('--nii_regex_path', type=str, required=True)
    parser.add_argument('--phenotype_path', type=str, required=True)
    parser.add_argument('--dataset', type=str, required=True)
    parser.add_argument('--qc_path', type=str, required=False)
    parser.add_argument('--id_type', type=str, choices=['str', 'int'], default='str',
                        help='Type of <participant_id> and <session> used for casting')
    parser.add_argument('--sep', type=str, choices=[',', '\t'], default='\t', help='Separator used in participants.csv')

    args = parser.parse_args()

    # # General case
    nii2npy(args.nii_regex_path,
            args.phenotype_path,
            args.dataset,
            args.output_path,
            qc=args.qc_path,
            sep=args.sep,
            id_type=eval(args.id_type),
            check=dict(shape=(182, 218, 182),
                       zooms=(1, 1, 1)))

    # !/bin/bash

    # MAIN = ~ / PycharmProjects / neurospin / scripts / 2019
    # _psy_datasets / 201906
    # _schizconnect - vip - prague - bsnip - biodb - icaar - start_assemble - all / generic_quasi_raw_preproc.py
    # MAIN_RESAMPLING = ~ / PycharmProjects / neurospin / scripts / 2019
    # _psy_datasets / 201906
    # _schizconnect - vip - prague - bsnip - biodb - icaar - start_assemble - all / quasi_raw_resample.py
    # OUTPUT_PATH = / neurospin / psy_sbox / analyses / 201906
    # _schizconnect - vip - prague - bsnip - biodb - icaar - start_assemble - all / data / quasi_raw /

    ## .nii to .npy for id-type == int and id-type == str (important to make the distinction, depends on the participant_id
    ## values)

    # for DATASET in  biobd hcp nar rbp  mpi-leipzig cnp abide1 abide2 ixi
    # do
    # PHENOTYPE=/neurospin/psy_sbox/$DATASET/${DATASET^^}_t1mri_mwp1_participants.csv
    # QC=/neurospin/psy_sbox/$DATASET/derivatives/cat12-12.6_vbm_qc/qc.tsv
    # python3 $MAIN --nii_regex_path '/neurospin/psy_sbox/'$DATASET'/derivatives/quasi-raw/sub-*/ses-*/anat/*preproc-linear*.nii.gz' --phenotype_path $PHENOTYPE --output_path $OUTPUT_PATH --qc_path $QC --dataset $DATASET --id_type int &> $DATASET.txt &
    # done

    # for DATASET in oasis3  #icbm localizer candi
    # do
    # PHENOTYPE=/neurospin/psy_sbox/$DATASET/${DATASET^^}_t1mri_mwp1_participants.csv
    # QC=/neurospin/psy_sbox/$DATASET/derivatives/cat12-12.6_vbm_qc/qc.tsv
    # python3 $MAIN --nii_regex_path '/neurospin/psy_sbox/'$DATASET'/derivatives/quasi-raw/sub-*/ses-*/anat/*preproc-linear*.nii.gz' --phenotype_path $PHENOTYPE --output_path $OUTPUT_PATH --qc_path $QC --dataset $DATASET --id_type str &> $DATASET.txt &
    # done

    ## Particular cases

    # DATASET=gsp
    # PHENOTYPE=/neurospin/psy_sbox/${DATASET^^}/${DATASET^^}_t1mri_mwp1_participants.csv
    # QC=/neurospin/psy_sbox/${DATASET^^}/derivatives/cat12-12.6_vbm_qc/qc.tsv
    # python3 $MAIN --nii_regex_path '/neurospin/psy_sbox/'${DATASET^^}'/derivatives/quasi-raw/sub-*/ses-*/anat/*preproc-linear*.nii.gz' --phenotype_path $PHENOTYPE --output_path $OUTPUT_PATH --qc_path $QC --dataset $DATASET --id_type int &> $DATASET.txt &
    #
    # DATASET=corr
    # PHENOTYPE=/neurospin/psy_sbox/CoRR/CoRR_t1mri_mwp1_participants.csv
    # QC=/neurospin/psy_sbox/CoRR/derivatives/cat12-12.6_vbm_qc/qc.tsv
    # python3 $MAIN --nii_regex_path '/neurospin/psy_sbox/CoRR/derivatives/quasi-raw/sub-*/ses-*/anat/*preproc-linear*.nii.gz' --phenotype_path $PHENOTYPE --output_path $OUTPUT_PATH --qc_path $QC --dataset $DATASET --id_type int &> $DATASET.txt &

    # DATASET=bsnip
    # PHENOTYPE=/neurospin/psy_sbox/bsnip1/${DATASET^^}_t1mri_mwp1_participants.csv
    # QC=/neurospin/psy_sbox/bsnip1/derivatives/cat12-12.6_vbm_qc/qc.tsv
    # python3 $MAIN --nii_regex_path '/neurospin/psy_sbox/bsnip1/derivatives/quasi-raw/sub-*/ses-*/anat/*preproc-linear*.nii.gz' --phenotype_path $PHENOTYPE --output_path $OUTPUT_PATH --qc_path $QC --dataset $DATASET --id_type str &> $DATASET.txt &
    #
    # DATASET=schizconnect-vip
    # PHENOTYPE=/neurospin/psy_sbox/schizconnect-vip-prague/${DATASET^^}_t1mri_mwp1_participants.csv
    # QC=/neurospin/psy_sbox/schizconnect-vip-prague/derivatives/cat12-12.6_vbm_qc/qc.tsv
    # python3 $MAIN --nii_regex_path '/neurospin/psy_sbox/schizconnect-vip-prague/derivatives/quasi-raw/sub-*/ses-*/anat/*preproc-linear*.nii.gz' --phenotype_path $PHENOTYPE --output_path $OUTPUT_PATH --qc_path $QC --dataset $DATASET --id_type str &> $DATASET.txt &


class Point:
    def __init__(self, x=5, y=9):
        self.x = x
        self.y = y

    def __del__(self):
        class_name = self.__class__.__name__
        print('{} Destroyed'.format(class_name))

pt1 = Point()
pt2 = pt1
pt3 = pt1
print(id(pt1), id(pt2), id(pt3))  # enter id objects
del pt1
del pt2
del pt3


#!/usr/bin/python3
"""
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence
ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes
a list of n numbers as input and checks whether there is a 132 pattern in the
list.

Note: n will be less than 15,000.
"""

class Solution:
    def find132pattern(self, nums):
        """
        Brute force i, j, k O(n^3)

        Optimize: I only need to keep the max number for the middle. O(N^2)

        Need better optimization?
        I need to keep both the min and max number for A[:i] when scanning A[i]
        min must be at left of max

        When scanning A[i], we need a list to keep both the min and max interval
        [min, max]. The list maintains intervals that end at A[i-1] and start at
        the min(A[:i-1])
        0. The max is not increasing, thus can only keep A[i-1]
        1. F(i) = min(A[:i-1]) is strictly non-increasing (softly decreasing)
        2. We don't need to keep any internal that interval.end <= A[i]
        3. The list can be replaced with stack

        O(N) since every number enters and pops the stack once

        :type nums: List[int]
        :rtype: bool
        """
        stack = []  # List[Interval]
        mini = float('Inf')
        for v in nums:
            while stack and stack[-1][1] <= v:  # error when < (e.g. [-2, 1, 1])
                stack.pop()
            if stack and stack[-1][0] < v:
                return True
            mini = min(mini, v)
            stack.append((mini, v))

        return False

    def find132pattern_TLE(self, nums):
        """
        Brute force i, j, k O(n^3)

        Optimize: you only need to keep the max number for the middle. O(N^2)
        :type nums: List[int]
        :rtype: bool
        """
        for i in range(len(nums)):
            maxa = nums[i]
            for j in range(i + 1, len(nums)):
                if nums[j] > nums[i]:
                    if nums[j] < maxa:
                        return True
                    maxa = max(maxa, nums[j])

        return False

if __name__ == "__main__":
    assert Solution().find132pattern([1, 2, 3, 4]) == False
    assert Solution().find132pattern([3, 1, 4, 2]) == True
    assert Solution().find132pattern([-1, 3, 2, 0]) == True
    assert Solution().find132pattern([-2, 1, 1]) == True


from infomap import Infomap
from pysam import AlignmentFile, AlignedSegment
from typing import Dict, List, Tuple, TextIO
import time
import glob
from file_io import *
from parsers import *
from random import shuffle, seed

class NbContig():
    # contig is the basic unit of this network
    # use this class as the basis
    # it might help reduce the overhead
    # and allows modifying or adding info to the contig
    __slots__ = ['id', 'length', 'num_id']
    def __init__(self, id:str, length=NotImplemented, num_id=NotImplemented):
        """
        :param id: NODE_1
        :param length: contig length
        :param num_id: numeric id for informap
        """
        self.id = id
        self.length = length
        self.num_id = num_id

# format_ctg_idf = lambda x: "_".join(x.strip().split(" ")[0].split("_")[:2])
format_ctg_idf = lambda x: x.strip().split(" ")[0]

# def make_infomap_nodes(ctg_lookup: Dict[str, NbContig]):
#     "multilayer network builder using Infomap API"
#     im_network = Infomap("-i ")
#     for i_nbc in ctg_lookup.items():
#         im_network.add_node(i_nbc)
    # with open(contig_fa, 'r') as fr:
    #     for iline in fr
#     add nodes from the contig list
#     input: contig.fasta
#     output: im_network

def make_complete_graph(nodes: List[NbContig], im_network, layer_idx) -> None:
    if len(nodes) > 1:
        link_count = 0
        for i in range(len(nodes)):
            for j in range(i+1, len(nodes)):
                inode = nodes[i]
                jnode = nodes[j]
                im_network.add_multilayer_intra_link(layer_idx, inode.num_id,
                                                     jnode.num_id, weight=1)
                link_count += 1
        print("[:::Message:::] made complete graph with {} edges".format(link_count))

def make_infomap_binned(args, im_network: Infomap, layer_idx,
                        ctg_lookup: Dict[str, NbContig]) -> None:

    def _subbin_handler(subbin: List[NbContig]) -> NbContig:
        """
        decide which contig is the center (median) that connects to all the rest of contigs
        which contig is the representative (biggest) that serves as the node connected to the other subbins.
        this handler makes star
        :return:
        """
        if len(subbin) == 1:
            return subbin[0]

        ctg_lens = [i.length for i in subbin]
        ctg_lens.sort()
        ctg_median = ctg_lens[len(ctg_lens) // 2 - 1]
        ctg_max = ctg_lens[-1]

        center_ctg = None
        rep_ctg = None
        circle_ctg = []

        for i in subbin:
            ilen = i.length
            if ilen == ctg_median:
                center_ctg = i
            elif ilen == ctg_max:
                rep_ctg = i
                circle_ctg.append(rep_ctg)
            else:
                circle_ctg.append(i)

        for i_ctg in circle_ctg:
            im_network.add_multilayer_intra_link(layer_idx, i_ctg.num_id, center_ctg.num_id, weight=1)

        return rep_ctg

    def make_network_bin_fa(bin_fa: str, s: int) -> int:
        """

        :param bin_fa: fasta file represent one bin
        :param s: number of sub-bins
        :return:
        """
        # step 1: decide the bin-dividing plan
        # step 2: fill in the edges

        # step 1
        bin_headers = set(parse_contig_fa(bin_fa).keys())
        bin_ctg = [ctg_lookup[i] for i in bin_headers if i in ctg_lookup]

        if len(bin_ctg) <= s:
            print("[:::Message:::] make complete graph for {}".format(bin_fa))
            make_complete_graph(nodes=bin_ctg, im_network=im_network, layer_idx=layer_idx)

        else:
            print("[:::Message:::] make density-reduced graph for {}".format(bin_fa))
            # seed(12345)
            seed(args.random_seed)
            shuffle(bin_ctg)

            N = len(bin_ctg)
            Lmin = N//s
            Lmax = Lmin + 1
            all_L = [Lmax] * (N%s)
            all_L.extend([Lmin]*(s - N%s))

            # step 2
            l0 = 0
            reps = []
            for l1 in all_L:
                i_subbin = bin_ctg[l0:l0+l1]
                irep = _subbin_handler(i_subbin)
                reps.append(irep)
                l0 += l1

            make_complete_graph(reps, im_network=im_network,
                                layer_idx=layer_idx)
        return len(bin_ctg)

    for i_bin_dir in args.bin_dir:
        bin_fa_files = glob.glob(i_bin_dir + "*." + args.bin_suffix)
        print("[=== Binning layer ===] Parsing binning result directory \"{}\".".format(i_bin_dir))
        if not len(bin_fa_files):
            print("[::Warning::] No contigs detected in \"{}\". Directory skipped".format(i_bin_dir))
            continue

        time_begin = time.time()

        n_binned_ctg = 0
        for i_fa in bin_fa_files:
            n_binned_ctg += make_network_bin_fa(bin_fa=i_fa, s=args.s)

        # update_dict_val(im_edges_content, i_edges)
        time_finish = time.time()
        print("[=== Binning layer ===] Built network for {} contigs in provided binning.".format(n_binned_ctg))
        print("[=== Binning layer ===] Processing binning result took {:.1f} secs".format(time_finish - time_begin))

def make_infomap_pairing(args, im_network: Infomap, layer_idx:int, ctg_lookup:Dict[str, NbContig]):
    def _update_dict_val(k: Tuple[int, int], v: int, ctg_links: Dict[Tuple[int, int], int]) -> None:
        if k in ctg_links:
            ctg_links[k] += v
        else:
            ctg_links[k] = v

    def process_sam() -> Dict[Tuple[int, int], int]:
        sam_handle = AlignmentFile(args.aln_file, 'rb')
        ctg_links = {}
        waiting_read_dict = {}
        for i_read in sam_handle.fetch():
            if not i_read.is_paired or i_read.is_secondary or i_read.is_supplementary:
                # if read.is_secondary or read.is_supplementary:
                continue

            i_rid = i_read.query_name
            if i_rid not in waiting_read_dict:
                if i_read.is_read1:
                    waiting_read_dict[i_rid] = (i_read, None)
                elif i_read.is_read2:
                    waiting_read_dict[i_rid] = (None, i_read)

            else:
                _imate = waiting_read_dict.pop(i_rid)
                i_mate = None
                if i_read.is_read1 and _imate[1]:
                    i_mate = _imate[1]
                elif i_read.is_read2 and _imate[0]:
                    i_mate = _imate[0]

                i_ctg_id = i_read.reference_name
                i_mate_ctg_id = i_mate.reference_name
                if i_ctg_id in ctg_lookup and \
                        i_mate_ctg_id in ctg_lookup and \
                        i_ctg_id != i_mate_ctg_id:
                    i_nbctg = ctg_lookup[i_ctg_id]
                    i_mate_nbctg = ctg_lookup[i_mate_ctg_id]
                    ij_weight = (i_read.query_alignment_length +
                                 i_mate.query_alignment_length)/(i_nbctg.length + i_mate_nbctg.length)
                    # no need to sort the keys because the mate always shows up before the read
                    # thus always i_nbctg.num_id > i_mate_nbctg.num_id
                    _update_dict_val((i_nbctg.num_id, i_mate_nbctg.num_id), ij_weight, ctg_links)
        sam_handle.close()
        return ctg_links

    time_begin = time.time()
    print("[:::Message:::] Parsing alignment...")

    links = process_sam()
    n_edges = 0
    for (i, j), w in links.items():
        if w > 0.001:
            im_network.add_multilayer_intra_link(layer_idx, i, j, w)
            n_edges += 1
    time_finish = time.time()

    print("[=== Read pairing layer ===] Made {} edges based on read pairing.".format(n_edges))
    print("[=== Read pairing layer ===] Processing read pairing took {:.1f} secs".format(
        time_finish - time_begin))

def make_infomap_linked(args, im_network: Infomap, layer_idx:int, ctg_lookup) -> None:
    """
    does 3 steps:
    1. get read-to-barcode mapping
    2. get {barcode: (ctg_num_id, aligned_bases, ctg_length)}
    3. establish ctg-to-ctg edges.
    The edge weight is based on aligned_bases to total_bases
    The edge weight has to be > 0.001 (for now)
    """

    def _get_reads2bars(seq_file: str):
        seqfile = SeqFile(seq_file)
        seq_idf = seqfile.idf
        reads2bars = {}

        _header_spliter = lambda x: x[1:].split(" ")
        for iline in seqfile.file_content:
            if iline.startswith(seq_idf):
                iline_split = _header_spliter(iline)
                reads2bars[iline_split[0]] = iline_split[-1]
        return reads2bars

    def _update_ctg_dict(dict_:Dict[str, int], elem_key: str, elem_val: int)-> None:
        """when iter over each contig, a dict {bar: weight} is made.
        this call updates the dict"""
        if elem_key in dict_:
            dict_[elem_key] += elem_val
        else:
            dict_[elem_key] = elem_val

    def _update_bars2ctgs(b2c: Dict[str, List[Tuple[int, int, int]]], ctg_dict: Dict[str, int], ctg_id: str) -> None:
        """b2c: {bar: (ctg_num_id, mapped_bases, ctg_len)}
        ctg_dict: {bar: n_mapped_bases}"""
        ctg = ctg_lookup[ctg_id]
        ctg_num_id = ctg.num_id
        ctg_len = ctg.length
        for k, v in ctg_dict.items():
            if k in b2c:
                b2c[k].append((ctg_num_id, v, ctg_len))
            else:
                b2c[k] = [(ctg_num_id, v, ctg_len)]

    def get_bars2ctgs():
        b2c = {}  # bars2contigs, {barcode: [contigs]}
        reads2bars = {}
        if args.se_reads:
            reads2bars = _get_reads2bars(args.se_reads)
        if args.pe_reads:
            reads2bars.update(_get_reads2bars(args.pe_reads[0]))
        print("[:::Message:::] Processed {} reads with unique ids.".format(len(reads2bars)))

        sam_handle = AlignmentFile(args.aln_file, 'rb')
        last_ctg = None
        i_ctg_dict = {}

        for i_read in sam_handle.fetch():
            if i_read.is_unmapped or i_read.is_secondary or i_read.is_supplementary:
                continue

            i_ctg = i_read.reference_name
            if i_ctg not in ctg_lookup:
                continue

            if not last_ctg:
                last_ctg = i_ctg
                i_aln_len = i_read.query_alignment_length
                i_bar = reads2bars[i_read.query_name]
                i_ctg_dict[i_bar] = i_aln_len
            elif i_ctg != last_ctg:
                # conclude the last contig: update the b2c
                _update_bars2ctgs(b2c, i_ctg_dict, last_ctg)
                # set up the new contig
                i_ctg_dict = {}
                last_ctg = i_ctg
                i_aln_len = i_read.query_alignment_length
                i_bar = reads2bars[i_read.query_name]
                i_ctg_dict[i_bar] = i_aln_len
            else:
                i_aln_len = i_read.query_alignment_length
                i_bar = reads2bars[i_read.query_name]
                _update_ctg_dict(i_ctg_dict, i_bar, i_aln_len)

        sam_handle.close()

        return b2c

    def _make_ctg_link_single_bar(ctg_links: Dict[Tuple[int, int], float],
                                  ictgs: List[Tuple[int, int, int]]) -> None:
        nctgs = len(ictgs)
        for i in range(nctgs):
            for j in range(i, nctgs):
                ik = ictgs[i][0]
                jk = ictgs[j][0]
                k = None
                if ik < jk:
                    k = (ik, jk)
                else:
                    k = (jk, ik)

                v = (ictgs[i][1]+ ictgs[j][1])/(ictgs[i][2]+ ictgs[j][2])
                if k in ctg_links:
                    ctg_links[k] += v
                else:
                    ctg_links[k] = v

    def generate_ctg2ctg_edges(bars2ctgs_dict:Dict[str, List[Tuple[int, int, int]]],
                               min_bar_size: int) -> Dict[Tuple[int, int], float]:
        ctg_links = {}
        test_nbars = 0
        for ibar, ictgs in bars2ctgs_dict.items():
            if len(ictgs) >= min_bar_size:
                _make_ctg_link_single_bar(ctg_links, ictgs)
                test_nbars += 1

        print("[:::Dev Message:::] no. legit bars: {}".format(test_nbars))
        return ctg_links

    print("[=== Linked read layer ===] Parsing linked reads...")
    time_begin = time.time()

    bars2ctgs = get_bars2ctgs()
    print("[:::Message:::] processed reads with {} barcodes".format(len(bars2ctgs)))
    time_bars2ctgs = time.time()
    print("[:::Dev Message:::]bars2ctgs: {:.1f} secs".format(time_bars2ctgs - time_begin))
    # bars2ctgs elements:
    # BX:Z:A61B28C05D34: [(1, 0.00021912252873939136), (6, 0.0005723737009561289), (2428, 0.1162340178225494)]
    links = generate_ctg2ctg_edges(bars2ctgs, args.min_bar_size)
    nedges = 0
    for k, v in links.items():
        if v > 0.001:
            im_network.add_multilayer_intra_link(layer_idx, k[0], k[1], v)
            nedges += 1

    time_finish = time.time()
    print("[=== Linked read layer ===] made {} edges based on read barcodes".format(nedges))
    print("[=== Linked read layer ===] Processing read linkage took {:.1f} secs".format(time_finish - time_begin))

def make_infomap_assembly(args, im_network: Infomap,
                          layer_idx: int, all_contigs: Set[str],
                          ctg_lookup: Dict[str, NbContig]):
    # get paths: {contig_num_id: [(path_left_end, path_right_end)]}
    #  Dict[int, List[Tuple]]
    time_begin = time.time()
    # obuffer = []

    if args.assembler == "spades":
        path_segs = parse_spades_contig_paths(all_contigs, ctg_lookup, args.ctg_paths)
        contigs = list(path_segs.keys())
        contigs.sort()

        counter = 0
        for i in range(len(contigs)):
            for j in range(i+1, len(contigs)):
                ic = contigs[i]
                jc = contigs[j]
                isegs = path_segs[ic]
                jsegs = path_segs[jc]
                if isegs.intersection(jsegs):
                    im_network.add_multilayer_intra_link(layer_idx, ic, jc)
                    # obuffer.append("{0} {1} {2}".format(layer_idx, ic, jc))
                    counter +=1
        print("[=== Assembly layer ===] made {} links with {} contigs".format(counter, len(contigs)))
    time_end = time.time()

    print("[=== Assembly layer ===] Processing assembly took {:.1f} secs".format(time_end - time_begin))
    # return "\n".join(obuffer)

def generate_ctg_lookup(contig_fa_lookup: Dict[str, str], min_ctg_len: int):
    # alternative func for generate ctg lookup
    # function: generate ctg lookup dict
    #           and initialize im by initializing the network and adding nodes
    # change: node_id is no longer a str, but an int
    """
    :param contig_fa_lookup: {fasta_header: seq_len}
    :param min_ctg_len:
    :return: contig_fa_lookup.keys(): all contig names as a set
    :return: ctg_lookup: Dict[str, NbContig]
            contigs that passed the min contig length check, formatted for use in infomap
    :return: unbinned: [fasta record as string]
    :return: im_network is no longer generated here.
    """
    im_network = Infomap("-s 1")
    ctg_lookup = {}
    unbinned_short = []
    i = 1

    for iheader, iseq in contig_fa_lookup.items():
        ilen = len(iseq)

        if ilen < min_ctg_len:
            unbinned_short.append(iheader)
        else:
            formatted_id = format_ctg_idf(iheader)
            cur_nbcontig = NbContig("", ilen, i)  # changed node_id to int
            ctg_lookup[iheader] = cur_nbcontig  # changed the key to the fasta header
            im_network.set_name(i, formatted_id)
            # im_network.add_node(node_id=i,
            #                     name=formatted_id)

        i += 1
    print("[=== Parsing contigs ===] {} contigs with minimum length {} for binning".format(len(ctg_lookup), min_ctg_len))

    return ctg_lookup, im_network, unbinned_short


from cmath import isnan
import pandas as pd
import numpy as np
from tracked_object import TrackedObj
import pickle
import time
import os

def groupby_prov(df, col_name, agg_name, id1 = 1, limit = 1000):
    """
    Sum group by with col_name, on aggregation_name
    """
    col1 = []
    col1_dict = {}
    col2 = []
    columns = list(df.columns)
    columns_dict = {}
    uniq = list(df[col_name].unique())
    for i, c in enumerate(columns):
        columns_dict[c] = i
    for index, row in df.iterrows():
        if index%10000 == 0:
            print(index)
        # if index >= limit:
        #     break
        if np.isnan(row[col_name]):
            continue
        elif row[col_name] in col1_dict:
            i = col1_dict[row[col_name]]
            col2[i] += TrackedObj(row[agg_name], (id1, columns_dict[agg_name], index)) # how to allow out of memory here?
        else:
            i = len(col1)
            col1_dict[row[col_name]] = i
            obj1 = TrackedObj(row[col_name], (id1, columns_dict[col_name], index))
            obj2 = TrackedObj(row[agg_name], (id1, columns_dict[agg_name], index))
            col1.append(obj1)
            col2.append(obj2)
    return np.asarray([col1, col2], dtype=object)

def _short(key):
    key = int(key[2:])
    return key

def join_prov(df1, df2, column_name1, column_name2,  id1 = 1, id2 = 2, limit = 100000):
    """
    Inner join on column_name over dataset1 and dataset2
    """
    df1 = df1.head(limit)
    df2 = df2.head(limit)
    array = []
    col1 = list(df1.columns)
    col1_dict = {}
    for i, c in enumerate(col1):
        col1_dict[c] = i
    col2 = list(df2.columns)
    col2_dict = {}
    for i, c in enumerate(col2):
        col2_dict[c] = i

    u1 = set(df1[column_name1].unique())
    u2 = set(df2[column_name2].unique())
    u = u1.intersection(u2)
    u = list(u)
    u.sort(key=_short)
    a = 0
    for val in u:
        if a%1000 == 0:
            print(val)
            print(a)
        df1_sub = df1.loc[df1[column_name1] == val]
        df2_sub = df2.loc[df2[column_name2] == val]
        for i, row1 in df1_sub.iterrows():
            for j, row2 in df2_sub.iterrows():
                row = []
                for name, value in row1.items():
                    obj = TrackedObj(value, (id1, col1_dict[name], i))
                    row.append(obj)
                for name, value in row2.items():
                    if name == column_name2:
                        continue
                    obj = TrackedObj(value, (id2, col2_dict[name], j))
                    row.append(obj)

                array.append(row)
        a += 1
    return np.asarray(array, dtype=object)

def join_prov_2(df1, df2, column_name1, column_name2, id1 = 1, id2 = 2, limit = 100000, save_location = './compression_tests_3/join_all', previous = 0):
    '''
    faster inner joiner for specified dataset
    '''
    array = []
    col1 = list(df1.columns)
    col1_dict = {}
    for i, c in enumerate(col1):
        col1_dict[c] = i
    col2 = list(df2.columns)
    col2_dict = {}
    for i, c in enumerate(col2):
        col2_dict[c] = i
    i = 0
    j = 0
    last_save = 0
    while True:
        if j%10000 == 0 and j != last_save:
            print(j)
            array = np.asarray(array, dtype=object)
            print(array.shape)
            with open(save_location + '/join_{}_{}.npy'.format(j, i), 'wb') as f:
                np.save(f, array, allow_pickle=True)
            array = []
            last_save = j
        if i == df1.shape[0]:
            break
        elif j == df2.shape[0]:
            break
        row1 = df1.iloc[i]
        row2 = df2.iloc[j]
        if row1[column_name1] == row2[column_name1]:
            row = []
            for name, value in row1.items():
                obj = TrackedObj(value, (id1, col1_dict[name], i))
                row.append(obj)
            for name, value in row2.items():
                if name == column_name2:
                    continue
                obj = TrackedObj(value, (id2, col2_dict[name], j))
                row.append(obj)
            array.append(row)
            j += 1
            i += 1
        else:
            i += 1
    array = np.asarray(array, dtype=object)
    with open(save_location + '/join_{}_{}.npy'.format(j, i), 'wb') as f:
        #pickle.dump(array, f)
        np.save(f, array, allow_pickle=True)

if __name__ == "__main__":
    folder = './compression_tests_3/join_all'
    paths = []
    for file in os.listdir(folder):
        if file.endswith('npy'):
            index = int(file.split('_')[1])
            full = os.path.join(folder, file)
            paths.append((index, full))
    paths.sort(key = lambda x : x[0])
    arrs = []
    for i, path in enumerate(paths):
        arr = np.load(path[1], allow_pickle=True)
        arrs.append(arr)
    array = np.concatenate(arrs, axis = 0)
    data = 'group_by_pandas_sorted.pickle'
    col_name = 'startYear'
    agg_name = 'isAdult'
    data = open(data, 'rb')
    data = pickle.load(data, encoding='latin1')
    data = groupby_prov(data, col_name, agg_name)


import math

def area(r):
    return math.pi * (r**2)

areas = []
redii = [ 2, 4, 6, 8]

for r in redii:
    a = area(r)
    areas.append(a)

#print(areas)

# x = list(map(area, redii))
# print(x)

# usando lambda

y = list(map(lambda r: math.pi * (r**2), redii))
print(y)


# 10.	Nested Tuples: Create a tuple of tuples representing a matrix (2D list) and access an element within it.

matrix = (
    (1, 2, 3),
    (4, 5, 6),
    (7, 8, 9)
)
x = matrix[2][0]
print(x)


class Solution:
    def alertNames(self, keyName: List[str], keyTime: List[str]) -> List[str]:
        nameTracker = defaultdict(lambda: [])
        def convertToInt(time):
            t = int(time[:2])*60 + int(time[3:])
            #print(time, t)
            return t
        output = set()
        for i,j in zip(keyName, keyTime):
            time = convertToInt(j)
            nameTracker[i].append(time)
        for k in nameTracker.keys():
            aa = sorted(nameTracker[k])
            for i in range(len(aa)-2):
                t1 = aa[i]
                t2 = aa[i+1]
                t3 = aa[i+2]
                if max(t1,t2,t3) - min(t1,t2,t3)<=60:
                    output.add(k)
                    break
        return sorted(list(output))


"""infer.py

Main inference script.

Usage:
  infer.py [--gpu=<id>] [--mode=<mode>]
  infer.py (-h | --help)
  infer.py --version

Options:
  -h --help      Show this string.
  --version      Show version.
  --gpu=<id>     Comma separated GPU list. [default: 0]
  --mode=<mode>  Inference mode- use either 'seg' or 'class'.
"""

from docopt import docopt
import argparse
import glob
import math
import time
import os
import sys
from collections import deque
from keras.utils import HDF5Matrix

import cv2
import numpy as np

from tensorpack.predict import OfflinePredictor, PredictConfig
from tensorpack.tfutils.sessinit import get_model_loader

from config import Config
from misc.utils import rm_n_mkdir, cropping_center
from model.utils.model_utils import crop_op

import json
import operator

from sklearn.metrics import roc_auc_score

def get_best_chkpts(path, metric_name, comparator=">"):
    """
    Return the best checkpoint according to some criteria.
    Note that it will only return valid path, so any checkpoint that has been
    removed wont be returned (i.e moving to next one that satisfies the criteria
    such as second best etc.)

    Args:
        path: directory contains all checkpoints, including the "stats.json" file
    """
    stat_file = path + "/stats.json"
    ops = {
        ">": operator.gt,
        "<": operator.lt,
    }

    op_func = ops[comparator]
    with open(stat_file) as f:
        info = json.load(f)

    if comparator == ">":
        best_value = -float("inf")
    else:
        best_value = +float("inf")

    best_chkpt = None
    for epoch_stat in info:
        epoch_value = epoch_stat[metric_name]
        if op_func(epoch_value, best_value):
            chkpt_path = "%s/model-%d.index" % (path, epoch_stat["global_step"])
            if os.path.isfile(chkpt_path):
                selected_stat = epoch_stat
                best_value = epoch_value
                best_chkpt = chkpt_path
    return best_chkpt, selected_stat

class InferClass(Config):
    def __gen_prediction(self, x, predictor):
        """
        Using 'predictor' to generate the prediction of image 'x'

        Args:
            x : input image to be segmented. It will be split into patches
                to run the prediction upon before being assembled back
            tissue: tissue mask created via otsu -> only process tissue regions
                    if it is provided
        """

        prob = predictor(x)[0]
        pred = np.argmax(prob, -1)
        pred = np.squeeze(pred)
        prob = np.squeeze(prob[..., 1])

        return prob, pred

    ####
    def run(self):
        if self.inf_auto_find_chkpt:
            print(
                '-----Auto Selecting Checkpoint Basing On "%s" Through "%s" Comparison'
                % (self.inf_auto_metric, self.inf_auto_comparator)
            )
            model_path, stat = get_best_chkpts(
                self.save_dir, self.inf_auto_metric, self.inf_auto_comparator
            )
            print("Selecting: %s" % model_path)
            print("Having Following Statistics:")
            for key, value in stat.items():
                print("\t%s: %s" % (key, value))
        else:
            model_path = self.inf_model_path
        model_constructor = self.get_model()
        pred_config = PredictConfig(
            model=model_constructor(),
            session_init=get_model_loader(model_path),
            input_names=self.eval_inf_input_tensor_names,
            output_names=self.eval_inf_output_tensor_names,
            create_graph=False)
        predictor = OfflinePredictor(pred_config)

        ####
        save_dir = self.inf_output_dir
        predict_list = [["case", "prediction"]]

        file_load_img = HDF5Matrix(
            self.inf_data_list[0] + "camelyonpatch_level_2_split_test_x.h5", "x"
        )
        file_load_lab = HDF5Matrix(
            self.inf_data_list[0] + "camelyonpatch_level_2_split_test_y.h5", "y"
        )

        true_list = []
        prob_list = []
        pred_list = []

        num_ims = file_load_img.shape[0]
        last_step = math.floor(num_ims / self.inf_batch_size)
        last_step = self.inf_batch_size * last_step
        last_batch = num_ims - last_step
        count = 0
        for start_batch in range(0, last_step + 1, self.inf_batch_size):
            sys.stdout.write("\rProcessed (%d/%d)" % (start_batch, num_ims))
            sys.stdout.flush()
            if start_batch != last_step:
                img = file_load_img[start_batch : start_batch + self.inf_batch_size]
                img = img.astype("uint8")
                lab = np.squeeze(
                    file_load_lab[start_batch : start_batch + self.inf_batch_size]
                )
            else:
                img = file_load_img[start_batch : start_batch + last_batch]
                img = img.astype("uint8")
                lab = np.squeeze(file_load_lab[start_batch : start_batch + last_batch])

            prob, pred = self.__gen_prediction(img, predictor)

            for j in range(prob.shape[0]):
                predict_list.append([str(count), str(prob[j])])
                count += 1

            prob_list.extend(prob)
            pred_list.extend(pred)
            true_list.extend(lab)

        prob_list = np.array(prob_list)
        pred_list = np.array(pred_list)
        true_list = np.array(true_list)
        accuracy = (pred_list == true_list).sum() / np.size(true_list)
        error = (pred_list != true_list).sum() / np.size(true_list)

        print("Accurcy (%): ", 100 * accuracy)
        print("Error (%): ", 100 * error)
        if self.model_mode == "class_pcam":
            auc = roc_auc_score(true_list, prob_list)
            print("AUC: ", auc)

        # Save predictions to csv
        rm_n_mkdir(save_dir)
        for result in predict_list:
            predict_file = open("%s/predict.csv" % save_dir, "a")
            predict_file.write(result[0])
            predict_file.write(",")
            predict_file.write(result[1])
            predict_file.write("\n")
            predict_file.close()

class InferSeg(Config):
    def __gen_prediction(self, x, predictor):
        """
        Using 'predictor' to generate the prediction of image 'x'

        Args:
            x : input image to be segmented. It will be split into patches
                to run the prediction upon before being assembled back
            tissue: tissue mask created via otsu -> only process tissue regions
                    if it is provided
        """
        step_size = self.infer_output_shape
        msk_size = self.infer_output_shape
        win_size = self.infer_input_shape

        def get_last_steps(length, msk_size, step_size):
            nr_step = math.ceil((length - msk_size) / step_size)
            last_step = (nr_step + 1) * step_size
            return int(last_step), int(nr_step + 1)

        im_h = x.shape[0]
        im_w = x.shape[1]

        last_h, nr_step_h = get_last_steps(im_h, msk_size[0], step_size[0])
        last_w, nr_step_w = get_last_steps(im_w, msk_size[1], step_size[1])

        diff_h = win_size[0] - step_size[0]
        padt = diff_h // 2
        padb = last_h + win_size[0] - im_h

        diff_w = win_size[1] - step_size[1]
        padl = diff_w // 2
        padr = last_w + win_size[1] - im_w

        x = np.lib.pad(x, ((padt, padb), (padl, padr), (0, 0)), "reflect")

        #### TODO: optimize this
        sub_patches = []
        skipped_idx = []
        # generating subpatches from orginal
        idx = 0
        for row in range(0, last_h, step_size[0]):
            for col in range(0, last_w, step_size[1]):
                win = x[row : row + win_size[0], col : col + win_size[1]]
                sub_patches.append(win)
                idx += 1

        pred_map = deque()
        while len(sub_patches) > self.inf_batch_size:
            mini_batch = sub_patches[: self.inf_batch_size]
            sub_patches = sub_patches[self.inf_batch_size :]
            mini_output = predictor(mini_batch)[0]
            if win_size[0] > msk_size[0]:
                mini_output = cropping_center(mini_output, (diff_h, diff_w))
            mini_output = np.split(mini_output, self.inf_batch_size, axis=0)
            pred_map.extend(mini_output)
        if len(sub_patches) != 0:
            mini_output = predictor(sub_patches)[0]
            if win_size[0] > msk_size[0]:
                mini_output = cropping_center(mini_output, (diff_h, diff_w))
            mini_output = np.split(mini_output, len(sub_patches), axis=0)
            pred_map.extend(mini_output)

        #### Assemble back into full image
        output_patch_shape = np.squeeze(pred_map[0]).shape

        ch = 1 if len(output_patch_shape) == 2 else output_patch_shape[-1]

        #### Assemble back into full image
        pred_map = np.squeeze(np.array(pred_map))
        pred_map = np.reshape(pred_map, (nr_step_h, nr_step_w) + pred_map.shape[1:])
        pred_map = (
            np.transpose(pred_map, [0, 2, 1, 3, 4])
            if ch != 1
            else np.transpose(pred_map, [0, 2, 1, 3])
        )
        pred_map = np.reshape(
            pred_map,
            (
                pred_map.shape[0] * pred_map.shape[1],
                pred_map.shape[2] * pred_map.shape[3],
                ch,
            ),
        )
        # just crop back to original size
        pred_map = np.squeeze(pred_map[:im_h, :im_w])

        return pred_map

    ####
    def run(self):

        if self.inf_auto_find_chkpt:
            print(
                '-----Auto Selecting Checkpoint Basing On "%s" Through "%s" Comparison'
                % (self.inf_auto_metric, self.inf_auto_comparator)
            )
            model_path, stat = get_best_chkpts(
                self.save_dir, self.inf_auto_metric, self.inf_auto_comparator
            )
            print("Selecting: %s" % model_path)
            print("Having Following Statistics:")
            for key, value in stat.items():
                print("\t%s: %s" % (key, value))
        else:
            model_path = self.inf_model_path

        model_constructor = self.get_model()
        pred_config = PredictConfig(
            model=model_constructor(),
            session_init=get_model_loader(model_path),
            input_names=self.eval_inf_input_tensor_names,
            output_names=self.eval_inf_output_tensor_names,
            create_graph=False,
        )
        predictor = OfflinePredictor(pred_config)

        for data_dir in self.inf_data_list:
            save_dir = self.inf_output_dir + "/raw/"
            file_list = glob.glob("%s/*%s" % (data_dir, self.inf_imgs_ext))
            file_list.sort()  # ensure same order

            rm_n_mkdir(save_dir)
            for filename in file_list:
                start = time.time()
                filename = os.path.basename(filename)
                basename = filename.split(".")[0]
                print(data_dir, basename, end=" ", flush=True)

                ##
                img = cv2.imread(data_dir + filename)
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

                pred_map = self.__gen_prediction(img, predictor)

                np.save("%s/%s.npy" % (save_dir, basename), [pred_map])
                end = time.time()
                diff = str(round(end - start, 2))
                print("FINISH. TIME: %s" % diff)

####
if __name__ == "__main__":
    args = docopt(__doc__)
    print(args)

    if args["--gpu"]:
        os.environ["CUDA_VISIBLE_DEVICES"] = args["--gpu"]
        nr_gpus = len(args["--gpu"].split(","))

    if args["--mode"] is None:
        raise Exception('Mode cannot be empty. Use either "class" or "seg".')

    if args["--mode"] == "class":
        infer = InferClass()
    elif args["--mode"] == "seg":
        infer = InferSeg()
    else:
        raise Exception('Mode not recognised. Use either "class" or "seg".')

    infer.run()


# -*- coding: utf-8 -*-
"""Основная логика работы бота. Токен получаем из переменной окружения """

import extensions
import os

if __name__ == '__main__':
    token = os.environ.get('TOKEN', None)
    if token is None:
        raise extensions.ApiException('Отсутствует токен для запуска бота!')
    else:
        print('Bot are running...')
        bot = extensions.TgBot(token)


"""
이름 : 박민지
날짜 : 2021/04/13
내용 : 파이썬 객체지향 캡슐화 실습하기 교재 p161
"""

from Ch06.sub1.Account import Account

kb = Account('국민은행', '121-11-1234', '김유신', 10000)
kb.deposit(20000)
kb.withdraw(5000)

# 캡슐화 적용으로 취약코드를 예방한다.
# 캡슐화 방법 : 멤버변수에 __를 선언
# kb.money -= 1 -> 이렇게 해도 sub1의 account에서 __사용해서 캡슐

kb.show()


import configly

# You can try this example out by setting the `CONFIGLY_API_KEY`
# environmental variable to our demo account: 'Dem0apiKEY'
configly.api_key = 'YOUR_API_KEY'

try:
    catalog = configly.get('store_catalog')

    # `get` returns None if the key is not found.
    if catalog is None:
        print('The key was not found')
    else:
        items = catalog['items']
        prices = catalog['prices']

        for index in range(0, len(items)):
            item = items[index]
            price = prices[index]
            if catalog['has_sale']:
                price = catalog['discount']*price
            print(f'{item}: {price:.2f} USD')
except Exception as error:
    print(error)
    # configly.errors.InvalidApiKeyError is thrown when there's an unknown API Key
    # configly.errors.ConfiglyRequestError is thrown on a timeout or failure to connection to the server
    # configly.errors.ConfiglyrequestError is thrown when the server responds with an error


from PyQt4 import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from Drawing import *
from mainWidget import *
from NetManager import *
import sys

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Painting(QtGui.QLabel):
    def __init__(self, model):
        QtGui.QLabel.__init__(self)
        self.drawingList = []
        self.usedTool = DrawingToolRectangle()
        self.background = None
        self.zoomX = 1
        self.model = model

    def paintEvent(self, ev):
        self.p = QPainter()
        self.p.begin(self)
        self.p.scale(self.zoomX, self.zoomX)
        if (self.background is not None):
            """
            w = self.background.width()
            h = self.background.height()
            backCopy = self.background.scaled(w * self.zoomX, h * self.zoomX, Qt.KeepAspectRatio, Qt.FastTransformation)
            """
            self.p.drawPixmap(0, 0, self.background)
        self.p.setBackground(QtGui.QColor("black"))
        for drawing in self.model.drawings():
            #print("Drawing: ", drawing)
            drawing.draw(self.p)
        if (self.usedTool.drawing is not None):
            self.usedTool.drawing.draw(self.p)
        self.p.end()

    def addDrawing(self, drawing):
        self.model.addDrawing(drawing)
        self.update()

    """
    def mousePressEvent(self, ev):
        self.pressed = 1
        self.usedTool.mousePressed(ev)
        self.update()

    def mouseMoveEvent(self, ev):
        if (self.pressed == 1):
            self.usedTool.mouseDragged(ev)
            self.update()

    def mouseReleaseEvent(self, ev):
        self.pressed = 0
        self.usedTool.mouseReleased(ev)
        self.addDrawing(self.usedTool.drawing)
        self.update()
    """

    def wheelEvent(self, ev):
        step =  float(ev.delta())/1000
        self.zoomX += step
        if (self.zoomX < 0.1):
            self.zoomX = 0.25
        if (self.zoomX > 2.5):
            self.xoomX = 2
        self.update()

    def setBackgroundImage(self, imageName):
        self.background = QPixmap(imageName)
        self.setPixmap(self.background)

    def customEvent(self,event):
        etype = event.type()
        arg   = event.arg
        if (etype == QEvent.User + 1):
            self.setBackgroundImage(arg)
        elif (etype == QEvent.User):
            self.addDrawing(arg)

class Main(QtGui.QMainWindow):

    def __init__(self, args):
        QtGui.QMainWindow.__init__(self)
        model = DrawingModel()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.centralwidget = QtGui.QWidget(self)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
        self.centralwidget.setSizePolicy(sizePolicy)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 900, 481))
        self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setMargin(0)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.scrollArea = QtGui.QScrollArea()
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
        self.painting = Painting(model)
        self.painting.setGeometry(QtCore.QRect(0, 0,90, 90))
        self.painting.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
        self.scrollArea.setWidget(self.painting)
        self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
        self.setCentralWidget(self.centralwidget)
        self.thread = NetManager(self.painting, args.host[0], str(args.port[0]), args.user[0], args.password[0], model)
        self.thread.begin()

    def resizeEvent(self, event):
        QMainWindow.resizeEvent(self,event)
        size = event.size()
        self.gridLayoutWidget.setGeometry(QtCore.QRect(0,0,size.width()-10, size.height()-60))

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description='Connects to a tellapic server')
    parser.add_argument('-c', '--host', required=True, nargs=1, type=str, help='the host name to connect to.')
    parser.add_argument('-p', '--port', required=True, nargs=1, type=int, help='the HOST port to use.')
    parser.add_argument('-u', '--user', required=True, nargs=1, type=str, help='the USERNAME to use.')
    parser.add_argument('-P', '--password', required=True, nargs=1, type=str, help='the server PASSWORD.')
    args = parser.parse_args()
    app = QtGui.QApplication(sys.argv)
    main = Main(args)
    main.show()
    sys.exit(app.exec_())


# Activity Python: Exam 4 Part 19
# File: E4_P19_thackeer.py
# Date:    13 February 2023
# By:      Elijah Thacker
# Section: 005
# Team:    063
#
# ELECTRONIC SIGNATURE
# Elijah Thacker
#
# The electronic signature above indicates the script
# submitted for evaluation is my individual work, and I
# have a general understanding of all aspects of its
# development and execution.
#
# A BRIEF DESCRIPTION OF WHAT THE SCRIPT OR FUNCTION DOES
# Use input values to calculate projectile motion variables
import math
def Rocket(Vo,N):
    angle = 180
    while angle >= 90 or angle <= 0:
        angle = input("Angle (degrees between ): ")
    height = (Vo^2 * math.sin(angle*math.pi/180))/2*9.81
    range = (2*(Vo^2)*math.cos(angle*math.pi/180)*math.sin(angle*math.pi/180))/9.81
    print("Vo=","{:0.1f}".format(Vo),"m/s, Th=", "{:0.1f}".format(angle),"deg., MaxHeight=","{:0.1f}".format(height),"m, MaxRange=", "{:0.1f}".format(range),"m")


# -*- coding: utf-8 -*-
"""
Created on Mon Oct  8 14:32:52 2018

@author: luolei

"""
from rq import Queue
import time
from redis import Redis
from tmp.jobs import func_low, func_high, func_default

redis_conn = Redis()

q_low = Queue('low', connection = redis_conn)
q_high = Queue('high', connection = redis_conn)
q_default = Queue('default', connection = redis_conn)

# 添加任务，注意添加顺序
max_queued_time = 30  # job停留在队列里的最长时间
result_ttl = 100  # 结果保留的最长时间

job_high = q_high.enqueue(
    f = func_high,
    ttl = max_queued_time,
    args = ('http://www.baidu.com',),  # func里的args参数
    result_ttl = result_ttl
)

job_default = q_default.enqueue(
    f = func_default,
    ttl = max_queued_time,
    args = ('http://www.baidu.com',),  # func里的args参数
    result_ttl = result_ttl
)

job_low = q_low.enqueue(
    f = func_low,
    ttl = max_queued_time,
    args = ('http://www.baidu.com',),  # func里的args参数
    result_ttl = result_ttl
)

print('\nstep = 0')
print(job_low.result, job_default.result, job_high.result)

for step in range(1, 200):
    time.sleep(1)
    print('\nstep = {}'.format(step))
    print(job_high.result, job_low.result, job_default.result)  # 即使没有打印也会有后台运行


"""
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs.

If z liters of water is measurable, you must have z liters of water contained within one or both buckets by the end.

Operations allowed:

Fill any of the jugs completely with water.
Empty any of the jugs.
Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.
Example 1: (From the famous "Die Hard" example)

Input: x = 3, y = 5, z = 4
Output: True
Example 2:

Input: x = 2, y = 6, z = 5
Output: False

Constraints:

0 <= x <= 10^6
0 <= y <= 10^6
0 <= z <= 10^6
"""

def can_measure_water(x, y, z):
    """
    :type x: int
    :type y: int
    :type z: int
    :rtype: bool
    """

    # define greatest common denominator func
    def gcd(x, y):
        if (x == 0) and (y != 0):
            return y
        elif (y == 0) and (x != 0):
            return x
        else:
            while x != y:
                if x > y:
                    x = x - y
                else:
                    y = y - x

        return x

    if x + y < z:
        return False

    if x + y == z:
        return True

    if z % gcd(x, y) == 0:
        return True
    else:
        return False

## TEST CODE ##

print(can_measure_water(3, 5, 4)) # True
print(can_measure_water(2, 6, 5)) # False
print(can_measure_water(7, 10, 6)) # True
print(can_measure_water(0, 0, 5)) # False


def  Palindrome(s):
        y = s[::-1]
        print (y)
        if s == s[::-1]:
            print ("Palindrome")
        else:
             print ("NOT Palindrome")

        '''length = len(s)
        i = 0
        #while i < length / 2 + 1:
        for i in range(length / 2 + 1):
            if s[i] != s[length-1-i]:
                print "Not Palindrome"
                break
            i += 1
        else:
            print "Palidrome"'''

Palindrome('Vinay')


from math import cos, sin, radians
from django import template
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _

register = template.Library()

class RecurseTreeNode(template.Node):
    def __init__(self, template_nodes, tree_var):
        self.template_nodes = template_nodes
        self.tree_var = tree_var

    def _render_node(self, context, node):
        bits = []
        context.push()
        if not node.is_leaf:
            for child in node.childrens:
                context['node'] = child
                bits.append(self._render_node(context, child))
            context['children'] = mark_safe(u''.join(bits))
        context['node'] = node
        rendered = self.template_nodes.render(context)
        context.pop()
        return rendered

    def render(self, context):
        tree = self.tree_var.resolve(context)
        return self._render_node(context, tree)

@register.tag
def recursetree(parser, token):
    """
    Iterates over the nodes in the tree, and renders the contained block for each node.
    This tag will recursively render children into the template variable {{ children }}.
    Only one database query is required (children are cached for the whole tree)

    Usage:

                {% recursetree nodes %}

                        {{ node.name }}
                        {% if not node.is_leaf_node %}

                                {{ children }}

                        {% endif %}

                {% endrecursetree %}

    """
    bits = token.contents.split()
    if len(bits) != 2:
        raise template.TemplateSyntaxError(_('%s tag requires a queryset') % bits[0])

    tree_var = template.Variable(bits[1])

    template_nodes = parser.parse(('endrecursetree',))
    parser.delete_first_token()

    return RecurseTreeNode(template_nodes, tree_var)

class RecurseMenuNode(template.Node):
    def __init__(self, template_nodes, tree_var, distance):
        self.template_nodes = template_nodes
        self.tree_var = tree_var
        self.start_min_angle = 0
        self.start_max_angle = 360
        self.distance = distance

    def _render_node(self, context, parent, node, min_angle, max_angle, originex, originey, distance):
        bits = []
        total_leafs = node.leafs
        if node.level > 0:
            angle_position = ((max_angle - min_angle) / 2) + min_angle
            node.angle = angle_position
            radian_position = radians(angle_position)
            node.x = int(cos(radian_position) * (node.level * distance) + originex)
            node.y = int(sin(radian_position) * (node.level * distance) + originey)
            node.fromx = parent.x
            node.fromy = parent.y
#            print u"%s angle:%d (%d %d), level:%d, cos:%f, sin:%f, x:%d, y:%d" % (node.name, angle_position, min_angle, max_angle, node.level, cos(radian_position), sin(radian_position), node.x, node.y)
        else:
            node.x = originex
            node.y = originey
            node.fromx = 0
            node.fromy = 0
            node.margin = (node.max_level * distance) + 40
            node.size = (node.margin * 2)
#            print u"%s angle:aucun (%d %d), level:%d, x:%d, y:%d" % (node.name, min_angle, max_angle, node.level, node.x, node.y)

        current_angle = min_angle
        context.push()
        if not node.is_leaf:
            for child in node.childrens:
                context['node'] = child
                if (child.leafs > 0 ):
                    angle = (max_angle - min_angle) * child.leafs / total_leafs
                else:
                    angle = (max_angle - min_angle) / total_leafs
                next_angle = current_angle + angle
                bits.append(self._render_node(context, node, child, current_angle, next_angle, originex, originey, distance))
                current_angle = next_angle
            context['children'] = mark_safe(u''.join(bits))
        context['node'] = node
        rendered = self.template_nodes.render(context)
        context.pop()
        return rendered

    def render(self, context):
        tree = self.tree_var.resolve(context)
        distance = self.distance.resolve(context)
        max_level = tree.max_level
        originex = (max_level * distance) + (distance / 2)
        originey = (max_level * distance) + (distance / 2)
#        print u"X:%d - Y:%d" % (originex, originey)
        return self._render_node(context, None, tree, self.start_min_angle, self.start_max_angle, originex, originey, distance)

@register.tag
def recursemenu(parser, token):
    bits = token.contents.split()
    if len(bits) != 3:
        raise template.TemplateSyntaxError(_('%s tag requires 3 arguments') % bits[0])

    tree = template.Variable(bits[1])
    distance = template.Variable(bits[2])

    template_nodes = parser.parse(('endrecursemenu',))
    parser.delete_first_token()

    return RecurseMenuNode(template_nodes, tree, distance)

class LevelNode(template.Node):
    def __init__(self, tree_var, distance):
        self.tree_var = tree_var
        self.distance = distance
        self.levels = []
        self.current_level = -1

    def _list_levels(self, node, originex, originey, distance):
        if (node.level > self.current_level):
            self.current_level = node.level
            radius = (node.level * distance) + (distance / 2)
            level = {'num': node.level, 'radius': radius, 'x': originex, 'y': originey}
            self.levels.append(level)
        if not node.is_leaf:
            for child in node.childrens:
                self._list_levels(child, originex, originey, distance)

    def render(self, context):
        tree = self.tree_var.resolve(context)
        distance = self.distance.resolve(context)
        max_level = tree.max_level
        originex = (max_level * distance) + (distance / 2)
        originey = (max_level * distance) + (distance / 2)

        self._list_levels(tree, originex, originey, distance)
        context['levels'] = self.levels
        return ""

@register.tag
def list_levels(parser, token):
    bits = token.contents.split()
    if len(bits) != 3:
        raise template.TemplateSyntaxError(_('%s tag requires 3 arguments') % bits[0])

    tree = template.Variable(bits[1])
    distance = template.Variable(bits[2])
    return LevelNode(tree, distance)


import os
from pytube import YouTube
import discord
import ffmpeg
from discord.ext import commands

class misc(commands.Cog):
    def __init__(self,bot):
        self.bot = bot

    @commands.command(name='play')
    async def play(self,ctx,url):
        guild= ctx.message.guild

        song_check = os.path.isfile(f'{guild}.mp3')
        try:
            if song_check:
                os.remove(f'{guild}.mp3')
        except  PermissionError:
            await ctx.send("Wait for current audio to end")
            return
        yt = YouTube(url)
        stream = yt.streams.filter(only_audio=True).first()
        stream.download()
        for file in os.listdir('./'):
            if file.endswith(".mp4"):
                os.rename(file, f'{guild}.mp3')
        channel = ctx.message.author.voice.channel
        vc = await  channel.connect()
        print('Joined')
        server = ctx.message.guild
        voice_client = server.voice_client
        player = vc.play(discord.FFmpegPCMAudio(f'{guild}.mp3'))
        print('Playing')

    @commands.command(name='clean')
    @commands.has_permissions(administrator=True)
    async def purge(self,ctx,limit:int = 50):
        if ctx.author.guild_permissions.administrator:
            await ctx.channel.purge(limit=limit)
            await ctx.send(f'Cleared by {ctx.author.mention}')
            await  ctx.message.delete()
        else:
            await ctx.send("Admin power only!")

    @commands.command(name='help')
    async def help(self,ctx):
        embed=discord.Embed(title="Dergbot Help", description="Information on how to use me either @ me or use 'db.'", color=0x6830cf)
        embed.add_field(name="Reddit", value="Top", inline=True)
        embed.add_field(name="Nsfw", value="e621 ,r34 example\n ' db.e621 dragon,-female or db.r34 dragon,-female'", inline=False)
        embed.add_field(name="Fun stuff", value=f"hug example \n'db.hug @user'", inline=False)
        embed.set_thumbnail(url='https://d.furaffinity.net/art/raventhedragon/1564470492/1564470492.raventhedragon_4_-_kaqcfbq.gif')
        embed.set_footer(text="For more Information contact @Raβen Ebonscaly#0432")
        await ctx.message.channel.send(embed=embed)

def setup(bot):
    bot.add_cog(misc(bot))