from ROOT import *

def eraToLumi(era):
    eraToLumi = {
        "Run2018":59.965, ## Run2018 luminosity without normtag
        "Run2017":42.131, ## Run2017 luminosity with latest normtag
        "Run2016":36.295, ## Run2016 luminosity with latest normtag
        "Run2015":2.834 , ## Run2015 luminosity with latest normtag
        "AllRun2":141.23, ## Sum of all Run2 data

        "Run2017B":4930.853/1000,
        "Run2017C":9961.856/1000,
        "Run2017D":4352.364/1000,
        "Run2017E":9536.896/1000,
        "Run2017F":13817.819/1000,

        "Run2018A_CCUError":1087.642/1000, ## Run2018A_MuonPhys with CCU error, without normtag yet
        "Run2018A":12096.396/1000, ## Run2018A_MuonPhys without normtag yet
    }
    if era in eraToLumi: return eraToLumi[era]
    return 0

def buildLabel(era, preset):
    lumiVal = eraToLumi(era)
    if era.startswith("Run"):
        if len(era) > 1 and not era[-1].isdigit(): era = era[:-1]
        era = era[3:] + " data"
    else:
        era = ""

    labels = []
    #left, top = 0.17, 0.82
    if preset == "inset":
        left, top = gStyle.GetPadLeftMargin()+0.05, 1-gStyle.GetPadTopMargin()-0.09
        coverText1 = TLatex(left,top-0.00,"CMS")
        coverText2 = TLatex(left,top-0.02,"Preliminary")
        coverText3 = TLatex(left,top-0.07,era)
        coverText1.SetNDC()
        coverText2.SetNDC()
        coverText3.SetNDC()
        coverText1.SetTextFont(62)
        coverText2.SetTextFont(52)
        coverText3.SetTextFont(52)
        coverText1.SetTextAlign(11)
        coverText2.SetTextAlign(13)
        coverText3.SetTextAlign(13)
        coverText1.SetTextSize(0.06)
        coverText2.SetTextSize(0.0456)
        coverText3.SetTextSize(0.0456)

        if lumiVal > 0:
            lumi = TLatex(1-gStyle.GetPadRightMargin(), 1-gStyle.GetPadTopMargin()+0.01,
                          "%.2f fb^{-1} (13 TeV)" % (lumiVal))
        elif era == "":
            lumi = TLatex(1-gStyle.GetPadRightMargin(), 1-gStyle.GetPadTopMargin()+0.01,
                          "(13 TeV)")
        else:
            lumi = TLatex(1-gStyle.GetPadRightMargin(), 1-gStyle.GetPadTopMargin()+0.01,
                          "(%s, 13 TeV)" % (era))
        lumi.SetTextSize(0.05)

        labels = [coverText1, coverText2, coverText3, lumi]
    else:
        left, top = gStyle.GetPadLeftMargin()+0.01, 1-gStyle.GetPadTopMargin()+0.01
        coverText1 = TLatex(left+0.00,top,"CMS")
        coverText2 = TLatex(left+0.12,top,"Preliminary")
        coverText1.SetNDC()
        coverText2.SetNDC()
        coverText1.SetTextFont(62)
        coverText2.SetTextFont(52)
        coverText1.SetTextAlign(11)
        coverText2.SetTextAlign(11)
        coverText1.SetTextSize(0.06)
        coverText2.SetTextSize(0.0456)

        if lumiVal > 0:
            lumi = TLatex(1-gStyle.GetPadRightMargin(), 1-gStyle.GetPadTopMargin()+0.01,
                          "%.2f fb^{-1} (%s, 13 TeV)" % (lumiVal, era))
        elif era == "":
            lumi = TLatex(1-gStyle.GetPadRightMargin(), 1-gStyle.GetPadTopMargin()+0.01,
                          "(13 TeV)")
        else:
            lumi = TLatex(1-gStyle.GetPadRightMargin(), 1-gStyle.GetPadTopMargin()+0.01,
                          "(%s, 13 TeV)" % (era))
        lumi.SetTextSize(0.04)

        labels = [coverText1, coverText2, lumi]

    lumi.SetNDC()
    lumi.SetTextAlign(31)
    lumi.SetTextFont(42)

    return labels


from bs4 import BeautifulSoup
import urllib.request
import time
import random
import sys

class TextCrawler:
    def __init__(self, gall_name, page_number):
        self.list_url = 'http://gall.dcinside.com/board/lists/?id=' + gall_name
        self.view_url = 'http://gall.dcinside.com/board/view/?id=' + gall_name
        self.page_number = page_number

    def crawl(self):
        page = 1
        f = open('data.txt', 'w')

        while page <= self.page_number:
            url_open = urllib.request.urlopen(self.list_url +
                                                '&page=' + str(page))
            soup = BeautifulSoup(url_open, 'html.parser',
                                    from_encoding='utf-8')
            notice_list = soup.findAll('td', {'class':'t_notice'})
            subject_list = soup.findAll('td', {'class':'t_subject'})

            for i in range(0, len(notice_list)):    # 공지사항 제거
                if notice_list[0].text == '공지':
                    notice_list.pop(0)
                    subject_list.pop(0)

            for subject in subject_list:    # 제목 저장
                f.write(subject.text + '\n')
                print(subject.text)

            random_number = random.randrange(1,5)
            print('%d page subject crawling complete. %d sec sleep...'\
                    % (page, random_number))
            time.sleep(random_number)

            for notice in notice_list:
                url_open = urllib.request.urlopen(self.view_url +
                                                    '&no=' + str(notice.text) +
                                                    '&page=' + str(page))
                soup = BeautifulSoup(url_open, 'html.parser',
                                        from_encoding='utf-8')
                content = soup.find('div', {'class':'s_write'})
                content = content.table.tr.td.text

                f.write(content + '\n')     # 본문 저장
                print(content)

                random_number = random.randrange(1,5)
                print('%s content crawling complete. %d sec sleep...'\
                        % (notice.text, random_number))
                time.sleep(random_number)

            page += 1

        f.close()

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('python3 wordcloud.py [GALL_NAME] [PAGE_NUMBER]')
        sys.exit()

    gall_name = str(sys.argv[1])
    page_number = int(sys.argv[2])
    t = TextCrawler(gall_name, page_number)
    t.crawl()


class Bike(object):
    def __init__(self,price,max_speed):
        self.price = price
        self.max_speed = max_speed
        self.miles = 0

    def displayInfo(self):
        print(self.price)
        print(self.max_speed)
        print(self.miles)
        return self

    def ride(self):
        print("Riding")
        self.miles += 10
        return self

    def reverse(self):
        print("Reversing")
        self.miles -= 5
        if (self.miles < 0):
            self.miles = 0
        return self

i_want_to_ride_my = Bike(1000,"50mph")
rosie = Bike(500,"100mph")
mudflap = Bike(25,"1000mph")

i_want_to_ride_my.ride().ride().ride().reverse().displayInfo()
rosie.ride().ride().reverse().reverse().displayInfo()
mudflap.reverse().reverse().reverse().displayInfo()


from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="C:\\Users\iabba\AppData\Local\Programs\chromedriver_win32(1)\chromedriver.exe")
driver.get("https://developer.salesforce.com/signup")

obj=driver.find_elements(By.NAME,"job_role")
p1=obj.find_elements(By.TAG_NAME,"option")

print(len(p1))

for element in range(len(p1)):
    if p1[element].is_displayed():
        print(p1[element].text)


import numpy as np
import json, os

# load data (149 in each class)
data = json.load(open(os.getcwd()+'/data/africanamerican_controls.json'))
X= np.array(data['africanamerican'])
Y= np.array(data['controls'])

'''
Feature output:

There are two mfcc and mfcc delta arrays, one that averages
over 20 millisecond windows appended with one over 0.5 second windows.
In this way, there can be some heirarchy in the embedding.

Particularly, here is the output

labels = ['mfcc1_mean_(0.02 second window)', 'mfcc1_std_(0.02 second window)', 'mfcc1_max_(0.02 second window)', 'mfcc1_min_(0.02 second window)',
		'mfcc2_mean_(0.02 second window)', 'mfcc2_std_(0.02 second window)', 'mfcc2_max_(0.02 second window)', 'mfcc2_min_(0.02 second window)',
		'mfcc3_mean_(0.02 second window)', 'mfcc3_std_(0.02 second window)', 'mfcc3_max_(0.02 second window)', 'mfcc3_min_(0.02 second window)',
		'mfcc4_mean_(0.02 second window)', 'mfcc4_std_(0.02 second window)', 'mfcc4_max_(0.02 second window)', 'mfcc4_min_(0.02 second window)',
		'mfcc5_mean_(0.02 second window)', 'mfcc5_std_(0.02 second window)', 'mfcc5_max_(0.02 second window)', 'mfcc5_min_(0.02 second window)',
		'mfcc6_mean_(0.02 second window)', 'mfcc6_std_(0.02 second window)', 'mfcc6_max_(0.02 second window)', 'mfcc6_min_(0.02 second window)',
		'mfcc7_mean_(0.02 second window)', 'mfcc7_std_(0.02 second window)', 'mfcc7_max_(0.02 second window)', 'mfcc7_min_(0.02 second window)',
		'mfcc8_mean_(0.02 second window)', 'mfcc8_std_(0.02 second window)', 'mfcc8_max_(0.02 second window)', 'mfcc8_min_(0.02 second window)',
		'mfcc9_mean_(0.02 second window)', 'mfcc9_std_(0.02 second window)', 'mfcc9_max_(0.02 second window)', 'mfcc9_min_(0.02 second window)',
		'mfcc10_mean_(0.02 second window)', 'mfcc10_std_(0.02 second window)', 'mfcc10_max_(0.02 second window)', 'mfcc10_min_(0.02 second window)',
		'mfcc11_mean_(0.02 second window)', 'mfcc11_std_(0.02 second window)', 'mfcc11_max_(0.02 second window)', 'mfcc11_min_(0.02 second window)',
		'mfcc12_mean_(0.02 second window)', 'mfcc12_std_(0.02 second window)', 'mfcc12_max_(0.02 second window)', 'mfcc12_min_(0.02 second window)',
		'mfcc13_mean_(0.02 second window)', 'mfcc13_std_(0.02 second window)', 'mfcc13_max_(0.02 second window)', 'mfcc13_min_(0.02 second window)',
		'mfccdelta1_mean_(0.02 second window)', 'mfccdelta1_std_(0.02 second window)', 'mfccdelta1_max_(0.02 second window)', 'mfccdelta1_min_(0.02 second window)',
		'mfccdelta2_mean_(0.02 second window)', 'mfccdelta2_std_(0.02 second window)', 'mfccdelta2_max_(0.02 second window)', 'mfccdelta2_min_(0.02 second window)',
		'mfccdelta3_mean_(0.02 second window)', 'mfccdelta3_std_(0.02 second window)', 'mfccdelta3_max_(0.02 second window)', 'mfccdelta3_min_(0.02 second window)',
		'mfccdelta4_mean_(0.02 second window)', 'mfccdelta4_std_(0.02 second window)', 'mfccdelta4_max_(0.02 second window)', 'mfccdelta4_min_(0.02 second window)',
		'mfccdelta5_mean_(0.02 second window)', 'mfccdelta5_std_(0.02 second window)', 'mfccdelta5_max_(0.02 second window)', 'mfccdelta5_min_(0.02 second window)',
		'mfccdelta6_mean_(0.02 second window)', 'mfccdelta6_std_(0.02 second window)', 'mfccdelta6_max_(0.02 second window)', 'mfccdelta6_min_(0.02 second window)',
		'mfccdelta7_mean_(0.02 second window)', 'mfccdelta7_std_(0.02 second window)', 'mfccdelta7_max_(0.02 second window)', 'mfccdelta7_min_(0.02 second window)',
		'mfccdelta8_mean_(0.02 second window)', 'mfccdelta8_std_(0.02 second window)', 'mfccdelta8_max_(0.02 second window)', 'mfccdelta8_min_(0.02 second window)',
		'mfccdelta9_mean_(0.02 second window)', 'mfccdelta9_std_(0.02 second window)', 'mfccdelta9_max_(0.02 second window)', 'mfccdelta9_min_(0.02 second window)',
		'mfccdelta10_mean_(0.02 second window)', 'mfccdelta10_std_(0.02 second window)', 'mfccdelta10_max_(0.02 second window)', 'mfccdelta10_min_(0.02 second window)',
		'mfccdelta11_mean_(0.02 second window)', 'mfccdelta11_std_(0.02 second window)', 'mfccdelta11_max_(0.02 second window)', 'mfccdelta11_min_(0.02 second window)',
		'mfccdelta12_mean_(0.02 second window)', 'mfccdelta12_std_(0.02 second window)', 'mfccdelta12_max_(0.02 second window)', 'mfccdelta12_min_(0.02 second window)',
		'mfccdelta13_mean_(0.02 second window)', 'mfccdelta13_std_(0.02 second window)', 'mfccdelta13_max_(0.02 second window)', 'mfccdelta13_min_(0.02 second window)',
		'mfcc1_mean_(0.50 second window)', 'mfcc1_std_(0.50 second window)', 'mfcc1_max_(0.50 second window)', 'mfcc1_min_(0.50 second window)',
		'mfcc2_mean_(0.50 second window)', 'mfcc2_std_(0.50 second window)', 'mfcc2_max_(0.50 second window)', 'mfcc2_min_(0.50 second window)',
		'mfcc3_mean_(0.50 second window)', 'mfcc3_std_(0.50 second window)', 'mfcc3_max_(0.50 second window)', 'mfcc3_min_(0.50 second window)',
		'mfcc4_mean_(0.50 second window)', 'mfcc4_std_(0.50 second window)', 'mfcc4_max_(0.50 second window)', 'mfcc4_min_(0.50 second window)',
		'mfcc5_mean_(0.50 second window)', 'mfcc5_std_(0.50 second window)', 'mfcc5_max_(0.50 second window)', 'mfcc5_min_(0.50 second window)',
		'mfcc6_mean_(0.50 second window)', 'mfcc6_std_(0.50 second window)', 'mfcc6_max_(0.50 second window)', 'mfcc6_min_(0.50 second window)',
		'mfcc7_mean_(0.50 second window)', 'mfcc7_std_(0.50 second window)', 'mfcc7_max_(0.50 second window)', 'mfcc7_min_(0.50 second window)',
		'mfcc8_mean_(0.50 second window)', 'mfcc8_std_(0.50 second window)', 'mfcc8_max_(0.50 second window)', 'mfcc8_min_(0.50 second window)',
		'mfcc9_mean_(0.50 second window)', 'mfcc9_std_(0.50 second window)', 'mfcc9_max_(0.50 second window)', 'mfcc9_min_(0.50 second window)',
		'mfcc10_mean_(0.50 second window)', 'mfcc10_std_(0.50 second window)', 'mfcc10_max_(0.50 second window)', 'mfcc10_min_(0.50 second window)',
		'mfcc11_mean_(0.50 second window)', 'mfcc11_std_(0.50 second window)', 'mfcc11_max_(0.50 second window)', 'mfcc11_min_(0.50 second window)',
		'mfcc12_mean_(0.50 second window)', 'mfcc12_std_(0.50 second window)', 'mfcc12_max_(0.50 second window)', 'mfcc12_min_(0.50 second window)',
		'mfcc13_mean_(0.50 second window)', 'mfcc13_std_(0.50 second window)', 'mfcc13_max_(0.50 second window)', 'mfcc13_min_(0.50 second window)',
		'mfccdelta1_mean_(0.50 second window)', 'mfccdelta1_std_(0.50 second window)', 'mfccdelta1_max_(0.50 second window)', 'mfccdelta1_min_(0.50 second window)',
		'mfccdelta2_mean_(0.50 second window)', 'mfccdelta2_std_(0.50 second window)', 'mfccdelta2_max_(0.50 second window)', 'mfccdelta2_min_(0.50 second window)',
		'mfccdelta3_mean_(0.50 second window)', 'mfccdelta3_std_(0.50 second window)', 'mfccdelta3_max_(0.50 second window)', 'mfccdelta3_min_(0.50 second window)',
		'mfccdelta4_mean_(0.50 second window)', 'mfccdelta4_std_(0.50 second window)', 'mfccdelta4_max_(0.50 second window)', 'mfccdelta4_min_(0.50 second window)',
		'mfccdelta5_mean_(0.50 second window)', 'mfccdelta5_std_(0.50 second window)', 'mfccdelta5_max_(0.50 second window)', 'mfccdelta5_min_(0.50 second window)',
		'mfccdelta6_mean_(0.50 second window)', 'mfccdelta6_std_(0.50 second window)', 'mfccdelta6_max_(0.50 second window)', 'mfccdelta6_min_(0.50 second window)',
		'mfccdelta7_mean_(0.50 second window)', 'mfccdelta7_std_(0.50 second window)', 'mfccdelta7_max_(0.50 second window)', 'mfccdelta7_min_(0.50 second window)',
		'mfccdelta8_mean_(0.50 second window)', 'mfccdelta8_std_(0.50 second window)', 'mfccdelta8_max_(0.50 second window)', 'mfccdelta8_min_(0.50 second window)',
		'mfccdelta9_mean_(0.50 second window)', 'mfccdelta9_std_(0.50 second window)', 'mfccdelta9_max_(0.50 second window)', 'mfccdelta9_min_(0.50 second window)',
		'mfccdelta10_mean_(0.50 second window)', 'mfccdelta10_std_(0.50 second window)', 'mfccdelta10_max_(0.50 second window)', 'mfccdelta10_min_(0.50 second window)',
		'mfccdelta11_mean_(0.50 second window)', 'mfccdelta11_std_(0.50 second window)', 'mfccdelta11_max_(0.50 second window)', 'mfccdelta11_min_(0.50 second window)',
		'mfccdelta12_mean_(0.50 second window)', 'mfccdelta12_std_(0.50 second window)', 'mfccdelta12_max_(0.50 second window)', 'mfccdelta12_min_(0.50 second window)',

Or, represented simply:

mfcc 1-13 - mean, std, max, min
mfcc delta 1-13 - mean, std, max, min

for 0.020 and 0.5 second windows.
'''

################################################################
################################################################
##                 UNSUPERVISED TECHNIQUES                    ##
################################################################
################################################################
'''
Unsupervised techniques do not need to know anything about the dataset;
they are simple manipulations mathematically to compress features to
a smaller feature space.

They calculate things like variance and/or mean values of data and manipulate
the matrices to a smaller space.

Here are some common techniques:

(1) Principal component analysis (PCA)
(2) Independent component analysis (ICA)
(3) K-means clustering (vector quantization)
(4) Canonical correlation analysis
(5) Partial least squares regression
(6) Manifold learning

'''

################################################################
# Principal component analysis (PCA) - useful for Gaussian processes
# http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html
################################################################
'''
PCA's main weakness is that it tends to be highly affected by outliers in the data.
For this reason, many robust variants of PCA have been developed, many of which act to
iteratively discard data points that are poorly described by the initial components.
Scikit-Learn contains a couple interesting variants on PCA, including RandomizedPCA and SparsePCA,
both also in the sklearn.decomposition submodule.
'''

from sklearn.decomposition import PCA

# calculate PCA for 50 components
pca = PCA(n_components=50)
pca.fit(X)
X_pca = pca.transform(X)

print("PCA original shape:   ", X.shape)
print("PCA transformed shape:", X_pca.shape)

print(pca.explained_variance_ratio_)
print(np.sum(pca.explained_variance_ratio_))

'''By using the explained_variance_ratio_, you can see that the the data is
quite distributed, and no one component really makes up the mixture. This is
expected, as mfcc coefficients are in different bands and represent the voice range,
meaning that we have a good, compressed feature representation for our dataset.

array([4.36108568e-01, 2.30122532e-01, 9.30360419e-02, 3.50530237e-02,
       3.11003034e-02, 2.58828314e-02, 2.22265155e-02, 1.85299568e-02,
       1.42322686e-02, 1.24397825e-02, 9.44568239e-03, 8.88464867e-03,
       6.50330969e-03, 5.80186788e-03, 5.31114033e-03, 4.59182861e-03,
       4.34041947e-03, 3.74514144e-03, 3.38655973e-03, 2.87419155e-03,
       2.58491433e-03, 2.38243972e-03, 2.29385894e-03, 1.69828308e-03,
       1.57929416e-03, 1.45641917e-03, 1.33611976e-03, 1.23183500e-03,
       1.20274412e-03, 1.02064060e-03, 9.78714995e-04, 8.22881795e-04,
       7.62307128e-04, 7.02134801e-04, 6.61520330e-04, 5.71506430e-04,
       4.51561555e-04, 3.94488637e-04, 3.78635627e-04, 3.59457300e-04,
       3.40696332e-04, 2.69683415e-04, 2.44483556e-04, 2.22901479e-04,
       2.10448873e-04, 1.96806180e-04, 1.83114974e-04, 1.55056915e-04,
       1.36493195e-04, 1.24699968e-04])
'''

print(pca.singular_values_)
''' Singular values are used to determine uniqueness of each variable
and directionality of variance in the dataset; useful for plotting things.
You don't really need to know much about this right now, though.'''

################################################################
# Independent component analysis (ICA)
# http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.FastICA.html
################################################################
'''
Useful for non-Gaussian processes / cocktail party

ICA assumes
(1) independence of source signals;
(2) sum of two signals tends toward a gaussian distribution more than each signal alone
(3) there is more complexity in the signal mixture than in the simplest source signal

Useful to separate out signals in a complex mixture - for example,
to hone in on a specific voice.
'''
from sklearn.decomposition import FastICA

ica = FastICA(n_components=50)
S_ = ica.fit_transform(X)  # Reconstruct signals

# The mixing matrix is analagous to PCA singular values
A_ = ica.mixing_  # Get estimated mixing matrix

################################################################
# K-means clustering ('vector quantization')
# http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html
################################################################
'''
Example: http://scikit-learn.org/stable/auto_examples/text/document_clustering.html
- useful for speech recognition purposes
- useful to reconstruct datasets that have incomplete data (with .predict)
'''
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=50, random_state=0).fit_transform(X)

# can predict as well and get cluster centers...
# print(kmeans.labels_)
# print(kmeans.predict(X)
# print(kmeans.cluster_centers_)

################################################################
# Canonical correlation analysis
# http://scikit-learn.org/stable/modules/generated/sklearn.cross_decomposition.CCA.html
################################################################
'''
Uses information from two groups of data to best fit and transform this data.

Creates a new matrix in X and Y directions with dimensionality N, 50
(if 50 clusters).
'''
from sklearn.cross_decomposition import CCA
cca = CCA(n_components=50).fit(X, Y).transform(X, Y)
new_X=cca[0]
new_Y=cca[1]

# if missing values
# Y_pred=cca.predict(xval)

################################################################
# Partial least squares regression
# http://scikit-learn.org/stable/modules/generated/sklearn.cross_decomposition.PLSRegression.html
################################################################
from sklearn.cross_decomposition import PLSRegression
pls = PLSRegression(n_components=50).fit(X, Y).transform(X, Y)
pls_X=pls[0]
pls_Y=pls[1]

# if missing values
#Y_pred = pls2.predict(X)

################################################################
# Manifold learning - isomap algorithm
# http://scikit-learn.org/stable/auto_examples/manifold/plot_compare_methods.html
################################################################
# (there are many types of algorithms here, just one for reference)
# check out [] for more examples
from sklearn import manifold

# make new dimensions for X and Y
manifold_X = manifold.Isomap(10, 50).fit_transform(X)
manifold_Y = manifold.Isomap(10,50).fit_transform(Y)

################################################################
################################################################
##                  SUPERVISED TECHNIQUES                     ##
################################################################
################################################################
'''
Supervised dimensionality reduction techniques use some of the
data from the original feature set in order to extract patterns
and then reduce dimensionality

Here, there are three common techniques outlined:

(1) supervised dictionary learning
(2) linear discriminant analysis
(3) variational autoencoder
'''

# ################################################################
# supervised dictionary learning (SDL)
# http://scikit-learn.org/stable/auto_examples/decomposition/plot_image_denoising.html
################################################################
'''
Takes a bit longer to execute, but has pros listed on table.
'''
from sklearn.decomposition import MiniBatchDictionaryLearning
dico_X = MiniBatchDictionaryLearning(n_components=50, alpha=1, n_iter=500).fit_transform(X)
dico_Y = MiniBatchDictionaryLearning(n_components=50, alpha=1, n_iter=500).fit_transform(Y)

################################################################
# linear discriminant analysis (LDA)
# http://scikit-learn.org/stable/modules/lda_qda.html
################################################################
'''
Unlike PCA, LDA tries to reduce dimensions of the feature set while
retaining the information that discriminates output classes.

PCA reduces dimensions by focusing on the data with the most variations.
This is useful for the high dimensional data because PCA helps us to draw a simple XY plot.

But we'll use LDA when we're interested maximizing the separability of the
data so that we can make the best decisions. In other words,
though LDA (supervised) is similar to PCA (unsupervised),
but LDA focuses on maximizing the separability among known categories.

This is useful for things like phoneme detection in speech recognition projects.
'''
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis as QDA
from sklearn.model_selection import train_test_split

# make a train set and train labels
Z=np.array(list(X)+list(Y))
newlabels=list()
for i in range(len(X)):
	newlabels.append('1')
for i in range(len(Y)):
	newlabels.append('0')

X_train, X_test, y_train, y_test = train_test_split(Z, newlabels, test_size=0.33, random_state=42)

lda = LDA(n_components=50).fit(X_train, y_train).transform(X)

################################################################
# variational autoencoder (deep learning technique)
# https://blog.keras.io/building-autoencoders-in-keras.html
################################################################
from keras.layers import Input, Dense
from keras.models import Model
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder

# preprocess labels (make into integers)
label_encoder = LabelEncoder()
y_train=label_encoder.fit_transform(y_train)
y_test=label_encoder.fit_transform(y_test)

# this is the size of our encoded representations (208 features in X)
encoding_dim = 32

# add a few dimensions for encoder and decoder
input_dim = Input(shape=X_train[0].shape)
encoder=Dense(encoding_dim, activation='tanh')

autoencoder = Model(input_dim, decoded)

# this model maps an input to its encoded representation
encoder = Model(input_dim, encoded)

# create a placeholder for an encoded (50-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))

# now train autoencoder
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(X_train, y_train,
                epochs=50,
                batch_size=256,
                shuffle=True,
                validation_data=(X_test, y_test))

# predict emebddings
encoded_audio = encoder.predict(X_test)
decoded_audio = decoder.predict(encoded_audio)


import struct

from pyreveng import leaf

# Characters from text strings that go into their labels.
LABELCHAR = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?_'

# Standard ASCII character set.
ASCII = {x : "%c" % x for x in range(32, 127)}
ASCII[10] = "\\n"
ASCII[13] = "\\r"

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

class Data(leaf.Leaf):
    def __init__(self, asp, lo, hi, t="data", fmt=None):
        super().__init__(lo, hi, t)
        asp.insert(self)
        self.fmt = fmt

    def render(self):
        if self.rendered:
            return self.rendered
        if self.fmt:
            return self.fmt
        return "<Data %x-%x %s>" % (self.lo, self.hi, self.tag)

class Const(Data):
    def __init__(self, asp, lo, hi=None, fmt=None, func=None, size=1):
        if hi is None:
            hi = lo + 1
        super().__init__(asp, lo, hi, "const")
        if func is None:
            func = asp.__getitem__
        if fmt is None:
            fmt = "0x%x"
        self.val = []
        for a in range(lo, hi, size):
            self.val.append(func(a))
        self.fmt = ",".join(fmt % i for i in self.val)
        self.typ = ".CONST"
        self.compact = True

    def render(self):
        return self.typ + "\t" + self.fmt

class Pstruct(Data):
    ''' Uses python struct.* to untangle data '''
    def __init__(self, asp, lo, spec, fmt=None, typ=".PSTRUCT"):
        bcnt = struct.calcsize(spec)
        self.data = struct.unpack(spec, asp.bytearray(lo, bcnt))
        super().__init__(asp, lo, lo + bcnt, "const")
        self.spec = spec
        self.fmt = fmt
        self.typ = typ

    def render(self):
        if self.fmt is not None:
            return self.typ + "\t" + self.fmt % self.data
        return self.typ + "\t" + self.spec + " = " + str(self.data)

class Codeptr(Data):
    def __init__(self, asp, lo, hi, dst):
        super().__init__(asp, lo, hi, "codeptr")
        self.dst = dst

    def render(self):
        return ".CODE\t" + self.aspace.adr(self.dst)

    def arg_render(self):
        return self.aspace.adr(self.dst)

class Dataptr(Data):
    def __init__(self, asp, lo, hi, dst, dasp=None, pfx="", sfx=""):
        super().__init__(asp, lo, hi, "dataptr")
        self.dst = dst
        if dasp:
            self.dasp = dasp
        else:
            self.dasp = asp
        self.pfx = pfx
        self.sfx = sfx

    def render(self):
        return ".PTR\t" + self.pfx + self.dasp.adr(self.dst) + self.sfx

def stringify(asp, lo, length=None, term=None, charset=None):
    if term is None:
        term = (0,)
    if not charset:
        charset = ASCII
    s = ""
    while True:
        x = asp[lo]
        lo += 1
        if length is None and x in term:
            return lo, s
        t = charset.get(x)
        if t:
            s += t
        else:
            s += "\\x%02x" % x
        if length is not None:
            length -= 1
            if length == 0:
                return lo, s

class Txt(Data):
    def __init__(self, asp, lo, hi=None,
        label=True, pfx=None, align=1, splitnl=False, **kwargs):
        self.splitnl = splitnl
        self.pre = ""
        if pfx == 1:
            x = asp[lo]
            self.pre = '%d,' % x
            hi, s = stringify(asp, lo + 1, length=x, **kwargs)
            while hi % align:
                hi += 1
        elif pfx is not None:
            raise Exception("unknown pfx")
        elif hi is None:
            hi, s = stringify(asp, lo, **kwargs)
            while hi % align:
                hi += 1
        else:
            hi, s = stringify(asp, lo, hi - lo, **kwargs)

        super().__init__(asp, lo, hi, "txt")
        self.txt = s
        self.fmt = "'" + s + "'"
        if label and s.strip() == "":
            lbl = 't_%d*SP' % len(s)
            asp.set_label(lo, lbl)
        elif label:
            lbl = 't_'
            for i in s:
                if i in LABELCHAR:
                    lbl += i
                elif lbl[-1] != '_':
                    lbl += '_'
            asp.set_label(lo, lbl)
        self.compact = True

    def render(self):
        if not self.splitnl:
            return ".TXT\t" + self.pre + "'" + self.txt + "'"

        l = self.txt.split('\\n')
        if len(l) == 1:
            return ".TXT\t" + self.pre + "'" + self.txt + "'"

        txt = ''
        p = self.pre

        for i in l[:-1]:
            txt += '.TXT\t' + p + "'" + i + "\\n'\n"
            p = ''
        if l[-1]:
            txt += '.TXT\t' + p + "'" + l[-1] + "'"
        return txt

    def arg_render(self):
        return "'" + self.txt + "'"


import discord
from discord.ext import commands

client = commands.Bot(command_prefix='!', intents=discord.Intents.all())

@client.event
async def on_ready():
  print('Is read:')
  print(client.user.id, client.user)

@client.command()
async def ping(ctx):
  await ctx.send('pong')

client.run('MTAzNDUxOTA4NTI4MDY2NTY2MQ.Gi3Kda.nCPw3BSMNVY11QiI5e5SRDvL5Sq2fNTS8CObks')


import argparse
import torch
from voiced import train

parser = argparse.ArgumentParser()

# Training and validation input filepaths
parser.add_argument('--train_images_path',
    type=str, required=True, help='Path to list of training image paths')
parser.add_argument('--train_sparse_depth_path',
    type=str, required=True, help='Path to list of training sparse depth paths')
parser.add_argument('--train_intrinsics_path',
    type=str, required=True, help='Path to list of training camera intrinsics paths')
parser.add_argument('--val_image_path',
    type=str, default=None, help='Path to list of validation image paths')
parser.add_argument('--val_sparse_depth_path',
    type=str, default=None, help='Path to list of validation sparse depth paths')
parser.add_argument('--val_ground_truth_path',
    type=str, default=None, help='Path to list of validation ground truth depth paths')

# Dataloader settings
parser.add_argument('--n_batch',
    type=int, default=8, help='Number of samples per batch')
parser.add_argument('--n_height',
    type=int, default=320, help='Height of of sample')
parser.add_argument('--n_width',
    type=int, default=768, help='Width of each sample')

# Input settings
parser.add_argument('--input_channels_image',
    type=int, default=3, help='Number of input image channels')
parser.add_argument('--input_channels_depth',
    type=int, default=2, help='Number of input depth channels')
parser.add_argument('--outlier_removal_kernel_size',
    type=int, default=7, help='Kernel size to filter outlier sparse depth')
parser.add_argument('--outlier_removal_threshold',
    type=float, default=1.5, help='Difference threshold to consider a point an outlier')

# Network settings
parser.add_argument('--encoder_type',
    nargs='+', type=str, default=['vggnet08'], help='Encoder type to use')
parser.add_argument('--n_filters_encoder_image',
    nargs='+', type=int, default=[48, 96, 192, 384, 384], help='Space delimited list of filters to use in each block of image encoder')
parser.add_argument('--n_filters_encoder_depth',
    nargs='+', type=int, default=[16, 32, 64, 128, 128], help='Space delimited list of filters to use in each block of depth encoder')
parser.add_argument('--n_filters_decoder',
    nargs='+', type=int, default=[256, 128, 128, 64, 32], help='Space delimited list of filters to use in each block of decoder')
parser.add_argument('--min_predict_depth',
    type=float, default=1.5, help='Minimum value of depth prediction')
parser.add_argument('--max_predict_depth',
    type=float, default=100.0, help='Maximum value of depth prediction')

# Weight settings
parser.add_argument('--weight_initializer',
    type=str, default='xavier_normal', help='Weight initialization type: kaiming_uniform, kaiming_normal, xavier_uniform, xavier_normal')
parser.add_argument('--activation_func',
    type=str, default='leaky_relu', help='Activation function after each layer: relu, leaky_relu, elu, sigmoid')

# Training settings
parser.add_argument('--learning_rates',
    nargs='+', type=float, default=[1.00e-4, 0.50e-4, 0.25e-4], help='Space delimited list of learning rates')
parser.add_argument('--learning_schedule',
    nargs='+', type=int, default=[20, 40, 60], help='Space delimited list to change learning rate')
parser.add_argument('--rotation_parameterization',
    type=str, default='euler', help='Rotation parameterization: euler, exponential')
parser.add_argument('--augmentation_random_crop_type',
    nargs='+', type=str, default=['horizontal', 'vertical', 'anchored', 'bottom'], help='Random crop type for data augmentation: horizontal, vertical, anchored, bottom')

# Loss function settings
parser.add_argument('--w_color',
    type=float, default=0.20, help='Weight of color consistency loss')
parser.add_argument('--w_structure',
    type=float, default=0.80, help='Weight of structural consistency loss')
parser.add_argument('--w_sparse_depth',
    type=float, default=1.00, help='Weight of sparse depth consistency loss')
parser.add_argument('--w_smoothness',
    type=float, default=0.15, help='Weight of local smoothness loss')
parser.add_argument('--w_pose',
    type=float, default=0.10, help='Weight of pose consistency loss')
parser.add_argument('--w_weight_decay_depth',
    type=float, default=0.0, help='Weight of weight decay regularization for depth')
parser.add_argument('--w_weight_decay_pose',
    type=float, default=1e-4, help='Weight of weight decay regularization for pose')

# Evaluation settings
parser.add_argument('--min_evaluate_depth',
    type=float, default=0.0, help='Minimum value of depth to evaluate')
parser.add_argument('--max_evaluate_depth',
    type=float, default=100.0, help='Maximum value of depth to evaluate')

# Checkpoint settings
parser.add_argument('--checkpoint_path',
    type=str, required=True, help='Path to save checkpoints')
parser.add_argument('--n_step_per_checkpoint',
    type=int, default=5000, help='Number of iterations for each checkpoint')
parser.add_argument('--n_step_per_summary',
    type=int, default=5000, help='Number of iterations before logging summary')
parser.add_argument('--n_image_per_summary',
    type=int, default=4, help='Number of samples to include in visual display summary')
parser.add_argument('--start_step_validation',
    type=int, default=200000, help='Number of steps before starting validation')
parser.add_argument('--depth_model_restore_path',
    type=str, default=None, help='Path to restore depth model from checkpoint')
parser.add_argument('--pose_model_restore_path',
    type=str, default=None, help='Path to restore pose model from checkpoint')

# Hardware settings
parser.add_argument('--device',
    type=str, default='cuda', help='Device to use: cuda, gpu, cpu')
parser.add_argument('--n_thread',
    type=int, default=8, help='Number of threads for fetching')

args = parser.parse_args()

if __name__ == '__main__':

    # Weight settings
    args.weight_initializer = args.weight_initializer.lower()

    args.activation_func = args.activation_func.lower()

    # Training settings
    assert len(args.learning_rates) == len(args.learning_schedule)

    args.augmentation_random_crop_type = [
        crop_type.lower() for crop_type in args.augmentation_random_crop_type
    ]

    # Hardware settings
    args.device = args.device.lower()
    if args.device not in ['cuda', 'gpu', 'cpu']:
        args.device = 'cuda' if torch.cuda.is_available() else 'cpu'

    args.device = 'cuda' if args.device == 'gpu' else args.device

    train(train_images_path=args.train_images_path,
          train_sparse_depth_path=args.train_sparse_depth_path,
          train_intrinsics_path=args.train_intrinsics_path,
          val_image_path=args.val_image_path,
          val_sparse_depth_path=args.val_sparse_depth_path,
          val_ground_truth_path=args.val_ground_truth_path,
          # Dataloader settings
          n_batch=args.n_batch,
          n_height=args.n_height,
          n_width=args.n_width,
          # Input settings
          input_channels_image=args.input_channels_image,
          input_channels_depth=args.input_channels_depth,
          outlier_removal_kernel_size=args.outlier_removal_kernel_size,
          outlier_removal_threshold=args.outlier_removal_threshold,
          # Network settings
          encoder_type=args.encoder_type,
          n_filters_encoder_image=args.n_filters_encoder_image,
          n_filters_encoder_depth=args.n_filters_encoder_depth,
          n_filters_decoder=args.n_filters_decoder,
          min_predict_depth=args.min_predict_depth,
          max_predict_depth=args.max_predict_depth,
          # Weight settings
          weight_initializer=args.weight_initializer,
          activation_func=args.activation_func,
          # Training settings
          learning_rates=args.learning_rates,
          learning_schedule=args.learning_schedule,
          rotation_parameterization=args.rotation_parameterization,
          augmentation_random_crop_type=args.augmentation_random_crop_type,
          # Loss function settings
          w_color=args.w_color,
          w_structure=args.w_structure,
          w_sparse_depth=args.w_sparse_depth,
          w_smoothness=args.w_smoothness,
          w_pose=args.w_pose,
          w_weight_decay_depth=args.w_weight_decay_depth,
          w_weight_decay_pose=args.w_weight_decay_pose,
          # Depth evaluation settings
          min_evaluate_depth=args.min_evaluate_depth,
          max_evaluate_depth=args.max_evaluate_depth,
          # Checkpoint settings
          checkpoint_path=args.checkpoint_path,
          n_step_per_checkpoint=args.n_step_per_checkpoint,
          n_step_per_summary=args.n_step_per_summary,
          n_image_per_summary=args.n_image_per_summary,
          start_step_validation=args.start_step_validation,
          depth_model_restore_path=args.depth_model_restore_path,
          pose_model_restore_path=args.pose_model_restore_path,
          # Hardware settings
          device=args.device,
          n_thread=args.n_thread)


# https://leetcode.com/problems/sqrtx/

# Use binarySearch only
def Solution(x=int):
    if x <= 1:
        return x
    left = 1
    right = x
    while True:
        mid = (left + right) // 2
        if mid ** 2 == x:
            return mid
        elif mid ** 2 > x:
            right = mid
        else:
            if (mid + 1) ** 2 > x:
                return mid
            else:
                left = mid


# -*- coding: utf-8 -*-

"""
Date                :  01/08/2017
Author              :  Adrián Homero Moreno García
Objective of Script :  Create functions for produce different type of structuring elements,
                       using numping arrays of pacakage numpy, for a better manipulation for operations
"""

import numpy as np
import cv2

class StructuringElements:

    def create_structuring_element(self, structuring_name, params):
        """
        This function is used to selected a type of structuring element and return it.
        :param structuring_name: Structuring elmenent name selected.
        :param params: Size of Structuring element selected.
        :return: It returns the structural element selected.
        """
        if(structuring_name == 'diamond'):
            return StructuringElements().create_diamond_mask(**params)
        elif(structuring_name == 'cross'):
            return StructuringElements().create_cross_shaped_mask(**params)
        elif(structuring_name == 'elliptical'):
            return StructuringElements().create_elipsed_mask(**params)

    def create_diamond_mask(self, radius):
        """
        This method will create a Diamond Structure element, the will be used
        for the morphological operations of the image
        :param radius: Specifies the distance from the structuring element origin to the point of the diamond
        :return: It returns the data of the  mask to be used
        """
        _radius_mask_ = radius + 1

        mask_values = []
        array_values = []

        for x in list(range(_radius_mask_)) + list(reversed(range(_radius_mask_ - 1))) + list(reversed(range(0))):
            mask_values.append('{:0<{w1}}{:1>{w2}}{:0<{w3}}'
                               .format('', '', '', w1=_radius_mask_ - x - 1, w2=x * 2 + 1, w3=_radius_mask_ - x - 1))

        for x in mask_values:
            for value in x:
                array_values.append(int(value))

        numpy_values = np.array(array_values)

        numpy_diamond = np.reshape(numpy_values, (-1, (radius*2)+1))

        return numpy_diamond

    def create_cross_shaped_mask(self, width, height):
        """
        Function for create a cross shaped structuring element.
        :param width: Width of the cross shaped structuring element.
        :param height: Height of the cross shaped structuring element.
        :return: It returns the cross shaped mask.
        """
        cross_mask = cv2.getStructuringElement(cv2.MORPH_CROSS, (width, height))

        return cross_mask

    def create_elipsed_mask(self, width, height):
        """
        Function for create a elipsed structuring element.
        :param width: Width of the elipsed structuring element.
        :param height: Height of the elipsed structuring element.
        :return: It returns the elipsed mask.
        """
        cross_mask = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (width, height))

        return cross_mask


#-*- coding: UTF-8 -*-
import time
from utilities import *
from culrcscrapers.azlyrics import lyricsScraper as lyricsScraper_azlyrics
from culrcscrapers.darklyrics import lyricsScraper as lyricsScraper_darklyrics
from culrcscrapers.genius import lyricsScraper as lyricsScraper_genius
from culrcscrapers.gomaudio import lyricsScraper as lyricsScraper_gomaudio
from culrcscrapers.lyricscom import lyricsScraper as lyricsScraper_lyricscom
from culrcscrapers.lyricsmode import lyricsScraper as lyricsScraper_lyricsmode
from culrcscrapers.minilyrics import lyricsScraper as lyricsScraper_minilyrics
from culrcscrapers.music163 import lyricsScraper as lyricsScraper_music163
from culrcscrapers.syair import lyricsScraper as lyricsScraper_syair

FAILED = []

def test_scrapers():
    dialog = xbmcgui.DialogProgress()
    TIMINGS = []

    # test alsong
    dialog.create(ADDONNAME, LANGUAGE(32163) % 'azlyrics')
    log('==================== azlyrics ====================')
    song = Song('La Dispute', 'Such Small Hands')
    st = time.time()
    lyrics = lyricsScraper_azlyrics.LyricsFetcher().get_lyrics(song)
    ft = time.time()
    tt = ft - st
    TIMINGS.append(['azlyrics',tt])
    if lyrics:
        log(lyrics.lyrics)
    else:
        FAILED.append('azlyrics')
        log('FAILED: azlyrics')
    if dialog.iscanceled():
        return

    # test darklyrics
    dialog.update(11, LANGUAGE(32163) % 'darklyrics')
    log('==================== darklyrics ====================')
    song = Song('Neurosis', 'Lost')
    st = time.time()
    lyrics = lyricsScraper_darklyrics.LyricsFetcher().get_lyrics(song)
    ft = time.time()
    tt = ft - st
    TIMINGS.append(['darklyrics',tt])
    if lyrics:
        log(lyrics.lyrics)
    else:
        FAILED.append('darklyrics')
        log('FAILED: darklyrics')
    if dialog.iscanceled():
        return

    # test genius
    dialog.update(22, LANGUAGE(32163) % 'genius')
    log('==================== genius ====================')
    song = Song('Maren Morris', 'My Church')
    st = time.time()
    lyrics = lyricsScraper_genius.LyricsFetcher().get_lyrics(song)
    ft = time.time()
    tt = ft - st
    TIMINGS.append(['genius',tt])
    if lyrics:
        log(lyrics.lyrics)
    else:
        FAILED.append('genius')
        log('FAILED: genius')
    if dialog.iscanceled():
        return

    # test gomaudio
    dialog.update(33, LANGUAGE(32163) % 'gomaudio')
    log('==================== gomaudio ====================')
    song = Song('Lady Gaga', 'Just Dance')
    st = time.time()
    lyrics = lyricsScraper_gomaudio.LyricsFetcher().get_lyrics(song, 'd106534632cb43306423acb351f8e6e9', '.mp3')
    ft = time.time()
    tt = ft - st
    TIMINGS.append(['gomaudio',tt])
    if lyrics:
        log(lyrics.lyrics)
    else:
        FAILED.append('gomaudio')
        log('FAILED: gomaudio')
    if dialog.iscanceled():
        return

    # test lyricscom
    dialog.update(44, LANGUAGE(32163) % 'lyricscom')
    log('==================== lyricscom ====================')
    song = Song('Blur', 'You\'re So Great')
    st = time.time()
    lyrics = lyricsScraper_lyricscom.LyricsFetcher().get_lyrics(song)
    ft = time.time()
    tt = ft - st
    TIMINGS.append(['lyricscom',tt])
    if lyrics:
        log(lyrics.lyrics)
    else:
        FAILED.append('lyricscom')
        log('FAILED: lyricscom')
    if dialog.iscanceled():
        return

    # test lyricsmode
    dialog.update(55, LANGUAGE(32163) % 'lyricsmode')
    log('==================== lyricsmode ====================')
    song = Song('Maren Morris', 'My Church')
    st = time.time()
    lyrics = lyricsScraper_lyricsmode.LyricsFetcher().get_lyrics(song)
    ft = time.time()
    tt = ft - st
    TIMINGS.append(['lyricsmode',tt])
    if lyrics:
        log(lyrics.lyrics)
    else:
        FAILED.append('lyricsmode')
        log('FAILED: lyricsmode')
    if dialog.iscanceled():
        return

    # test minilyrics
    dialog.update(66, LANGUAGE(32163) % 'minilyrics')
    log('==================== minilyrics ====================')
    song = Song('Chicago', 'Stay The Night')
    st = time.time()
    lyrics = lyricsScraper_minilyrics.LyricsFetcher().get_lyrics(song)
    ft = time.time()
    tt = ft - st
    TIMINGS.append(['minilyrics',tt])
    if lyrics:
        log(lyrics.lyrics)
    else:
        FAILED.append('minilyrics')
        log('FAILED: minilyrics')
    if dialog.iscanceled():
        return

    # test music163
    dialog.update(77, LANGUAGE(32163) % 'music163')
    log('==================== music163 ====================')
    song = Song('Chicago', 'Stay The Night')
    st = time.time()
    lyrics = lyricsScraper_music163.LyricsFetcher().get_lyrics(song)
    ft = time.time()
    tt = ft - st
    TIMINGS.append(['music163',tt])
    if lyrics:
        log(lyrics.lyrics)
    else:
        FAILED.append('music163')
        log('FAILED: music163')
    if dialog.iscanceled():
        return

    # test syair
    dialog.update(88, LANGUAGE(32163) % 'syair')
    log('==================== syair ====================')
    song = Song('Madonna', 'Crazy For You')
    st = time.time()
    lyrics = lyricsScraper_syair.LyricsFetcher().get_lyrics(song)
    ft = time.time()
    tt = ft - st
    TIMINGS.append(['syair',tt])
    if lyrics:
        log(lyrics.lyrics)
    else:
        FAILED.append('syair')
        log('FAILED: syair')
    if dialog.iscanceled():
        return

    dialog.close()
    log('=======================================')
    log('FAILED: %s' % str(FAILED))
    log('=======================================')
    for item in TIMINGS:
        log('%s - %i' % (item[0], item[1]))
    log('=======================================')
    if FAILED:
        dialog = xbmcgui.Dialog().ok(ADDONNAME, LANGUAGE(32165) % ' / '.join(FAILED))
    else:
        dialog = xbmcgui.Dialog().ok(ADDONNAME, LANGUAGE(32164))


from django.contrib.auth.models import User
from django.shortcuts import render
from firebase_admin import auth
from rest_framework import mixins, generics
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework import serializers
from firebase_admin import auth
from firebase_admin import credentials
import firebase_admin
from django.contrib.auth import authenticate

cred = credentials.Certificate(
    "fir-ae42b-firebase-adminsdk-2yapl-6fb1688ad1.json")
default_app = firebase_admin.initialize_app(cred)

class SocialLoginSerializer(serializers.Serializer):
    model = User
    fields = []

# Create your views here.

def Firebase_validation(id_token):
    """
   This function receives id token sent by Firebase and
   validate the id token then check if the user exist on
   Firebase or not if exist it returns True else False
   """
    try:
        decoded_token = auth.verify_id_token(id_token)
        # print(decoded_token, "Decoded Token ...................")
        uid = decoded_token['uid']
        # print(uid, "UID......................................")
        provider = decoded_token['firebase']['sign_in_provider']
        identities = decoded_token['firebase']['identities']
        values = list(identities.values())
        identity_id = values[0][0]
        full_name = decoded_token.get("name")

        # print(provider, "Provider..........")
        image = None
        name = None
        if "name" in decoded_token:
            name = decoded_token['name']
            # print(name, "Namee Inside If...................")

        if "picture" in decoded_token:
            image = decoded_token['picture']
            # print(image, "Imageee Inside If...................")
        try:
            user = auth.get_user(uid)
            email = user.email
            if user:
                return {
                    "status": True,
                    "uid": uid,
                    "email": email,
                    "name": name,
                    "provider": provider,
                    "image": image
                }
            else:
                return False
        except auth.UserNotFoundError:
            print("user not exist")
    except auth.ExpiredIdTokenError:
        print("invalid token")

class SocialSignupAPIView(mixins.CreateModelMixin, generics.GenericAPIView):
    """
   api for creating user from social logins
   """
    permission_classes = (AllowAny,)
    serializer_class = SocialLoginSerializer

    def post(self, request):
        auth_header = request.META.get('HTTP_AUTHORIZATION')
        if auth_header:
            id_token = auth_header.split(' ').pop()
            print(id_token, "ID token...........")
            validate = Firebase_validation(id_token)

            if validate:
                user = User.objects.filter(username=validate['uid']).first()

                # user = authenticate(username=validate['uid'])
                print(user, "User...............................................")
                # if full_name:
                #     full_name_split = full_name.split(" ")
                #     profile.first_name = full_name_split[0]
                #     profile.last_name = full_name_split[1] if len(full_name_split) > 1 else full_name_split[0]
                #     setattr(profile, "fullname", full_name)

                if user:
                    data = {
                        'id': user.id,
                        'email': user.email,
                        # 'name': user.name,
                        # 'image': user.image,
                        'type': 'existing_user',
                        'provider': validate['provider'],
                    }
                    return Response({'data': data,
                                     'message': 'Login Successful'})
                else:
                    user = User(email=validate['email'],
                                # name=validate['name'],
                                # uid=validate['uid'],
                                # image=validate['image']
                                )
                    user.save()
                    data = {
                        'id': user.id,
                        'email': user.email,
                        # 'name': user.name,
                        # 'image': user.image,
                        'type': 'new_user',
                        'provider': validate['provider'],
                    }
                return Response({'data': data,
                                 'message': 'User Created Successfully'})
            else:
                return Response({'message': 'invalid token'})
        else:
            return Response({'message': 'token not provided'})


#!/usr/bin/env python

import os, logging, re
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
from common.entities import pcapFile, WarningAlert
from canari.maltego.message import Label
from canari.framework import configure #, superuser

__author__ = 'catalyst256'
__copyright__ = 'Copyright 2013, Sniffmypackets Project'
__credits__ = []

__license__ = 'GPL'
__version__ = '0.1'
__maintainer__ = 'catalyst256'
__email__ = 'catalyst256@gmail.com'
__status__ = 'Development'

__all__ = [
    'dotransform'
]

#@superuser
@configure(
    label='L4 - Find Null Share Connections [SmP]',
    description='Looks for attackers mapping null share',
    uuids=[ 'sniffMyPackets.v2.pcap2null_shares' ],
    inputs=[ ( 'sniffMyPackets', pcapFile ) ],
    debug=True
)
def dotransform(request, response):

    pkts = rdpcap(request.value)
    ips = []
    success = 'SMBu\\x00\\x00\\x00\\x00'
    null_share = 'IPC$'

    for p in pkts:
        if p.haslayer(TCP) and p.getlayer(TCP).dport == 445 and p.haslayer(Raw):
            raw = p.getlayer(Raw).load
            srcip = p.getlayer(IP).src
            dstip = p.getlayer(IP).dst
            if success and null_share in raw:
                convo = srcip, dstip
                if convo not in ips:
                    ips.append(convo)

    for src, dst in ips:
        e = WarningAlert('Null Share:\n' + str(src) + '->' + str(dst))
        e.linklabel = str(null_share)
        e.linkcolor = 0xFF0000
        response += e
    return response


def gcd(x,y):
    quotient = 0
    remainder = 0
    if y == 0:
        return x
    else:
        quotient = x//y
        remainder = x%y
        # divideList.append([x,y,quotient,remainder])
        print("{divide} = {divisor} * {quot} + {remain}".format(divide=x, divisor=y, quot=quotient, remain=remainder))
        return gcd(y, remainder)

num1 = int(input("Enter the larger number: "))
num2 = int(input("Enter the smaller number: "))
print("The GCD is: {}".format(gcd(num1, num2)))


#!/usr/bin/env python3

import matplotlib
matplotlib.use('Agg')

from matplotlib import pyplot as plt

import argparse
import csv
import itertools
import numpy as np
import os
import seaborn as sns
import sys

import base

def main(inputs, output, colors):

    def convert(b):
        return float(b.replace(',', '.'))

    def read(fname, skiprows=0):
        with open(fname, 'r') as f:
            for line in csv.reader(f, delimiter=',', quotechar='"'):
                if skiprows == 0:
                    yield [convert(x) for x in line]
                else:
                    skiprows -= 1

    c, bi = base.init()
    _, ax = base.create_figure(6, 3)

    for fname, c in itertools.zip_longest(inputs, colors):
        x = np.array(list(read(fname, skiprows=2)))
        Y = x[:, 1]
        x = x[:, 0]

        plt.plot(x, Y, color=c)

    plt.xlabel("Frequency (kHz)")
    plt.xlim(0, 300)
    plt.ylabel("Power (dBm)")

    base.finalize(None, bi)
    plt.tight_layout()

    base.save_figure(output)
    return 0

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("data", metavar="TSV", action="store", type=str, nargs="+",
                        help="the data to plot.")
    parser.add_argument("output", metavar="OUT", action="store", type=str,
                        help="the file to plot to. This also specifies the output type.")
    parser.add_argument("--colors", action="store", nargs="+", default=[],
                        help="the colors to be used for the sample rate plots.")

    args = parser.parse_args()
    sys.exit(main(args.data, args.output, args.colors))


import sys

try:
    filename = sys.argv[-1]
    ar = []
    for arg in sys.argv[1:-1]:
        ar.append(arg)

    with open(filename, 'rt') as file:
        data = file.read().split('\n')
        if '--sort' in ar:
            data = sorted(data)

        for i, el in enumerate(data):
            if '--num' in ar:
                print(i, el)
            else:
                print(el)

        if '--count' in ar:
            print(f'rows count: {len(data)}')

except Exception:
    print('ERROR')


__author__ = 'zhizhong pan'

import unittest
from math import factorial

def combinations(m, n):
    return factorial(m) / (factorial(n) * factorial(m - n))

def check_choose(m, n):
    max_x = n // 2
    min_x = 0

    while min_x <= max_x:
        mid_x = min_x + (max_x - min_x) // 2
        chose = combinations(n, mid_x)
        if chose < m:
            min_x = mid_x + 1
        elif chose > m:
            max_x = mid_x - 1
        else:
            return mid_x

    return -1

class TestCheckChooseMethods(unittest.TestCase):
    def test_combinations(self):
        self.assertEqual(combinations(3, 1), 3)
        self.assertEqual(combinations(5, 2), 10)

    def test_seven(self):

        self.assertEqual(check_choose(47129212243960, 50), 20)

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


import requests
import sys
from multiprocessing import Pool

# for pokemon in dict_response
# all_url = "https://pokeapi.co/api/v2/pokemon/"
all_url = "https://pokeapi.co/api/v2/pokemon-species/"
wanted_games = ["red", "blue", "leafgreen", "white"]

pokemon_by_version = {version: [] for version in wanted_games}

# limit the calls, use pagination
offset = 0
limit = 10

pokemon_count = 0

while pokemon_count < 10:
    dict_response = requests.get(f"{all_url}?{offset}&{limit}").json()
    get_pokemon = dict_response["results"]

    if len(get_pokemon) == 0:
        break

    for pokemon in get_pokemon:
        individual_pokemon = pokemon["url"]

        get_url = requests.get(individual_pokemon).json()

        if len(get_url["flavor_text_entries"]) > 0:
            for game_version in get_url["flavor_text_entries"]:
                version_name = game_version["version"]["name"]

                if version_name in wanted_games and game_version["flavor_text"]:
                    pokemon_by_version[version_name].append(pokemon["name"])

                break

        pokemon_count += 1

    offset += limit

pokemon_red = set(pokemon_by_version["red"])
pokemon_blue = set(pokemon_by_version["blue"])
pokemon_leafgreen = set(pokemon_by_version["leafgreen"])
pokemon_white = set(pokemon_by_version["white"])

filtered_pokemon = sorted(
    list(pokemon_red.union(pokemon_blue, pokemon_leafgreen, pokemon_white))
)
# print(filtered_pokemon)
# print(type(filtered_pokemon))

# make a call with the names only from the deduplicated set

pokemon_url = "https://pokeapi.co/api/v2/pokemon"

for pokemon in filtered_pokemon:
    pokemon_data = requests.get(f"{pokemon_url}/{pokemon}").json()

    wanted_slots = [1, 2]  # list
    available_slots = [type_dict["slot"] for type_dict in pokemon_data["types"]]
    # attack_types = [d["type"] for d in pokemon_data]

    # check =  all(item in available_slots  for item in wanted_slots)
    slots = []
    if any(slot in available_slots for slot in wanted_slots):
        for type_dict in pokemon_data["types"]:
            slot = type_dict.get("slot")
            attack_name = type_dict.get("type").get("name")
            slots.append({slot: attack_name})

    default_sprites = pokemon_data["sprites"]["front_default"]

    stats = {
        "name": pokemon_data["name"].capitalize(),
        "id": pokemon_data["id"],
        "base_experience": pokemon_data["base_experience"],
        "weight": pokemon_data["weight"],
        "height": pokemon_data["height"],
        "order": pokemon_data["order"],
        "slot 1": slots[0].get(1),
        "slot 1": slots[0][1],
        "slot 2": slots[0].get("2"),
        "bmi": round(pokemon_data["weight"] / (pokemon_data["height"] ** 2), 4),
        "front_default": pokemon_data["sprites"]["front_default"],
    }

    print(stats)


#Conditional Statements with Advanced Data Types
#Lists and membership operators can be used in conditional statements.

#Usage
my_list = ["2", 3, 5, 4.3]
if 3 in my_list:
    print(my_list)

z = 10
x = 2
y = 5

if x > y and z > y: ##False and true ---> False
    print("hello")
elif x > y or z > y: ##False OR true ---> True
    print("Hi")


from channels.generic.websocket import AsyncWebsocketConsumer
from channels.db import database_sync_to_async
from asgiref.sync import sync_to_async
from django.contrib.auth.models import User
from channels.middleware import BaseMiddleware
from django.db import close_old_connections
from rest_framework.authtoken.models import Token
import json
from .models import CustomUser, Table, Order, DishInOrder, Dish
from rest_framework import status
from rest_framework.response import Response
from .serializers import CustomUserSerializer, DishSerializer, DishInOrderSerializer, TableSerializer
from django.core import serializers

class BaseConsumer(AsyncWebsocketConsumer):

    user = None
    custom_user = None

    @database_sync_to_async
    def get_user_from_token(self, token):
        try:
            return Token.objects.get(key=token).user
        except Token.DoesNotExist:
            return None

    @database_sync_to_async
    def get_custom_user_from_user(self, user):
        try:
            return CustomUser.objects.get(user=user.id)
        except CustomUser.DoesNotExist:
            return None

    @database_sync_to_async
    def save_user_name(self, scope, username):
        try:
            self.user.username = username
            self.user.save()
        except:
            return None

    @database_sync_to_async
    def get_token_new_role(self, role):
        user = User.objects.create()
        user.username = user.id
        user.save()
        custom_user = CustomUser.objects.create(user=user, role=role)
        token = Token.objects.create(user=user)
        token_key = token.key
        return token.key

    @database_sync_to_async
    def perform_data_for_table(self, table_id, table_status):
        table = Table.objects.get(id=table_id)
        table.status = table_status
        if table_status == 1:
            table.waiter = None
            table.order = None
        table.save()

    @database_sync_to_async
    def perform_i_serve(self, table_id):
        table = Table.objects.get(id=table_id)
        table.waiter = self.user
        table.save()

    @database_sync_to_async
    def perform_data_for_order(self, table_id, orders_from_request):
        table = Table.objects.get(id=table_id)
        order = Order.objects.create()
        table.order = order
        table.save()
        for dish_id in orders_from_request:
            DishInOrder.objects.create(
                to_order=order, dish=Dish.objects.get(id=dish_id))

    @database_sync_to_async
    def get_order_info(self, order_id):
        data = DishInOrder.objects.filter(to_order=order_id)
        serializer = DishInOrderSerializer(data, many=True)
        json_data = serializer.data
        return json_data

    @sync_to_async
    def async_print(self, message):
        print(message)

    @sync_to_async
    def form_menu_data(self):
        data = Dish.objects.all()
        serializer = DishSerializer(data, many=True)
        json_data = serializer.data
        return json_data

    @sync_to_async
    def form_tables_data(self):
        data = Table.objects.all()
        serializer = TableSerializer(data, many=True)
        json_data = serializer.data
        return json_data

    @sync_to_async
    def get_users_data(self):
        data = CustomUser.objects.all()
        serializer = CustomUserSerializer(data, many=True)
        json_data = serializer.data
        return json_data

    async def connect(self):
        token = self.scope['query_string'].decode('utf8').split('=')[1]
        self.user = await self.get_user_from_token(token)
        self.custom_user = await self.get_custom_user_from_user(self.user)
        if self.user:
            await self.accept()
            await self.send(text_data=json.dumps({"action": "GET_ROLE_RESPONSE", "data": self.custom_user.role}))
        else:
            await self.close()

    async def disconnect(self, close_code):
        pass

    async def receive(self, text_data):
        print("receive performed")
        data = json.loads(text_data)
        action = data.get('action')

        # создать нового официанта/повара
        """
            {
                "action": "NEW_ROLE",
                "data": 2
            }
        """
        role = data.get('data')
        if action == 'NEW_ROLE' and role in (1, 2):
            token = await self.get_token_new_role(role)
            await self.send(text_data=json.dumps({"action": "NEW_ROLE_RESPONSE", "data": token}, ensure_ascii=False))

        # войти новым пользователем и установить имя
        """
            {
                "action": "ENTER_NAME",
                "data": "Альберт"
            }
        """
        if action == 'ENTER_NAME':
            username = data.get('data')
            await self.save_user_name(self, username)
            await self.send(text_data=json.dumps({'action': 'ENTER_NAME_RESPONSE', 'data': 'Данные были обновлены.'}, ensure_ascii=False))

        # управлять столиком, если ты официант
        """
            {
                "action": "MANAGE_TABLE",
                "data": {
                    "table_id": 4,
                    "table_status": 3,
                }
            }
        """
        if action == "MANAGE_TABLE":
            data = data.get("data")
            table_id = data["table_id"]
            table_status = data["table_status"]
            await self.perform_data_for_table(table_id, table_status)

        # взять на себя обслуживание столика
        """
        {
            "action": "I_SERVE",
            "data": 4
        }
        """
        if action == "I_SERVE":
            await self.perform_i_serve(data.get("data"))
            await self.send(text_data=json.dumps({"action": "I_SERVE_RESPONSE", "data": "Официант взял столик."}, ensure_ascii=False))

        # просмотреть все меню
        """
        {
            "action": "SHOW_MENU"
        }
        """
        if action == "SHOW_MENU":
            temp_data = await self.form_menu_data()
            await self.send(text_data=json.dumps({"action": "SHOW_MENU_RESPONSE", "data": temp_data}, ensure_ascii=False))

        # просмотреть все столики
        """
        {
            "action": "SHOW_TABLES"
        }
        """
        if action == "SHOW_TABLES":
            temp_data = await self.form_tables_data()
            await self.send(text_data=json.dumps({"action": "SHOW_TABLES_RESPONSE", "data": temp_data}, ensure_ascii=False))

        # сделать заказ
        """
        {
            "action": "MAKE_ORDER",
            "data": {
                "table_id": 4,
                "order": [
                    1,
                    1,
                    2,
                    3
                ]
            }
        }
        """
        if action == "MAKE_ORDER":
            table_id = data.get("data")["table_id"]
            orders_from_request = data.get("data")["order"]
            await self.perform_data_for_order(table_id, orders_from_request)
            await self.send(text_data=json.dumps({"action": "MAKE_ORDER_RESPONSE", "data": "Заказ создан."}, ensure_ascii=False))

        # получить список всех пользователей, если ты администратор
        """
        {
            "action": "GET_USERS"
        }
        """
        if action == "GET_USERS":
            temp_data = await self.get_users_data()
            await self.send(text_data=json.dumps({"action": "GET_USERS_RESPONSE", "data": temp_data}, ensure_ascii=False))

        # получить данные о готовке и блюдах в заказе
        """
        {
            "action": "ORDER_INFO",
            "data": 4
        }
        """
        if action == "ORDER_INFO":
            order_id = data.get("data")
            temp_data = await self.get_order_info(order_id)
            await self.send(text_data=json.dumps({"action": "ORDER_INFO_RESPONSE", "data": temp_data}, ensure_ascii=False))

        # изменить статус готовки блюда
        """
        {
            "action": "UPDATE_ORDER",
            "data": {
                "table_id": 4,
                "order": [
                    {
                        "dish_in_order_id": 7
                        "new_status": 2
                    },
                    {
                        "dish_in_order_id": 8
                        "new_status": 3
                    },
                    {
                        "dish_in_order_id": 9
                        "new_status": 3
                    },
                    {
                        "dish_in_order_id": 10
                        "new_status": 4
                    }
                ]
            }
        }
        """
        if action == "UPDATE_ORDER":
            temp_data = data.get("data")
            table_id = temp_data["table_id"]
            order_from_request = temp_data["order"]


from __future__ import print_function
import sys
import os
import math
import isceobj
from isceobj.Location.Offset import OffsetField,Offset
from iscesys.Component.Component import Component,Port
from iscesys.Compatibility import Compatibility
Compatibility.checkPythonVersion()
from iscesys.StdOEL.StdOELPy import create_writer
from .Ampcor import Ampcor
from isceobj.Util.mathModule import is_power2
import logging

logger = logging.getLogger('mroipac.ampcor.nstage')

NUMBER_STAGES = Component.Parameter('nStages',
        public_name='NUMBER_STAGES',
        default=4,
        type=int,
        mandatory=False,
        doc = 'Number of stages for multi-scale offset estimator.')

SCALE = Component.Parameter('scale',
        public_name='SCALE',
        default=2,
        type=int,
        mandatory=False,
        doc = 'Scale factor in between each stage of ampcor.')

COARSE_NUMBER_WINDOWS_ACROSS = Component.Parameter('coarseNumWinAcross',
        public_name='COARSE_NUMBER_WINDOWS_ACROSS',
        default=10,
        type = int,
        mandatory = False,
        doc = 'Number of windows in range for coarse scales.')

COARSE_NUMBER_WINDOWS_DOWN = Component.Parameter('coarseNumWinDown',
        public_name='COARSE_NUMBER_WINDOWS_DOWN',
        default=10,
        type=int,
        mandatory=False,
        doc = 'Number of windows in azimuth for coarse scales.')

COARSE_OVERSAMPLING_FACTOR = Component.Parameter('coarseOversamplingFactor',
        public_name='COARSE_OVERSAMPLING_FACTOR',
        default=4,
        type=int,
        mandatory=False,
        doc = 'Oversampling factor for coarse scales.')

COARSE_SNR_THRESHOLD = Component.Parameter('coarseSNRThreshold',
        public_name='COARSE_SNR_THRESHOLD',
        default = 2.0,
        type = float,
        mandatory=False,
        doc = 'SNR threshold for culling at coarser scales.')

COARSE_DISTANCE_THRESHOLD = Component.Parameter('coarseDistance',
        public_name='COARSE_DISTANCE_THRESHOLD',
        default = 10.0,
        type=float,
        mandatory=False,
        doc = 'SNR threshold for culling at coarser scales.')

WINDOW_SIZE_WIDTH = Component.Parameter('windowSizeWidth',
        public_name='WINDOW_SIZE_WIDTH',
        default = 64,
        type = int,
        mandatory = False,
        doc = 'Width of the reference data window to be used for correlation')

WINDOW_SIZE_HEIGHT = Component.Parameter('windowSizeHeight',
        public_name='WINDOW_SIZE_HEIGHT',
        default = 64,
        type = int,
        mandatory = False,
        doc = 'Height of the reference data window to be used for correlation')

SEARCH_WINDOW_SIZE_WIDTH = Component.Parameter('searchWindowSizeWidth',
        public_name='SEARCH_WINDOW_SIZE_WIDTH',
        default = 100,
        type = int,
        mandatory = False,
        doc = 'Width of the search data window to be used for correlation')

SEARCH_WINDOW_SIZE_HEIGHT = Component.Parameter('searchWindowSizeHeight',
        public_name='SEARCH_WINDOW_SIZE_HEIGHT',
        default = 100,
        type = int,
        mandatory = False,
        doc = 'Height of the search data window to be used for correlation')

COARSE_ZOOM_WINDOW_SIZE = Component.Parameter('coarseZoomWindowSize',
        public_name='COARSE_ZOOM_WINDOW_SIZE',
        default = 32,
        type=int,
        mandatory=False,
        doc = 'Zoom window around local maxima at coarse scales.')

ZOOM_WINDOW_SIZE = Component.Parameter('zoomWindowSize',
        public_name = 'ZOOM_WINDOW_SIZE',
        default = 16,
        type = int,
        mandatory = False,
        doc = 'Zoom window around the local maximum for first pass')

OVERSAMPLING_FACTOR = Component.Parameter('oversamplingFactor',
        public_name = 'OVERSAMPLING_FACTOR',
        default = 16,
        type = int,
        mandatory = False,
        doc = 'Oversampling factor for the FFTs to get sub-pixel shift.')

ACROSS_GROSS_OFFSET = Component.Parameter('acrossGrossOffset',
        public_name = 'ACROSS_GROSS_OFFSET',
        default = None,
        type = int,
        mandatory = False,
        doc = 'Gross offset in the range direction.')

DOWN_GROSS_OFFSET = Component.Parameter('downGrossOffset',
        public_name = 'DOWN_GROSS_OFFSET',
        default = None,
        type = int,
        mandatory = False,
        doc = 'Gross offset in the azimuth direction.')

ACROSS_LOOKS = Component.Parameter('acrossLooks',
        public_name = 'ACROSS_LOOKS',
        default = 1,
        type = int,
        mandatory = False,
        doc = 'Number of looks to take in range before correlation')

DOWN_LOOKS = Component.Parameter('downLooks',
        public_name = 'DOWN_LOOKS',
        default = 1,
        type = int,
        mandatory = False,
        doc = 'Number of looks to take in azimuth before correlation')

NUMBER_WINDOWS_ACROSS = Component.Parameter('numberLocationAcross',
        public_name = 'NUMBER_WINDOWS_ACROSS',
        default = 40,
        type = int,
        mandatory = False,
        doc = 'Number of windows in range direction')

NUMBER_WINDOWS_DOWN = Component.Parameter('numberLocationDown',
        public_name = 'NUMBER_WINDOWS_DOWN',
        default = 40,
        type = int,
        mandatory = False,
        doc = 'Number of windows in azimuth direction')

DOWN_SPACING_PRF1 = Component.Parameter('prf1',
        public_name = 'DOWN_SPACING_PRF1',
        default = 1.0,
        type = float,
        mandatory = False,
        doc = 'PRF or a similar scale factor for azimuth spacing of reference image.')

DOWN_SPACING_PRF2 = Component.Parameter('prf2',
        public_name = 'DOWN_SPACING_PRF2',
        default = 1.0,
        type = float,
        mandatory = False,
        doc = 'PRF or a similar scale factor for azimuth spacing of search image.')

ACROSS_SPACING1 = Component.Parameter('rangeSpacing1',
        public_name = 'ACROSS_SPACING1',
        default = 1.0,
        type = float,
        mandatory = False,
        doc = 'Range pixel spacing or similar scale factor for reference image.')

ACROSS_SPACING2 = Component.Parameter('rangeSpacing2',
        public_name = 'ACROSS_SPACING2',
        default = 1.0,
        type = float,
        mandatory = False,
        doc = 'Range pixel spacing or similar scale for search image.')

IMAGE_DATATYPE1 = Component.Parameter('imageDataType1',
        public_name = 'IMAGE_DATATYPE1',
        default='',
        type = str,
        mandatory = False,
        doc = 'Image data type for reference image (complex / real)')

IMAGE_DATATYPE2 = Component.Parameter('imageDataType2',
        default='',
        type = str,
        mandatory=False,
        doc = 'Image data type for search image (complex / real)')

SNR_THRESHOLD = Component.Parameter('thresholdSNR',
        public_name = 'SNR_THRESHOLD',
        default = 0.001,
        type = float,
        mandatory=False,
        doc = 'SNR threshold for valid matches.')

COV_THRESHOLD = Component.Parameter('thresholdCov',
        public_name = 'COV_THRESHOLD',
        default = 1000.0,
        type = float,
        mandatory=False,
        doc = 'Covariance threshold for valid matches.')

BAND1 = Component.Parameter('band1',
        public_name='BAND1',
        default=0,
        type = int,
        mandatory = False,
        doc = 'Band number of image1')

BAND2 = Component.Parameter('band2',
        public_name='BAND2',
        default=0,
        type=int,
        mandatory=False,
        doc = 'Band number of image2')

class NStage(Component):

    family = 'nstage'
    logging_name = 'isce.mroipac.nstage'

    parameter_list = (NUMBER_STAGES,
                      SCALE,
                      COARSE_NUMBER_WINDOWS_ACROSS,
                      COARSE_NUMBER_WINDOWS_DOWN,
                      COARSE_OVERSAMPLING_FACTOR,
                      COARSE_SNR_THRESHOLD,
                      COARSE_DISTANCE_THRESHOLD,
                      COARSE_ZOOM_WINDOW_SIZE,
                      WINDOW_SIZE_WIDTH,
                      WINDOW_SIZE_HEIGHT,
                      SEARCH_WINDOW_SIZE_WIDTH,
                      SEARCH_WINDOW_SIZE_HEIGHT,
                      ZOOM_WINDOW_SIZE,
                      OVERSAMPLING_FACTOR,
                      ACROSS_GROSS_OFFSET,
                      DOWN_GROSS_OFFSET,
                      ACROSS_LOOKS,
                      DOWN_LOOKS,
                      NUMBER_WINDOWS_ACROSS,
                      NUMBER_WINDOWS_DOWN,
                      DOWN_SPACING_PRF1,
                      DOWN_SPACING_PRF2,
                      ACROSS_SPACING1,
                      ACROSS_SPACING2,
                      IMAGE_DATATYPE1,
                      IMAGE_DATATYPE2,
                      SNR_THRESHOLD,
                      COV_THRESHOLD,
                      BAND1,
                      BAND2)

    def nstage(self,slcImage1 = None,slcImage2 = None):
        if not (slcImage1 == None):
            self.slcImage1 = slcImage1
        if (self.slcImage1 == None):
            logger.error("Error. reference slc image not set.")
            raise Exception
        if not (slcImage2 == None):
            self.slcImage2 = slcImage2
        if (self.slcImage2 == None):
            logger.error("Error. secondary slc image not set.")
            raise Exception

        self.fileLength1 = self.slcImage1.getLength()
        self.lineLength1 = self.slcImage1.getWidth()
        self.fileLength2 = self.slcImage2.getLength()
        self.lineLength2 = self.slcImage2.getWidth()

        ####Run checks
        self.checkTypes()
        self.checkWindows()

        ####Actual processing
        mSlc = self.slcImage1
        sSlc = self.slcImage2
        coarseAcross = self.acrossGrossOffset
        coarseDown = self.downGrossOffset
        nStageName = self.name
        logger.info('NSTAGE NAME = %s'%(self.name))
        for iterNum in range(self.nStages-1, -1, -1):
            ####Rewind the images
            try:
                mSlc.rewind()
                sSlc.rewind()
            except:
                logger.error('Issues when rewinding images.')
                raise Exception

            objOff = None
            objAmpcor = None

            logger.debug('Starting Iteration Stage: %d'%(iterNum))
            logger.debug('Gross Across: %s'%(coarseAcross))
            logger.debug('Gross Down  : %s'%(coarseDown))

            objAmpcor = Ampcor(name='%s_%d'%(nStageName,iterNum))
            objAmpcor.configure()
            objAmpcor.setImageDataType1(self.imageDataType1)
            objAmpcor.setImageDataType2(self.imageDataType2)
            objAmpcor.setFirstPRF(self.prf1)
            objAmpcor.setSecondPRF(self.prf2)
            objAmpcor.setFirstRangeSpacing(self.rangeSpacing1)
            objAmpcor.setSecondRangeSpacing(self.rangeSpacing2)

            ######Scale all the reference and search windows
            scaleFactor = self.scale**iterNum
            logger.debug('Scale Factor: %d'%(int(scaleFactor)))
            objAmpcor.windowSizeWidth = scaleFactor*self.windowSizeWidth
            objAmpcor.windowSizeHeight = scaleFactor*self.windowSizeHeight
            objAmpcor.searchWindowSizeWidth = scaleFactor*self.searchWindowSizeWidth
            objAmpcor.searchWindowSizeHeight = scaleFactor*self.searchWindowSizeHeight

            xMargin = 2*objAmpcor.searchWindowSizeWidth + objAmpcor.windowSizeWidth
            yMargin = 2*objAmpcor.searchWindowSizeHeight + objAmpcor.windowSizeHeight

            #####Set image limits for search
            offAc = max(objAmpcor.margin,-coarseAcross)+xMargin
            offDn = max(objAmpcor.margin,-coarseDown)+yMargin

            offAcmax = int(coarseAcross + ((self.rangeSpacing1/self.rangeSpacing2)-1)*self.lineLength1)
            logger.debug("Gross Max Across: %s" % (offAcmax))
            lastAc = int(min(self.lineLength1, self.lineLength2-offAcmax) - xMargin)

            offDnmax = int(coarseDown + ((self.prf2/self.prf1)-1)*self.lineLength1)
            logger.debug("Gross Max Down: %s" % (offDnmax))

            lastDn = int(min(self.fileLength1, self.fileLength2-offDnmax)  - yMargin)

            objAmpcor.setFirstSampleAcross(offAc)
            objAmpcor.setLastSampleAcross(lastAc)
            objAmpcor.setFirstSampleDown(offDn)
            objAmpcor.setLastSampleDown(lastDn)

            objAmpcor.setAcrossGrossOffset(coarseAcross)
            objAmpcor.setDownGrossOffset(coarseDown)

            if (offAc > lastAc) or (offDn > lastDn):
                logger.info('Search window scale is too large.')
                logger.info('Skipping scale: %d'%(iterNum+1))
                continue

            logger.debug('First Sample Across = %d'%(offAc))
            logger.debug('Last Sampe Across = %d'%(lastAc))
            logger.debug('First Sample Down = %d'%(offDn))
            logger.debug('Last Sample Down = %d'%(lastDn))
            logger.debug('Looks = %d'%(scaleFactor))
            logger.debug('Correlation window sizes: %d %d'%(objAmpcor.windowSizeWidth, objAmpcor.windowSizeHeight))
            logger.debug('Search window sizes: %d %d'%(objAmpcor.searchWindowSizeWidth, objAmpcor.searchWindowSizeHeight))

            objAmpcor.band1 = self.band1
            objAmpcor.band2 = self.band2
            if (iterNum == 0):
                objAmpcor.setNumberLocationAcross(self.numberLocationAcross)
                objAmpcor.setNumberLocationDown(self.numberLocationDown)
                objAmpcor.setAcrossLooks(self.acrossLooks)
                objAmpcor.setDownLooks(self.downLooks)
                objAmpcor.setZoomWindowSize(self.zoomWindowSize)
                objAmpcor.setOversamplingFactor(self.oversamplingFactor)
            else:
                objAmpcor.setNumberLocationAcross(self.coarseNumWinAcross)
                objAmpcor.setNumberLocationDown(self.coarseNumWinDown)
                objAmpcor.setAcrossLooks(scaleFactor*self.acrossLooks)
                objAmpcor.setDownLooks(scaleFactor*self.downLooks)
                objAmpcor.setZoomWindowSize(self.coarseZoomWindowSize)
                objAmpcor.setOversamplingFactor(self.coarseOversamplingFactor)

            objAmpcor.ampcor(mSlc, sSlc)

            offField = objAmpcor.getOffsetField()

            if (iterNum != 0):
                objOff = isceobj.createOffoutliers()
                objOff.wireInputPort(name='offsets', object=offField)
                objOff.setSNRThreshold(self.coarseSNRThreshold)
                objOff.setDistance(self.coarseDistance)
                self._stdWriter.setFileTag("nstage_offoutliers"+str(iterNum), "log")
                self._stdWriter.setFileTag("nstage_offoutliers"+str(iterNum), "err")
                self._stdWriter.setFileTag("nstage_offoutliers"+str(iterNum), "out")
                objOff.setStdWriter(self._stdWriter)
                objOff.offoutliers()

                fracLeft = len(objOff.indexArray)/(1.0*len(offField._offsets))

                print('FracLEft = ', fracLeft)
                if (fracLeft < 0.1):
                    logger.error('NStage - Iteration: %d, Fraction Windows left: %d. Increase number of windows or improve gross offset estimate manually.'%(iterNum, int(100*fracLeft)))
                    raise Exception('NStage matching failed at iteration : %d'%(iterNum))
                elif (fracLeft < 0.2):
                    logger.error('NStage - Iteration: %d, Fraction Windows left: %d. Increase number of windows or improve gross offset estimate manually.'%(iterNum, int(100*fracLeft)))

                coarseAcross = int(objOff.averageOffsetAcross)
                coarseDown = int(objOff.averageOffsetDown)

        mSlc.finalizeImage()
        sSlc.finalizeImage()

        self.getState(offField)
        objOff = None
        objAmpcor = None
        return

    def getState(self, off):
        '''
        Set up the output variables.
        '''
        upack = off.unpackOffsetswithCovariance()
        for val in upack:
            self.locationAcross.append(val[0])
            self.locationAcrossOffset.append(val[1])
            self.locationDown.append(val[2])
            self.locationDownOffset.append(val[3])
            self.snrRet.append(val[4])
            self.cov1Ret.append(val[5])
            self.cov2Ret.append(val[6])
            self.cov3Ret.append(val[7])

        self.numRows = len(upack)
        return

    def checkTypes(self):
        '''Check if the image datatypes are set.'''

        if self.imageDataType1 == '':
            if self.slcImage1.getDatatype().upper().startswith('C'):
                self.imageDataType1 = 'complex'
            else:
                raise ValueError('Undefined value for imageDataType1. Should be complex/real/rmg1/rmg2')
        else:
            if self.imageDataType1 not in ('complex','real'):
                raise ValueError('ImageDataType1 should be either complex/real/rmg1/rmg2.')

        if self.imageDataType2 == '':
            if self.slcImage2.getDatatype().upper().startswith('C'):
                self.imageDataType2 = 'complex'
            else:
                raise ValueError('Undefined value for imageDataType2. Should be complex/real/rmg1/rmg2')
        else:
            if self.imageDataType2 not in ('complex','real'):
                raise ValueError('ImageDataType1 should be either complex/real.')

    def checkWindows(self):
        '''Ensure that the window sizes are valid for the code to work.'''

        if (self.windowSizeWidth%2 == 1):
            raise ValueError('Window size width needs to be an even number.')

        if (self.windowSizeHeight%2 == 1):
            raise ValueError('Window size height needs to be an even number.')

        if not is_power2(self.zoomWindowSize):
            raise ValueError('Zoom window size needs to be a power of 2.')

        if not is_power2(self.oversamplingFactor):
            raise ValueError('Oversampling factor needs to be a power of 2.')

        if not is_power2(self.coarseOversamplingFactor):
            raise ValueError('Coarse oversampling factor needs to be a power of 2.')

        if self.searchWindowSizeWidth >=  2*self.windowSizeWidth :
            raise ValueError('Search Window Size Width should be < 2 * Window Size Width')

        if self.searchWindowSizeHeight >= 2*self.windowSizeHeight :
            raise ValueError('Search Window Size Height should be < 2 * Window Size Height')

        if self.zoomWindowSize >= min(self.searchWindowSizeWidth, self.searchWindowSizeHeight):
            raise ValueError('Zoom window size should be <= Search window size')

        if self._stdWriter is None:
            self._stdWriter = create_writer("log", "", True, filename="nstage.log")

    def setImageDataType1(self, var):
        self.imageDataType1 = str(var)
        return

    def setImageDataType2(self, var):
        self.imageDataType2 = str(var)
        return

    def setLineLength1(self,var):
        self.lineLength1 = int(var)
        return

    def setLineLength2(self, var):
        self.LineLength2 = int(var)
        return

    def setFileLength1(self,var):
        self.fileLength1 = int(var)
        return

    def setFileLength2(self, var):
        self.fileLength2 = int(var)

    def setNumberLocationAcross(self,var):
        self.numberLocationAcross = int(var)
        return

    def setCoarseNumWinAcross(self,var):
        self.coarseNumWinAcross = int(var)
        return

    def setCoarseNumWinDown(self,var):
        self.coarseNumWinDown = int(var)
        return

    def setNumberLocationDown(self,var):
        self.numberLocationDown = int(var)
        return

    def setAcrossGrossOffset(self,var):
        self.acrossGrossOffset = int(var)
        return

    def setDownGrossOffset(self,var):
        self.downGrossOffset = int(var)
        return

    def setFirstPRF(self,var):
        self.prf1 = float(var)
        return

    def setSecondPRF(self,var):
        self.prf2 = float(var)
        return

    def setFirstRangeSpacing(self,var):
        self.rangeSpacing1 = float(var)
        return

    def setSecondRangeSpacing(self,var):
        self.rangeSpacing2 = float(var)

    def setReferenceSlcImage(self,im):
        self.slcImage1 = im
        return

    def setSecondarySlcImage(self,im):
        self.slcImage2 = im
        return

    def setWindowSizeWidth(self, var):
        temp = int(var)
        if (temp%2 == 1):
            raise ValueError('Window width needs to be an even number.')
        self.windowSizeWidth = temp
        return

    def setWindowSizeHeight(self, var):
        temp = int(var)
        if (temp%2 == 1):
            raise ValueError('Window height needs to be an even number.')
        self.windowSizeHeight = temp
        return

    def setZoomWindowSize(self, var):
        temp = int(var)
        if not is_power2(temp):
            raise ValueError('Zoom window size needs to be a power of 2.')
        self.zoomWindowSize = temp

    def setOversamplingFactor(self, var):
        temp = int(var)
        if not is_power2(temp):
            raise ValueError('Oversampling factor needs to be a power of 2.')
        self.oversamplingFactor = temp

    def setCoarseOversamplingFactor(self, var):
        temp = int(var)
        if not is_power2(temp):
            raise ValueError('Coarse oversampling factor needs to be a power of 2.')
        self.coarseOversamplingFactor = temp

    def setSearchWindowSizeWidth(self, var):
        self.searchWindowSizeWidth = int(var)
        return

    def setSearchWindowSizeHeight(self, var):
        self.searchWindowSizeHeight = int(var)
        return

    def setAcrossLooks(self, var):
        self.acrossLooks = int(var)
        return

    def setDownLooks(self, var):
        self.downLooks = int(var)
        return

    def stdWriter(self, var):
        self._stdWriter = var
        return

    def getOffsetField(self):
        """Return and OffsetField object instead of an array of results"""
        offsets = OffsetField()
        for i in range(self.numRows):
            across = self.locationAcross[i]
            down = self.locationDown[i]
            acrossOffset = self.locationAcrossOffset[i]
            downOffset = self.locationDownOffset[i]
            snr = self.snrRet[i]
            sigx = self.cov1Ret[i]
            sigy = self.cov2Ret[i]
            sigxy = self.cov3Ret[i]
            offset = Offset()
            offset.setCoordinate(across,down)
            offset.setOffset(acrossOffset,downOffset)
            offset.setSignalToNoise(snr)
            offset.setCovariance(sigx,sigy,sigxy)
            offsets.addOffset(offset)

        return offsets

    def getLocationAcross(self):
        return self.locationAcross

    def getLocationAcrossOffset(self):
        return self.locationAcrossOffset

    def getLocationDown(self):
        return self.locationDown

    def getLocationDownOffset(self):
        return self.locationDownOffset

    def getSNR(self):
        return self.snrRet

    def getCov1(self):
        return self.cov1Ret

    def getCov2(self):
        return self.cov2Ret

    def getCov3(self):
        return self.cov3Ret

    def __init__(self, name=''):
        super(NStage, self).__init__(family=self.__class__.family, name=name)
        self.locationAcross = []
        self.locationAcrossOffset = []
        self.locationDown = []
        self.locationDownOffset = []
        self.snrRet = []
        self.cov1Ret = []
        self.cov2Ret = []
        self.cov3Ret = []
        self.lineLength1 = None
        self.lineLength2 = None
        self.fileLength1 = None
        self.fileLength2 = None
        self.scaleFactorX = None
        self.scaleFactorY = None
        self.numRows = None
        self._stdWriter = None
        self.dictionaryOfVariables = { \
                                      'IMAGETYPE1' : ['imageDataType1', 'str', 'optional'], \
                                      'IMAGETYPE2' : ['imageDataType2', 'str', 'optional'], \
                                      'NUMBER_LOCATION_ACROSS' : ['numberLocationAcross', 'int','mandatory'], \
                                      'NUMBER_LOCATION_DOWN' : ['numberLocationDown', 'int','mandatory'], \
                                      'COARSE_NUMBER_LOCATION_ACROSS' : ['coarseNumWinAcross','int','mandatory'], \
                                      'COARSE_NUMBER_LOCATION_DOWN' : ['coarseNumWinDown', 'int', 'mandatory'], \
                                      'ACROSS_GROSS_OFFSET' : ['acrossGrossOffset', 'int','optional'], \
                                      'DOWN_GROSS_OFFSET' : ['downGrossOffset', 'int','optional'], \
                                      'PRF1' : ['prf1', 'float','optional'], \
                                      'PRF2' : ['prf2', 'float','optional'], \
                                      'RANGE_SPACING1' : ['rangeSpacing1', 'float', 'optional'], \
                                      'RANGE_SPACING2' : ['rangeSpacing2', 'float', 'optional'], \
                                      }
        self.dictionaryOfOutputVariables = { \
                                            'LOCATION_ACROSS' : 'locationAcross', \
                                            'LOCATION_ACROSS_OFFSET' : 'locationAcrossOffset', \
                                            'LOCATION_DOWN' : 'locationDown', \
                                            'LOCATION_DOWN_OFFSET' : 'locationDownOffset', \
                                            'SNR' : 'snrRet' \
                                            }
#        self.descriptionOfVariables = {}
#        self.mandatoryVariables = []
#        self.optionalVariables = []
#        self.initOptionalAndMandatoryLists()
        return None

#end class
if __name__ == "__main__":
    sys.exit(main())