from Ga import *
from main import *

### Benchmarking sequences
# length of 20
S1 = "HPHPPHHPHPPHPHHPPHPH"
# length of 48
H1 = "HPHHPPHHHHPHHHPPHHPPHPHHHPHPHHPPHHPPPHPPPPPPPPHH"

# length of 90
F_90_1 = "PPHHHPPPHHPPPPHHPHHHHHHPHPHPHHPHHHHHPHHHPHPHHHHPHHPPPPHHHPHPHPPHHHPHHPHPHPPHHHPPPPHHPPHPPP"
# length of 135
S1_2 = "HHHHPHHHHHHPPHHPHHHHHHHHPHHPHHHHHHHHHHPPHHPPPPPHHPHHHHHHHHPHPPHHPPPHHHHHHHHPHHHHHHPPHHHHHHHPHPPHHHHHHHHHPPHHPPPHHHHHHHPHHPHHHHHHHPPHHHH"
# length of 200
R1 = "PPPHPHHPHHPPPHPHPPPPHPHHPPHPHHHHHPPHH" \
     "PPHHHHHHPPHPPHHPPHPHPHHHHHPHHPHHHPPPHHHPHHPPHPHPPHPPP" \
     "HPPHPPHPPHHHPHHHPHPPHPHHPHHHHPHPHHHPHHHPPPPPPHHHHHHPPPPP" \
     "PPPHHHPPHPHPPPHPHPHPHHPPHHPPPPHHHHHHPPPHHPPPPPHPPPHHPP"

# demonstrating neighbours
def plotneighbours():
    vect = [[1, 1, 0], [1, -1, 0], [-1, -1, 0], [-1, 1, 0],
            # up
            [1, 0, 1], [0, -1, 1], [-1, 0, 1], [0, 1, 1],
            # down
            [1, 0, -1], [0, -1, -1], [-1, 0, -1], [0, 1, -1]]
    ax = plt.axes(projection='3d')

    zline, xline, yline = [], [], []

    for point in vect:
        print(point)

        xline.append(point[0])
        yline.append(point[1])
        zline.append(point[2])

    xline.append(0)
    yline.append(0)
    zline.append(0)

    # lines
    # ax.plot3D(xline, yline, zline)
    # points
    ax.scatter3D(xline, yline, zline)
    # ax.scatter3D(xline[0], yline[0], zline[0])
    ax.scatter3D(xline[-1], yline[-1], zline[-1])

    axes = plt.gca()
    axes.set_xlim([-2, 2])
    axes.set_ylim([-2, 2])
    axes.set_zlim([-2, 2])

    plt.show()

# demostrating mutation
def plotmutation():
    Pep1 = Pep([],"0101101001")

    Pep2 = Pep1.mutate()

    Pep1.plot(1,2)
    Pep2.plot(2,2)

    for a,b in zip(Pep1.aas,Pep2.aas):
        print(a,b)

# demonstrating crossover
def plotcrossover():
    Pep1 = Pep([],"010111110101001")
    Pep2 = Pep([],"010110111001001")

    c1,c2 = Pep1.crossover(Pep2)

    Pep1.plot(1,4)
    Pep2.plot(2,4)
    c1.plot(3,4)
    c2.plot(4,4)

    for a,b,c,d in zip(Pep1.aas,Pep2.aas,c1.aas,c2.aas):
        print(a,b,c,d)


import tvo
import numpy as np
import torch as to
from torch import Tensor

def init_W_data_mean(
    data: Tensor,
    H: int,
    std_factor: float = 0.25,
    dtype: to.dtype = to.float64,
    device: to.device = None,
) -> Tensor:
    """Initialize weights W based on noisy mean of the data points.

    :param data: Data set, is (N, D).
    :param H: Number of basis functions to be generated.
    :param std_factor: Scalar to control amount of standard deviation of additive noise
    :param dtype: dtype of output Tensor. Defaults to torch.float64.
    :param device: torch.device of output Tensor. Defaults to tvo.get_device().
    :returns: Weight matrix W with shape (D,H).
    """
    device_ = tvo.get_device() if device is None else device
    data_nanmean = to.from_numpy(np.nanmean(data.detach().cpu().numpy(), axis=0)).to(
        dtype=dtype, device=device_
    )
    var = init_sigma2_default(data, dtype, device_)
    return data_nanmean.repeat((H, 1)).t() + std_factor * to.sqrt(var) * to.randn(
        (len(data_nanmean), H), dtype=dtype, device=device_
    )

def init_sigma2_default(
    data: Tensor, dtype: to.dtype = to.float64, device: to.device = None
) -> Tensor:
    """Initialize scalar sigma parameter based on variance of the data points.

    :param data: Data set, is (N, D).
    :param dtype: dtype of output Tensor. Defaults to torch.float64.
    :param device: torch.device of output Tensor. Defaults to tvo.get_device().
    :returns: Scalar sigma parameter.

    Returns the mean of the variance in each dimension d=1,...,D.
    """
    _device = tvo.get_device() if device is None else device
    var = to.from_numpy(np.nanvar(data.detach().cpu().numpy(), axis=0)).to(
        device=_device, dtype=dtype
    )
    return to.mean(var) + to.tensor([0.001], device=_device, dtype=dtype)

def init_pies_default(
    H: int, crowdedness: float = 2.0, dtype: to.dtype = to.float64, device: to.device = None
):
    """Initialize pi parameter based on given crowdedness.

    :param H: Length of pi vector.
    :param crowdedness: Average crowdedness corresponding to sum of elements in vector pi.
    :param dtype: dtype of output Tensor. Defaults to torch.float64.
    :param device: torch.device of output Tensor. Defaults to tvo.get_device().
    :returns: Vector pi.
    """

    if device is None:
        device = tvo.get_device()
    return to.full((H,), fill_value=crowdedness / H, dtype=dtype, device=device)


import pandas as pd

metadata = pd.read_csv("Metadata.csv", index_col = 'Sample' )
bpm = pd.read_csv('suis_biovar_files/Blast_Primer_Matching.csv', converters={'File ID': lambda x: str(x)})
files = list(bpm['File ID'].unique())

sample_matching = {} # key = shortened sample name, value = full sample name
for sample in metadata.index.values:
	sample_matching[sample[4:13]]=sample

biovar_1 = [773, 774, 775, 424, 425, 426, 196, 197, 198]
biovar_2 = [773, 774, 775, 550, 551, 552, 277, 278, 279]
biovar_3 = [773, 774, 775, 298, 299, 300, 196, 197, 198]
biovar_4 = [773, 774, 775, 613, 614, 615, 196, 197, 198]
biovar_5 = [773, 613, 277, 196]

def biovar(file):
	file_df = bpm[bpm['File ID']==file].fillna(0)
	p1 = file_df['Primer One (BMEI1426-BMEI1427)'].to_list() # amplicons derived from primer 1
	p2 = file_df['Primer Two (BR1080f-BR1080r)'].to_list() # amplicons derived from primer 2
	p3 = file_df['Primer Three (BMEI1688-BMEI1687)'].to_list() # amplicons derived from primer 3
	p4 = file_df['Primer Four (BMEI0205f-BMEI0205r)'].to_list() # amplicons derived from primer 4

	amplicons = p1+p2+p3+p4
	amplicons = [int(amp) for amp in amplicons if amp != 0] # removing the nan values
	amplicons = list(set(amplicons))

	if len(amplicons)<3: # every biovar class requires at least three primer matches, therefore if there are less then three amplicons
		return('No Matched Biovar')
	else:
		bv1 = all(amps in biovar_1 for amps in amplicons)
		bv2 = all(amps in biovar_2 for amps in amplicons)
		bv3 = all(amps in biovar_3 for amps in amplicons)
		bv4 = all(amps in biovar_4 for amps in amplicons)
		bv5 = all(amps in amplicons for amps in biovar_5)

		if bv1: # if all of the amplicons found in the given file are found in the biovar 1 criteria
			return('Biovar 1')
		if bv2: # if all of the amplicons returned for one sample are found in the biovar 2 criteria
			return('Biovar 2')
		if bv3: # if all of the amplicons returned for one sample are found in the biovar 3 criteria
			return('Biovar 3')
		if bv5: # if all of the amplicons returned for one sample are found in the biovar 5 criteria
			return('Biovar 5')
		if bv4: # if all of the amplicons returned for one sample are found in the biovar 4 criteria
			return('Biovar 4')
		else:
			return('No Matched Biovar')

df = pd.DataFrame(index = ['Biovar Classification'])
for file in files:
	df[sample_matching[file]] = [biovar(file)]

df = df.T
df.to_csv('suis_biovar_files/Suis_Biovar.csv')


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

# In[1]:

import torch
from torch.utils.data import Dataset, DataLoader
from enum import Enum
import pandas as pd
from PIL import Image
import numpy as np
import torchvision.transforms as transforms
import matplotlib.pyplot as plt

# In[2]:

class Modes(Enum):
    train = 1
    test = 2

class ODIR5K(Dataset):
    def __init__(self, mode, augmentations):
        super(ODIR5K, self).__init__()
        self.mode = mode
        self.csv_path = f"/pytorch_fake{self.mode.name}.csv"
        self.csv = pd.read_csv(self.csv_path)
        self.aug = augmentations

    def __len__(self):
        return len(self.csv)

    def __getitem__(self, index):
        csv_line = self.csv.iloc[index]
        img_path = csv_line['path']
        label = torch.tensor(int(csv_line['label']))

        image = Image.open(img_path).convert("RGB")

        if self.aug is not None:
            image = self.aug(image)

        return image, label

# In[6]:

if __name__ == "__main__":
    train_idx, val_idx = train_test_split(list(range(len(odir_train))), test_size=0.20, random_state=42)
    t = transforms.Compose([transforms.Resize((500, 500)),
                            transforms.ToTensor()])

    ds = ODIR5K(Modes.train, t, train_idx)
    dl = DataLoader(ds, batch_size = 4, shuffle=True)
    batch = next(iter(dl))

    for img in batch['image']:
#         print(b)
        img = np.transpose(img.numpy(), (1, 2, 0))
        plt.figure()
        plt.imshow(img)
        plt.show()

# In[ ]:


def toys(w):
    cnt = 0
    x = -1
    w.sort()
    for i in w:
        if i > x:
            x = i + 4
            cnt += 1
    return cnt

if __name__ == '__main__':
    n = int(input().strip())
    w = list(map(int, input().rstrip().split()))
    result = toys(w)
    print(result)


import os, django
import re
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nta_library.settings")
django.setup()
from books.models import Book, Borrower
from faker import Faker

fakegen = Faker()
def populate(N=5):
    rows = []
    for entry in range(N):
        fake_name = fakegen.name()
        fake_auther = fakegen.name()
        fake_type = fakegen.name()
        fake_isbn = fakegen.zipcode()
        fake_copies = fakegen.zipcode()
        fake_issue_date = fakegen.date()
        fake_date = fakegen.date()
        fake_out = fakegen.boolean()
        # New entry
        boooooks = Book.objects.get_or_create(name=fake_name,auther=fake_auther,type=fake_type,isbn = fake_isbn,copies=fake_copies,
        issue_date=fake_issue_date,out=fake_out  )[0]
        borrrrrowerrrr= Borrower.objects.get_or_create(borrower_book = boooooks, name= fake_name,
        department = fake_name, borrow_date = fake_date,return_date = fake_date, blacklisted = fake_out)[0]
        rows.append(boooooks)
    return rows

populate(5)


from pathlib import Path

import pandas as pd
import pytest
from typer.testing import CliRunner

from ilids.commands.experiments import typer_app

runner = CliRunner(mix_stderr=False)

@pytest.mark.handcrafted_files(("actionclip_sequences.csv", "actionclip_sequences"))
@pytest.mark.ckpt_files((Path("actionclip") / "vit-b-16-8f.pt", "actionclip_ckpt"))
def test_actionclip(actionclip_sequences: Path, actionclip_ckpt: Path, tmp_path: Path):
    output_path = tmp_path / "output.pkl"

    input_df = pd.read_csv(actionclip_sequences)

    min_input_path = tmp_path / "min_input.csv"
    input_df.head(n=2).to_csv(min_input_path)

    result = runner.invoke(
        typer_app,
        [
            "actionclip",
            str(actionclip_ckpt),
            str(min_input_path),
            str(output_path),
        ],
    )

    assert result.exit_code == 0

    assert output_path.exists() and output_path.is_file()

    df = pd.read_pickle(output_path)

    assert len(df) == 2
    assert len(df.columns) == 512, df.columns


"""
124. Binary Tree Maximum Path Sum

https://leetcode.com/problems/binary-tree-maximum-path-sum/
"""
from typing import Optional

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:

    res = -(10**20)

    def maxPathSum(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        left = self.go(root.left)
        right = self.go(root.right)

        return max(left + right + root.val, root.val, self.res, root.val + left, root.val + right)

    def go(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0

        left = self.go(root.left)
        right = self.go(root.right)

        maxOne = max(root.val, left + root.val, right + root.val)
        maxAll = max(maxOne, left + right + root.val)

        self.res = max(self.res, maxAll)

        return maxOne

def main():
    s = Solution()
    print(s.maxPathSum(TreeNode(-10, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7)))))
    print(s.maxPathSum(TreeNode(2, TreeNode(-1))))

if __name__ == '__main__':
    raise(SystemExit(main()))


# coding:utf-8
"""
-------------------------------------------------
   File Name：     rnn_model
   Description :
   Author :       deep
   date：          18-1-11
-------------------------------------------------
   Change Activity:
                   18-1-11:

   __author__ = 'deep'
-------------------------------------------------
"""

import sys
import os
import time
import codecs
import argparse
import re

parentdir = os.path.dirname(os.path.dirname(
    os.path.abspath(__file__)))
sys.path.insert(0, parentdir)

grandfatherdir = os.path.dirname(os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))))

sys.path.insert(0, grandfatherdir)
sys.path.append(grandfatherdir)

import numpy as np
import pickle
import tensorflow as tf
from tqdm import tqdm

from sklearn.metrics import roc_auc_score

import rnn.data_helper as data_helper
from rnn.config import RNNConfig as Config
from rnn.model import RNN_Model
import utils.embedding.vector_helper as vector_helper

def run_epoch(model, config, session, data, metadata,
              train_op=None, verbose=2, display=False):
    dp = config.keep_prob
    if train_op is None:
        train_op = tf.no_op()
        dp = 1

    total_steps = len(data[0]) // config.batch_size

    total_loss = []
    accuracy = []
    error = []

    _roc_auc_scores_ = []

    # shuffle data
    # p = np.random.permutation(len(data[0]))
    questions, q_lens, answers = data

    if not display:
        for step in tqdm(range(total_steps)):
            # index = range(step * config.batch_size,
            #               (step + 1) * config.batch_size)
            b = step * config.batch_size
            e = (step + 1) * config.batch_size
            batch_size_data = questions[b:e], q_lens[b:e], answers[b:e]
            question_input = data_helper.vectorize_data(
                batch_size_data, metadata)
            question_lens_input = np.array(q_lens[b:e])
            answer_input = np.array(answers[b:e])
            # print(qp[:2])
            # print(il_vc)
            feed = {model.question_placeholder: question_input,
                    model.question_len_placeholder: question_lens_input,
                    model.target_placeholder: answer_input,
                    model.dropout_placeholder: dp}

            # _ = session.run([train_op], feed_dict=feed)
            loss, pred, _ = session.run(
                [model.loss, model.pred, train_op], feed_dict=feed)

            # print loss
            # total_loss.append(loss)
            # if verbose and step % verbose == 0:
            #     sys.stdout.write('\r{} / {} : loss = {}'.format(
            #         step, total_steps, np.mean(total_loss)))
            #     sys.stdout.flush()

        return display

    for step in range(total_steps):
        # index = range(step * config.batch_size,
        #               (step + 1) * config.batch_size)
        b = step * config.batch_size
        e = (step + 1) * config.batch_size
        batch_size_data = questions[b:e], q_lens[b:e], answers[b:e]
        question_input = data_helper.vectorize_data(batch_size_data, metadata)
        question_lens_input = np.array(q_lens[b:e])
        answer_input = np.array(answers[b:e])

        feed = {model.question_placeholder: question_input,
                model.question_len_placeholder: question_lens_input,
                model.target_placeholder: answer_input,
                model.dropout_placeholder: dp}

        loss, pred, _ = session.run(
            [model.loss, model.predict_proba_op, train_op], feed_dict=feed)

        # if train_writer is not None:
        #     train_writer.add_summary(
        #         summary, num_epoch * total_steps + step)

        batch_answers = batch_size_data[2]
        batch_questions = batch_size_data[0]

        """
        AUC score
        http://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_auc_score.html#sklearn.metrics.roc_auc_score

        >>> import numpy as np
        >>> from sklearn.metrics import roc_auc_score
        >>> y_true = np.array([0, 0, 1, 1])
        >>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
        >>> roc_auc_score(y_true, y_scores)
        0.75
        """
        for i in range(len(batch_answers)):
            try:
                _roc_auc_score_ = roc_auc_score(batch_answers[i], pred[i])
                _roc_auc_scores_.append(_roc_auc_score_)
            except ValueError:
                pass
        # accuracy.append(np.sum(pred == batch_answers) /
        #                 float(len(batch_answers)))
        # idx2candid = metadata['idx2candid']
        # for Q, A, P in zip(batch_questions, batch_answers, pred):
        #     # print(A, P)
        #     if A != P:
        #         # Q = ''.join([idx2w.get(idx, '')
        #         #              for idx in Q.astype(np.int32).tolist()])
        #         Q = ''.join(Q)
        #         Q = Q.replace(vector_helper.UNK, '')
        #         A = idx2candid[A]
        #         P = idx2candid[P]
        #         error.append((Q, A, P))

        total_loss.append(loss)
        if verbose and step % verbose == 0:
            sys.stdout.write('\r{} / {} : loss = {} --- roc_auc = {}'.format(
                step, total_steps, np.mean(total_loss), np.mean(_roc_auc_scores_)))
            sys.stdout.flush()

    if verbose:
        sys.stdout.write('\r')

    return np.mean(total_loss), np.mean(_roc_auc_scores_), error

def train(config, restore=False):
    # load info
    with open(config.data_config["metadata_path"] + 'info.pkl', 'rb') as f:
        info = pickle.load(f)
    partitions = info['partitions']
    # sentences_embedding = metadata['sentences_embedding']

    model = RNN_Model(config, info)

    print('==> Training RNN start\n')
    # context.train_info.append("Training RNN start")
    print('==> initializing variables')
    # context.train_info.append("initializing variables")
    init = tf.global_variables_initializer()
    saver = tf.train.Saver()

    with tf.Session() as session:
        session.run(init)

        best_train_epoch = 0
        best_train_loss = float('inf')
        best_train_accuracy = 0
        prev_epoch_loss = float('inf')

        if restore:
            print('==> restoring weights')
            # context.train_info.append("restoring weights")
            _check_restore_parameters(sess=session,
                                      saver=saver, model_path=config.data_config["ckpt_path"])
            # saver.restore(
            #     session, config.data_config["ckpt_path"] + 'rnn.weights')

        print('==> commence training')
        # context.train_info.append("commence training")

        for epoch in range(config.max_epochs):
            partition = epoch % partitions
            train_data, valid_data, metadata = load_data(config.data_config, partition=partition)
            if not (epoch % config.interval_epochs == 0 and epoch > 1):
                # context.train_info.append("Epoch"+str(epoch))
                print('Epoch {}, using partion {}'.format(epoch, partition))
                _ = run_epoch(model=model,
                              config=config,
                              session=session,
                              data=train_data,
                              metadata=metadata,
                              train_op=model.train_op,
                              verbose=2,
                              display=False)
            else:
                # context.train_info.append("Epoch" + str(epoch))
                print('Epoch {}, using partion {}'.format(epoch, partition))
                start = time.time()
                train_loss, train_accuracy, train_error = run_epoch(model=model,
                                                                    config=config,
                                                                    session=session,
                                                                    data=train_data,
                                                                    metadata=metadata,
                                                                    train_op=model.train_op,
                                                                    verbose=2,
                                                                    display=True)
                valid_loss, valid_accuracy, valid_error = run_epoch(model=model,
                                                                    config=config,
                                                                    session=session,
                                                                    data=train_data,
                                                                    metadata=metadata,
                                                                    train_op=model.train_op,
                                                                    verbose=2,
                                                                    display=True)
                if train_accuracy > 0.99:
                    for e in train_error:
                        print(e)
                # context.train_info.append("train_loss:" + str(train_loss) + "&nbsp&nbsptrain_accuracy:" + str(train_accuracy) + "&nbsp&nbsptrain_error:" + str(train_error))
                # context.train_info.append("valid_loss:" + str(valid_loss) + "&nbsp&nbspvalid_accuracy:" + str(valid_accuracy) + "&nbsp&nbspvalid_error:" + str(valid_error))
                print('Training loss: {}'.format(train_loss))
                print('Validation loss: {}'.format(valid_loss))
                print('Training accuracy: {}'.format(train_accuracy))
                print('Vaildation accuracy: {}'.format(valid_accuracy))

                if train_loss < best_train_loss:
                    print('Saving weights and updating best_train_loss:{} -> {},\
                            best_train_accuracy:{} -> {}'.format(best_train_loss, train_loss,
                                                                 best_train_accuracy, train_accuracy))
                    best_train_accuracy = train_accuracy
                    saver.save(
                        session, config.data_config["ckpt_path"] + 'rnn.weights')
                    best_train_loss = train_loss
                    best_train_epoch = epoch
                print('best_train_loss: {}'.format(best_train_loss))
                print('best_train_epoch: {}'.format(best_train_epoch))
                print('best_train_accuracy: {}'.format(best_train_accuracy))
                # context.train_info.append("best_train_loss:" + str(best_train_loss))
                # context.train_info.append("best_train_epoch:" + str(best_train_epoch))
                # context.train_info.append("best_train_accuracy:" + str(best_train_accuracy))
                # anneal
                if train_loss > prev_epoch_loss * config.anneal_threshold:
                    config.lr /= config.anneal_by
                    print('annealed lr to %f' % config.lr)

                prev_epoch_loss = train_loss

                # if epoch - best_val_epoch > config.early_stopping:
                #     break
                print('Total time: {}'.format(time.time() - start))
        print('==> training stop')
        # context.train_info.append("training stop")

def replace_model_path(checkpoint_path):
    pat = re.compile(r'\"(/.+/)model-\d+\"$')
    model = ''.join(pat.findall(checkpoint_path))
    text = re.sub(model, '', checkpoint_path)
    return text

def prepare_data(data_config):
    train_file_path = os.path.join(data_config['data_dir'], 'train.csv')
    num_train_lines = sum(1 for _ in open(train_file_path))
    max_scan = data_config['max_scan_lines']
    partitions = int(num_train_lines / max_scan) + 1
    print('num of train lines is {}, {} partitions will be produced..'.format(num_train_lines, partitions))
    info = dict()
    for i in range(partitions):
        metadata, data = data_helper.load_raw_data(data_config, i, partitions)

        with open(data_config["metadata_path"] + 'metadata_partition_' + str(i) + ".pkl", 'wb') as f:
            pickle.dump(metadata, f)
        with open(data_config["data_path"] + 'data_partition_' + str(i) + ".pkl", 'wb') as f:
            pickle.dump(data, f)

        info["candidate_size"] = metadata['candidate_size']
        info['candid2idx'] = metadata['candid2idx']
        info['candid2idx'] = metadata['idx2candid']
    info["partitions"] = partitions
    with open(data_config["metadata_path"] + 'info.pkl', 'wb') as f:
        pickle.dump(info, f)

def load_data(data_config, partition):
    """Loads metadata and data"""
    with open(data_config["metadata_path"] + 'metadata_partition_' + str(partition) + ".pkl", 'rb') as f:
        metadata = pickle.load(f)

    with open(data_config["data_path"] + 'data_partition_' + str(partition) + ".pkl", 'rb') as f:
        data = pickle.load(f)
    train = data['train']
    valid = data['valid']

    # embedding
    print('pre embedding')
    # context.train_info.append('pre embedding')
    sentences_embedding = data_helper.sentence_embedding(metadata)
    print('pre embedding done')
    # context.train_info.append('pre embedding done')
    # metadata['max_q_len'] = max_len
    # metadata['max_sen_len'] = max_len
    metadata['sentences_embedding'] = sentences_embedding

    return train, valid, metadata

def _check_restore_parameters(sess, saver, model_path):
    """ Restore the previously trained parameters if there are any. """
    print("--checking directory:", model_path)
    ckpt = tf.train.get_checkpoint_state(model_path)
    if ckpt and ckpt.model_checkpoint_path:
        print("Loading parameters for the model")
        saver.restore(sess, ckpt.model_checkpoint_path)
    else:
        print("Initializing fresh parameters for the model")

def parse_args(args):
    parser = argparse.ArgumentParser(description='LSTM')
    parser.add_argument("-r", "--restore", action='store_true',
                        help="restore previously trained weights")
    parser.add_argument("-s", "--strong_supervision",
                        help="use labelled supporting facts (default=false)")
    parser.add_argument("-l", "--l2_loss", type=float,
                        default=0.001, help="specify l2 loss constant")
    parser.add_argument("-n", "--num_runs", type=int,
                        help="specify the number of model runs")
    parser.add_argument(
        "-p", "--infer", action='store_true', help="predict")
    parser.add_argument("-t", "--train", action='store_true', help="train")
    parser.add_argument("-d", "--prep_data",
                        action='store_true', help="prepare data")

    args = vars(parser.parse_args(args))
    return args

def infer(config):
    import pandas as pd
    df = pd.read_csv('/opt/luis/toxic/data/train/automata/train.csv',
                     names=["id", "comment_text", "toxic", "severe_toxic",
                            "obscene", "threat",
                            "insult", "identity_hate"], skiprows=1)
    from tqdm import tqdm
    import utils.query_util as query_util
    from utils.embedding.vector_helper import BenebotVector as VectorHelper
    vector_helper = VectorHelper()
    for index, row in df.iterrows():
        u = row['comment_text']
        _id_ = row['id']
        u = query_util.tokenize(u, char=8)
        u_embedding = [vector_helper.getVector(word) for word in u]

def main(args):
    args = parse_args(args)
    config = Config()
    args['train'] = True
    if args['prep_data']:
        data_config = config.data_config
        print('\n>> Preparing Data\n')
        begin = time.clock()
        prepare_data(data_config)
        end = time.clock()
        print('>> Preparing Data Time:{}'.format(end - begin))
        sys.exit()
    elif args['train']:
        train(config, args['restore'])
    elif args['infer']:
        infer(config)
    else:
        print('ERROR:Unknow Mode')

if __name__ == '__main__':
    main(sys.argv[1:])


def ccw(p1, p2, p3):
    return p1[0] * p2[1] + p2[0] * p3[1] + p3[0] * p1[1] - \
        (p1[1] * p2[0] + p2[1] * p3[0] + p3[1] * p1[0])

def cross():
    if x1 == x2:    # 첫번째 선분이 y축과 평행할 때(기울기가 무한)
        m2 = (y4 - y3) / (x4 - x3)      # 두번째 선분의 기울기
        k2 = y3 - m2 * x3               # 두번째 선분의 y절편
        cross_y = m2 * x1 + k2
        cross_x = x1
    elif x3 == x4:  # 두번째 선분이 y축과 평행할 때(기울기가 무한)
        m1 = (y2 - y1) / (x2 - x1)      # 첫번째 선분의 기울기
        k1 = y1 - m1 * x1               # 첫번째 선분의 y절편
        cross_y = m1 * x3 + k1
        cross_x = x3
    else:
        m1 = (y2 - y1) / (x2 - x1)      # 첫 선분의 기울기
        m2 = (y4 - y3) / (x4 - x3)      # 두번째 선분의 기울기

        k1 = y1 - m1 * x1               # 첫 선분의 y절편
        k2 = y3 - m2 * x3               # 두번째 선분의 y절편
        cross_x = (k2 - k1) / (m1 - m2)     # 두 직선의 교차점의 x 좌표
        cross_y = m1 * cross_x + k1
    return cross_x, cross_y

x1, y1, x2, y2 = map(int, input().split())
x3, y3, x4, y4 = map(int, input().split())
p1, p2 = [x1, y1], [x2, y2]
p3, p4 = [x3, y3], [x4, y4]

p12 = ccw(p1, p2, p3) * ccw(p1, p2, p4)  # p1, p2 선분으로 p3과 p4에 대한 ccw의 곱
p34 = ccw(p3, p4, p1) * ccw(p3, p4, p2)  # p3, p4 선분으로 p1과 p2에 대한 ccw의 곱
if p12 <= 0 and p34 <= 0:   # 두 선분이 교차하는 경우(한 직선 위에 있을 때 생각!)
    if p12 == 0 and p34 == 0:   # 두 선분이 한 점에서 만나거나 겹칠 때
        p1, p2 = min(p1, p2), max(p1, p2)
        p3, p4 = min(p3, p4), max(p3, p4)
        if p3 <= p2 and p1 <= p4:       # 두 선분이 만나는 경우
            if ccw(p1, p2, p3) == 0 and ccw(p1, p2, p4) == 0:   # 두 선분이 겹칠 때(한 점에서 안 만나야 한다.)
                if p3 == p2:    # p3와 p2 한 점에서 만날 때
                    print(1)
                    print(*p2)
                    exit()
                if p1 == p4:    # p1과 p4 한 점에서 만날 때
                    print(1)
                    print(*p1)
                    exit()
                print(1)        # 두 선분이 여러 점에서 겹칠 때
                exit()
            print(1)            # 두 선분이 한 점에서 만날 때(기울기가 다름)
            print(*cross())
            exit()
    else:
        print(1)
        print(*cross())
        exit()
print(0)        # 나머지는 교차하지 않는다.


import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'

import numpy as np
import gwpe

# Load data.
series = 'logisticmap'
x = np.loadtxt('../data/'+series+'.dat')
n = x.shape[0]

# Parameters.
w = 6           # word size
qdelta = 0.1
qmin = -10
qmax = 10

# Generalized Weighted Permutation Entropy.
Hlist = []
Clist = []
qlist = []
for q in np.arange(qmin,qmax+qdelta,qdelta):
    H, C = gwpe.calc(x, n, w, q)
    qlist.append(q)
    Hlist.append(H)
    Clist.append(C)
H = np.asarray(Hlist)
C = np.asarray(Clist)
q = np.asarray(qlist)

# Plot signature curve.
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter(H, C, q)#, marker=m)
ax.set_xlabel('H')
ax.set_ylabel('C')
ax.set_zlabel('q')
plt.show()
fig.savefig(series + '.png')


from .models import Rating
from .services import Method
from django.shortcuts import render
from .libraries.KohonenNet import KohonenNet
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def about(request):
    return render(request, 'banknet/about.html')

def rating_list(request, method = 'byEuclid'):
    rows = 25
    count = Rating.objects.count()
    page = request.GET.get('page')
    end = rows * int(page if page else 1)
    start = end - rows
    ratings_list = Method.make_retings(Rating.objects.all().order_by('id').reverse()[start:end], method)
    try:
        paginator = Paginator(list(range(0, start)) + list(ratings_list) + list(range(end, count)), rows)
        ratings = paginator.page(page)
    except PageNotAnInteger:
        ratings = paginator.page(1)
    except EmptyPage:
        ratings = paginator.page(paginator.num_pages)
    except Exception:
        ratings = None
    return render(request, 'banknet/rating_list.html', {'ratings': ratings})

def correct_weight(request):
    success = None
    max_samples_count = Rating.objects.count()
    if request.method == 'POST':
        success = Method.save(
            Rating.objects.all(),
            request.POST.get('samples-count', '20'),
            request.POST.get('ratio-upper-limit', '0.3'),
            request.POST.get('ratio-lower-limit', '0.0'),
            request.POST.get('ratio-step', '0.5'),
            request.POST.get('method', 'byEuclid')
        )
    return render(request, 'banknet/correct_weight.html', {
        'max_samples_count': max_samples_count,
        'success': success
    })


from clear import clear
from time import sleep
respawnPrompt = input("Type 1 to respawn. Type 2 to exit.")
if respawnPrompt == "1":
    print("Respawning...")
    sleep(4)
    clear()
    exec(open("cutscene1.py").read())
if respawnPrompt == 2:
    print("Exiting...")
    sleep(3)
    clear()
    quit()


'''Crie um programa que tenha uma tupla totalmente preenchida com uma contagem por extenso, de zero até vinte.
Seu programa deverá ler um número pelo teclado entre (0 e 20) e mostra-lo por extenso'''

contagem = ('Zero', 'Um', 'Dois', 'Três', 'Quatro', 'Cinco', 'Seis', 'Sete', 'Oito', 'Nove', 'Dez',
            'Onze', 'Doze', 'Treze', 'Catorze', 'Quinze', 'Dezesseis', 'Dezessete', 'Dezoito', 'Dezenove', 'Vinte')

numero = int(input('Digite um número entre 0 e 20: '))
while numero < 0 or numero > 20:
    numero = int(input('Número inválido. Digite um número entre 0 e 20: '))
print(f'O número digitado {numero} se escreve por extenso: {contagem[numero]}')

Mundo 03 - Estruturas Compostas/Atividade 072 - Tuplas.py


# https://www.hackerrank.com/challenges/the-birthday-bar/problem
# !/bin/python3

def birthday(s, d, m):
    count = 0
    total = sum(s[:m])

    if total == d:
        count += 1

    # sliding
    for i in range(m, len(s)):
        total += s[i]
        total -= s[i - m]
        if total == d:
            count += 1

    return count

if __name__ == '__main__':
    n = int(input().strip())
    s = list(map(int, input().rstrip().split()))
    first_multiple_input = input().rstrip().split()
    d = int(first_multiple_input[0])
    m = int(first_multiple_input[1])
    result = birthday(s, d, m)
    print(result)


import requests
from bs4 import BeautifulSoup
import pandas as pd
import re
import json
from tqdm import tqdm
import config
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def getInshortsNews(response_text):

    newsData = []
    soup = BeautifulSoup(response_text, 'lxml')
    newsCards = soup.find_all(class_='news-card')

    for card in newsCards:
        try:
            title = card.find(class_='news-card-title').find('a').text
        except AttributeError:
            title = None

        try:
            url = ('https://www.inshorts.com' + card.find(class_='news-card-title')
                   .find('a').get('href'))
        except AttributeError:
            url = None

        try:
            content = card.find(class_='news-card-content').find('div').text
        except AttributeError:
            content = None

        try:
            author = card.find(class_='author').text
        except AttributeError:
            author = None

        try:
            time = card.find(class_='time')['content']
        except AttributeError:
            time = None

        try:
            source = card.find(class_="news-card-footer news-right-box").find('a').get_text()
        except AttributeError:
            source = None
        try:
            readMoreUrl = card.find(class_='read-more').find('a').get('href')
        except AttributeError:
            readMoreUrl = None

        newsObject = {
            'title': title,
            'url': url,
            'content': content,
            'author': author,
            'time': time,
            'source':source,
            'readMoreUrl': readMoreUrl
        }
        newsData.append(newsObject)

    return newsData

if __name__=="__main__":

    # load first page:
    agent = {"User-Agent":'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
    html_doc = requests.get('https://www.inshorts.com/en/read', headers=agent).text
    min_news_id = re.search(r'min_news_id = "([^"]+)"', html_doc).group(1)

    logger.info(f"Successfully extracted the offest of first page - {min_news_id}")
    url = config.url
    news_offset = min_news_id
    n_pages = config.n_page
    news_data = []
    logger.info(f"Start scrapping the website for {n_pages} pages")
    for i in tqdm(range(n_pages)):
        while True:
            response = requests.post(url, data={"category": "", "news_offset": news_offset})
            if response.status_code == 200:
                break
            else:
                print(response.status_code)

        response_json = json.loads(response.text)
        news_article= getInshortsNews(response_json["html"]),
        news_offset = response_json["min_news_id"]
        news_data.extend(news_article[0])

    df = pd.DataFrame(news_data)
    df = df[df['source'].isin(['Hindustan Times','Reuters','Times Now','The Print','Free Press Journal','News18','Sportskeeda','Moneycontrol','Press Trust of India'])]
    df = df[df["readMoreUrl"].str.contains("twitter.com|youtube.com|youtu.be") == False].reset_index(drop=True)
    df = df.dropna().reset_index(drop=True)
    df.to_csv('../data/inshorts_data.csv', index=False)
    logger.info("Successfully scrapped data from inshorts")


#!/usr/bin/env python3

"""
Unittests for the NIR subtraction algorithm
"""

import pytest
import rasterio
from pathlib import Path

from sungc import deglint
from . import urd

# specify the path to the odc_metadata.yaml of the test datasets
data_path = Path(__file__).parent / "data"
odc_meta_file = data_path / "ga_ls8c_aard_3-2-0_091086_2014-11-06_final.odc-metadata.yaml"

# specify the sub_product
sub_product = "lmbadj"

def test_mnir_image():
    """
    Check that the generated deglinted band is nearly identical
    to the expected deglinted band
    """
    # Initiate the sunglint correction class
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    # ---------------------- #
    #     NIR subtraction    #
    # ---------------------- #
    mnir_xarrlist = g.glint_subtraction(
        vis_bands=["3"],
        corr_band="6",
        water_val=5,
    )

    sungc_band = mnir_xarrlist[0].lmbadj_green.values  # 3D array

    # path to expected sunglint corrected output from NIR subtraction
    exp_sungc_band = (
        data_path
        / "MINUS_NIR"
        / "ga_ls8c_lmbadj_3-2-0_091086_2014-11-06_final_band03-deglint-600m.tif"
    )

    # ensure that all valid sungint corrected pixels match expected
    with rasterio.open(exp_sungc_band, "r") as exp_sungc_ds:
        urd_band = urd(sungc_band[0, :, :], exp_sungc_ds.read(1), exp_sungc_ds.nodata)
        assert urd_band.max() < 0.001

def test_mnir_bands():
    """
    Ensure that glint_subtraction() raises and Exception if
    the specified vis_band_id/corr_band do not exist
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    with pytest.raises(Exception) as excinfo:
        g.glint_subtraction(
            vis_bands=["20"],  # this band id doesn't exist
            corr_band="6",
            water_val=5,
        )
    assert "is missing from bands" in str(excinfo)

    with pytest.raises(Exception) as excinfo:
        g.glint_subtraction(
            vis_bands=["3"],
            corr_band="20",  # this band id doesn't exist
            water_val=5,
        )
    assert "is missing from bands" in str(excinfo)

def test_empty_inputs():
    """
    Ensure that glint_subtraction() raises and Exception if
    the VIS and NIR band only contain nodata pixels
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    with pytest.raises(Exception) as excinfo:
        g.glint_subtraction(
            vis_bands=["3"],
            corr_band="7",  # this dummy band only contains nodata
            water_val=5,
        )
    assert "only contains a single value" in str(excinfo)

    with pytest.raises(Exception) as excinfo:
        g.glint_subtraction(
            vis_bands=["7"],  # this dummy band only contains nodata
            corr_band="6",
            water_val=5,
        )
    assert "only contains a single value" in str(excinfo)


a = []
b = {}
for _ in range(int(input())):
  a.append(input())
for x in a:
  sum = 0
  for y in x:
    if y.isdigit():
      sum += int(y)
  b[x] = sum
b = sorted(b.items(), key=lambda x: (len(x[0]), x[1], x[0]) )
for x in b:
  print(x[0])


#!/usr/bin/env python

import json
import urllib2
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('email')
parser.add_argument('certfile')

args = parser.parse_args()

req = urllib2.Request('http://localhost:25353/{}/dnsZoneLineForCert'.format(args.email))
req.add_header('Content-Type', 'application/json')

with open(args.certfile) as f:
    cert = f.read()

response = urllib2.urlopen(req, cert)
j = json.load(response)
print(j)


"""
Q1: as a senior backend engineer, you are tasked
with developing a fast in-memory data
structure to manage profile info (username
name and email) for 100million users.
1. Insert profile information
2. Find profile info of a user, given username
3. update profile information of a user, given their username
4. list all users of platform, sorted by username
Assume all usernames are unique.
"""

from lesson2 import tree_size, display_keys, User, user_dataset_2
from lesson2_BST import find, insert, fix_unbalanced_BST, update, list_all

class TreeMap:
    def __init__(self):
        self.root = None

    # insert and update
    def __setitem__(self, key, value):
        node = find(self.root, key)
        if not node:
            self.root = insert(self.root, value)
            self.root = fix_unbalanced_BST(self.root)
        else:
            update(self.root, key, value)

    def __getitem__(self, key):
        node = find(self.root, key)
        return node.value if node else None

    def __iter__(self):
        return (x for x in list_all(self.root))

    def __len__(self):
        return tree_size(self.root)

    def display(self):
        return display_keys(self.root)

# populate a new database with some users from previous list
treemap = TreeMap()
(
    treemap["john"],
    treemap["jane"],
    treemap["captainjl"],
    treemap["kathrynj"],
    treemap["chrisp"],
    treemap["scotc"],
) = user_dataset_2[0:6]
treemap.display()

Lesson 2 - BSTs, traversal, recursion/lesson2_userDB.py


from re import I
from flask import Flask

app = Flask(__name__)

@app.route("/data")
def data():
    with open("data.csv", "r") as f:
        data = f.read()
    return data

if __name__ == "__main__":
    app.run()


"""
Joseph Vita
CSCI-141 Herring
10/22/2019

birthday.py reads the file birthday20000.txt and asks the user for the minimum
number count for birthday occurences to return. prints all birthdays that occur
a minimum of min_times
"""
from dataclasses import dataclass

@dataclass(frozen=True)
class birthday:
    __slots__ = "month", "day", "year"
    month: str
    day: int
    year: int

def main():
    """
    builds birthday dictionary and asks the user to input a min count.
    prints out birthdays that occur a min of min_count times.
    :return:
    """
    bd_counts = build_dictionary("birthday20000.txt")
    min_count = int(input("Enter a minimum count: "))
    list_birthdays = birthdays_atleast(bd_counts, min_count)
    print("Birthdays occurred at least " + str(min_count) + " times:")
    print(list_birthdays)
    print()
    list_strings = to_strings(list_birthdays)
    print(list_strings)

def build_dictionary(file):
    """
    reads a file and creates a dictionary that maps a birthday class object
    to the number of times it has occurred.
    :param file:
    :return:
    """
    dict = {}
    lines = open(file)
    for line in lines:
        line = line.split(' ')
        line[2] = line[2].strip('\n')
        bday = birthday(line[0], int(line[1]), int(line[2]))
        if bday not in dict:
            dict[bday] = 1
        else:
            dict[bday] += 1

    return dict

def times_in(lst, obj):
    """
    returns the number of times obj appears in lst
    :param lst:
    :param obj:
    :return:
    """
    count = 0
    for x in lst:
        if obj == x:
            count += 1
    return count

def birthdays_atleast(bd_counts, min_count):
    """
    creates a list of birthday instances from bd_counts that occurs at least
    min_count times
    :param bd_counts:
    :param min_count:
    :return:
    """
    at_least = []
    for bday in bd_counts:
        if bd_counts[bday] >= min_count:
            at_least.append(bday)

    return at_least

def to_strings(list_birthdays):
    """
    takes a list of birthdays and creates a list of strings formatted like
    month/day/year

    :param list_birthdays:
    :return:
    """
    strlst = []
    for x in range(len(list_birthdays)):
        strlst.append(str(list_birthdays[x].month) + '/' +
                      str(list_birthdays[x].day) + '/' +
                      str(list_birthdays[x].year))
    return strlst

if __name__ == '__main__':
    main()


import sys
sys.path.insert(0,'..')
from opt import *
import json
import os
import os.path
import numpy as np
import pandas as pd
import argparse
import cv2

def cal_contact_block(row,num_frames):
  contact_frame=row['contact_frame']
  intervals=[-6,-3,0,3,6]
  contact_block=[min(contact_frame+item,num_frames )for item in intervals]
  return contact_block

def cal_pre_blocks(row,sample_interval=3,num_blocks=5,fps=30):
  contact_frame=row['contact_frame']
  num_frames = num_blocks*0.5*fps
  num_frames=int(num_frames)
  helper=[*range(0,num_frames,sample_interval)]
  helper=[-item for item in helper][::-1]
  end_frame = contact_frame - fps * 0.5
  pre_frames=[int(end_frame+item) for item in helper]
  #ensure idx of frame >0
  pre_frames=[max(1,item) for item in pre_frames]
  frames_per_block=int(len(pre_frames)/num_blocks)
  # pre_frames=1
  pre_blocks=[pre_frames[x:x+frames_per_block] for x in range(0,len(pre_frames),frames_per_block)]

  return pre_blocks

# prepare annotations for self-supervised egocentric representation learning
def make_annotations_for_RL(input_folder,output_folder):
  clip_ids = [f for f in os.listdir(input_folder) if  os.path.isfile(os.path.join(input_folder, f))]
  for i, clip_id in enumerate(clip_ids):
    print(f'{i + 1}/{len(clip_ids)} working on clip: {clip_id}')
    clip_path=os.path.join('/local/home/luohwu/EGO4D_initial_data/EGO4D_initial_data/v1/clips/',f"{clip_id[:-4]}.mp4")
    assert os.path.exists(clip_path),f"{clip_path} not exist"
    vid_cap=cv2.VideoCapture(clip_path)
    num_frames=int(vid_cap.get(cv2.CAP_PROP_FRAME_COUNT))
    vid_cap.release()
    original_annotation_file=os.path.join(input_folder,clip_id)
    target_annotation_file=os.path.join(output_folder,clip_id)
    original_annotation=pd.read_csv(original_annotation_file)
    target_annotation=original_annotation.drop_duplicates(subset='contact_frame')[['uid','clip_uid','clip_frame','contact_frame','noun','verb']]
    target_annotation['contact_block']=target_annotation.apply(cal_contact_block,num_frames=num_frames,axis=1)
    target_annotation['pre_blocks']=target_annotation.apply(cal_pre_blocks,axis=1)
    target_annotation.to_csv(target_annotation_file,index=False)
    # target_clip_folder = os.path.join(output_folder, clip_id)
    # if not os.path.exists(target_clip_folder):
    #   os.mkdir(target_clip_folder)
    # images = [image for image in os.listdir(original_clip_folder)]

if __name__=='__main__':
  input_folder=os.path.join(args.data_path,'nao_annotations')
  output_folder=os.path.join(args.data_path,'ssl_annotations')
  make_annotations_for_RL(input_folder,output_folder)


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

import matplotlib.pyplot as plt
import numpy as np
import pylab
'''
利用数组进行数据处理 简介
• NumPy数组使你可以将许多种数据处理任务表述为简洁的数组表达式(否则需 要编写循环)。用数组表达式代替循环的做法，通常被称为矢量化。
• 矢量化数组运算要比等价的纯Python方式快上一两个数量级
'''
points = np.arange(-5, 5, 0.01) # 生成100个点
xs, ys = np.meshgrid(points, points)  # xs, ys互为转置矩阵
print (xs)
print (ys)
z = np.sqrt(xs ** 2 + ys ** 2)
print (z)
# 画图
plt.imshow(z, cmap = plt.cm.gray);
plt.colorbar()
plt.title("Image plot of $\sqrt{x^2 + y^2}$ for a grid of values")
pylab.show()


import torch
import torch.nn.functional as F
import torch.nn as nn

class GraphAttentionLayer(nn.Module):
    def __init__(self, in_c, out_c, embed_dim):
        super(GraphAttentionLayer, self).__init__()
        self.in_c = in_c
        self.out_c = out_c

        self.F = F.softmax

        self.W = nn.Linear(in_c, out_c, bias=False)  # y = W * x
        self.b = nn.Parameter(torch.Tensor(out_c))

        nn.init.normal_(self.W.weight)
        nn.init.normal_(self.b)

        self.weights_pool = nn.Parameter(torch.FloatTensor(embed_dim, in_c, out_c))
        self.bias_pool = nn.Parameter(torch.FloatTensor(embed_dim, out_c))

    def forward(self, inputs, node_embeddings):
        """
        :param inputs: input features, [B, N, C].
        :param graph: graph structure, [N, N].
        :return:
            output features, [B, N, D].
        """

        graph = F.softmax(F.relu(torch.mm(node_embeddings, node_embeddings.transpose(0, 1))), dim=1)

        weights = torch.einsum('nd,dio->nio', node_embeddings, self.weights_pool)  # N, cheb_k, dim_in, dim_out
        bias = torch.matmul(node_embeddings, self.bias_pool)  # N, dim_out

        # h = self.W(inputs)  # [B, N, D]，一个线性层，就是第一步中公式的 W*h
        h = torch.einsum("nio,bni->bno", weights, inputs)  # B, cheb_k, N, dim_in

        # 下面这个就是，第i个节点和第j个节点之间的特征做了一个内积，表示它们特征之间的关联强度
        # 再用graph也就是邻接矩阵相乘，因为邻接矩阵用0-1表示，0就表示两个节点之间没有边相连
        # 那么最终结果中的0就表示节点之间没有边相连
        outputs = torch.bmm(h, h.transpose(1, 2)) * graph.unsqueeze(
            0)  # [B, N, D]*[B, D, N]->[B, N, N],         x(i)^T * x(j)

        # 由于上面计算的结果中0表示节点之间没关系，所以将这些0换成负无穷大，因为softmax的负无穷大=0
        outputs.data.masked_fill_(torch.eq(outputs, 0), -float(1e16))

        attention = self.F(outputs, dim=2)  # [B, N, N]，在第２维做归一化，就是说所有有边相连的节点做一个归一化，得到了注意力系数
        return torch.bmm(attention, h) + bias  # [B, N, N] * [B, N, D]，，这个是第三步的，利用注意力系数对邻域节点进行有区别的信息聚合

class GATSubNet(nn.Module): # 这个是多头注意力机制
    def __init__(self, in_c, hid_c, out_c, n_heads, embed_dim):
        super(GATSubNet, self).__init__()

        # 用循环来增加多注意力， 用nn.ModuleList变成一个大的并行的网络
        self.attention_module = nn.ModuleList(
            [GraphAttentionLayer(in_c, hid_c, embed_dim) for _ in range(n_heads)])  # in_c为输入特征维度，hid_c为隐藏层特征维度

        # 上面的多头注意力都得到了不一样的结果，使用注意力层给聚合起来
        self.out_att = GraphAttentionLayer(hid_c * n_heads, out_c, embed_dim)

        self.act = nn.LeakyReLU()

    def forward(self, inputs, graph):
        """
        :param inputs: [B, N, C]
        :param graph: [N, N]
        :return:
        """
        outputs = torch.cat([attn(inputs, graph) for attn in self.attention_module], dim=-1)  # [B, N, hid_c * h_head]
        outputs = self.act(outputs)

        outputs = self.out_att(outputs, graph)

        return self.act(outputs)

class NAL_GAT(nn.Module):
    def __init__(self, dim_in, dim_out, gat_hiden, gat_atten, embed_dim):
        super(NAL_GAT, self).__init__()
        self.subnet = GATSubNet(dim_in, gat_hiden, dim_out, gat_atten, embed_dim)

    def forward(self, x, node_embeddings):

        prediction = self.subnet(x, node_embeddings)  # [B, N, 1, C]，这个１加上就表示预测的是未来一个时刻

        return prediction


# -*- coding: utf-8 -*-
""" dioea.py
    Contains the class InverOver that apply the homonym algorithm to solve
    the dynamic TSP.
"""

__all__ = ['Dioea']
__version__ = '0.2'
__author__ = 'Emanuele Lovera'

import numpy as np

from inver_over.inver_over import InverOver

from utils.tsp_utils import dynamic_objfun

class Dioea():
    def __init__(self, filename, population_size=100, timer_limit=20000):
        self.weights = get_weights_matrix(filename, static=False)

        self.best_solution = None
        self.best_cost = float('Inf')

        self.POP_SIZE = population_size
        self.population = None
        self.init_population()
        print("BEST INITIAL COST = {}".format(self.best_cost))
        print("BEST INITIAL SOLUTION = {}".format(self.best_solution))

        self.inver_over = None
        self.d_list = None

    def run_dioea():
        """Main function for the dioea algorithm."""
        self.get_d_list()
        while timer < self.TIMER_LIMIT: # I'm not sure of the blocking condition.
            self.inver_over = InverOver(None, weights=self.weights, population=self.population, is_static=False)
            self.inver_over.inver_over_algorithm()
            self.population = self.inver_over.get_population()

            idx_d_list = 0 # index of d_list
            timer = 0
            while idx_d_list < len(self.d_list[0]):
                city = (self.d_list[0, idx_d_list], self.d_list[1, idx_d_list])
                self.change(city)
                idx_d_list = idx_d_list + 1
            self.t = self.t + 1

    def change(self, city):
        for element in self.population:
            #delete(element, city)
            index_c = element.index(city)

            #insert(element, city)

    def get_d_list(self):
        """Compute the d_list from the current population."""
        try:
            diff_matrix = abs(self.weights[self.t, :, :] - self.weights[self.t + 1, :, :])
            limit = self.matrix[self.t, :, :] * self.SENSIBILITY
            mask = diff_matrix < limit
            (x, y) = np.nonzero(mask) # indicies of all the cities that have moved over time.
            self.d_list = (x, y)
        except IndexError:
            print("IndexError. self.t={}".format(self.t))

    def init_population(self):
        """Init randomly the population. THe size of the population is expressed by POP_SIZE and the range
        with n."""
        self.population = []
        for i in range(self.POP_SIZE):
            self.population.append(random.sample(
                range(self.N_CITIES), self.N_CITIES))
            cost = dynamic_objfun(self.weights, self.population[-1])
            if cost < self.best_cost:
                self.best_cost = cost
                self.best_solution = self.population[-1]

def delete(element, city):
    pass

def insert(element, city):
    pass


from typing import TYPE_CHECKING, Iterable, Optional, Tuple
from os.path import join

from rastervision.pipeline.config import register_config
from rastervision.core.evaluation.classification_evaluator_config import (
    ClassificationEvaluatorConfig)
from rastervision.core.evaluation.semantic_segmentation_evaluator import (
    SemanticSegmentationEvaluator)

if TYPE_CHECKING:
    from rastervision.core.data import ClassConfig

def ss_evaluator_config_upgrader(cfg_dict: dict, version: int) -> dict:
    if version < 3:
        try:
            # removed in version 3
            del cfg_dict['vector_output_uri']
        except KeyError:
            pass
    return cfg_dict

@register_config(
    'semantic_segmentation_evaluator', upgrader=ss_evaluator_config_upgrader)
class SemanticSegmentationEvaluatorConfig(ClassificationEvaluatorConfig):
    def build(self,
              class_config: 'ClassConfig',
              scene_group: Optional[Tuple[str, Iterable[str]]] = None
              ) -> SemanticSegmentationEvaluator:
        if scene_group is None:
            output_uri = self.get_output_uri()
            vector_output_uri = self.get_vector_output_uri()
        else:
            group_name, _ = scene_group
            output_uri = self.get_output_uri(group_name)
            vector_output_uri = self.get_vector_output_uri(group_name)

        evaluator = SemanticSegmentationEvaluator(class_config, output_uri,
                                                  vector_output_uri)
        return evaluator

    def get_vector_output_uri(self,
                              scene_group_name: Optional[str] = None) -> str:
        if scene_group_name is None:
            return join(self.output_uri, 'vector-eval.json')
        return join(self.output_uri, scene_group_name, 'vector-eval.json')


from pyspark.sql import SparkSession

spark = SparkSession.builder.master("local[1]").appName("spark-practice").getOrCreate()

# Broadcast Variables, Immutable variables that can be shared with executors
# in order to avoid resending or deserializing the value with each task.
my_collection = "Spark The Definitive Guide : Big Data Processing Made Simple".split(
    " "
)
words = spark.sparkContext.parallelize(my_collection, 2)

# If we want to supplement our list of words with other information from maybe
# a dictionary as following then we would need to store it as a broadcast variable
# technically, This can be considered as a right-join
# The reason we don't store the value in the code is that it can be a large value like
# a big lookup table or large ML model. It can be inefficient if you use it in a closure
# because it must be deserialized on the worker nodes once per task.
# Also, If we use the variable in multiple spark actions and jobs it will be re-sent
# to the worker every time with every job.

supplementalData = {"Spark": 1000, "Definitive": 200, "Big": -300, "Simple": 100}

suppBroadcast = spark.sparkContext.broadcast(supplementalData)

# We can later access this value by referencing the broadcast variable
suppBroadcast.value

words.map(lambda word: (word, suppBroadcast.value.get(word, 0))).sortBy(
    lambda wordPair: wordPair[1]
).toDF().show()

# Accumulators
# They provide a mutable variable that a spark cluster can update on a per-row basis.
# can be used for debugging or to create low-level aggregations.
flights = spark.read.parquet(
    "/home/ahmed/Documents/Spark-The-Definitive-Guide-master/data/flight-data/parquet/2010-summary.parquet"
)
accChina = spark.sparkContext.accumulator(0)

def accChinaFunc(flight_row):
    destination = flight_row["DEST_COUNTRY_NAME"]
    origin = flight_row["ORIGIN_COUNTRY_NAME"]
    if destination == "China":
        accChina.add(flight_row["count"])
    if origin == "China":
        accChina.add(flight_row["count"])

flights.foreach(lambda flight_row: accChinaFunc(flight_row))
print(accChina.value)


import os

def get_input():
    inputfile = "./input/i" + os.path.basename(__file__)[0:2] + ".txt"
    data = []

    with open(inputfile, "r") as f:
        for line in f.readlines():
            if line == "\n":
                continue
            line = line.strip("\n")
            n = len(line)
            data.append([line[:n//2], line[n//2:]])
    return data

def score(c):
    if c.islower():
        return ord(c) - 96
    else:
        return ord(c) - 64 + 26

if __name__ == "__main__":
    common = []
    for r in get_input():
        common.extend(list(set(r[0]) & set(r[1])))
    print(sum(map(score, common)))


from Ant import Ant

class Controller:
    def __init__(self, problem):
        self.problem = problem
        # The number of nodes in the graph
        self.size = problem.noOfCubes * 24

    def initTrace(self, noCubes):
        # Compute the adjacency matrix of the graph
        noRotations = 24
        size = noRotations * noCubes
        matrix = [[0 for _ in range(size)] for _ in range(size)]
        for i in range(noRotations * noCubes):
            x = i // noRotations
            for j in range(noRotations):
                if x * noRotations - noRotations + j >= 0:
                    matrix[x * noRotations - noRotations + j][i] = 1
        return matrix

    def iteration(self, noAnts, trace, alpha, beta, q0, rho):
        antSet = [Ant(self.problem) for _ in range(noAnts)]
        # the number of iterations of an iteration it's the number of cubes that we have
        for i in range(self.problem.noOfCubes):
            for x in antSet:
                # add a move for each ant we have
                x.addMove(q0, trace, alpha, beta)
        # we update the trace with the fermons left behind by the ants
        dTrace = [1.0 / antSet[i].fitness() for i in range(len(antSet))]
        for i in range(self.size):
            for j in range(self.size):
                trace[i][j] = (1 - rho) * trace[i][j]
        for i in range(len(antSet)):
            for j in range(len(antSet[i].path) - 1):
                x = antSet[i].path[j]
                y = antSet[i].path[j + 1]
                trace[x][y] = trace[x][y] + dTrace[i]
        # return best ant path
        f = [[antSet[i].fitness(), i] for i in range(len(antSet))]
        f = max(f)
        return antSet[f[1]].path

    def run(self, noEpoch, noAnts, alpha, beta, rho, q0):
        bestSol = []
        trace = self.initTrace(self.problem.noOfCubes)
        for i in range(noEpoch):
            print(str((i / noEpoch) * 100) + "%")
            sol = self.iteration(noAnts, trace, alpha, beta, q0, rho).copy()
            if len(sol) > len(bestSol):
                bestSol = sol.copy()
        print("100% ~ DONE")
        print("The length of the best solution is: ", len(bestSol))


from enum import (Enum,
                  IntEnum,
                  IntFlag,
                  auto,
                  EnumMeta)

class AutoMeta(EnumMeta):
    last_value = 1
    def __call__(cls, *args, **kwargs):
        members = cls.__members__.keys()
        last_value = cls.last_value
        new_cls = super().__call__(value=super().__name__,
                         names=', '.join(members),
                         start=cls.last_value)
        cls.last_value = cls.last_value + len(members)
        print(new_cls.last_value)

        return new_cls

    @staticmethod
    def _get_mixins_(bases):
        """Returns the type for creating enum members, and the first inherited
        enum class.

        bases: the tuple of bases that was given to __new__

        """
        if not bases:
            return object, Enum

        # double check that we are not subclassing a class with existing
        # enumeration members; while we're at it, see if any other data
        # type has been mixed in so we can use the correct __new__
        member_type = first_enum = None

        # base is now the last base in bases
        if not issubclass(bases[-1], Enum):
            raise TypeError("new enumerations must be created as "
                    "`ClassName([mixin_type,] enum_type)`")

        # get correct mix-in type (either mix-in type of Enum subclass, or
        # first base if last base is Enum)
        if not issubclass(bases[0], Enum):
            member_type = bases[0]     # first data type
            first_enum = bases[-1]  # enum type
        else:
            for base in bases[0].__mro__:
                # most common: (IntEnum, int, Enum, object)
                # possible:    (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>,
                #               <class 'int'>, <Enum 'Enum'>,
                #               <class 'object'>)
                if issubclass(base, Enum):
                    if first_enum is None:
                        first_enum = base
                else:
                    if member_type is None:
                        member_type = base

        return member_type, first_enum

class Color(IntEnum, metaclass=AutoMeta):
    RED = auto()
    GREEN = auto()
    BLUE = auto()
    YELLOW = auto()

color_1 = Color()
color_2 = Color()
color_3 = Color()

print(color_1.RED.value)
print(color_2.RED.value)
print(color_3.RED.value)

for name, member in color_1.__members__.items():
    print(name, member.value)

for member in color_2:
    print(member.name, member.value)

member_1 = color_1.RED
member_2 = color_1.BLUE

print(member_1 != member_2)
print(member_1 > member_2)

class Permission(IntFlag):
    READ = 1
    WRITE = 2
    CLOSE = 3
    APPEND = 4
    EXECUTION = 6

permission_1 = Permission.READ
permission_2 = Permission.APPEND

permission_3 = Permission.READ | Permission.WRITE
permission_4 = Permission.READ + Permission.WRITE

# print(permission_1)
# print(permission_2)
# print(permission_3)
# print(permission_4)
# print(permission_1.value)
# print(Permission.R_W.value)
# print(permission_1 in permission_3)

# print(Permission.R_W.value & Permission.R_W.value == permission_1.value)
# print(Permission.__members__.items())
# print(Permission.R_W in Permission.EXECUTION)
# if permission_3 > Permission.R_W:
#     pass

class ResponseStatus(IntEnum):
    BAD_REQUEST = 400
    BAD_GATWAY = 500
    SUCCESS = 200

import requests
response = requests.get('https://www.google.com')
if response.status_code < ResponseStatus.BAD_REQUEST:
    print(response.content)
else:
    print(response.text)


def get_base_counts(dna):
    total = {'A': 0, 'C': 0, 'G': 0, 'T': 0}

    for letter in dna:
        if letter not in total.keys():
            return 'The input DNA string is invalid'
        else:
            total[letter] += 1

    return total


class Animal:
  # initializer/constructor
  def __init__(self, species, name):
    self.species = species
    self.name    = name

  # method
  def sayHello(self):
    print ('Hello! My name is ' + self.name +
           '! I am ' + self.species + '!')

# a1 is a zebra named 'Dora'
a1 = Animal('zebra', 'Dora')
a1.sayHello()

# print(a1.species)
# print(a1.name)

# a2 is an elephant called 'Jumbo'
a2 = Animal('elephant', 'Jumbo')
a2.sayHello()

# print(a2.species)
# print(a2.name)

2020 letni (Spring)/Python Programming (Clark)/2020-03-21/zoo.py


import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import time
import h5py
import sys

IM_SHAPE = (64, 64, 3)

class TrainingDatasetLoader(object):
    def __init__(self, data_path):

        print("Opening: ", format(data_path))
        sys.stdout.flush()

        self.cache = h5py.File(data_path, 'r')

        print("Loading data into memory...")
        sys.stdout.flush()
        self.images = self.cache['images'][:]
        self.labels = self.cache['labels'][:]
        self.image_dims = self.images.shape
        n_train_samples = self.image_dims[0]

        self.train_inds = np.random.permutation(np.arange(n_train_samples))

        self.pos_train_inds = self.train_inds[ self.labels[self.train_inds, 0] == 1.0 ]
        self.neg_train_inds = self.train_inds[ self.labels[self.train_inds, 0] != 1.0 ]

    def get_train_size(self):
        return self.train_inds.shape[0]

    def get_train_steps_per_epoch(self, batch_size, factor=10):
        return self.get_train_size()//factor//batch_size

    def get_batch(self, n, only_faces=False, p_pos=None, p_neg=None, return_inds=False):
        if only_faces:
            selected_inds = np.random.choice(self.pos_train_inds, size=n, replace=False, p=p_pos)
        else:
            selected_pos_inds = np.random.choice(self.pos_train_inds, size=n//2, replace=False, p=p_pos)
            selected_neg_inds = np.random.choice(self.neg_train_inds, size=n//2, replace=False, p=p_neg)
            selected_inds = np.concatenate((selected_pos_inds, selected_neg_inds))

        sorted_inds = np.sort(selected_inds)
        train_img = self.images[sorted_inds,:,:,::-1]/255.
        train_label = self.labels[sorted_inds,...]
        return (train_img, train_label, sorted_inds) if return_inds else (train_img, train_label)

    def get_n_most_prob_faces(self, prob, n):
        idx = np.argsort(prob)[::-1]
        most_prob_inds = self.pos_train_inds[idx[:10*n:10]]
        return self.images[most_prob_inds,...]/255.

    def get_all_train_faces(self):
        return self.images[ self.pos_train_inds ]

class PPBFaceEvaluator:
    ''' Evaluate on the PPB dataset'''
    def __init__(self, skip=4):

        #path_to_faces = tf.keras.utils.get_file('ppb', 'https://www.dropbox.com/s/l0lp6qxeplumouf/PPB.tar?dl=1', extract=True)
        import csv
        #self.ppb_root = os.path.join(os.path.split(path_to_faces)[0], 'PPB-2017')
        self.anno_dict = {}
        self.ppb_root = './PPB-2017/'
        #ppb_anno = './PPB-2017/PPB-2017-metadata.csv'
        image_dir = './PPB-2017/imgs/'

        with open('./PPB-2017/PPB-2017-metadata.csv', mode='r') as csvFile:
            reader = csv.reader(csvFile)
            for row in reader:
                #print(row)
                name = row[1]
                gender = row[2]
                skin = row[4]
                self.anno_dict[name] = (gender.lower(),skin.lower())

        image_files = sorted(os.listdir(image_dir))[::skip] #sample every 4 images for computation time in the lab

        self.raw_images = {
            'male_darker':[],
            'male_lighter':[],
            'female_darker':[],
            'female_lighter':[],
        }

        for filename in image_files:
            if not filename.endswith(".jpg"):
                continue
            image = cv2.imread(os.path.join(image_dir,filename))[:,:,::-1]
            gender, skin = self.anno_dict[filename]
            self.raw_images[gender+'_'+skin].append(image)

    def get_sample_faces_from_demographic(self, gender, skin_color):
        key = self.__get_key(gender, skin_color)
        data = self.raw_images[key][50]/255.
        return data

    def evaluate(self, models_to_test, gender, skin_color, output_idx=None, from_logit=False, patch_stride=0.2, patch_depth=5):
        correct_predictions = [0.0]*len(models_to_test)

        key = self.__get_key(gender, skin_color)
        num_faces = len(self.raw_images[key])

        import progressbar
        bar = progressbar.ProgressBar()
        for face_idx in bar(range(num_faces)):

            image = self.raw_images[key][face_idx]
            height, width, _ = image.shape

            patches, bboxes = slide_square(image, patch_stride, width/2, width, patch_depth)
            patches = tf.cast(tf.constant(patches, dtype=tf.uint8), tf.float32)/255.

            for model_idx, model in enumerate(models_to_test):
                out = model(patches)
                y = out if output_idx is None else out[output_idx]
                y = y.numpy()
                y_inds = np.argsort(y.flatten())
                most_likely_prob = y[y_inds[-1]]
                if (from_logit and most_likely_prob >= 0.0) or \
                   (not from_logit and most_likely_prob >= 0.5):
                        correct_predictions[model_idx] += 1

        accuracy = [correct_predictions[i]/num_faces for i,_ in enumerate(models_to_test)]
        return accuracy

    def __get_key(self, gender, skin_color):
        gender = gender.lower()
        skin_color = skin_color.lower()
        assert gender in ['male', 'female']
        assert skin_color in ['lighter', 'darker']
        return '{}_{}'.format(gender, skin_color)

''' function to slide a square across image and extract square regions
img = the image
stride = (0,1], provides the fraction of the dimension for which to slide to generate a crop
max_size = maximum square size
min_size = minimum square size
n = number of different sizes including min_size, max_size '''
def slide_square(img, stride, min_size, max_size, n):
    img_h, img_w = img.shape[:2]

    square_sizes = np.linspace(min_size, max_size, n, dtype=np.int32)
    square_images = []
    square_bbox = [] # list of list of tuples: [(i1,j1), (i2,j2)] where i1,j1 is the top left corner; i2,j2 is bottom right corner
    # for each of the square_sizes
    for level, sq_dim in enumerate(square_sizes):

        stride_length = int(stride*sq_dim)
        stride_start_i = xrange(0,int(img_h-sq_dim+1),stride_length)
        stride_start_j = xrange(0,int(img_w-sq_dim+1),stride_length)
        for i in stride_start_i:
            for j in stride_start_j:
                square_top_left = (i,j)

                square_bottom_right = (i+sq_dim,j+sq_dim)

                square_corners = (square_top_left,square_bottom_right)
                square_corners_show = ((j,i),(j+sq_dim,i+sq_dim))
                square_image = img[i:i+sq_dim,j:j+sq_dim]

                square_resize = cv2.resize(square_image, IM_SHAPE[:2], interpolation=cv2.INTER_NEAREST)
                # append to list of images and bounding boxes

                square_images.append(square_resize)
                square_bbox.append(square_corners)

    return square_images, square_bbox


import datetime
import logging
import logging.config
import os
from logging import handlers

import colorlog

from f_tools.f_general import get_path_root

'''
args=(os.path.abspath(os.getcwd() + "/info.log"),"midnight", 1, 6,'utf-8')
每一天午夜12点将当天的日志转存到一份新的日志文件中，并且加上时间戳后缀，最多保存6个文件
'''

def Singleton(cls):
    # 单例
    _instance = {}

    def _singleton(*args, **kargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kargs)
        return _instance[cls]

    return _singleton

def get_logger(name='root'):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)

    # fmt = '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
    # formatter = logging.Formatter(fmt)
    # black, red, green, yellow, blue, purple, cyan and white {color}，fg_ {color}，bg_ {color}：前景色和背景色
    log_colors_config = {
        'DEBUG': 'blue',  # 蓝色
        # 'INFO': 'green',  # 绿色
        'INFO': 'cyan',  # 蓝绿
        'WARNING': 'yellow',
        'ERROR': 'red',
        'CRITICAL': 'red,bg_white',
    }
    formatter = colorlog.ColoredFormatter(
        '%(log_color)s[%(asctime)s] [%(filename)s:%(lineno)d] [%(module)s:%(funcName)s] [%(levelname)s]- %(message)s',
        log_colors=log_colors_config)  # 日志输出格式

    sh = logging.StreamHandler()
    sh.setFormatter(formatter)
    # sh.setLevel(logging.DEBUG)
    logger.addHandler(sh)

    '''
    实例化TimedRotatingFileHandler
        #interval是时间间隔，backupCount是备份文件的个数，如果超过这个个数，就会自动删除，when是间隔的时间单位，单位有以下几种：
        # S 秒
        # M 分
        # H 小时、
        # D 天、
        # W 每星期（interval==0时代表星期一）
        # midnight 每天凌晨
    '''
    path_root = os.path.join(get_path_root(), 'logs')
    if not os.path.exists(path_root):
        os.makedirs(path_root)
    th = handlers.TimedRotatingFileHandler(
        filename=os.path.join(path_root, datetime.datetime.now().strftime('%Y_%m_%d') + '.log'),
        when='D',
        backupCount=5, encoding='utf-8')
    th.setFormatter(formatter)
    logger.addHandler(th)

    # fh = RotatingFileHandler(
    #     filename=os.path.join(get_path_root(), 'logs', datetime.datetime.now().strftime('%Y_%m_%d') + '.log'),
    #     mode='a', maxBytes=1024 * 1024 * 5, backupCount=5,
    #     encoding='utf-8')  # 使用RotatingFileHandler类，滚动备份日志
    # fh.setLevel(logging.CRITICAL)
    # fh.setFormatter(formatter)
    # print(logger.handlers)
    # logger.removeHandler(ch)
    return logger

flog = get_logger(__name__)  # 返回一个叫__name__ 的obj，并应用默认的日志级别、Handler和Formatter设置

# flog=Log()
# flog1=Log()
# print(id(flog))
# print(id(flog1))

if __name__ == '__main__':
    # flog.debug('一个连接只需一个 %s', get_path_root)
    flog.debug('多个连接无需   %s%s', [1, 2, {123}], get_path_root())
    flog.info(123)
    flog.warning('多个连接无需   %s%s', [1, 2, {123}], get_path_root())
    flog.error(123)
    flog.critical(123)
    pass


import speech_recognition
import os
import pyaudio
import playsound
from gtts import gTTS
import random
import pyowm
import sys
from random import randint
import pygame
import webbrowser
pygame.init()
white = (255,255,255)
red= (255,0,0)
height= 850
width = 500
temp = 850
bg= pygame.transform.scale(pygame.image.load("bg.jpg"),(temp,temp))
font= pygame.font.Font("freesansbold.ttf", 50)
show= pygame.display.set_mode((height, width))
pygame.display.set_caption("hello")
show.fill(white)
for event in pygame.event.get():
		if event.type== pygame.QUIT:
			pygame.quit()
			sys.exit(0)
while True:
	hear = speech_recognition.Recognizer()
	with speech_recognition.Microphone() as mic:
		hear.adjust_for_ambient_noise(mic)
		ai="em xin nghe"
		print(ai)
		output= gTTS(ai,lang="vi", slow=False)
		output.save("out.mp3")
		playsound.playsound("out.mp3")
		os.remove("out.mp3")
		audio= hear.record(mic,duration=3)
		for event in pygame.event.get():
			if event.type== pygame.QUIT:
				pygame.quit()
				sys.exit(0)
	try:
		you= hear.recognize_google(audio, language="vi-VN")
		print(you)
		for event in pygame.event.get():
			if event.type== pygame.QUIT:
				pygame.quit()
				sys.exit(0)
	except:
		you="??????"
	out= "Tôi: "+ you
	text= font.render(out, True, white)
	textRect = text.get_rect()
	textRect.center= ( height//2, width//2)
	show.blit(bg, pygame.Rect(0,0,temp, temp))
	show.blit(text, textRect)
	pygame.display.update()
	if "mở facebook" in you or "Mở facebook" in you  or "Mở Facebook" in you:
		while True:
			a= randint(1,10)
			b=randint(1,10)
			c= a+b
			d= str(c)
			hear1 = speech_recognition.Recognizer()
			with speech_recognition.Microphone() as mic:
				hear1.adjust_for_ambient_noise(mic)
				ai="xin xác nhận mã capcha " + str(a) +"+ " + str(b) +" bằng bao nhiêu"
				output= gTTS(ai,lang="vi", slow=False)
				output.save("out.mp3")
				playsound.playsound("out.mp3")
				os.remove("out.mp3")
				audio1= hear1.record(mic,duration=3)
			try:
				you1= hear.recognize_google(audio1, language="vi-VN")
				print(you1)
			except:
				you1="?????"

			if(you1==d):
				ai="mã capcha đúng, đang mở facebook"
				output= gTTS(ai,lang="vi", slow=False)
				output.save("out.mp3")
				playsound.playsound("out.mp3")
				os.remove("out.mp3")
				webbrowser.open("https://fb.com", new=1)
				break
			else:
				ai="mã capcha sai vui lòng học lại lớp 1"
				output= gTTS(ai,lang="vi", slow=False)
				output.save("out.mp3")
				playsound.playsound("out.mp3")
				os.remove("out.mp3")
	elif "hello" in you:
		ai="xin chào"
		output= gTTS(ai,lang="vi", slow=False)
		output.save("out.mp3")
		playsound.playsound("out.mp3")
		os.remove("out.mp3")
	elif "cút" in you or "thôi" in you or "tạm biệt" in you or "tắt" in you:
		ai="bye bye"
		output= gTTS(ai,lang="vi", slow=False)
		output.save("out.mp3")
		playsound.playsound("out.mp3")
		os.remove("out.mp3")
		break
	elif "tìm bạn" in you or "Tìm bạn" in you:
		string = you
		string = string[7:None]
		ai="đang tìm bạn"+ string
		output= gTTS(ai,lang="vi", slow=False)
		output.save("out.mp3")
		playsound.playsound("out.mp3")
		os.remove("out.mp3")
		webbrowser.open("https://www.facebook.com/search/top/?q="+string, new=1)
	elif "là ai" in you or "Là ai" in you:
		ai="đang tìm kiếm"+ you
		output= gTTS(ai,lang="vi", slow=False)
		output.save("out.mp3")
		playsound.playsound("out.mp3")
		os.remove("out.mp3")
		webbrowser.open("https://www.google.com/search?query="+you, new=1)
	elif "văn bản" in you or "Văn bản" in you:
		ai="đang mở trình soạn thảo văn bản"
		output= gTTS(ai,lang="vi", slow=False)
		output.save("out.mp3")
		playsound.playsound("out.mp3")
		os.remove("out.mp3")
		os.system("start D:\\Word.lnk")

	elif "youtube" in you or "Youtube" in you or "YouTube" in you:
		string = you
		string = string[7:None]
		ai="đang tìm video"+ string
		output= gTTS(ai,lang="vi", slow=False)
		output.save("out.mp3")
		playsound.playsound("out.mp3")
		os.remove("out.mp3")
		webbrowser.open("https://www.youtube.com/results?search_query="+string, new=1)
	elif "thời tiết" in you or "Thời tiết" in you:
		owm = pyowm.OWM("f582deb1c5ae0bf090fe4a6bf9f9d053")
		observation= owm.weather_at_place("hanoi,VN")
		weather= observation.get_weather()
		temper = weather.get_temperature('celsius')["temp"]
		to= int (temper)
		nhietdo= str(to)
		ai="nhiệt độ hiện tại là: "+nhietdo+ " độ C"
		output= gTTS(ai,lang="vi", slow=False)
		output.save("out.mp3")
		playsound.playsound("out.mp3")
		os.remove("out.mp3")
	else:
		ai="không hiểu"
		output= gTTS(ai,lang="vi", slow=False)
		output.save("out.mp3")
		playsound.playsound("out.mp3")
		os.remove("out.mp3")
	for event in pygame.event.get():
		if event.type== pygame.QUIT:
			pygame.quit()
			sys.exit(0)


import math
import random
from collections import defaultdict

# arr = ["sentence","like","THAT","IN","This"]
# arr2 = ["11","22","33","44","55"]

# for i in range(len(arr)):
#     index = len(arr) - i
#     for j in range(index):
#         word = arr[j:i+j+1]
#         t = ' '.join([tag for tag in arr[j:i+j+1] if len(tag) > 0])
#         print(t)
#         if i > 0:
#             print(i,",",j,":")
#             l = 0
#             for m in range(i):
#                 print(m,",",j,"+",i-(m+1),",",j+m+1)

# cfg_rules = {   'S': ['NP VP'],
#                 'VP': ['Verb NP'],
#                 'NP': ['Det Noun', 'Pronoun', 'NP PP'],
#                 'PP': ['Prep NP'],
#                 'Noun': ['Adj Noun'] }

# key_list = list(cfg_rules.keys())
# for i in range(5):
#     if not key_list[i].isupper():
#         print(key_list[i])
# next_rule = ['NP VP']
# print([[word for word in rule if not word.isupper()] for rule in next_rule])
# print([sum([1 for word in rule if not word.isupper()]) for rule in next_rule])
# next_rule = ['NP VP', 'NP PP']
# print([[word for word in rule if not word.isupper()] for rule in next_rule])
# print([sum([word.isupper() for word in rule]) for rule in next_rule])
# next_rule = ['NP VP', 'NP PP', 'Verb NP']
# print([[word for word in rule if not word.isupper()] for rule in next_rule])
# print([sum([word.isupper() for word in rule]) for rule in next_rule])
# next_rule = ['NP VP', 'NP PP', 'Verb NP', 'Det Noun']
# print([[word for word in rule if not word.isupper()] for rule in next_rule])
# print([sum([word.isupper() for word in rule]) for rule in next_rule])

cfg_rules = {      'S': ['NP VP'],
                    'VP': ['Verb NP'],
                    'NP': ['Det Noun | Pronoun | NP PP'],
                    'PP': ['Prep NP'],
                    'Noun': ['Adj Noun'],
                    'Verb': ['ate | wanted | kissed | washed | pickled | is | prefer | like | need | want',],
                    'Det': ['the | a | every | this | that'],
                    'Noun': ['president | sandwich | pickle | mouse | floor'],
                    'Adj': ['fine | delicious | beautiful | old'],
                    'Prep': ['with | on | under | in | to | from'],
                    'Pronoun': ['me | I | you | it'] }

class CFG(object):
    def __init__(self):
        self.prod = defaultdict(list)

    def add_prod(self, lhs, rhs):
        """ Add production to the grammar. 'rhs' can
            be several productions separated by '|'.
            Each production is a sequence of symbols
            separated by whitespace.

            Usage:
                grammar.add_prod('NT', 'VP PP')
                grammar.add_prod('Digit', '1|2|3|4')
        """
        prods = rhs[0].split("|")
        for prod in prods:
            self.prod[lhs].append(tuple(prod.split()))
        print(self.prod)
    def gen_random(self, symbol):
        """ Generate a random sentence from the
            grammar, starting with the given
            symbol.
        """
        sentence = ''

        # select one production of this symbol randomly
        rand_prod = random.choice(self.prod[symbol])

        for sym in rand_prod:
            # for non-terminals, recurse
            if sym in self.prod:
                sentence += self.gen_random(sym)
            else:
                sentence += sym + ' '

        return sentence

cfg1 = CFG()
for k,v in cfg_rules.items():
    cfg1.add_prod(k,v)

# cfg1.add_prod(cfg_rules)
# # cfg1.add_prod('S', 'NP VP')
# # cfg1.add_prod('NP', 'Det N | Det N')
# # cfg1.add_prod('NP', 'I | he | she | Joe')
# # cfg1.add_prod('VP', 'V NP | VP')
# # cfg1.add_prod('Det', 'a | the | my | his')
# # cfg1.add_prod('N', 'elephant | cat | jeans | suit')
# # cfg1.add_prod('V', 'kicked | followed | shot')

for i in range(10):
    print(cfg1.gen_random('S'))


class myclass:
    """Напишите программу, в которой для объектов класса предусмотрен специальный режим доступа к полям. В частности, у объекта должно
быть поле-список, значением которому можно присваивать только список. Из присваиваемого списка в поле-список копируются только текстовые значения.
При считывании значения этого поля возвращается текстовая строка, содержащая только начальные буквы текстовых значений, которые входят в список."""

    def __setattr__(self,name,val):
        if type(val)==list:
            self.__dict__[name]=[]
            for i in val:
                if type(i)==str:
                    self.__dict__[name].append(i)
        else:
            print("Приваиваемое поле не список!")
    def __getattribute__(self,name):
        if type(object.__getattribute__(self,name))==list:
            print("Запрашивается поле",name)
            res=""
            for i in object.__getattribute__(self,name):
                res+=i[0]
            return res
        else:
            return object.__getattribute__(self,name)

A=myclass()
A.list1=["Я",65,2,"буду",78,"программистом"]
print("Словарь для проверки:",A.__dict__)
print("Результат:",A.list1)


import os
import torch
import pandas as pd
import numpy as np
from getData import getData
from preprocessing.pca_process import transformation_data

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def main(methods, transform_method):
    all_losses = []
    all_stages = []
    all_methods = []
    for method in methods:
        data = getData()

        print('Procesando metodo: ', method)
        if method == '':
            method = 'seq_to_seq'
            name = 'DB: PIP y features ' + method
            g = data.seq_to_seq_edge_regression()
            if transform_method == 'pca':
                transform_instance = transformation_data()
                data_transform_pca, pca_instance = transform_instance.apply_pca_linear(g.ndata['feat'])
                data_transform_pca = np.round(data_transform_pca, 3)
                g.ndata['feat'] = torch.Tensor(data_transform_pca)
            elif transform_method == 'kernel_pca':
                transform_instance = transformation_data()
                data_transform_pca, pca_instance = transform_instance.apply_kernel_pca(g.ndata['feat'])
                data_transform_pca = np.round(data_transform_pca, 3)
                g.ndata['feat'] = torch.Tensor(data_transform_pca)
        else:
            name = 'DB: PIP y features ' + method
            g = data.encoding_edge_regression(method)
            if transform_method == 'pca':
                transform_instance = transformation_data()
                data_transform_pca, pca_instance = transform_instance.apply_pca_linear(g.ndata['feat'])
                data_transform_pca = np.round(data_transform_pca, 3)
                g.ndata['feat'] = torch.Tensor(data_transform_pca)
            elif transform_method == 'kernel_pca':
                transform_instance = transformation_data()
                data_transform_pca, pca_instance = transform_instance.apply_kernel_pca(g.ndata['feat'])
                data_transform_pca = np.round(data_transform_pca, 3)
                g.ndata['feat'] = torch.Tensor(data_transform_pca)

        ruta = 'pip_data_modelos/' + transform_method + '/' + method
        if not os.path.exists(ruta):
            os.mkdir(ruta)

        nodes_features = pd.DataFrame(np.round(g.ndata['feat'].numpy(), 3))
        nodes_features.to_csv('pip_data_modelos/' + transform_method + '/' + method + '/nodes_features_.csv', index=False)

        if not os.path.isfile('pip_data_modelos/edge_index.csv'):
            print('Se crea edge_index')
            edge_index = pd.DataFrame()
            src, dst = g.edges()
            train_edges = torch.cat([src.unsqueeze(0), dst.unsqueeze(0)], dim=0)
            edge_index['edge_1'] = train_edges.tolist()[0]
            edge_index['edge_2'] = train_edges.tolist()[1]
            edge_index.to_csv('pip_data_modelos/edge_index.csv', index=False)

        if not os.path.isfile('pip_data_modelos/weights.csv'):
            print('Se crea edge_weights')
            edge_weights = pd.DataFrame()
            edge_weights['weights'] = g.edata['weight'].tolist()
            edge_weights.to_csv('pip_data_modelos/weights.csv', index=False)

        if not os.path.isfile('pip_data_modelos/labels.csv'):
            print('Se crea edge_labels')
            edge_labels = pd.DataFrame()
            edge_labels['labels'] = g.edata['label'].tolist()
            edge_labels.to_csv('pip_data_modelos/labels.csv', index=False)

if __name__ == "__main__":
    ruta = 'Protein-Protein/encoded_sequences'
    methods = ['']

    for elemento in os.listdir(ruta):
        if os.path.isdir(os.path.join(ruta, elemento)):
            methods.append(elemento)

    transform_method = ['no_transform', 'pca', 'kernel_pca']
    for i in transform_method:
        main(methods, i)


import plotly.graph_objects as go
from plotly.subplots import make_subplots

labels = ["Specific", "Left Specific", "Right Specific", "Unspecific"]
titles = ["HeLa PSM", "Serum PSM", "HeLa Peptide", "Serum Peptide"]

fig = make_subplots(rows=2, cols=2,
                    subplot_titles=titles,
                    specs=[[{"type": "domain"}, {"type": "domain"}],
                           [{"type": "domain"}, {"type": "domain"}]])
hole_size = .3
# HeLa PSM
fig.add_trace(go.Pie(
    labels=labels,
    values=[19322, 1475, 3593, 1223],
    hole=hole_size
), row=1, col=1)

# HeLa Peptide
fig.add_trace(go.Pie(
    labels=labels,
    values=[14811, 1306, 3057, 1166],
    hole=hole_size
), row=1, col=2)

# Serum PSM
fig.add_trace(go.Pie(
    labels=labels,
    values=[6644, 849, 1887, 228],
    hole=hole_size
), row=2, col=1)

# Serum Peptide
fig.add_trace(go.Pie(
    labels=labels,
    values=[2343, 537, 1207, 186],
    hole=hole_size
), row=2, col=2)

fig.show()
fig.write_image(r"D:\data\semi\lly\donut.svg")