u'\n:\u521b\u5efa\u65f6\u95f4: 2020/5/18 23:57\n:\u4f5c\u8005: \u82cd\u4e4b\u5e7b\u7075\n:\u6211\u7684\u4e3b\u9875: https://cpcgskill.com\n:QQ: 2921251087\n:\u7231\u53d1\u7535: https://afdian.net/@Phantom_of_the_Cang\n:aboutcg: https://www.aboutcg.org/teacher/54335\n:bilibili: https://space.bilibili.com/351598127\n'
import maya.cmds as cmds
from ..api import OpenMayaUI
try:
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
except ImportError:
    try:
        from PySide2.QtGui import *
        from PySide2.QtCore import *
        from PySide2.QtWidgets import *
    except ImportError:
        from PySide.QtGui import *
        from PySide.QtCore import *
try:
    from shiboken2 import *
except ImportError:
    from shiboken import *
try:
    mui = wrapInstance(long(OpenMayaUI.MQtUtil.mainWindow()), QWidget)
except:
    mui = None
deleteWidget = delete

class CPwindow(QWidget, ):

    @staticmethod
    def newMui():
        win = cmds.window()
        return wrapInstance(long(OpenMayaUI.MQtUtil.findWindow(win)), QWidget)

    def __init__(self):
        self.maya_window = self.newMui()
        self._main_layout = QVBoxLayout(self.maya_window)
        self._main_layout.setContentsMargins(0, 0, 0, 0)
        super(CPwindow, self).__init__()
        self._main_layout.addWidget(self)
        self.main_layout = QVBoxLayout(self)

    def show(self):
        super(CPwindow, self).show()
        self.maya_window.show()

    def close(self):
        super(CPwindow, self).close()
        self.maya_window.close()

    def setWindowTitle(self, p_str):
        self.maya_window.setWindowTitle(p_str)

    def windowTitle(self):
        return self.maya_window.windowTitle()

    def setWindowIcon(self, icon):
        return self.maya_window.setWindowIcon(icon)

    def windowIcon(self):
        return self.maya_window.windowIcon()

    def resize(self, size):
        self.maya_window.resize(size)

    def size(self):
        return self.maya_window.size()

    def paintEvent(self, event):
        super(CPwindow, self).resize(self.maya_window.size())

    def deleteWindow(self):
        super(CPwindow, self).close()
        delete(self.maya_window)

class CPQWindow(QWidget, ):
    u'\n    \u7531CPMel\u91cd\u5efa\u7684QWidget\u5bf9\u8c61\u53ef\u4ee5\u81ea\u52a8\u7684\u5c06\u81ea\u8eab\u8bbe\u7f6e\u4e3aMaya\u4e3b\u7a97\u53e3\u7684\u5b50\u5bf9\u8c61\n\n    '

    def __init__(self, *args):
        super(CPQWindow, self).__init__(*args)
        self.setParent(mui)
        self.setWindowFlags(Qt.Window)
        self.main_layout = QVBoxLayout(self)

    def deleteWindow(self):
        super(CPQWindow, self).close()
        delete(self)

class CPQWidget(QWidget, ):
    u'\n    \u5bf9CPQWindow\u7684\u91cd\u5199\n    \u4e0d\u518d\u6784\u5efaself.main_layout\n    '

    def __init__(self, *args):
        super(CPQWidget, self).__init__(*args)
        self.setParent(mui)
        self.setWindowFlags(Qt.Window)

    def deleteWindow(self):
        super(CPQWidget, self).close()
        delete(self)

class FrameBlock(QWidget, ):
    u'\n    \u53ef\u6298\u53e0\u6846\u67b6\u5b9e\u73b0\n\n    '

    class Bn(QPushButton, ):

        def __init__(self, *args):
            super(FrameBlock.Bn, self).__init__()
            self.__is_turn_on = False

            def _(*args):
                self.__is_turn_on = (not self.__is_turn_on)
            self.clicked.connect(_)
            self.setStyleSheet('text-align: left;')
            self.setMaximumHeight(20)

        def setIsTurnOn(self, bool):
            self.__is_turn_on = bool

        def isTurnOn(self):
            return self.__is_turn_on

    class Block(QWidget, ):

        def __init__(self, widget=QWidget):
            super(FrameBlock.Block, self).__init__()
            self.main_layout = QVBoxLayout(self)
            self.main_layout.addWidget(widget)

        def paintEvent(self, event):
            super(FrameBlock.Block, self).paintEvent(event)
            p = QPainter(self)
            p.setPen(Qt.NoPen)
            p.setBrush(QBrush(QColor(0, 0, 0, 15)))
            p.drawRoundRect(self.rect(), 5, 5)
            p.end()

    def __init__(self, name, widget, status=False):
        u'\n\n\n        :param name: \u6807\u9898\n        :param widget: QWidget \u5bf9\u8c61\n        :param status: \u9ed8\u8ba4\u72b6\u6001\n        '
        super(FrameBlock, self).__init__()
        self.main_layout = QVBoxLayout(self)
        self.main_layout.setContentsMargins(0, 0, 0, 0)
        self.__bn = FrameBlock.Bn()
        self.__bn.setIsTurnOn(status)
        self.name = name
        self.__bn.clicked.connect(self.upwidget)
        self.main_layout.addWidget(self.__bn)
        self.widget = FrameBlock.Block(widget)
        self.main_layout.addWidget(self.widget)
        self.upwidget()

    def unfold(self):
        self.__bn.setIsTurnOn(True)
        self.upwidget()

    def fold(self):
        self.__bn.setIsTurnOn(False)
        self.upwidget()

    def upwidget(self, *args):
        if self.__bn.isTurnOn():
            self.__bn.setText(self.name)
            self.widget.show()
        else:
            self.__bn.setText((self.name + u'...'))
            self.widget.close()

def getFileName():
    u'\n    \u83b7\u5f97\u6587\u4ef6\u8def\u5f84\n\n    :return: unicode is None\n    '
    path = QFileDialog.getOpenFileName()
    if (len(path) < 3):
        return None
    return path

def getDirectoryName():
    u'\n    \u83b7\u5f97\u6587\u4ef6\u5939\u8def\u5f84\n\n    :return: unicode is None\n    '
    path = QFileDialog.getExistingDirectory()
    if (len(path) < 3):
        return None
    return path

def input(title=u'\u8f93\u5165', message=u'>>>', text=u''):
    u'\n    \u521b\u5efa\u4e00\u4e2a\u63a5\u53d7\u5b57\u7b26\u4e32\u8f93\u5165\u7684\u8f93\u5165\u6846\n\n    :param title: \u6807\u9898\n    :param message: \u6d88\u606f\n    :param text: \u9ed8\u8ba4\u53c2\u6570\n    :return: unicode is None\n    '
    (v, is_ok) = QInputDialog.getText(mui, title, message, text)
    if is_ok:
        return v
    return None

def inputInt(title=u'\u8f93\u5165', message=u'>>>', text=0):
    u'\n    \u521b\u5efa\u4e00\u4e2a\u63a5\u53d7\u6574\u6570\u8f93\u5165\u7684\u8f93\u5165\u6846\n\n    :param title: \u6807\u9898\n    :param message: \u6d88\u606f\n    :param text: \u9ed8\u8ba4\u53c2\u6570\n    :return: unicode is None\n    '
    (v, is_ok) = QInputDialog.getInt(mui, title, message, text)
    if is_ok:
        return v
    return None

def inputDouble(title=u'\u8f93\u5165', message=u'>>>', text=0.0):
    u'\n    \u521b\u5efa\u4e00\u4e2a\u63a5\u53d7\u6d6e\u70b9\u8f93\u5165\u7684\u8f93\u5165\u6846\n\n    :param title: \u6807\u9898\n    :param message: \u6d88\u606f\n    :param text: \u9ed8\u8ba4\u53c2\u6570\n    :return: unicode is None\n    '
    (v, is_ok) = QInputDialog.getDouble(mui, title, message, text)
    if is_ok:
        return v
    return None

def inputFloat(title=u'\u8f93\u5165', message=u'>>>', text=0.0):
    u'\n    \u521b\u5efa\u4e00\u4e2a\u63a5\u53d7\u6d6e\u70b9\u8f93\u5165\u7684\u8f93\u5165\u6846\n\n    :param title: \u6807\u9898\n    :param message: \u6d88\u606f\n    :param text: \u9ed8\u8ba4\u53c2\u6570\n    :return: unicode is None\n    '
    return inputDouble(title, message, text)

def inputMultiLineText(title=u'\u8f93\u5165', message=u'>>>', text=u''):
    u'\n    \u521b\u5efa\u4e00\u4e2a\u63a5\u53d7\u5b57\u7b26\u4e32\u8f93\u5165\u7684\u591a\u884c\u8f93\u5165\u6846\n\n    :param title: \u6807\u9898\n    :param message: \u6d88\u606f\n    :param text: \u9ed8\u8ba4\u53c2\u6570\n    :return: unicode is None\n    '
    (v, is_ok) = QInputDialog.getMultiLineText(mui, title, message, text)
    if is_ok:
        return v
    return None

def confirm(title=u'\u786e\u8ba4', message=u''):
    u'\n    \u786e\u8ba4\u5bf9\u8bdd\u6846\n\n    :param title: \u6807\u9898\n    :param message: \u6d88\u606f\n    :return: bool\n    '
    reply = QMessageBox.question(mui, title, message, (QMessageBox.Yes | QMessageBox.No), QMessageBox.No)
    if (reply == QMessageBox.StandardButton.No):
        return False
    else:
        return True


# 用CCSCC那种记号来照合正解文
import re, os, csv
import pipei as pi
from pykakasi import kakasi

# 打标签的思路是：因为CCSS的符号是跟正解文对应的，跟识别结果不对应，这个是最大的难点，所以要做的是先去找标志文件里面的C,
# 也就是正确认识的部分，然后返回这些C的索引，然后根据索引去正解文中找被正确识别的单词，找到之后把单词顺序插入到一个list中去，
# 在用识别结果里的单词去对应这个list,按顺序一个单词一个单词地对，每成功对应上一个单词就从list中把这个单词删除掉，并获取这个单词
# 的帧数表，然后打上标签0，没有对应上的单词就在这个单词的范围内打上标签1

BASE_DIRS = r'C:\Users\a7825\Desktop\工作空间\语音数据\UUDB\第一次实验\打标签\第一批'
# 主要文件的路径

name = 'symbol.txt'
# 表记着CCCCSSSS标志的文件

name_tezheng = 'log'
# 装有特征值的那个文件的文件名

houzhui = '.wav.csv'
# 特征值文件中除去id号之后的后缀部分

tezhengzhi = 'log'
# 特征值文件的名字

xinde = 'xinde'
# 装入新的特征值的文件名

symbolcidian = {}
# 这样的词典,标志词典
# id: C001L_086
# ['S', 'S', 'S', 'C', 'S', 'D', 'D', 'D', 'C']
# id: C001L_087
# ['S', 'D', 'D', 'C']
# id: C001L_088
# ['S', 'S', 'S', 'S', 'D', 'D', 'D', 'D', 'C', 'C']
zhengjie = {}
# 正解文词典

shibie = {}

# 识别结果词典

# 这是一个字典，字典里面的每一个元素都是一个list

# if 'id' in i[0]:
#     print(i[0])
# if 'EVAL:' in i[0]:
#     print(i[0])

def csv_to_list(csv_path):  # 把数据读入并转换为列表
    a = csv.reader(open(csv_path, 'r', encoding='utf-8'))
    return [i for i in a]

def zidian(dir):  # 建立三个字典

    txtwenjian = csv.reader(open(os.path.join(BASE_DIRS, dir, name), 'r', encoding='cp932',
                                 errors='ignore'))  # 把symbol标志文件读进来,编码有问题的话就用encoding='utf-8'

    banyun = ''

    b = [i for i in txtwenjian]  # 转化为list

    for q in b:
        if q != []:
            if 'id' in q[0]:
                banyun = q[0]  # 标志文件里面有文件名
                # print(q[0])  # 这里的id就是文件名

            if 'EVAL' in q[0]:
                s = q[0].split()
                s.pop(0)  # 把列表的第一个元素pop掉

                while 'I' in s:
                    s.remove('I')  # 一次性只会删除一个I,所以要用while,很多时候是因为I，符号的个数比正解文的单词数还要多
                # print(len(s))
                # os.system("pause")
                symbolcidian[banyun.replace('id: ', '')] = s  # 把符号放入字典

            if 'REF' in q[0]:
                s = q[0].split()
                s.pop(0)
                # print(len(s))
                # print(s)
                zhengjie[banyun.replace('id: ', '')] = s  # 正解文

            if 'HYP' in q[0]:
                s = q[0].split()
                s.pop(0)
                # print(s)
                shibie[banyun.replace('id: ', '')] = s  # 识别结果

        else:
            pass
    # print("正解")
    # print(zhengjie)
    # print(symbolcidian)
    # print("识别结果")
    # print(shibie)

    return zhengjie, symbolcidian

if __name__ == '__main__':

    kakasi = kakasi()
    kakasi.setMode("H", "a")  # Hiragana to ascii, default: no conversion
    kakasi.setMode("K", "a")  # Katakana to ascii, default: no conversion
    kakasi.setMode("J", "a")  # Japanese to ascii, default: no conversion
    kakasi.setMode("r", "Hepburn")  # default: use Hepburn Roman table
    kakasi.setMode("s", True)  # add space, default: no separator

    for per_dirs in os.listdir(BASE_DIRS):  # per_dirs = C001L,C001R...

        zhengjie, symbolcidian = zidian(per_dirs)
        # 从标志文件中把标志塞进symbolcidian字典里

        for id in os.listdir(os.path.join(BASE_DIRS, per_dirs, name_tezheng)):  # id = C001L,C001R下面的文件的名字

            banyun_1 = []  # 存储C的索引
            banyun_2 = []  # 存储正确的单词

            banyun_3 = []  # 存储非C的索引
            banyun_4 = []  # 存储暂时不正确的单词
            dianout = []

            id = id.replace(houzhui, '')  # 把文件名中的.wav.csv去掉只剩id

            # print(id)
            # print(symbolcidian[id])

            enumerate(symbolcidian[id])

            banyun_1 = [i for i, x in enumerate(symbolcidian[id]) if x == 'C']  # 返回标志C的索引
            banyun_3 = [i for i, x in enumerate(symbolcidian[id]) if x != 'C']

            t_file = os.path.join(BASE_DIRS, per_dirs, tezhengzhi, id + houzhui)
            t_file_list = csv_to_list(t_file)  # 把特征值文件转化为list

            if len(banyun_1) == 0:  # 如果没有一个是正确的，全错，所有的数据都打标签1
                for i in range(len(t_file_list)):
                    t_file_list[i].insert(0, '1')

            else:
                for u in banyun_1:  # banyun_1里面装的全是标志C的索引

                    if u + 1 <= len(zhengjie[id]):  # 正解文单词的个数可能没有标志的个数多
                        # print(banyun_1)
                        # print(zhengjie[id][u])
                        # print(zhengjie[id])
                        # print("已经把正确单词 %s 加入数组"%str(zhengjie[id][u]))
                        banyun_2.append(zhengjie[id][u])
                    else:  # 如果C标志的索引号大于正解文单词的索引号，那就只能手动去调整了
                        print("手动调一下这个文件吧%s" % id)
                        print("它的正确单词是")
                        print(banyun_2)
                        os.system("pause")

                    # print(banyun_2)
                    # os.system("pause")

                # for p in symbolcidian[id]:
                #     os.system("pause")
                #     # while p == 'C':
                #     print(p.index('C'))

                dir_out = os.path.join(BASE_DIRS, per_dirs, 'keka', id + '.out')
                dianout = pi.read_out(dir_out)  # 提取出来的帧号跟julius识别结果一样
                # print(dianout)
                # os.system('pause')

                start = dianout.pop(0)[1][1]

                # print(start)

                for i in range(start + 1):
                    t_file_list[i].insert(0, '0')  # 最前面的无音区间全部都打标签0,把它们当做正确认识来处理

                for y in dianout:  # dianout是识别结果跟对应的帧数表

                    # print("此时的单词是%s"%y)
                    # print("此时的匹配结果是")
                    # print(dianout)
                    # os.system("pause")

                    if y[1][1] + 1 <= len(t_file_list):  # 判断这个单词的范围是否超出了特征值得总行数

                        if y[0] == '':
                            continue
                        if y[0] in banyun_2:  # 如果这个单词存在列表banyun_2中，就给这个单词对应的帧数范围打标签0
                            start, end = y[1]
                            print("正在为文件 %s 的单词 %s 打标签" % (os.path.split(dir_out)[1], y[0]))
                            for i in range(start, end + 1):
                                t_file_list[i].insert(0, '0')
                            banyun_2.remove(y[0])  # 打完标签0之后再从列表中把这个单词删

                        else:
                            start, end = y[1]  # 如果这个单词不在列表banyun_2中，就给这个单词对应的帧数范围打标签1
                            print("正在为文件 %s 的单词 %s 打标签" % (os.path.split(dir_out)[1], y[0]))
                            for i in range(start, end + 1):
                                t_file_list[i].insert(0, '1')

                    elif y[1][1] + 1 > len(t_file_list):

                        if y[0] == '':
                            continue
                        if y[0] in banyun_2:
                            start = y[1][0]
                            end = len(t_file_list)  # 如果这个单词的帧数表的范围超出了特征值得行数，就以特征值行数作为end
                            print("正在为文件 %s 的单词 %s 打标签" % (os.path.split(dir_out)[1], y[0]))
                            for i in range(start, end):
                                t_file_list[i].insert(0, '0')
                            banyun_2.remove(y[0])
                        else:
                            start = y[1][0]
                            end = len(t_file_list)
                            print("正在为文件 %s 的单词 %s 打标签" % (os.path.split(dir_out)[1], y[0]))
                            for i in range(start, end):
                                t_file_list[i].insert(0, '1')

            with open(os.path.join(BASE_DIRS, per_dirs, xinde, id + '.csv'), 'w+', encoding='utf-8') as mergen_file:
                for i in t_file_list:
                    mergen_file.write('%s\n' % ','.join(i))

            # os.system("pause")

            # 请参看pipei.py


import unittest

import numpy as np
import torch
from onnx import TensorProto, helper
from torch import nn

np.random.seed(0)
torch.manual_seed(0)
torch.set_printoptions(threshold=10000)

def create_neox_attention_graph(
    batch_size,
    seq_len,
    hidden_size,
    qkv_weight,
    qkv_bias,
    num_heads,
):
    nodes = [
        helper.make_node(
            "Attention",
            [
                "input",
                "weight",
                "bias",
            ],
            ["output"],
            "NeoXAttention_0",
            num_heads=num_heads,
            unidirectional=1,
            do_rotary=1,
            domain="com.microsoft",
        ),
    ]

    initializers = [
        helper.make_tensor("weight", TensorProto.FLOAT, [hidden_size, 3 * hidden_size], qkv_weight.flatten().tolist()),
        helper.make_tensor("bias", TensorProto.FLOAT, [3 * hidden_size], qkv_bias.flatten().tolist()),
    ]

    graph = helper.make_graph(
        nodes,
        "NeoXAttention_Graph",
        [
            helper.make_tensor_value_info("input", TensorProto.FLOAT, [batch_size, seq_len, hidden_size]),
        ],
        [
            helper.make_tensor_value_info("output", TensorProto.FLOAT, [batch_size, seq_len, hidden_size]),
        ],
        initializers,
    )

    model = helper.make_model(graph)
    return model.SerializeToString()

def create_neox_decoder_masked_self_attention_graph(
    batch_size,
    seq_len,
    past_seq_len,
    hidden_size,
    qkv_weight,
    qkv_bias,
    num_heads,
):
    nodes = [
        helper.make_node(
            "DecoderMaskedSelfAttention",
            [
                "input",
                "weight",
                "bias",
                "mask_index",
                "past",
                "",  # relative_position_bias
                "past_sequence_length",
            ],
            ["output", "present"],
            "NeoXDecoderMaskedSelfAttention_0",
            num_heads=num_heads,
            past_present_share_buffer=1,
            do_rotary=1,
            domain="com.microsoft",
        ),
    ]

    initializers = [
        helper.make_tensor("weight", TensorProto.FLOAT, [hidden_size, 3 * hidden_size], qkv_weight.flatten().tolist()),
        helper.make_tensor("bias", TensorProto.FLOAT, [3 * hidden_size], qkv_bias.flatten().tolist()),
    ]

    total_seq_len = seq_len + past_seq_len

    graph = helper.make_graph(
        nodes,
        "NeoXAttention_Graph",
        [
            helper.make_tensor_value_info("input", TensorProto.FLOAT, [batch_size, seq_len, hidden_size]),
            helper.make_tensor_value_info("mask_index", TensorProto.INT32, [batch_size, total_seq_len]),
            helper.make_tensor_value_info(
                "past", TensorProto.FLOAT, [2, batch_size, num_heads, total_seq_len, hidden_size // num_heads]
            ),
            helper.make_tensor_value_info("past_sequence_length", TensorProto.INT32, [1]),
        ],
        [
            helper.make_tensor_value_info("output", TensorProto.FLOAT, [batch_size, seq_len, hidden_size]),
            helper.make_tensor_value_info(
                "present", TensorProto.FLOAT, [2, batch_size, num_heads, total_seq_len, hidden_size // num_heads]
            ),
        ],
        initializers,
    )

    model = helper.make_model(graph)
    return model.SerializeToString()

class RotaryEmbedding(torch.nn.Module):
    def __init__(self, dim, max_position_embeddings, base=10000, device=None):
        super().__init__()
        inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float().to(device) / dim))
        self.register_buffer("inv_freq", inv_freq)

        # Build here to make `torch.jit.trace` work.
        self.max_seq_len_cached = max_position_embeddings
        t = torch.arange(self.max_seq_len_cached, device=self.inv_freq.device, dtype=self.inv_freq.dtype)
        freqs = torch.einsum("i,j->ij", t, self.inv_freq)
        # Different from paper, but it uses a different permutation in order to obtain the same calculation
        emb = torch.cat((freqs, freqs), dim=-1)
        self.cos_cached = emb.cos()[None, None, :, :]
        self.sin_cached = emb.sin()[None, None, :, :]

    def forward(self, x, seq_len=None):
        # x: [bs, num_attention_heads, seq_len, head_size]
        # This `if` block is unlikely to be run after we build sin/cos in `__init__`. Keep the logic here just in case.
        if seq_len > self.max_seq_len_cached:
            self.max_seq_len_cached = seq_len
            t = torch.arange(self.max_seq_len_cached, device=x.device, dtype=self.inv_freq.dtype)
            freqs = torch.einsum("i,j->ij", t, self.inv_freq)
            # Different from paper, but it uses a different permutation in order to obtain the same calculation
            emb = torch.cat((freqs, freqs), dim=-1).to(x.device)
            self.cos_cached = emb.cos()[None, None, :, :]
            self.sin_cached = emb.sin()[None, None, :, :]
        return self.cos_cached[:seq_len, ...].to(x.device), self.sin_cached[:seq_len, ...].to(x.device)

def rotate_half(x):
    """Rotates half the hidden dims of the input."""
    x1 = x[..., : x.shape[-1] // 2]
    x2 = x[..., x.shape[-1] // 2 :]
    return torch.cat((-x2, x1), dim=-1)

def apply_rotary_pos_emb(q, k, cos, sin, offset: int = 0):
    cos = cos[..., offset : q.shape[-2] + offset, :]
    sin = sin[..., offset : q.shape[-2] + offset, :]
    q_embed = (q * cos) + (rotate_half(q) * sin)
    k_embed = (k * cos) + (rotate_half(k) * sin)
    return q_embed, k_embed

class GPTNeoXAttention(nn.Module):
    def __init__(self, batch_size, seq_len, num_head, hidden_size, past_seq_len=0):
        super().__init__()
        self.do_rotary = True
        self.num_attention_heads = num_head
        self.hidden_size = hidden_size
        self.head_size = self.hidden_size // self.num_attention_heads
        self.rotary_ndims = int(self.head_size)
        max_positions = 2048
        self.register_buffer(
            "bias",
            torch.tril(torch.ones((max_positions, max_positions), dtype=torch.uint8)).view(
                1, 1, max_positions, max_positions
            ),
        )
        self.register_buffer("masked_bias", torch.tensor(-1e9))
        self.rotary_emb = RotaryEmbedding(self.rotary_ndims, 2048, 10000)
        self.norm_factor = torch.sqrt(torch.tensor(self.head_size, dtype=torch.float32)).to(torch.get_default_dtype())
        self.query_key_value = nn.Linear(hidden_size, 3 * hidden_size)

        # self.query_key_value.weight.data.copy_(torch.tensor(np.ones((3 * hidden_size, hidden_size))))
        # self.query_key_value.bias.data.copy_(torch.tensor(np.zeros((3 * hidden_size))))

        if past_seq_len > 0:
            self.onnx_graph = create_neox_decoder_masked_self_attention_graph(
                batch_size,
                seq_len,
                past_seq_len,
                self.hidden_size,
                self.query_key_value.weight.reshape(self.num_attention_heads, 3, -1)
                .transpose(0, 1)
                .reshape(3 * self.hidden_size, -1)
                .transpose(0, 1),
                self.query_key_value.bias.reshape(self.num_attention_heads, 3, -1).transpose(0, 1).reshape(-1),
                self.num_attention_heads,
            )
        else:
            self.onnx_graph = create_neox_attention_graph(
                batch_size,
                seq_len,
                self.hidden_size,
                self.query_key_value.weight.reshape(self.num_attention_heads, 3, -1)
                .transpose(0, 1)
                .reshape(3 * self.hidden_size, -1)
                .transpose(0, 1),
                self.query_key_value.bias.reshape(self.num_attention_heads, 3, -1).transpose(0, 1).reshape(-1),
                self.num_attention_heads,
            )

    @classmethod
    def _merge_heads(cls, tensor, num_attention_heads, attn_head_size):
        """
        Merges attn_head_size dim and num_attn_heads dim into hidden dim
        """
        # tensor [bs, num_attention_heads, seq_len, attn_head_size]
        tensor = tensor.permute(0, 2, 1, 3).contiguous()
        # -> [bs, seq_len, num_attention_heads, attn_head_size]
        tensor = tensor.view(tensor.size(0), tensor.size(1), num_attention_heads * attn_head_size)
        # -> [bs, seq_len, hidden_size]
        return tensor

    def _attn(self, query, key, value, attention_mask=None, head_mask=None):
        # q, k, v: [bs, num_attention_heads, seq_len, attn_head_size]
        # compute causal mask from causal mask buffer
        batch_size, num_attention_heads, query_length, attn_head_size = query.size()
        key_length = key.size(-2)

        causal_mask = self.bias[:, :, key_length - query_length : key_length, :key_length].bool()

        query = query.view(batch_size * num_attention_heads, query_length, attn_head_size)
        key = key.view(batch_size * num_attention_heads, key_length, attn_head_size)
        attn_scores = torch.zeros(
            batch_size * num_attention_heads,
            query_length,
            key_length,
            dtype=query.dtype,
            device=key.device,
        )
        attn_scores = torch.baddbmm(
            attn_scores,
            query,
            key.transpose(1, 2),
            beta=1.0,
            alpha=(torch.tensor(1.0, dtype=self.norm_factor.dtype, device=self.norm_factor.device) / self.norm_factor),
        )
        attn_scores = attn_scores.view(batch_size, num_attention_heads, query_length, key_length)

        mask_value = torch.finfo(attn_scores.dtype).min
        # Need to be a tensor, otherwise we get error: `RuntimeError: expected scalar type float but found double`.
        # Need to be on the same device, otherwise `RuntimeError: ..., x and y to be on the same device`
        mask_value = torch.tensor(mask_value, dtype=attn_scores.dtype).to(attn_scores.device)
        attn_scores = torch.where(causal_mask, attn_scores, mask_value)

        if attention_mask is not None:
            # Apply the attention mask
            attn_scores = attn_scores + attention_mask

        attn_weights = nn.functional.softmax(attn_scores, dim=-1)
        attn_weights = attn_weights.to(value.dtype)

        # Mask heads if we want to
        if head_mask is not None:
            attn_weights = attn_weights * head_mask

        attn_output = torch.matmul(attn_weights, value)
        return attn_output, attn_weights

    # Reorder 'K' from [B, N, S, H] to [B, N, H/4, S, 4]
    def reorder_key_cache(self, key_cache, batch_size, num_heads, sequence_length, head_size, max_sequence_length):
        ordered = np.zeros_like(key_cache)

        # assume float
        num_inner_elements = 4
        chunks = int(head_size / num_inner_elements)

        for b in range(batch_size):
            for h in range(num_heads):
                for c in range(chunks):
                    for s in range(sequence_length):
                        base_offset = (b * num_heads * max_sequence_length * head_size) + (
                            h * max_sequence_length * head_size
                        )
                        input_base_offset = base_offset + (s * head_size) + (c * num_inner_elements)
                        output_base_offset = (
                            base_offset + (c * max_sequence_length * num_inner_elements) + (s * num_inner_elements)
                        )
                        for e in range(num_inner_elements):
                            ordered[output_base_offset + e] = key_cache[input_base_offset + e]

        return ordered

    def onnx_forward(
        self,
        hidden_states,
        attention_mask=None,
        layer_past=None,
    ):
        import onnxruntime

        sess_options = onnxruntime.SessionOptions()
        cuda_providers = ["CUDAExecutionProvider"]
        if cuda_providers[0] not in onnxruntime.get_available_providers():
            return None
        ort_session = onnxruntime.InferenceSession(self.onnx_graph, sess_options, providers=["CUDAExecutionProvider"])

        ort_inputs = {
            "input": np.ascontiguousarray(hidden_states.cpu().numpy()),
        }

        if attention_mask is not None:
            ort_inputs["mask_index"] = np.ascontiguousarray(attention_mask.cpu().numpy())

        if layer_past is not None:
            past_key = np.ascontiguousarray(layer_past[0].detach().numpy())
            past_value = np.ascontiguousarray(layer_past[1].detach().numpy())

            past_seq_len = past_key.shape[2]
            max_seq_len = past_seq_len + hidden_states.shape[1]

            past_key_padded = np.zeros(
                [past_key.shape[0], past_key.shape[1], max_seq_len, past_key.shape[3]],
                dtype=np.float32,
            )
            past_value_padded = np.zeros(
                [past_value.shape[0], past_value.shape[1], max_seq_len, past_value.shape[3]],
                dtype=np.float32,
            )
            past_key_padded[:, :, : past_key.shape[2], :] = past_key
            past_value_padded[:, :, : past_value.shape[2], :] = past_value
            reordered_past_key = self.reorder_key_cache(
                past_key_padded.flatten(),
                batch_size=past_key_padded.shape[0],
                num_heads=self.num_attention_heads,
                sequence_length=past_seq_len,
                head_size=self.head_size,
                max_sequence_length=max_seq_len,
            )
            reordered_past_key = reordered_past_key.reshape(past_key_padded.shape)
            ort_inputs["past"] = np.stack((reordered_past_key, past_value_padded), axis=0)
            ort_inputs["past_sequence_length"] = np.array([past_seq_len], dtype=np.int32)

        ort_output = ort_session.run(None, ort_inputs)

        output = torch.tensor(ort_output[0])

        return output

    def torch_forward(
        self,
        hidden_states,
        attention_mask=None,
        layer_past=None,
    ):
        has_layer_past = layer_past is not None

        # Compute QKV
        # Attention heads [batch, seq_len, hidden_size]
        #   --> [batch, seq_len, (np * 3 * head_size)]
        qkv = self.query_key_value(hidden_states)

        # [batch, seq_len, (num_heads * 3 * head_size)]
        #   --> [batch, seq_len, num_heads, 3 * head_size]
        new_qkv_shape = qkv.size()[:-1] + (self.num_attention_heads, 3 * self.head_size)
        qkv = qkv.view(*new_qkv_shape)

        # [batch, seq_len, num_attention_heads, 3 * head_size] --> 3 [batch, num_attention_heads, seq_len, head_size]
        query = qkv[..., : self.head_size].permute(0, 2, 1, 3)
        key = qkv[..., self.head_size : 2 * self.head_size].permute(0, 2, 1, 3)
        value = qkv[..., 2 * self.head_size :].permute(0, 2, 1, 3)

        if self.do_rotary:
            # Compute rotary embeddings on rotary_ndims
            query_rot = query[..., : self.rotary_ndims]
            query_pass = query[..., self.rotary_ndims :]
            key_rot = key[..., : self.rotary_ndims]
            key_pass = key[..., self.rotary_ndims :]

            # Compute token offset for rotary embeddings (when decoding)
            seq_len = key.shape[-2]
            offset = 0
            if has_layer_past:
                offset = layer_past[0].shape[-2]
                seq_len += offset
            cos, sin = self.rotary_emb(value, seq_len=seq_len)
            query, key = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, offset=offset)
            query = torch.cat((query, query_pass), dim=-1)
            key = torch.cat((key, key_pass), dim=-1)

        # Cache QKV values
        if has_layer_past:
            past_key = layer_past[0]
            past_value = layer_past[1]
            key = torch.cat((past_key, key), dim=-2)
            value = torch.cat((past_value, value), dim=-2)

        # Compute attention
        attn_output, _ = self._attn(query, key, value, attention_mask, head_mask=None)

        # Reshape outputs
        attn_output = self._merge_heads(attn_output, self.num_attention_heads, self.head_size)

        return attn_output

class TestGPTNeoXAttention(unittest.TestCase):
    def test_gpt_neox_attention(self):
        for batch_size in [1, 2, 4, 8]:
            for seq_len in [32, 128, 512, 1024, 2048]:
                for num_head in [12]:
                    for hidden_size in [768]:
                        attn = GPTNeoXAttention(batch_size, seq_len, num_head, hidden_size)

                        hidden_states = torch.normal(mean=0.5, std=0.1, size=(batch_size, seq_len, hidden_size)).to(
                            torch.float32
                        )

                        torch_output = attn.torch_forward(hidden_states)
                        ort_output = attn.onnx_forward(hidden_states)
                        if ort_output is not None:
                            assert torch.allclose(torch_output, ort_output, atol=1e-4)

    def test_gpt_neox_decoder_masked_self_attention(self):
        for batch_size in [1, 2, 4, 8]:
            for past_seq_len in [1, 4, 32, 128, 512, 1024]:
                total_seq_len = past_seq_len + 1
                for num_head in [12]:
                    for hidden_size in [768]:
                        attn = GPTNeoXAttention(batch_size, 1, num_head, hidden_size, past_seq_len)

                        hidden_states = torch.normal(mean=0.5, std=0.1, size=(batch_size, 1, hidden_size)).to(
                            torch.float32
                        )

                        attention_mask = torch.ones((batch_size, total_seq_len)).to(torch.int32)
                        past_key = torch.normal(
                            mean=0.5,
                            std=0.1,
                            size=(batch_size, num_head, past_seq_len, hidden_size // num_head),
                        ).to(torch.float32)
                        past_value = torch.normal(
                            mean=0.5,
                            std=0.1,
                            size=(batch_size, num_head, past_seq_len, hidden_size // num_head),
                        ).to(torch.float32)
                        layer_past = (past_key, past_value)

                        torch_output = attn.torch_forward(
                            hidden_states, attention_mask=attention_mask, layer_past=layer_past
                        )
                        ort_output = attn.onnx_forward(
                            hidden_states, attention_mask=attention_mask, layer_past=layer_past
                        )
                        if ort_output is not None:
                            assert torch.allclose(torch_output, ort_output, atol=1e-4)

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


#!/bin/python3

import math
import os
import random
import re
import sys

def gradingStudents(grades):
    list=[]
    for i in range(len(grades)):
        if grades[i]>=38:
            value=grades[i]%5;
            minus=5-value
            if minus<=2:
                list.append(grades[i]+minus)
            else:
                list.append(grades[i])
        else:
            list.append(grades[i])
    return list

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    grades_count = int(input().strip())

    grades = []

    for _ in range(grades_count):
        grades_item = int(input().strip())
        grades.append(grades_item)

    result = gradingStudents(grades)

    fptr.write('\n'.join(map(str, result)))
    fptr.write('\n')
    fptr.close()


import counteract as ct

# Return JWT
token = ct.getToken()
# print("Token returned: " + str(token))

# Pull all hosts from CounterACT
hosts = ct.getHosts(token)
# print(hosts)                # All hosts json object
# print(len(hosts["hosts"]))  # Total number of active hosts

# Pull all policies from CounterACT
policies = ct.getPolicies(token)
# print(policies)                # All policies json object

# Find policy ID with policy name
policyid = ct.getPoliciesMainID(token, "HVAC")
# print(policyid)

# Get all Host fields
hostfields = ct.getHostFields(token)
# print(hostfields)

# Get all properites for a host
props = ct.getHostProps(token, "10.80.100.100")
# print(props)


class Node:
    def __init__(self, s):
        parts = s.split(" ")
        self.name = parts[0]
        self.weight = int(parts[1][1:-1])
        self.childrennames = []
        self.children = []
        self.parent = None
        if len(parts) > 2:
            self.childrennames = [p.strip(",") for p in parts[3:]]
    def setparent(self, p):
        self.parent = p
        p.children.append(self)

    def childcount(self):
        return len(self.childrennames)

    def childweight(self):
        if self.childcount() == 0:
            return self.weight
        return sum(c.childweight() for c in self.children)

    def childrenbalanced(self):
        w = [c.childweight for c in self.children]
        return max(w) == min(w)

    def totalweight(self):
        if self.childcount() == 0:
            return self.weight
        return self.weight + sum(c.totalweight() for c in self.children)

inp = [Node(l.strip()) for l in open('7.txt')]

print([n.childrennames for n in inp])

def getnode(name):
    for n in inp:
        if n.name == name:
            return n
    raise IndexError

for n in inp:
    for c in n.childrennames:
        getnode(c).setparent(n)

p = inp[0]
while p.parent != None:
    p = p.parent

print(p.name)
print([c.totalweight() for c in p.children])

def examineweight(n):
    cw = [c.totalweight() for c in n.children]

    if min(cw) != max(cw):
        print(n.name, n.weight, cw)
        exit()
    for c in n.children:
        examineweight(c)

#examineweight(p.children[3].children[1])

n = p.children[3].children[1].children[2]
print(n.weight, n.childrennames)
for c in n.children:
    print(n.totalweight())


#https://note.nkmk.me/python-opencv-face-detection-haar-cascade/
import cv2
import numpy as np
import sys
import os.path

def main():
	#images = os.listdir("/Users/dennomaaya/Desktop/py/sei/") #ディレクトリのパスをここに書く
	data_dir_path = u"/Users/dennomaaya/Desktop/py/cascade_directory/seikai"
	file_list = os.listdir(r'/Users/dennomaaya/Desktop/py/cascade_directory/seikai')
	i2 = 311

	for file_name in file_list:
		name, ext = os.path.splitext(file_name)
		if ext == u'.png' or u'.jpeg' or u'.jpg' or u'.JPG' and name == ".DS_Store":
			abs_name = data_dir_path + '/' + file_name
			img = cv2.imread(abs_name)
			#dst2 = cv2.resize(img, (1000, 1000), interpolation=cv2.INTER_LINEAR)
			# 結果を出力
			cv2.imwrite("create/photo/" + str(i2) + ".jpg", img,[cv2.IMWRITE_JPEG_QUALITY,100])
			print(file_name + ".jpg" + "変換完了")
			i2 += 1

if __name__ == "__main__":
	main()


# -*- coding: utf-8 -*-
"""
Created on Sat Sep 23 21:27:26 2017

@author: Deven
"""

def answer(x, y):
    # your code here
    a= 1
    b = x+y-1
    count = 0
    """for i in range(1,b+1):
        count +=i"""
    count = ((b+1)*(b+2))/2
    count -= (b-a)
    count += (x-1)
    return count

if __name__ == '__main__':
    s = answer(5,10)
    print(s)


"""
Created on Wed Nov 17 11:04:50 2021

Global columnarity index

@author: apizz
"""

import glob
import numpy as np
import nibabel as nb
import matplotlib.pyplot as plt
import matplotlib as mpl
import os
from my_layer_profiles import *
import math
from copy import copy
import pandas as pd

STUDY_PATH = "D:\\Pilot_Exp_VASO\\pilotAOM"
SUBJ = ['sub-02', 'sub-03', 'sub-04', 'sub-05', 'sub-06']
CONDT = ['standard']
FUNC = ['BOLD', 'VASO']
ROI = ['leftMT_Sphere16radius']
n_lay = 3

for itSbj, su in enumerate(SUBJ):
    PATH_IN = os.path.join(STUDY_PATH, su, 'derivatives', 'anat', 'patch_flatten')

    # %% Loading input files
    # 1) Metric file
    os.chdir(PATH_IN)
    for FILE_1 in glob.glob('{}_seg_rim_4_9_metric_equivol_binz_*_flat_[0-9][0-9][0-9]x[0-9][0-9][0-9]_voronoi.*'.format(su)):
        print('For {}, loading file {}'.format(su, FILE_1))
    nii1 = nb.load(os.path.join(PATH_IN, FILE_1))
    metrics_map = nii1.get_fdata()
    dims = np.shape(metrics_map)
    lay = my_layer_profiles(metrics_map, n_lay)
    lay = np.reshape(lay, dims)
    os.chdir(PATH_IN)

    for itFunc, fu in enumerate(FUNC):

        # 2) Map (e.g. percent signal change map) /// can be changed in the future
        os.chdir(PATH_IN)
        for FILE_2 in glob.glob('{}_{}_CV_{}_t_map_fix_hd_scaled_4_binz_*_flat_[0-9][0-9][0-9]x[0-9][0-9][0-9]_voronoi.nii.gz'.format(su, ROI[0], fu)):
            print('For {}, loading file {}'.format(su, FILE_2))
        nii2 = nb.load(os.path.join(PATH_IN, FILE_2))
        func_map = nii2.get_fdata()

        psc_deep = copy(func_map)
        psc_sup = copy(func_map)

        # 4) winner map
        os.chdir(PATH_IN)
        for FILE_4 in glob.glob('{}_{}_CV_{}_winner_map_fix_hd_scaled_4_plus1_binz_*_flat_[0-9][0-9][0-9]x[0-9][0-9][0-9]_voronoi.nii.gz'.format(su, ROI[0], fu)):
            print('Working on {}, file {}'.format(su, FILE_4))
        nii4 = nb.load(os.path.join(PATH_IN, FILE_4))
        winner_map = nii4.get_fdata()
        winner_map[winner_map > 0] = 1
        dims = np.shape(winner_map)

        # Create a mask
        act_mask = np.zeros(dims)
        max_val = np.nanmax(winner_map, axis=2)
        act_mask[max_val == 1, :] = 1

        out_name = os.path.join(PATH_IN, '{}_{}_act_mask_{}_flat'.format(su, ROI[0], fu))
        out = nb.Nifti1Image(act_mask, header=nii2.header, affine=nii2.affine)
        nb.save(out, out_name)

        # Compute winner map for all voxels

        # # Find values for deep lay
        # idx_deep = lay == 1
        # idx_deep = np.reshape(idx_deep, dims)

        # idx_deep = np.array(act_mask * idx_deep, dtype=bool)

        # psc_deep[idx_deep == 0] = np.nan
        # deep = np.nanmean(psc_deep, axis=2)

        # idx_nan = np.isnan(deep) * 1
        # idx_not_nan = idx_nan == 0
        # x = deep[idx_not_nan]
        # # x = deep[deep != np.nan]

        # # Find values for superficial lay
        # idx_sup = lay == n_lay
        # idx_sup = np.reshape(idx_sup, dims)

        # idx_sup = np.array(act_mask * idx_sup, dtype=bool)

        # psc_sup[idx_sup == 0] = np.nan
        # sup = np.nanmean(psc_sup, axis=2)
        # y = sup[idx_not_nan]

        # # //// correlation within columns
        # # get empirical correlation between deep and superficial grid points
        # vecCorrCol = np.corrcoef(x, y)
        # print("For {}, {}, correlation = {}".format(su, fu, vecCorrCol[0, 1]))

        # Scatter plot
        # plt.scatter(x, y)


import functools
import hashlib  # For hashing
import json
import pickle  # binary JSON alternative

# External packages
# Python equivalent to axios in the front end app
import requests

# from utility is a folder. with the empty file __init__.py all the other
# files are bundled.
# so we can import 'from folderName.fileName import className or functionName
from utility.hash_utils import hash_block
from utility.verification import Verification

from block import Block
from transaction import Transaction
from wallet import Wallet

MINING_REWARD = 10
# owner = set(['Chris'])
# we can define a set with {} like dictionaries without assigning key pairs.
# Python will know it's a set
# sets don't allow duplicates. If a duplicate is added it just will be ignored,
# it doesn't throw an error

# Will be passed from the node owner = 'Chris'

class Blockchain:
    def __init__(self, public_key, node_id):    # node_id for developpment
                                                # Remove for production
        # Our starting block
        genesis_block = Block(0, '', [], 100, 0)
        # Initialize our blockchain list
        self.chain = [genesis_block]
        # Note '__' double underscore in attribute name define them as private,
        # they cannot be changed from outside this class
        self.__open_transactions = []
        # Initialising peer_node as empty set. A set doesn't keep duplicate
        # values, but doesn't throw error. It just doesn't add the duplicate
        self.__peer_nodes = set()
        self.public_key = public_key
        self.node_id = node_id  # node_id for developpment.Remove for prod
        self.resolve_conflicts = False
        self.load_data()

    @property  # defines a getter for this attribute
    # The method name has to be the name of the attribute we want to make the
    # getter.
    # Python will automatically make it private. So in the getter function we
    # need to refer it with '__' double underscore
    # From outside the getter function we can acces it with the normal
    # attribute name
    def chain(self):
        return self.__chain[:]

    # defines setter method for this attribute. the name before the .setter
    # has to be the attribute name

    @chain.setter
    # The method name has to be the name of the attribute we want to make the
    # setter.
    # Python will automatically make it private. So in the setter function we
    # need to refer it with '__' double underscore
    # From outside the getter function we can acces it with the normal
    # attribute name
    # The 2 arguments self and val are passed automatically by python
    def chain(self, val):
        self.__chain = val

    # Not needed anymore. We handle it with the getter function above
    # def get_chain(self):
    #     ## Note, with the range selector all [:] we return a copy of the
    # chain, not the chain itself
    #     return self.__chain[:]

    def get_open_transactions(self):
        # Note, with the range selector all [:] we return a copy of the
        # open_transactions, not the transactions themselves
        return self.__open_transactions[:]

    def load_data(self):
        # Runtime Error handling with 'try' and 'except'
        try:
            # Load from PICKLE
            # Better implementation of special datatypes (eg. Ordered dicts)
            # Cannot display the file in readable form
            # Switch to JSON for this course so we can read the file
            # with open('blockchain.p', mode='rb') as f:
            #     global blockchain
            #     global open_transactions
            #     file_content = pickle.loads(f.read())
            #     blockchain = file_content['chain']
            #     open_transactions = file_content['otx']

            # Load from JSON
            # node_id for developpment only. Remove for production
            with open('blockchain-{}.txt'.format(self.node_id), mode='r') as f:
                text_content = f.readlines()
                # Loading blockchain
                # Range selector -1 to remove the line break
                blockchain = json.loads(text_content[0][:-1])
                # We need to alter the transaction in the block to add the
                # ordereddic structure
                updated_blockchain = []
                for block in blockchain:
                    converted_tx = [Transaction(
                        tx['sender'], tx['recipient'], tx['amount'],
                        tx['signature']) for tx in block['transactions']]

                    updated_block = Block(
                        block['index'], block['previous_hash'], converted_tx,
                        block['proof'], block['timestamp'])

                    updated_blockchain.append(updated_block)
                # will trigger the setter method defined for the chain
                # attribute
                self.chain = updated_blockchain
                # Loading transactions
                open_transactions = json.loads(text_content[1][:-1])
                updated_transactions = []
                for tx in open_transactions:
                    updated_transaction = Transaction(
                        tx['sender'], tx['recipient'], tx['amount'],
                        tx['signature'])
                    updated_transactions.append(updated_transaction)
                self.__open_transactions = updated_transactions
                # Loading peer nodes
                peer_nodes = json.loads(text_content[2])
                self.__peer_nodes = set(peer_nodes)
        except (IOError, IndexError):
            print('File not found')
        # except ValueError:
        #     print('Value Error!')
        # finally: #Always executes, if try succeed or Except errors handles.
        # Used to cleanup
        #     print('Cleanup!')

    def save_data(self):
        try:
            # Using PICKLE
            # In this course we switch to JSON for readble file (see load_data)
            # with open('blockchain.p', mode='wb') as f:
            #     #In binary we cannot make a line break.
            #     #So we create a dictionary holding the x lists to save
            #     save_data = {
            #         'chain': blockchain,
            #         'otx': open_transactions
            #     }
            #     f.write(pickle.dumps(save_data))

            # Using JSON
            # node_id for developpment only. Remove for production
            with open('blockchain-{}.txt'.format(self.node_id), mode='w') as f:
                # Convert block objects to dictionary. JSON doesn't know
                # Python objects
                saveable_chain = [block.__dict__ for block in
                                  [Block(block_el.index,
                                         block_el.previous_hash,
                                         [tx.__dict__ for tx in
                                          block_el.transactions],
                                         block_el.proof, block_el.timestamp)
                                   for block_el in self.__chain]]
                f.write(json.dumps(saveable_chain))
                f.write('\n')
                saveable_tx = [tx.__dict__ for tx in self.__open_transactions]
                f.write(json.dumps(saveable_tx))
                f.write('\n')
                f.write(json.dumps(list(self.__peer_nodes)))
        except IOError:
            print("Couldn't save data!")

    def proof_of_work(self):
        last_block = self.__chain[-1]
        last_hash = hash_block(last_block)
        proof = 0
        while not Verification.valid_proof(self.__open_transactions, last_hash,
                                           proof):
            proof += 1
        print(f"The proof number is: {proof}")
        return proof

    def get_last_blockchain_value(self):
        if len(self.__chain) < 1:
            return None

        return self.__chain[-1]

    def get_balance(self, sender=None):
        if sender is None:
            if self.public_key is None:
                return None
            participant = self.public_key
        else:
            participant = sender
        # Sent amounts in Blockchain
        tx_sender = [[tx.amount for tx in block.transactions  # block['transactions']
                      if tx.sender == participant] for block in self.__chain]
        # Sent amounts in open Transactions
        open_tx_sender = [tx.amount
                          for tx in self.__open_transactions if tx.sender == participant]
        tx_sender.append(open_tx_sender)

        tx_recipient = [[tx.amount for tx in block.transactions  # ['transactions']
                         if tx.recipient == participant] for block in self.__chain]

        # Calculating amount with use of reducer and lambda function
        # Reduce function has to be imported from functools.1st argument is a
        # function, 2nd is the Listname
        # 3rd optional is the start value. It returns the old value + new
        # value of operation
        # Lambda is a temporary function. The fields after lambda are the
        # arguments followed by ':' Then the function itself
        sent_amount = functools.reduce(lambda tx_sum, tx_amount: tx_sum + sum(
            tx_amount) if len(tx_amount) > 0 else tx_sum + 0, tx_sender, 0)
        received_amount = functools.reduce(lambda tx_sum, tx_amount: tx_sum + sum(
            tx_amount) if len(tx_amount) > 0 else tx_sum + 0, tx_recipient, 0)

        return received_amount - sent_amount

        # Calculating amount with use of For loop
        # sent_amount = 0
        # for tx in tx_sender:
        #     if len(tx) > 0:
        #         idx = 0
        #         while idx < len(tx):
        #             sent_amount += tx[idx]
        #             idx += 1

        # received_amount = 0
        # # Received amounts in Blockchain
        # tx_recipient = [[tx['amount'] for tx in block['transactions']
        # if tx['recipient'] == participant] for block in blockchain]
        # for tx in tx_recipient:
        #     if len(tx) > 0:
        #         idx = 0
        #         while idx < len(tx):
        #             received_amount += tx[idx]
        #             idx += 1

        # Other way to calculate the balance (My first solution I come up)
        # balance = 0
        # for block_idx in range(len(blockchain)):
        #     for tx_idx in range(len(blockchain[block_idx]['transactions'])):
        #         if blockchain[block_idx]['transactions'][tx_idx]['sender'] == participant:
        #             balance = balance - blockchain[block_idx]['transactions'][tx_idx]['amount']
        #         if blockchain[block_idx]['transactions'][tx_idx]['recipient'] == participant:
        #             balance = balance + blockchain[block_idx]['transactions'][tx_idx]['amount']
        # return balance

    def add_transaction(self, recipient, sender, signature, amount=1.0, is_receiving=False):
        """Adds transactions to the open_transactions dictionary
        Arguments:
            :sender: the sender of the transaction
            :recipient: the receiver of the transaction
            :signature: the signature of the transaction
            :amount: the value of the transaction (default 1.0)
        """

        # As the order of Dictionaries can change we need to work with
        # imported Ordereddics package
        # The order is important to us for the SHA256 hash algorithm
        # It's defined as a list of tuples
        # transaction = OrderedDict(
        #     [('sender', sender), ('recipient', recipient), ('amount', amount)])

        # prevent adding transaction when no wallet loaded
        # if self.public_key == None:
        #     return None

        transaction = Transaction(sender, recipient, amount, signature)
        if Verification.verify_transaction(transaction, self.get_balance):
            self.__open_transactions.append(transaction)
            self.save_data()
            # Broadcast transaction to all peer nodes as long it is NOT a
            # received broadcast
            if not is_receiving:
                for node in self.__peer_nodes:
                    url = 'http://{}/broadcast-transaction'.format(node)
                    try:
                        response = requests.post(url, json={
                                                 'sender': sender,
                                                 'recipient': recipient,
                                                 'amount': amount,
                                                 'signature': signature})
                        if response.status_code == 400 or response.status_code == 500:
                            print('Transaction declined. Need resolving')
                            return False
                    # continue with next peer node if connection error (node not online)
                    except requests.exceptions.ConnectionError:
                        continue

            return transaction
        else:
            return None

    def mine_block(self):
        # prevent adding transaction when no wallet loaded
        if self.public_key is None:
            return None

        # index [-1] accesses the last block of the chain
        last_block = self.__chain[-1]

        hashed_block = hash_block(last_block)
        # We copy the list open_transaction with the ':' range selector to
        # copy the whole list
        # In complex objects like lists,tuples,sets,dictionary just assigning
        # a new value
        # with '=' just copies the reference. So if we change the copied list
        # it will also be changed in the original
        copied_open_transactions = self.__open_transactions[:]

        # We calculate the proof number without the mining reward.
        # To validate the chain we need to remove the reward_transaction
        # before validating it
        proof = self.proof_of_work()

        # verify each open_transactions signatures. if one signature doesn't
        # verify correctly we abort the mining
        # this is done without the MINING transaction
        for tx in copied_open_transactions:
            if not Wallet.verify_transaction_signature(tx):
                return None

        reward_transaction = Transaction(
            'MINING', self.public_key, MINING_REWARD, '')
        copied_open_transactions.append(reward_transaction)
        block = Block(len(self.__chain), hashed_block,
                      copied_open_transactions, proof)

        self.__chain.append(block)

        # Empty open transaction files and save the datas
        self.__open_transactions = []
        self.save_data()
        # Broadcasting new block
        for node in self.__peer_nodes:
            url = 'http://{}/broadcast-block'.format(node)
            converted_block = block.__dict__.copy()
            converted_block['transactions'] = [
                tx.__dict__ for tx in converted_block['transactions']]
            try:
                response = requests.post(url, json={'block': converted_block})
                if response.status_code == 400 or response.status_code == 500:
                    print('Block declined!')
                # when broadcasting block to peers returns a 409 message then
                # set reolve_conflicts. see node.py broadcast_block
                if response.status_code == 409:
                    self.resolve_conflicts = True

            except requests.exceptions.ConnectionError:
                continue

        return block

    def add_block(self, block):
        # converting the received dictionary transactions to transaction objects
        transactions = [Transaction(
            tx['sender'], tx['recipient'], tx['amount'], tx['signature']) for tx in block['transactions']]
        proof_is_valid = Verification.valid_proof(
            transactions[:-1], block['previous_hash'], block['proof'])
        hashes_match = hash_block(self.chain[-1]) == block['previous_hash']
        if not proof_is_valid or not hashes_match:
            return False

        converted_block = Block(
            block['index'], block['previous_hash'], transactions, block['proof'], block['timestamp'])
        self.__chain.append(converted_block)
        # Removing transaction from open_transaction if they are in the mined block
        stored_transactions = self.__open_transactions[:]
        for itx in block['transactions']:
            for opentx in stored_transactions:
                if opentx.sender == itx['sender'] and opentx.recipient == itx['recipient'] and opentx.amount == itx['amount'] and opentx.signature == itx['signature']:
                    try:
                        self.__open_transactions.remove(opentx)
                    except ValueError:
                        print('Item was alredy removed')

        self.save_data()
        return True

    def resolve(self):
        winner_chain = self.chain
        replace = False
        for node in self.__peer_nodes:
            url = 'http://{}/chain'.format(node)
            try:
                response = requests.get(url)
                node_chain = response.json()
                # node_chain.transactions = [Transaction(tx['sender'],
                # tx['recipient'], tx['amount'], tx['signature']) for tx in
                # node_chain.transactions]
                node_chain = [Block(block['index'],
                                    block['previous_hash'],
                                    [Transaction(tx['sender'], tx['recipient'],
                                                 tx['amount'], tx['signature'])
                                     for tx in block['transactions']],
                                    block['proof'],
                                    block['timestamp'])
                              for block in node_chain]

                if (len(node_chain) > len(winner_chain)
                        and Verification.verify_chain(node_chain)):
                    replace = True
                    winner_chain = node_chain
            except requests.exceptions.ConnectionError:
                continue
        self.chain = winner_chain
        self.resolve_conflicts = False
        if replace:
            self.__open_transactions = []
        self.save_data()
        return replace

    def add_peer_node(self, node):
        """Adds new node to the peer_node_set
            Arguments:
                :node: the URL of the node that should be added
        """
        self.__peer_nodes.add(node)
        self.save_data()

    def remove_peer_node(self, node):
        """removes node from the peer_node_set
            Arguments:
                :node: the URL of the node that should be removed
        """
        self.__peer_nodes.discard(node)
        self.save_data()

    def get_peer_nodes(self):
        # returns a copy of the peer_nodes
        return list(self.__peer_nodes)


from get_data import get_data

with open("inputs/day4ex.txt", "r") as f:
    data = [x.strip() for x in f]
data = get_data(2022, 4)

data = list(row.split(",") for row in data)

def has_overlap(row):
    l1, r1 = row[0].split("-")
    l2, r2 = row[1].split("-")

    return (int(l1) >= int(l2) and int(r1) <= int(r2)) or (
        int(l2) >= int(l1) and int(r2) <= int(r1)
    )

print("Part 1:", sum(has_overlap(row) for row in data))

def has_partial_overlap(row):
    l1, r1 = row[0].split("-")
    l2, r2 = row[1].split("-")

    return int(l2) <= int(l1) <= int(r2) or int(l1) <= int(l2) <= int(r1)

print("Part 2:", sum(has_partial_overlap(row) for row in data))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Date ：2020/11/16 23:53
# Author：Lydia
from time import sleep

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support.wait import WebDriverWait

class TestWX:
    def setup(self):
        # 定义字典
        caps = {
            "platformName": "Android",
            "deviceName": "wework",
            "appPackage": "com.tencent.wework",
            "appActivity": ".launch.WwMainActivity",
            # noReset 保留缓存， 比如登录状态
            "noReset": "True",
            # 不停止应用，直接运行测试用例
            # "dontStopAppOnReset": "true",
            "skipDeviceInitialization": "true",
            "skipServerInstallation": "true"
            # "settings[waitForIdleTimeout]": 0
        }

        # 本机ip:server端口; 建立客户端和服务端连接
        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
        # 隐式等待
        self.driver.implicitly_wait(5)

    def teardown(self):
        self.driver.quit()

    def test_contact(self):
        name = 'lydia001'
        gender = '女'
        phonenum = '13800012301'
        self.driver.find_element(MobileBy.XPATH, "//*[@text='通讯录']").click()
        # self.driver.find_element(MobileBy.XPATH, "//*[@text='添加成员']").click()
        # 滚动查找【添加成员】
        self.driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,
                                 'new UiScrollable(new UiSelector()\
                                 .scrollable(true).instance(0))\
                                 .scrollIntoView(new UiSelector()\
                                 .text("添加成员").instance(0));').click()
        # 点击【手动输入添加】
        self.driver.find_element(MobileBy.XPATH, "//*[@text='手动输入添加']").click()
        # 设置【用户名】【性别】【手机号】
        self.driver.find_element(MobileBy.XPATH,
                                 "//*[contains(@text, '姓名')]/../*[@text='必填']").send_keys(name)
        self.driver.find_element(MobileBy.XPATH,
                                 "//*[contains(@text, '性别')]/..//*[@text='男']").click()
        if gender == "男":
            WebDriverWait(self.driver, 10).until(lambda x: x.find_element(MobileBy.XPATH, "//*[@text='女']"))
            self.driver.find_element(MobileBy.XPATH, "//*[@text='男']").click()
        else:
            self.driver.find_element(MobileBy.XPATH, "//*[@text='女']").click()

        self.driver.find_element(MobileBy.XPATH,
                                 "//*[contains(@text, '手机') and contains(@class, 'TextView')]/..//*[@text='手机号']").send_keys(
            phonenum)
        # 点击【保存】
        self.driver.find_element(MobileBy.XPATH, "//*[@text='保存']").click()
        sleep(2)
        print(self.driver.page_source)
        # 验证 添加成功 toast
        result = self.driver.find_element(MobileBy.XPATH, "//*[@class='android.widget.Toast']").text
        assert "添加成功" == result


"""
Tests for L{vor.beanstalk}.
"""

import time

from twisted.internet import reactor
from twisted.trial import unittest

from vor.graphite import FakeGraphiteProtocol
from vor.beanstalk import BeanstalkGraphiteService

class BeanstalkGraphiteServiceTest(unittest.TestCase):

    def setUp(self):
        self.collector = BeanstalkGraphiteService(reactor, 'localhost', 11300)
        self.collector.protocol = FakeGraphiteProtocol()

        self.stats = {
            'data': {
                'binlog-current-index': 38251,
                'binlog-max-size': 10485760,
                'binlog-oldest-index': 37838,
                'binlog-records-migrated': 50794314,
                'binlog-records-written': 1319757004,
                'cmd-bury': 0,
                'cmd-delete': 363640185,
                'cmd-ignore': 0,
                'cmd-kick': 0,
                'cmd-list-tube-used': 0,
                'cmd-list-tubes': 9,
                'cmd-list-tubes-watched': 0,
                'cmd-pause-tube': 0,
                'cmd-peek': 7482443,
                'cmd-peek-buried': 0,
                'cmd-peek-delayed': 0,
                'cmd-peek-ready': 0,
                'cmd-put': 363619059,
                'cmd-release': 541720773,
                'cmd-reserve': 0,
                'cmd-reserve-with-timeout': 1428990806,
                'cmd-stats': 796,
                'cmd-stats-job': 3263658965,
                'cmd-stats-tube': 4348,
                'cmd-touch': 0,
                'cmd-use': 343686629,
                'cmd-watch': 13871,
                'current-connections': 518,
                'current-jobs-buried': 0,
                'current-jobs-delayed': 4439,
                'current-jobs-ready': 108,
                'current-jobs-reserved': 33,
                'current-jobs-urgent': 0,
                'current-producers': 171,
                'current-tubes': 17,
                'current-waiting': 312,
                'current-workers': 345,
                'job-timeouts': 1959,
                'max-job-size': 65535,
                'pid': 974,
                'rusage-stime': 215212.02191,
                'rusage-utime': 98421.882983,
                'total-connections': 22336794,
                'total-jobs': 363619059,
                'uptime': 9296864,
                'version': 1.5
                },
            'state': 'ok',
            'bytes': 991
            }

    def test_gotStats(self):
        """
        Stats metrics are emitted as metrics.
        """
        self.collector.gotStats(self.stats, 'prefix')
        result = self.collector.protocol.output
        self.assertEqual(
                17,
                result['prefix.current-tubes'][0])

    def test_gotStatsTimestamp(self):
        """
        Stats metrics have a timestamp.
        """
        self.collector.gotStats(self.stats, 'prefix')
        result = self.collector.protocol.output
        self.assertApproximates(
                time.time(),
                result['prefix.current-tubes'][1],
                3)

    def test_gotStatsNoVersion(self):
        """
        Stats metrics have no version metric.
        """
        self.collector.gotStats(self.stats, 'prefix')
        result = self.collector.protocol.output
        self.assertNotIn('prefix.version', result)

    def test_gotStatsNoPID(self):
        """
        Stats metrics have not PID metric.
        """
        self.collector.gotStats(self.stats, 'prefix')
        result = self.collector.protocol.output
        self.assertNotIn('prefix.pid', result)


decks = []

while True:
    deck = input()

    if deck == '-':
        break

    m = int(input())

    for i in range(m):
        h = int(input())
        deck = deck[h:]+deck[:h]
    decks.append(deck)

for sorted_deck in decks:
    print(sorted_deck)


def es_primo(numero):
    if numero <= 1:
        return False  # Los números menores o iguales a 1 no son primos

    # Comprobar si el número es divisible por cualquier número entre 2 y la raíz cuadrada del número
    for i in range(2, int(numero ** 0.5) + 1):
        if numero % i == 0:
            return False  # El número es divisible, no es primo

    return True  # Si no se encontraron divisores, el número es primo

# Solicitar al usuario que ingrese un número
numero = int(input("Ingrese un número: "))

# Llamar a la función es_primo y mostrar el resultado
if es_primo(numero):
    print(f"{numero} es un número primo.")
else:
    print(f"{numero} no es un número primo.")


# Import TfidfVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer

# Create a TfidfVectorizer: tfidf
tfidf = TfidfVectorizer()

# Apply fit_transform to document: csr_mat
csr_mat = tfidf.fit_transform(documents)

# Print result of toarray() method
print(csr_mat.toarray())

# Get the words: words
words = tfidf.get_feature_names()

# Print words
print(words)

# Perform the necessary imports
#TruncatedSVD is able to perform PCA on sparse arrays in csr_matrix format, such as word-frequency arrays
#Combine knowledge of TruncatedSVD and k-means to cluster some popular pages from Wikipedia.
#DATASET -  https://blog.lateral.io/2015/06/the-unknown-perils-of-mining-wikipedia/

from sklearn.decomposition import TruncatedSVD
from sklearn.cluster import KMeans
from sklearn.pipeline import make_pipeline

# Create a TruncatedSVD instance: svd
svd = TruncatedSVD(n_components = 50)

# Create a KMeans instance: kmeans
kmeans = KMeans(n_clusters = 6)

# Create a pipeline: pipeline
pipeline = make_pipeline(svd, kmeans)

# Import pandas
import pandas as pd

# Fit the pipeline to word-frequency array - articles
pipeline.fit(articles)

# Predict the cluster labels.
# Calculate the cluster labels: labels
labels = pipeline.predict(articles)

# Create a DataFrame aligning labels and titles: df
df = pd.DataFrame({'label': labels, 'article': titles})

# Display df sorted by cluster label
print(df.sort_values('label'))
# cluster of wikipedia articles formed