# %%
import math
import matplotlib.pyplot as plt
import numpy as np
import time
from PIL import Image
from cmath import inf
from numba import jit, prange

# %%
# Vector support functions
# ----------------------------------------------

@jit(nopython=True)
def vec2(x, y):
    return np.array([x, y])

@jit(nopython=True)
def vec3(x, y, z):
    return np.array([x, y, z])

@jit(nopython=True)
def vec4(x, y, z, w):
    return np.array([x, y, z, w])

# %%
# Conveniance functions
# ----------------------------------------------

LINE = 0
RECT = 1
CIRCLE = 2
POLYGON = 3

def generate_render_texture(width, height, depth=4, type=np.uint8):
    return np.full((height, width, depth), fill_value=255, dtype=np.uint8)

def save_tex(texture, name="image.png"):
    Image.fromarray(texture).convert("RGBA").save(name)

def show_image(texture):
    Image.fromarray(texture).show("SDF Renderer")

@jit(nopython=True)
def clamp(x, lower, upper):
    if x < lower:
        return lower
    if x > upper:
        return upper
    return x

@jit(nopython=True)
def smoothstep(x, e1, e2):
    x = clamp((x - e1) / (e2 - e1), 0.0, 1.0)
    return x * x * (3 - 2 * x)

@jit(nopython=True)
def dot(x1, y1, x2, y2):
    return x1 * x2 + y1 * y2

@jit(nopython=True)
def length(x1, y1):
    return math.sqrt(x1 ** 2 + y1 ** 2)

@jit(nopython=True)
def inflate(distance, amount):
    return distance - amount

@jit(nopython=True)
def deflate(distance, amount):
    return distance + amount

@jit(nopython=True)
def interpolate_colors(c1, c2, t):
    # t: float[0..1]
    # c1, c2: int[0...255, 0...255, 0...255, 0...255]
    r = c1[0] * (1 - t) + c2[0] * t
    g = c1[1] * (1 - t) + c2[1] * t
    b = c1[2] * (1 - t) + c2[2] * t
    a = c1[3] * (1 - t) + c2[3] * t
    return [r, g, b, a]

# %%
# SDF function of basic shapes
# ----------------------------------------------

@jit(nopython=True)
def line(sample_x, sample_y, startx, starty, endx, endy, radius):
    pax, pay = sample_x - startx, sample_y - starty
    bax, bay = endx - startx, endy - starty

    h = min(1.0, max(0.0, (pax * bax + pay * bay) / (bax * bax + bay * bay)))

    return length(sample_x - startx - bax * h, sample_y - starty - bay * h) - radius

# TODO: Implement line bounding box, dont really know how to implement

@jit(nopython=True)
def box(sample_x, sample_y, bx, by, bw, bh):
    rx, ry = bw / 2, bh / 2
    qx = abs(sample_x - bx) - rx
    qy = abs(sample_y - by) - ry

    return length(max(qx, 0), max(qy, 0)) + min(max(qx, qy), 0)

@jit(nopython=True)
def rounded_box(sample_x, sample_y, bx, by, bw, bh, radius):
    return inflate(box(sample_x, sample_y, bx, by, bw - 2 * radius, bh - 2 * radius), radius)

@jit(nopython=True)
def circle(sample_x, sample_y, r, cx, cy):
    return math.sqrt((sample_x - cx) ** 2 + (sample_y - cy) ** 2) - r

@jit(nopython=True)
def polygon(sample_x, sample_y, points):
    n = len(points)
    d = dot(sample_x - points[0][0], sample_y - points[0][1], sample_x - points[0][0], sample_y - points[0][1])
    s = 1.0
    for i in range(n):
        j = (i + n - 1) % n

        ex = points[j][0] - points[i][0]
        ey = points[j][1] - points[i][1]

        wx = sample_x - points[i][0]
        wy = sample_y - points[i][1]

        dd = dot(wx, wy, ex, ey) / dot(ex, ey, ex, ey)
        cl = clamp( dd, 0.0, 1.0)

        bx = wx - ex * cl
        by = wy - ey * cl

        d = min(d, dot(bx, by, bx, by))

        cond_x = sample_y >= points[i][1]
        cond_y = sample_y < points[j][1]
        cond_z = ex * wy > ey * wx

        if((cond_x and cond_y and cond_z) or (not cond_x and not cond_y and not cond_z)):
            s = -s

    return s * math.sqrt(d)

@jit(nopython=True)
def multiply(c, f):
    r = c[0] * f
    g = c[1] * f
    b = c[2] * f
    return [r, g, b, 255]

@jit(nopython=True, parallel=True)
def render(render_tex, tex_width, tex_height, objects):
    for w in prange(tex_width):
        for h in prange(tex_height):
            for object_descriptor in objects:
                d = inf
                if object_descriptor[0] == LINE:
                    d = line(w, h, 10, 10, 1910, 1070, 0.075)
                if object_descriptor[0] == RECT:
                    d = rounded_box(w, h, 1200, 540, 500, 700, 15)
                if object_descriptor[0] == CIRCLE:
                    d = circle(w, h, 200, 800, 500)
                if object_descriptor[0] == POLYGON:
                    d = inflate(polygon(w, h, [[250, 250], [650, 250], [650, 550], [400, 550], [250, 400]]), 15)

                if d == inf:
                    continue

                shadow_angle = np.pi
                elevation = object_descriptor[4]

                if d < -0.5:
                    render_tex[h][w] = [object_descriptor[1], object_descriptor[2], object_descriptor[3], 255]
                    continue

                if 1 >= d >= -0.5:
                    s = smoothstep(d, 0, 1)
                    if d > 0:
                         shadow_angle -= np.arctan(elevation/d)
                    render_tex[h][w] = interpolate_colors([object_descriptor[1], object_descriptor[2], object_descriptor[3], 255], multiply(render_tex[h][w], abs(shadow_angle/np.pi)), s)
                    continue

                if d > 1:
                    shadow_angle -= np.arctan(elevation/d)
                    render_tex[h][w] = multiply(render_tex[h][w], abs(shadow_angle/np.pi))
                    continue

# %%
# Helper functions
# ----------------------------------------------

@jit(nopython=True)
def rgb(r, g, b):
    return [r, g, b]

# %%
# Driver code
# ----------------------------------------------

TEX_WIDTH, TEX_HEIGHT = 1920, 1200

render_tex = generate_render_texture(TEX_WIDTH, TEX_HEIGHT)

# Setup objects
N_OBJECTS = 3

object_descriptors = np.zeros((N_OBJECTS, 5), np.int32)

# Universal Object Description: [type, r, g, b], to be extended...
object_descriptors[0] = [RECT, *rgb(79, 77, 231), 2]
object_descriptors[1] = [CIRCLE, *rgb(237, 173, 74), 3]
object_descriptors[2] = [POLYGON, *rgb(163, 15, 92), 4]
# object_descriptors[3] = [LINE, *rgb(255, 0, 0), 0]

start = time.time()
render(render_tex, TEX_WIDTH, TEX_HEIGHT, object_descriptors)
print("Took:", (time.time() - start))

# Show results
plt.axis("off")
plt.tight_layout()
plt.imshow(render_tex)
plt.show()

save_tex(render_tex)
show_image(render_tex)

# %%

# %%


import logging
import kivy
logging.basicConfig(filename = 'test5.log',level=logging.DEBUG,format ='%(asctime)s %(message)s')
logging.info('Importing all required kivy libraries')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button

class MyGridLayout(GridLayout):
    #initialize infinite keywords
    logging.info('Creating Gridlayout class')
    def __init__(self,**kwargs):
        #call grid layout constructor
        super(MyGridLayout, self).__init__(**kwargs)
        #set columns
        logging.info('Creating GUI buttons and widgets')
        self.cols = 1
        self.add_widget(Label(text="Put your Path here: ",font_size='25sp'))
        self.address = TextInput()
        self.add_widget(self.address)
        # add submit button
        self.submit = Button(text = 'SUBMIT',width=10,height=10,pos_hint={'center_x':0.8})
        self.submit.bind(on_press=self.PDF_merging)
        self.add_widget((self.submit))

    logging.info('Calling our Main PDF_merging() function after getting input')
    def PDF_merging(self,instance):
        import os
        try:
            path = self.address.text
            files = os.listdir(path)  # list down all the pdf file in the given path
            count = 0
            logging.info('Counting Number of PDF files at the location')
            for i in files:
                if i.endswith('.pdf'):
                    count += 1
            logging.error("Given path may be not correct")
            if count == 0:
                self.add_widget(Label(text=f'there are no pdf files in the location: {path}'))
            elif count == 1:
                self.add_widget(Label(text=f'there are only one pdf files in the location: {path}'))
            else:
                #get merged pdf name
                import datetime
                now = datetime.datetime.now()
                name = now.strftime("%B%d_%Y_%H_%M_%S%p")

                #merging pdfs to a one file
                try:
                    from PyPDF2 import PdfFileMerger
                    merger = PdfFileMerger()
                    st = name + '.pdf'
                    for i in files:
                        if i.endswith('.pdf'):
                            merger.append(os.path.join(path, i))
                    final = merger.write(path + st)
                    merger.close()

                    # move processed pdfs to newly created directory

                    self.add_widget(Label(text=f'we found {count} PDFs at the location :{path}.find Merged PDF and log file at current working directory'))
                    self.address.text = ''
                except Exception as e:
                    loggin.error(e)
        except Exception as e:
            logging.error(e)

class Myapp(App):
    def build(self):
        return MyGridLayout()

if __name__ == '__main__':
    Myapp().run()


import re

from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from util import tokenise, is_number

class TextField:
    def __init__(self, value):
        self.raw_text = value
        self.word_vec = re.split(r'[^\w]+', value.lower())
        self.word_vec = [w for w in self.word_vec if len(w) > 0]
        self.word_set = frozenset(self.word_vec)

    def next_of(self, w):
        try:
            n = self.word_vec[self.word_vec.index(w) + 1]
        except:
            n = None
        return n

    def contains(self, w):
        return w in self.word_set

class Document:

    def __init__(self, r):
        self.id = r['id']
        self.brand = r['brand']
        self.supplier = r['supplier']
        self.title = TextField(r['title'])
        self.breadcrumbs = TextField(r['breadcrumbs'])
        self.option_text = TextField(r['option_text'])
        s1 = r['short_description'] if r['short_description'] else ''
        s2 = r['description'] if r['description'] else ''
        self.descriptions = TextField(s1 + ' ' + s2)
        self.stop_words = set(stopwords.words('english'))
        # numbers
        word_tokens = word_tokenize(self.title.raw_text)
        filtered_numbers = [w for w in word_tokens if w.isnumeric()]
        self.numbers = filtered_numbers
        # brand

    def has_brand(self):
        return self.brand and len(self.brand) > 0

    def preprocess(self):
        # abstract title
        word_tokens = word_tokenize(self.title.raw_text)
        filtered_numbers= [w for w in word_tokens if w.isnumeric()]
        self.numbers = filtered_numbers

        # abstract descriptions
        # reg_tokenizer = RegexpTokenizer(r'\w+')
        # word_tokens = reg_tokenizer.tokenize(self.descriptions.raw_text)
        # # word_tokens = word_tokenize(self.descriptions.__str__())
        # filtered_sentence = [w for w in word_tokens if not w in self.stop_words]
        # self.descriptions = filtered_sentence

class Feature:
    def __init__(self):
        self.wc_total_strong = 0
        self.wc_total_weak = 0
        self.wc_breadcrumbs = 0
        self.wc_option_text = 0
        self.wc_description = 0
        self.wc_brand_name = 0
        self.wc_number = 0
        self.wc_spec_number = 0
        self.brand_count = 0
        self.same_supplier = 0
        # self.wc_title = 0

class FeatureGenerator:
    def __init__(self, brand_names, unit_words):
        self.brand_names = brand_names
        self.unit_words = unit_words

    def update(self, result, w, d, d1):
        if w in self.brand_names:
            result.wc_brand_name += 1

        if is_number(w):
            result.wc_number += 1
            if d.title.next_of(w) in self.unit_words:
                result.wc_spec_number += 1

        hit_count = 0

        if d1.breadcrumbs.contains(w):
            result.wc_breadcrumbs += 1
            hit_count += 1

        if d1.option_text.contains(w):
            result.wc_option_text += 1
            hit_count += 1

        if d1.descriptions.contains(w):
            result.wc_description += 1
            hit_count += 1

        if hit_count == 0:
            result.wc_total_strong += 1

    def generate(self, d1, d2):
        result = Feature()

        s1_2 = d1.title.word_set - d2.title.word_set
        s2_1 = d2.title.word_set - d1.title.word_set

        result.wc_total_weak = len(s1_2) + len(s2_1)

        word_tokens1 = word_tokenize(d1.title.raw_text)
        word_tokens2 = word_tokenize(d2.title.raw_text)
        # result.wc_title = set(word_tokens1) ^ set(word_tokens2)

        for w in s1_2:
            self.update(result, w, d1, d2)

        for w in s2_1:
            self.update(result, w, d2, d1)

        if d1.has_brand():
            result.brand_count += 1

        if d2.has_brand():
            result.brand_count += 1

        if d1.supplier == d2.supplier:
            result.same_supplier = 1

        return result


from vosk import Model, KaldiRecognizer
import json
import pyaudio
import pyttsx3

p=pyttsx3.init('sapi5')
voices=p.getProperty('voices')
p.setProperty('voice',voices[1].id)
p.setProperty('rate',190)
p.setProperty('volume',1.0)

def speak(audio):
    print('')
    p.say(audio)
    print(audio)
    p.runAndWait()

# Download vosk-model-small-en-us-0.15
# add it path
model=Model("W:\\Projects\\Projects\\vosk-model-small-en-us-0.15")
recognizer=KaldiRecognizer(model,16000)
mic=pyaudio.PyAudio()
stream=mic.open(format=pyaudio.paInt16,channels=1,rate=16000,input=True,frames_per_buffer=8192)
while True:
    data=stream.read(4096,exception_on_overflow=False)
    if len(data)==0:
        break
    if recognizer.AcceptWaveform(data):
          rec=recognizer.Result()
          command=json.loads(rec)
          print(command['text'])

        #   if 'hello' in command['text']:
        #       stream.stop_stream()
        #       print('\t\t\t\t\t\t\t\t\t\t\t\t\t'+command['text'])
        #       speak('Hi sir, I am Py assist,your personal assistant')
        #       speak('How may i help you?')
        #       stream.start_stream()
        #       print('')
        #       print('Listening')
        #       print('')


import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Dropout, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.regularizers import l2
from tensorflow.keras.callbacks import EarlyStopping

# from spektral.datasets import citeseer
import spektral
from spektral.layers import GraphConv

from scipy.sparse import coo_matrix
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import shuffle

# Load dataset
# adjacency, features, labels, _, _, _ = citeseer.load_data()

from tensorflow.keras import activations, initializers, constraints
from tensorflow.keras.layers import Layer
from tensorflow.keras import regularizers

# class GraphConv(Layer):
#     def __init__(self, units, activation=None, use_bias=True,
#                  kernel_initializer='glorot_uniform', bias_initializer='zeros',
#                  kernel_regularizer=None, bias_regularizer=None,
#                  activity_regularizer=None, kernel_constraint=None, bias_constraint=None,
#                  **kwargs):
#         super(GraphConv, self).__init__(**kwargs)
#         self.units = units
#         self.activation = activations.get(activation)
#         self.use_bias = use_bias
#         self.kernel_initializer = initializers.get(kernel_initializer)
#         self.bias_initializer = initializers.get(bias_initializer)
#         self.kernel_regularizer = regularizers.get(kernel_regularizer)
#         self.bias_regularizer = regularizers.get(bias_regularizer)
#         self.activity_regularizer = regularizers.get(activity_regularizer)
#         self.kernel_constraint = constraints.get(kernel_constraint)
#         self.bias_constraint = constraints.get(bias_constraint)

#     def build(self, input_shape):
#         assert len(input_shape) == 2
#         self.input_dim = input_shape[-1]
#         self.kernel = self.add_weight(shape=(self.input_dim, self.units),
#                                       initializer=self.kernel_initializer,
#                                       name='kernel',
#                                       regularizer=self.kernel_regularizer,
#                                       constraint=self.kernel_constraint)
#         if self.use_bias:
#             self.bias = self.add_weight(shape=(self.units,),
#                                         initializer=self.bias_initializer,
#                                         name='bias',
#                                         regularizer=self.bias_regularizer,
#                                         constraint=self.bias_constraint)
#         else:
#             self.bias = None
#         self.built = True

#     def call(self, inputs):
#         features, adjacency = inputs
#         output = tf.sparse.sparse_dense_matmul(adjacency, features)
#         output = tf.matmul(output, self.kernel)
#         if self.use_bias:
#             output = tf.nn.bias_add(output, self.bias)
#         if self.activation is not None:
#             output = self.activation(output)
#         return output

#     def compute_output_shape(self, input_shape):
#         assert input_shape and len(input_shape) == 2
#         return input_shape[0], self.units

cora_dataset = spektral.datasets.citation.Citation(name='cora')
test_mask = cora_dataset.mask_te
train_mask = cora_dataset.mask_tr
val_mask = cora_dataset.mask_va
graph = cora_dataset.graphs[0]
features = graph.x
adjacency = graph.a
labels = graph.y

# adjacency, features, labels, _, _, _ = spektral.datasets.citation.Citation('citeseer')

# Parameters
N = adjacency.shape[0]       # Number of nodes in the graph
F = features.shape[1]        # Number of node features
n_classes = labels.shape[1]  # Number of classes
l2_reg = 5e-4                # Regularization rate for l2
learning_rate = 1e-2         # Learning rate for optimizer
epochs = 200                 # Number of training epochs
batch_size = N               # Batch size
es_patience = 10             # Patience for early stopping

# Convert the data to a list of sparse matrices
A_list = [coo_matrix(adjacency).astype(np.float32)]
X_list = [features.astype(np.float32)]

# Convert the labels to integers
label_encoder = LabelEncoder()
labels = label_encoder.fit_transform(np.argmax(labels, axis=1))

# Shuffle the data
X, A, y = shuffle(features, adjacency, labels, random_state=0)

# Model definition
X_in = Input(shape=(F, ))
A_in = Input((N, ), sparse=True)

# Graph Convolutional Layers
H = Dropout(0.5)(X_in)
H = tf.keras.layers.Dense(16, activation='relu', kernel_regularizer=l2(l2_reg))(H)
H = tf.keras.layers.Dropout(0.5)(H)
H = tf.keras.layers.Dense(n_classes, activation='softmax')(H)
output = GraphConv(n_classes, activation='softmax')([H,A_in])

# Build model
model = Model(inputs=[X_in, A_in], outputs=output)
optimizer = Adam(lr=learning_rate)
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Training
validation_data = ([X]+A_list, y, val_mask)
model.fit([X]+A_list, y, sample_weight=train_mask, batch_size=batch_size, epochs=epochs,
          validation_data=validation_data, shuffle=False, callbacks=[
        EarlyStopping(patience=es_patience, restore_best_weights=True)
    ])

# Evaluation
test_results = model.evaluate([X]+A_list, y, sample_weight=test_mask, batch_size=N)
print('Test loss: {:.3f}'.format(test_results[0]))
print('Test accuracy: {:.3f}'.format(test_results[1]))


__author__ = "Joshua Laniado and Todd O. Yeates"
__copyright__ = "Copyright 2020, Nanohedra"
__version__ = "1.0"

class FragMatchInfo:
    def __init__(self, res_pair_freqs, oligomer_1_ch_id, oligomer_1_res_num, oligomer_2_ch_id, oligomer_2_res_num, z_val):
        self.res_pair_freqs = res_pair_freqs
        self.oligomer_1_ch_id = oligomer_1_ch_id
        self.oligomer_1_res_num = oligomer_1_res_num
        self.oligomer_2_ch_id = oligomer_2_ch_id
        self.oligomer_2_res_num = oligomer_2_res_num
        self.z_val = z_val

        self.score = 1 / float(1 + (z_val ** 2))

        self.oligomer_1_res_freqs = {}
        self.oligomer_2_res_freqs = {}
        for res_pair in self.res_pair_freqs:
            res1_type, res2_type = res_pair[0]
            res_freq = res_pair[1]

            if res1_type in self.oligomer_1_res_freqs:
                self.oligomer_1_res_freqs[res1_type] += res_freq
            else:
                self.oligomer_1_res_freqs[res1_type] = res_freq

            if res2_type in self.oligomer_2_res_freqs:
                self.oligomer_2_res_freqs[res2_type] += res_freq
            else:
                self.oligomer_2_res_freqs[res2_type] = res_freq

    def get_res_pair_freqs(self):
        return self.res_pair_freqs

    def get_oligomer_1_ch_id(self):
        return self.oligomer_1_ch_id

    def get_oligomer_1_res_num(self):
        return self.oligomer_1_res_num

    def get_oligomer_2_ch_id(self):
        return self.oligomer_2_ch_id

    def get_oligomer_2_res_num(self):
        return self.oligomer_2_res_num

    def get_z_val(self):
        return self.z_val

    def get_oligomer_1_res_freqs(self):
        return self.oligomer_1_res_freqs

    def get_oligomer_2_res_freqs(self):
        return self.oligomer_2_res_freqs

    def get_score(self):
        return self.score

class SeqFreqInfo:
    def __init__(self, frag_match_info_list):
        self.frag_match_info_list = frag_match_info_list

        self.oligomer_1 = []
        self.oligomer_2 = []

        oligomer_1_freqs_w_sum = {}
        oligomer_2_freqs_w_sum = {}
        score_sum_dict_1 = {}
        score_sum_dict_2 = {}
        for frag_match_info in frag_match_info_list:
            # get information from specific match
            match_score = frag_match_info.get_score()
            match_oligomer_1_ch_id = frag_match_info.get_oligomer_1_ch_id()
            match_oligomer_1_res_num = frag_match_info.get_oligomer_1_res_num()
            match_oligomer_1_freqs = frag_match_info.get_oligomer_1_res_freqs()
            match_oligomer_2_ch_id = frag_match_info.get_oligomer_2_ch_id()
            match_oligomer_2_res_num = frag_match_info.get_oligomer_2_res_num()
            match_oligomer_2_freqs = frag_match_info.get_oligomer_2_res_freqs()

            # weigh matched residue frequencies by match score
            # do so for the matched residue on oligomer 1 and the matched residue on oligomer 2
            match_oligomer_1_freqs_w = {res_type: freq*match_score for (res_type, freq) in match_oligomer_1_freqs.items()}
            match_oligomer_2_freqs_w = {res_type: freq*match_score for (res_type, freq) in match_oligomer_2_freqs.items()}

            # add match score to sum of the residue match scores
            # do so for the matched residue on oligomer 1 and the matched residue on oligomer 2
            if match_oligomer_1_ch_id in score_sum_dict_1:
                if match_oligomer_1_res_num in score_sum_dict_1[match_oligomer_1_ch_id]:
                    score_sum_dict_1[match_oligomer_1_ch_id][match_oligomer_1_res_num] += match_score
                else:
                    score_sum_dict_1[match_oligomer_1_ch_id][match_oligomer_1_res_num] = match_score
            else:
                score_sum_dict_1[match_oligomer_1_ch_id] = {match_oligomer_1_res_num: match_score}

            if match_oligomer_2_ch_id in score_sum_dict_2:
                if match_oligomer_2_res_num in score_sum_dict_2[match_oligomer_2_ch_id]:
                    score_sum_dict_2[match_oligomer_2_ch_id][match_oligomer_2_res_num] += match_score
                else:
                    score_sum_dict_2[match_oligomer_2_ch_id][match_oligomer_2_res_num] = match_score
            else:
                score_sum_dict_2[match_oligomer_2_ch_id] = {match_oligomer_2_res_num: match_score}

            # for each residue type add the weighted residue frequency to the sum of weighted residue type frequencies
            # do so for the matched residue on oligomer 1 and the matched residue on oligomer 2
            if match_oligomer_1_ch_id in oligomer_1_freqs_w_sum:
                if match_oligomer_1_res_num in oligomer_1_freqs_w_sum[match_oligomer_1_ch_id]:
                    for (match_res_type, match_freq) in match_oligomer_1_freqs_w.items():
                        if match_res_type in oligomer_1_freqs_w_sum[match_oligomer_1_ch_id][match_oligomer_1_res_num]:
                            oligomer_1_freqs_w_sum[match_oligomer_1_ch_id][match_oligomer_1_res_num][match_res_type] += match_freq
                        else:
                            oligomer_1_freqs_w_sum[match_oligomer_1_ch_id][match_oligomer_1_res_num][match_res_type] = match_freq
                else:
                    oligomer_1_freqs_w_sum[match_oligomer_1_ch_id][match_oligomer_1_res_num] = match_oligomer_1_freqs_w
            else:
                oligomer_1_freqs_w_sum[match_oligomer_1_ch_id] = {match_oligomer_1_res_num: match_oligomer_1_freqs_w}

            if match_oligomer_2_ch_id in oligomer_2_freqs_w_sum:
                if match_oligomer_2_res_num in oligomer_2_freqs_w_sum[match_oligomer_2_ch_id]:
                    for (match_res_type, match_freq) in match_oligomer_2_freqs_w.items():
                        if match_res_type in oligomer_2_freqs_w_sum[match_oligomer_2_ch_id][match_oligomer_2_res_num]:
                            oligomer_2_freqs_w_sum[match_oligomer_2_ch_id][match_oligomer_2_res_num][match_res_type] += match_freq
                        else:
                            oligomer_2_freqs_w_sum[match_oligomer_2_ch_id][match_oligomer_2_res_num][match_res_type] = match_freq
                else:
                    oligomer_2_freqs_w_sum[match_oligomer_2_ch_id][match_oligomer_2_res_num] = match_oligomer_2_freqs_w
            else:
                oligomer_2_freqs_w_sum[match_oligomer_2_ch_id] = {match_oligomer_2_res_num: match_oligomer_2_freqs_w}

        # for each residue calculate the weighted average frequency for the different residue types
        for ch_id_1 in oligomer_1_freqs_w_sum:
            for res_num_1 in oligomer_1_freqs_w_sum[ch_id_1]:
                res_score_sum_1 = score_sum_dict_1[ch_id_1][res_num_1]
                for res_type_1 in oligomer_1_freqs_w_sum[ch_id_1][res_num_1]:
                    oligomer_1_freqs_w_sum[ch_id_1][res_num_1][res_type_1] /= float(res_score_sum_1)

        for ch_id_2 in oligomer_2_freqs_w_sum:
            for res_num_2 in oligomer_2_freqs_w_sum[ch_id_2]:
                res_score_sum_2 = score_sum_dict_2[ch_id_2][res_num_2]
                for res_type_2 in oligomer_2_freqs_w_sum[ch_id_2][res_num_2]:
                    oligomer_2_freqs_w_sum[ch_id_2][res_num_2][res_type_2] /= float(res_score_sum_2)

        # sort sequence frequency information by chain ID, residue number and frequency
        # for oligomer 1 and oligomer 2
        for (ch_id_1, ch_id_1_resnums) in sorted(oligomer_1_freqs_w_sum.items(), key=lambda kv: kv[0]):
            sorted_freqs_1 = []
            for (res_num_1, res_num_1_freqs) in sorted(oligomer_1_freqs_w_sum[ch_id_1].items(), key=lambda kv: kv[0]):
                sorted_freqs_1.append((res_num_1, sorted(oligomer_1_freqs_w_sum[ch_id_1][res_num_1].items(), key=lambda kv: kv[1], reverse=True)))
            self.oligomer_1.append((ch_id_1, sorted_freqs_1))

        for (ch_id_2, ch_id_2_resnums) in sorted(oligomer_2_freqs_w_sum.items(), key=lambda kv: kv[0]):
            sorted_freqs_2 = []
            for (res_num_2, res_num_2_freqs) in sorted(oligomer_2_freqs_w_sum[ch_id_2].items(), key=lambda kv: kv[0]):
                sorted_freqs_2.append((res_num_2, sorted(oligomer_2_freqs_w_sum[ch_id_2][res_num_2].items(), key=lambda kv: kv[1], reverse=True)))
            self.oligomer_2.append((ch_id_2, sorted_freqs_2))

    def get_oligomer_1(self):
        return self.oligomer_1

    def get_oligomer_2(self):
        return self.oligomer_2

    def write(self, output_file_path):

        outfile = open(output_file_path, "a+")
        outfile.write("***** WEIGHTED RESIDUE FREQUENCIES *****\n\n")
        outfile.close()

        oligomers_seq_freqs = (self.get_oligomer_1(), self.get_oligomer_2())

        # write output for oligomers 1 and 2 to output file
        for oligomer in range(2):
            for (ch_id, ch_id_resnums) in oligomers_seq_freqs[oligomer]:
                outfile = open(output_file_path, "a+")
                outfile.write("OLIGOMER %s  |  CHAIN %s\n" % (str(oligomer + 1), ch_id))
                ch_id_res_freqs = []
                for (res_num, res_num_freqs) in ch_id_resnums:
                    res_num_freqs_rounded = [(res_type, round(res_freq, 3)) for (res_type, res_freq) in res_num_freqs]
                    outfile.write("RES NUM " + str(res_num) + "\n" + str(res_num_freqs_rounded) + "\n\n")
                    ch_id_res_freqs.append(res_num_freqs)
                outfile.write("\n")
                outfile.close()


def sortByHeight(a):
    heights = []
    for element in a:
        if element != -1:
            heights.append(element)
    j = 0
    for i in range(0,len(a)):
        if a[i] != -1:
            a[i] = sorted(heights)[j]
            j = j + 1
    return a


# coding=utf-8
import urllib2
from bs4 import BeautifulSoup as bs
import hashlib, time, json

_time = time.strftime('%Y%m%d',time.localtime(time.time())) # 当前时间 格式为yearmonthday

def moviespider():
    url = 'http://theater.mtime.com/China_Zhejiang_Province_Fenghua/3869/'

    page = urllib2.urlopen(url)
    soup = bs(page)

    original = soup.find_all(class_='table')
    m_count = len(original)
    movie_list = []
    movie_name = []
    movie_time = []
    movie_price = []

    for i in range(0, m_count):
        name_temp = []
        time_temp = []
        price_temp = []

        name_temp.append(original[i].find(class_='c_000').text)

        for t in original[i].find_all('strong'):
            time_temp.append(t.text)
        #time_temp=qc(time_temp)
        for p in original[i].find_all('em'):
            price_temp.append(p.text)

        movie_name.extend(name_temp)
        movie_time.append(time_temp)
        movie_price.append(price_temp)

    movie_list.append(movie_name)
    movie_list.append(movie_time)
    movie_list.append(movie_price)

    return movie_list

def verification(request):
    """
    接入和消息推送校验
    """
    signature = request.GET.get('signature')
    timestamp = request.GET.get('timestamp')
    nonce = request.GET.get('nonce')

    token = 'seyren'
    tmplist = [token, timestamp, nonce]
    tmplist.sort()
    tmpstr = ''.join(tmplist)
    hashstr = hashlib.sha1(tmpstr).hexdigest()

    if hashstr == signature:
        return True
    return False

def parse_movie_list(movie_list):
    for x in xrange(len(movie_list[0])):
        for y in xrange(len(movie_list[1][x])):
            return movie_list[0][x] + '\n' + movie_list[1][x][y] + movie_list[2][x][y]


# function to map the file name and image name
def create_image_mapping_table(db_connection):
    db_connection.execute('''CREATE TABLE IMAGEMAPPING
         (ID INTEGER PRIMARY KEY  AUTOINCREMENT NOT NULL ,
         username           TEXT    NOT NULL,
         original_name           TEXT    NOT NULL,
         mapped_name        TEXT    NOT NULL);''')
    db_connection.commit()

# function to insert into IMAGEMAPPING into table
def insert_into_image_mapping_table(db_connection, username, original, mapped):
    db_connection.execute('''INSERT INTO IMAGEMAPPING (username, original_name, mapped_name) VALUES ("{username}","{original}",
    "{mapped}")'''.format(username=username, original=original, mapped=mapped))
    db_connection.commit()

# function to get the original image name from the mapped name
def get_original_name(db_connection, username, mapped_name):
    cursor = db_connection.execute('''SELECT original_name FROM IMAGEMAPPING where username = "{username}" AND mapped_name
     = "{mapped}"
    LIMIT 1'''.format(username=username, mapped=mapped_name))
    for row in cursor:
        if row:
            return row[0]
    return False


def subset_sum(arr, target, n, memo):
    # Base cases
    if target == 0:
        return True
    if n == 0:
        return False

    # Check if the subproblem has already been solved
    if memo[n][target] != -1:
        return memo[n][target]

    # If the last element is greater than the target, exclude it
    if arr[n - 1] > target:
        memo[n][target] = subset_sum(arr, target, n - 1, memo)
        return memo[n][target]

    # Recursively check if the target can be achieved either by including or excluding the last element
    memo[n][target] = subset_sum(arr, target - arr[n - 1], n - 1, memo) or subset_sum(arr, target, n - 1, memo)
    return memo[n][target]

# Read the number of test cases
num_test_cases = int(input())

# Iterate through each test case
for _ in range(num_test_cases):
    # Read the number of elements in the array
    n = int(input())

    # Read the array elements
    arr = list(map(int, input().split()))

    # Read the target sum
    target = int(input())

    # Initialize memoization table with -1 values
    memo = [[-1] * (target + 1) for _ in range(n + 1)]

    # Check if there exists a subset with the given sum
    if subset_sum(arr, target, n, memo):
        print("Yes")
    else:
        print("No")


#! /usr/bin/env python

import os
import datetime
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

import Config
from utils import DataLoader, GensimModels, DataPlotter

# Parameters
# ==================================================
ftype = torch.cuda.FloatTensor
ltype = torch.cuda.LongTensor

# Data loading params
train_file = Config.path_culture

# Model Hyperparameters
feat_dim = 10
cnn_w = [1,2,3,5]
cnn_h = [25,50,75,125]
sum_h = sum(cnn_h)

# Training Parameters
batch_size = 20
num_epochs = 30
learning_rate = 0.001
momentum = (0.9, 0.999)
evaluate_every = 3

# Data Preparation
# ===========================================================
# Load data
print("Loading data...")
dl = DataLoader.DataLoader()
id2cult, id2ingr, train_cult, train_ingr, train_ingr_len, valid_cult, valid_ingr, valid_ingr_len, test_cult, test_ingr, test_ingr_len, max_ingr_cnt, ingrid2vec = dl.load_data(train_file, feat_dim)

print("Train/Test/Cult/ingr: {:d}/{:d}/{:d}/{:d}".format(len(train_cult), len(test_cult), len(id2cult), len(id2ingr)))
print("==================================================================================")

class ConvModule(nn.Module):
    def __init__(self, input_size, kernel_sizes, ingr_cnt):
        super(ConvModule, self).__init__()

        # attributes:
        self.maxlen = max_ingr_cnt
        self.in_channels = input_size
        self.out_channels = len(id2cult) #[25*k for k in kernel_sizes]
        self.cnn_kernel_size = 3 #kernel_sizes
        self.mp_kernel_size = self.maxlen - self.cnn_kernel_size + 1

        # modules:
        self.ingr_weight = nn.Embedding(ingr_cnt, feat_dim).type(ftype)
        #self.ingr_weight.weight.data.copy_(torch.from_numpy(np.asarray(ingrid2vec)))
        self.cnn1 = nn.Conv1d(self.in_channels, 32, self.cnn_kernel_size)
        self.cnn2 = nn.Conv1d(32, self.out_channels, self.cnn_kernel_size)
        '''
        self.cnn = nn.ModuleList([nn.Conv1d(self.in_channels, self.out_channels[i],
                                kernel_size=self.cnn_kernel_size[i], stride=1)
                                for i in xrange(len(kernel_sizes))])
        '''
        self.maxpool1 = nn.MaxPool1d(3)
        self.maxpool2 = nn.MaxPool1d(18)
        self.relu = nn.ReLU()

    def forward(self, ingredient, emb_mask):
        ingredient = self.ingr_weight(ingredient)
        ingredient = torch.mul(ingredient, emb_mask).permute(0,2,1)

        output = self.cnn1(ingredient)
        output = torch.squeeze(self.maxpool1(output))
        output = self.relu(output)

        output = self.cnn2(output)
        output = torch.squeeze(self.maxpool2(output))
        output = self.relu(output)
        '''
        output = []
        for i, conv in enumerate(self.cnn):
            output.append(torch.max(F.tanh(conv(ingredient)), dim=2)[0])
        output = torch.cat(output, 1)
        '''

        return output

class LinearModule(nn.Module):
    def __init__(self, input_size, output_size):
        super(LinearModule, self).__init__()

        # attributes:
        self.input_size = input_size #275
        self.hidden_size1 = 128
        self.hidden_size2 = 64
        self.output_size = output_size

        # modules:
        #self.linear1 = nn.Linear(self.input_size, self.hidden_size1)
        self.linear2 = nn.Linear(self.hidden_size1, self.hidden_size2)
        self.active = nn.Sigmoid()

    def forward(self, x):
        #x = self.active(self.linear1(x))
        x = self.active(self.linear2(x))

        return x

def parameters():
    params = []
    for model in [cnn_model, linear_model]:
        params += list(model.parameters())

    return params

def make_mask(maxlen, dim, length):
    one = [1]*dim
    zero = [0]*dim
    mask = []
    for c in length:
        mask.append(one*c + zero*(maxlen-c))

    # (batch) * maxlen * dim
    # [[1 1 1 ... 1 0 0 0 ... 0]...]
    return Variable(torch.from_numpy(np.asarray(mask)).type(ftype), requires_grad=False)

def run(culture, ingredient, ingredient_cnt, step):

    optimizer.zero_grad()

    # (batch)
    culture = Variable(torch.from_numpy(np.asarray(culture))).type(ltype)
    # (batch) x (max_ingr_cnt(65))
    ingredient = Variable(torch.from_numpy(np.asarray(ingredient))).type(ltype)
    emb_mask = make_mask(max_ingr_cnt, feat_dim, ingredient_cnt).view(-1, max_ingr_cnt, feat_dim)
    # (batch) x (sum(25 * cnn_w)(275))
    cnn_output = cnn_model(ingredient, emb_mask)
    # (batch) x (culture_cnt)
    #lin_output = linear_model(cnn_output)

    J = loss_model(cnn_output, culture)

    cnn_output = np.argmax(cnn_output.data.cpu().numpy(), axis=1)
    culture = culture.data.cpu().numpy()
    hit_cnt = np.sum(np.array(cnn_output) == np.array(culture))

    if step == 2:
        return hit_cnt, J.data.cpu().numpy()

    J.backward()
    optimizer.step()

    return hit_cnt, J.data.cpu().numpy()

def print_score(batches, step):
    total_hc = 0.0
    total_loss = 0.0

    for i, batch in enumerate(batches):
        batch_cult, batch_ingr, batch_ingr_len = zip(*batch)
        batch_hc, batch_loss = run(batch_cult, batch_ingr, batch_ingr_len, step=step)
        total_hc += batch_hc
        total_loss += batch_loss

    print("loss: ", total_loss/i)
    print("acc.: ", total_hc/len(test_cult)*100)
    if step == 3:
        np.save("ingredient_weight.npy", cnn_model.ingr_weight.weight.data.cpu().numpy())
        np.save("id2ingr.npy", id2ingr)

###############################################################################################
cnn_model = ConvModule(feat_dim, cnn_w, len(id2ingr)).cuda()
linear_model = LinearModule(sum_h, len(id2cult)).cuda()
loss_model = nn.CrossEntropyLoss().cuda()
#optimizer = torch.optim.SGD(parameters(), lr=learning_rate, momentum=momentum)
optimizer = torch.optim.Adam(parameters(), lr=learning_rate, betas=momentum)

for i in xrange(num_epochs):
    # Training
    train_batches = dl.batch_iter(list(zip(train_cult, train_ingr, train_ingr_len)), batch_size)
    total_hc = 0.
    total_loss = 0.
    for j, train_batch in enumerate(train_batches):
        batch_cult, batch_ingr, batch_ingr_len = zip(*train_batch)
        batch_hc, batch_loss = run(batch_cult, batch_ingr, batch_ingr_len, step=1)
        total_hc += batch_hc
        total_loss += batch_loss
        if (j+1) % 500 == 0:
            print("batch #{:d}: ".format(j+1)), "batch_loss :", total_loss/j, "acc. :", total_hc/batch_size/j*100, datetime.datetime.now()

    # Evaluation
    if (i+1) % evaluate_every == 0:
        print("==================================================================================")
        print("Evaluation at epoch #{:d}: ".format(i+1))
        test_batches = dl.batch_iter(list(zip(valid_cult, valid_ingr, valid_ingr_len)), batch_size)
        print_score(test_batches, step=2)

# Testing
print("Training End..")
print("==================================================================================")
print("Test: ")
test_batches = dl.batch_iter(list(zip(test_cult, test_ingr, test_ingr_len)), batch_size)
print_score(test_batches, step=3)


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

import base
import re

class service(base.baseService):
    """ 站内消息
    """

    def __init__(self, model, param):
        base.baseService.__init__(self, model, param)

        self.blogModel = self.importModel('blog')
        self.messageModel = self.importModel('message')

    # def create(self):
    #     """ 生成消息
    #     """
    #     pass
    dicMsg = {
    'reg':'{{nickname}} 您好，欢迎使用一道自媒体平台。现在一道提供自媒体广告交易系统，您可以 发布广告需求，也可以 入驻自媒体 接单，如有疑问请联系客服。',
    'pass':'您的广告需求{{demand_title}}已通过审核，您可以邀请自媒体接单。',
    'denied':'您的广告需求{{demand_title}}因为“{{check_info}}”未通过审核，请重新发布广告需求。',
    'take_order': '您的广告需求{{demand_title}}已有自媒体接单。',
    'apply_change_price': '您的{{media_title}} - {{price_name}}收到改价申请，前往广告需求{{demand_title}}改价。',
    'already_change_price':'自媒体{{media_title}} - {{price_name}}已改价为{{price}}，查看预选单。',
    'pay': '您的{{media_title}}成功收到下单，来自广告需求{{demand_title}}，前往查看订单详情。',
    'nopay': '您接单的广告需求 {{demand_title}} 已经投放给其它自媒体，请找寻更适合您的 广告需求。',
    'feedback': '您已收到自媒体{{media_title}} 的反馈，前往查看反馈结果。',#反馈
    'feedback_check': '您的 {{media_title}} 提交的反馈已验收，交易款项即将结算，前往查看 订单详情 并给广告主评价.',#验收
    'complain': '广告主对您的 {{media_title}} 提交的反馈有异议并已提交申诉，客服将会帮助调解，前往查看 订单申诉。',
    'appeal_done_media':'您的 {{media_title}} 收到的申诉已有处理结果，前往查看 申诉结果。',
    'appeal_done':'您对 {{media_title}} 提交的申诉已有处理结果，前往查看 申诉结果。',
    'demand_cancel':'您接单的广告需求 {{demand_title}} 已被广告主撤销，请找寻其它 广告需求。',
    'demand_expire':'您的广告需求 {{demand_title}} 即将过期，已有 {{take_order_num}} 个自媒体接单。',
    'demand_expire_media':'您接单的广告需求 {{demand_title}} 已过期，请找寻其它 广告需求',
    'settlement': '亲爱的{{nickname}}您好，您完成的需求“{{demand_title}}”已经确认结算，详情请前往一道自媒体平台查看。',
    'invite':'您的 {{media_title}} 收到广告需求 {{demand_title}} 的接单邀请。',
    }

    def msg(self,strKey,dicData):
        if strKey == 'demand_expire':
            dicMap = {
                '{{demand_id}}': dicData['demand_id'],
                '{{demand_title}}': dicData['demand_title'],
                '{{take_order_num}}': dicData['take_order_num'],
            }

        if strKey == 'pass' or strKey == 'take_order' or strKey == 'demand_expire_media' or strKey == 'demand_cancel':
            dicMap = {
                '{{demand_title}}': dicData['demand_title'],
                '{{demand_id}}': dicData['demand_id'],
            }

        if strKey == 'apply_change_price':
            dicMap = {
                '{{demand_id}}': dicData['demand_id'],
                '{{demand_title}}': dicData['demand_title'],
                '{{media_title}}': dicData['media_title'],
                '{{price_name}}': dicData['price_name'],
            }

        if strKey == 'already_change_price':
            dicMap = {
                '{{nickname}}': dicData['nickname'],
                '{{price}}': dicData['price'],
                '{{media_title}}': dicData['media_title'],
                '{{price_name}}': dicData['price_name'],
                '{{cart_id}}': dicData['cart_id'],
            }

        if strKey == 'denied':
            dicMap = {
                '{{demand_id}}': dicData['demand_id'],
                '{{demand_title}}': dicData['demand_title'],
                '{{check_info}}': dicData['check_info']
            }

        if strKey == 'feedback' or strKey == 'feedback_check' or strKey == 'complain' or strKey == 'appeal_done' or strKey == 'appeal_done_media':
            dicMap = {
                '{{order_id}}': dicData['order_id'],
                '{{media_title}}': dicData['media_title']
            }
        if strKey == 'pay':
            dicMap = {
                '{{demand_title}}': dicData['demand_title'],
                '{{media_title}}': dicData['media_title'],
                '{{demand_id}}': dicData['demand_id'],
                '{{order_id}}': dicData['order_id'],
            }

        if strKey == 'nopay'  :
            dicMap = {

                '{{demand_title}}': dicData['demand_title'],
                '{{demand_id}}': dicData['demand_id'],
            }

        if strKey == 'reg':
            dicMap = {
                '{{nickname}}': dicData['nickname'],

            }
        if strKey == 'settlement':
            dicMap = {
                '{{nickname}}': dicData['nickname'],
                '{{demand_title}}': dicData['demand_title'],
                '{{demand_id}}': dicData['demand_id'],
            }

        return self.multiple_replace(self.dicMsg[strKey], dicMap)

# 替换多个
    def multiple_replace(self,text, adict):
        rx = re.compile('|'.join(map(re.escape, adict)))

        def one_xlat(match):
            return adict[match.group(0)]

        return rx.sub(one_xlat, text)
    def send_message(self,userId,strKey,dicDate):
        #print 10
        content = self.msg(strKey,dicDate)
        #print content
        self.messageModel.insert({
            'key': 'user_id,type, info, status, create_time',
                'val': '\'{uId}\', \'{type}\', \'{info}\',{status}, {time}'.format(
                    uId=userId, type=0, info=content, status=1, time=int(self.time.time())
                )
            })
        if self.model.db.status != 200:
            return {'statusCode': 601}
        return {'statusCode': 200}


# -*- coding: utf-8 -*-
from __future__ import print_function

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

import os
import io
import getopt
import time
import numpy as np
import soundfile as sf
from scikits.samplerate import resample
import codecs
import random
random.seed(42)

"""
noisify.py:
    Adds noise to a given audio file.
    Input is metadata.csv-directory, noise-file and noise-percentage
    If the noise-file is longer than the audio-file, it will choose randomly from
    the noise-file a segment that is then added to the audio-file
    Random-SEED is set to 42, in order to have reproducible results.

    It assumes the original wav-files to be in <metadata.csv>-dir/wavs
    It creates new wavs in <metadata.csv>-dir/wavs/<noise_filename> (w/o ext) in lower-case

Copyright (c) 2019 Imdat Solak
All rights reserved.

Created: 2018-02-16 12:30 CET, KA
Updated: 2018-02-24 18:00 CET, ISO
    - Fixed indent
    - Simplified usage
    - switched to our metadata.csv-format
Updated: 2018-02-26 08:20 CET, ISO
    - Added support for random noise selection
"""

def resample_noise_file(sr_audio, sr_noise, data_noise):
    start1 = time.time()
    if sr_noise != 0:
        resampling_rate = float(sr_audio) / float(sr_noise)
        data_noise = resample(data_noise, resampling_rate, "sinc_best").astype(np.int16)
    end1 = time.time()
    # print("0:", end1-start1)
    return data_noise

def check_noise(filename):
    try:
        audio_info = sf.info(filename)
        data_noise, sr_noise = sf.read(filename, dtype='int16')
    except:
        return False, None, None

    if audio_info.format != 'WAV':
        print("Invalid noise file format {} for the file:{}".format(audio_info.format, fileName))
        return False, None, None
    elif audio_info.channels >= 2:
        data_noise = data_noise[:, 0]
        print("Noise file has two channel and kept just first channel...")
    elif audio_info.subtype != 'PCM_16':
        print(type(audio_info.subtype), audio_info.subtype)
        print("Samples must be {} for the file:{}".format(audio_info.subtype, fileName))
        data_noise = data_noise.astype(np.int16)

    return True, data_noise, sr_noise

def create_noise(new_audio_filename, audio_filename, data_audio, data_noise, sr_audio, noise_percent):
    # min_len = min(len(data_audio), len(data_noise))
    l_audio = len(data_audio)
    l_noise = len(data_noise)
    if l_audio <= l_noise:
        # We want to extract randomly a part of noise data if the noise data is
        # larger than the audio-file
        noise_start = random.randint(0, l_noise - l_audio - 1)
        data_noise = data_noise[noise_start:noise_start + l_audio]
    else:
        new_noise = np.empty((0,))
        for i in range(int(l_audio/l_noise)+1):
            new_noise = np.concatenate((new_noise, data_noise), axis=0)
        data_noise = new_noise[:l_audio]
      # data_noise = np.pad(data_noise, (0, l_audio - l_noise%l_audio), 'constant')
    noised = (data_audio[:] * (1 - noise_percent)) + (data_noise[:] * noise_percent)
    noised = noised.astype(np.int16)
    sf.write(new_audio_filename, noised, sr_audio)

def noisify(meta_filename, noise_filename, noise_percent):
    is_noise, data_noise, sr_noise_orig = check_noise(noise_filename)
    if is_noise:
        noise_name = os.path.splitext(os.path.basename(noise_filename))[0].lower()
        print('Using noise-name: \'{}\''.format(noise_name))
        sr_noise = sr_noise_orig
        source_dir = os.path.dirname(meta_filename)
        wavs_dir = os.path.join(source_dir, 'wavs')
        out_dir = os.path.join(source_dir, 'wavs', noise_name)
        meta_outname = os.path.join(source_dir, 'metadata-{}.csv'.format(noise_name))
        meta_data = codecs.open(meta_filename, 'r', 'utf-8').readlines()
        num_entries = len(meta_data)
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)
        out_meta = []
        for i, line in enumerate(meta_data):
            filename, orig_text, clean_text = line.strip().split('|')
            print('{:-7d} /{:-7d}: {}.wav{}'.format(i, num_entries, filename, ' '*30), end='\r')
            if filename.startswith('$'):
                # Filename is an absolute path...
                filename = filename[1:]
                infile = os.path.join(source_dir, filename + '.wav')
            else:
                infile = os.path.join(wavs_dir, filename + '.wav')
            outfile = os.path.join(out_dir, filename + '.wav')
            if os.path.exists(infile):
                data_audio, sr_audio = sf.read(infile, dtype='int16')
            else:
                data_audio = None
                sr_audio = None
            if data_audio is not None:
                if sr_audio != sr_noise:
                    noise_data = resample_noise_file(sr_audio, sr_noise, data_noise)
                    sr_noise = sr_audio
                create_noise(outfile, infile, data_audio, noise_data, sr_audio, noise_percent)
                out_meta.append(u'{}/{}|{}|{}'.format(noise_name, filename, orig_text, clean_text))
        print('\nSaving new metadata... ', end='')
        sys.stdout.flush()
        outf = codecs.open(meta_outname, 'w', 'utf-8')
        for l in out_meta:
            print(l, file=outf)
        outf.close()
        print('done')
        print('Added meta-file: {}'.format(meta_outname))
    else:
        print('Invalid Noise data. Exiting!')

def usage():
    print('Missing or Wrong Parameters.')
    print('Usage:')
    print('  python noisify.py -m <meta-path> -n <noise-file> -N <noise-name> -p <percent>')
    print('      -m <metadata.csv-path>   The full path to the metadata.csv-file')
    print('      -n <noise_file>           Full path to the noise file')
    print('      -p <percent>              Noise percentage (0.0 < p < 1.0)')
    sys.exit(1)

if __name__ == '__main__':
    try:
        options, arguments = getopt.getopt(sys.argv[1:], 'm:n:p:', ['meta', 'noise_file', 'percent'])
    except getopt.GetoptError:
        usage()

    meta_filename = None
    noise_filename = None
    noise_percent = 0.0

    for opt, arg in options:
        if opt in ['-m', '--meta']:
         meta_filename = arg
        elif opt in ('-n', '--noise_file'):
         noise_filename = arg
        elif opt in ('-p', '--percent'):
            noise_percent = float(arg)
        elif opt in ("-h", "--help"):
            usage()

    if meta_filename is not None and noise_filename is not None and (0 < noise_percent < 1):
        noisify(meta_filename, noise_filename, noise_percent)
    else:
        usage()


"""
Sylvia Liu
122090359
"""

import os
from os import path
import errno
from glob import glob
import cv2
import numpy as np
import poisson

IMG_EXTENSIONS = ["png", "jpg", "jpeg", "JPG", "tif", "tiff", "bmp"]

# Define a function to find image files, and return a tuple.
def collect(img_path, extension=IMG_EXTENSIONS):
    files = sum(map(glob, [img_path + ext for ext in extension]), [])
    return files

# Define a function to normalize the mask.
def normalize(image):
    mask = np.atleast_3d(image).astype(float) / 255.
    mask[mask != 1] = 0
    mask = mask[:, :, 0]
    return mask

# Find the input folder.
subfolders = os.walk("input")
next(subfolders)

for dirpath, dirname, filenames in subfolders:

    # Select the latest input folder.
    input_dir = os.path.split(dirpath)[-1]
    output_dir = os.path.join("output", input_dir)

    # Show which folder is being executed.
    print("input " + input_dir)

    # Find the original images.
    source_path = collect(os.path.join(dirpath, 'source.'))
    target_path = collect(os.path.join(dirpath, 'target.'))
    mask_path = collect(os.path.join(dirpath, 'mask.'))

    # Read images.
    source_img = cv2.imread(source_path[0], cv2.IMREAD_COLOR)
    target_img = cv2.imread(target_path[0], cv2.IMREAD_COLOR)
    mask_img = cv2.imread(mask_path[0], cv2.IMREAD_GRAYSCALE)
    print("Catch the images.")

    # Execute the mask image.
    mask = normalize(mask_img)

    channels = source_img.shape[-1]  # Represents the number of image channels.
    composite = [poisson.img_edit(source_img[:, :, i], target_img[:, :, i], mask) for i in range(channels)]
    result = cv2.merge(composite)  # Merge the channels back.

    # Create the output directory.
    try:
        os.makedirs(output_dir)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

    # Write the result image.
    cv2.imwrite(path.join(output_dir, 'result.png'), result)
    print("Work Done.")


from myUtils.FoldersManager import *
from myUtils.ImageProcessing import *

# Este script permite recorrer las carpetas y archivos de un directorio y los copia en otro, realizando un aumentado de datos.
# El aumentado de datos como parametros tiene:
# - Factor de aumentado
# - Lista de efectos a realizar
# Por cada imagen realizará aleatoriamente "factor de aumentado" veces uno de los procesos de la lista de efectos.

if __name__ == '__main__':
    path = "C:/Users/TTe_J/Downloads/tiny-imagenet-200/train"
    destPath = "C:/Users/TTe_J/Downloads/tiny-imagenet-200/train_aug"

    # CARACTERISTICAS DISPONIBLES
    # filtros = {"blur", "resize", "transpose_rotate"}
    # tipos = {"rangeInt", "rangeDouble", "list": rFilterList}
    # rotacion/transposicion = [FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM, ROTATE_90, ROTATE_180, ROTATE_270, TRANSPOSE] Image.method

    # [factor de aumentado, {'filtro': (tipo, [rango/lista])}]
    augFactor = 5
    filters = [augFactor, {"blur": ("rangeInt", [2,10]), "resize": ("rangeDouble", [0.15,2.5]),
                           "transpose_rotate": ("list", [Image.FLIP_LEFT_RIGHT, Image.FLIP_TOP_BOTTOM, Image.ROTATE_90,
                                                         Image.ROTATE_180, Image.ROTATE_270, Image.TRANSPOSE])}]

    createFolders(destPath, True) # Crea el root
    getInto(path, destPath, imgFilters, filters)


from py2max import Patcher

def test_varname():
    p = Patcher('outputs/test_varname.maxpat')
    osc1 = p.add_textbox('cycle~ 440', varname='osc')
    gain = p.add_textbox('gain~', varname="volume")
    dac = p.add_textbox('ezdac~')
    p.add_line(osc1, gain)
    p.add_line(gain, dac)
    p.save()

if __name__ == '__main__':
    test_varname()


import numpy as np

# Softmax calculation for one sample
# Assume that a is the activation at five different nodes
a = np.random.randn(5)
expa =np.exp(a)
answer = expa/expa.sum()

# Softmax calculation for 100 sample
A = np.random.randn(100, 5)
expA = np.exp(A)
Answer = expA/expA.sum(axis=1, keepdims=True)  # sum along rows, keepdims=True for dividing
                                               # each row of matrix by the corresponding element of the vector


"""Handling of objective functions and objective quantities."""

from abc import ABC, abstractmethod
import numpy as np
import meep as mp
from .filter_source import FilteredSource
from matplotlib import pyplot as plt

class ObjectiveQuantitiy(ABC):
    @abstractmethod
    def __init__(self):
        return
    @abstractmethod
    def register_monitors(self):
        return
    @abstractmethod
    def place_adjoint_source(self):
        return
    @abstractmethod
    def __call__(self):
        return
    @abstractmethod
    def get_evaluation(self):
        return

class EigenmodeCoefficient(ObjectiveQuantitiy):
    def __init__(self,sim,volume,mode,forward=True,k0=None,**kwargs):
        '''
        '''
        self.sim = sim
        self.volume=volume
        self.mode=mode
        self.forward = 0 if forward else 1
        self.normal_direction = None
        self.k0 = k0
        self.eval = None
        self.EigenMode_kwargs = kwargs
        return

    def register_monitors(self,fcen,df,nf):
        self.fcen=fcen
        self.df=df
        self.nf=nf

        self.monitor = self.sim.add_mode_monitor(self.fcen,self.df,self.nf,mp.FluxRegion(center=self.volume.center,size=self.volume.size))
        self.normal_direction = self.monitor.normal_direction
        return self.monitor

    def place_adjoint_source(self,dJ,dt):
        '''Places an equivalent eigenmode monitor facing the opposite direction. Calculates the
        correct scaling/time profile.

        dJ ........ the user needs to pass the dJ/dMonitor evaluation
        dt ........ the timestep size from sim.fields.dt of the forward sim
        '''
        dJ = np.atleast_1d(dJ)
        # determine starting kpoint for reverse mode eigenmode source
        direction_scalar = 1 if self.forward else -1
        if self.k0 is None:
            if self.normal_direction == 0:
                k0 = direction_scalar * mp.Vector3(x=1)
            elif self.normal_direction == 1:
                k0 = direction_scalar * mp.Vector3(y=1)
            elif self.normal_direction == 2:
                k0 == direction_scalar * mp.Vector3(z=1)
        else:
            k0 = direction_scalar * self.k0

        # -------------------------------------- #
        # Get scaling factor
        # -------------------------------------- #
        # combine source for multi-output objective functions (i.e. multiple frequencies)
        if dJ.ndim == 2:
            dJ = np.sum(dJ,axis=1)

        # Determine the correct resolution scale factor
        if self.sim.cell_size.y == 0:
            dV = 1/self.sim.resolution
        elif self.sim.cell_size.z == 0:
            dV = 1/self.sim.resolution * 1/self.sim.resolution
        else:
            dV = 1/self.sim.resolution * 1/self.sim.resolution * 1/self.sim.resolution
        da_dE = 0.5*(dV * self.cscale)
        scale = da_dE * dJ * 1j * 2 * np.pi * self.freqs / np.array([self.time_src.fourier_transform(f) for f in self.freqs]) # final scale factor
        if self.freqs.size == 1:
            # Single frequency simulations. We need to drive it with a time profile.
            src = self.time_src
            amp = scale
        else:
            # TODO: In theory we should be able drive the source without normalizing out the time profile.
            # But for some reason, there is a frequency dependent scaling discrepency. It works now for
            # multiple monitors and multiple sources, but we should figure out why this is.
            src = FilteredSource(self.time_src.frequency,self.freqs,scale,dt,self.time_src) # generate source from braodband response
            amp = 1
        # generate source object
        self.source = mp.EigenModeSource(src,
                    eig_band=self.mode,
                    direction=mp.NO_DIRECTION,
                    eig_kpoint=k0,
                    amplitude=amp,
                    size=self.volume.size,
                    center=self.volume.center,
                    **self.EigenMode_kwargs)

        return self.source

    def __call__(self):
        # We just need a workable time profile, so just grab the first available time profile and use that.
        self.time_src = self.sim.sources[0].src

        # Eigenmode data
        ob = self.sim.get_eigenmode_coefficients(self.monitor,[self.mode],**self.EigenMode_kwargs)
        self.eval = np.squeeze(ob.alpha[:,:,self.forward]) # record eigenmode coefficients for scaling
        self.cscale = ob.cscale # pull scaling factor

        # record all freqs of interest
        self.freqs = np.atleast_1d(mp.get_eigenmode_freqs(self.monitor))

        return self.eval
    def get_evaluation(self):
        '''Returns the requested eigenmode coefficient.
        '''
        try:
            return self.eval
        except AttributeError:
            raise RuntimeError("You must first run a forward simulation before resquesting an eigenmode coefficient.")


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils import weight_norm, spectral_norm

class DiscriminatorR(torch.nn.Module):
    def __init__(self, hp, resolution):
        super(DiscriminatorR, self).__init__()

        self.resolution = resolution
        self.LRELU_SLOPE = hp.mpd.lReLU_slope

        norm_f = weight_norm if hp.mrd.use_spectral_norm == False else spectral_norm

        self.convs = nn.ModuleList([
            norm_f(nn.Conv2d(1, 32, (3, 9), padding=(1, 4))),
            norm_f(nn.Conv2d(32, 32, (3, 9), stride=(1, 2), padding=(1, 4))),
            norm_f(nn.Conv2d(32, 32, (3, 9), stride=(1, 2), padding=(1, 4))),
            norm_f(nn.Conv2d(32, 32, (3, 9), stride=(1, 2), padding=(1, 4))),
            norm_f(nn.Conv2d(32, 32, (3, 3), padding=(1, 1))),
        ])
        self.conv_post = norm_f(nn.Conv2d(32, 1, (3, 3), padding=(1, 1)))

    def forward(self, x):
        fmap = []

        x = self.spectrogram(x)
        x = x.unsqueeze(1)
        for l in self.convs:
            x = l(x)
            x = F.leaky_relu(x, self.LRELU_SLOPE)
            fmap.append(x)
        x = self.conv_post(x)
        fmap.append(x)
        x = torch.flatten(x, 1, -1)

        return fmap, x

    def spectrogram(self, x):
        n_fft, hop_length, win_length = self.resolution
        x = F.pad(x, (int((n_fft - hop_length) / 2), int((n_fft - hop_length) / 2)), mode='reflect')
        x = x.squeeze(1)
        x = torch.stft(x, n_fft=n_fft, hop_length=hop_length, win_length=win_length, center=False, return_complex=False) #[B, F, TT, 2]
        mag = torch.norm(x, p=2, dim =-1) #[B, F, TT]

        return mag

class MultiResolutionDiscriminator(torch.nn.Module):
    def __init__(self, hp):
        super(MultiResolutionDiscriminator, self).__init__()
        self.resolutions = eval(hp.mrd.resolutions)
        self.discriminators = nn.ModuleList(
            [DiscriminatorR(hp, resolution) for resolution in self.resolutions]
        )

    def forward(self, x):
        ret = list()
        for disc in self.discriminators:
            ret.append(disc(x))

        return ret  # [(feat, score), (feat, score), (feat, score)]


#
#
# File not used at this point
#

import mss
import numpy as np
import time

class MSSShot:

    # an object that continously yields screenshots as numpy arrays

    def __init__(self, top=False, left=False, width=False, height=False):
        self.top = top
        self.left = left
        self.width = width
        self.height = height
        self.quit = False
        self.init_time = time.time()

    def __call__(self):
        # if any params in self are missing, it will just shot the whole screen
        # yields (time passed since function was called, image as numpy array)
        run = True
        with mss.mss() as sct:
            if all([bool(i) for i in [self.width, self.height]]):
                monitor = {"top": self.top, "left": self.left, "width": self.width, "height": self.height}
            else:
                monitor = False
            if monitor:
                run = True
                while run:
                    image = np.array(sct.grab(monitor))
                    yield (time.time()-self.init_time, image)
                    # mss yields tensors of dim BGRA (Blue, Green, Red, Alpha)
                    if self.quit:
                        run = False
            else:
                run = True
                while run:
                    image = np.array(sct.grab(sct.monitors[1]))
                    yield (time.time()-self.init_time, image)
                    if self.quit:
                        run = False
        self.quit = False

    def quit_progr(self):
        self.quit = True

class CV2Shot:

    def __init__(self, f_rate):
        self.f_rate = f_rate
        self.init_time = time.time()

    def __call__(self, path):
        pass

if __name__ == '__main__':
    pass


from itertools import combinations
from collections import Counter

def solution(orders, course):
    answer = []

    for num in course:
        array = []
        for order in orders:
            order = sorted(order)
            array.extend(list(combinations(order, num)))

        count = Counter(array)

        if count:
            if max(count.values()) >= 2:
                for key, value in count.items():
                    if value == max(count.values()):
                        answer.append("".join(key))

    return sorted(answer)

print(solution(["ABCFG", "AC", "CDE", "ACDE", "BCFG", "ACDEH"], [2,3,4]))
print(solution(["ABCDE", "AB", "CD", "ADE", "XYZ", "XYZ", "ACD"], [2,3,5]))
print(solution(["XYZ", "XWY", "WXA"], [2,3,4]))

# from collections import defaultdict
# from itertools import combinations

# def solution(orders, course):
#     alpha = []
#     for order in orders:
#         for o in order:
#             if o not in alpha:
#                 alpha.append(o)

#     alpha.sort()
#     answer = []
#     for c in course:
#         combi = combinations(alpha, c)
#         c_dict = defaultdict(list)
#         for com in combi:
#             tmp = ''.join(com)
#             cnt = 0
#             for order in orders:
#                 for i in tmp:
#                     if i not in order:
#                         break
#                 else:
#                     cnt += 1
#             if cnt >= 2:
#                 c_dict[cnt].append(tmp)
#         try:
#             max_cnt = max(list(c_dict.keys()))
#             for i in c_dict[max_cnt]:
#                 answer.append(i)
#         except:
#             pass

#     answer_sorted = []
#     for a in answer:
#         tmp = []
#         for i in a:
#             tmp.append(i)
#         tmp = ''.join(tmp)
#         answer_sorted.append(tmp)

#     answer_sorted.sort()
#     return answer_sorted


from typing import List

from webdnn.backend.webgl.attributes.channel_mode import ChannelMode, ChannelModeEnum
from webdnn.backend.webgl.generator import WebGLDescriptorGenerator
from webdnn.backend.webgl.kernel import Kernel
from webdnn.backend.webgl.kernel_code import KernelCode
from webdnn.backend.webgl.kernels.util import get_output_position, change_order, convert_coord, texture_shape, texture_stride
from webdnn.graph.axis import Axis
from webdnn.graph.operators.col2im import Col2Im
from webdnn.graph.order import OrderNHWC, Order
from webdnn.util.misc import mul

@WebGLDescriptorGenerator.register_handler(Col2Im)
def col2im(op: Col2Im) -> List[Kernel]:
    col = op.inputs["col"]
    im = op.outputs["im"]

    assert col.order.check_same_axes(Order([Axis.N, Axis.H, Axis.W, Axis.KH, Axis.KW, Axis.C]))
    assert col.order.axes_dict[Axis.KH] + 2 == col.order.axes_dict[Axis.KW] + 1 == col.order.axes_dict[Axis.C] == 5
    assert im.order.check_same_axes(OrderNHWC)
    assert ChannelMode.get(col) == ChannelModeEnum.R
    assert ChannelMode.get(im) == ChannelModeEnum.R

    col_shape = col.shape[0:3] + (mul(col.shape[3:6]),)
    col_stride = [mul(col_shape[i + 1:]) for i in range(len(col_shape))]
    col_order = Order(col.order.axes[0:3] + (Axis.C,))

    code = KernelCode(["""
void main() {
    ivec4 variable_position_im = """, change_order(get_output_position(im), im.order, OrderNHWC), f""";

    int n = variable_position_im.x;
    int h1 = variable_position_im.y;
    int w1 = variable_position_im.z;
    int c1 = variable_position_im.w;

    float sum = 0.0;

    for (int kh = 0; kh < {op.KH}; kh++) {{
        int h2 = (h1 + {op.PH} - kh) / {op.SH};
        if (mod(h1 + {op.PH} - kh, {op.SH}) != 0 || h2 < 0 || h2 >= {col.shape_dict[Axis.H]}) continue;

        for (int kw = 0; kw < {op.KW}; kw++) {{
            int w2 = (w1 + {op.PW} - kw) / {op.SW};
            if (mod(w1 + {op.PW} - kw, {op.SW}) != 0 || w2 < 0 || w2 >= {col.shape_dict[Axis.W]}) continue;

            int khkwc1 = (kh * {op.KW} + kw) * {im.shape_dict[Axis.C]} + c1;

            sum += texture2D(""", col, ",", convert_coord(change_order("vec4(n, h2, w2, khkwc1)", OrderNHWC, col_order),
                                                          col_shape, col_stride,
                                                          texture_shape(col)[:2][::-1], texture_stride(col)[:2][::-1]), """).r;
        }
    }

    gl_FragColor.r = sum;
}
"""], name=op.__class__.__name__)
    source = code.generate()
    return [Kernel(
        source,
        code.name,
        code.samplers,
        code.uniforms,
        im
    )]


from transformers import (
    AutoTokenizer,
    TextDataset,
    DataCollatorForLanguageModeling,
    Trainer,
    TrainingArguments,
    AutoModelWithLMHead,
)

def load_dataset(train_path, test_path, tokenizer):
    train_dataset = TextDataset(
        tokenizer=tokenizer, file_path=train_path, block_size=256
    )

    test_dataset = TextDataset(tokenizer=tokenizer, file_path=test_path, block_size=256)

    data_collator = DataCollatorForLanguageModeling(
        tokenizer=tokenizer,
        mlm=False,
    )

    return train_dataset, test_dataset, data_collator

# Freezing the lower layers increases the training speed and reduces the memory requirement.
# Depending on your task you may want to freeze all layers and train addition layers that you are adding to the model
# or unfreeze as many layers that you can affort training with a reasonable batchsize.
def freeze_lower_layers():
    for param in model.base_model.parameters():
        param.requires_grad = False

    for param in (
        model.base_model.h[23].parameters() or model.base_model.h[22].parameters()
    ):
        param.requires_grad = True

# load model
model = AutoModelWithLMHead.from_pretrained("bolbolzaban/gpt2-persian")

# freeze lower layers and only train top layers
freeze_lower_layers()

# load dataset
tokenizer = AutoTokenizer.from_pretrained("bolbolzaban/gpt2-persian")
train_dataset, test_dataset, data_collator = load_dataset(
    "./data/train.txt", "./data/test.txt", tokenizer
)

# train
training_args = TrainingArguments(
    output_dir="./model",
    overwrite_output_dir=True,
    num_train_epochs=5,
    # Set the batch size to a maximum value that could fit into GPU memory,
    # for example 12 is the largest batch size that could work on a 6gb GPU when training the last to layers
    per_device_train_batch_size=12,
    per_device_eval_batch_size=12,
    eval_steps=1000,
    save_steps=1000,
    warmup_steps=500,
)

trainer = Trainer(
    model=model,
    args=training_args,
    data_collator=data_collator,
    train_dataset=train_dataset,
    eval_dataset=test_dataset,
)

trainer.train()

# save
trainer.save_model()


class Hero:
    def __init__(self, nama, darah, serang, kekuatan):
        self.nama = nama
        self.darah = darah
        self.serang = serang
        self.kekuatan = kekuatan

    def nyerang(self, Hero):
        print(self.nama + ' menyerang ' + Hero.nama)
        Hero.diserang(self, self.serang)

    def diserang(self, Hero, attackpower_lawan):
        print(self.nama + ' diserang ' + Hero.nama)
        attack_diterima = attackpower_lawan / self.kekuatan

        # membuat informasi damage
        self.darah -= attack_diterima
        print("seranga terasa : " + str(attack_diterima))
        print('darah ' + self.nama + ' tersisa ' + str(self.darah))

sniper = Hero('sniper', 100, 200, 20)
balmond = Hero('balmomd', 100, 100, 50)
sniper.nyerang(balmond)

print("/n")

sniper.nyerang(balmond)


def getSeatId(line):
  row, rowLo, rowHi = 0, 0, 127
  col, colLo, colHi = 0, 0, 7
  for c in line:
    if c == 'F':
      row = (rowLo + rowHi) // 2
      rowHi = row
    elif c == 'B':
      # python 3 rounding is banker's rounding (round half to even).
      # Ex: round(4.5) == 4, round(5.5) == 6
      # We add 0.1 to make it always round 0.5 up
      row = round((rowLo + rowHi + 0.1) / 2)
      rowLo = row
    elif c == 'R':
      col = round((colLo + colHi + 0.1)/2)
      colLo = col
    elif c == 'L':
      col = (colLo + colHi) // 2
      colHi = col
  return row*8+col

if __name__=="__main__":
  f = open("day05_input1.txt")

  maxId = -1
  seatIds = []
  for line in f:
    seatId = getSeatId(line)
    seatIds.append(seatId)
    maxId = max(maxId, seatId)
  print("maxId:%d" % maxId)
  seatIds.sort()
  for i in range(0, len(seatIds)-1):
    if seatIds[i] + 1 != seatIds[i+1]:
      print("my seat id: %d" % (seatIds[i]+1))
      break
  f.close()


import sys
import os, os.path

from Database.DBConst import DBConst

from Util.Config import Config
from Util.Const import Const

from ming import create_datastore
from ming.odm import ThreadLocalODMSession

class Database(object):
	__INSTANCE = None
	# Wrapper around Mondodb.

	def __init__(self, config):
		assert Database.__INSTANCE is None, "Database.__init__ - Init called twice access through getInstance"
		self.m_url	= config[DBConst.DATABASE_URL]
		self.m_port		= config[DBConst.DATABASE_PORT]
		self.m_schema		= config[DBConst.DATABASE_SCHEMA]
		print(self.m_url, self.m_port, self.m_schema)
		self.m_datastore	= create_datastore("mongodb://{0:s}:{1:s}/{2:s}".format(self.m_url, self.m_port, self.m_schema))
		self.m_session		= ThreadLocalODMSession(bind=self.m_datastore)
		Database.__INSTANCE	= self

	@classmethod
	def getInstance(cls):
		if not(Database.__INSTANCE):
			print("Creating Database", Const.CONFIG_PATH)
			with Config(Const.CONFIG_PATH) as config:
				db = Database(config)
		return Database.__INSTANCE.m_session

	@classmethod
	def flush(cls):
		db = Database.getInstance()
		db.flush()

if __name__ == "__main__":
	sys.path.append(os.path.join(os.getcwd(), ".."))
	print("Setting up database...")
	with Config(Const.CONFIG_PATH) as config:
		instance = Database.getInstance()


#!C:/Python27/ArcGIS10.2/python.exe
#-*- coding:utf-8 -*-
"""
#============================================
#
# Project: mmanagecelery
# Name: The file name is begin
# Purpose:
# Auther: Administrator
# Tel: 17372796660
#
#============================================
#
"""

import sys
from celery_app import clipfromsde

reload(sys)
sys.setdefaultencoding('utf8')

def begin():
    MEDIA=u"F:/data"
    taskname=u"task2018"
    user_id=u"1"
    clipfromsde.clipfromsde(u"I49E019021,I49E019022,I49E019023", MEDIA,taskname,user_id)

    #clipfromsde.clipfromsde.delay(u"I49E019021,I49E019022,I49E019023", MEDIA,taskname)
    # clipfromsde.clipfromsde.delay(6, 9)
    return True

if __name__ == "__main__":
    begin()


from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_conversion
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops.linalg import linear_operator
from tensorflow.python.ops.linalg import linear_operator_util
from tensorflow.python.util.tf_export import tf_export

__all__ = ["LinearOperatorFullMatrix"]

@tf_export("linalg.LinearOperatorFullMatrix")
@linear_operator.make_composite_tensor
class LinearOperatorFullMatrix(linear_operator.LinearOperator):
  """`LinearOperator` that wraps a [batch] matrix.

  This operator wraps a [batch] matrix `A` (which is a `Tensor`) with shape
  `[B1,...,Bb, M, N]` for some `b >= 0`.  The first `b` indices index a
  batch member.  For every batch index `(i1,...,ib)`, `A[i1,...,ib, : :]` is
  an `M x N` matrix.

  ```python
  # Create a 2 x 2 linear operator.
  matrix = [[1., 2.], [3., 4.]]
  operator = LinearOperatorFullMatrix(matrix)

  operator.to_dense()
  ==> [[1., 2.]
       [3., 4.]]

  operator.shape
  ==> [2, 2]

  operator.log_abs_determinant()
  ==> scalar Tensor

  x = ... Shape [2, 4] Tensor
  operator.matmul(x)
  ==> Shape [2, 4] Tensor

  # Create a [2, 3] batch of 4 x 4 linear operators.
  matrix = tf.random.normal(shape=[2, 3, 4, 4])
  operator = LinearOperatorFullMatrix(matrix)
  ```

  #### Shape compatibility

  This operator acts on [batch] matrix with compatible shape.
  `x` is a batch matrix with compatible shape for `matmul` and `solve` if

  ```
  operator.shape = [B1,...,Bb] + [M, N],  with b >= 0
  x.shape =        [B1,...,Bb] + [N, R],  with R >= 0.
  ```

  #### Performance

  `LinearOperatorFullMatrix` has exactly the same performance as would be
  achieved by using standard `TensorFlow` matrix ops.  Intelligent choices are
  made based on the following initialization hints.

  * If `dtype` is real, and `is_self_adjoint` and `is_positive_definite`, a
    Cholesky factorization is used for the determinant and solve.

  In all cases, suppose `operator` is a `LinearOperatorFullMatrix` of shape
  `[M, N]`, and `x.shape = [N, R]`.  Then

  * `operator.matmul(x)` is `O(M * N * R)`.
  * If `M=N`, `operator.solve(x)` is `O(N^3 * R)`.
  * If `M=N`, `operator.determinant()` is `O(N^3)`.

  If instead `operator` and `x` have shape `[B1,...,Bb, M, N]` and
  `[B1,...,Bb, N, R]`, every operation increases in complexity by `B1*...*Bb`.

  #### Matrix property hints

  This `LinearOperator` is initialized with boolean flags of the form `is_X`,
  for `X = non_singular, self_adjoint, positive_definite, square`.
  These have the following meaning:

  * If `is_X == True`, callers should expect the operator to have the
    property `X`.  This is a promise that should be fulfilled, but is *not* a
    runtime assert.  For example, finite floating point precision may result
    in these promises being violated.
  * If `is_X == False`, callers should expect the operator to not have `X`.
  * If `is_X == None` (the default), callers should have no expectation either
    way.
  """

  def __init__(self,
               matrix,
               is_non_singular=None,
               is_self_adjoint=None,
               is_positive_definite=None,
               is_square=None,
               name="LinearOperatorFullMatrix"):
    r"""Initialize a `LinearOperatorFullMatrix`.

    Args:
      matrix:  Shape `[B1,...,Bb, M, N]` with `b >= 0`, `M, N >= 0`.
        Allowed dtypes: `float16`, `float32`, `float64`, `complex64`,
        `complex128`.
      is_non_singular:  Expect that this operator is non-singular.
      is_self_adjoint:  Expect that this operator is equal to its hermitian
        transpose.
      is_positive_definite:  Expect that this operator is positive definite,
        meaning the quadratic form `x^H A x` has positive real part for all
        nonzero `x`.  Note that we do not require the operator to be
        self-adjoint to be positive-definite.  See:
        https://en.wikipedia.org/wiki/Positive-definite_matrix#Extension_for_non-symmetric_matrices
      is_square:  Expect that this operator acts like square [batch] matrices.
      name: A name for this `LinearOperator`.

    Raises:
      TypeError:  If `diag.dtype` is not an allowed type.
    """
    parameters = dict(
        matrix=matrix,
        is_non_singular=is_non_singular,
        is_self_adjoint=is_self_adjoint,
        is_positive_definite=is_positive_definite,
        is_square=is_square,
        name=name
    )

    with ops.name_scope(name, values=[matrix]):
      self._matrix = linear_operator_util.convert_nonref_to_tensor(
          matrix, name="matrix")
      self._check_matrix(self._matrix)

      super(LinearOperatorFullMatrix, self).__init__(
          dtype=self._matrix.dtype,
          is_non_singular=is_non_singular,
          is_self_adjoint=is_self_adjoint,
          is_positive_definite=is_positive_definite,
          is_square=is_square,
          parameters=parameters,
          name=name)

  def _check_matrix(self, matrix):
    """Static check of the `matrix` argument."""
    allowed_dtypes = [
        dtypes.float16,
        dtypes.float32,
        dtypes.float64,
        dtypes.complex64,
        dtypes.complex128,
    ]

    matrix = tensor_conversion.convert_to_tensor_v2_with_dispatch(
        matrix, name="matrix"
    )

    dtype = matrix.dtype
    if dtype not in allowed_dtypes:
      raise TypeError(f"Argument `matrix` must have dtype in {allowed_dtypes}. "
                      f"Received: {dtype}.")

    if matrix.shape.ndims is not None and matrix.shape.ndims < 2:
      raise ValueError(f"Argument `matrix` must have at least 2 dimensions. "
                       f"Received: {matrix}.")

  @property
  def matrix(self):
    """The matrix defining this operator."""
    return self._matrix

  def _shape(self):
    return self._matrix.shape

  def _shape_tensor(self):
    return array_ops.shape(self._matrix)

  def _matmul(self, x, adjoint=False, adjoint_arg=False):
    return math_ops.matmul(
        self._matrix, x, adjoint_a=adjoint, adjoint_b=adjoint_arg)

  def _solve(self, rhs, adjoint=False, adjoint_arg=False):
    return self._dense_solve(rhs, adjoint=adjoint, adjoint_arg=adjoint_arg)

  def _to_dense(self):
    return self._matrix

  @property
  def _composite_tensor_fields(self):
    return ("matrix",)

  @property
  def _experimental_parameter_ndims_to_matrix_ndims(self):
    return {"matrix": 2}


import configparser
import gettext

from io import StringIO

from otopi import constants as otopicons
from otopi import common
from otopi import plugin
from otopi import util

from ovirt_setup_lib import dialog

from ovirt_hosted_engine_ha.lib import heconflib

from ovirt_hosted_engine_setup import constants as ohostedcons

def _(m):
    return gettext.dgettext(message=m, domain='ovirt-hosted-engine-setup')

@util.export
class Plugin(plugin.PluginBase):
    """Answer file fetcher plugin"""

    def __init__(self, context):
        super(Plugin, self).__init__(context=context)
        self._config = configparser.ConfigParser()
        self._config.optionxform = str
        self._tmp_ans = None

    def _check_he35_from_answerfile(self):
        if self.environment[
            ohostedcons.StorageEnv.CONF_IMG_UUID
        ] is None:
            return True
        return False

    def _fetch_answer_file(self):
        self.logger.debug('_fetch_answer_file')

        source = heconflib.get_volume_path(
            self.environment[ohostedcons.StorageEnv.SP_UUID],
            self.environment[ohostedcons.StorageEnv.SD_UUID],
            self.environment[ohostedcons.StorageEnv.CONF_IMG_UUID],
            self.environment[ohostedcons.StorageEnv.CONF_VOL_UUID],
        )
        self.logger.debug('fetching from: ' + str(source))

        if not heconflib.validateConfImage(self.logger, source):
            msg = _('Unable to get the answer file from the shared storage')
            self.logger.error(msg)
            raise RuntimeError(msg)
        self._tmp_ans = heconflib.extractConfFile(
            self.logger,
            source,
            ohostedcons.FileLocations.HECONFD_ANSWERFILE,
        )
        self.logger.debug(
            'Answer file form the shared storage: {content}'.format(
                content=self._tmp_ans
            )
        )
        self.logger.info(_('Answer file successfully loaded'))

    def _parse_answer_file(self):
        buf = StringIO(unicode(self._tmp_ans))
        try:
            self._config.readfp(buf)
        except configparser.Error as ex:
            msg = _(
                'The answer file on the shared storage is invalid, '
                'please check and fix it: {ex}'
            ).format(ex=ex)
            self.logger.error(msg)
            raise RuntimeError(msg)

        for name, value in self._config.items(
            otopicons.Const.CONFIG_SECTION_DEFAULT
        ):
            try:
                value = common.parseTypedValue(value)
                self.logger.debug('%s=%s' % (name, value))
            except Exception as e:
                raise RuntimeError(
                    _(
                        "Cannot parse configuration file key "
                        "{key} at section {section}: {exception}"
                    ).format(
                        key=name,
                        section=otopicons.Const.CONFIG_SECTION_DEFAULT,
                        exception=e,
                    )
                )
            self.environment[name] = value

    @plugin.event(
        stage=plugin.Stages.STAGE_INIT,
    )
    def _init(self):
        self.environment.setdefault(
            ohostedcons.FirstHostEnv.SKIP_SHARED_STORAGE_ANSWERF,
            False
        )
        self.environment.setdefault(
            ohostedcons.FirstHostEnv.DEPLOY_WITH_HE_35_HOSTS,
            None
        )

    @plugin.event(
        name=ohostedcons.Stages.REQUIRE_ANSWER_FILE,
        stage=plugin.Stages.STAGE_CUSTOMIZATION,
        after=(
            ohostedcons.Stages.DIALOG_TITLES_S_STORAGE,
            ohostedcons.Stages.CONFIG_STORAGE_LATE,
        ),
        before=(
            ohostedcons.Stages.DIALOG_TITLES_E_STORAGE,
        ),
        condition=lambda self: (
            self.environment[ohostedcons.CoreEnv.UPGRADING_APPLIANCE] or
            self.environment[ohostedcons.CoreEnv.ROLLBACK_UPGRADE]
        ),
    )
    def _customization(self):
        if self.environment[
            ohostedcons.FirstHostEnv.SKIP_SHARED_STORAGE_ANSWERF
        ]:
            if self.environment[otopicons.CoreEnv.CONFIG_FILE_APPEND] is None:
                raise RuntimeError(
                    _(
                        'Cannot deploy Hosted Engine on additional hosts '
                        'without access to the configuration used on '
                        'other hosts'
                    )
                )
            else:
                self.logger.info(
                    _(
                        "Setup will proceed assuming that '{answfile}' "
                        "was correctly and completely generated "
                        "on another HE host"
                    ).format(
                        answfile=self.environment[
                            otopicons.CoreEnv.CONFIG_FILE_APPEND
                        ]
                    )
                )
        else:
            _ovf = self.environment[ohostedcons.VMEnv.OVF]
            if (
                self.environment[ohostedcons.StorageEnv.CONF_IMG_UUID] and
                self.environment[ohostedcons.StorageEnv.CONF_VOL_UUID]
            ):
                self._fetch_answer_file()
                self._parse_answer_file()
                if self.environment[ohostedcons.CoreEnv.UPGRADING_APPLIANCE]:
                    self.environment[ohostedcons.VMEnv.OVF] = _ovf
                    self.environment[ohostedcons.VMEnv.CDROM] = None
                    self.environment[
                        ohostedcons.CloudInit.GENERATE_ISO
                    ] = ohostedcons.Const.CLOUD_INIT_GENERATE
                    self.environment[
                        ohostedcons.CloudInit.EXECUTE_ESETUP
                    ] = True
                    self.environment[
                        ohostedcons.VMEnv.AUTOMATE_VM_SHUTDOWN
                    ] = True
                    self.environment[
                        ohostedcons.Upgrade.BACKUP_SIZE_GB
                    ] = self.environment[ohostedcons.StorageEnv.IMAGE_SIZE_GB]
                    self.environment[
                        ohostedcons.StorageEnv.IMAGE_SIZE_GB
                    ] = None
            else:
                self.logger.error(_(
                    'Unable to find the hosted-engine configuration volume '
                    'on the shared storage.'
                ))

        # Prevent directly deploying an HE host from 3.6 if the answer file of
        # the first host is at 3.5 since in that case the configuration volume
        # is not on the shared storage and we are not going to create it.
        he_answerfile_from_35 = self._check_he35_from_answerfile()
        if self.environment[
            ohostedcons.FirstHostEnv.DEPLOY_WITH_HE_35_HOSTS
        ] is None and he_answerfile_from_35:
            self.dialog.note(
                text=_(
                    'It seems like your existing HE infrastructure was '
                    'deployed with version 3.5 (or before) and never upgraded '
                    'to current release.\n'
                    'Mixing hosts with HE from 3.5 (or before) and current '
                    'release is not supported.\n'
                    'Please upgrade the existing HE hosts to current release '
                    'before adding this host.\n'
                    'Please check the log file for more details.\n'
                ),
            )
            self.environment[
                ohostedcons.FirstHostEnv.DEPLOY_WITH_HE_35_HOSTS
            ] = dialog.queryBoolean(
                dialog=self.dialog,
                name='OVEHOSTED_PREVENT_MIXING_HE_35_CURRENT',
                note=_(
                    'Replying "No" will abort Setup.\n'
                    'Continue? '
                    '(@VALUES@) [@DEFAULT@]: '
                ),
                prompt=True,
                default=False,
            )

        if not self.environment[
            ohostedcons.FirstHostEnv.DEPLOY_WITH_HE_35_HOSTS
        ] and he_answerfile_from_35:
            raise RuntimeError(
                _('other hosted-engine host is still at version 3.5')
            )

# vim: expandtab tabstop=4 shiftwidth=4


from __future__ import absolute_import, print_function, unicode_literals

import json
import logging
import time

from api import datamanage_api

logger = logging.getLogger(__name__)

class MetricDictMapper(dict):
    def __init__(self, original_dict, metric_type):
        if metric_type == "range":
            new_dict = {
                "biz_count": original_dict["biz_count"],
                "proj_count": original_dict["project_count"],
                "depth": original_dict["depth"],
                "node_count": original_dict["node_count_list"],
                "weighted_node": original_dict["weighted_node_count"],
                "range_score": original_dict["range_score"],
                "normalized_range_score": original_dict["normalized_range_score"],
                "app_code_count": original_dict["app_code_count"],
            }
        elif metric_type == "asset_value":
            new_dict = {
                "normalized_range_score": original_dict["normalized_range_score"],
                "heat_score": original_dict["heat_score"],
                "importance_score": original_dict["importance_score"],
                "asset_value_score": original_dict["asset_value_score"],
            }
        elif metric_type == "lifecycle":
            new_dict = {
                "assetvalue_to_cost": original_dict["assetvalue_to_cost"],
            }
        elif metric_type == "importance":
            new_dict = {
                "dataset_score": original_dict["dataset_score"],
                "biz_score": original_dict["biz_score"],
                "is_bip": 1 if original_dict["is_bip"] else 0,
                "oper_state_name": original_dict["oper_state_name"],
                "oper_state": original_dict["oper_state"],
                "bip_grade_name": original_dict["bip_grade_name"],
                "bip_grade_id": original_dict["bip_grade_id"],
                "app_important_level_name": original_dict["app_important_level_name"],
                "app_important_level": original_dict["app_important_level"],
                "project_score": original_dict["project_score"],
                "active": 1 if original_dict["active"] else 0,
                "importance_score": original_dict["importance_score"],
            }
        elif metric_type == "heat":
            new_dict = {
                "query_count": original_dict["query_count"],
                "queue_service_count": original_dict["queue_service_count"],
                "heat_score": original_dict["heat_score"],
            }
        elif metric_type == "cost":
            new_dict = {
                "hdfs_capacity": float(original_dict["hdfs_capacity"]),
                "tspider_capacity": float(original_dict["tspider_capacity"]),
                "total_capacity": float(original_dict["total_capacity"]),
                "capacity_score": float(original_dict["capacity_score"]),
            }

        super(MetricDictMapper, self).__init__(**new_dict)

def send_metric_to_kafka(dataset_id, dataset_type, metric_dict, metric_type):
    """
    指标写kafka topic
    :param dataset_id:
    :param dataset_type:
    :param metric_dict:
    :return:
    """
    kafka_metric_dict = MetricDictMapper(metric_dict, metric_type)
    send_message_to_kafka(
        "{}_metric".format(metric_type), dataset_id, dataset_type, kafka_metric_dict
    )

def send_heat_metric_to_kafka(dataset_id, dataset_type, metric_dict):
    """
    热度指标写kafka topic
    :param dataset_id:
    :param dataset_type:
    :param heat_metric:
    :return:
    """
    heat_metric = MetricDictMapper(metric_dict, "heat")
    send_message_to_kafka("heat_metrics", dataset_id, dataset_type, heat_metric)

def send_message_to_kafka(
    measurement, dataset_id, dataset_type, metric_dict, dimension_dict={}
):
    """
    :param measurement: kafka measurement
    :param dataset_id: 数据集id
    :param dataset_type: 数据集类型
    :param metric_dict: 数据集
    :return:
    """
    # 1）database统一为monitor_custom_metrics
    # 2）measurement为不同的表
    # 3）tags（维度字段）统一有dataset_id和dataset_type
    # 4）time统一为当前时间戳
    time_stamp = int(time.time() - 24 * 3600)
    message = {
        "database": "monitor_custom_metrics",
        measurement: {
            "tags": {"dataset_id": dataset_id, "dataset_type": dataset_type},
        },
        "time": time_stamp,
    }
    # 5) 添加维度字段
    if dimension_dict:
        message[measurement]["tags"].update(dimension_dict)
    # 6）会对度量字段做处理
    property_list = list(metric_dict.keys())
    for each_property in property_list:
        message[measurement][each_property] = metric_dict.get(each_property)
    # 7）send_message_to_kafka, 上报influxdb
    message_str = json.dumps(message)
    try:
        report_dict = datamanage_api.influx_report(
            {"message": message_str, "kafka_topic": "bkdata_monitor_metrics591"},
            retry_times=3,
            raise_exception=True,
        )
    except Exception as e:
        logger.error(
            "influx_report error:{}, dataset_id:{}".format(e.message, dataset_id)
        )
        return
    if not report_dict.is_success() or not report_dict.data:
        logger.error(
            "influx_report error:{}, dataset_id:{}".format(
                report_dict.message, dataset_id
            )
        )

def send_rt_day_query_count_to_kafka(
    dataset_id, dataset_type, metric_dict, dimension_dict
):
    """
    热度指标(rt日查询量)写kafka topic
    包括按天统计的查询量，app_code
    :param dataset_id:
    :param dataset_type:
    :param count:
    :return:
    """
    send_message_to_kafka(
        "heat_related_metric", dataset_id, dataset_type, metric_dict, dimension_dict
    )