import boto3

rds_data_client = boto3.client('rds-data')
sts_connection = boto3.client('sts')

# assumes mgmt role to set up cognito client associated with mgmt account
def create_cognito_client(mgmt_account_role_arn):
    mgmt_account = sts_connection.assume_role(
        RoleArn=mgmt_account_role_arn,
        RoleSessionName="mgmt_cognito_rds_sync_lambda"
    )
    # create cognito client using the assumed role credentials in mgmt acc
    global cognito_client
    cognito_client = boto3.client(
        'cognito-idp',
        aws_access_key_id=mgmt_account['Credentials']['AccessKeyId'],
        aws_secret_access_key=mgmt_account['Credentials']['SecretAccessKey'],
        aws_session_token=mgmt_account['Credentials']['SessionToken']
    )

# returns list of all users and their 'sub' attribute from userpool - includes enabled status
def get_users_in_userpool(user_pool_id):

    response = cognito_client.list_users(
        UserPoolId=user_pool_id,
    )
    users = response.get('Users')

    while response.get('PaginationToken') is not None:
        response = cognito_client.list_users(
            UserPoolId      = user_pool_id,
            PaginationToken = response.get('PaginationToken')
        )
        users.extend(response.get('Users'))

    return users

# connects to RDS instance and executes the SQL statement passed in
def execute_statement(sql, db_credentials_secrets_store_arn, database_name, db_cluster_arn):
    response = rds_data_client.execute_statement(
        secretArn=db_credentials_secrets_store_arn,
        database=database_name,
        resourceArn=db_cluster_arn,
        sql=sql
    )
    return response


from telegram.ext import Updater, CommandHandler
from selenium import webdriver
from urllib.parse import quote
import os

updater = Updater(token=os.getenv('TELEGRAM_TOKEN'), use_context=True)
dispatcher = updater.dispatcher
driver = webdriver.Chrome(executable_path='/Users/tung491/Downloads/chromedriver')

def start(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")

def uds(update, context):
    word = ' '.join(context.args)
    url = f'https://www.urbandictionary.com/define.php?term={quote(word)}'
    driver.get(url)
    def_panels = driver.find_elements_by_xpath('//div[@class="def-panel "]')
    if def_panels:
        def_panels.remove(def_panels[1])
        for def_panel in def_panels:
            for cls in ['row', 'mug-ad', 'def-footer', 'contributor']:
                rm_element = def_panel.find_element_by_class_name(cls)
                driver.execute_script("""
                                        var element = arguments[0];
                                        element.parentNode.removeChild(element);
                                        """, rm_element)
                with open('definition.png', 'wb') as f:
                    f.write(def_panel.screenshot_as_png)
            context.bot.send_photo(update.effective_chat.id, photo=open('definition.png', 'rb'))
    else:
        context.bot.send_message(chat_id=update.effective_chat.id, text='There is no definition for this word')

if __name__ == '__main__':
    start_handler = CommandHandler('start', start)
    dispatcher.add_handler(start_handler)

    uds_handler = CommandHandler('uds', uds)
    dispatcher.add_handler(uds_handler)
    updater.start_polling()


import Blockchain
import hashlib
if __name__ == '__main__':
    b = Blockchain.aoo_Blockchain()
    b.aoo_new_transaction(sender="Leksus", recipient="Zheka", amount=15)
    b.aoo_new_transaction(sender="Lena", recipient="Milena", amount=3422)
    b.aoo_new_block(proof=b.aoo_proof_of_work(last_proof=b.aoo_last_block['proof']))
    print(b.aoo_chain)
    proof_1 = b.aoo_chain[0]['proof']
    proof_2 = b.aoo_chain[1]['proof']
    print(hashlib.sha256(f'{proof_1}{proof_2}'.encode()).hexdigest())


import pandas as pd
import random

from playlist import Playlist

class BasicPlaylist(Playlist):

    # Create dataframes with users tracks history

    def create_user_history(self, user_id: int) -> pd.DataFrame:
        """
        method creates new dataframe with tracks listened by specific user
        :user_id: id of the user
        :return: dataframe with history of tracks listened by user
        """
        user_session_df = self.sessions_df.loc[
            self.sessions_df['user_id'] == user_id]

        user_history = dict()

        for idx, row in user_session_df.iterrows():
            track = row['track_id']
            if track not in user_history:
                user_history[track] = 0
            if row['event_type'] == 'play':
                user_history[track] += 1

        user_history = pd.DataFrame.from_dict(
            user_history, orient='index')

        return user_history

    def generate_users_histories(self) -> list:
        """
        method generates list of dataframes which contains
        all tracks listened by specified users
        :return: list of track history of every user
        """
        users_histories = []
        for user_id in self.users_id:
            users_histories.append(self.create_user_history(user_id))
        return users_histories

    # Create playlist

    def create_playlist_user_history(self, songs_per_user: int) -> list:
        """
        method creates playlist based on users favourite songs
        and excluding disliked tracks
        :return: list of songs ids
        """
        users_histories = self.generate_users_histories()
        basic_playlist = []
        for user_history in users_histories:
            basic_playlist.extend(list(
                user_history
                .sample(n=songs_per_user, replace=True)
                .index))

        return basic_playlist

    def create_full_basic_playlist(self) -> pd.DataFrame:
        basic_playlist_df = self.create_first_part_playlist()
        basic_playlist = basic_playlist_df['id'].tolist()
        duration = self.count_duration(basic_playlist)

        remaining_time = self.playlist_duration - duration
        remaining_songs_estimate = remaining_time / self.median_song_duration

        basic_playlist.extend(list(
            self.tracks_df
            .sample(n=int(remaining_songs_estimate), replace=True)
            ['id']))

        duration = self.count_duration(basic_playlist)

        while duration > self.playlist_duration + 300 or duration < self.playlist_duration - 300:
            if duration > self.playlist_duration + 300:
                basic_playlist.pop(
                    random.randrange(len(basic_playlist)))
            else:
                basic_playlist.extend(list(
                    self.tracks_df
                    .sample(n=1, replace=True)
                    ['id']))
            duration = self.count_duration(basic_playlist)

        return self.tracks_df.loc[self.tracks_df['id'].isin(basic_playlist)]


# WAP to take a string of length k and a var n where n is a factor of k and print
# n lines taking a contiguous block of n values from k with distinct characters.

def splitStr(ss, n):
    i = 0
    while (i < len(ss)):
        s = ""
        for j in ss[i:i + n]:
            if j not in s:
                s += j
        print(s)
        i += n

ss = "ABCCAAADA"
n = 3
splitStr(ss, n)


from django.contrib.auth import get_user_model
from django.test import TestCase

from ..models import Group, Post

User = get_user_model()

class PostModelTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.user = User.objects.create_user(username='auth')
        cls.group = Group.objects.create(
            title='Тестовая группа',
            slug='Тестовый слаг',
            description='Тестовое описание',
        )
        cls.post = Post.objects.create(
            author=cls.user,
            text='Тестовый пост для тестовых тестов',
        )

    def test_model_Group_has_correct_object_names(self):
        """Проверяем, что у моделей корректно работает __str__."""
        self.assertEqual(str(self.group), self.group.title)

    def test_model_Post_has_correct_object_names(self):
        """Проверяем, что у моделей корректно работает __str__."""
        self.assertEqual(str(self.post), self.post.text[:15])

    def test_model_Post_has_correct_verbose(self):
        """Проверка содержания verbose_name"""
        field_verboses = {
            'author': 'Автор',
            'pub_date': 'Время публикации',
            'text': 'Текст поста',
            'image': 'Картинка',
            'group': 'Группа'
        }
        for value, expected in field_verboses.items():
            with self.subTest(value=value):
                self.assertEqual(
                    self.post._meta.get_field(value).verbose_name, expected)

    def test_model_Post_has_correct_help_text(self):
        """Проверка содержания help_text"""
        field_helps = {
            'text': 'Введите текст поста',
            'image': 'Прикрепите картинку',
            'group': 'Выберите группу'
        }
        for value, expected in field_helps.items():
            with self.subTest(value=value):
                self.assertEqual(
                    self.post._meta.get_field(value).help_text, expected)


from decimal import Decimal
from unittest import TestCase

from hummingbot.connector.in_flight_order_base import InFlightOrderBase
from hummingbot.core.data_type.in_flight_order import OrderState
from hummingbot.core.event.events import LimitOrderStatus, OrderType, TradeType

class InFlightOrderBaseTests(TestCase):

    def test_string_repr(self):
        order = InFlightOrderBase(
            client_order_id="OID1",
            exchange_order_id="EOID1",
            trading_pair="COINALPHA-HBOT",
            order_type=OrderType.LIMIT,
            trade_type=TradeType.BUY,
            price=Decimal(1000),
            amount=Decimal(1),
            initial_state=OrderState.PENDING_CREATE.name,
            creation_timestamp=1640001112.0
        )

        expected_repr = ("InFlightOrder(client_order_id='OID1', exchange_order_id='EOID1', "
                         "creation_timestamp=1640001112, trading_pair='COINALPHA-HBOT', order_type=OrderType.LIMIT, "
                         "trade_type=TradeType.BUY, price=1000, amount=1, executed_amount_base=0, "
                         "executed_amount_quote=0, fee_asset='None', fee_paid=0, last_state='PENDING_CREATE')")

        self.assertEqual(expected_repr, repr(order))

    def test_get_creation_timestamp(self):
        order = InFlightOrderBase(
            client_order_id="OID1",
            exchange_order_id="EOID1",
            trading_pair="COINALPHA-HBOT",
            order_type=OrderType.LIMIT,
            trade_type=TradeType.BUY,
            price=Decimal(1000),
            amount=Decimal(1),
            initial_state=OrderState.PENDING_CREATE.name,
            creation_timestamp=1640001112.0
        )

        self.assertEqual(1640001112, order.creation_timestamp)

    def test_creation_timestamp_taken_from_order_id_when_not_specified(self):
        order = InFlightOrderBase(
            client_order_id="OID1-1640001112223334",
            exchange_order_id="EOID1",
            trading_pair="COINALPHA-HBOT",
            order_type=OrderType.LIMIT,
            trade_type=TradeType.BUY,
            price=Decimal(1000),
            amount=Decimal(1),
            creation_timestamp=-1,
            initial_state=OrderState.PENDING_CREATE.name
        )

        self.assertEqual(1640001112.223334, order.creation_timestamp)

    def test_serialize_order_to_json(self):
        order = InFlightOrderBase(
            client_order_id="OID1",
            exchange_order_id="EOID1",
            trading_pair="COINALPHA-HBOT",
            order_type=OrderType.LIMIT,
            trade_type=TradeType.BUY,
            price=Decimal(1000),
            amount=Decimal(1),
            initial_state=OrderState.PENDING_CREATE.name,
            creation_timestamp=1640001112.0
        )

        expected_json = {
            "client_order_id": order.client_order_id,
            "exchange_order_id": order.exchange_order_id,
            "trading_pair": order.trading_pair,
            "order_type": order.order_type.name,
            "trade_type": order.trade_type.name,
            "price": str(order.price),
            "amount": str(order.amount),
            "executed_amount_base": str(order.executed_amount_base),
            "executed_amount_quote": str(order.executed_amount_quote),
            "fee_asset": order.fee_asset,
            "fee_paid": str(order.fee_paid),
            "last_state": order.last_state,
            "creation_timestamp": order.creation_timestamp
        }

        self.assertEqual(expected_json, order.to_json())

    def test_deserialize_order_from_json(self):
        json = {
            "client_order_id": "OID1",
            "exchange_order_id": "EOID",
            "trading_pair": "COINALPHA-HBOT",
            "order_type": OrderType.LIMIT.name,
            "trade_type": TradeType.BUY.name,
            "price": "1000.0",
            "amount": "1.0",
            "executed_amount_base": "0.5",
            "executed_amount_quote": "510.0",
            "fee_asset": "BNB",
            "fee_paid": "10.0",
            "last_state": OrderState.PARTIALLY_FILLED.name,
            "creation_timestamp": 1640001112.0
        }

        order = InFlightOrderBase.from_json(json)

        self.assertEqual(json["client_order_id"], order.client_order_id)
        self.assertEqual(json["exchange_order_id"], order.exchange_order_id)
        self.assertEqual(json["trading_pair"], order.trading_pair)
        self.assertEqual(OrderType.LIMIT, order.order_type)
        self.assertEqual(TradeType.BUY, order.trade_type)
        self.assertEqual(Decimal(json["price"]), order.price)
        self.assertEqual(Decimal(json["amount"]), order.amount)
        self.assertEqual(Decimal(json["executed_amount_base"]), order.executed_amount_base)
        self.assertEqual(Decimal(json["executed_amount_quote"]), order.executed_amount_quote)
        self.assertEqual(json["fee_asset"], order.fee_asset)
        self.assertEqual(Decimal(json["fee_paid"]), order.fee_paid)
        self.assertEqual(OrderState.PARTIALLY_FILLED.name, order.last_state)
        self.assertEqual(json["creation_timestamp"], order.creation_timestamp)

    def test_to_limit_order(self):
        order = InFlightOrderBase(
            client_order_id="OID1",
            exchange_order_id="EOID1",
            trading_pair="COINALPHA-HBOT",
            order_type=OrderType.LIMIT,
            trade_type=TradeType.BUY,
            price=Decimal(1000),
            amount=Decimal(1),
            initial_state=OrderState.PENDING_CREATE.name,
            creation_timestamp=1640001112.223330
        )

        limit_order = order.to_limit_order()

        self.assertEqual("OID1", limit_order.client_order_id)
        self.assertEqual("COINALPHA-HBOT", limit_order.trading_pair)
        self.assertTrue(limit_order.is_buy)
        self.assertEqual("COINALPHA", limit_order.base_currency)
        self.assertEqual("HBOT", limit_order.quote_currency)
        self.assertEqual(Decimal(1000), limit_order.price)
        self.assertEqual(Decimal(1), limit_order.quantity)
        self.assertTrue(limit_order.filled_quantity.is_nan())
        self.assertEqual(1640001112223330, limit_order.creation_timestamp)
        self.assertEqual(LimitOrderStatus.UNKNOWN, limit_order.status)


# coding:utf-8

""" 価値に基づく手法 (Value-based Methods)
    今回は，価値に基づく代表的な手法として，
    ニューラルネットワークを用いて価値関数を近似した手法である．
    Deep Q-Network(DQN)を扱います．
"""

# 必要なライブラリのインポート．
import numpy as np
import copy
from collections import deque
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

""" Deep Q-Network(DQN)
    価値関数をニューラルネットワークによって近似することで，
    状態空間の離散化を行わずに連続値のまま扱うことができます．

    ・経験リプレイ(experience replay)の利用
    　リプレイバッファ(replay buffer)にこれまでの状態遷移を記録しておき，
    　そこからサンプルすることでQ関数のミニバッチ学習をします．

    ・固定したターゲットネットワーク(fixed target Q-network)の利用
    　学習する対象のQ関数と更新によって近づける目標値は，
    　同じパラメータを持つQ関数を利用しているため，そのまま勾配法による最適化を行うと，
    　元のQ値と目標値の両方が更新されてしまいます．
    　これを避けるために，目標値は固定した上でQ関数の最適化を行います．
"""

# Q関数の定義．
class QNetwork(nn.Module):
    def __init__(self, num_state, num_action, hidden_size=16):
        super(QNetwork, self).__init__()
        self.fc1 = nn.Linear(num_state, hidden_size)
        self.fc2 = nn.Linear(hidden_size, hidden_size)
        self.fc3 = nn.Linear(hidden_size, hidden_size)
        self.fc4 = nn.Linear(hidden_size, num_action)

    def forward(self, x):
        h = F.elu(self.fc1(x))
        h = F.elu(self.fc2(h))
        h = F.elu(self.fc3(h))
        y = F.elu(self.fc4(h))
        return y

# リプレイバッファの定義．
class ReplayBuffer:
    def __init__(self, memory_size):
        self.memory_size = memory_size
        self.memory = deque([], maxlen = memory_size)

    def append(self, transition):
        self.memory.append(transition)

    def sample(self, batch_size):
        batch_indexes = np.random.randint(0, len(self.memory), size=batch_size)
        states      = np.array([self.memory[index]['state'] for index in batch_indexes])
        next_states = np.array([self.memory[index]['next_state'] for index in batch_indexes])
        rewards     = np.array([self.memory[index]['reward'] for index in batch_indexes])
        actions     = np.array([self.memory[index]['action'] for index in batch_indexes])
        dones   = np.array([self.memory[index]['done'] for index in batch_indexes])
        return {'states': states, 'next_states': next_states, 'rewards': rewards, 'actions': actions, 'dones': dones}

# DQNエージェントのクラス．
class DqnAgent:
    def __init__(self, num_state, num_action, gamma=0.99, lr=0.001, batch_size=32, memory_size=50000):
        self.num_state = num_state
        self.num_action = num_action
        self.gamma = gamma                          # 割引率．
        self.batch_size = batch_size                # Q関数の更新に用いる遷移の数．
        self.qnet = QNetwork(num_state, num_action)
        self.target_qnet = copy.deepcopy(self.qnet) # ターゲットネットワーク．
        self.optimizer = optim.Adam(self.qnet.parameters(), lr=lr)
        self.replay_buffer = ReplayBuffer(memory_size)

    # Q関数を更新．
    def update_q(self):
        batch = self.replay_buffer.sample(self.batch_size)
        q = self.qnet(torch.tensor(batch["states"], dtype=torch.float))
        targetq = copy.deepcopy(q.data.numpy())

        # maxQの計算．
        maxq = torch.max(self.target_qnet(torch.tensor(batch["next_states"],dtype=torch.float)), dim=1).values

        # targetqのなかで，バッチのなかで実際に選択されていた行動 batch["actions"][i] に対応する要素に対して，Q値のターゲットを計算してセット．
        # 注意：選択されていない行動のtargetqの値はqと等しいためlossを計算する場合には影響しない．
        for i in range(self.batch_size):

            # 終端状態の場合はmaxQを0にしておくと学習が安定します（ヒント：maxq[i] * (not batch["dones"][i])）
            targetq[i, batch["actions"][i]] = batch["rewards"][i] + self.gamma * maxq[i] * (not batch["dones"][i])

        self.optimizer.zero_grad()

        # lossとしてMSEを利用．
        loss = nn.MSELoss()(q, torch.tensor(targetq))
        loss.backward()
        self.optimizer.step()

        # ターゲットネットワークのパラメータを更新．
        self.target_qnet = copy.deepcopy(self.qnet)

    # Q値が最大の行動を選択．
    def get_greedy_action(self, state):
        state_tensor = torch.tensor(state, dtype=torch.float).view(-1, self.num_state)
        action = torch.argmax(self.qnet(state_tensor).data).item()
        return action

    # ε-greedyに行動を選択．
    def get_action(self, state, episode):
        epsilon = 0.7 * (1/(episode+1))  # ここでは0.5から減衰していくようなεを設定．
        if epsilon <= np.random.uniform(0,1):
            action = self.get_greedy_action(state)
        else:
            action = np.random.choice(self.num_action)
        return action


import os
import sys
import numpy as np
import json
import io
from sklearn.model_selection import StratifiedKFold
from tqdm import tqdm

import paddle
from pgl.utils.data import Dataset,Dataloader

import pgl
from pgl.utils.logger import log

def random_split(dataset, split_ratio=0.7, seed=2019, shuffle=True):
    """random splitter"""
    np.random.seed(seed)
    indices = list(range(len(dataset)))
    np.random.shuffle(indices)
    trn_split = int(split_ratio * len(dataset))
    test_split = int(0.85 * len(dataset))

    train_idx, test_idx, valid_idx = indices[:trn_split], indices[trn_split:test_split], indices[test_split:]

    log.info("train_set : valid_set : test_set = %d : %d: %d" %
             (len(train_idx), len(valid_idx), len(test_idx)))
    return Subset(dataset, train_idx), Subset(dataset, valid_idx), Subset(dataset, test_idx)

class Subset(Dataset):
    """
    Subset of a dataset at specified indices.
    """

    def __init__(self, dataset, indices):
        self.dataset = dataset
        self.indices = indices

    def __getitem__(self, idx):
        """getitem"""
        return self.dataset[self.indices[idx]]

    def __len__(self):
        """len"""
        return len(self.indices)

class SIGNDataset(Dataset):

    """Dataset for Detecting Beneficial Feature Interactions for Recommender Systems (L0_SIGN)
    Adapted from https://github.com/ruizhang-ai/SIGN-Detecting-Beneficial-Feature-Interactions-for-Recommender-Systems/data/ml-tag/ml-tag.data.
    """

    def __init__(self,
                 data_path,
                 dataset_name,
                 pred_edges=1,
                 self_loop=True,
                 degree_as_nlabel=False):
        self.data_path = data_path
        self.dataset_name = dataset_name
        self.pred_edges = pred_edges
        self.self_loop = self_loop
        self.degree_as_nlabel = degree_as_nlabel

        self.graph_list = []
        self.glabel_list = []

        # global num
        self.num_graph = 0  # total graphs number
        self.num_feature = 0
        self.n = 0  # total nodes number
        self.m = 0  # total edges number

        self._load_data()

    def __len__(self):
        """return the number of graphs"""
        return len(self.graph_list)

    def __getitem__(self, idx):
        """getitem"""
        return self.graph_list[idx], self.glabel_list[idx]

    def read_data(self):
        # handle node and class
        filename = os.path.join(self.data_path, self.dataset_name,
                                "%s.data" % self.dataset_name)
        log.info("loading data from %s" % filename)
        node_list = []
        label = []
        max_node_index = 0
        data_num = 0
        with open(filename, 'r') as f:
            for line in f:
                data_num += 1
                data = line.split()
                # the first element is the label of the class
                label.append(float(data[0]))
                #the rest of the elements are the nodes
                int_list = [int(data[i]) for i in range(len(data))[1:]]
                node_list.append(int_list)
                if max_node_index < max(int_list):
                    max_node_index = max(int_list)

        if not self.pred_edges:
            # pass
            edge_list = []
            sr_list = []    #sender_receiver_list, containing node index
            for nodes in node_list:
                edge_l, sr_l = self.construct_full_edge_list(nodes)
                edge_list.append(edge_l)
                # sr_list.append(sr_l)

                # print('edge_l:',edge_l)
                # print('sr_l:',sr_l)
            # edge_list = [[[],[]] for _ in range(data_num)]
            # sr_list = []
            # # handle edges
            # with open(self.edgefile, 'r') as f:
            #     for line in f:
            #         edge_info = line.split()
            #         node_index = int(edge_info[0])
            #         edge_list[node_index][0].append(int(edge_info[1]))
            #         edge_list[node_index][1].append(int(edge_info[2]))
        else:
            edge_list = []
            sr_list = []    #sender_receiver_list, containing node index
            for nodes in node_list:
                edge_l, sr_l = self.construct_full_edge_list(nodes)
                edge_list.append(edge_l)
                sr_list.append(sr_l)

        # print(label[0:10])
        # print('==========='*10)
        label = self.construct_one_hot_label(label) # 去掉
        # print(label[0:10])

        return node_list, edge_list, label, sr_list, max_node_index + 1, data_num

    def construct_full_edge_list(self, nodes):
        num_node = len(nodes)
        edge_list = [[],[]]         #first for sender, second for receiver
        sender_receiver_list = []
        for i in range(num_node):
            for j in range(num_node)[i:]:
                edge_list[0].append(i)
                edge_list[1].append(j)
                sender_receiver_list.append([nodes[i],nodes[j]])

        return edge_list, sender_receiver_list

    def construct_one_hot_label(self, label):
        """Convert an iterable of indices to one-hot encoded labels."""
        nb_classes = int(max(label)) + 1
        targets = np.array(label, dtype=np.int32).reshape(-1)
        return np.eye(nb_classes)[targets]

    def _load_data(self):
        """Loads dataset
        """
        self.node, edge, label, self.sr_list, node_num, data_num = self.read_data()

        self.num_graph = len(self.node)
        self.num_feature = node_num

        for i in tqdm(range(self.num_graph)):
            # if int((i + 1) %  100000) == 0:
            #     log.info("processing graph %s" % (i + 1))

            graph = dict()
            edges = []
            num_edges = 0

            node_features = np.array(self.node[i],dtype='int32').reshape(len(self.node[i]),1)
            num_nodes = len(self.node[i])

            for u,v in zip(edge[i][0],edge[i][1]):
                u_v = (u,v)
                edges.append(u_v)

            num_edges = len(edges)

            self.glabel_list.append(label[i])

            if self.pred_edges:
                sr = self.sr_list[i]    #the sender receiver list, stored in edge_attr
            else:
                sr = []

            graph['node_attr'] = node_features
            graph['num_nodes'] = num_nodes
            graph['edge_attr'] = sr
            graph['edges'] = edges

            assert num_edges == len(edges)

            g = pgl.Graph(
                num_nodes=graph['num_nodes'],
                edges=graph['edges'],
                node_feat={'node_attr': graph['node_attr']},
                edge_feat={'edge_attr': graph['edge_attr']})

            self.graph_list.append(g)

            # update statistics of graphs
            self.n += graph['num_nodes']
            self.m += num_edges

        msg = "Finished loading data\n"
        msg += """
                num_graph:%d
                num_feature:%d
                nodes/graph:%d
                num_edges:%d
                """ %(
                    self.num_graph,
                    self.num_feature,
                    self.n/self.num_graph,
                    self.m
                )
        log.info(msg)

def collate_fn(batch_data):
    graphs = []
    labels = []
    for g, l in batch_data:
        graphs.append(g)
        labels.append(l)

    labels = np.array(labels, dtype="int64")

    return graphs, labels

if __name__ == "__main__":
    signdataset = SIGNDataset("./data/", "twitter", pred_edges=0)

    train_ds,val_ds,test_ds = random_split(signdataset)

    loader = Dataloader(
        train_ds,
        batch_size=3,
        shuffle=False,
        num_workers=1,
        collate_fn=collate_fn)
    cc = 0
    for batch in loader:
        g, label = batch
        # g = pgl.Graph.batch(g).tensor()

        print(label)
        print(g.num_graph)
        print('====='*20)
        # print(g.edges)
        print(g.graph_node_id)

        print(g.node_feat['node_attr'])
        print(g.edge_feat['edge_attr'])

        # for data in g:
        #     print(data.edges)

        # for data in g:
        #     print(data.node_feat['node_attr'])

        # for data in g:
        #     print(data.edge_feat['edge_attr'])

        cc += 1
        if cc == 2:
            break


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from pprint import pprint

class UpperAttrMetaclass(type):
    def __new__(cls, name, bases, dct):
        attrs = ((name, value) for name, value in dct.items() if not name.startswith('__'))
        uppercase_attr = dict((name.upper(), value) for name, value in attrs)
        return super(UpperAttrMetaclass, cls).__new__(cls, name, bases, uppercase_attr)

class UpperCaseMetaClass(type):

    def __new__(cls, name, bases, attrs):
        new_attrs = dict((name.upper(), val) for name, val in attrs.items() if not name.startswith('__'))
        return super().__new__(cls, name, bases, new_attrs)
        #return type(name, bases, new_attrs)

# for python 2.x version
class Foo:
    __metaclass__ = UpperAttrMetaclass
    bar = 'aaa'

# for python 3.x version
class Bar(object, metaclass=UpperCaseMetaClass):
    foo = 'foo'

class Hello:

    def hello(self, name="world"):
        print("hello, {}".format(name))

def fn(self, name="world"):
    print("hello, {}".format(name))

Hello1 = type('Hello1', (object, ), dict(hello=fn))

if __name__ == '__main__':
    print("testing for metaclass....")

    f = Foo()
    pprint([a for a in dir(f) if not a.startswith('__')])

    b = Bar()
    pprint([a for a in dir(b) if not a.startswith('__')])

    h = Hello()
    h.hello('tww')
    print(type(h))
    print(type(Hello))

    h = Hello1()
    h.hello('tww')
    print(type(h))
    print(type(Hello))


import threading
from time import sleep

CAPACITY = 2
buffer = ["a"] * CAPACITY
buffer_in_index = 0
buffer_out_index = 0

mutex = threading.Semaphore()
bhargav = threading.Semaphore()
no_of_unoccupied_slots = threading.Semaphore(CAPACITY)
no_of_occupied_slots = threading.Semaphore(0)

class Producer(threading.Thread):
    def run(self):
        global CAPACITY, buffer, buffer_in_index, buffer_out_index
        global mutex, no_of_unoccupied_slots, no_of_occupied_slots

        pitem = 0
        counter = 0

        while pitem < 6:
            no_of_unoccupied_slots.acquire()
            bhargav.acquire()
            mutex.acquire()

            counter += 1
            buffer[buffer_in_index] = counter
            buffer_in_index = (buffer_in_index + 1) % CAPACITY
            print("produced item is: ", counter)

            mutex.release()
            no_of_occupied_slots.release()
            bhargav.release()

            sleep(1)

            pitem += 1

# Consumer Thread Class
class Consumer(threading.Thread):
    def run(self):
        global CAPACITY, buffer, buffer_in_index, buffer_out_index, counter
        global mutex, no_of_unoccupied_slots, no_of_occupied_slots

        consumed_items = 0

        while consumed_items < 10:
            no_of_occupied_slots.acquire()
            mutex.acquire()

            item = buffer[buffer_out_index]
            buffer_out_index = (buffer_out_index + 1) % CAPACITY
            print("Citem : ", item)

            mutex.release()
            no_of_unoccupied_slots.release()

            sleep(2.5)

            consumed_items += 1

producer = Producer()
consumer = Consumer()

consumer.start()
producer.start()

producer.join()
consumer.join()


import wx
import service

class Layout(wx.Frame):
    def __init__(self, parent=None, size=(1000, 700), title="Video Temporal Labeling Tool"):
        wx.Frame.__init__(self, parent=parent, size=size, title=title)
        self.service = service.Service(self)

        self.initUI()

    def initUI(self):
        self.initMenu()

        # set up dataset info inputs
        self.datasetInput = wx.TextCtrl(self)

        inputStyle = wx.ALIGN_CENTER | wx.ALL
        datasetInputBox = wx.BoxSizer()
        datasetInputBox.Add(wx.StaticText(self, label="Dataset Path"), 1, inputStyle, border=10)
        datasetInputBox.Add(self.datasetInput, 5, inputStyle, border=10)

        # set up store path inputs
        self.storeInput = wx.TextCtrl(self)
        storeInputBox = wx.BoxSizer()
        storeInputBox.Add(wx.StaticText(self, label="Store Path"), 1, inputStyle, border=10)
        storeInputBox.Add(self.storeInput, 5, inputStyle, border=10)

        # set up input box (left)
        inputBoxLeftStyle = wx.EXPAND | wx.ALL
        inputBoxLeft = wx.BoxSizer(wx.VERTICAL)
        inputBoxLeft.Add(datasetInputBox, 1, inputBoxLeftStyle, border=2)
        inputBoxLeft.Add(storeInputBox, 1, inputBoxLeftStyle, border=2)

        # set up input confirm button
        inputConfirm = wx.Button(self, label="Confirm")
        inputConfirm.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "confirm_input"))

        # set up input box
        inputBoxStyle = wx.ALIGN_CENTER | wx.ALL
        inputBox = wx.BoxSizer()
        inputBox.Add(inputBoxLeft, 6, inputBoxStyle, border=10)
        inputBox.Add(inputConfirm, 1, inputBoxStyle, border=10)

        # default image
        img = wx.Image("./resource/test_frame.jpg", wx.BITMAP_TYPE_JPEG)
        img = wx.Bitmap(img)

        # video infos box
        videoInfoStaticBox = wx.StaticBox(self, label='Video Information')
        videoInfoStaticBoxSizer = wx.StaticBoxSizer(videoInfoStaticBox, wx.VERTICAL)

        self.selectVideo = wx.Choice(self)
        self.selectVideo.Bind(wx.EVT_CHOICE, lambda x: self.service.button_event(x, "selectVideo"))
        videoBoxStyle = wx.EXPAND | wx.ALL

        self.videoFrame = wx.StaticText(self)

        videoGridBox = wx.FlexGridSizer(2, 2, 5, 5)
        videoGridBox.Add(wx.StaticText(self, label="Video: "), 1, videoBoxStyle)
        videoGridBox.Add(self.selectVideo, 3, videoBoxStyle)
        videoGridBox.Add(wx.StaticText(self, label="Current Frame: "), 1, videoBoxStyle)
        videoGridBox.Add(self.videoFrame, 1, videoBoxStyle)

        videoInfoStaticBoxSizer.Add(videoGridBox, 1, wx.EXPAND | wx.ALL, border=5)

        # video control box
        prevVideo = wx.Button(self, label="Previous")
        prevVideo.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "prevVideo"))

        nextVideo = wx.Button(self, label="Next")
        nextVideo.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "nextVideo"))

        videoControlBox = wx.StaticBox(self, label="Video Control")
        videoControlBoxSizer = wx.StaticBoxSizer(videoControlBox, wx.HORIZONTAL)
        videoControlStyle = wx.EXPAND | wx.ALL
        videoControlBoxSizer.Add(prevVideo, 1, videoControlStyle, border=5)
        videoControlBoxSizer.Add(nextVideo, 1, videoControlStyle, border=5)

        # label info box
        labelInfoBox = wx.StaticBox(self, label="Label Information")
        labelInfoBoxSizer = wx.StaticBoxSizer(labelInfoBox, wx.VERTICAL)

        labelInfoGirdBox = wx.GridSizer(2, 2, 5, 5)

        self.selectedStart = wx.StaticText(self, label='0')
        self.selectedEnd = wx.StaticText(self, label='0')

        labelInfoGirdBox.Add(wx.StaticText(self, label="Selected Start Frame: "), videoBoxStyle)
        labelInfoGirdBox.Add(self.selectedStart, videoBoxStyle)
        labelInfoGirdBox.Add(wx.StaticText(self, label="Selected End Frame: "), videoBoxStyle)
        labelInfoGirdBox.Add(self.selectedEnd, videoBoxStyle)

        labelInfoBoxSizer.Add(labelInfoGirdBox, 1, wx.EXPAND | wx.ALL, border=5)

        # control box
        controlStaticBox = wx.StaticBox(self, label="Frame Control")
        controlStaticBoxSizer = wx.StaticBoxSizer(controlStaticBox, wx.VERTICAL)

        prevNFrame = wx.Button(self, label="-FPI")
        prevNFrame.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "prevNFrame"))

        prevFrame = wx.Button(self, label="-1")
        prevFrame.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "prevFrame"))

        nextFrame = wx.Button(self, label="+1")
        nextFrame.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "nextFrame"))

        nextNFrame = wx.Button(self, label="+FPI")
        nextNFrame.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "nextNFrame"))

        buttonStyle = wx.EXPAND | wx.ALL

        controlGridBox = wx.GridSizer(2, 2, 5, 5)
        controlGridBox.Add(prevFrame, buttonStyle)
        controlGridBox.Add(nextFrame, buttonStyle)
        controlGridBox.Add(prevNFrame, buttonStyle)
        controlGridBox.Add(nextNFrame, buttonStyle)

        controlStaticBoxSizer.Add(controlGridBox, 1, wx.EXPAND, border=5)

        # label box
        labelBox = wx.StaticBox(self, label="Labelling Box")
        labelBoxSizer = wx.StaticBoxSizer(labelBox, wx.VERTICAL)

        selectStart = wx.Button(self, label="Start")
        selectStart.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "selectStart"))

        selectEnd = wx.Button(self, label="End")
        selectEnd.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "selectEnd"))

        # select label box
        selectLabelBox = wx.BoxSizer()
        selectLabelBox.Add(selectStart, 1, buttonStyle, border=5)
        selectLabelBox.Add(selectEnd, 1, buttonStyle, border=5)

        selectClose = wx.Button(self, label="Close")
        selectClose.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "selectClose"))

        selectMid = wx.Button(self, label="Mid")
        selectMid.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "selectMid"))

        selectFar = wx.Button(self, label="Far")
        selectFar.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "selectFar"))

        saveLabel = wx.Button(self, label="Save")
        saveLabel.Bind(wx.EVT_BUTTON, lambda e: self.service.button_event(e, "saveLabel"))

        selectTypeBox = wx.BoxSizer()
        selectTypeBox.Add(selectClose, 1, buttonStyle, border=5)
        selectTypeBox.Add(selectMid, 1, buttonStyle, border=5)
        selectTypeBox.Add(selectFar, 1, buttonStyle, border=5)

        labelBoxSizer.Add(selectLabelBox, 1, wx.EXPAND, border=5)
        labelBoxSizer.Add(selectTypeBox, 1, wx.EXPAND, border=5)
        labelBoxSizer.Add(saveLabel, 1, wx.EXPAND | wx.ALL, border=5)

        # action type selection
        actionTypeStaticBox = wx.StaticBox(self, label="Action Type")
        self.actionTypeChoice = wx.Choice(self)
        self.actionTypeChoice.Bind(wx.EVT_CHOICE, lambda x: self.service.button_event(x, "selectAction"))
        self.actionTypeChoice.SetItems(['tripping', 'faceoff'])
        self.actionTypeChoice.Selection = 0
        actionTypeStaticBoxSizer = wx.StaticBoxSizer(actionTypeStaticBox)
        actionTypeStaticBoxSizer.Add(self.actionTypeChoice, 1, wx.EXPAND, border=5)

        # info box
        infoBox = wx.BoxSizer(wx.VERTICAL)
        infoBoxStyle = wx.EXPAND
        infoBox.Add(videoInfoStaticBoxSizer, 2, infoBoxStyle, border=5)
        infoBox.Add(videoControlBoxSizer, 2, infoBoxStyle, border=5)
        infoBox.Add(labelInfoBoxSizer, 2, infoBoxStyle, border=5)
        infoBox.Add(controlStaticBoxSizer, 2, infoBoxStyle, border=5)
        infoBox.Add(actionTypeStaticBoxSizer, 1, infoBoxStyle, border=5)
        infoBox.Add(labelBoxSizer, 3, infoBoxStyle, border=5)

        # display box
        self.display = wx.StaticBitmap(parent=self)
        self.display.SetBitmap(img)
        displayBoxStyle = wx.ALIGN_CENTER | wx.ALL
        displayBox = wx.BoxSizer()

        frameBox = wx.StaticBox(self, label="Video Frame")
        frameBoxSizer = wx.StaticBoxSizer(frameBox)
        frameBoxSizer.Add(self.display, 1, wx.EXPAND)

        displayBox.Add(frameBoxSizer, 5, displayBoxStyle, border=10)
        displayBox.Add(infoBox, 2, displayBoxStyle, border=10)

        # set up feedback message
        self.feedbackMessage = wx.StaticText(self, label="Welcome to use Video Temporal Labelling Tool v1.1")
        self.feedbackMessage.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL))

        # construct main layout
        myStyle = wx.ALL | wx.ALIGN_CENTER
        mainBox = wx.BoxSizer(wx.VERTICAL)
        mainBox.Add(self.feedbackMessage, 1, myStyle, border=1)
        mainBox.Add(inputBox, 6, myStyle, border=1)
        mainBox.Add(displayBox, 20, myStyle, border=1)

        # set main box in frame
        self.SetSizer(mainBox)
        self.Centre()

        # accelerator table

    def initMenu(self):
        menuBar = wx.MenuBar()

        fileMenu = wx.Menu()

        # fps selection menu
        fpsMenu = wx.Menu()
        fpsMenu.Append(wx.MenuItem(fileMenu, id=11, text="5", kind=wx.ITEM_RADIO))
        fpsMenu.Append(wx.MenuItem(fileMenu, id=12, text="10", kind=wx.ITEM_RADIO))
        fpsMenu.Append(wx.MenuItem(fileMenu, id=13, text="15", kind=wx.ITEM_RADIO))
        fpsMenu.Append(wx.MenuItem(fileMenu, id=14, text="20", kind=wx.ITEM_RADIO))
        fpsMenu.Append(wx.MenuItem(fileMenu, id=15, text="30", kind=wx.ITEM_RADIO))

        fileMenu.Append(wx.ID_ANY, "Frame Per Interval", fpsMenu)

        menuBar.Append(fileMenu, title="Setting")

        self.SetMenuBar(menuBar)

        self.Bind(wx.EVT_MENU, self.service.menu_event)

    def updateMessage(self, msg):
        self.feedbackMessage.SetLabel("Message: "+msg)
        self.feedbackMessage.SetForegroundColour((0,0,0))

    def updateError(self, error):
        self.feedbackMessage.SetLabel("Error: "+error)
        self.feedbackMessage.SetForegroundColour((255,0,0))

    def updateFrame(self, cv2_frame, no):
        h, w = cv2_frame.shape[:2]
        self.display.SetBitmap(wx.Bitmap.FromBuffer(w, h, cv2_frame))
        self.videoFrame.SetLabel(str(no))

    def updateStartLabel(self, start):
        self.selectedStart.SetLabel(str(start))

    def updateEndLabel(self, end):
        self.selectedEnd.SetLabel(str(end))

class App(wx.App):
    """
        This is only for displaying the UI
    """

    def OnInit(self):
        a = Layout()
        a.Show()
        return True

if __name__ == '__main__':
    try:
        debug = App()
        debug.MainLoop()
    except:
        exit(0)


import os
import math
from functools import partial

from PyQt4 import QtCore, QtGui

import config

lineRadius = 2
ITEM_LINE = 0
ITEM_FG = 1
ITEM_SPAWN = 2
ITEM_RECT = 3

class ZoneSceneView(QtGui.QGraphicsView):
    def __init__(self, scene, parent):
        super(ZoneSceneView, self).__init__(scene)
        self.scene = scene
        self.parent = parent
        self.setMouseTracking(True)
        self.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.gray))
        self.Scale = 1
        self.mousePrev = False
        self.objects = []
        self.linePreview = None
        self.selected = None
        self.moveSteps = 10
        self.keysDown = set()

    def snapToLine(self, pos, ignore=None):
        if QtCore.Qt.Key_Alt in self.keysDown: return # don't snap

        for obj in self.objects:
            if obj["obj"] == ignore: continue

            if obj["type"] == ITEM_LINE:
                x1 = obj["obj"].x()
                y1 = obj["obj"].y()
                x2 = x1 + obj["obj"].line().x2()
                y2 = y1 + obj["obj"].line().y2()
                if pos.x() >= x1-lineRadius and pos.x() <= x1+lineRadius and pos.y() >= y1-lineRadius and pos.y() <= y1+lineRadius:
                    pos.setX(x1)
                    pos.setY(y1)
                elif pos.x() >= x2-lineRadius and pos.x() <= x2+lineRadius and pos.y() >= y2-lineRadius and pos.y() <= y2+lineRadius:
                    pos.setX(x2)
                    pos.setY(y2)

    def lineToItemPos(self, lineItem): # convert line x1,y1,x2,y2 to qgraphicsitem position & width
        line = lineItem.line()
        if line.x1() == 0 and line.y1() == 0: return # already done

        x1 = line.x1()
        x2 = line.x2()
        y1 = line.y1()
        y2 = line.y2()

        lineItem.setPos(x1, y1)
        line.setP1(QtCore.QPointF(0, 0))
        line.setP2(QtCore.QPointF(x2-x1, y2-y1))
        lineItem.setLine(line)

    def addLine(self, line):
        self.lineToItemPos(line)
        self.objects.append({"type": ITEM_LINE, "obj": line, "angle": line.line().angle(), "rect": None})
        self.parent.changes = True

    def deselect(self):
        if self.selected:
            pen = self.selected["rect"].pen()
            pen.setColor(QtCore.Qt.red)
            self.selected["rect"].setPen(pen)
            self.selected = None

    def keyPressEvent(self, event):
        if not event.isAutoRepeat():
            self.keysDown.add(event.key())

    def keyReleaseEvent(self, event):
        if not event.isAutoRepeat():
            self.keysDown.remove(event.key())

    def mousePressEvent(self, event):
        pos = self.mapToScene(event.pos())
        if event.button() == QtCore.Qt.LeftButton:
            if self.parent.currtool == 0: # select
                self.deselect()

                for obj in self.objects:
                    if obj["type"] == ITEM_LINE:
                        x1 = obj["obj"].x()
                        y1 = obj["obj"].y()
                        x2 = x1 + obj["obj"].line().x2()
                        y2 = y1 + obj["obj"].line().y2()
                        if pos.x() >= x1-lineRadius and pos.x() <= x1+lineRadius and pos.y() >= y1-lineRadius and pos.y() <= y1+lineRadius:
                            pen = obj["rect"].pen()
                            pen.setColor(QtCore.Qt.green)
                            obj["rect"].setPen(pen)
                            self.selected = {"moveType": ITEM_LINE, "type": obj["type"], "obj": obj["obj"], "line": obj["obj"].line(), "linePoint": 1, "rect": obj["rect"]}
                            self.moveSteps = 10
                            break
                        elif pos.x() >= x2-lineRadius and pos.x() <= x2+lineRadius and pos.y() >= y2-lineRadius and pos.y() <= y2+lineRadius:
                            pen = obj["rect"].pen()
                            pen.setColor(QtCore.Qt.green)
                            obj["rect"].setPen(pen)
                            self.selected = {"moveType": ITEM_LINE, "type": obj["type"], "obj": obj["obj"], "line": obj["obj"].line(), "linePoint": 2, "rect": obj["rect"]}
                            self.moveSteps = 10
                            break

                    if pos.x() >= obj["rect"].x() and pos.y() >= obj["rect"].y() and pos.x() <= obj["rect"].x() + obj["rect"].rect().width() and pos.y() <= obj["rect"].y() + obj["rect"].rect().height(): # ordinary object
                        pen = obj["rect"].pen()
                        pen.setColor(QtCore.Qt.green)
                        obj["rect"].setPen(pen)
                        self.selected = {"moveType": ITEM_RECT, "type": obj["type"], "obj": obj["obj"], "rect": obj["rect"]}
                        self.moveSteps = 10
                        break

            elif self.parent.currtool == 1: # draw line
                if self.linePreview: self.scene.removeItem(self.linePreview)
                self.linePreview = QtGui.QGraphicsLineItem(QtCore.QLineF(round(pos.x()), round(pos.y()), round(pos.x()), round(pos.y())), scene=self.scene)
                pen = self.linePreview.pen()
                pen.setWidth(1)
                pen.setCosmetic(False)
                self.linePreview.setPen(pen)

    def mouseMoveEvent(self, event):
        pos = self.mapToScene(event.pos())
        if self.parent.currtool == 0: # select tool
            if self.selected:
                if self.moveSteps > 0:
                    self.moveSteps -= 1
                    return

                if self.selected["moveType"] == ITEM_RECT:
                    self.selected["obj"].setPos(round(pos.x()), round(pos.y()))
                    if self.selected["type"] == ITEM_LINE: # moving the line as well
                        line = self.selected["obj"].line()

                        # if line's p2 has a negative coordinate, align it so it's inside the rect
                        if line.x2() < 0: self.selected["obj"].setX(self.selected["obj"].x() - line.x2())
                        if line.y2() < 0: self.selected["obj"].setY(self.selected["obj"].y() - line.y2())

                        # if line isn't diagonal, align it to be centered in the rect instead of the top
                        if line.x1() == line.x2(): self.selected["obj"].setX(self.selected["obj"].x() + 1)
                        if line.y1() == line.y2(): self.selected["obj"].setY(self.selected["obj"].y() + 1)
                    self.selected["rect"].setPos(round(pos.x()), round(pos.y()))
                elif self.selected["moveType"] == ITEM_LINE:
                    self.snapToLine(pos, self.selected["obj"])
                    line = self.selected["obj"].line() # copy
                    linePoint = getattr(line, "p%d"%self.selected["linePoint"])() # get line point
                    linePoint.setX(round(pos.x()) - self.selected["obj"].x())
                    linePoint.setY(round(pos.y()) - self.selected["obj"].y())
                    getattr(line, "setP%d"%self.selected["linePoint"])(linePoint) # set line point
                    self.selected["obj"].setLine(line)

        elif self.parent.currtool == 1: # draw line tool
            if self.linePreview:
                pos = QtCore.QPointF(round(pos.x()), round(pos.y()))
                self.snapToLine(pos)
                self.linePreview.setLine(self.linePreview.line().x1(), self.linePreview.line().y1(), pos.x(), pos.y())

    def mouseReleaseEvent(self, event):
        pos = self.mapToScene(event.pos())
        if event.button() == QtCore.Qt.LeftButton:
            if self.parent.currtool == 0: # select
                if self.selected and self.moveSteps <= 0:
                    pass # add undo action

                    if self.selected["moveType"] == ITEM_LINE:
                        line = self.selected["obj"].line()
                        x = self.selected["obj"].x()
                        y = self.selected["obj"].y()
                        x1 = line.x1()
                        y1 = line.y1()
                        x2 = line.x2()
                        y2 = line.y2()
                        line.setP1(QtCore.QPointF(x1 + x, y1 + y))
                        line.setP2(QtCore.QPointF(x2 + x, y2 + y))
                        self.selected["obj"].setPos(0, 0)
                        self.selected["obj"].setLine(line)
                        self.lineToItemPos(self.selected["obj"])

                        x1 = min(self.selected["obj"].x(), self.selected["obj"].x() + self.selected["obj"].line().x2())
                        x2 = max(self.selected["obj"].x(), self.selected["obj"].x() + self.selected["obj"].line().x2())
                        y1 = min(self.selected["obj"].y(), self.selected["obj"].y() + self.selected["obj"].line().y2())
                        y2 = max(self.selected["obj"].y(), self.selected["obj"].y() + self.selected["obj"].line().y2())
                        if x1 == x2:
                            x1 -= 1
                            x2 += 1
                        elif y1 == y2:
                            y1 -= 1
                            y2 += 1
                        self.selected["rect"].setPos(x1, y1)
                        self.selected["rect"].setRect(0, 0, x2-x1, y2-y1)

                self.deselect()

            elif self.parent.currtool == 1: # draw line
                if self.linePreview:
                    self.addLine(self.linePreview)
                    x1 = min(self.linePreview.x(), self.linePreview.x() + self.linePreview.line().x2())
                    x2 = max(self.linePreview.x(), self.linePreview.x() + self.linePreview.line().x2())
                    y1 = min(self.linePreview.y(), self.linePreview.y() + self.linePreview.line().y2())
                    y2 = max(self.linePreview.y(), self.linePreview.y() + self.linePreview.line().y2())
                    if x1 == x2:
                        x1 -= 1
                        x2 += 1
                    elif y1 == y2:
                        y1 -= 1
                        y2 += 1
                    rect = QtGui.QGraphicsRectItem(0, 0, x2-x1, y2-y1, scene=self.scene)
                    rect.setPos(x1, y1)
                    rect.setZValue(self.linePreview.zValue()-1)
                    pen = QtGui.QPen(QtCore.Qt.red)
                    pen.setWidth(1)
                    pen.setCosmetic(False)
                    rect.setPen(pen)
                    self.objects[-1]["rect"] = rect
                    self.linePreview = None

    @QtCore.pyqtSlot()
    def zoomIn(self):
        self.Scale += 1
        self.scale(2, 2)

    @QtCore.pyqtSlot()
    def zoomOut(self):
        self.Scale -= 1
        if self.Scale <= 0:
            self.Scale = 1
            return False
        self.scale(0.5, 0.5)
        return True

class ZoneEditor(QtGui.QWidget):
    def __init__(self):
        super(ZoneEditor, self).__init__()

        self.changes = False
        self.currtool = 0

        layout = QtGui.QHBoxLayout(self)
        leftlayout = QtGui.QVBoxLayout()
        leftlayout.setAlignment(QtCore.Qt.AlignTop)
        rightlayout = QtGui.QVBoxLayout()
        rightlayout.setAlignment(QtCore.Qt.AlignTop)

        ######### left layout #########
        # zone list
        self.zonelist = QtGui.QListWidget()
        self.zonelist.setMaximumSize(200, 300)
        self.zonelist.setMinimumSize(200, 300)
        self.zonelist.itemDoubleClicked.connect(self.onClickZoneItem)
        self.refreshZoneList()

        # tool layout
        toollayout = QtGui.QGridLayout()
        toollayout.setAlignment(QtCore.Qt.AlignLeft)
        toollayout.setHorizontalSpacing(2)
        toollayout.setVerticalSpacing(2)

        # select tool
        self.selecttool = QtGui.QPushButton()
        self.selecttool.setMaximumSize(32,32)
        self.selecttool.setToolTip("Selection tool")
        selecttoolicn = QtGui.QIcon(QtGui.QPixmap("img/tool-select.png"))
        self.selecttool.setIcon(selecttoolicn)
        self.selecttool.setIconSize(selecttoolicn.availableSizes()[0])
        self.selecttool.setCheckable(True)
        self.selecttool.toggled.connect(partial(self.onClickTool, 0))
        # line tool
        self.linetool = QtGui.QPushButton()
        self.linetool.setMaximumSize(32,32)
        self.linetool.setToolTip("Draw collision line tool")
        linetoolicn = QtGui.QIcon(QtGui.QPixmap("img/tool-line.png"))
        self.linetool.setIcon(linetoolicn)
        self.linetool.setIconSize(linetoolicn.availableSizes()[0])
        self.linetool.setCheckable(True)
        self.linetool.toggled.connect(partial(self.onClickTool, 1))
        # all tools for use on the onClickTool slot
        self.tools = [self.selecttool, self.linetool]

        ######### right layout #########
        # zone view
        self.scene = QtGui.QGraphicsScene(0, 0, 1, 1)
        self.view = ZoneSceneView(self.scene, self)
        self.zonebg = None # qgraphicspixmapitem

        # some tools (zoom, etc)
        zonetoollayout = QtGui.QHBoxLayout()
        # zoom in
        self.zoomInbtn = QtGui.QPushButton()
        self.zoomInbtn.setMaximumSize(32,32)
        self.zoomInbtn.setToolTip("Zoom in")
        zoominicn = QtGui.QIcon(QtGui.QPixmap("img/zoom-in.png"))
        self.zoomInbtn.setIcon(zoominicn)
        self.zoomInbtn.setIconSize(zoominicn.availableSizes()[0])
        self.zoomInbtn.clicked.connect(self.view.zoomIn)
        # zoom out
        self.zoomOutbtn = QtGui.QPushButton()
        self.zoomOutbtn.setMaximumSize(32,32)
        self.zoomOutbtn.setToolTip("Zoom out")
        zoomouticn = QtGui.QIcon(QtGui.QPixmap("img/zoom-out.png"))
        self.zoomOutbtn.setIcon(zoomouticn)
        self.zoomOutbtn.setIconSize(zoomouticn.availableSizes()[0])
        self.zoomOutbtn.clicked.connect(self.view.zoomOut)

        ## left side layout: ##
        # add tools to tool layout
        toollayout.addWidget(self.selecttool, 0, 0)
        toollayout.addWidget(self.linetool, 0, 1)
        # add tool layout to left layout
        leftlayout.addWidget(self.zonelist)
        leftlayout.addLayout(toollayout)

        ## right side layout: ##
        # add tools below zone view to a layout
        zonetoollayout.setAlignment(QtCore.Qt.AlignLeft)
        zonetoollayout.addWidget(self.zoomInbtn)
        zonetoollayout.addWidget(self.zoomOutbtn)
        # add zone view and some tool buttons to right layout
        rightlayout.addWidget(self.view)
        rightlayout.addLayout(zonetoollayout)
        # add left & right layouts to the final main layout
        layout.addLayout(leftlayout)
        layout.addLayout(rightlayout)

        self.onClickTool(0, True) # default to the select tool

    def refreshZoneList(self):
        self.zonelist.clear()
        for f in os.listdir(config.datadir+"/zones"):
            if os.path.isdir(config.datadir+"/zones/"+f):
                self.zonelist.addItem(f)
        self.zonelist.sortItems()

    def loadZone(self, name):
        if os.path.isdir(config.datadir+"/zones/"+name):
            if self.changes:
                answer = QtGui.QMessageBox(QtGui.QMessageBox.Question, "Unsaved changes", "Would you like to save changes?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | QtGui.QMessageBox.Cancel).exec_()
                if answer == QtGui.QMessageBox.Yes:
                    pass
                elif answer == QtGui.QMessageBox.Cancel:
                    return # stop loading
                self.changes = False

            self.scene.clear()
            self.view.objects = []

            self.zonebg = QtGui.QGraphicsPixmapItem(scene=self.scene)
            pixmap = QtGui.QPixmap(config.datadir+"/zones/"+name+"/zone.png")
            self.zonebg.setPixmap(pixmap)
            self.zonebg.setZValue(-10000)
            self.scene.setSceneRect(0, 0, pixmap.size().width(), pixmap.size().height())
            while self.view.zoomOut(): pass # set to normal scale

    @QtCore.pyqtSlot("int", "bool")
    def onClickTool(self, ind, toggled):
        self.currtool = ind
        for tool in self.tools:
            tool.blockSignals(True)
            tool.setChecked(self.tools[ind] == tool)
            tool.blockSignals(False)

    @QtCore.pyqtSlot("QListWidgetItem")
    def onClickZoneItem(self, item):
        self.loadZone(item.text())


class Solution:
    def networkDelayTime(self, times, N, K):
        """
        :type times: List[List[int]]
        :type N: int
        :type K: int
        :rtype: int
        """
        from collections import defaultdict
        edges = defaultdict(dict)
        for u, v, w in times:
            edges[u][v] = w
        reach = {K: 0}
        frontier = {K}
        while frontier:
            u = frontier.pop()
            cur_dist = reach[u]
            for v, d in edges[u].items():
                if v not in reach or cur_dist + d < reach[v]:
                    reach[v] = cur_dist + d
                    frontier.add(v)
        if len(reach) == N:
            return max(reach.values())
        else:
            return -1

fn = Solution().networkDelayTime

print(fn([[1, 2, 3], [1, 3, 1], [3, 2, 3]], 3, 1))
print(fn([], 2, 1))


#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import string
letters =string.ascii_uppercase[:8]

f = open('test.txt', 'r')
contents = [i.rstrip() for i in f]
#print(contents)
f.close()

teams = ['Team_' + s for s in letters]
print(teams)

numbers = list(range(12))
new_numbers = [i for i in range(12)]

s_numbers = [ n ** 2 for n in new_numbers]
print(s_numbers)

str_nums = [ str(i) for i in s_numbers]
print('+'.join(str_nums))
print(eval('+'.join(str_nums)))

print(sum(s_numbers))

geometric_prog = [ 3 * (2**n) for n in range(10)]
print(sum(geometric_prog))


from __future__ import division

import torch
import torch.nn as nn
import torch.nn.functional as f
from torch.autograd import Variable
import numpy as np
from util import *

def read_cfg(file):
	file = open(file, 'r')
	lines = file.read().split('\n')
	lines = [line for line in lines if len(line)>0]
	lines = [line for line in lines if line[0] != '#']
	lines = [line.rstrip().lstrip() for line in lines]

	block = {}
	blocks = []
	for line in lines:
		if line[0] == '[':
			if len(block) != 0:
				blocks.append(block)
				block = {}
			block['type'] = line[1: -1].rstrip()

		else:
			key, value = line.split('=')
			block[key.rstrip()] = value.lstrip()

	blocks.append(block)

	return blocks

def create_modules(blocks):
	net_info = blocks[0]
	module_list = nn.ModuleList()
	prev_filters = 3
	output_filters = []

	for index, x in enumerate(blocks[1:]):
		module = nn.Sequential()

		if (x['type'] == 'convolutional'):

			activation = x['activation']

			try:
				batch_normalize = int(x['batch_normalize'])
				bias = False

			except:
				batch_normalize = 0
				bias = True

			filters = int(x['filters'])
			padding = int(x['pad'])
			kernel_size = int(x['size'])
			stride = int(x['stride'])

			if padding:
				pad = (kernel_size - 1)//2
			else:
				pad = 0

			conv = nn.Conv2d(prev_filters, filters, kernel_size, stride, pad, bias = bias)
			module.add_module('conv_{0}'.format(index), conv)

			if batch_normalize:
				norm = nn.BatchNorm2d(filters)
				module.add_module('batch_norm_{0}'.format(index), norm)

			if activation == 'leaky':
				act = nn.LeakyReLU(0.1, inplace = True)
				module.add_module('leaky_{0}'.format(index), act)

		elif (x['type'] == 'upsample'):
			stride = int(x['stride'])
			upsample = nn.Upsample(scale_factor = 2, mode = 'bilinear')
			module.add_module('upsample_{0}'.format(index), upsample)

		elif (x['type'] == 'route'):
			x['layers'] = x['layers'].split(',')

			start = int(x['layers'][0])

			try:
				end = int(x['layers'][1])
			except:
				end = 0

			if start >0 :
				start = start - index

			if end > 0:
				end = end - index

			route = EmptyLayer()
			module.add_module('route_{0}'.format(index), route)

			if len(x['layers']) > 1:
				filters = output_filters[index + start] + output_filters[index + end]

			else:
				filters = output_filters[index + start]

		elif (x['type'] == 'shortcut'):
			shortcut = EmptyLayer()
			module.add_module('shortcut_{0}'.format(index), shortcut)

		elif (x['type'] == 'yolo'):
			mask = x['mask'].split(',')
			mask = [int(a) for a in mask]

			anchors = x['anchors'].split(',')
			anchors = [int(a) for a in anchors]
			anchors = [(anchors[i], anchors[i + 1]) for i in range(0, len(anchors), 2)]
			anchors = [anchors[i] for i in mask]
			num_classes = int(net_info['classes'])
			img_dim = int(net_info['height'])
			detection = DetectionLayer(anchors, num_classes, img_dim)
			module.add_module('detection_{0}'.format(index), detection)

		module_list.append(module)
		prev_filters = filters
		output_filters.append(filters)

	return(net_info, module_list)

class EmptyLayer(nn.Module):
	def __init__(self):
		super(EmptyLayer, self).__init__()

class DetectionLayer(nn.Module):
	def __init__(self, anchors, num_classes, img_dim):
		super(DetectionLayer, self).__init__()
		self.anchors = anchors
		self.num_anchors = len(anchors)
		self.num_classes = num_classes
		self.bbox_attrs = 5 + num_classes
		self.image_dim = img_dim
		self.ignore_thres = 0.5
		self.lambda_coord = 1

		self.mse_loss = nn.MSELoss(size_average=True)  # Coordinate loss
		self.bce_loss = nn.BCELoss(size_average=True)  # Confidence loss
		self.ce_loss = nn.CrossEntropyLoss()  # Class loss

	def forward(self, x, targets = None):
		nA = self.num_anchors
		nB = x.size(0)
		nG = x.size(2)
		stride = self.image_dim / nG

		FloatTensor = torch.cuda.FloatTensor if x.is_cuda else torch.FloatTensor
		LongTensor = torch.cuda.LongTensor if x.is_cuda else torch.LongTensor
		ByteTensor = torch.cuda.ByteTensor if x.is_cuda else torch.ByteTensor

		prediction = x.view(nB, nA, self.bbox_attrs, nG, nG).permute(0, 1, 3, 4, 2).contiguous()
		x = torch.sigmoid(prediction[..., 0])  # Center x
		y = torch.sigmoid(prediction[..., 1])  # Center y
		w = prediction[..., 2]  # Width
		h = prediction[..., 3]  # Height
		pred_conf = torch.sigmoid(prediction[..., 4])  # Conf
		pred_cls = torch.sigmoid(prediction[..., 5:])

		grid_x = torch.arange(nG).repeat(nG, 1).view([1, 1, nG, nG]).type(FloatTensor)
		grid_y = torch.arange(nG).repeat(nG, 1).t().view([1, 1, nG, nG]).type(FloatTensor)
		scaled_anchors = FloatTensor([(a_w / stride, a_h / stride) for a_w, a_h in self.anchors])
		anchor_w = scaled_anchors[:, 0:1].view((1, nA, 1, 1))
		anchor_h = scaled_anchors[:, 1:2].view((1, nA, 1, 1))

		pred_boxes = FloatTensor(prediction[..., :4].shape)
		pred_boxes[..., 0] = x.data + grid_x
		pred_boxes[..., 1] = y.data + grid_y
		pred_boxes[..., 2] = torch.exp(w.data) * anchor_w
		pred_boxes[..., 3] = torch.exp(h.data) * anchor_h

		if targets is not None:

			if x.is_cuda:
				self.mse_loss = self.mse_loss.cuda()
				self.bce_loss = self.bce_loss.cuda()
				self.ce_loss = self.ce_loss.cuda()

			nGT, nCorrect, mask, conf_mask, tx, ty, tw, th, tconf, tcls = build_targets(
				pred_boxes=pred_boxes.cpu().data,
				pred_conf=pred_conf.cpu().data,
				pred_cls=pred_cls.cpu().data,
				target=targets.cpu().data,
				anchors=scaled_anchors.cpu().data,
				num_anchors=nA,
				num_classes=self.num_classes,
				grid_size=nG,
				ignore_thres=self.ignore_thres,
				img_dim=self.image_dim,
			)

			nProposals = int((pred_conf > 0.5).sum().item())
			recall = float(nCorrect / nGT) if nGT else 1
			precision = float(nCorrect / nProposals)

			# Handle masks
			mask = Variable(mask.type(ByteTensor))
			conf_mask = Variable(conf_mask.type(ByteTensor))

			# Handle target variables
			tx = Variable(tx.type(FloatTensor), requires_grad=False)
			ty = Variable(ty.type(FloatTensor), requires_grad=False)
			tw = Variable(tw.type(FloatTensor), requires_grad=False)
			th = Variable(th.type(FloatTensor), requires_grad=False)
			tconf = Variable(tconf.type(FloatTensor), requires_grad=False)
			tcls = Variable(tcls.type(LongTensor), requires_grad=False)

			# Get conf mask where gt and where there is no gt
			conf_mask_true = mask
			conf_mask_false = conf_mask - mask

			# Mask outputs to ignore non-existing objects
			loss_x = self.mse_loss(x[mask], tx[mask])
			loss_y = self.mse_loss(y[mask], ty[mask])
			loss_w = self.mse_loss(w[mask], tw[mask])
			loss_h = self.mse_loss(h[mask], th[mask])
			loss_conf = self.bce_loss(pred_conf[conf_mask_false], tconf[conf_mask_false]) + self.bce_loss(pred_conf[conf_mask_true], tconf[conf_mask_true])
			loss_cls = (1 / nB) * self.ce_loss(pred_cls[mask], torch.argmax(tcls[mask], 1))
			loss = loss_x + loss_y + loss_w + loss_h + loss_conf + loss_cls

			return (
				loss,
				loss_x.item(),
				loss_y.item(),
				loss_w.item(),
				loss_h.item(),
				loss_conf.item(),
				loss_cls.item(),
				recall,
				precision,
			)

		else:
			output = torch.cat((pred_boxes.view(nB, -1, 4) * stride, pred_conf.view(nB, -1, 1),pred_cls.view(nB, -1, self.num_classes),),-1)
			return output

class Yolov3(nn.Module):
	def __init__(self, cfgfile, img_size):
		super(Yolov3, self).__init__()
		self.blocks = read_cfg(cfgfile)
		self.net_info, self.module_list = create_modules(self.blocks)
		self.img_size = img_size
		self.loss_names = ['x', 'y', 'w', 'h', 'objectness', 'class_prob', 'recall', 'precision']

	def forward(self, x, targets = None):
		detections = []
		modules = self.blocks[1:]
		layer_outputs = {}   #We cache the outputs for the route layer
		self.losses = defaultdict(float)
		output = []

		write = 0
		for i in range(len(modules)):

			module_type = (modules[i]["type"])
			if module_type == "convolutional" or module_type == "upsample":

				x = self.module_list[i](x)

			elif module_type == "route":
				layers = modules[i]["layers"]
				layers = [int(a) for a in layers]

				if (layers[0]) > 0:
					layers[0] = layers[0] - i

				if len(layers) == 1:
					x = layer_outputs[i + (layers[0])]

				else:
					if (layers[1]) > 0:
						layers[1] = layers[1] - i

					map1 = layer_outputs[i + layers[0]]
					map2 = layer_outputs[i + layers[1]]

					x = torch.cat([map1, map2], 1)

			elif  module_type == "shortcut":
				from_ = int(modules[i]["from"])
				x = layer_outputs[i-1] + layer_outputs[i+from_]

			elif module_type == 'yolo':
				if targets is not None:
					x, *losses = self.module_list[i][0](x, targets)
					for name, loss in zip(self.loss_names, losses):
						self.losses[name] += loss

				else:
					x = module(x)
				output.append(x)
			layer_outputs[i] = x

		self.losses['recall'] /= 3
		self.losses['precision'] /= 3
		return sum(output) if not (targets == None) else torch.cat(output, 1)

cuda = torch.cuda.is_available()

os.makedirs('output', exist_ok = True)

model = Yolov3('yolov3-voc.cfg', 416)

model.apply(init_weights)

if cuda:
	model = model.cuda()

model.train


# TRIX (Trix)
# http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:trix

# Üçlü üstel olarak düzleştirilmiş hareketli ortalamanın değişim yüzdesini gösterir.

# Argümanlar:
#  close(pandas.Series): veri kümesi 'Kapat' sütunu.
#  window(int): n periyodu.
#  fillna(bool): True ise, nan değerlerini doldur.

import pandas as pd
from _utilities import IndicatorMixin, _ema

class TRIXIndicator(IndicatorMixin):
    def __init__(self, close: pd.Series, window: int = 15, fillna: bool = False):
        self._close = close
        self._window = window
        self._fillna = fillna
        self._run()

    def _run(self):
        ema1 = _ema(self._close, self._window, self._fillna)
        ema2 = _ema(ema1, self._window, self._fillna)
        ema3 = _ema(ema2, self._window, self._fillna)
        self._trix = (ema3 - ema3.shift(1, fill_value=ema3.mean())) / ema3.shift(1, fill_value=ema3.mean())
        self._trix *= 100

    def trix(self) -> pd.Series:
        trix_series = self._check_fillna(self._trix, value=0)
        return pd.Series(trix_series, name=f"trix_{self._window}")


'''Given a directory name dir and an integer n, finds the n largest contigs in dir and puts them in separate fasta files
for subsequent BLAST'''

import sys
import numpy as np

directory, n = sys.argv[1], int(sys.argv[2])

lengths = []

with open(directory+'/stats.txt', 'r') as f:
	f.readline() #read header
	for line in f: lengths.append( int(line.split()[1]) ) #store lengths

lengths = list(reversed(np.sort(lengths)))[:n]

print('finding contigs of lengths', lengths)

with open(directory+'/contigs.fa', 'r') as f:

	for line in f:
		for l in lengths:
			if 'length_'+str(l) in line:
				print('found length', l)
				with open(directory+'/contig'+str(lengths.index(l))+'.fa', 'w') as fout:
					finished = False
					L = 61
					while L == 61: #standard length of line in velvet .fa file. Last line is less than this.
						newline = f.readline()
						L = len(newline)
						#print(newline, L)
						if not 'NODE' in newline: fout.write(newline) #if by chance N%60 = 0, this stops us writing


import sys
from unittest.mock import Mock
from pathlib import Path

import pytest
import jupytext
import nbformat
import nbconvert

from ploomber import DAG, DAGConfigurator
from ploomber.tasks import NotebookRunner
from ploomber.products import File
from ploomber.exceptions import DAGBuildError
from ploomber.tasks import notebook
from ploomber.executors import Serial

def fake_from_notebook_node(self, nb, resources):
    return bytes(42), None

def test_error_when_path_has_no_extension():
    with pytest.raises(ValueError) as excinfo:
        notebook.NotebookConverter('a')

    msg = 'Could not determine output format for product: "a"'
    assert msg in str(excinfo.value)

@pytest.mark.parametrize(
    'path, exporter',
    [('file.ipynb', None), ('file.pdf', nbconvert.exporters.pdf.PDFExporter),
     ('file.html', nbconvert.exporters.html.HTMLExporter),
     ('file.md', nbconvert.exporters.markdown.MarkdownExporter)])
def test_notebook_converter_get_exporter_from_path(path, exporter):
    converter = notebook.NotebookConverter(path)
    assert converter.exporter == exporter

@pytest.mark.parametrize(
    'exporter_name, exporter',
    [('ipynb', None), ('pdf', nbconvert.exporters.pdf.PDFExporter),
     ('html', nbconvert.exporters.html.HTMLExporter),
     ('md', nbconvert.exporters.markdown.MarkdownExporter),
     ('markdown', nbconvert.exporters.markdown.MarkdownExporter),
     ('slides', nbconvert.exporters.slides.SlidesExporter)])
def test_notebook_converter_get_exporter_from_name(exporter_name, exporter):
    converter = notebook.NotebookConverter('file.ext', exporter_name)
    assert converter.exporter == exporter

@pytest.mark.parametrize('output', [
    'file.ipynb', 'file.pdf',
    pytest.param(
        'file.html',
        marks=pytest.mark.xfail(
            sys.platform == 'win32',
            reason='nbconvert has a bug when exporting to HTML on windows')),
    'file.md'
])
def test_notebook_conversion(monkeypatch, output, tmp_directory):
    # we mock the method that does the conversion to avoid having to
    # intstall tex for testing
    monkeypatch.setattr(nbconvert.exporters.pdf.PDFExporter,
                        'from_notebook_node', fake_from_notebook_node)

    nb = nbformat.v4.new_notebook()
    cell = nbformat.v4.new_code_cell('1 + 1')
    nb.cells.append(cell)

    with open(output, 'w') as f:
        nbformat.write(nb, f)

    conv = notebook.NotebookConverter(output)
    conv.convert()

@pytest.mark.parametrize(
    'name, out_dir',
    [
        ['sample.py', '.'],
        ['sample.R', '.'],
        ['sample.ipynb', '.'],
        # check still works even if the folder does not exit yet
        ['sample.ipynb', 'missing_folder']
    ])
def test_execute_sample_nb(name, out_dir, tmp_sample_tasks):
    dag = DAG()

    NotebookRunner(Path(name),
                   product=File(Path(out_dir, name + '.out.ipynb')),
                   dag=dag)
    dag.build()

@pytest.mark.xfail(
    sys.platform == 'win32',
    reason='nbconvert has a bug when exporting to HTML on windows')
def test_can_convert_to_html(tmp_sample_tasks):
    dag = DAG()

    NotebookRunner(Path('sample.ipynb'),
                   product=File(Path('out.html')),
                   dag=dag,
                   name='nb')
    dag.build()

def test_can_execute_with_parameters(tmp_directory):
    dag = DAG()

    code = """
# + tags=["parameters"]
1 + 1
    """

    NotebookRunner(code,
                   product=File(Path(tmp_directory, 'out.ipynb')),
                   dag=dag,
                   kernelspec_name='python3',
                   params={'var': 1},
                   ext_in='py',
                   name='nb')
    dag.build()

def test_can_execute_when_product_is_metaproduct(tmp_directory):
    dag = DAG()

    code = """
# + tags=["parameters"]
# something

# +
from pathlib import Path

Path(product['model']).touch()
    """

    product = {
        'nb': File(Path(tmp_directory, 'out.ipynb')),
        'model': File(Path(tmp_directory, 'model.pkl'))
    }

    NotebookRunner(code,
                   product=product,
                   dag=dag,
                   kernelspec_name='python3',
                   params={'var': 1},
                   ext_in='py',
                   nb_product_key='nb',
                   name='nb')
    dag.build()

def test_raises_error_if_key_does_not_exist_in_metaproduct(tmp_directory):
    dag = DAG()

    product = {
        'some_notebook': File(Path(tmp_directory, 'out.ipynb')),
        'model': File(Path(tmp_directory, 'model.pkl'))
    }

    code = """
# + tags=["parameters"]
# some code

# +
    """

    with pytest.raises(KeyError) as excinfo:
        NotebookRunner(code,
                       product=product,
                       dag=dag,
                       kernelspec_name='python3',
                       params={'var': 1},
                       ext_in='py',
                       nb_product_key='nb',
                       name='nb')

    assert "Key 'nb' does not exist in product" in str(excinfo.value)

def test_failing_notebook_saves_partial_result(tmp_directory):
    dag = DAG()

    code = """
# + tags=["parameters"]
# some code

raise Exception('failing notebook')
    """

    # attempting to generate an HTML report
    NotebookRunner(code,
                   product=File('out.html'),
                   dag=dag,
                   kernelspec_name='python3',
                   params={'var': 1},
                   ext_in='py',
                   name='nb')

    # build breaks due to the exception
    with pytest.raises(DAGBuildError):
        dag.build()

    # but the file with ipynb extension exists to help debugging
    assert Path('out.ipynb').exists()

def test_error_if_wrong_exporter_name(tmp_sample_tasks):
    dag = DAG()

    with pytest.raises(ValueError) as excinfo:
        NotebookRunner(Path('sample.ipynb'),
                       product=File(Path('out.ipynb')),
                       dag=dag,
                       nbconvert_exporter_name='wrong_name')

    assert ('Could not find nbconvert exporter with name "wrong_name"'
            in str(excinfo.value))

def test_error_if_cant_find_exporter_name(tmp_sample_tasks):
    dag = DAG()

    with pytest.raises(ValueError) as excinfo:
        NotebookRunner(Path('sample.ipynb'),
                       product=File(Path('out.wrong_ext')),
                       dag=dag,
                       nbconvert_exporter_name=None)

    assert 'Could not find nbconvert exporter' in str(excinfo.value)

def test_skip_kernel_install_check(tmp_directory):
    dag = DAG()

    code = """
# + tags=["parameters"]
1 + 1
    """

    NotebookRunner(code,
                   product=File(Path(tmp_directory, 'out.ipynb')),
                   dag=dag,
                   kernelspec_name='unknown_kernel',
                   ext_in='py',
                   name='nb',
                   check_if_kernel_installed=False)
    dag.render()

# TODO: we are not testing output, we have to make sure params are inserted
# correctly
@pytest.fixture
def tmp_dag(tmp_directory):
    dag = DAG()

    code = """
# + tags=["parameters"]
# some code

# +
1 + 1
    """
    p = Path('some_notebook.py')

    p.write_text(code)

    NotebookRunner(p,
                   product=File(Path(tmp_directory, 'out.ipynb')),
                   dag=dag,
                   kernelspec_name='python3',
                   params={'var': 1},
                   name='nb')

    dag.render()

    return dag

def test_develop_saves_changes(tmp_dag, monkeypatch):
    def mock_jupyter_notebook(args, check):
        nb = jupytext.reads('2 + 2', fmt='py')
        # args: "jupyter" {app} {path} {other args, ...}
        nbformat.write(nb, args[2])

    monkeypatch.setattr(notebook.subprocess, 'run', mock_jupyter_notebook)
    monkeypatch.setattr(notebook, '_save', lambda: True)

    tmp_dag['nb'].develop()
    path = str(tmp_dag['nb'].source.loc)

    assert Path(path).read_text().strip() == '2 + 2'

@pytest.mark.parametrize('app', ['notebook', 'lab'])
def test_develop_with_custom_args(app, tmp_dag, monkeypatch):
    mock = Mock()

    monkeypatch.setattr(notebook.subprocess, 'run', mock)
    monkeypatch.setattr(notebook, '_save', lambda: True)

    tmp_dag['nb'].develop(app=app, args='--port=8888 --no-browser')

    # make sure params are quoted to prevent code injection
    mock.assert_called_once_with([
        'jupyter', app, 'some_notebook-tmp.ipynb', '--port=8888',
        '--no-browser'
    ],
                                 check=True)

def test_develop_unknown_app(tmp_dag):
    with pytest.raises(ValueError) as excinfo:
        tmp_dag['nb'].develop(app='unknown')

    assert '"app" must be one of' in str(excinfo.value)

def test_develop_workflow_with_hot_reload(tmp_directory, monkeypatch):
    cfg = DAGConfigurator()
    cfg.params.hot_reload = True
    dag = cfg.create()

    code = """
# + tags=["parameters"]
# some code

# +
1 + 1
    """
    p = Path('some_notebook.py')

    p.write_text(code)

    t = NotebookRunner(p,
                       product=File(Path(tmp_directory, 'out.ipynb')),
                       dag=dag,
                       kernelspec_name='python3',
                       params={'var': 1},
                       name='nb')

    def mock_jupyter_notebook(args, check):
        nb = jupytext.reads('2 + 2', fmt='py')
        # args: "jupyter" {app} {path} {others, ...}
        nbformat.write(nb, args[2])

    dag.render()

    monkeypatch.setattr(notebook.subprocess, 'run', mock_jupyter_notebook)
    monkeypatch.setattr(notebook, '_save', lambda: True)

    t.develop()

    # source code must be updated
    assert '2 + 2' in str(t.source).strip()

    nb = nbformat.reads(t.source.nb_str_rendered,
                        as_version=nbformat.NO_CONVERT)
    source = jupytext.writes(nb, fmt='py')

    assert '2 + 2' in source

def test_develop_error_if_r_notebook(tmp_sample_tasks):
    dag = DAG()

    t = NotebookRunner(Path('sample.R'), product=File('out.ipynb'), dag=dag)

    dag.render()

    with pytest.raises(NotImplementedError):
        t.develop()

    with pytest.raises(NotImplementedError):
        t.debug()

# TODO: make a more general text and parametrize by all task types
# but we also have to test it at the source level
# also test at the DAG level, we have to make sure the property that
# code differ uses (raw code) it also hot_loaded
def test_hot_reload(tmp_directory):
    cfg = DAGConfigurator()
    cfg.params.hot_reload = True

    dag = cfg.create()

    path = Path('nb.py')
    path.write_text("""
# + tags=["parameters"]
# some code

# +
1 + 1
    """)

    t = NotebookRunner(path,
                       product=File('out.ipynb'),
                       dag=dag,
                       kernelspec_name='python3')

    t.render()

    path.write_text("""
# + tags=["parameters"]
# some code

# +
2 + 2
    """)

    t.render()

    assert '2 + 2' in str(t.source)
    assert t.product._outdated_code_dependency()
    assert not t.product._outdated_data_dependencies()

    assert '2 + 2' in t.source.nb_str_rendered

    report = dag.build()

    assert report['Ran?'] == [True]

    # TODO: check task is not marked as outdated

@pytest.mark.parametrize('kind, to_patch', [
    ['ipdb', 'IPython.terminal.debugger.Pdb.run'],
    ['pdb', 'pdb.run'],
    ['pm', None],
])
def test_debug(monkeypatch, kind, to_patch, tmp_dag):
    if to_patch:
        mock = Mock()
        monkeypatch.setattr(to_patch, mock)

    tmp_dag['nb'].debug(kind=kind)

    if to_patch:
        mock.assert_called_once()

@pytest.mark.xfail(sys.platform == "win32",
                   reason="Two warnings are displayed on windows")
def test_warns_if_export_args_but_ipynb_output(tmp_sample_tasks):
    dag = DAG(executor=Serial(build_in_subprocess=False))

    NotebookRunner(Path('sample.ipynb'),
                   File('out.ipynb'),
                   dag,
                   nbconvert_export_kwargs=dict(exclude_input=True))

    with pytest.warns(UserWarning) as record:
        dag.build()

    # NOTE: not sure why on windows two records are displayed, maybe another
    # library is throwing the warning
    assert len(record) == 1
    assert ("Output 'out.ipynb' is a notebook file"
            in record[0].message.args[0])


from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from com.liu.wine import X_train_std,X_test_std,y_train,y_test
from sklearn.linear_model import LogisticRegression
from com.liu.sklearntest import plot_decision_regions
import matplotlib.pyplot as plt

'''
线性判别分析压缩无监督数据
与PCA相比,LDA是一种更优越的用于分类的特征提取技术
是一种监督降维技术，在线性特征空间中尝试使得类别最大可分时，需要使用
    训练数据集中的类别信息
'''
lda = LinearDiscriminantAnalysis(n_components=2)
print(X_train_std)
X_train_lda = lda.fit_transform(X_train_std,y_train)
lr = LogisticRegression()
lr = lr.fit(X_train_lda,y_train)
plot_decision_regions(X_train_lda,y_train,classifier=lr)
plt.xlabel('LD 1')
plt.ylabel('LD 2')
plt.legend(loc='lower left')
plt.show()

#显示模型在测试数据集上的效果
X_test_lda = lda.transform(X_test_std)
plot_decision_regions(X_test_lda,y_test,classifier=lr)
plt.xlabel('LD 1')
plt.ylabel('LD 2')
plt.legend(loc='lower left')
plt.show()


def main():
    # Basy Logic la python w Condition.
    x = 5
    y = 12
    zero = x > y
    print(zero)

    yak= x==y
    print(yak)

    dw= x is y # (is) barawrdy shwen dakat.
    print(dw)

    se= x>y and x<y # la AND false w true anjamaky false
    print(se)

    chwar= x>y or x<y # har danayak true bet awysh dabeta true la OR
    print(chwar)

    b=1
    not (b)
    print(b)
    b=0
    not (b)
    print(b)
    False & True # Ama bitwise a & , la bitwise bit ba bit ta3amwly lagall akat.

if __name__ == '__main__':main()


class Solution:
  def topKFrequentWords(self,words,k):
    dict={}
    res= []
    for word in words:
      if word not in dict:
        dict[word]= 1
      else:
        dict[word]+=1
    sorted_d= sorted(dict.items(),key=lambda x:x[1],reverse=True)
    for i in range(k):
      res.append(sorted_d[i][0])
    return res
if __name__== "__main__":
  generator= ["yes","long","code",
             "yes","code","baby",
             "you","baby","chrome",
           "safari","long","code",
             "body","long","code"]
  k= 4
  solution= Solution()
  print("输入:",generator)
  print("输入:","k= ",k)
  print("输出:",solution.topKFrequentWords(generator,k))


import io
import warnings

import asdf
import asdf_wcs_schemas
import gwcs.extension

import pytest

@pytest.mark.skipif(asdf_wcs_schemas.__version__ < "0.2.0", reason="version 0.2 provides the new manifests")
def test_empty_extension():
    """
    Test that an empty extension was installed for gwcs 1.0.0
    and that extensions are installed for gwcs 1.0.1 and 1.1.0
    """
    extensions = gwcs.extension.get_extensions()
    assert len(extensions) > 1

    extensions_by_uri = {ext.extension_uri: ext for ext in extensions}

    # check for duplicate uris
    assert len(extensions_by_uri) == len(extensions)

    # check that all 3 versions are installed
    for version in ('1.0.0', '1.0.1', '1.1.0'):
        assert f"asdf://asdf-format.org/astronomy/gwcs/extensions/gwcs-{version}" in extensions_by_uri

    # the 1.0.0 extension should support no tags or types
    legacy = extensions_by_uri["asdf://asdf-format.org/astronomy/gwcs/extensions/gwcs-1.0.0"]
    assert len(legacy.tags) == 0
    assert len(legacy.converters) == 0

def test_open_legacy_without_warning():
    """
    Opening a file produced with extension 1.0.0 should not produce any
    warnings because of the empty extension registered for 1.0.0
    """
    asdf_bytes = b"""#ASDF 1.0.0
#ASDF_STANDARD 1.5.0
%YAML 1.1
%TAG ! tag:stsci.edu:asdf/
--- !core/asdf-1.1.0
asdf_library: !core/software-1.0.0 {author: The ASDF Developers, homepage: 'http://github.com/asdf-format/asdf',
  name: asdf, version: 2.9.2}
history:
  extensions:
  - !core/extension_metadata-1.0.0
    extension_class: asdf.extension._manifest.ManifestExtension
    extension_uri: asdf://asdf-format.org/astronomy/gwcs/extensions/gwcs-1.0.0
    software: !core/software-1.0.0 {name: gwcs, version: 0.18.0}
foo: 1
..."""
    with warnings.catch_warnings():
        warnings.simplefilter("error")
        with asdf.open(io.BytesIO(asdf_bytes)) as af:
            assert af['foo'] == 1


# Estudiante: Aaron Joel Limachi Quispe
# CI: 12732282
# Problema de Balanar

import numpy as np
import matplotlib.pyplot as plt
import sympy as sp

a, v, r = sp.symbols('a,v,r', real=True)
A, B, t, vo = sp.symbols('A,B,t,vo', positive=True)
# print(A, B, t, v, vo)

# Funcion original
a = A - B*v
print('Aceleracion')
print(a)
# Integrando para obtener la velocidad
i = 1 / (A - B*v)
i1 = sp.integrate(i, (v, vo, v))
# Obteninedo solucion de la ecuacion
v = sp.solve(i1 - t, v)
print('Velocidad')
print(v)

r = sp.integrate(v[0], (t, 0, t))
# print(r)

# Simplificando
r = r.collect((A - B*vo))
print('Desplazamiento')
print(r)

# Establecemos los valores para reemplazar en la ecuacion
val = {A: 10, B: 0.5, vo: 50}
# Creamos el espacio del grafico
t1 = np.linspace(0, 50, 50)

# Reemplazando valores Velocidad vs tiempo
v1 = v[0]
v2 = v1.subs(val)
V = sp.lambdify(t, v2, 'numpy')

# Reemplazando valores distancia vs tiempo
r = r.subs(val)
R = sp.lambdify(t, r, 'numpy')

# Reemplazando valores Aceleracion vs tiempo
Ac = A-B*v[0]
Ac = Ac.subs(val)
Ac = sp.lambdify(t, Ac, 'numpy')

val_4 = {A: 10, B: 0.5, vo: 50, t: 20}
v1_4 = v[0]
v2_4 = v1.subs(val_4)
print('La velocidad en t=20s es igual a: ', v2_4)

# Graficando
fig, axs = plt.subplots(3, figsize=(6, 6))
# fig.suptitle('Graficas para el movimiento de Balanar')
axs[0].plot(t1, Ac(t1))
axs[0].set_title('Aceleracion vs tiempo')
axs[0].set_xlabel('Tiempo(t)')
axs[0].set_ylabel('Aceleracion(a)')
axs[1].plot(V(t1), t1)
axs[1].set_title('Velocidad vs tiempo')
axs[1].set_xlabel('Tiempo(t)')
axs[1].set_ylabel('Velocidad(V)')
axs[2].plot(R(t1), t1)
axs[2].set_xlabel('Tiempo(t)')
axs[2].set_ylabel('Posicion(r)')
axs[2].set_title('Posicion vs tiempo')
plt.subplots_adjust(left=0.1, right=0.95, bottom=0.1, top=0.95, wspace=0.1, hspace=0.6)
plt.show()

# Estudiante: Aaron Joel Limachi Quispe
# CI: 12732282


#!/usr/bin/python3

def magic_calculation(a, b):
    """Python function mimic given bytecode behavior.

    Args:
        a (int): The first input value.
        b (int): The second input value.

    Returns:
        Result of calculation.
    """
    result = 0
    for i in range(1, 3):
        try:
            if i > a:
                raise Exception('Too far')
            else:
                result += a ** b / i
        except:
            result = b + a

    return result


#make dictionary for NELL relation
import sys

x = open(sys.argv[1],"w")

with open('../TBOXesNELL/ListofRelations.txt') as a:
	seenRel=set()
	#i dimulai dari $numLineInstance
	i=int(sys.argv[2])
	for line in a:
		row=line.split()
		if(row[0] not in seenRel):
			seenRel.add(row[0])
			x.write(row[0]+"\t"+str(i)+"\n")
			i += 1

x.close()


import torch
import torch.nn as nn
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
from torch.utils.data import DataLoader

import argparse
import rdkit
import math, random, sys, os
import numpy as np
from tqdm import tqdm

from fuseprop import *
from properties import get_scoring_function

qed_sa_func = lambda x: x[0] >= 0.5 and x[1] >= 0.5 and x[2] >= 0.6 and x[3] <= 4.0
normal_func = lambda x: min(x) >= 0.5

def remove_order(s):
    for x in range(15):
        s = s.replace(":%d]" % (x,), ":1]")
    return s

# Decode molecules
def decode_rationales(model, rationale_dataset):
    loader = DataLoader(rationale_dataset, batch_size=1, shuffle=False, num_workers=0, collate_fn=lambda x:x[0])
    model.eval()
    cand_mols = []
    with torch.no_grad():
        for init_smiles in tqdm(loader):
            final_smiles = model.decode(init_smiles)
            mols = [(x,y) for x,y in zip(init_smiles, final_smiles) if y and '.' not in y]
            mols = [(x,y) for x,y in mols if Chem.MolFromSmiles(y).HasSubstructMatch(Chem.MolFromSmiles(x))]
            cand_mols.extend(mols)
    return cand_mols

# Predict properties and filter
def property_filter(cand_mols, scoring_function, args):
    rationales, smiles_list = zip(*cand_mols)
    cand_props = scoring_function(smiles_list)
    new_data = []
    rationale_dist = {remove_order(r) : [0,0] for r in rationales}
    with open(args.save_dir + '/valid.' + str(epoch), 'w') as f:
        for (init_smiles, final_smiles), prop in zip(cand_mols, cand_props):
            rationale = remove_order(init_smiles)
            rationale_dist[rationale][1] += 1
            print(init_smiles, final_smiles, prop, file=f)
            if args.compare_func(prop):
                rationale_dist[rationale][0] += 1
                new_data.append( (init_smiles, final_smiles) )

    print('property filter: %d -> %d' % (len(cand_mols), len(new_data)))
    rationale_dist = {r : x / n for r,(x,n) in rationale_dist.items() if x / n >= args.alpha}
    return new_data, rationale_dist

if __name__ == "__main__":
    lg = rdkit.RDLogger.logger()
    lg.setLevel(rdkit.RDLogger.CRITICAL)

    parser = argparse.ArgumentParser()
    parser.add_argument('--rationale', required=True)
    parser.add_argument('--prop', required=True)
    parser.add_argument('--save_dir', required=True)
    parser.add_argument('--init_model', type=str)
    parser.add_argument('--load_epoch', type=int, default=-1)
    parser.add_argument('--atom_vocab', default=common_atom_vocab)

    parser.add_argument('--rnn_type', type=str, default='LSTM')
    parser.add_argument('--hidden_size', type=int, default=400)
    parser.add_argument('--embed_size', type=int, default=400)
    parser.add_argument('--batch_size', type=int, default=10)
    parser.add_argument('--decode_batch_size', type=int, default=20)
    parser.add_argument('--latent_size', type=int, default=20)
    parser.add_argument('--depth', type=int, default=10)
    parser.add_argument('--diter', type=int, default=3)

    parser.add_argument('--lr', type=float, default=5e-4)
    parser.add_argument('--clip_norm', type=float, default=20.0)
    parser.add_argument('--alpha', type=float, default=1.0)
    parser.add_argument('--beta', type=float, default=0.3)
    parser.add_argument('--num_decode', type=int, default=200)

    parser.add_argument('--epoch', type=int, default=1)
    parser.add_argument('--anneal_rate', type=float, default=1.0)
    parser.add_argument('--print_iter', type=int, default=50)

    args = parser.parse_args()
    print(args)

    if args.prop == "gsk3,jnk3,qed,sa" or args.prop == "jnk3,gsk3,qed,sa":
        args.compare_func = qed_sa_func
    else:
        args.compare_func = normal_func

    prop_funcs = [get_scoring_function(prop) for prop in args.prop.split(',')]
    scoring_function = lambda x : list( zip(*[func(x) for func in prop_funcs]) )

    with open(args.rationale) as f:
        rationales = [line.split()[1] for line in f]
        rationales = unique_rationales(rationales)
        rationale_dataset = SubgraphDataset(rationales, args.atom_vocab, args.decode_batch_size, args.num_decode)

    model = AtomVGNN(args).cuda()
    if args.load_epoch >= 0:
        path = os.path.join(args.save_dir, f"model.{args.load_epoch}")
        model.load_state_dict(torch.load(path)[1])
    else:
        model.load_state_dict(torch.load(args.init_model))

    print("Model #Params: %dK" % (sum([x.nelement() for x in model.parameters()]) / 1000,))

    optimizer = optim.Adam(model.parameters(), lr=args.lr)
    scheduler = lr_scheduler.ExponentialLR(optimizer, args.anneal_rate)

    param_norm = lambda m: math.sqrt(sum([p.norm().item() ** 2 for p in m.parameters()]))
    grad_norm = lambda m: math.sqrt(sum([p.grad.norm().item() ** 2 for p in m.parameters() if p.grad is not None]))

    for epoch in range(args.load_epoch + 1, args.epoch):
        print('epoch', epoch)
        cand_mols = decode_rationales(model, rationale_dataset)
        cand_mols, rationale_dist = property_filter(cand_mols, scoring_function, args)

        model_ckpt = (rationale_dist, model.state_dict())
        torch.save(model_ckpt, os.path.join(args.save_dir, f"model.{epoch}"))

        cand_mols = list(set(cand_mols))
        random.shuffle(cand_mols)

        # Update model
        dataset = MoleculeDataset(cand_mols, args.atom_vocab, args.batch_size)
        dataloader = DataLoader(dataset, batch_size=1, shuffle=True, num_workers=0, collate_fn=lambda x:x[0])
        model.train()

        meters = np.zeros(5)
        for total_step, batch in enumerate(dataset):
            if batch is None: continue

            model.zero_grad()
            loss, kl_div, wacc, tacc, sacc = model(*batch, beta=args.beta)
            loss.backward()
            nn.utils.clip_grad_norm_(model.parameters(), args.clip_norm)
            optimizer.step()

            meters = meters + np.array([kl_div, loss.item(), wacc * 100, tacc * 100, sacc * 100])

            if (total_step + 1) % args.print_iter == 0:
                meters /= args.print_iter
                print("[%d] Beta: %.3f, KL: %.2f, loss: %.3f, Word: %.2f, Topo: %.2f, Assm: %.2f, PNorm: %.2f, GNorm: %.2f" % (total_step + 1, args.beta, meters[0], meters[1], meters[2], meters[3], meters[4], param_norm(model), grad_norm(model)))
                sys.stdout.flush()
                meters *= 0

        scheduler.step()
        print("learning rate: %.6f" % scheduler.get_lr()[0])


'''
Setup script for bdpy

This file is a part of BdPy.
'''

from setuptools import setup

VERSION = '0.18'

if __name__ == '__main__':

    # Long description
    with open('./README.md') as f:
        long_description = f.read()

    # Setup
    setup(name='bdpy',
          version=VERSION,
          description='Brain decoder toolbox for Python',
          long_description=long_description,
          long_description_content_type='text/markdown',
          author='Shuntaro C. Aoki',
          author_email='brainliner-admin@atr.jp',
          maintainer='Shuntaro C. Aoki',
          maintainer_email='brainliner-admin@atr.jp',
          url='https://github.com/KamitaniLab/bdpy',
          license='MIT',
          keywords='neuroscience, neuroimaging, brain decoding, fmri, machine learning',
          packages=['bdpy',
                    'bdpy.bdata',
                    'bdpy.dataform',
                    'bdpy.distcomp',
                    'bdpy.dl',
                    'bdpy.dl.torch',
                    'bdpy.evals',
                    'bdpy.feature',
                    'bdpy.fig',
                    'bdpy.ml',
                    'bdpy.mri',
                    'bdpy.opendata',
                    'bdpy.preproc',
                    'bdpy.recon',
                    'bdpy.recon.torch',
                    'bdpy.stats',
                    'bdpy.util'],
          install_requires=[
              'numpy',
              'scipy',
              'scikit-learn',
              'h5py',
              'hdf5storage',
              'pyyaml'
          ])


from __future__ import annotations

import json
import logging

from globals import api
from globals import con

def main():
    logger = logging.getLogger(__name__)
#    match = api.get_match(7207632145)
#    json.dump(match, open("7207632145.json", "w", encoding="utf-8"))
    player_matches = api.get_player_matches(293437752, take=50)
    json.dump(
        player_matches, open(
            'player_293437752.json', 'w', encoding='utf-8',
        ),
    )

    cur = con.cursor()
    cur.execute("""
                CREATE TABLE IF NOT EXISTS
                matches(id INT NOT NULL PRIMARY KEY, body TEXT)
                """)
    cur.execute("""
                CREATE TABLE IF NOT EXISTS
                match (id INT NOT NULL PRIMARY KEY, body TEXT)
                """)
    insert_matches = 'insert into matches(id, body) values(?, ?)'
    insert_match = 'insert into match(id, body) values(?, ?)'
    for match in player_matches:
        match_detail = ''
        match_detail = api.get_match(match['id'])
        try:
            con.execute(insert_match, (match['id'], json.dumps(match_detail)))
            con.execute(insert_matches, (match['id'], json.dumps(match)))
        except Exception as e:
            logger.error(f"Match ID: {match['id']}")
            logger.error(f"Sql error: {''.join(e.args)}")
            logger.error(f'Exception class: {e.__class__}')
            continue
    con.commit()

if __name__ == '__main__':
    main()