#########################################################################
# File Name: getKmerFromVCF_REF.py
# Author: yanbo
# mail: liyanbotop@163.com
# Created Time: Thu 09 May 2019 10:45:06 AEST
#########################################################################
#!/bin/bash

import collections
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
import re
import sys
import tools
import read

def write_pair_kmer(outFile, kmers):

    sortedKmers = sorted(kmers)
    with open(outFile, "w") as f:
        for (kmer1, kmer2, pos) in sortedKmers:
            #f.write("%s %s %s %s %s %s %s %s\n" % (ele[0], ele[1], ele[2], ele[3], ele[4], ele[5], tools.reverse(ele[0]), tools.reverse(ele[1]) ) )
            f.write("%s %s %s\n" % (kmer1, kmer2, pos) )

def get_snp_pair_kmer(vcfFilename):

    snps = read.read_vcf(vcfFilename)
    kmerFilename="chr" + sys.argv[1] + ".snp.real." + sys.argv[2] + "mer"
    kmers = []
    for key in snps:
        #assert seq[key-1] == snps[key][0] or seq[key-1] == snps[key][1]
        assert seq[key-1] == snps[key][0]
        h1 = seq[key-int(k/2)-1 : key-1] + snps[key][0] + seq[key : key+int(k/2) ] # 0
        h2 = seq[key-int(k/2)-1 : key-1] + snps[key][1] + seq[key : key+int(k/2) ] # 1
        if h1.count('N') > 0 or h2.count('N') > 0:
            continue
        '''
        new_h1 = tools.reverse(h1) # 0
        new_h2 = tools.reverse(h2) # 1
        min_h= min(h1,h2)
        min_newh = min(new_h1, new_h2)
        ID = snps[key][2]
        if min_h < min_newh:
            if h1 < h2:
                kmers.append( (h1, h2, key, ID, 0, 1) )
            else:
                kmers.append( (h2, h1, key, ID, 1, 0) )
        else:
            if new_h1 < new_h2:
                kmers.append( (new_h1, new_h2, key, ID, 0, 1) )
            else:
                kmers.append( (new_h2, new_h1, key, ID, 1, 0) )
        '''
        smallerH1, smallerH2 = tools.get_smaller_pair_kmer(h1, h2)
        kmers.append( (smallerH1, smallerH2, key) )

    write_pair_kmer(kmerFilename, kmers)

def get_indel_pair_kmer(vcfFilename):

    indels = read.read_vcf(vcfFilename)
    kmerFilename="chr" + sys.argv[1] + ".indel.real." + sys.argv[2] + "mer"
    kmers = []
    indel_length1_cnt = 0
    for key in indels:
        s1, s2, ID = indels[key]
        lenS1, lenS2 = len(s1), len(s2)
        if lenS1 + lenS2 > 3:
            continue

        indel_length1_cnt += 1
        assert lenS1 + lenS2 >= 2
        if len(s1) == 1 and len(s2) == 2:
            assert seq[key-1] == s1
            assert s2[0] != s2[1]
            #while s2[1] == seq[key-1]: # delete content is s2[1]
                #key = key-1            # delete happen at "AAA" region, always think delete first poisition
            h1 = seq[key-int(k/2) : key+int(k/2)] # k-1
            h2 = seq[key-int(k/2) : key-1] + s2 + seq[key : key+int(k/2)] # 1 # len: k
            assert len(h1) == k-1 and len(h2) == k
            h1, h2 = h2, h1 # h1 always is longer one
            initialH1 = h1
            if h1.count('N') > 0 or h2.count('N') > 0:
                continue
            #print key, "11", h1
            smallerH1, smallerH2 = tools.get_smaller_pair_kmer(h1, h2)
            kmers.append( (smallerH1, smallerH2, key) )
            # delete happen at multipe "AAAA" region, more pair kmer happen
            l = len(h1)
            mid = l/2
            i=1
            while mid+i<l and initialH1[mid+i] == initialH1[mid]:
                h1 = seq[key-int(k/2)+i : key+int(k/2)+i] # move right i
                h2 = seq[key-int(k/2)+i : key-1] + s2 + seq[key : key+int(k/2)+i] # move right i
                h1, h2 = h2, h1 # h1 always is longer one
                smallerH1, smallerH2 = tools.get_smaller_pair_kmer(h1, h2)
                #print key, "aa"
                kmers.append( (smallerH1, smallerH2, key) )
                i+=1
            i=1
            while mid-i>=0 and initialH1[mid-i] == initialH1[mid]:
                h1 = seq[key-int(k/2)-i : key+int(k/2)-i] # move left i
                h2 = seq[key-int(k/2)-i : key-1] + s2 + seq[key : key+int(k/2)-i] # move right i
                h1, h2 = h2, h1 # h1 always is longer one
                smallerH1, smallerH2 = tools.get_smaller_pair_kmer(h1, h2)
                #print key, "bb"
                kmers.append( (smallerH1, smallerH2, key) )
                i+=1

            ''' # for test can grouth-truth can always keep min strand delete first
            if h1 > tools.reverse(h1):
                print "aa"
                print h1, h2
                print tools.reverse(h1), tools.reverse(h2)
                while s2[1] == seq[key]:
                    key+=1
                h1 = seq[key-int(k/2) : key+int(k/2)] # k-1
                h2 = seq[key-int(k/2) : key] + s2[1] + seq[key : key+int(k/2)] # 1 # len: k
                h1, h2 = h2, h1
                print h1, h2
                print tools.reverse(h1), tools.reverse(h2)
            '''
        elif len(s1) == 2 and len(s2) == 1:
            assert seq[key-1:key+1] == s1
            assert s1[0] != s1[1]
            h1 = seq[key-int(k/2) : key+int(k/2)+1] # k
            h2 = seq[key-int(k/2) : key] + seq[key+1 : key+int(k/2)+1] # 1 # len: k-1
            assert len(h1) == k and len(h2) == k-1
            initialH1 = h1
            if h1.count('N') > 0 or h2.count('N') > 0:
                continue
            smallerH1, smallerH2 = tools.get_smaller_pair_kmer(h1, h2)

            #print key, "22"
            kmers.append( (smallerH1, smallerH2, key) )

            l = len(h1)
            mid = l/2
            i=1
            while initialH1[mid+i] == initialH1[mid]:
                h1 = seq[key-int(k/2)+i : key+int(k/2)+1+i] # k
                h2 = seq[key-int(k/2)+i : key] + seq[key+1 : key+int(k/2)+1+i] # 1 # len: k-1
                smallerH1, smallerH2 = tools.get_smaller_pair_kmer(h1, h2)
                #print key, "cc"
                kmers.append( (smallerH1, smallerH2, key) )
                i+=1
            i=1
            while initialH1[mid-i] == initialH1[mid]:
                h1 = seq[key-int(k/2)-i : key+int(k/2)+1-i] # k
                h2 = seq[key-int(k/2)-i : key] + seq[key+1 : key+int(k/2)+1-i] # 1 # len: k-1
                smallerH1, smallerH2 = tools.get_smaller_pair_kmer(h1, h2)
                #print key, "dd"
                kmers.append( (smallerH1, smallerH2, key) )
                i+=1
    print ("there are ", indel_length1_cnt, "indels, create ", len(kmers), "indel pair kmer")

    write_pair_kmer(kmerFilename, kmers)

'''
allFile = "chr" + sys.argv[1] + ".all." + sys.argv[2] + "mer"
foutAll = open(allFile, "w")
for i in range(seqLen-21):
    mer = seq[i:i+k]
    if mer.count('N') > 0:
        continue
    Rmer = tools.reverse(mer)
    if Rmer < mer:
        mer = Rmer
    foutAll.write("%s %s\n" % (mer, i))
foutAll.close()
'''

# this simulate data is based on hg18
refFilename="/home/yulin/bio/Data/reference/NCBI36_hg18/chr22.fa"
snpVCFFile="/home/yulin/bio/VariationCalling/data/NA12878/VCF/NA12878_hg18_snp_VCFs/chr22.vcf"
indelVCFFile="/home/yulin/bio/VariationCalling/data/NA12878/VCF/NA12878_hg18_indel_VCFs/chr22.vcf"

# this illumina data align to hg19
#print ("input chrID kmer-size")
#refFilename ="/home/yulin/bio/Data/reference/GRCh37_hg19/chr" + sys.argv[1] + ".fa"
#vcfFilename ="/home/yulin/software/HapCUT2/reproduce_hapcut2_paper/run_hapcut2_fosmid/data/NA12878_hg19_VCFs/chr" + sys.argv[1] + ".phased.vcf"

record = SeqIO.read(open(refFilename), "fasta")
print (record.id)
seq = str(record.seq).upper()
seqLen = len(seq)
k=int(sys.argv[2])
get_snp_pair_kmer(snpVCFFile)
get_indel_pair_kmer(indelVCFFile)


#!/usr/bin/python3

# enable debugging
import cgi, cgitb
import json
import requests
import responses
cgitb.enable()

class Expense:
     def __init__(self, exp_name,exp_date,exp_amount,exp_type):
         self.name = exp_name
         self.date = exp_date
         self.amount = exp_amount
         self.type = exp_type

form = cgi.FieldStorage()
exp_name = form.getvalue('exp_name')
exp_date  = form.getvalue('exp_date')
exp_amount = form.getvalue('exp_amount')
exp_type  = form.getvalue('exp_type')

expense = Expense(exp_name,exp_date,exp_amount,exp_type)

jsonString = json.dumps(expense.__dict__)
jsonFile = open("/var/www/html/data.json", "a+")
jsonFile.write(jsonString)
jsonFile.close()

print('Content-Type: text/plain')
print('')
print('sucessful')
print('<br>')
print(jsonString)


"""
This module contains the main transmittance/reflectance calculation
bits.

Users can run the calculations through `model.Model()` and avoid
accessing `core` directly.
"""

import numpy as np
import scipy as sp

def rt_amp(index, delta, theta, pol):
    """Calculate the reflected and transmitted amplitudes through the
    system.

    Parameters
    ----------
    index : numpy array
        An array of refractive indices, ordered from source layer to
        terminator layer.
    delta : numpy array
        An array of wavenumber offsets.
    theta : numpy array
        An array of angles in radians.
    pol : string
        The polarization of the source wave: 's' or 'p',
        or 'u'.

    Returns
    -------
    r, t : tuple
        A tuple where 'r' is the reflected amplitude, and 't' is the
        transmitted amplitude.
    """
    t_amp, r_amp = make_rt_amp_matrix(index, theta, pol)
    m_mat = make_m_matrix(index, t_amp, r_amp, delta)

    m_prime = make_2x2(1., 0., 0., 1., dtype=complex)
    for i in range(1, len(index)-1):
        m_prime = np.dot(m_prime, m_mat[i])

    C_m = make_2x2(1., r_amp[0, 1], r_amp[0, 1], 1., dtype=complex)
    m_prime = np.dot(C_m / t_amp[0, 1], m_prime)
    trans_amp = 1 / m_prime[0, 0]
    ref_amp = m_prime[1, 0] / m_prime[0, 0]
    return ref_amp, trans_amp

def make_rt_amp_matrix(index, theta, pol):
    """Construct reflection and transmission amplitude matrices.

    Parameters
    ----------
    index : numpy array
        An array of refractive indices, ordered from source layer to
        terminator layer.
    theta : numpy array
        An array of angles in radians.
    pol : string
        The polarization of the source wave: 's' or 'p'.

    Returns
    -------
    t_mat, r_mat : tuple
        The t- and r-amplitude matrices.
    """
    t_mat = np.zeros((len(index), len(index)), dtype=complex)
    r_mat = np.zeros((len(index), len(index)), dtype=complex)
    for i in range(len(index) - 1):
        t_mat[i, i+1] = t_interface(index[i], index[i+1], theta[i], theta[i+1], pol)
        r_mat[i, i+1] = r_interface(index[i], index[i+1], theta[i], theta[i+1], pol)
    return t_mat, r_mat

def make_m_matrix(index, t_matrix, r_matrix, delta):
    """Construct the characteristic matrix of the model.

    Parameters
    ----------
    index : numpy array
        An array of refractive indices, ordered from source layer to
        terminator layer.
    t_matrix : numpy array
        The t-amplitude matrix
    r_matrix : numpy array
        The r-amplitude matrix
    delta : numpy array
        An array of wavenumber offsets.

    Returns
    -------
    m_mat : numpy array
        The characteristic matrix of the model
    """
    m_mat = np.zeros((len(index), 2, 2), dtype=complex)
    for i in range(1, len(index)-1):
        C_m = make_2x2(np.exp(-1j * delta[i]), 0., 0., np.exp(1j * delta[i]),
                       dtype=complex)
        r_m = make_2x2(1., r_matrix[i, i+1], r_matrix[i, i+1], 1., dtype=complex)
        m_mat[i] = (1 / t_matrix[i, i+1]) * np.dot(C_m, r_m)
    return m_mat

def r_power(r_amp):
    """Return the fraction of reflected power.

    Parameters
    ----------
    r_amp : float
        The net reflection amplitude after calculating the transfer
        matrix.

    Returns
    -------
    R : numpy array
        The model reflectance
    """
    return np.abs(r_amp)**2

def t_power(t_amp, index_i, index_f, theta_i, theta_f):
    """Return the fraction of transmitted power.

    Parameters
    ----------
    t_amp : float
        The net transmission amplitude after calculating the transfer
        matrix.
    index_i : float
        The index of refraction of the source material.
    index_f : float
        The index of refraction of the terminating material.
    theta_i : float
        The angle of incidence (radians) at the initial interface.
    theta_f : float
        The angle of incidence (radians) at the final interface.

    Returns
    -------
    T : numpy array
        The model transmittance
    """
    return np.abs(t_amp**2) * \
           ( (index_f * np.cos(theta_f)) / (index_i * np.cos(theta_i) ) )

def r_interface(index1, index2, theta1, theta2, pol):
    """Calculate the reflected amplitude at an interface.

    Parameters
    ----------
    index1 : float
        The index of refraction of the first material.
    index2 : float
        The index of refraction of the second material.
    theta1 : float
        The angle of incidence at interface 1, in radians
    theta2 : float
        The angle of incidence at interface 2, in radians
    pol : string
        The polarization of the source wave (either 's' or 'p').

    Returns
    -------
    reflected amplitude : float
        The amplitude of the reflected field at the interface
    """
    if pol == 's':
        numerator = (index1 * np.cos(theta1) - index2 * np.cos(theta2))
        denominator = (index1 * np.cos(theta1) + index2 * np.cos(theta2))
    elif pol == 'p':
        numerator = (index2 * np.cos(theta1) - index1 * np.cos(theta2))
        denominator = (index1 * np.cos(theta2) + index2 * np.cos(theta1))
    else:
        raise ValueError("Polarization must be 's' or 'p'")
    return numerator / denominator

def t_interface(index1, index2, theta1, theta2, pol):
    """Calculate the transmission amplitude at an interface.

    Parameters
    ----------
    index1 : float
        The index of refraction of the first material.
    index2 : float
        The index of refraction of the second material.
    theta1 : float
        The angle of incidence at interface 1, in radians
    theta2 : float
        The angle of incidence at interface 2, in radians
    pol : string
        The polarization of the source wave (either 's' or 'p').

    Returns
    -------
    transmitted_amplitude : float
        The amplitude of the transmitted field at the interface
    """
    if pol == 's':
        numerator = 2 * index1 * np.cos(theta1)
        denominator = (index1 * np.cos(theta1) + index2 * np.cos(theta2))
    elif pol == 'p':
        numerator = 2 * index1 * np.cos(theta1)
        denominator = (index1 * np.cos(theta2) + index2 * np.cos(theta1))
    else:
        raise ValueError("Polarization must be 's' or 'p'")
    return numerator / denominator

def wavenumber(freq, index, tand):
    """Calculate the wavenumber in a material.

    Parameters
    ----------
    freq : float
        The frequency at which to calculate the wavevector, k
    tand : numpy array
        An array of loss tangents, ordered from source to terminating
    index : numpy array
        An array of refractive indices, ordered from source to
        terminating layer

    Returns
    -------
    k : array
        The complex wavenumber, k
    """
    k = 2 * np.pi * (freq / 3e8) * index * np.sqrt(1 + 1j * tand)
    return k

def alpha2imagn(freq, a, b, n):
    """Convert Halpern's 'a' and 'b' from an absorption coefficient
    of the form `a*freq**b` to a (frequency-dependent) .

    Parameters
    ----------
    freq : numpy array or float
        The frequency (Hz) (or frequencies) at which to calculate the loss
        tangent.
    a : float
        Halpern's 'a' coefficient
    b : float
        Halpern's 'b' coefficient
    n : float
        The real part of the material's refractive index

    Returns
    -------
    imagn : numpy array or float
        The imaginary component of the refractive index
    """
    nu = freq / 30e9
    # First we need the frequency-dependent absorption coefficient,
    # alpha, which we get from the Halpern fit. From that we will
    # calculate k(appa), the extinction coefficient, for each
    # frequency of interest
    alpha = 2 * a * nu**b

    # This is the absorption-extinction coefficient relation as ~written
    # in Born & Wolf Principles of Optics 1st Ed., 1959, Ch. 13.1,
    # Pg. 614, Eq. 21
    # The factor of 3e10 (c in units of cms^-1) ensures that our k is
    # unitless, as it ought to be.
    imagn = (100 * 3e8 * alpha) / (4 * np.pi * n * freq)
    return imagn

def alpha2tand(freq, a, b, n):
    """Convert Halpern's 'a' and 'b' from an absorption coefficient
    of the form `a*freq**b` to a (frequency-dependent) loss tangent.

    Parameters
    ----------
    freq : numpy array or float
        The frequency (Hz) (or frequencies) at which to calculate the loss
        tangent.
    a : float
        Halpern's 'a' coefficient
    b : float
        Halpern's 'b' coefficient
    n : float
        The real part of the material's refractive index

    Returns
    -------
    tand : numpy array
        The loss tangent of the material at the given frequency and
        Halpern coefficients.
    """
    imagn = alpha2imagn(freq, a, b, n)

    # The complex index of refraction of a material is related to the
    # complex (relative) permittivity by the relation:
    #   e_r = e' + i*e'' = n^2 = (n + i*k)^2 = n^2 - k^2 + i*2nk
    # By equating the real and imaginary parts we are left with:
    #   e' = (n^2 - k^2); e'' = 2nk
    # With this information we can find the loss tangent, which is simply
    # the ratio of the real and imaginary parts of the relative
    # permittivity:
    #   tand = (e''/e')
    ep = n**2 - imagn**2
    epp = 2 * n * imagn
    tand = epp / ep
    return tand

def make_2x2(a11, a12, a21, a22, dtype=float):
    """Return a 2x2 array quickly.

    Parameters
    ----------
    a11 : float
        Array element [0, 0].
    a12 : float
        Array element [0, 1].
    a21 : float
        Array element [1, 0].
    a22 : float
        Array element [1, 1].
    dtype : dtype, optional
        The datatype of the array. Defaults to float.

    Returns
    -------
    array : numpy array
        A 2x2 array [[a11, a12], [a21, a22]]
    """
    array = np.empty((2, 2), dtype=dtype)
    array[0, 0] = a11
    array[0, 1] = a12
    array[1, 0] = a21
    array[1, 1] = a22
    return array

def prop_wavenumber(k, d, theta):
    """Propagate the wave through a material and calculate its offset,
    delta.

    Parameters
    ----------
    k : array
        The wavenumber
    d : array
        An array of distances (thicknesses), ordered from source to
        terminating layer
    theta : float
        The angle the wave passes through the medium

    Returns
    -------
    delta : array
        The phase difference
    """
    # Turn off 'invalid multiplication' error; it's just the 'inf' boundaries
    olderr = sp.seterr(invalid='ignore')
    delta = k * d * np.cos(theta)
    # Now turn the error back on
    sp.seterr(**olderr)
    return delta

def refract(n, theta0):
    """Calculate the angle by which an incident ray is refracted

    Parameters
    ----------
    n : numpy array
        An array of refractive indices, ordered from source layer to
        terminator layer.
    theta0 : float
        The initial angle of incidence (radians)

    Returns
    -------
    thetas : numpy array
        The Snell angles at each interface
    """
    # Make a nice pairwise generator so we can avoid playing games with
    # index counting
    thetas = [theta0]
    ngen = zip(n, n[1:])
    for i, rind in enumerate(ngen):
        theta = np.arcsin(np.real_if_close( rind[0] * np.sin(thetas[i]) / rind[1] ))
        thetas.append(theta)
    return np.asarray(thetas)

def replace_tand(freq, tand_array, halpern_dict):
    """Calculate a frequency-dependent loss tangent from a material's
    Halpern coefficiencts if they exist.

    Parameters
    ----------
    freq : float
        The frequency at which to calculate the loss tangent
    tand_array : numpy array
        The loss tangents of the materials, ordered from Source to
        Terminator
    halpern_dict : dict
        A dictionary keyed by layer index, containing Halpern coefficients

    Returns
    -------
    tand_array : numpy array
        The loss tangents of the materials, ordered from Source to
        Terminator. Where possible, the Halpern coefficients have been
        applied to make the terms frequency-dependent.
    """
    for k, v in halpern_dict.items():
        tand_array[k] = alpha2tand(freq, v['a'], v['b'], v['n'])
    return tand_array

def main(params):
    """Run a transmittance/reflectance calculation for the given parameters.

    This function is the primary entry-point to the calculation, and should
    not be called directly. Instead, call `Model.run()`.

    If you must call `core.main()` directly, only do so after first calling
    `Model.set_up()`.

    Parameters
    ----------
    params : dict
        The dictionary contructed by `Model.set_up`. See that function
        documentation for details.

    Returns
    -------
    result : dict
        A dictionary with three keys:
         * `frequency`: the frequency (in Hz) at which T and R were calculated
         * `transmittance`: the output transmittance (T) of the model
         * `reflectance`: the output reflectance (R) of the model
    """
    rind = params['rind']
    thick = params['thick']
    tand = params['tand']
    pol = params['pol']
    theta0 = params['theta0']
    theta = refract(rind, theta0)
    freq = params['freq']
    halps = params['halpern_layers']

    # Create containers for the reflection/transmission values we calculate
    # at each frequency
    ts = []
    rs = []

    for f in freq:
        if len(halps.keys()) > 0:
            tand = replace_tand(f, tand, halps)
        ks = wavenumber(f, rind, tand)
        delta = prop_wavenumber(ks, thick, theta)
        r_amp, t_amp = rt_amp(rind, delta, theta, pol)
        t_pow = t_power(t_amp, rind[0], rind[-1], theta[0], theta[-1])
        r_pow = r_power(r_amp)
        ts.append(t_pow)
        rs.append(r_pow)

    ts = np.asarray(ts)
    rs = np.asarray(rs)

    results = {'frequency':freq, 'transmittance':ts, 'reflectance':rs}
    return results


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

import PySide2.QtWidgets as qtwidgets
import PySide2.QtCore as qtcore
import PySide2.QtGui as qtgui
import PySide2.QtNetwork as qtnetwork

import os.path
import signal
import socket

class HButtonBar(qtwidgets.QWidget):
    layout=qtwidgets.QHBoxLayout

    def __init__(self,def_list):
        qtwidgets.QWidget.__init__(self)
        b_layout=self.layout()
        for label,callback in def_list:
            button = qtwidgets.QPushButton(label)
            button.clicked.connect(callback)
            b_layout.addWidget(button)
        self.setLayout(b_layout)

class VButtonBar(HButtonBar):
    layout=qtwidgets.QVBoxLayout

class OpenFileWidget(qtwidgets.QWidget):

    def __init__(self):
        qtwidgets.QWidget.__init__(self)
        self.field=qtwidgets.QLineEdit()
        button=qtwidgets.QPushButton("Browse...")
        layout = qtwidgets.QHBoxLayout()
        layout.addWidget(self.field,stretch=1)
        layout.addWidget(button,stretch=0)
        self.setLayout(layout)
        button.pressed.connect(self._open)

    def text(self):
        return self.field.text()

    def setText(self,txt):
        self.field.setText(txt)

    def blockTextSignals(self,flag):
        self.field.blockSignals(flag)

    def _open(self):
        dialog = qtwidgets.QFileDialog(self)
        dialog.setFileMode(qtwidgets.QFileDialog.ExistingFile)
        dialog.setAcceptMode(qtwidgets.QFileDialog.AcceptOpen)
        old=self.field.text()
        if not old:
            dialog.setDirectory(".")
        else:
            dialog.setDirectory(os.path.dirname(old))
            dialog.selectFile(old)
        if dialog.exec_():
            fnames = dialog.selectedFiles()
            self.field.setText(fnames[0])

class SaveFileWidget(OpenFileWidget):
    def _open(self):
        dialog = qtwidgets.QFileDialog(self)
        dialog.setFileMode(qtwidgets.QFileDialog.AnyFile)
        dialog.setAcceptMode(qtwidgets.QFileDialog.AcceptSave)
        old=self.field.text()
        if not old:
            dialog.setDirectory(".")
        else:
            dialog.setDirectory(os.path.dirname(old))
            dialog.selectFile(old)
        if dialog.exec_():
            fnames = dialog.selectedFiles()
            self.field.setText(fnames[0])

class OpenDirWidget(OpenFileWidget):

    def _open(self):
        dialog = qtwidgets.QFileDialog(self)
        dialog.setFileMode(qtwidgets.QFileDialog.Directory)
        dialog.setAcceptMode(qtwidgets.QFileDialog.AcceptOpen)
        dialog.setOptions(qtwidgets.QFileDialog.ShowDirsOnly)
        old=self.field.text()
        if not old:
            dialog.setDirectory(".")
        else:
            dialog.setDirectory(os.path.dirname(old))
            dialog.selectFile(old)
        if dialog.exec_():
            fnames = dialog.selectedFiles()
            self.field.setText(fnames[0])

class SignalWakeupHandler(qtnetwork.QAbstractSocket):

    def __init__(self, parent=None):
        super().__init__(qtnetwork.QAbstractSocket.UdpSocket, parent)
        self.old_fd = None
        # Create a socket pair
        self.wsock, self.rsock = socket.socketpair(type=socket.SOCK_DGRAM)
        # Let Qt listen on the one end
        self.setSocketDescriptor(self.rsock.fileno())
        # And let Python write on the other end
        self.wsock.setblocking(False)
        self.old_fd = signal.set_wakeup_fd(self.wsock.fileno())
        # First Python code executed gets any exception from
        # the signal handler, so add a dummy handler first
        self.readyRead.connect(lambda : None)
        # Second handler does the real handling
        self.readyRead.connect(self._readSignal)

    def __del__(self):
        # Restore any old handler on deletion
        if self.old_fd is not None and signal and signal.set_wakeup_fd:
            signal.set_wakeup_fd(self.old_fd)

    def _readSignal(self):
        # Read the written byte.
        # Note: readyRead is blocked from occuring again until readData()
        # was called, so call it, even if you don't need the value.
        data = self.readData(1)
        # Emit a Qt signal for convenience
        self.signalReceived.emit(data[0])

    signalReceived = qtcore.Signal(int)

class FormDialog(qtwidgets.QDialog):
    def _font(self,style,size):
        font_db = qtgui.QFontDatabase()
        family="Raleway"
        font=font_db.font(family,style,size)
        return font

    def __init__(self,window,title,form,*args,**kwargs):
        super().__init__(window,*args,**kwargs)
        self.setWindowTitle(title)
        flags = qtwidgets.QDialogButtonBox.Ok | qtwidgets.QDialogButtonBox.Cancel

        button_box = qtwidgets.QDialogButtonBox(flags)
        button_box.accepted.connect(self.accept)
        button_box.rejected.connect(self.reject)

        for w in button_box.findChildren(qtwidgets.QWidget):
            w.setFont(self._font("Medium",10))

        f_widget=qtwidgets.QWidget()
        self._form=form
        f_widget.setLayout(self._form)
        for w in f_widget.findChildren(qtwidgets.QWidget):
            w.setFont(self._font("Medium",10))

        v_layout = qtwidgets.QVBoxLayout()
        v_layout.addWidget(f_widget)
        v_layout.addWidget(button_box)
        self.setLayout(v_layout)

    def get_data(self):
        print("dialog")
        ret=self.exec_()
        data=list(self._form.get_data())
        data.append(ret==self.Accepted)
        return tuple(data)

class AwesomeToolBar(qtwidgets.QToolBar):
    def _font(self,family,style,size):
        font_db = qtgui.QFontDatabase()
        family="Font Awesome 5 "+family
        font=font_db.font(family,style,size)
        return font

    def __init__(self,parent): #icon,tooltip,size=8,style="Solid",family="Free"):
        qtwidgets.QToolBar.__init__(self,parent)

    def addAction(self,icon,tooltip,size=8,style="Solid",family="Free"):
        action=qtwidgets.QToolBar.addAction(self,icon)
        action.setToolTip(tooltip)
        action.setFont(self._font(family,style,size))
        return action

class AddRootProxyModel(qtcore.QIdentityProxyModel):
    root="==root=="

    def data(self, index, role):
        parent=index.parent()
        if parent.isValid():
            return qtcore.QIdentityProxyModel.data(self,index,role)
        row=index.row()
        if row==0:
            if role not in [ qtcore.Qt.DisplayRole, qtcore.Qt.EditRole]:
                ret=qtcore.QIdentityProxyModel.data(self,index,role)
                print(role,ret)
                return ret
            return "----"
        sibling=index.sibling(row-1,index.column())
        return qtcore.QIdentityProxyModel.data(self,sibling,role)

    def flags(self,index):
        if not index.parent().isValid():
            if index.row()==0:
                return qtcore.Qt.ItemIsEnabled | qtcore.Qt.ItemIsSelectable | qtcore.Qt.ItemNeverHasChildren
        return qtcore.Qt.ItemIsEnabled | qtcore.Qt.ItemIsSelectable

    def rowCount(self,index):
        if index.isValid():
            if index.parent().isValid():
                return qtcore.QIdentityProxyModel.rowCount(self,index)
            if index.row()==0: return 0
            return qtcore.QIdentityProxyModel.rowCount(self,index)
        return 1+qtcore.QIdentityProxyModel.rowCount(self)

    def index(self,row,column,parent=qtcore.QModelIndex()):
        if parent.isValid():
            return qtcore.QIdentityProxyModel.index(self,row,column,parent)
        if row==0:
            ret=self.createIndex(0,column,self.root)
            return ret
        old=qtcore.QIdentityProxyModel.index(self,row-1,column,parent)
        return self.createIndex(row,column,old.internalPointer())

    def parent(self,index):
        if not index.isValid(): return qtcore.QModelIndex()
        obj=index.internalPointer()
        if obj==self.root: return qtcore.QModelIndex()
        return qtcore.QIdentityProxyModel.parent(self,index)

    def mapToSource(self,proxyIndex):
        new_index=qtcore.QIdentityProxyModel.mapToSource(self,proxyIndex)
        if new_index.internalPointer()==self.root:
            return qtcore.QModelIndex()
        return new_index

    def mapFromSource(self,sourceIndex):
        new_index=qtcore.QIdentityProxyModel.mapFromSource(self,sourceIndex)
        if new_index.parent().isValid(): return new_index
        return self.createIndex(1+new_index.row(),new_index.column(),
                                new_index.internalPointer())

    # def mapFromSource(self, sourceIndex):
    #     if not sourceIndex.isValid(): return qtcore.QModelIndex()
    #     parent=sourceIndex.parent()
    #     if parent.isValid():
    #         return self.createIndex(sourceIndex.row(),
    #                                 sourceIndex.column(),
    #                                 sourceIndex.internalPointer())
    #     return self.createIndex(1+sourceIndex.row(),
    #                             sourceIndex.column(),
    #                             sourceIndex.internalPointer())

    # def mapToSource(self, proxyIndex):
    #     if not proxyIndex.isValid(): return qtcore.QModelIndex()
    #     parent=proxyIndex.parent()
    #     if parent.isValid:
    #         return qtcore.QIdentityProxyModel.mapToSource(self,proxyIndex)
    #     obj=proxyIndex.internalPointer()
    #     if obj==self.root: return qtcore.QModelIndex()
    #     return self.sourceModel().createIndex(proxyIndex.row()-1,
    #                                           proxyIndex.column(),
    #                                           obj)


import cv2
import glob
from matplotlib import pyplot as plt
faceDet = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
faceDet_two = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")
faceDet_three = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
faceDet_four = cv2.CascadeClassifier("haarcascade_frontalface_alt_tree.xml")

fishface = cv2.face.FisherFaceRecognizer_create()
fishface.read('fish.xml')
emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"]

for files in glob.glob("C:\\Users\\HP\\Desktop\\classify\\*"):
    gray = cv2.imread(files)
    gray  = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)

    face = faceDet.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10, minSize=(5, 5), flags=cv2.CASCADE_SCALE_IMAGE)
    face_two = faceDet_two.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10, minSize=(5, 5), flags=cv2.CASCADE_SCALE_IMAGE)
    face_three = faceDet_three.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10, minSize=(5, 5), flags=cv2.CASCADE_SCALE_IMAGE)
    face_four = faceDet_four.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10, minSize=(5, 5), flags=cv2.CASCADE_SCALE_IMAGE)
    if len(face) == 1:
        facefeatures = face
    elif len(face_two) == 1:
        facefeatures = face_two
    elif len(face_three) == 1:
        facefeatures = face_three
    elif len(face_four) == 1:
        facefeatures = face_four
    else:
        facefeatures = ""
    for (x, y, w, h) in facefeatures:
        gray = gray[y:y+h, x:x+w]
        try:
            gray = cv2.resize(gray, (350, 350))
        except:
           pass

    plt.subplot(132)
    plt.title('img')
    plt.imshow(gray, 'gray')
    plt.xticks([])
    plt.yticks([])
    plt.show()
    Class, abc = fishface.predict(gray)
    print(emotions[Class])


from sklearn.model_selection import train_test_split
from sklearn import svm

def svm_classification(X, y, C_in, gamma_in, kernel_in):
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=y, random_state=42)

    classifierSVM = svm.SVC(C=C_in, degree=2, gamma=gamma_in, kernel=kernel_in)
    # training
    classifierSVM.fit(X_train, y_train)
    # prediksi data test

    y_pred_SVM = classifierSVM.predict(X_test)
    # return X_train, X_test, y_train, y_test, classifierSVM, y_pred_SVM
    return classifierSVM, y_pred_SVM, y_test


# Rotating or flipping an image
from PIL import Image

def main():
    image = Image.open('../lenna.png')
    image.show('Original')

    # Rotate 60 degrees counter clockwise
    rotated_image = image.rotate(60)
    rotated_image.show('Rotate 60')

    # Rotate using Image.transpose
    # Transpose supports these values:
    #  - Image.FLIP_LEFT_RIGHT
    #  - Image.FLIP_TOP_BOTTOM
    #  - Image.ROTATE_90
    #  - Image.ROTATE_180
    #  - Image.ROTATE_270
    rotated_image = image.transpose(Image.ROTATE_90)
    rotated_image.show('Rotate 90')

    # Flip horizontal
    flipped_image = image.transpose(Image.FLIP_LEFT_RIGHT)
    flipped_image.show('Flip horizontal')

    # Flip vertical
    flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM)
    flipped_image.show('Flip vertical')

if __name__ == '__main__':
    main()


import re
import math
import scipy.stats as stats
from statsmodels.stats.multitest import multipletests
import numpy as np
import pandas as pd

from tqdm import tqdm
# import functools
import pprint

from mutagene.dna import (
    nucleotides, complementary_nucleotide,
    bases_dict,
    # comp_dict,
    extended_nucleotides, complementary_extended_nucleotide)

from mutagene.io.motifs import get_known_motifs

import logging
logger = logging.getLogger(__name__)

def identify_motifs(samples_mutations, custom_motif=None, strand=None, threshold=None, dump_matches=None, stat_type=None):
    """
    :param samples_mutations: list of mutations from input file
    :param custom_motif: specified motif to search for
    :param strand: strand(s) to search on (T: transcribed, N: non-transcribed, A: any, or a combination theirof: 'TNA')
    :param dump_matches: pass through to process_mutations, stores all motif matches
    :param stat_type: pass through to process_mutations, choose statistical test
    :return: command-line output
    """
    motif_matches = []
    sig_motif_matches = []
    pvals = []

    if strand is None:
        strand = 'A'
    else:
        strand = set(strand)  # in case TNA codes repeat

    if threshold is None:
        threshold = 0.05

    if custom_motif:
        search_motifs = scanf_motif(custom_motif)
    else:
        motifs = get_known_motifs()
        search_motifs = motifs.copy()
    # search_motifs.extend(scanf_motif(custom_motif))

    _strand_map = {
        'T': 'transcribed',
        'N': 'non-transcribed',
        'A': 'any strand'
    }

    disable_progress_bar = logger.getEffectiveLevel() == logging.DEBUG

    for sample, mutations in tqdm(samples_mutations.items(), leave=False, disable=disable_progress_bar):
        if mutations is not None and len(mutations) > 0:
            first_mut_seq_with_coords = mutations[0][-1]
            window_size = (len(first_mut_seq_with_coords) - 1) // 2

            for m in tqdm(search_motifs, leave=False, disable=disable_progress_bar):
                for s in strand:
                    result, saved_data = process_mutations(
                        mutations,
                        m['motif'],
                        m['position'],
                        m['ref'],
                        m['alt'],
                        window_size,
                        s,
                        stat_type=stat_type)

                    if dump_matches:
                        for chrom, pos in saved_data['mutation_motif']:
                            dump_matches.write(
                                "chr{}\t{}\t{}\t{}\t{}\t{}\n".format(
                                    chrom, pos, int(pos) + 1, sample, m['logo'], _strand_map[s]))

                    debug_data = {
                        'sample': sample,
                        'motif': m['logo'],
                        'strand': s}
                    debug_data.update(result)
                    debug_string = pprint.pformat(debug_data, indent=4)
                    logger.debug(debug_string)

                    motif_matches.append({
                        'sample': sample,
                        'mutagen': m['name'],
                        'motif': m['logo'],
                        'strand': _strand_map[s],
                        'enrichment': result['enrichment'],
                        'mut_min': result['mutation_load'],
                        'mut_max': result['bases_mutated_in_motif'],
                        'odds_ratio': result['odds_ratio'],
                        'pvalue': result['pvalue']
                    })
                    pvals.append(result['pvalue'])

    qvalues = get_corrected_pvalues(pvals)
    for i, motif_dict in enumerate(motif_matches):
        motif_matches[i]['qvalue'] = qvalues[i]
        if motif_dict['mut_min'] == 0:
            continue
        if motif_dict['qvalue'] >= threshold:
            continue
        sig_motif_matches.append(motif_dict)

    return sig_motif_matches

def scanf_motif(custom_motif):
    """ recognize motif syntax like A[C>T]G and create a motif entry """
    m = re.search(
        r'([' + extended_nucleotides + ']*)\\[([' + nucleotides + '])>([' + extended_nucleotides + '])\\]([' + extended_nucleotides + ']*)',
        custom_motif.upper())
    if m:
        g = m.groups('')
        # print("GROUPS", m.group(1), m.group(2), m.group(3), m.group(4))
        entry = {}
        entry['logo'] = m.group(0)
        entry['motif'] = g[0] + g[1] + g[3]
        entry['position'] = len(g[0])
        entry['ref'] = g[1]
        entry['alt'] = g[2]
        if entry['ref'] == entry['alt']:
            return []
        entry['name'] = 'Custom motif'
        entry['references'] = ''
        return [entry, ]
    return []

def calculate_RR(ct):
    """
    Mutation is treatment
    No mutation is placebo

    :param ct: mutually exclusive counts of mutated matching motifs, matching mutations, matching motifs, and matching bases
    :return: enrichment or risk ratio
    """
    try:
        RR = ((ct.loc['mutation', 'motif'] / (ct.loc['mutation', 'motif'] + ct.loc['mutation', 'no motif'])) /
              (ct.loc['no mutation', 'motif'] / (ct.loc['no mutation', 'motif'] + ct.loc['no mutation', 'no motif'])))
    except ZeroDivisionError:
        RR = 0.0
    return RR

def calculate_RR_for_motif(ct):
    """
    Motif is treatment
    No motif is placebo

    :param ct: mutually exclusive counts of mutated matching motifs, matching mutations, matching motifs, and matching bases
    :return: enrichment or risk ratio
    """
    try:
        RR = ((ct.loc['mutation', 'motif'] / (ct.loc['mutation', 'motif'] + ct.loc['no mutation', 'motif'])) /
              (ct.loc['mutation', 'no motif'] / (ct.loc['mutation', 'no motif'] + ct.loc['no mutation', 'no motif'])))
    except ZeroDivisionError:
        RR = 0.0
    return RR

def calculate_OR(ct):
    """
    :param ct: mutually exclusive counts of mutated matching motifs, matching mutations, matching motifs, and matching bases
    :return: odds ratio
    """
    try:
        OR = (
            (ct.loc['mutation', 'motif'] / ct.loc['mutation', 'no motif']) /
            (ct.loc['no mutation', 'motif'] / ct.loc['no mutation', 'no motif']))
    except ZeroDivisionError:
        OR = 0.0
    return OR

def Haldane_correction(ct, pseudocount=0.5):
    """
    :param ct: mutually exclusive counts of mutated matching motifs, matching mutations, matching motifs, and matching bases
    :return: contigency table after Haldane correction is applied
    """
    """    apply Haldane correction (+ 0.5) if any of the values in the contingency table is zero """

    return ct + pseudocount if np.any(np.isclose(ct.to_numpy(), 0.0)) else ct

def calculate_mutation_load(N_mutations, enrichment):
    """
    Mutation load (minimum estimate) calculation following Gordenin et al protocol
    However, at this point motif matches are not filtered for p-value significance
    That's done in the end after multiple testing correction
    """
    mutation_load = 0.0
    if enrichment > 1.0:
        mutation_load = N_mutations * (enrichment - 1) / enrichment
    # elif p_value < p_value_threshold:   tests for enrichment depletion
    return mutation_load

def get_stats(ct, stat_type='fisher'):
    """
    Calculate Fisher and Chi2 test pvalues,
    :param ct: counts of mutated matching motifs, matching mutations, matching motifs, and matching bases
    :param stat_type: Type of pvalue (Fisher's ('fisher') or Chi-Square ('chi2'))
    :return: pvalue of the corresponding statistical test
    """
    p_val = 1.0

    if stat_type is None:
        stat_type = 'fisher'

    stat_type = stat_type.lower()

    acceptable_tests = ('fisher', 'chi2')
    if stat_type not in acceptable_tests:
        logger.warning('get_stats() can only calculate p-values for ' + str(acceptable_tests))

    if stat_type == 'fisher':
        try:
            p_val = stats.fisher_exact(ct, alternative="greater")[1]
            # if p_val > 0.05:
            #     p_val = stats.fisher_exact(ct, alternative="less")[1] #calculates if motif is underrepresented
        except ValueError:
            p_val = 1.0
    elif stat_type == 'chi2':
        try:
            p_val = stats.chi2_contingency(ct)[1]
        except ValueError:
            p_val = 1.0
    return p_val

def get_corrected_pvalues(p_values):
    qvalues = []
    if len(p_values):
        qvalues = multipletests(pvals=p_values, method='fdr_bh')[1]
    return qvalues

# @functools.lru_cache(maxsize=None)
def get_rev_comp_seq(sequence):
    """
    :param sequence: forward DNA sequence
    :return: reverse complimentary DNA sequence
    """
    # rev_comp_seq = "".join([complementary_nucleotide[i] for i in reversed(sequence)])
    cn = complementary_nucleotide
    return [(i[0], i[1], cn[i[2]], '-') for i in reversed(sequence)]

def mutated_base(mutation, ref, alt):
    """
    :param mutation: [(record.CHROM, record.POS, record.REF, record.ALT)]
    :param ref: list the nucleotide base pre-mutation
    :param alt: list the nucleotide base post-mutation
    :return: True if mutation matches the specified ref and alt
    """
    # makes sure single base substitution
    _, _, mut_ref, mut_alt = mutation
    if mut_alt and mut_ref and len(mut_ref) == 1 and len(mut_alt) == 1 and mut_ref != mut_alt:
        # mutation matches the substitution
        if mutation[2] in bases_dict[ref] and mutation[3] in bases_dict[alt]:
            return True

def find_matching_motifs(seq, motif, motif_position):
    """
    :param seq: DNA sequence
    :param motif: specified motif
    :param motif_position: position of mutated base in motif, 0-base numbering
    :return: generator of matching positions

    TODO: SLOW algorithm O(n * m). Need to create a suffix tree with regexp
    """
    # print("Looking for motif {} in {}, {}".format(motif, sequence, len(sequence) - len(motif)))
    for i in range(len(seq) - len(motif) + 1):
        # s = seq[i: i + len(motif)]
        # print(s)
        for j, c in enumerate(motif):
            if seq[i + j][2] not in bases_dict[c]:
                break
        else:
            yield seq[i + motif_position]

def find_matching_bases(seq, ref, motif, motif_position):
    """
    :param seq:
    :param ref:
    :param motif:
    :param motif_position:
    :return: bases that match mutations
    """
    for i in range(motif_position, len(seq) - (len(motif) - motif_position) + 1):
        # range excludes border of sequence that may be motifs that don't fit window size
        if seq[i][2] in bases_dict[ref]:
            yield seq[i]

def make_contingency_table(
        array=None,
        motif_mutation=None,
        no_motif_mutation=None,
        motif_no_mutation=None,
        no_motif_no_mutation=None):
    """ Make a 2x2 contingency table out of a numpy array or four integers"""

    if array is not None:
        assert isinstance(array, np.ndarray)
        assert array.shape == (2, 2)
    else:
        array = np.array([
            [motif_mutation, no_motif_mutation],
            [motif_no_mutation, no_motif_no_mutation]
        ])
    contingency_table = pd.DataFrame(array)
    contingency_table.columns = ["motif", "no motif"]
    contingency_table.index = ["mutation", "no mutation"]
    return contingency_table

def process_mutations(mutations, motif, motif_position, ref, alt, range_size, strand, stat_type=None):
    """
    :param mutations: mutations to be analyzed
    :param motif: specified motif to search for
    :param motif_position: location of mutation in motif, 0-base numbering from left of motif
    :param ref: base pre-mutation
    :param alt: base post-mutation
    :param range_size: how far in the motif to search for
    :param strand: strand motif should be searched on
    :param stat_type: type of pvalue: Fisher's (default) or Chi-Square
    :param dump_matches: an optional file handle to save all mutations matching the motif regardless of their significance
    :return: (results summary disctionary, data_dump with stored_data or None if dump_matches is None)
    """
    assert range_size >= 0
    assert len(ref) == 1
    assert len(alt) == 1
    assert 0 <= motif_position < len(motif)
    assert len(set(strand) - set("ATN")) == 0, "[process_mutations] only A, T, N allowed in strand parameter"

    matching_bases = set()
    matching_motifs = set()
    matching_mutated_motifs = set()
    matching_mutated_bases = set()

    # extra loop for sample in sample list
    for chrom, pos, transcript_strand, x, y, seq in mutations:
        # extract the longest sequence we would ever need (motif + range_size); range size = # bases outside mutation
        mutation = chrom, pos, x, y
        rev_seq = get_rev_comp_seq(seq)

        # assuming that all mutations are reported in '+' reference strand
        if strand == 'A' or (strand == 'T' and transcript_strand == '+') or (strand == 'N' and transcript_strand == '-'):
            # not mutated:
            for ref_match in find_matching_bases(seq, ref, motif, motif_position):
                matching_bases.add(ref_match[0:2])
            for motif_match in find_matching_motifs(seq, motif, motif_position):
                matching_motifs.add(motif_match[0:2])

            # mutated:
            if mutated_base(mutation, ref, alt):
                # m = (mutation[0], mutation[1], mutation[2], "+")
                matching_mutated_bases.add(mutation[0:2])

                context_of_mutation = seq[range_size - motif_position: range_size - motif_position + len(motif)]
                for motif_match in find_matching_motifs(context_of_mutation, motif, motif_position):
                    matching_mutated_motifs.add(motif_match[0:2])

        if strand == 'A' or (strand == 'T' and transcript_strand == '-') or (strand == 'N' and transcript_strand == '+'):
            # rev compl: not mutated:
            for ref_match in find_matching_bases(rev_seq, ref, motif, motif_position):
                matching_bases.add(ref_match[0:2])

            for motif_match in find_matching_motifs(rev_seq, motif, motif_position):
                matching_motifs.add(motif_match[0:2])

            # rev compl: mutated:
            if mutated_base(mutation, complementary_extended_nucleotide[ref], complementary_extended_nucleotide[alt]):
                # m = (mutation[0], mutation[1], mutation[2], "-")
                matching_mutated_bases.add(mutation[0:2])

                # rev comp:
                context_of_mutation = rev_seq[range_size - motif_position: range_size - motif_position + len(motif)]
                for motif_match in find_matching_motifs(context_of_mutation, motif, motif_position):
                    matching_mutated_motifs.add(motif_match[0:2])

    motif_mutation_count = len(matching_mutated_motifs)  # bases mutated in motif
    stat_mutation_count = len(matching_mutated_bases - matching_mutated_motifs)  # bases mutated not in motif
    stat_motif_count = len(matching_motifs - matching_mutated_motifs)  # bases not mutated in motif
    stat_ref_count = len(matching_bases - (matching_motifs | matching_mutated_bases))  # bases not mutated not in motif

    # number of A[T>G]T occurrences               motif_mutation_count
    # / number of [T>G] occurrences               stat_mutation_count + motif_mutation_count
    # ----------
    # number of ATT occurrences in DNA context    stat_motif_count
    # / number of T occurrences in DNA context    stat_ref_count + stat_motif_count

    contingency_table = make_contingency_table(
        motif_mutation=motif_mutation_count,
        no_motif_mutation=stat_mutation_count,
        motif_no_mutation=stat_motif_count,
        no_motif_no_mutation=stat_ref_count)

    # data={
    # "'{}>{}' mutation".format(ref, alt): [stat_mutation_count, motif_mutation_count],
    # "no '{}>{}' mutation".format(ref, alt): [stat_ref_count, stat_motif_count]},
    # index=("no '{}' motif".format(motif), "'{}' motif".format(motif)))
    logger.debug("\n" + contingency_table.to_string() + "\n")

    logger.debug("({} / ({} + {})  ) /  ({} / ({} + {}))".format(
        contingency_table.loc['mutation', 'motif'],
        contingency_table.loc['mutation', 'motif'],
        contingency_table.loc['mutation', 'no motif'],
        contingency_table.loc['no mutation', 'motif'],
        contingency_table.loc['no mutation', 'motif'],
        contingency_table.loc['no mutation', 'no motif']))

    contingency_table = Haldane_correction(contingency_table)

    enrichment = risk_ratio = calculate_RR(contingency_table)  # enrichment = risk ratio
    odds_ratio = calculate_OR(contingency_table)

    p_val = get_stats(contingency_table, stat_type)

    mut_load = calculate_mutation_load(motif_mutation_count, enrichment)

    result = {
        'enrichment': enrichment,  # AKA risk ratio
        'odds_ratio': odds_ratio,
        'mutation_load': math.ceil(mut_load),
        'pvalue': p_val,
        'bases_mutated_in_motif': motif_mutation_count,
        'bases_mutated_not_in_motif': stat_mutation_count,
        'bases_not_mutated_in_motif': stat_motif_count,
        'bases_not_mutated_not_in_motif': stat_ref_count,
        'total_mutations': len(mutations)
    }

    saved_matches = {
        'mutation_motif': matching_mutated_motifs
    }
    return result, saved_matches


import torch
import torch.nn as nn
from Descriptor import Descriptor
from Recovery_Submodule import R_t, Pyramid_maxout

class TR(nn.Module):
    # translucency recovery(TR) module
    def __init__(self, input_channel=3, beta=4, gamma=4):
        super(TR, self).__init__()
        self.D_t = Descriptor(input_channel, gamma)
        self.R_t = R_t(385, beta)

    def forward(self, x, **kwargs):
        f_t = self.D_t(x)
        y_, f_c, z_hat, a = self.R_t(x, f_t, **kwargs)
        return y_, f_c, z_hat, a

class TR_new(nn.Module):
    # A new translucency recovery(TR) module with two descriptors
    def __init__(self, input_channel=3, beta=4, gamma=4):
        super(TR_new, self).__init__()
        self.D_t_1 = Descriptor(input_channel, gamma)
        self.D_t_2 = Descriptor(input_channel, gamma)
        self.SE = Pyramid_maxout(385, 1, beta)
        self.AE = Pyramid_maxout(385, 3, beta)

    def forward(self, x, **kwargs):
        f_t_1 = self.D_t_1(x)
        z_hat = self.SE(f_t_1)
        z_hat[z_hat >= 1] = 1
        z_hat[z_hat <= 0] = 0
        z_hat_ = z_hat.detach()
        f_t_2 = self.D_t_2(x)
        a = self.AE(f_t_2)
        # yield estimated snow-free image y'
        y_ = (z_hat_ < 1) * (x - a * z_hat_) / (1 - z_hat_ + 1e-8) + (z_hat_ == 1) * x
        y_[y_ >= 1] = 1
        y_[y_ <= 0] = 0
        # yield feature map f_c
        f_c = torch.cat([y_, z_hat_, a], dim=1)
        return y_, f_c, z_hat, a

class TR_za(nn.Module):
    # A  translucency recovery(TR) module predict z\times a
    def __init__(self, input_channel=3, beta=4, gamma=4):
        super(TR_za, self).__init__()
        self.D_t = Descriptor(input_channel, gamma)
        self.SE = Pyramid_maxout(385, 1, beta)
        self.SAE = Pyramid_maxout(385, 3, beta)

    def forward(self, x, **kwargs):
        f_t = self.D_t(x)
        z_hat = self.SE(f_t)
        za = self.SAE(f_t)
        z_hat[z_hat >= 1] = 1
        z_hat[z_hat <= 0] = 0
        za[za >= 1] = 1
        za[za <= 0] = 0
        # yield estimated snow-free image y'
        y_ = (z_hat < 1) * (x - za) / (1 - z_hat + 1e-8) + (z_hat == 1) * x
        y_[y_ >= 1] = 1
        y_[y_ <= 0] = 0
        # yield feature map f_c
        f_c = torch.cat([y_, z_hat, za], dim=1)
        return y_, f_c, z_hat, za

class RG(nn.Module):
    # the residual generation (RG) module
    def __init__(self, input_channel=7, beta=4, gamma=4):
        super(RG, self).__init__()
        self.D_r = Descriptor(input_channel, gamma)
        block = []
        for i in range(beta):
            block.append(nn.Conv2d(385, 3, 2 * i + 1, 1, padding=i))
        self.conv_module = nn.ModuleList(block)
        self.activation = nn.Tanh()

    def forward(self, f_c):
        f_r = self.D_r(f_c)
        for i, module in enumerate(self.conv_module):
            if i == 0:
                r = module(f_r)
            else:
                r += r + module(f_r)
        r = self.activation(r)
        return r

class DesnowNet(nn.Module):
    # the DesnowNet
    def __init__(self, input_channel=3, beta=4, gamma=4, mode='original'):
        super(DesnowNet, self).__init__()
        if mode == 'original':
            self.TR = TR(input_channel, beta, gamma)
        elif mode == 'new_descriptor':
            self.TR = TR_new(input_channel, beta, gamma)
        elif mode == 'za':
            self.TR = TR_za(input_channel, beta, gamma)
        else:
            raise ValueError("Invalid architectural mode")
        self.RG = RG(beta=beta, gamma=gamma)

    def forward(self, x, **kwargs):
        y_, f_c, z_hat, a = self.TR(x, **kwargs)
        r = self.RG(f_c)
        y_hat = r + y_
        return y_hat, y_, z_hat, a

if __name__ == '__main__':
    device = 'cuda'
    net = DesnowNet().to(device)
    mask = torch.zeros([2, 1, 64, 64]).to(device)
    img = torch.zeros([2, 3, 64, 64]).to(device)
    y_hat, y_, z_hat, a = net(img, mask=mask)
    y_hat.mean().backward()
    print("finished")


from __future__ import division

# Our Backend for the App!
# Built with Flask

# Import Flask
import flask
import requests
import os
from flask import send_file

import re
import sys
# Create the application
app = flask.Flask(__name__)

# serving home.html
@app.route('/', methods=['GET'])
def serve_page():
    return flask.render_template('home.html')

# process query
@app.route('/process_query', methods=['POST'])
def process_query():
    data = flask.request.form  # is a dictionary
    input = data['user_input']
    input_in_list = input.split(' ')
    return flask.render_template('home.html', same=processInput(input_in_list), og=input)

def processInput(input_in_list):
    for s, i in enumerate(input_in_list):
        if "bye" in i.lower():
            input_in_list[s] = "static/bye.jpg"
        if "hello" in i.lower():
            input_in_list[s] = "static/hello.png"
        if "yes" in i.lower():
            input_in_list[s] = "static/yes.png"
        if "no" in i.lower():
            input_in_list[s] = "static/no.png"
        if "please" in i.lower():
            input_in_list[s] = "static/please.png"
        if "thanks" in i.lower():
            input_in_list[s] = "static/thanks.png"
        if "who" in i.lower():
            input_in_list[s] = "static/who.png"
        if "what" in i.lower():
            input_in_list[s] = "static/what.png"
        if "when" in i.lower():
            input_in_list[s] = "static/when.png"
        if "where" in i.lower():
            input_in_list[s] = "static/where.png"
        if "why" in i.lower():
            input_in_list[s] = "static/why.png"
        if "which" in i.lower():
            input_in_list[s] = "static/which.png"
        if "how" in i.lower():
            input_in_list[s] = "static/how.png"
    return input_in_list
def listen_print_loop(responses):
    """Iterates through server responses and prints them.

    The responses passed is a generator that will block until a response
    is provided by the server.

    Each response may contain multiple results, and each result may contain
    multiple alternatives; for details, see https://goo.gl/tjCPAU.  Here we
    print only the transcription for the top alternative of the top result.

    In this case, responses are provided for interim results as well. If the
    response is an interim one, print a line feed at the end of it, to allow
    the next result to overwrite it, until the response is a final one. For the
    final one, print a newline to preserve the finalized transcription.
    """
    num_chars_printed = 0
    for response in responses:
        if not response.results:
            continue

        # The `results` list is consecutive. For streaming, we only care about
        # the first result being considered, since once it's `is_final`, it
        # moves on to considering the next utterance.
        result = response.results[0]
        if not result.alternatives:
            continue

        # Display the transcription of the top alternative.
        transcript = result.alternatives[0].transcript

        # Display interim results, but with a carriage return at the end of the
        # line, so subsequent lines will overwrite them.
        #
        # If the previous result was longer than this one, we need to print
        # some extra spaces to overwrite the previous result
        overwrite_chars = ' ' * (num_chars_printed - len(transcript))

        if not result.is_final:
            sys.stdout.write(transcript + overwrite_chars + '\r')
            sys.stdout.flush()

            num_chars_printed = len(transcript)

        else:
            return flask.render_template('home.html', same=processInput("".join(transcript).split(" ")), og="".join(transcript))

            # Exit recognition if any of the transcribed phrases could be
            # one of our keywords.
            if re.search(r'\b(exit|quit)\b', transcript, re.I):
                print('Exiting..')
                break

            num_chars_printed = 0
@app.route('/speech', methods=['GET'])
def main():
    # See http://g.co/cloud/speech/docs/languages
    # for a list of supported languages.
    language_code = 'en-US'  # a BCP-47 language tag

if __name__ == '__main__':
    app.run(debug=True)


from langdetect import detect

def to_sentences(text):
    text = text.replace("\n", " ")
    sentences = [s + '.' for s in text.split(".") if s != ""]
    return sentences

def divide(text, input_size=5000):
    """
    Divide text into chunks of input_size

    Args:
        text (str): Text to be divided
        input_size (int): Size of each chunk
    """
    # short input_size if asian
    lang = detect(text)
    if lang in ['ko', 'ja', 'zh-cn', 'zh-tw', 'zh-hk']:
        input_size = min(input_size, 1300)

    # divide text by words
    text = to_sentences(text)
    result = []
    temp = ""
    for word in text:
        if len(temp + word) >= input_size:
            result.append(temp)
            temp = ""
        temp += word
    result.append(temp)
    return result


# coding: utf8

"""锁Lock 用于避免进程间对shared memory的争夺"""

import multiprocessing
import time

def job(val, num, lo):
    lo.acquire()   # 取得锁
    for _ in range(10):
        time.sleep(0.1)
        val.value += num
        print(val.value)
    lo.release()   # 释放锁

def multicore():
    lo = multiprocessing.Lock()   # 创建锁对象

    share_memory = multiprocessing.Value("i", 0)   # 初始化为0的一个共享int块变量

    res1 = multiprocessing.Process(target=job, args=(share_memory, 1, lo))
    res2 = multiprocessing.Process(target=job, args=(share_memory, 9, lo))
    res1.start()
    res2.start()
    res1.join()
    res2.join()

if __name__ == "__main__":
    multicore()


from RepSys import Error, config
from RepSys.util import execcmd
from RepSys.VCS import *
from os.path import basename, dirname
from os import chdir, getcwd
import sys
import re
import time
from xml.etree import cElementTree as ElementTree
import subprocess

class GITLogEntry(VCSLogEntry):
    def __init__(self, revision, author, date):
        VCSLogEntry.__init__(self, revision, author, data)

class GIT(VCS):
    def __init__(self):
        VCS.__init__(self)
        self.vcs_name = "git"
        self.vcs_command = config.get("global", "git-command", "git")
        self.vcs_supports['clone'] = True
        self.env_defaults = {"GIT_SSH": self.vcs_wrapper}

    def clone(self, url, targetpath, **kwargs):
        if url.split(':')[0].find("svn") < 0:
            return VCS.clone(self, url, targetpath, **kwargs)
        else:
            # To speed things up on huge repositories, we'll just grab all the
            # revision numbers for this specific directory and grab these only
            # in stead of having to go through each and every revision...
            retval, result = execcmd("svn log --stop-on-copy --xml %s" % url)
            if retval:
                return retval
            parser = ElementTree.XMLTreeBuilder()
            result = "".join(result.split("\n"))
            parser.feed(result)
            log = parser.close()
            logentries = log.getiterator("logentry")
            revisions = []
            topurl = dirname(url)
            trunk = basename(url)
            tags = "releases"
            execcmd("git svn init %s --trunk=%s --tags=%s %s" % (topurl, trunk, tags, targetpath), show=True)
            chdir(targetpath)
            for entry in logentries:
                revisions.append(entry.attrib["revision"])
            while revisions:
                execcmd("git svn fetch -r%d" % int(revisions.pop()), show=True)

            cmd = ["svn", "rebase"]
            return self._execVcs_success(*cmd, **kwargs)

class SVNLook(VCSLook):
    def __init__(self, repospath, txn=None, rev=None):
        VCSLook.__init__(self, repospath, txn, rev)

# vim:et:ts=4:sw=4


from operator import add
from itertools import chain, combinations
from functools import reduce

import math
import numpy as np
from scipy import ndimage
from tkinter import *

class GF2(object):
    def __init__(self, a=0):
        self.value = int(a) & 1

    def __add__(self, rhs):
        return GF2(self.value + GF2(rhs).value)

    def __mul__(self, rhs):
        return GF2(self.value * GF2(rhs).value)

    def __sub__(self, rhs):
        return GF2(self.value - GF2(rhs).value)

    def __truediv__(self, rhs):
        return GF2(self.value / GF2(rhs).value)

    def __repr__(self):
        return str(self.value)

    def __eq__(self, rhs):
        if isinstance(rhs, GF2):
            return self.value == rhs.value
        return self.value == rhs

    def __le__(self, rhs):
        if isinstance(rhs, GF2):
            return self.value <= rhs.value
        return self.value <= rhs

    def __lt__(self, rhs):
        if isinstance(rhs, GF2):
            return self.value < rhs.value
        return self.value < rhs

    def __int__(self):
        return self.value

    def __long__(self):
        return self.value

GF2array = np.vectorize(GF2)

def gjel(A):
    nulldim = 0
    for i, row1 in enumerate(A):
        pivot = A[i:, i].argmax() + i
        if A[pivot, i] == 0:
            nulldim = len(A) - i
            break
        new_row = A[pivot] / A[pivot, i]
        A[pivot] = A[i]
        row1[:] = new_row

        for j, row2 in enumerate(A):
            if j == i:
                continue
            row2[:] -= new_row*A[j, i]
    return A, nulldim

def GF2inv(A):
    n = len(A)
    assert n == A.shape[1], "Matrix must be square"

    A = np.hstack([A, np.eye(n)])
    B, nulldim = gjel(GF2array(A))

    inverse = np.int_(B[-n:, -n:])
    E = B[:n, :n]
    null_vectors = []
    if nulldim > 0:
        null_vectors = E[:, -nulldim:]
        null_vectors[-nulldim:, :] = GF2array(np.eye(nulldim))
        null_vectors = np.int_(null_vectors.T)

    return inverse, null_vectors

def lightsoutbase(n):
    a = np.eye(n*n)
    a = np.reshape(a, (n*n, n, n))
    a = np.array(list(map(ndimage.binary_dilation, a)))
    return np.reshape(a, (n*n, n*n))

def powerset(iterable):
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

class LightsOut(object):
    def __init__(self, size=5):
        self.n = size
        self.base = lightsoutbase(self.n)
        self.invbase, self.null_vectors = GF2inv(self.base)

    def solve(self, b):
        b = np.asarray(b)
        assert b.shape[0] == b.shape[1] == self.n, "incompatible shape"

        if not self.issolvable(b):
            raise ValueError("The given setup is not solvable")

        first = np.dot(self.invbase, b.ravel()) & 1

        solutions = [(first + reduce(add, nvs, 0)) & 1 for nvs in powerset(self.null_vectors)]
        final = min(solutions, key=lambda x: x.sum())
        return np.reshape(final, (self.n, self.n))

    def issolvable(self, b):
        b = np.asarray(b)
        assert b.shape[0] == b.shape[1] == self.n, "incompatible shape"
        b = b.ravel()
        p = [np.dot(x, b) & 1 for x in self.null_vectors]
        return not any(p)

def text_to_mat(gridtxt, invert=True):
    gridlist = [int(s) for s in list(gridtxt)]

    shape = np.sqrt(len(gridlist))
    if shape%1 != 0:
        print("input matrix is not square.")
        return 1

    shape = int(shape)

    matlist = [gridlist[i: i+shape] for i in range(0, len(gridlist), shape)]
    mat = np.array(matlist)
    if invert:
        mat = 1-mat

    return mat

def mat_to_text(mat, invert=False):
    s = ""
    for i in mat:
        for j in i:
            if invert:
                s += str(1-j)
            else:
                s += str(j)
    return s

def text_solver(gridtxt):
    mat_inv = text_to_mat(gridtxt, True)
    if type(mat_inv) == int:
        return 1
    lo = LightsOut(3)
    try:
        bsol = lo.solve(mat_inv)
    except:
        print("Error in determining solution")
        return 1

    return bsol

master = Tk()
master_gridtxt = StringVar(value="000000000")
master.title("DVa's Puzzle Solver")
master.geometry("400x115")
master.resizable(width=False, height=False)

check_size = 25
check_on = PhotoImage(width=check_size, height=check_size)
check_off = PhotoImage(width=check_size, height=check_size)
check_on.put(("green"), to=(0,0,check_size,check_size))
check_off.put(("red"), to=(0,0,check_size,check_size))

label_text = StringVar()

def update_gridtxt():
    b_solve['state'] = NORMAL
    master_gridtxt.set("")
    for i in range(9):
        s = str(globals()[f"b_state{i}"].get())
        master_gridtxt.set(master_gridtxt.get() + s)

def reset_boxes():
    for i in range(9):
        globals()[f"b_state{i}"].set(0)
    label_text.set("")
    b_solve['state'] = NORMAL

def final_wrapper(gridtxt):
    mat = text_solver(gridtxt)
    gridtxt_final = mat_to_text(mat)
    reset_boxes()
    for idx, i in enumerate(gridtxt_final):
        if i == "1":
            globals()[f"b{idx}"].select()
    b_solve['state'] = DISABLED
    label_text.set("Solved. Shoot the lamps marked with green boxes.")

for i in range(9):
    j = i+1
    col = i%3
    row = math.ceil(j/3)
    globals()[f"b_state{i}"] = IntVar()
    globals()[f"b{i}"] = Checkbutton(master, variable=globals()[f"b_state{i}"],
                                    image=check_off, selectimage=check_on, indicatoron=False,
                                    onvalue=1, offvalue=0, command=update_gridtxt)
    globals()[f"b{i}"].grid(row=row, column=col, padx=1, pady=1)

b_solve = Button(master, text="Solve", command=lambda:final_wrapper(master_gridtxt.get()), anchor="w")
b_solve.grid(row=1, column=4, padx=1, pady=1, sticky="w")

b_reset = Button(master, text="Reset", command=reset_boxes, anchor="w")
b_reset.grid(row=2, column=4, padx=1, pady=1, sticky="w")

lbl = Label(master, textvariable=label_text, anchor="w")
lbl.grid(row=3, column=4, padx=1, pady=1)

master.mainloop()


"""
include packages
"""

from settings import *
import sqlite3
import discord
from discord import app_commands
import sys
import signal
import deepl
from typing import Optional

from lib import vote as vt
from lib import deepl as dl

"""
Global variables
"""

connection : sqlite3.Connection       = sqlite3.connect(DATABASE)
intents    : discord.Intents          = discord.Intents.all()
client     : discord.Client           = discord.Client(intents=intents)
tree       : app_commands.CommandTree = app_commands.CommandTree(client=client)

"""
Setup
"""

vt.init(connection)
with open(DEEPL_API_KEY) as f:
    dl_translator = dl.LoggingTranslator(f.read(), connection=connection)

"""
Commands
"""

@tree.command(
    name='test',
    description='This is a test'
)
@app_commands.describe(
    message='Your message',
    hello='Hello message'
)
@app_commands.rename(
    message='text'
)
@app_commands.choices(
    hello=[
        app_commands.Choice(name='Good Morning', value='Good Morning'),
        app_commands.Choice(name='Good Afternoon', value='Good Afternoon'),
        app_commands.Choice(name='Good Evening', value='Good Evening'),
        app_commands.Choice(name='Good Night', value='Good Night')
    ]
)
@app_commands.guild_only
async def test(ctx: discord.Interaction, message: str, hello: str):
    await ctx.response.send_message('This is a test message.\nYour message is ...\n'+message+'\n'+hello)

@tree.command(
    name='vote',
    description='投票を行う'
)
@app_commands.describe(
    title='投票のお題',
    visible='投票結果を表示する際に投票先を表示する',
)
@app_commands.choices(
    visible=[
        app_commands.Choice(name='表示する', value='Yes'),
        app_commands.Choice(name='表示しない', value='No')
    ]
)
@app_commands.guild_only
async def vote_with_any_choices(ctx: discord.Interaction, title: str, visible: str='Yes'):
    try:
        await ctx.response.send_modal(vt.VoteModal(title=title, visible=visible))
    except Exception as e:
        print(e.with_traceback(sys.exc_info()[2]))

@tree.command(
    name='deepl',
    description='DeepL翻訳を使用してテキストを翻訳する（default: Auto→JP）'
)
@app_commands.describe(
    text='翻訳するテキスト',
    source_language='翻訳前の言語（default: 自動検出）',
    target_language='翻訳後の言語（default: 日本語）'
)
@app_commands.choices(
    source_language=dl.DcLanguageList.SOURCE,
    target_language=dl.DcLanguageList.TARGET
)
async def deepl_translate(ctx: discord.Interaction, text: str, source_language: Optional[str] = None, target_language: str = deepl.Language.JAPANESE):
    try:
        if source_language == "":
            source_language = None

        translated_text = dl_translator.translate_text(
            ctx=ctx,
            text=text,
            source_lang=source_language,
            target_lang=target_language
        )

        t = "> " + text.replace("\n", "\n> ") + "\n"

        await ctx.response.send_message(t + translated_text.text)

    except Exception as e:
        print(e.with_traceback(sys.exc_info()[2]))

"""
Events
"""

@client.event
async def on_ready():
    print('Bot is ready')
    await tree.sync()

"""
Cleanups
"""

def cleanup():
    global connection
    connection.close()

def signal_handler(signum, frame):
    cleanup()
    sys.exit(1)

if __name__ == '__main__':
    signal.signal(signal.SIGTERM, signal_handler)
    try:
        with open(TOKEN) as f:
            client.run(f.read())
    finally:
        signal.signal(signal.SIGTERM, signal.SIG_DFL)
        cleanup()


import pytest

from base.client_base import TestcaseBase
from common import common_func as cf
from common import common_type as ct
from common.common_type import CaseLabel
from utils.util_log import test_log as log

# customer rg
rg_name_0 = "RG_0"
rg_name_1 = "RG_1"

# coll name
coll_name_1 = "ResourceGroup_111"
coll_name_2 = "ResourceGroup_222"

# resource group info of 4 qns
resource_group_info = [
    {"name": rg_name_0, "available_node": 1, "capacity": 1, "loaded_replica": {coll_name_1: 1}},
    {"name": rg_name_1, "available_node": 1, "capacity": 1, "loaded_replica": {coll_name_1: 1}},
    {"name": ct.default_resource_group_name, "available_node": 2,
     "capacity": ct.default_resource_group_capacity, "loaded_replica": {coll_name_2: 2}}
]

class TestChaosRG(TestcaseBase):
    """ Test case of end to end"""

    def teardown_method(self, method):
        log.info(("*" * 35) + " teardown " + ("*" * 35))
        log.info("[teardown_method] Start teardown test case %s..." %
                 method.__name__)
        log.info("skip drop collection")

    @pytest.mark.tags(CaseLabel.L3)
    def test_milvus_resource_group(self):
        nb = 10000
        # collection rg map
        collection_rg_map = {
            coll_name_1: {"resource_groups": [rg_name_0, rg_name_1], "replica_number": 2},
            coll_name_2: {"resource_groups": [ct.default_resource_group_name], "replica_number": 2}
        }

        self._connect()
        # create RG_0, RG_1, transfer 1 node to RG_0, 1 node to RG_1

        for rg_info in resource_group_info:
            rg_name = rg_info["name"]
            if rg_name != ct.default_resource_group_name:
                _, create_rg_res = self.utility_wrap.create_resource_group(rg_name)
                assert create_rg_res
                log.info(f"[ResourceGroup] Create rg {rg_name} done")
                self.utility_wrap.transfer_node(source=ct.default_resource_group_name, target=rg_name,
                                                num_node=rg_info["available_node"])
                log.info(
                    f'[ResourceGroup] Transfer {rg_info["available_node"]} nodes from {ct.default_resource_group_name} to {rg_name} done')

        # verify RGs
        resource_groups, _ = self.utility_wrap.list_resource_groups()
        assert len(resource_groups) == len(resource_group_info)
        assert all([rg_info["name"] in resource_groups for rg_info in resource_group_info])
        for rg_info in resource_group_info:
            rg_info = {"name": rg_info["name"],
                       "capacity": rg_info["capacity"],
                       "num_available_node": rg_info["available_node"],
                       "num_loaded_replica": {},
                       "num_outgoing_node": {},
                       "num_incoming_node": {}
                       }
            desc_rg_info, _ = self.utility_wrap.describe_resource_group(name=rg_info["name"],
                                                                        check_task=ct.CheckTasks.check_rg_property,
                                                                        check_items=rg_info)
            log.info(f'[ResourceGroup] Rg of {rg_info["name"]} info is: {desc_rg_info}')

        # prepare collection C1, C2
        # create
        data = cf.gen_default_dataframe_data(nb=nb)
        index_params = {"index_type": "HNSW", "metric_type": "L2", "params": {"M": 48, "efConstruction": 500}}

        for coll_name in coll_name_1, coll_name_2:
            # create
            collection_w = self.init_collection_wrap(name=coll_name, active_trace=True)
            log.info(f"create collection {collection_w.name} done")
            entities = collection_w.num_entities

            # insert
            _, res = collection_w.insert(data)
            assert res
            log.info(f"insert {nb} entities done")

            # flush
            _, check_result = collection_w.flush(timeout=180)
            assert check_result
            assert collection_w.num_entities == nb + entities
            entities = collection_w.num_entities
            log.info(f"flush done with entities: {entities}")

            # index
            index, _ = collection_w.create_index(field_name=ct.default_float_vec_field_name,
                                                 index_params=index_params,
                                                 index_name=cf.gen_unique_str())
            index, _ = collection_w.create_index(field_name=ct.default_string_field_name,
                                                 index_params={},
                                                 index_name=cf.gen_unique_str())
            index_infos = [index.to_dict() for index in collection_w.indexes]
            log.info(f"index info: {index_infos}")

            # load coll_rg_a, 2 replicas -> RG_0, RG_1
            # load coll_rg_b, 2 replicas -> default_RG
            collection_w.load(replica_number=collection_rg_map[coll_name]["replica_number"],
                              _resource_groups=collection_rg_map[coll_name]["resource_groups"])

            # show query segment info
            segment_info, _ = self.utility_wrap.get_query_segment_info(collection_w.name)
            log.info(f"{collection_w.name} segment info: {segment_info}")

            # show replicas info
            replicas, _ = collection_w.get_replicas()
            log.info(f"{collection_w.name} replica info: {replicas}")

            # search
            search_vectors = cf.gen_vectors(ct.default_nq, ct.default_dim)
            search_params = {"metric_type": "L2", "params": {"ef": 64}}
            search_res, _ = collection_w.search(data=search_vectors,
                                                anns_field=ct.default_float_vec_field_name,
                                                param=search_params, limit=ct.default_limit, expr="int64 >= 0")
            assert len(search_res) == ct.default_nq
            assert len(search_res[0]) == ct.default_limit

            # query and delete
            term_expr = f'{ct.default_int64_field_name} < 100'
            query_res, _ = collection_w.query(term_expr)
            assert len(query_res) == 100

            delete_expr = f'{ct.default_int64_field_name} in {[i for i in range(100)]}'
            collection_w.delete(delete_expr)
            collection_w.query(term_expr, check_task=ct.CheckTasks.check_query_empty)

        # verify rg replica info
        for rg_info in resource_group_info:
            rg_info = {"name": rg_info["name"],
                       "capacity": rg_info["capacity"],
                       "num_available_node": rg_info["available_node"],
                       "num_loaded_replica": rg_info["loaded_replica"],
                       "num_outgoing_node": {},
                       "num_incoming_node": {}
                       }
            desc_rg_info_2, _ = self.utility_wrap.describe_resource_group(name=rg_info["name"],
                                                                          check_task=ct.CheckTasks.check_rg_property,
                                                                          check_items=rg_info)
            log.info(f'[ResourceGroup] Rg of {rg_info["name"]} info is: {desc_rg_info_2}')

    @pytest.mark.tags(CaseLabel.L3)
    def test_verify_milvus_resource_group(self):
        self._connect()

        # verify collection exist
        all_collections, _ = self.utility_wrap.list_collections()
        assert all(coll_name in all_collections for coll_name in [coll_name_1, coll_name_2])

        # verify resource groups
        for rg_info in resource_group_info:
            rg_info = {"name": rg_info["name"],
                       "capacity": rg_info["capacity"],
                       "num_available_node": rg_info["available_node"],
                       "num_loaded_replica": rg_info["loaded_replica"],
                       "num_outgoing_node": {},
                       "num_incoming_node": {}
                       }
            desc_rg_info, _ = self.utility_wrap.describe_resource_group(name=rg_info["name"],
                                                                        check_task=ct.CheckTasks.check_rg_property,
                                                                        check_items=rg_info)
            log.info(f'[ResourceGroup] Rg of {rg_info["name"]} info is: {desc_rg_info}')

        # search
        for coll_name in coll_name_2, coll_name_1:
            # get query segment info
            segment, _ = self.utility_wrap.get_query_segment_info(coll_name)
            log.info(f"{coll_name} query segment info: {segment}")

            # get replicas
            collection_w = self.init_collection_wrap(name=coll_name, active_trace=True)
            replicas, _ = collection_w.get_replicas(check_task=ct.CheckTasks.check_nothing)
            log.info(f"{coll_name} replicas: {replicas}")

            # search
            for i in range(100):
                search_vectors = cf.gen_vectors(ct.default_nq, ct.default_dim)
                search_params = {"metric_type": "L2", "params": {"ef": 64}}
                search_res, _ = collection_w.search(data=search_vectors,
                                                    anns_field=ct.default_float_vec_field_name,
                                                    param=search_params, limit=ct.default_limit, expr="int64 >= 0")
                assert len(search_res) == ct.default_nq
                assert len(search_res[0]) == ct.default_limit

            # show query segment info finally
            segment_2, _ = self.utility_wrap.get_query_segment_info(coll_name)
            log.info(f"{coll_name} query segment info: {segment_2}")

            # show replicas finally
            replicas_2, _ = collection_w.get_replicas()
            log.info(f"{coll_name} replicas: {replicas_2}")


from Film import Film
from Forgalmazo import Forgalmazo
import datetime

def fajl_beolvas():
    filmek = []
    fp = open('nyitohetvege.txt', 'r', encoding='utf-8')
    lines = fp.readlines()
    fp.close()
    for line in lines[1:]:
        n_line = line.rstrip()
        (eredetiCim, magyarCim,bemutato,forgalmazo,
            bevel,latogato) = n_line.split(';')
        film = Film(eredetiCim, magyarCim,bemutato,forgalmazo,
            bevel,latogato)
        filmek.append(film)
    return filmek

def feladat3(filmek):
    print('3. feladat: Filmek száma az állományban: ', end='')
    filmek_szama = len(filmek)
    print(filmek_szama, 'db')

def feladat4(filmek):
    print('4. feladat: UIP Duna Film forgalmazó 1. ', end='')
    print('hetes bevételeinek összege: ', end='')
    osszeg=0
    for film in filmek:
        if film.forgalmazo == 'UIP':
            osszeg+=int(film.bevel)
    print("{:,}".format(osszeg), 'Ft')

def feladat5(filmek):
    print('5. feladat: Legtöbb látogató az első héten:')
    max_film = filmek[0]
    for film in filmek:
        if int(film.latogato) > int(max_film.latogato):
            max_film = film

    print('\tEredeti cím:', max_film.eredetiCim)
    print('\tMagyar cím:', max_film.magyarCim)
    print('\tForgalmazó:', max_film.forgalmazo)
    print('\tBevétel az első héten:', max_film.bevel, 'Ft')
    print('\tLátogatók száma:', max_film.latogato, 'fő')

def tartalmazTeszt(eredetiCim, magyarCim):
    eredetiTartalmaz=False
    if 'W' in eredetiCim:
        eredetiTartalmaz=True
    if 'w' in eredetiCim:
        eredetiTartalmaz=True
    magyarTartalmazza=False
    if 'W' in magyarCim:
        magyarTartalmazza=True
    if 'w' in magyarCim:
        magyarTartalmazza=True

    if eredetiTartalmaz and magyarTartalmazza:
        return True
    else:
        return False

def feladat6(filmek):
    print('6. feladat: ', end='')

    n=len(filmek)
    i=0
    while (i<n and
        not tartalmazTeszt(filmek[i].eredetiCim, filmek[i].magyarCim)):
        i+=1
    if i<n:
        print("Ilyen film volt!")
    else:
        print("Ilyen film nem volt!")

def forgalmazoTeszt(forgalmazok, forgalmazo):
    n=len(forgalmazok)
    i=0
    while i<n and forgalmazok[i].nev != forgalmazo:
        i+=1
    if i<n:
        return True
    else:
        return False

def feladat7(filmek):
    mezonevek = 'forgalmazo;filmekSzama\n'
    forgalmazok = []
    for film in filmek:
        if not forgalmazoTeszt(forgalmazok, film.forgalmazo):
            forgalmazo = Forgalmazo(film.forgalmazo)
            forgalmazok.append(forgalmazo)
        else:
            n=len(forgalmazok)
            for i in range(0, n):
                if forgalmazok[i].nev == film.forgalmazo:
                    forgalmazok[i].filmek += 1
    fp = open('stat.csv', 'w', encoding='utf-8')
    fp.write(mezonevek)
    for forgalmazo in forgalmazok:
        if forgalmazo.filmek>1:
            fp.write(forgalmazo.nev + ';' + str(forgalmazo.filmek) + '\n')
    fp.close()

def feladat8(filmek):
    print('8. feladat: A leghosszabb időszak két ', end='')
    print('InterCom-os bemutató között: ', end='')

    elsoBemutato = None
    max_kul = 0
    for film in filmek:
        if film.forgalmazo == 'InterCom':
            isoDatum = film.bemutato.replace('.', '-')
            if elsoBemutato == None:
                elsoBemutato=datetime.date.fromisoformat(isoDatum)
            else:
                kovBemutato = datetime.date.fromisoformat(isoDatum)
                kul = kovBemutato - elsoBemutato
                if kul.total_seconds() > max_kul:
                    max_kul=kul.total_seconds()
                elsoBemutato = kovBemutato
    nap = max_kul  // (24 * 3600)
    print(int(nap), 'nap')

filmek = fajl_beolvas()
# ~ feladat3(filmek)
# ~ feladat4(filmek)
# ~ feladat5(filmek)
# ~ feladat6(filmek)
# ~ feladat7(filmek)
feladat8(filmek)


import matplotlib.pyplot as plt
import numpy as np

# ~~~ DEFINE DATA ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
np.random.seed(1337)

n = 1000000
x = np.random.standard_normal(n)
y = x + .5 * np.random.standard_normal(n)
hist, xedges, yedges = np.histogram2d(x, y, bins=100, density=True)
hist[hist == 0] = None

t = np.linspace(0, 3 * np.pi, 1000)
style = 'mpl'

# ~~~ PLOT LINEAR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fig, ax = plt.subplots()
plt.plot(t, np.sin(t), t, np.cos(t), t, 2 * np.cos(t))
plt.tight_layout()
plt.savefig(f'gallery/{style}_plot.png')
plt.close()

# legend
fig, ax = plt.subplots()
plt.plot(t, np.sin(t), label='sin')
plt.plot(t, np.cos(t), label='cos')
plt.plot(t, 2 * np.cos(t), label='2cos')
plt.legend(title='function:')
plt.tight_layout()
plt.savefig(f'gallery/{style}_plot_legend.png')
plt.close()

# mulitple subgallery
fig, axs = plt.subplots(3, 1, sharex=True, gridspec_kw={'hspace': 0.000})
axs[0].plot(t, np.sin(t))
axs[1].plot(t[::20], np.cos(t[::20]), 'o-')
axs[2].plot(t, 2 * np.cos(t), t, np.sin(t))
plt.tight_layout()
plt.savefig(f'gallery/{style}_plot_multiple.png')
plt.close()

# ~~~ PLOT IMSHOW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fig, ax = plt.subplots()
plt.imshow(hist)
plt.tight_layout()
plt.savefig(f'gallery/{style}_imshow.png')
plt.close()

# cbar
fig, ax = plt.subplots()
im = plt.imshow(hist)
plt.colorbar(im)
plt.tight_layout()
plt.savefig(f'gallery/{style}_imshow_cbar.png')
plt.close()


from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple
from uuid import uuid4

import pygame

from .clock import Clock, clock
from .keyboard import Keyboard
from .screen import Screen
from .utils.event_dispatcher import EventDispatcher

if TYPE_CHECKING:
    from .application import Application

class Scene(EventDispatcher):
    """
    The idea and the original code was taken from [EzPyGame](https://github.com/Mahi/EzPyGame)

    An isolated scene which can be ran by an application.

    Create your own scene by subclassing and overriding any methods.

    Example:
    ```
    class Menu(Scene):

        def __init__(self):
            self.font = pygame.font.Font(...)

        def on_enter(self, previous_scene):
            self.title = 'Main Menu'
            self.resolution = (640, 480)
            self.update_rate = 30

        def draw(self, screen):
            pygame.draw.rect(...)
            text = self.font.render(...)
            screen.blit(text, ...)

        def handle_event(self, event):
            if event.type == pygame.MOUSEBUTTONUP:
                if event.button == 1:
                    game_size = self._get_game_size(event.pos)
                    self.change_scene(Game(game_size))

        def _get_game_size(self, mouse_pos_upon_click):
            ...

    class Game(pgz.Scene):
        title = 'The Game!'
        resolution = (1280, 720)
        update_rate = 60

        def __init__(self, size):
            super().__init__()
            self.size = size
            self.player = ...
            ...

        def on_enter(self, previous_scene):
            super().on_enter(previous_scene)
            self.previous_scene = previous_scene

        def draw(self, screen):
            self.player.draw(screen)
            for enemy in self.enemies:
                ...

        def update(self, dt):
            self.player.move(dt)
            ...
            if self.player.is_dead():
                self.change_scene(self.previous_scene)
            elif self.player_won():
                self.change_scene(...)

        def handle_event(self, event):
            ...  # Player movement etc.
    ```

    The above two classes use different approaches for changing
    the application's settings when the scene is entered:

    1. Manually set them in `on_enter`, as seen in `Menu`
    2. Use class variables, as I did with `Game`

    When using class variables (2), you can leave out any setting
    (defaults to `None`) to not override that particular setting.
    If you override `on_enter` in the subclass, you must call
    `super().on_enter(previous_scene)` to use the class variables.

    These settings can further be overridden in individual instances:

    ```
        my_scene0 = MyScene()
        my_scene0.resolution = (1280, 720)
        my_scene1 = MyScene(title='My Second Awesome Scene')
    ```

    Example:
    Shortcuts foe event gandling while `Scene` subclassing.
    ```
    def on_mouse_up(self, pos, button):
        # Override this for easier events handling.
        pass

    def on_mouse_down(self, pos, button):
        # Override this for easier events handling.
        pass

    def on_mouse_move(self, pos):
        # Override this for easier events handling.
        pass

    def on_key_down(self, key):
        # Override this for easier events handling.
        pass

    def on_key_up(self, key):
        # Override this for easier events handling.
        pass
    ```
    """

    _title: Optional[str] = None
    _resolution: Optional[Tuple[int, int]] = None
    _update_rate: Optional[int] = None

    def __init__(self, title: Optional[str] = None, resolution=None, update_rate: Optional[int] = None) -> None:
        self._application: Optional["Application"] = None
        if title is not None:
            self._title = title
        if resolution is not None:
            self._resolution = resolution
        if update_rate is not None:
            self._update_rate = update_rate

        self._keyboard = Keyboard()

        # Client data is the data was provided by the client during the handshake: it's usually stuff like player name, avatar, etc
        self._client_data: Dict[str, Any] = {}

        # The scene UUID is used for communication
        self._scene_uuid = str(uuid4())

    @property
    def scene_uuid(self) -> str:
        """
        Get scene UUID.
        """
        return self._scene_uuid

    def set_client_data(self, client_data: Dict[str, Any]) -> None:
        self._client_data = client_data

    @property
    def client_data(self) -> Dict[str, Any]:
        """
        Get data provided by client side.
        """
        return self._client_data

    def change_scene(self, new_scene: Optional["Scene"]) -> None:
        if not self._application:
            raise Exception("Application was not configured properly.")
        self._application.change_scene(new_scene)

    @property
    def title(self) -> str:
        """Get application title

        Returns:
            str: application title
        """
        if not self._application:
            raise Exception("Application was not configured properly.")
        return self._application.title

    @title.setter
    def title(self, value: str) -> None:
        """Change application title

        Args:
            value (str): application title to set
        """
        if not self._application:
            print("Warning: application was not configured - 'title' setting was ignored")
            return
        self._application.title = value

    @property
    def resolution(self) -> Tuple[int, int]:
        """Get application screen resolution

        Returns:
            Tuple[int, int]: application screen resolution
        """
        if not self._application:
            raise Exception("Application was not configured properly.")
        return self._application.resolution

    @resolution.setter
    def resolution(self, value: Tuple[int, int]) -> None:
        """Change application screen resolution

        Args:
            value (Tuple[int, int]): application screen resolution to use
        """
        if not self._application:
            print("Warning: application was not configured - 'resolution' setting was ignored")
            return
        self._application.resolution = value

    @property
    def update_rate(self) -> int:
        """Get application update rate

        Returns:
            int: application update rate
        """
        if not self._application:
            raise Exception("Application was not configured properly.")
        return self._application.update_rate

    @update_rate.setter
    def update_rate(self, value: int) -> None:
        """Change application update rate

        Args:
            value (int): application update rate to set
        """
        if not self._application:
            print("Warning: application was not configured - 'update_rate' setting was ignored")
            return
        self._application.update_rate = value

    @property
    def clock(self) -> Clock:
        """
        Get `Clock` object.

        Actually returns the global clock object.

        Returns:
            Clock: clock object
        """
        return clock

    @property
    def keyboard(self) -> Keyboard:
        """
        Get `Keyboard` object.

        Returns:
            Keyboard: keyboard object
        """
        return self._keyboard

    def draw(self, screen: Screen) -> None:
        """
        Override this with the scene drawing.

         Args:
            screen (Screen): screen to draw the scene on
        """

    def update(self, dt: float) -> None:
        """
        Override this with the scene update tick.

        Args:
            dt (float): time in milliseconds since the last update
        """

    def handle_event(self, event: pygame.event.Event) -> None:
        """
        Override this to handle an event in the scene.

        All of `pygame`'s events are sent here, so filtering
        should be applied manually in the subclass.

        Args:
            event (pygame.event.Event): event to handle
        """

        if event.type == pygame.KEYDOWN:
            self._keyboard._press(event.key)
        elif event.type == pygame.KEYUP:
            self._keyboard._release(event.key)

    def on_enter(self, previous_scene: Optional["Scene"]) -> None:
        """
        Override this to initialize upon scene entering.

        If you override this method and want to use class variables
        to change the application's settings, you must call
        ``super().on_enter(previous_scene)`` in the subclass.

        Args:
            previous_scene (Optional[Scene]): previous scene was running
        """
        for attr in ("_title", "_resolution", "_update_rate"):
            value = getattr(self, attr)
            if value is not None:
                if self._application is None:
                    print(f"Warning: application was not configured - '{attr}' setting was ignored")
                    continue
                setattr(self._application, attr.lower(), value)

        # Set event dispatcher
        self.load_handlers()

    def on_exit(self, next_scene: Optional["Scene"]) -> None:
        """
        Override this to deinitialize upon scene exiting.

        Args:
            next_scene (Optional[Scene]): next scene to run
        """


"""!

@brief CCORE Wrapper for X-Means algorithm.

@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause

"""

from ctypes import c_double, c_longlong, c_size_t, c_uint, POINTER

from pyclustering.core.wrapper import ccore_library
from pyclustering.core.pyclustering_package import pyclustering_package, package_extractor, package_builder

def xmeans(sample, centers, kmax, tolerance, criterion, alpha, beta, repeat, random_state, metric_pointer):
    random_state = random_state or -1
    pointer_data = package_builder(sample, c_double).create()
    pointer_centers = package_builder(centers, c_double).create()

    ccore = ccore_library.get()

    ccore.xmeans_algorithm.restype = POINTER(pyclustering_package)
    package = ccore.xmeans_algorithm(pointer_data, pointer_centers, c_size_t(kmax), c_double(tolerance),
                                     c_uint(criterion), c_double(alpha), c_double(beta), c_size_t(repeat),
                                     c_longlong(random_state), metric_pointer)

    result = package_extractor(package).extract()
    ccore.free_pyclustering_package(package)

    return result