text:
import pandas as pd
import numpy as np
import os
from sklearn.externals import joblib
from gensim.models import word2vec
from keras.preprocessing.sequence import pad_sequences
from keras.callbacks import History
from keras.models import load_model
import jieba
import json

from SentimentAnalysis.creat_data import bat
from SentimentAnalysis.sentence_transform.creat_vocab_word2vec import creat_vocab_word2vec
from SentimentAnalysis.models.sklearn_supervised import sklearn_supervised
from SentimentAnalysis.models import sklearn_config
from SentimentAnalysis.models.neural_bulit import neural_bulit

jieba.setLogLevel('WARN')
DIR = os.path.dirname(__file__)

class SentimentAnalysis():
    # def __init__(self):
    #     pass

    # open api
    def open_api(self):
        os.system("python %s" % (DIR + '/flask_api.py'))
        os.close()

    # get labels from baidu,ali,tencent
    def creat_label(self, texts):
        results_dataframe = bat.creat_label(texts)
        return results_dataframe

    def creat_vocab(self,
                    texts=None,
                    sg=0,
                    size=5,
                    window=5,
                    min_count=1,
                    vocab_savepath=DIR + '/models/vocab_word2vec.model'):
        '''
        get dictionary by word2vec
        :param texts: list of text
        :param sg: 0 CBOW,1 skip-gram
        :param size: the dimensionality of the feature vectors
        :param window: the maximum distance between the current and predicted word within a sentence
        :param min_count: ignore all words with total frequency lower than this
        :param vocab_savepath: path to save word2vec dictionary
        :return: None
        '''
        # 构建词向量词库
        self.vocab_word2vec = creat_vocab_word2vec(texts=texts,
                                                   sg=sg,
                                                   vocab_savepath=vocab_savepath,
                                                   size=size,
                                                   window=window,
                                                   min_count=min_count)

    def load_vocab_word2vec(self,
                            vocab_loadpath=DIR + '/models/vocab_word2vec.model'):
        '''
        load dictionary
        :param vocab_loadpath: path to load word2vec dictionary
        :return:
        '''
        self.vocab_word2vec = word2vec.Word2Vec.load(vocab_loadpath)

    def train(self,
              texts=None,
              label=None,
              model_name='SVM',
              model_savepath=DIR + '/models/classify.model',
              net_shape=None,
              batch_size=100,  # 神经网络参数
              epochs=2,  # 神经网络参数
              verbose=2,  # 神经网络参数
              maxlen=None,  # 神经网络参数
              **sklearn_param):
        '''
        use sklearn/keras to train model
        :param texts: x
        :param label: y
        :param model_name: name want to train
        :param model_savepath: model save path
        :param batch_size: for keras fit
        :param epochs: for keras fit
        :param verbose: for keras fit
        :param maxlen: for keras pad_sequences
        :param sklearn_param: param for sklearn
        :return: None
        '''
        self.model_name = model_name
        self.label = np.unique(np.array(label))
        # 文本转词向量
        vocab_word2vec = self.vocab_word2vec
        texts_cut = [[word for word in jieba.lcut(one_text) if word != ' '] for one_text in texts]  # 分词
        data = [[vocab_word2vec[word] for word in one_text if word in vocab_word2vec] for one_text in texts_cut]
        if maxlen is None:
            maxlen = max([len(i) for i in texts_cut])
        self.maxlen = maxlen
        # sklearn模型，词向量计算均值
        if model_name in ['SVM', 'KNN', 'Logistic']:
            data = [sum(i) / len(i) for i in data]
            # 配置sklearn模型参数
            if model_name == 'SVM':
                if sklearn_param == {}:
                    sklearn_param = sklearn_config.SVC
            elif model_name == 'KNN':
                if sklearn_param == {}:
                    sklearn_param = sklearn_config.KNN
            elif model_name == 'Logistic':
                if sklearn_param == {}:
                    sklearn_param = sklearn_config.Logistic
            # 返回训练模型
            self.model = sklearn_supervised(data=data,
                                            label=label,
                                            model_savepath=model_savepath,
                                            model_name=model_name,
                                            **sklearn_param)

        # keras神经网络模型，
        elif model_name in ['Conv1D_LSTM', 'Conv1D', 'LSTM']:
            data = pad_sequences(data, maxlen=maxlen, padding='post', value=0, dtype='float32')
            label_transform = np.array(pd.get_dummies(label))
            if net_shape is None:
                if model_name == 'Conv1D_LSTM':
                    net_shape = [
                        {'name': 'InputLayer', 'input_shape': data.shape[1:]},
                        {'name': 'Conv1D', 'filters': 64, 'kernel_size': 3, 'strides': 1, 'padding': 'same',
                         'activation': 'relu'},
                        {'name': 'MaxPooling1D', 'pool_size': 5, 'padding': 'same', 'strides': 2},
                        {'name': 'LSTM', 'units': 16, 'activation': 'tanh', 'recurrent_activation': 'hard_sigmoid',
                         'dropout': 0., 'recurrent_dropout': 0.},
                        {'name': 'Flatten'},
                        {'name': 'Dense', 'activation': 'relu', 'units': 64},
                        {'name': 'Dropout', 'rate': 0.2, },
                        {'name': 'softmax', 'activation': 'softmax', 'units': len(np.unique(label))}
                    ]

                elif model_name == 'LSTM':
                    net_shape = [
                        {'name': 'InputLayer', 'input_shape': data.shape[1:]},
                        {'name': 'Masking'},
                        {'name': 'LSTM', 'units': 16, 'activation': 'tanh', 'recurrent_activation': 'hard_sigmoid',
                         'dropout': 0., 'recurrent_dropout': 0.},
                        {'name': 'Dense', 'activation': 'relu', 'units': 64},
                        {'name': 'Dropout', 'rate': 0.2, },
                        {'name': 'softmax', 'activation': 'softmax', 'units': len(np.unique(label))}
                    ]
                elif model_name == 'Conv1D':
                    net_shape = [
                        {'name': 'InputLayer', 'input_shape': data.shape[1:]},
                        {'name': 'Conv1D', 'filters': 64, 'kernel_size': 3, 'strides': 1, 'padding': 'same',
                         'activation': 'relu'},
                        {'name': 'MaxPooling1D', 'pool_size': 5, 'padding': 'same', 'strides': 2},
                        {'name': 'Flatten'},
                        {'name': 'Dense', 'activation': 'relu', 'units': 64},
                        {'name': 'Dropout', 'rate': 0.2, },
                        {'name': 'softmax', 'activation': 'softmax', 'units': len(np.unique(label))}
                    ]

            model = neural_bulit(net_shape=net_shape,
                                 optimizer_name='Adagrad',
                                 lr=0.001,
                                 loss='categorical_crossentropy')
            history = History()
            model.fit(data, label_transform,
                      batch_size=batch_size, epochs=epochs, verbose=verbose, callbacks=[history])
            train_log = pd.DataFrame(history.history)
            self.model = model
            self.train_log = train_log
            if model_savepath != None:
                model.save(model_savepath)
        with open(DIR + '/data_info.json', mode='w', encoding='utf-8') as f:
            json.dump({'maxlen': maxlen, 'label': list(self.label)}, f)

    def load_model(self,
                   model_loadpath=DIR + '/models/classify.model',
                   model_name=None,
                   data_info_path=DIR + '/data_info.json'):
        '''
        load sklearn/keras model
        :param model_loadpath: path to load sklearn/keras model
        :param model_name: load model name
        :param data_info_path: date information path
        :return: None
        '''

        with open(data_info_path, encoding='utf-8') as f:
            data_info = json.load(f)
        self.maxlen = data_info['maxlen']
        self.label = data_info['label']
        self.model_name = model_name

        if self.model_name in ['SVM', 'KNN', 'Logistic']:
            self.model = joblib.load(model_loadpath)
        elif self.model_name in ['Conv1D_LSTM', 'Conv1D', 'LSTM']:
            self.model = load_model(model_loadpath)

    def predict_prob(self,
                     texts=None):
        '''
        predict probability
        :param texts:  list of text
        :return: list of probability
        '''
        # 文本转词向量
        vocab_word2vec = self.vocab_word2vec
        if self.model_name in ['SVM', 'KNN', 'Logistic']:
            texts_cut = [[word for word in jieba.lcut(one_text) if word != ' '] for one_text in texts]  # 分词
            data = [[vocab_word2vec[word] for word in one_text if word in vocab_word2vec] for one_text in texts_cut]
            data = [sum(i) / len(i) for i in data]
            self.testdata = data
            results = self.model.predict_proba(data)
        elif self.model_name in ['Conv1D_LSTM', 'Conv1D', 'LSTM']:
            texts_cut = [[word for word in jieba.lcut(one_text) if word != ' '] for one_text in texts]  # 分词
            data = [[vocab_word2vec[word] for word in one_text if word in vocab_word2vec] for one_text in texts_cut]
            data = pad_sequences(data, maxlen=self.maxlen, padding='post', value=0, dtype='float32')
            self.testdata = data
            results = self.model.predict(data)
        return results

    def predict(self,
                texts=None):
        '''
        predict class
        :param texts:  list of text
        :return: list of classify
        '''
        # 文本转词向量
        vocab_word2vec = self.vocab_word2vec
        if self.model_name in ['SVM', 'KNN', 'Logistic']:
            texts_cut = [[word for word in jieba.lcut(one_text) if word != ' '] for one_text in texts]  # 分词
            data = [[vocab_word2vec[word] for word in one_text if word in vocab_word2vec] for one_text in texts_cut]
            data = [sum(i) / len(i) for i in data]
            self.testdata = data
            results = self.model.predict(data)
        elif self.model_name in ['Conv1D_LSTM', 'Conv1D', 'LSTM']:
            texts_cut = [[word for word in jieba.lcut(one_text) if word != ' '] for one_text in texts]  # 分词
            data = [[vocab_word2vec[word] for word in one_text if word in vocab_word2vec] for one_text in texts_cut]
            data = pad_sequences(data, maxlen=self.maxlen, padding='post', value=0, dtype='float32')
            self.testdata = data
            results = self.model.predict(data)
            results = pd.DataFrame(results, columns=self.label)
            results = results.idxmax(axis=1)
        return results

if __name__ == '__main__':
    train_data = ['国王喜欢吃苹果',
                  '国王非常喜欢吃苹果',
                  '国王讨厌吃苹果',
                  '国王非常讨厌吃苹果']
    train_label = ['正面', '正面', '负面', '负面']
    # print('train data\n',
    #       pd.DataFrame({'data': train_data,
    #                     'label': train_label},
    #                    columns=['data', 'label']))
    test_data = ['涛哥喜欢吃苹果',
                 '涛哥讨厌吃苹果',
                 '涛哥非常喜欢吃苹果',
                 '涛哥非常讨厌吃苹果']
    test_label = ['正面', '负面', '正面', '负面']

    # 创建模型
    model = SentimentAnalysis()

    # 查看bat打的标签
    print(model.creat_label(test_data))

    model.creat_vocab(texts=train_data,
                      sg=0,
                      size=5,
                      window=5,
                      min_count=1,
                      vocab_savepath=None)

    # 导入词向量词包
    # model.load_vocab_word2vec(vocab_loadpath=DIR + '/vocab_word2vec.model')

    ###################################################################################
    # 进行机器学习
    model.train(texts=train_data,
                label=train_label,
                model_name='SVM',
                model_savepath=DIR + '/models/classify.model')

    # 导入机器学习模型
    model.load_model(model_loadpath=DIR + '/models/classify.model',
                     model_name='SVM',
                     data_info_path=DIR + '/data_info.json')

    # 进行预测:概率
    result_prob = model.predict_prob(texts=test_data)
    result_prob = pd.DataFrame(result_prob, columns=model.label)
    result_prob['predict'] = result_prob.idxmax(axis=1)
    result_prob['data'] = test_data
    result_prob = result_prob[['data'] + list(model.label) + ['predict']]
    print('prob:\n', result_prob)
    print('score:', np.sum(result_prob['predict'] == np.array(test_label)) / len(result_prob['predict']))

max_stars_repo_name: renjunxiang/Sentiment-analysis

max_issues_repo_name: renjunxiang/Sentiment-analysis

max_forks_repo_name: renjunxiang/Sentiment-analysis


text:
from __future__ import print_function
from cube_tic_tac_toe_3d.CubeTicTacToeLogic import Board
from framework.Game import Game
import numpy as np
import pygame
import math

class CubeTicTacToeGame(Game):
    """
    Game класс для кубических крестиков - ноликов. Потомок класса Game.
    """
    def __init__(self, n, players=(), ui=False):
        self.score = [0, 0]
        self.n = n
        self.tile_size = 40
        self.circle = self.load_picture("./cube_tic_tac_toe_3d/images/circle.bmp")
        self.cross = self.load_picture("./cube_tic_tac_toe_3d/images/cross.bmp")
        self.gray = self.load_picture("./cube_tic_tac_toe_3d/images/empty.png")
        self.players = players
        if ui:
            pygame.init()
            self.ui = ui
            self.surf = pygame.display.set_mode((self.ui['width'], self.ui['height']))
            self.surf.fill((255, 255, 255))

    def getInitBoard(self):
        """
        Возвращает стартовую доску, преобразованную в массив numpy.
        """
        b = Board(self.n)
        #print(b.pieces)
        return np.array(b.pieces)

    def getBoardSize(self):
        """
        Возвращает кортеж (z , y, x) с размерностями доски.
        """
        return (self.n, self.n, self.n)

    def getActionSize(self):
        """
        Возвращает количество возможных действий.
        """
        return self.n * self.n * self.n + 1

    def getNextState(self, board, player, action):
        """
        Возвращает следующее состояние доски.

        :param board: Текущая доска.
        :param player: Текущий игрок.
        :param action:
                    Действие, которое нужно применить.
                    Действие должно быть допустимым ходом.
        :return: следующее состояние доски, после совершение действия.
        """
        if action == self.n * self.n * self.n:
            return (board, -player)
        b = Board(self.n)
        b.pieces = np.copy(board)
        move = (int((action / self.n) / self.n), int((action / self.n) % self.n), int((action % self.n) % self.n))
        #print("MOVE:", move)
        b.execute_move(move, player)
        return (b.pieces, -player)

    def getValidMoves(self, board, player):
        """
        Возвращает numpy массив с допустимыми ходами для состояния доски
        board и текущего игрока player.

        :param board: Текущее состояние доски.
        :param player: Текущий игрок.
        """
        valids = [0] * self.getActionSize()
        b = Board(self.n)
        b.pieces = np.copy(board)
        legal_moves = b.get_legal_moves(player)
        if len(legal_moves) == 0:
            valids[-1] = 1
            return np.array(valids)
        for z, y, x in legal_moves:
            valids[self.n * self.n * z + self.n * y + x] = 1
        return np.array(valids)

    def getEstimatePos(self, board, player):
        """
			1.сумма в строке
			2.сумма в столбце
			3.cумма в глубину
			4.диагональ побочная
			5.главная диагональ
			6.диагональ вниз с верхней строки
			7.диагональ вниз с нижней строки
			8.диагональ вглубь с верхнего левого до нижнего правого в одной строке в глубину
			9.диагональ вниз с верхнего правого до нижнего левого в одной строке в глубину
			10.диагольналь от верхнего левого до правого нижнего в глубину и в ширину
			11.диагональ от верхнего правого до левого нижнего в глубину и в ширину
			12.диагональ от (в верхнем слое) нижнего левого до (нижнего слоя) правого верхнего
            13.диагональ от (в верхнем слое) нижнего правого до (нижнего слоя) левого верхнего
        """
        b = Board(self.n)
        b.pieces = np.copy(board)
        n = self.n

        valid_moves_pl1 = [0]*self.getActionSize()
        valid_moves_pl2 = [0]*self.getActionSize()
        #print(player)
        for z in range(n):
            for y in range(n):
                for x in range(n):
                    if b.pieces[z][y][x] == 0:
                        valid_moves_pl1[z * n * n  + y * n + x ] = +(sum([b.pieces[z][j][x] == player for j in range(n)]) + \
                            sum([b.pieces[z][y][i] == player for i in range(n)]) + \
                            sum([b.pieces[k][y][x] == player for k in range(n)]) + \
                            ((n - 1 - y) == x) * sum([b.pieces[z][(n - 1 - i)][i] == player for i in range(n)]) +\
                            (y == x) * sum([b.pieces[z][i][i] == player for i in range(n)]) + \
                            (z == y) * sum([b.pieces[k][k][x] == player for k in range(n)]) + \
                            (z == (n - 1 - y)) * sum([b.pieces[k][(n - 1 - k)][x] == player for k in range(n)]) + \
                            (z == x) * sum([b.pieces[k][y][k] == player for k in range(n)]) + \
                            (z == (n - 1 - x)) * sum([b.pieces[k][y][n - 1 - k] == player for k in range(n)]) + \
                            (z == y == x) * sum([b.pieces[k][k][k] == player for k in range(n)]) + \
                            (z == y == (n - 1 - x)) * sum([b.pieces[k][k][n - 1 - k] == player for k in range(n)]) + \
                            (z == (n - 1 - y) == x) * sum([b.pieces[k][(n - 1 - k)][k] == player for k in range(n)]) + \
                            (z == (n - 1 - y) == (n - 1 - x)) * sum([b.pieces[k][(n - 1 - k)][n - 1 - k] == player for k in range(n)]))
                        valid_moves_pl2[z * n * n  + y * n + x ] = +(sum([b.pieces[z][j][x] == -player for j in range(n)]) + \
                            sum([b.pieces[z][y][i] == -player for i in range(n)]) + \
                            sum([b.pieces[k][y][x] == -player for k in range(n)]) + \
                            ((n - 1 - y) == x) * sum([b.pieces[z][(n - 1 - i)][i] == -player for i in range(n)]) +\
                            (y == x) * sum([b.pieces[z][i][i] == -player for i in range(n)]) + \
                            (z == y) * sum([b.pieces[k][k][x] == -player for k in range(n)]) + \
                            (z == (n - 1 - y)) * sum([b.pieces[k][(n - 1 - k)][x] == -player for k in range(n)]) + \
                            (z == x) * sum([b.pieces[k][y][k] == -player for k in range(n)]) + \
                            (z == (n - 1 - x)) * sum([b.pieces[k][y][n - 1 - k] == -player for k in range(n)]) + \
                            (z == y == x) * sum([b.pieces[k][k][k] == -player for k in range(n)]) + \
                            (z == y == (n - 1 - x)) * sum([b.pieces[k][k][n - 1 - k] == -player for k in range(n)]) + \
                            (z == (n - 1 - y) == x) * sum([b.pieces[k][(n - 1 - k)][k] == -player for k in range(n)]) + \
                            (z == (n - 1 - y) == (n - 1 - x)) * sum([b.pieces[k][(n - 1 - k)][n - 1 - k] == -player for k in range(n)]))
                    else:
                        continue

        #print(valid_moves_pl2)
        #v = [1] * len(board)
        #return [(board,v)]
        return (valid_moves_pl1, valid_moves_pl2)
    """
    def getGameEnded(self, board, player):
        # return 0 if not ended, 1 if player 1 won, -1 if player 1 lost
        # player = 1
        b = Board(self.n)
        b.pieces = np.copy(board)
        #print(b.pieces)
        if b.is_win(player):
            return 1
        if b.is_win(-player):
            return -1
        if b.has_legal_moves():
            return 0
        # draw has a very little value
        return 1e-4

    """
    def getGameEnded(self, board, player):

#тут у нас сами правила
 #требует доработки

        b = Board(self.n)
        b.pieces = np.copy(board)
        status = 1e-4
        n = self.n
        if any([any([sum([b.pieces[k][j][i] for i in range(n)]) == self.n for j in range(n)]) == 1 for k in range(n)]) or \
                any([any([sum([b.pieces[k][j][i] for j in range(n)]) == self.n for i in range(n)]) == 1 for k in range(n)]) or \
                any([any([sum([b.pieces[k][j][i]for k in range(n)]) == self.n for i in range(n)]) == 1 for j in range(n)]) or \
                any([any([sum([b.pieces[k][i][i] for i in range(n)]) == self.n]) == 1 for k in range(n)]) or \
                any([any([sum([b.pieces[k][(n - 1 - i)][i] for i in range(n)]) == self.n]) == 1 for k in range(n)]) or \
                any([any([sum([b.pieces[k][k][i] for k in range(n)]) == self.n]) == 1 for i in range(n)]) or \
                any([any([sum([b.pieces[k][(n - 1 - k)][i] for k in range(n)]) == self.n]) == 1 for i in range(n)]) or \
                any([any([sum([b.pieces[k][i][k] for k in range(n)]) == self.n]) == 1 for i in range(n)]) or \
                any([any([sum([b.pieces[k][i][(n - 1 - k)] for k in range(n)]) == self.n]) == 1 for i in range(n)]) or \
                sum([b.pieces[k][k][k] for k in range(n)]) == self.n or \
                sum([b.pieces[k][k][(n - 1 - k)] for k in range(n)]) == self.n or \
                sum([b.pieces[k][(n - 1 - k)][k] for k in range(n)]) == self.n or \
                sum([b.pieces[k][(n - 1 - k)][(n - 1 - k)] for k in range(n)]) == self.n :
            status = 1
        elif any([any([sum([b.pieces[k][j][i] for i in range(n)]) == -self.n for j in range(n)]) == 1 for k in range(n)]) or \
                any([any([sum([b.pieces[k][j][i] for j in range(n)]) == -self.n for i in range(n)]) == 1 for k in range(n)]) or \
                any([any([sum([b.pieces[k][j][i] for k in range(n)]) == -self.n for i in range(n)]) == 1 for j in range(n)]) or \
                any([any([sum([b.pieces[k][i][i] for i in range(n)]) == -self.n]) == 1 for k in range(n)]) or \
                any([any([sum([b.pieces[k][(n - 1 - i)][i] for i in range(n)]) == -self.n]) == 1 for k in range(n)]) or \
                any([any([sum([b.pieces[k][k][i] for k in range(n)]) == -self.n]) == 1 for i in range(n)]) or \
                any([any([sum([b.pieces[k][(n - 1 - k)][i] for k in range(n)]) == -self.n]) == 1 for i in range(n)]) or \
                any([any([sum([b.pieces[k][i][k] for k in range(n)]) == -self.n]) == 1 for i in range(n)]) or \
                any([any([sum([b.pieces[k][i][(n - 1 - k)] for k in range(n)]) == -self.n]) == 1 for i in range(n)]) or \
                sum([b.pieces[k][k][k] for k in range(n)]) == -self.n or \
                sum([b.pieces[k][k][(n - 1 - k)] for k in range(n)]) == -self.n or \
                sum([b.pieces[k][(n - 1 - k)][k] for k in range(n)]) == -self.n or \
                sum([b.pieces[k][(n - 1 - k)][(n - 1 - k)] for k in range(n)]) == -self.n :
            status = -1
        if status != 1e-4:
            return status
        if any([b.pieces[k][i][j] == 0 for k in range(self.n) for i in range(self.n) for j in range(self.n)]):
            status = 0
        return status

    def getCanonicalForm(self, board, player):
        #print(board)
        return player * board

    def getSymmetries(self, board, pi):
        assert(len(pi) == self.getActionSize())
        pi = np.copy(pi)
        #l = [(board, pi)]
        #return l
        #return [(board, pi), (board[:,::-1,], pi[::-1])]

        pi_board = np.reshape(pi[:-1], (self.n,self.n, self.n))
       # print(pi)
        li = []
        rot1 = {(1,0),(2,0)}

        for i in range(1, 5):
            for m in range(1, 5):
                for j in [True, False]:
                    newB = np.rot90(np.rot90(board, m, (1,-1)), i, (0, 2))
                    newPi = np.rot90(np.rot90(pi_board, m, (1,-1)), i, (0,2))
                    if j:
                        newB = np.fliplr(newB)
                        newPi = np.fliplr(newPi)
                    li += [(newB, list(newPi.ravel()) + [pi[-1]])]
        for i in {1,3}:
            for m in range(1, 5):
                for j in [True, False]:
                    newB = np.rot90(np.rot90(board, m, (1,-1)), i, (0, 1))
                    newPi = np.rot90(np.rot90(pi_board, m, (1,-1)), i, (0, 1))
                    if j:
                        newB = np.fliplr(newB)
                        newPi = np.fliplr(newPi)
                    li += [(newB, list(newPi.ravel()) + [pi[-1]])]

        #print(li)

        return li

    def stringRepresentation(self, board):
        #print(board)
        return board.tostring()

    def getScore(self, board, player):
        b = Board(self.n)
        b.pieces = np.copy(board)
        return b.count_diff(player)

    def getCount(self, board, player):
        b = Board(self.n)
        b.pieces = np.copy(board)
        return b.get_count(player)

    def load_picture(self, filepath):
        """
        :param      filepath: path to file
        :return:    picture: picture with size TILESIZE
        """
        picture = pygame.image.load(filepath)
        picture = pygame.transform.scale(picture, (self.tile_size, self.tile_size))
        return picture

    def displays(self, board):
        #n = board.shape[0]
        n = self.n
        self.surf.fill((255, 255, 255))
        TILESIZE = self.tile_size
        for x in range(n):
            for y in range(n):
                for z in range(n):
                    piece = board[z][y][x]

                    if piece == 1:
                        picture = self.cross
                    elif piece == -1:
                        picture = self.circle
                    else:
                        picture = self.gray
                    rect = pygame.Rect(x * TILESIZE, z * TILESIZE * n + y * TILESIZE, TILESIZE, TILESIZE)

                    self.surf.blit(picture, rect)
                    pygame.draw.rect(self.surf, (240, 240, 240), rect, 1)

                    if z != 0:
                        pygame.draw.line(self.surf, (255, 160, 0), (0, TILESIZE * n * z ), (TILESIZE * n,TILESIZE * n * z), 5)

                    draw_msg(self.surf, "Крестики: " + str(self.getCount(board, 1)),
                         (6 * self.tile_size, 0 * self.tile_size))
                    draw_msg(self.surf, "Нолики: " + str(self.getCount(board, -1)),
                         (6 * self.tile_size, int(1 * self.tile_size)))
                    draw_msg(self.surf, "Счет " + self.score[0].__str__() + " : " + self.score[1].__str__(),
                         (6 * self.tile_size, int(2 * self.tile_size)))
                    draw_msg(self.surf, "Игрок 1: " + "Нолики" if self.players[0] == -1 else "Игрок 1: " + "Крестики",
                         (6 * self.tile_size, int(4 * self.tile_size)))
                    draw_msg(self.surf, "Игрок 2: " + "Нолики" if self.players[1] == -1 else "Игрок 2: " + "Крестики",
                         (6 * self.tile_size, int(5 * self.tile_size)))
        pygame.display.update()

def display(board):
    print(board)

def draw_msg(surf, text, pos, fontsize=40, color_param=(0, 0, 0), bold=False, erase=False):
    myfont = pygame.font.SysFont("arial", fontsize, bold)
    if erase:
        surf.fill(pygame.Color("white"), (pos[0], pos[1], surf.get_width(), pos[1] + len(text) * fontsize))
    label = myfont.render(text, 1, color_param)
    surf.blit(label, pos)

ext: py

reason: import numpy


text:
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

from __future__ import print_function

import argparse
import logging
import os
import numpy as np
import json
import time

import mxnet as mx
from mxnet import gluon
from mxnet.gluon import nn
from mxnet import autograd as ag
from mxnet.gluon.data.vision import transforms

import gluoncv as gcv
from gluoncv.data.transforms import video
from gluoncv.data import VideoClsCustom
from gluoncv.model_zoo import get_model
from gluoncv.utils import makedirs, LRSequential, LRScheduler, split_and_load, TrainingHistory

logging.basicConfig(level=logging.DEBUG)

# ------------------------------------------------------------ #
# Training methods                                             #
# ------------------------------------------------------------ #

def train(args):
    # SageMaker passes num_cpus, num_gpus and other args we can use to tailor training to
    # the current container environment
    num_gpus = mx.context.num_gpus()
    ctx = [mx.gpu(i) for i in range(num_gpus)] if num_gpus > 0 else [mx.cpu()]
    # retrieve the hyperparameters we set in notebook (with some defaults)

    #number of training examples utilized in one iteration.
    batch_size = args.batch_size
    #number of times an entire dataset is passed forward and backward through the neural network
    epochs = args.epochs
    #tuning parameter in an optimization algorithm that determines the step size at each iteration while moving toward a   minimum of a loss function.
    learning_rate = args.learning_rate
    #Momentum remembers the update Δ w at each iteration, and determines the next update as a linear combination of the gradient and the previous update
    momentum = args.momentum
    #Optimizers are algorithms or methods used to change the attributes of your neural network such as weights and learning rate in order to reduce the losses.
    optimizer = args.optimizer
    #after each update, the weights are multiplied by a factor slightly less than 1.
    wd = args.wd
    optimizer_params = {'learning_rate': learning_rate, 'wd': wd, 'momentum': momentum}
    log_interval = args.log_interval

    #In this example, we use Inflated 3D model (I3D) with ResNet50 backbone trained on Kinetics400 dataset. We want to replace the last classification (dense) layer to the number of classes in the dataset.
    model_name = 'i3d_resnet50_v1_custom'
    #number of classes in the dataset
    nclass = 101
    #number of workers for the data loader
    num_workers = 8

    current_host = args.current_host
    hosts = args.hosts
    model_dir = args.model_dir
    CHECKPOINTS_DIR = '/opt/ml/checkpoints'
    checkpoints_enabled = os.path.exists(CHECKPOINTS_DIR)

    data_dir = args.train
    segments = 'rawframes'
    train ='ucfTrainTestlist/ucf101_train_split_2_rawframes.txt'

    #load the data with data loader
    train_data = load_data(data_dir,batch_size,num_workers,segments,train)
    # define the network
    net = define_network(ctx,model_name,nclass)
    #define the gluon trainer
    trainer = gluon.Trainer(net.collect_params(), optimizer, optimizer_params)
    #define loss function
    loss_fn = gluon.loss.SoftmaxCrossEntropyLoss()
    #define training metric
    train_metric = mx.metric.Accuracy()
    train_history = TrainingHistory(['training-acc'])
    net.hybridize()
    #learning rate decay hyperparameters
    lr_decay_count = 0
    lr_decay = 0.1
    lr_decay_epoch = [40, 80, 100]
    for epoch in range(epochs):
        tic = time.time()
        train_metric.reset()
        train_loss = 0

        # Learning rate decay
        if epoch == lr_decay_epoch[lr_decay_count]:
            trainer.set_learning_rate(trainer.learning_rate*lr_decay)
            lr_decay_count += 1

        # Loop through each batch of training data
        for i, batch in enumerate(train_data):
            # Extract data and label
            data = split_and_load(batch[0], ctx_list=ctx, batch_axis=0,even_split=False)
            label = split_and_load(batch[1], ctx_list=ctx, batch_axis=0,even_split=False)

            # AutoGrad
            with ag.record():
                output = []
                for _, X in enumerate(data):
                    X = X.reshape((-1,) + X.shape[2:])
                    pred = net(X)
                    output.append(pred)
                loss = [loss_fn(yhat, y) for yhat, y in zip(output, label)]

            # Backpropagation
            for l in loss:
                l.backward()

            # Optimize
            trainer.step(batch_size)

            # Update metrics
            train_loss += sum([l.mean().asscalar() for l in loss])
            train_metric.update(label, output)

            if i == 100:
                break

        name, acc = train_metric.get()

        # Update history and print metrics
        train_history.update([acc])
        print('[Epoch %d] train=%f loss=%f time: %f' %
            (epoch, acc, train_loss / (i+1), time.time()-tic))

    print('saving the model')
    save(net, model_dir)

def save(net, model_dir):
    # save the model
    net.export('%s/model'% model_dir)

def define_network(ctx,model_name,nclass):
    #In GluonCV, we can get a customized model with one line of code.
    net = get_model(name=model_name, nclass=nclass)
    net.collect_params().reset_ctx(ctx)
    print(net)
    return net

def load_data(data_dir, batch_size,num_workers,segments,train):

    #The transformation function does three things: center crop the image to 224x224 in size, transpose it to num_channels,num_frames,height*width, and normalize with mean and standard deviation calculated across all ImageNet images.

    #Use the general gluoncv dataloader VideoClsCustom to load the data with num_frames = 32 as the length. For another  dataset, you can just replace the value of root and setting to your data directory and your prepared text file.

    transform_train = video.VideoGroupTrainTransform(size=(224, 224), scale_ratios=[1.0, 0.8], mean=[0.485, 0.456, 0.406],
                                                          std=[0.229, 0.224, 0.225])
    train_dataset = VideoClsCustom(root=data_dir + '/' +
                                   segments,setting=data_dir + '/' + train,train=True,new_length=32,transform=transform_train)
    print(os.listdir(data_dir+ '/' + segments))
    print('Load %d training samples.' % len(train_dataset))
    return gluon.data.DataLoader(train_dataset, batch_size=batch_size,
                                                   shuffle=True, num_workers=num_workers)

# ------------------------------------------------------------ #
# Training execution                                           #
# ------------------------------------------------------------ #

def parse_args():
    parser = argparse.ArgumentParser()

    parser.add_argument('--batch-size', type=int, default=8)
    parser.add_argument('--epochs', type=int, default=10)
    parser.add_argument('--learning-rate', type=float, default=0.001)
    parser.add_argument('--momentum', type=float, default=0.9)
    parser.add_argument('--wd', type=float, default=0.0001)
    parser.add_argument('--log-interval', type=float, default=100)

    parser.add_argument('--optimizer', type=str, default='sgd')
    parser.add_argument('--model-dir', type=str, default=os.environ['SM_MODEL_DIR'])
    parser.add_argument('--train', type=str, default=os.environ['SM_CHANNEL_TRAINING'])

    parser.add_argument('--current-host', type=str, default=os.environ['SM_CURRENT_HOST'])
    parser.add_argument('--hosts', type=list, default=json.loads(os.environ['SM_HOSTS']))

    return parser.parse_args()

if __name__ == '__main__':
    args = parse_args()

    train(args)


text:
"""
Интерактивная проверка работы отфайнтюненной ruT5 (см. train_t5.py)
"""

import io
import re
import os

import tqdm
import torch
from transformers import T5ForConditionalGeneration, T5Tokenizer, T5Config
from transformers import TrainingArguments
from transformers import Trainer
from transformers import AdamW
from torch.utils.data import Dataset, DataLoader

import numpy as np

sentinel0 = '<extra_id_0>'
eos_token = '</s>'

if __name__ == '__main__':
    EPOCHS = 1
    tmp_dir = './tmp'
    model_name = 'sberbank-ai/ruT5-large'

    weights_path = os.path.join(tmp_dir, 'rut5_for_np_substituton.pt')

    tokenizer = T5Tokenizer.from_pretrained(model_name,)
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    print('Device={}'.format(device))

    t5config = T5Config.from_pretrained(model_name, )
    model = T5ForConditionalGeneration(t5config)
    model.load_state_dict(torch.load(weights_path, map_location=device))

    t5_input = 'Голодная кошка ловит <extra_id_0> .'
    input_ids = tokenizer(t5_input, return_tensors='pt').input_ids
    out_ids = model.generate(input_ids=input_ids,
                             max_length=40,
                             eos_token_id=tokenizer.eos_token_id,
                             early_stopping=True)

    t5_output = tokenizer.decode(out_ids[0][1:])
    print('t5_output={}'.format(t5_output))


text:
"""ProtoTorch datasets test suite"""

import os
import shutil
import unittest

import numpy as np
import torch

import prototorch as pt
from prototorch.datasets.abstract import Dataset, ProtoDataset

class TestAbstract(unittest.TestCase):
    def setUp(self):
        self.ds = Dataset("./artifacts")

    def test_getitem(self):
        with self.assertRaises(NotImplementedError):
            _ = self.ds[0]

    def test_len(self):
        with self.assertRaises(NotImplementedError):
            _ = len(self.ds)

    def tearDown(self):
        del self.ds

class TestProtoDataset(unittest.TestCase):
    def test_download(self):
        with self.assertRaises(NotImplementedError):
            _ = ProtoDataset("./artifacts", download=True)

    def test_exists(self):
        with self.assertRaises(RuntimeError):
            _ = ProtoDataset("./artifacts", download=False)

class TestNumpyDataset(unittest.TestCase):
    def test_list_init(self):
        ds = pt.datasets.NumpyDataset([1], [1])
        self.assertEqual(len(ds), 1)

    def test_numpy_init(self):
        data = np.random.randn(3, 2)
        targets = np.array([0, 1, 2])
        ds = pt.datasets.NumpyDataset(data, targets)
        self.assertEqual(len(ds), 3)

class TestCSVDataset(unittest.TestCase):
    def setUp(self):
        data = np.random.rand(100, 4)
        targets = np.random.randint(2, size=(100, 1))
        arr = np.hstack([data, targets])
        if not os.path.exists("./artifacts"):
            os.mkdir("./artifacts")
        np.savetxt("./artifacts/test.csv", arr, delimiter=",")

    def test_len(self):
        ds = pt.datasets.CSVDataset("./artifacts/test.csv")
        self.assertEqual(len(ds), 100)

    def tearDown(self):
        os.remove("./artifacts/test.csv")

class TestSpiral(unittest.TestCase):
    def test_init(self):
        ds = pt.datasets.Spiral(num_samples=10)
        self.assertEqual(len(ds), 10)

class TestIris(unittest.TestCase):
    def setUp(self):
        self.ds = pt.datasets.Iris()

    def test_size(self):
        self.assertEqual(len(self.ds), 150)

    def test_dims(self):
        self.assertEqual(self.ds.data.shape[1], 4)

    def test_dims_selection(self):
        ds = pt.datasets.Iris(dims=[0, 1])
        self.assertEqual(ds.data.shape[1], 2)

class TestBlobs(unittest.TestCase):
    def test_size(self):
        ds = pt.datasets.Blobs(num_samples=10)
        self.assertEqual(len(ds), 10)

class TestRandom(unittest.TestCase):
    def test_size(self):
        ds = pt.datasets.Random(num_samples=10)
        self.assertEqual(len(ds), 10)

class TestCircles(unittest.TestCase):
    def test_size(self):
        ds = pt.datasets.Circles(num_samples=10)
        self.assertEqual(len(ds), 10)

class TestMoons(unittest.TestCase):
    def test_size(self):
        ds = pt.datasets.Moons(num_samples=10)
        self.assertEqual(len(ds), 10)

# class TestTecator(unittest.TestCase):
#     def setUp(self):
#         self.artifacts_dir = "./artifacts/Tecator"
#         self._remove_artifacts()

#     def _remove_artifacts(self):
#         if os.path.exists(self.artifacts_dir):
#             shutil.rmtree(self.artifacts_dir)

#     def test_download_false(self):
#         rootdir = self.artifacts_dir.rpartition("/")[0]
#         self._remove_artifacts()
#         with self.assertRaises(RuntimeError):
#             _ = pt.datasets.Tecator(rootdir, download=False)

#     def test_download_caching(self):
#         rootdir = self.artifacts_dir.rpartition("/")[0]
#         _ = pt.datasets.Tecator(rootdir, download=True, verbose=False)
#         _ = pt.datasets.Tecator(rootdir, download=False, verbose=False)

#     def test_repr(self):
#         rootdir = self.artifacts_dir.rpartition("/")[0]
#         train = pt.datasets.Tecator(rootdir, download=True, verbose=True)
#         self.assertTrue("Split: Train" in train.__repr__())

#     def test_download_train(self):
#         rootdir = self.artifacts_dir.rpartition("/")[0]
#         train = pt.datasets.Tecator(root=rootdir,
#                                     train=True,
#                                     download=True,
#                                     verbose=False)
#         train = pt.datasets.Tecator(root=rootdir, download=True, verbose=False)
#         x_train, y_train = train.data, train.targets
#         self.assertEqual(x_train.shape[0], 144)
#         self.assertEqual(y_train.shape[0], 144)
#         self.assertEqual(x_train.shape[1], 100)

#     def test_download_test(self):
#         rootdir = self.artifacts_dir.rpartition("/")[0]
#         test = pt.datasets.Tecator(root=rootdir, train=False, verbose=False)
#         x_test, y_test = test.data, test.targets
#         self.assertEqual(x_test.shape[0], 71)
#         self.assertEqual(y_test.shape[0], 71)
#         self.assertEqual(x_test.shape[1], 100)

#     def test_class_to_idx(self):
#         rootdir = self.artifacts_dir.rpartition("/")[0]
#         test = pt.datasets.Tecator(root=rootdir, train=False, verbose=False)
#         _ = test.class_to_idx

#     def test_getitem(self):
#         rootdir = self.artifacts_dir.rpartition("/")[0]
#         test = pt.datasets.Tecator(root=rootdir, train=False, verbose=False)
#         x, y = test[0]
#         self.assertEqual(x.shape[0], 100)
#         self.assertIsInstance(y, int)

#     def test_loadable_with_dataloader(self):
#         rootdir = self.artifacts_dir.rpartition("/")[0]
#         test = pt.datasets.Tecator(root=rootdir, train=False, verbose=False)
#         _ = torch.utils.data.DataLoader(test, batch_size=64, shuffle=True)

#     def tearDown(self):
#         self._remove_artifacts()

max_stars_repo_name: si-cim/prototorch

max_issues_repo_name: si-cim/prototorch

max_forks_repo_name: si-cim/prototorch


text:
import gym
import numpy as np
from pilco.models.pilco import PILCO
from pilco.policies import RBFNPolicy
from pilco.cost import SaturatingCost

# from pilco.cost import ExponentialReward

from env.cart_pole_env import CartPole
env = CartPole()

# env = gym.make('InvertedPendulum-v2')
env.reset()

def rollout(policy, timesteps):
    """ Performs a rollout of a given policy on the environment for a given number of timesteps
    Args:
        policy: function representing policy to rollout
        timesteps: the number of timesteps to perform

    Returns:
        X: list of training inputs - (x, u) where x=state and u=control
        Y: list of training targets - differences y = x(t) - x(t-1)

    """
    x_set = []
    y_set = []
    env.reset()
    x, _, _, _ = env.step(0)
    x = np.append(x, np.sin(x[1]))
    x = np.append(x, np.cos(x[1]))
    x = np.delete(x, 1)
    print(x)
    for t in range(timesteps):
        env.render()
        # print(x)
        u = policy(x)
        # print("u_%i: %.3f" % (t, u))
        x_new, reward, done, _ = env.step(u)
        if done:
            print("Episode finished after {} timesteps".format(t + 1))
            break
        x_new = np.append(x_new, np.sin(x_new[1]))
        x_new = np.append(x_new, np.cos(x_new[1]))
        x_new = np.delete(x_new, 1)
        x_set.append(np.hstack((x, u)))
        y_set.append(x_new - x)
        x = x_new
    return np.stack(x_set), np.stack(y_set)

def random_policy(x):
    # a = env.action_space.sample() * 4
    a = env.action_space.sample()
    # if a >= env.action_space.high:
    #     a = env.action_space.high
    # if a <= env.action_space.low:
    #     a = env.action_space.low
    print(a)
    return a
    # return env.action_space.sample()

def pilco_policy(x):
    return pilco.compute_action(x[None, :])[0, :]

x, y = rollout(policy=random_policy, timesteps=40)
for i in range(1, 3):
    x_, y_ = rollout(policy=random_policy, timesteps=40)
    x = np.vstack((x, x_))
    y = np.vstack((y, y_))

state_dim = y.shape[1]
control_dim = x.shape[1] - state_dim

RBFNPolicy = RBFNPolicy(
    state_dim,
    control_dim=control_dim,
    num_basis_fun=10,
    max_action=env.action_space.high[0])

a = 0.25
l = 0.6
C = np.array([[1., l, 0.0], [0., 0., l]])
iT = a**(-2) * np.dot(C.T, C)

cost = SaturatingCost(state_dim=3, state_idxs=[0, 3, 4], W=iT, t=[0., 0., -1.])
# print(state_dim)
# cost = SaturatingCost(state_dim=state_dim)
# cost = SaturatingCost(
#     x_target=[0., 0., -1.], iT=iT, state_dim=3, state_idxs=[0, 3, 4])
# cost = ExponentialReward(state_dim)

pilco = PILCO(x, y, policy=RBFNPolicy, cost=cost, horizon=40)

for rollouts in range(15):
    # Learn dynamics model & use it to simulate/optimise policy
    pilco.optimize()
    import pdb

    pdb.set_trace()

    # Execute policy on environment
    x_new, y_new = rollout(policy=pilco_policy, timesteps=100)

    # Update dataset
    x = np.vstack((x, x_new))
    y = np.vstack((y, y_new))
    pilco.dynamics_model.set_XY(x, y)

max_stars_repo_name: aidanscannell/pilco-tensorflow

max_issues_repo_name: aidanscannell/pilco-tensorflow

max_forks_repo_name: aidanscannell/pilco-tensorflow


text:
import argparse
import binascii
import cv2
import json
import numpy as np
import sys

from collections import namedtuple
from itertools import chain

Metadata = namedtuple('Metadata', 'page_num num_pages num_bytes_on_page sha1')

# 47 = 255*3//16
PALETTE_RGB_WIDTH = 47

def convertToPaletteScale(val):
    val_int = int(val)
    if val_int > 255 * 3:
        raise ValueError(f'cannot convert {val_int} as it is greater than 255*3')
    remainder = val_int % PALETTE_RGB_WIDTH
    quotient = val_int // PALETTE_RGB_WIDTH
    if remainder > PALETTE_RGB_WIDTH / 2:
        # round up
        quotient = min(16, quotient + 1)
    return quotient

def rescale(image, regionOnGuest):
    w, h = [int(r) for r in regionOnGuest.split('x')]
    resized_image = cv2.resize(image, (w, h))
    return resized_image

def identifyBlock(img, r, c, w):
    # debug method to visualise which square we are processing
    # columns are x, rows are y
    x = c * w
    y = r * w
    print(f'drawing a green square at ({x},{y}) with width {w}')
    cv2.rectangle(img, (x, y), (x + w, y + w), (0, 255, 0), 2)
    cv2.namedWindow('output2', cv2.WINDOW_NORMAL)
    cv2.imshow('output2', img)
    cv2.waitKey(0)

def blockify(image, width, block_num=-1):
    """Converts the raw image into a list of blocks according to the given size

    E.g. if the image is of size 20x10 (width x height) and each block is of size
    5x5, this will return a list of 8 blocks, and each block will contain 25 elements
    representing the sum of the rgb components of each individual pixel
    """

    # we need to generate 'blocks' of pixels of width x width
    h, w, channels = image.shape  # we don't use channels
    print(f'h: {h}, w: {w}')
    num_rows = h // width
    num_cols = w // width
    print(f'dividing the image in {num_rows} rows and {num_cols} columns')
    counter = 0
    scale = width * width
    # block_num = 0
    ret = []
    for r in range(num_rows):
        for c in range(num_cols):
            tally = []
            for ii, i in enumerate(image[r * width : (r + 1) * width]):
                for jj, j in enumerate(i[c * width : (c + 1) * width]):
                    # r,g,b are the first 3 components
                    # the 4th is alpha which we don't want
                    tally.append(np.sum(j[:3]))
            if counter == block_num:
                print(f'average pixel: {sum(tally)/scale}')
                identifyBlock(image, r, c, width)
            ret.append(tally)
            counter += 1
    return ret

def pyramid_weights(n):
    """Weighs inner values more than outer values according to the height of the pyramid

    We scale the weights to ensure their sum is equal to one
    """

    r = np.arange(n)
    d = np.minimum(r, r[::-1])
    p = np.minimum.outer(d, d)
    sum_weights = sum(p.reshape(n * n))
    return p / sum_weights

def weigh_blocks(vals, width, sum_values=False):
    """Gives more weight to inner values of a block compared to outer values

    This is only applicable if the size of a block is > than 2x2 - otherwise
    the original blocks will be returned.

    From size 3x3 and above, the values at the edges of each block are weighed at 0.
    """

    if width < 3:
        return vals

    p = pyramid_weights(width)
    num_elems = width * width

    ret = []
    for val in vals:
        res = np.multiply(np.array(val).reshape((width, width)), p).reshape(
            1, num_elems
        )
        if sum_values:
            ret.append(sum(sum(r) for r in res))
        else:
            ret.extend(res)
    return ret

def combine_half_bytes(arr):
    pairs = zip(arr[::2], arr[1::2])
    return [((a << 4) + b) for (a, b) in pairs]

def extract_meta_data(byte_array, meta_data_length):
    ret = {}
    byteorder = 'big'
    data = byte_array[-meta_data_length:]
    page_num = int.from_bytes(data[:2], byteorder=byteorder)
    num_of_pages = int.from_bytes(data[2:4], byteorder=byteorder)
    num_bytes_on_page = int.from_bytes(data[4:6], byteorder=byteorder)
    sha1 = binascii.hexlify(data[6:26])  # 20 bytes
    print(f'{page_num}/{num_of_pages} - {num_bytes_on_page} - {sha1}')
    return Metadata(page_num, num_of_pages, num_bytes_on_page, sha1)

def extract(image, sqWidth, regionOnGuest, outFile, verify):
    print(f'processing {image}')
    img = cv2.imread(image, cv2.IMREAD_UNCHANGED)
    rimg = rescale(img, regionOnGuest)

    blocks = blockify(rimg, sqWidth)
    extract = weigh_blocks(blocks, sqWidth, sum_values=True)
    ps = [convertToPaletteScale(ex) for ex in extract]
    int_array = combine_half_bytes(ps)
    byte_array_all = bytearray(int_array)
    w, h = [int(r) for r in regionOnGuest.split('x')]
    # e.g. 1200/5 = 240 squares, which is 120 bytes
    meta_data_length = w // sqWidth // 2
    meta_data = extract_meta_data(byte_array_all, meta_data_length)
    byte_array = byte_array_all[: meta_data.num_bytes_on_page]

    if verify:
        import hashlib

        m = hashlib.sha1()
        m.update(byte_array)
        print(f'computed  sha1: {m.hexdigest()}')
        print(f'extracted sha1: {meta_data.sha1.decode("ascii")}')
    if outFile is None:
        import os

        bp = os.path.dirname(image)
        outFile = os.path.join(
            bp,
            f'block_{str(meta_data.page_num).zfill(5)}_{str(meta_data.num_pages).zfill(5)}_{meta_data.sha1.decode("ascii")}',
        )
    with open(outFile, 'wb') as f:
        f.write(byte_array)
        print(f'wrote file to {outFile}')

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--image',
        action='store',
        default='roi.png',
        type=str,
        help='path to image containing the data',
    )
    parser.add_argument(
        '--numBytes',
        action='store',
        default=1200 * 800,
        type=int,
        help='length (in bytes) of the file encoded in the image',
    )
    parser.add_argument(
        '--out', action='store', default='out.bin', help='out path for the decoded file'
    )
    parser.add_argument(
        '--verify',
        action='store_true',
        default=False,
        help='toggle computing the SHA1 of the decoded block and compares with what was extracted from the metadata',
    )
    parser.add_argument(
        '--blockSize',
        action='store',
        default=5,
        type=int,
        help='the width of a block in pixels',
    )
    parser.add_argument(
        '--regionOnGuest',
        action='store',
        default='1200x800',
        type=str,
        help='width x height area containing the data on the guest',
    )
    args = parser.parse_args()
    extract(args.image, args.blockSize, args.regionOnGuest, args.out, args.verify)

max_stars_repo_name: axiomiety/pixfiltrator

max_issues_repo_name: axiomiety/pixfiltrator

max_forks_repo_name: axiomiety/pixfiltrator


text:
# Copyright 2022 The EvoJAX Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import numpy as np
from typing import Tuple

import jax
import jax.numpy as jnp
from flax.struct import dataclass

from evojax.task.base import TaskState
from evojax.task.base import VectorizedTask

@dataclass
class State(TaskState):
    obs: jnp.ndarray
    sel: jnp.ndarray

class MDKP(VectorizedTask):
    """Multi-Dimensional Knapsack Problem (MDKP).

    Many real-world problems are MDKP in different contexts, and this task
    allows a user to simulate one by using their own data in the form of csv
    files. In addition, users can also use synthesized data.

    The format of the csv files are:

    1. capacity_csv should have N lines and S columns.
        bin1_attr1_cap, bin1_attr2_cap, ..., bin1_attrS_cap
        bin2_attr1_cap, bin2_attr2_cap, ..., bin2_attrS_cap
        ...
        binN_attr1_cap, binN_attr2_cap, ..., binN_attrS_cap

    2. items_csv should have M lines and S + 1 columns.
        item1_attr1, item1_attr2, ..., item1_attrS, item1_value
        item2_attr1, item2_attr2, ..., item2_attrS, item2_value
        ...
        itemM_attr1, itemM_attr2, ..., itemM_attrS, itemM_value

    A valid solution should select K<=M items that maximizes sum(item{k}_value),
    while keeping sum(item{k}_attr{j}) < bin{i}_attr{j}_cap
    if item{k} is assigned to bin{i}, for i in [1, N] and j in [1, S].
    """

    def __init__(self,
                 items_csv=None,
                 capacity_csv=None,
                 use_synthesized_data=True,
                 test=False):
        self.max_steps = 1
        self.test = test

        if use_synthesized_data:
            rnd = np.random.RandomState(seed=0)
            num_attrs = 3
            num_items = 2000
            num_bins = 5
            upper_bound = 100
            items = rnd.randint(
                low=0, high=upper_bound, size=(num_items, num_attrs + 1))

            num_items_per_bin = int(num_items * 0.5 / num_bins)
            num_sel_items = num_items_per_bin * num_bins
            ix = np.arange(num_items)
            rnd.shuffle(ix)
            sel_ix = ix[:num_sel_items]
            sel_items = items[sel_ix, :-1]
            caps = np.array(
                [sel_items[
                 (i * num_items_per_bin):(i + 1) * num_items_per_bin
                 ].sum(axis=0) for i in range(num_bins)])
        else:
            try:
                import pandas as pd
            except ModuleNotFoundError:
                print('This task requires pandas, '
                      'run "pip install -U pandas" to install.')
                sys.exit(1)

            caps = pd.read_csv(capacity_csv, header=None).values
            num_bins, num_attrs = caps.shape
            items = pd.read_csv(items_csv, header=None).values
            num_items, num_cols = items.shape
            assert num_cols == num_attrs + 1

        self.num_items = num_items
        self.num_attrs = num_attrs
        self.num_bins = num_bins
        self.items = items
        self.caps = caps
        items = jnp.array(items)
        caps = jnp.array(caps)
        self.obs_shape = tuple([num_items, num_attrs + 1])
        self.act_shape = tuple([num_bins, num_items])

        def reset_fn(key):
            return State(obs=items, sel=jnp.zeros([num_bins, num_items]))
        self._reset_fn = jax.jit(jax.vmap(reset_fn))

        def step_fn(state, action):
            # selection.shape = (N, M), obs.shape = (M, S + 1)
            selection = jnp.where(action > 0.5, 1., 0.)
            # bin_items_attr_sum.shape = (N, S + 1)
            bin_items_attr_sum = jnp.matmul(selection, state.obs)
            reward = bin_items_attr_sum[:, -1].sum()
            # If any item is assigned more than once, we set reward to zero.
            times_selected = selection.sum(axis=0)
            assignment_violation = jnp.prod(jnp.where(times_selected > 1, 0, 1))
            reward = reward * assignment_violation
            # If there is any limit violation, we set reward to zero.
            violation = jnp.prod(
                jnp.where(bin_items_attr_sum[:, :-1] > caps, 0, 1).ravel())
            reward = reward * violation
            return State(obs=state.obs, sel=action), reward, jnp.ones(())
        self._step_fn = jax.jit(jax.vmap(step_fn))

    def reset(self, key: jnp.array) -> TaskState:
        return self._reset_fn(key)

    def step(self,
             state: TaskState,
             action: jnp.ndarray) -> Tuple[TaskState, jnp.ndarray, jnp.ndarray]:
        return self._step_fn(state, action)

max_stars_repo_name: google/evojax

max_issues_repo_name: google/evojax

max_forks_repo_name: google/evojax

reason: import numpy,import jax


text:
import numpy as np
import cv2
import face_recognition

# 繪製delaunay triangles
def draw_delaunay(img, TriangleList, delaunary_color):

    size = img.shape
    r = (0, 0, size[1], size[0])

    for t in TriangleList:
        pt1 = (t[0], t[1])
        pt2 = (t[2], t[3])
        pt3 = (t[4], t[5])

        # if rect_contains(r, pt1) and rect_contains(r, pt2) and rect_contains(r, pt3):
        cv2.line(img, pt1, pt2, delaunary_color, 1)
        cv2.line(img, pt2, pt3, delaunary_color, 1)
        cv2.line(img, pt3, pt1, delaunary_color, 1)

def get_Ms(t1,t2):
    Ms = []
    for i in range(len(t1)):
        pts1 = np.array([[t1[i][0],t1[i][1]],[t1[i][2],t1[i][3]],[t1[i][4],t1[i][5]]]).astype(np.float32)
        pts2 = np.array([[t2[i][0],t2[i][1]],[t2[i][2],t2[i][3]],[t2[i][4],t2[i][5]]]).astype(np.float32)
        # print(pts1)
        Ms.append(cv2.getAffineTransform(pts1,pts2))
    return Ms

def delaunay(h,w,points):
    subdiv = cv2.Subdiv2D((0,0,w,h))
    for i in range(len(points)):
        subdiv.insert(tuple(points[i]))
    TriangleList = subdiv.getTriangleList()
    return TriangleList

def get_borderpoints(img):
    h,w = img.shape[:2]
    h,w = h-1,w-1
    points = [[0,0],[w//2,0],[w,0],[w,h//2],[w,h],[w//2,h],[0,h],[0,h//2]]
    return np.array(points)

def get_masks(img,t):
    masks = np.zeros((len(t),img.shape[0],img.shape[1]), dtype=np.uint8)
    for i in range(len(t)):

        points = np.array([[t[i][0],t[i][1]],[t[i][2],t[i][3]],[t[i][4],t[i][5]]]).astype(np.int64)
        #print(points)
        masks[i] = cv2.fillConvexPoly(masks[i],points,(255))
        cv2.line(masks[i], tuple(points[0]), tuple(points[1]), (255), 3)
        cv2.line(masks[i], tuple(points[0]), tuple(points[2]), (255), 3)
        cv2.line(masks[i], tuple(points[1]), tuple(points[2]), (255), 3)

        #masks[i] = cv2.drawContours(masks[i], points, contourIdx=0, color=255, thickness=2)
    return masks

def changeTriangleList(t,point,move):
    t = t.astype(np.int64)
    for i in range(len(t)):
        if t[i][0]==point[0] and t[i][1]==point[1]:
            t[i][0],t[i][1] = t[i][0]+move[0],t[i][1]+move[1]
        elif t[i][2]==point[0] and t[i][3]==point[1]:
            t[i][2],t[i][3] = t[i][2]+move[0],t[i][3]+move[1]
        elif t[i][4]==point[0] and t[i][5]==point[1]:
            t[i][4],t[i][5] = t[i][4]+move[0],t[i][5]+move[1]
    return t

def replace_delaunay(img,Ms,masks):
    img_new  = np.zeros_like(img)
    h,w = img.shape[:2]
    for i in range(len(Ms)):
        # _img = img.copy()
        mask = cv2.merge([masks[i], masks[i], masks[i]])
        mask_inv = cv2.bitwise_not(mask)
        tmp = cv2.warpAffine(img,Ms[i],(w,h),borderMode = cv2.BORDER_REFLECT_101,flags=cv2.INTER_CUBIC)
        tmp = cv2.bitwise_and(mask,tmp)
        img_new = cv2.bitwise_and(mask_inv,img_new)
        img_new = cv2.add(tmp,img_new)
    return img_new

def get_nikemouth_landmark(src_landmark,alpha=1,aspect_ratio=1.0,mode = 'only_mouth'):

    nike = cv2.imread('./imgs/nike.png')
    landmark = face_recognition.face_landmarks(nike)[0]
    if mode == 'only_mouth':
        src_landmark = src_landmark[56:]
        nikemouth = np.array(landmark['top_lip']+landmark['bottom_lip'])
    else:
        src_landmark = src_landmark[25:]
        nikemouth = np.array(landmark['left_eyebrow']+landmark['right_eyebrow']+landmark['nose_bridge']+\
            landmark['nose_tip']+landmark['left_eye']+landmark['right_eye']+landmark['top_lip']+\
            landmark['bottom_lip'])

    # 中心置0
    nikemouth = nikemouth-[np.mean(nikemouth[:,0]),np.mean(nikemouth[:,1])]
    nikemouth[:,0] = nikemouth[:,0]*aspect_ratio
    # 获取嘴巴大小
    nikemouth_h = np.max(nikemouth[:,1])-np.min(nikemouth[:,1])
    nikemouth_w = np.max(nikemouth[:,0])-np.min(nikemouth[:,0])
    src_h = np.max(src_landmark[:,1])-np.min(src_landmark[:,1])
    src_w = np.max(src_landmark[:,0])-np.min(src_landmark[:,0])

    # 调整大小及位置
    beta = nikemouth_w/src_w
    nikemouth = alpha*nikemouth/beta+[np.mean(src_landmark[:,0]),np.mean(src_landmark[:,1])]
    return np.around(nikemouth,0)

def convert(face,size=1,intensity=1,aspect_ratio=1.0,ex_move=[0,0],mode='all_face'):

    h,w = face.shape[:2]
    landmark = face_recognition.face_landmarks(face)[0]
    # print(landmark)
    landmark_src = np.array(landmark['chin']+landmark['left_eyebrow']+landmark['right_eyebrow']+\
        landmark['nose_bridge']+landmark['nose_tip']+landmark['left_eye']+landmark['right_eye']+landmark['top_lip']+\
        landmark['bottom_lip'])

    landmark_src = np.concatenate((get_borderpoints(face), landmark_src), axis=0)
    TriangleList_src = delaunay(h,w,landmark_src)
    TriangleList_dst = TriangleList_src.copy()

    nikemouth_landmark = get_nikemouth_landmark(landmark_src,alpha=size,aspect_ratio=aspect_ratio,mode = mode)
    if mode == 'only_mouth':
        for i in range(24):
            move = ex_move+(nikemouth_landmark[i]-landmark_src[56+i])*intensity
            TriangleList_dst = changeTriangleList(TriangleList_dst, landmark_src[56+i],move)
    else:
        for i in range(80-25):
            move = ex_move+(nikemouth_landmark[i]-landmark_src[25+i])*intensity
            TriangleList_dst = changeTriangleList(TriangleList_dst, landmark_src[25+i],move)

    Ms = get_Ms(TriangleList_src,TriangleList_dst)
    masks = get_masks(face, TriangleList_dst)
    face_new = replace_delaunay(face, Ms, masks)

    return face_new

# # draw_delaunay(img, TriangleList, delaunary_color):

# cv2.namedWindow('image', cv2.WINDOW_NORMAL)
# cv2.imshow('image',face_new)
# cv2.waitKey(0)
# cv2.destroyAllWindows()


text:
# -*- coding: utf-8 -*-
# subnetworks.py
# author: Antoine Passemiers

__all__ = [
    'MultiLayerPerceptron',
    'Subnetwork',
    'StartSubnetwork',
    'TransitionSubnetwork',
    'EmissionSubnetwork',
    'StartMLP',
    'TransitionMLP',
    'EmissionMLP']

from archmm.ann.layers import *
from archmm.ann.model import Optimizer

import numpy as np
from abc import ABCMeta, abstractmethod
from operator import mul
from functools import reduce

class MultiLayerPerceptron:
    """ Multi-layer perceptron (MLP).

    Args:
        n_in (int):
            Number of inputs
        n_hidden (int):
            Number of hidden neurons
        n_out (int):
            Number of output neurons
        hidden_activation (str):
            Name of the activation function to be
            used by the hidden layer
        is_classifier (bool):
            Whether the MLP is a classifier. If classifier,
            a softmax activation function is applied on
            the output values.

    Attributes:
        layers (list):
            A list of :obj:`Layer` instances. The MLP behaves
            like a stack, by propagating the input from the first
            layer of the list to the last one. During backward pass,
            the signal is backpropagated from last layer of the
            list to the first one.
    """

    def __init__(self, n_in, n_hidden, n_out,
                 hidden_activation='sigmoid', is_classifier=True):
        self.n_in = n_in
        self.n_hidden = n_hidden
        self.n_out = n_out
        self.hidden_activation = hidden_activation
        self.is_classifier = is_classifier

        self.layers = list()
        self.layers.append(FullyConnected(n_in, n_hidden))
        self.layers.append(Activation(func=self.hidden_activation))
        self.layers.append(FullyConnected(n_hidden, n_out))
        if self.is_classifier:
            self.layers.append(Activation(func='softmax'))

        self.optimizer = Optimizer()

    def eval(self, X):
        """ Given inputs X, evaluate the output of the MLP.

        Args:
            X (:obj:`np.ndarray`):
                Array of shape (n_samples, n_features)

        Returns:
            :obj:`np.ndarray`:
                Array of shape (n_samples, output_dim)
        """
        out = X
        for layer in self.layers:
            out = layer.forward(out)
        return out

    def backpropagation(self, y_hat):
        signal = y_hat
        for layer in reversed(self.layers):
            signal, gradient = layer.backward(signal)
            if gradient is not None:
                self.optimizer.add_gradient_fragments(layer, gradient)
        self.optimizer.update()

class Subnetwork(metaclass=ABCMeta):

    @abstractmethod
    def eval(self, X):
        pass

class StartSubnetwork(Subnetwork):

    def __init__(self):
        pass

    @abstractmethod
    def train(self, gamma_c):
        pass

class TransitionSubnetwork(Subnetwork):

    def __init__(self):
        pass

    @abstractmethod
    def train(self, X_c, xi_j_c):
        pass

class EmissionSubnetwork(Subnetwork):

    def __init__(self):
        pass

    @abstractmethod
    def train(self, X_c, memory_j_c):
        pass

class StartMLP(StartSubnetwork, MultiLayerPerceptron):

    def __init__(self, *args, **kwargs):
        kwargs['is_classifier'] = True
        MultiLayerPerceptron.__init__(self, *args, **kwargs)

    def eval(self, X):
        return MultiLayerPerceptron.eval(self, X)

    def train(self, gamma_c):
        pass

class TransitionMLP(TransitionSubnetwork, MultiLayerPerceptron):

    def __init__(self, *args, **kwargs):
        kwargs['is_classifier'] = True
        MultiLayerPerceptron.__init__(self, *args, **kwargs)

    def eval(self, X):
        return MultiLayerPerceptron.eval(self, X)

    def train(self, X_c, xi_j_c):
        assert(X_c.shape[0] == xi_j_c.shape[0])
        indices = np.arange(len(X_c))
        np.random.shuffle(indices)
        indices = indices[:32] # TODO

        X_batch = X_c[indices]
        phi_batch = MultiLayerPerceptron.eval(self, X_batch)
        h_batch = xi_j_c[indices]

        MultiLayerPerceptron.backpropagation(self, - h_batch / phi_batch)

class EmissionMLP(EmissionSubnetwork, MultiLayerPerceptron):

    def __init__(self, *args, **kwargs):
        MultiLayerPerceptron.__init__(self, *args, **kwargs)

    def eval(self, X):
        return MultiLayerPerceptron.eval(self, X)

    def train(self, X_c, memory_j_c):
        assert(X_c.shape[0] == memory_j_c.shape[0])
        indices = np.arange(len(X_c))
        np.random.shuffle(indices)
        indices = indices[:32] # TODO

        X_batch = X_c[indices]
        eta_batch = MultiLayerPerceptron.eval(self, X_batch)
        memory_batch = memory_j_c[indices]

        MultiLayerPerceptron.backpropagation(self, (eta_batch.T * memory_batch).T)

max_stars_repo_name: AntoinePassemiers/ArchMM

max_issues_repo_name: AntoinePassemiers/ArchMM

max_forks_repo_name: AntoinePassemiers/ArchMM


text:
"""
 *
 * Copyright (c) 2021 Manuel Yves Galliker
 *               2021 Autonomous Systems Lab ETH Zurich
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name Data Driven Dynamics nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
"""

__author__ = "Manuel Yves Galliker"
__maintainer__ = "Manuel Yves Galliker"
__license__ = "BSD 3"

from src.models.rotor_models import TiltingRotorModel
import os
import numpy as np
import math

def test_rotor_thrust_prediction():
    rotor_config_dict = {"description": "test rotor",
                         "dataframe_name": "u0",
                         "rotor_type": "TiltingRotorModel",
                         "tilt_actuator_dataframe_name": "u_tilt",
                         "rotor_axis": [1, 0, 0],
                         "tilt_axis": [0, 1, 0],
                         "max_tilt_angle_deg": 90,
                         "turning_direction": 1,
                         "position": [0, 0, 0]}

    tilt_actuator_vec = np.array([0, 0.5, 1, 0, 0.5, 1])
    actuator_input_vec = np.array([0.5, 0.5, 0.5, 1, 1, 1])
    v_airspeed_mat = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0], [
                              0, 0, 0], [0, 0, 0], [0, 0, 0]])
    rotor = TiltingRotorModel(rotor_config_dict, actuator_input_vec, v_airspeed_mat,
                              tilt_actuator_vec)

    thrust_coef_list = [-1, 10]
    correct_force_prediction = np.array([[3.06250000,   0,            0],
                                         [2.16551452,   0,  -2.16551452],
                                         [0,            0,      -3.0625],
                                         [12.25,        0,            0],
                                         [8.66205807,   0,  -8.66205807],
                                         [0,            0,       -12.25]])
    X_forces, coef_dict, col_names = rotor.compute_actuator_force_matrix()
    print(np.linalg.norm(rotor.predict_thrust_force(
        thrust_coef_list) - correct_force_prediction))
    assert (np.linalg.norm(rotor.predict_thrust_force(
        thrust_coef_list) - correct_force_prediction) < 10e-8)

def test_local_airspeed_computation():
    rotor_config_dict = {"description": "test rotor",
                         "dataframe_name": "u0",
                         "rotor_type": "TiltingRotorModel",
                         "tilt_actuator_dataframe_name": "u_tilt",
                         "rotor_axis": [1, 0, 0],
                         "tilt_axis": [0, 1, 0],
                         "max_tilt_angle_deg": 90,
                         "turning_direction": 1,
                         "position": [0, 0, 0]}

    actuator_input_vec = np.array([0, 0, 0, 0, 0])
    tilt_actuator_vec = np.array([0, 0.5, 1, 0.5, 0])
    v_airspeed_mat = np.array(
        [[5, 0, 0], [5*math.sqrt(0.5), 0, -5*math.sqrt(0.5)], [0, 0, -5], [0, 0, -5], [0, 0, -5]])
    rotor = TiltingRotorModel(rotor_config_dict, actuator_input_vec, v_airspeed_mat,
                              tilt_actuator_vec)

    assert (np.linalg.norm(rotor.v_air_parallel_abs -
            np.array([5, 5, 5, 5/math.sqrt(2), 0])) < 10e-10)
    assert (np.linalg.norm(
        rotor.v_airspeed_perpendicular_to_rotor_axis[0, :] - np.array([0, 0, 0])) < 10e-10)
    assert (np.linalg.norm(
        rotor.v_airspeed_perpendicular_to_rotor_axis[1, :] - np.array([0, 0, 0])) < 10e-10)
    assert (np.linalg.norm(
        rotor.v_airspeed_perpendicular_to_rotor_axis[2, :] - np.array([0, 0, 0])) < 10e-10)
    assert (np.linalg.norm(
        rotor.v_airspeed_perpendicular_to_rotor_axis[3, :] - np.array([-2.5, 0, -2.5])) < 10e-10)
    assert (np.linalg.norm(
        rotor.v_airspeed_perpendicular_to_rotor_axis[4, :] - np.array([0, 0, -5])) < 10e-10)

def test_tilting_x_axis():
    rotor_config_dict = {"description": "test rotor",
                         "dataframe_name": "u0",
                         "rotor_type": "TiltingRotorModel",
                         "tilt_actuator_dataframe_name": "u_tilt",
                         "rotor_axis": [0, 1, 0],
                         "tilt_axis": [1, 0, 0],
                         "max_tilt_angle_deg": 180,
                         "turning_direction": 1,
                         "position": [0, 0, 0]}

    actuator_input_vec = np.array([0, 0, 0])
    tilt_actuator_vec = np.array([0, 0.5, 1])
    v_airspeed_mat = np.zeros((3, 3))
    rotor = TiltingRotorModel(rotor_config_dict, actuator_input_vec, v_airspeed_mat,
                              tilt_actuator_vec)
    print(rotor.rotor_axis_mat)
    assert (np.linalg.norm(
        rotor.rotor_axis_mat[0, :] - np.array([0, 1, 0])) < 10e-10)
    assert (np.linalg.norm(
        rotor.rotor_axis_mat[1, :] - np.array([0, 0, 1])) < 10e-10)
    assert (np.linalg.norm(
        rotor.rotor_axis_mat[2, :] - np.array([0, -1, 0])) < 10e-10)

def test_tilting_y_axis():
    rotor_config_dict = {"description": "test rotor",
                         "dataframe_name": "u0",
                         "rotor_type": "TiltingRotorModel",
                         "tilt_actuator_dataframe_name": "u_tilt",
                         "rotor_axis": [1, 0, 0],
                         "tilt_axis": [0, 1, 0],
                         "max_tilt_angle_deg": 90,
                         "turning_direction": 1,
                         "position": [0, 0, 0]}

    actuator_input_vec = np.array([0, 0, 0])
    tilt_actuator_vec = np.array([0, 0.5, 1])
    v_airspeed_mat = np.zeros((3, 3))
    rotor = TiltingRotorModel(rotor_config_dict, actuator_input_vec, v_airspeed_mat,
                              tilt_actuator_vec)
    print(rotor.rotor_axis_mat)
    assert (np.linalg.norm(
        rotor.rotor_axis_mat[0, :] - np.array([1, 0, 0])) < 10e-10)
    assert (np.linalg.norm(
        rotor.rotor_axis_mat[1, :] - np.array([math.sqrt(0.5), 0, -math.sqrt(0.5)])) < 10e-10)
    assert (np.linalg.norm(
        rotor.rotor_axis_mat[2, :] - np.array([0, 0, -1])) < 10e-10)

def test_tilting_z_axis():
    rotor_config_dict = {"description": "test rotor",
                         "dataframe_name": "u0",
                         "rotor_type": "TiltingRotorModel",
                         "tilt_actuator_dataframe_name": "u_tilt",
                         "rotor_axis": [0, 1, 0],
                         "tilt_axis": [0, 0, 1],
                         "max_tilt_angle_deg": 360,
                         "turning_direction": 1,
                         "position": [0, 0, 0]}

    actuator_input_vec = np.array([0, 0, 0])
    tilt_actuator_vec = np.array([0.25, 0.5, 0.75])
    v_airspeed_mat = np.zeros((3, 3))
    rotor = TiltingRotorModel(rotor_config_dict, actuator_input_vec, v_airspeed_mat,
                              tilt_actuator_vec)
    print(rotor.rotor_axis_mat)
    assert (np.linalg.norm(
        rotor.rotor_axis_mat[0, :] - np.array([-1, 0, 0])) < 10e-10)
    assert (np.linalg.norm(
        rotor.rotor_axis_mat[1, :] - np.array([0, -1, 0])) < 10e-10)
    assert (np.linalg.norm(
        rotor.rotor_axis_mat[2, :] - np.array([1, 0, 0])) < 10e-10)

# Run uas module using 'python3 -m tests.test_dynamics_model' for development and testing of the test.
if __name__ == "__main__":
    # set cwd to project directory when run as module
    cwd = os.getcwd()
    parent = os.path.join(cwd, os.pardir)
    des_cwd = os.path.join(parent, os.pardir)
    os.chdir(des_cwd)

    test_rotor_thrust_prediction()
    test_local_airspeed_computation()
    test_tilting_x_axis()
    test_tilting_y_axis()
    test_tilting_z_axis()

max_stars_repo_name: ethz-asl/data-driven-dynamics

max_issues_repo_name: ethz-asl/data-driven-dynamics

max_forks_repo_name: ethz-asl/data-driven-dynamics


text:
import tempfile

import numpy as np

from btk.catalog import CatsimCatalog
from btk.draw_blends import CatsimGenerator
from btk.measure import MeasureGenerator
from btk.measure import sep_measure
from btk.metrics import meas_ksb_ellipticity
from btk.metrics import MetricsGenerator
from btk.sampling_functions import DefaultSampling
from btk.survey import get_surveys
from btk.utils import load_all_results

def test_save():
    output_dir = tempfile.mkdtemp()
    catalog_name = "data/sample_input_catalog.fits"
    stamp_size = 24.0
    batch_size = 8
    catalog = CatsimCatalog.from_file(catalog_name)
    sampling_function = DefaultSampling(stamp_size=stamp_size)
    draw_blend_generator = CatsimGenerator(
        catalog,
        sampling_function,
        get_surveys("Rubin"),
        batch_size=batch_size,
        stamp_size=stamp_size,
        save_path=output_dir,
    )
    meas_generator = MeasureGenerator(sep_measure, draw_blend_generator, save_path=output_dir)
    metrics_generator = MetricsGenerator(
        meas_generator,
        target_meas={"ellipticity": meas_ksb_ellipticity},
        save_path=output_dir,
    )
    blend_results, measure_results, metrics_results = next(metrics_generator)
    blend_results2, measure_results2, metrics_results2 = load_all_results(
        output_dir, ["Rubin"], ["sep_measure"], batch_size
    )
    np.testing.assert_array_equal(
        blend_results["blend_images"], blend_results2["blend_images"]["Rubin"]
    )
    np.testing.assert_array_equal(
        measure_results["segmentation"]["sep_measure"][0],
        measure_results2["segmentation"]["sep_measure"][0],
    )
    np.testing.assert_array_equal(
        measure_results["deblended_images"]["sep_measure"][0],
        measure_results2["deblended_images"]["sep_measure"][0],
    )
    np.testing.assert_array_equal(
        metrics_results["galaxy_summary"]["sep_measure"]["distance_closest_galaxy"],
        metrics_results2["galaxy_summary"]["sep_measure"]["distance_closest_galaxy"],
    )

max_stars_repo_name: b-biswas/BlendingToolKit

max_issues_repo_name: b-biswas/BlendingToolKit

max_forks_repo_name: b-biswas/BlendingToolKit


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

"""UNIT TESTS FOR MKSTUFF

This module contains unit tests for the mkstuff module.

"""

from unittest import TestCase
import numpy.testing as npt
from ..mkstuff import *

class MathTestCase(TestCase):

    def setUp(self):

        self.a = np.array([[4, 2], [2, 4]])
        self.vector = np.ones((3))
        self.tensor = np.ones((3,2,4))
        self.zero = np.ones((4,4))
        self.zero[2,2] = 0

    def tearDown(self):

        self.a = None

    def test_corr_coeff(self):

        npt.assert_almost_equal(corr_coeff(self.a)[0, 1], 0.5,
                                err_msg='Incorrect correlation coefficient')

        npt.assert_raises(ValueError, corr_coeff, self.tensor)
        npt.assert_raises(ValueError, corr_coeff, self.vector)

        npt.assert_raises(ZeroDivisionError, corr_coeff, self.zero)

max_stars_repo_name: martinkilbinger/mkstuff

max_issues_repo_name: martinkilbinger/mkstuff

max_forks_repo_name: martinkilbinger/mkstuff


text:
import numpy as np
from molsysmt import puw
from molsysmt._private_tools.molecular_system import digest_molecular_system
from molsysmt._private_tools.frame_indices import digest_frame_indices
from .lib import geometry as libgeometry

def get_dihedral_angles(molecular_system, dihedral_angle=None, selection='all', quartets=None,
                        frame_indices='all', syntaxis='MolSysMT', pbc=False):

    from molsysmt.multitool import get

    molecular_system = digest_molecular_system(molecular_system)
    frame_indices = digest_frame_indices(frame_indices)

    if quartets is None:

        from molsysmt import covalent_dihedral_quartets

        quartets = covalent_dihedral_quartets(molecular_system, dihedral_angle=dihedral_angle, selection=selection, syntaxis=syntaxis)

    elif type(quartets) in [list,tuple]:
        quartets = np.array(quartets, dtype=int)
    elif type(quartets) is np.ndarray:
        pass
    else:
        raise ValueError

    shape = quartets.shape

    if len(shape)==1:
        if shape[0]==4:
            quartets=quartets.reshape([1,4])
        else:
            raise ValueError
    elif len(shape)==2:
        if shape[1]!=4:
            raise ValueError
    else:
        raise ValueError

    coordinates = get(molecular_system, target='system', frame_indices=frame_indices, coordinates=True)

    n_angles = quartets.shape[0]
    n_frames = coordinates.shape[0]
    n_atoms = coordinates.shape[1]

    if pbc:

        box, box_shape = get(molecular_system, target='system', frame_indices=frame_indices, coordinates=True, box=True, box_shape=True)
        if box_shape is None:
            raise ValueError("The system has no PBC box. The input argument 'pbc' can not be True.")
        orthogonal = 0

        if box_shape is None:
            orthogonal =1
        elif box_shape == 'cubic':
            orthogonal =1

    else:

        orthogonal = 1
        box = np.zeros([n_frames,3,3])*puw.unit('nm')

    box = np.asfortranarray(puw.get_value(box, to_unit='nm'), dtype='float64')
    coordinates = np.asfortranarray(puw.get_value(coordinates, to_unit='nm'), dtype='float64')

    angles = libgeometry.dihedral_angles(coordinates, box, orthogonal, int(pbc), quartets, n_angles, n_atoms, n_frames)
    angles = np.ascontiguousarray(angles)*puw.unit('degrees')

    return angles

def ramachandran_angles(molecular_system, selection='all', frame_indices='all', syntaxis='MolSysMT', pbc=False, plot=False):

    from molsysmt.covalent import covalent_dihedral_quartets

    molecular_system = digest_molecular_system(molecular_system)
    frame_indices = digest_frame_indices(frame_indices)

    phi_covalent_chain = covalent_dihedral_quartets(molecular_system, dihedral_angle='phi', selection=selection, syntaxis=syntaxis)
    psi_covalent_chain = covalent_dihedral_quartets(molecular_system, dihedral_angle='psi', selection=selection, syntaxis=syntaxis)

    n_chains = phi_covalent_chain.shape[0]

    angles = get_dihedral_angles(molecular_system, quartets=np.vstack([phi_covalent_chain, psi_covalent_chain]), frame_indices=frame_indices, pbc=pbc)

    return phi_covalent_chain, psi_covalent_chain, angles[:,:n_chains], angles[:,n_chains:]

def shift_dihedral_angles(molecular_system, quartets=None, angles_shifts=None, blocks=None,
                          frame_indices='all', pbc=False, in_place=True, engine='MolSysMT'):

    from molsysmt.multitool import get

    molecular_system = digest_molecular_system(molecular_system)
    frame_indices = digest_frame_indices(frame_indices)

    if type(quartets) in [list,tuple]:
        quartets = np.array(quartets, dtype=int)
    elif type(quartets) is np.ndarray:
        pass
    else:
        raise ValueError

    shape = quartets.shape

    if len(shape)==1:
        if shape[0]==4:
            quartets=quartets.reshape([1,4])
        else:
            raise ValueError
    elif len(shape)==2:
        if shape[1]!=4:
            raise ValueError
    else:
        raise ValueError

    n_quartets = quartets.shape[0]
    n_frames = get(molecular_system, target='system', frame_indices=frame_indices, n_frames=True)

    angles_shifts_units = puw.get_unit(angles_shifts)
    angles_shifts_value = puw.get_value(angles_shifts)

    if type(angles_shifts_value) in [float]:
        if (n_quartets==1 and n_frames==1):
            angles_shifts_value = np.array([[angles_shifts_value]], dtype=float)
        else:
            raise ValueError("angles_shifts do not match the number of frames and quartets")
    elif type(angles_shifts_value) in [list,tuple]:
        angles_shifts_value = np.array(angles_shifts_value, dtype=float)
    elif type(angles_shifts_value) is np.ndarray:
        pass
    else:
        raise ValueError

    shape = angles_shifts_value.shape

    if len(shape)==1:
        angles_shifts_value = angles_shifts_value.reshape([n_frames, n_quartets])

    angles_shifts=angles_shifts_value*angles_shifts_units

    angles = get_dihedral_angles(molecular_system, quartets=quartets, frame_indices=frame_indices, pbc=pbc)
    angles = angles + angles_shifts

    return set_dihedral_angles(molecular_system, quartets=quartets, angles=angles, blocks=None,
                               frame_indices=frame_indices, pbc=pbc, in_place=in_place, engine=engine)

def set_dihedral_angles(molecular_system, quartets=None, angles=None, blocks=None, frame_indices='all', pbc=False,
                        in_place=True, engine='MolSysMT'):

    from molsysmt.multitool import get, convert, set, copy

    molecular_system = digest_molecular_system(molecular_system)
    frame_indices = digest_frame_indices(frame_indices)

    if type(quartets) in [list,tuple]:
        quartets = np.array(quartets, dtype=int)
    elif type(quartets) is np.ndarray:
        pass
    else:
        raise ValueError

    shape = quartets.shape

    if len(shape)==1:
        if shape[0]==4:
            quartets=quartets.reshape([1,4])
        else:
            raise ValueError
    elif len(shape)==2:
        if shape[1]!=4:
            raise ValueError
    else:
        raise ValueError

    n_atoms = get(molecular_system, target='system', n_atoms=True)
    n_quartets = quartets.shape[0]
    n_frames = get(molecular_system, target='system', frame_indices=frame_indices, n_frames=True)

    angles_units = puw.get_unit(angles)
    angles_value = puw.get_value(angles)

    if type(angles_value) in [float]:
        if (n_quartets==1 and n_frames==1):
            angles_value = np.array([[angles_value]], dtype=float)
        else:
            raise ValueError("angles do not match the number of frames and quartets")
    elif type(angles_value) in [list,tuple]:
        angles_value = np.array(angles_value, dtype=float)
    elif type(angles_value) is np.ndarray:
        pass
    else:
        raise ValueError

    shape = angles_value.shape

    if len(shape)==1:
        angles_value = angles_value.reshape([n_frames, n_quartets])

    angles = angles_value*angles_units

    if engine=='MolSysMT':

        if blocks is None:

            from molsysmt.covalent import covalent_blocks

            blocks = []

            for quartet in quartets:

                tmp_blocks = covalent_blocks(molecular_system, remove_bonds=[quartet[1], quartet[2]])
                blocks.append(tmp_blocks)

        coordinates = get(molecular_system, target='system', frame_indices=frame_indices, coordinates=True)

        if pbc:

            box, box_shape = get(molecular_system, target='system', frame_indices=frame_indices, coordinates=True, box=True, box_shape=True)
            if box_shape is None:
                raise ValueError("The system has no PBC box. The input argument 'pbc' can not be True.")
            orthogonal = 0

            if box_shape is None:
                orthogonal =1
            elif box_shape == 'cubic':
                orthogonal =1

        else:

            orthogonal = 1
            box= np.zeros([n_frames,3,3])*puw.unit('nm')

        length_units = puw.unit(coordinates)
        box = np.asfortranarray(puw.get_value(box, to_unit=length_units), dtype='float64')
        coordinates = np.asfortranarray(puw.get_value(coordinates), dtype='float64')
        angles = np.asfortranarray(puw.get_value(angles), dtype='float64')

        aux_blocks = []
        aux_atoms_per_block = []

        for block in blocks:

            aux_atoms_per_block.append(len(block[1]))
            aux_blocks.append(list(block[1]))

        aux_blocks = np.ravel(aux_blocks)
        aux_atoms_per_block = np.array(aux_atoms_per_block, dtype=int)

        libgeometry.set_dihedral_angles(coordinates, box, orthogonal, int(pbc), quartets, angles,
                                         aux_blocks, aux_atoms_per_block, n_quartets, n_atoms, n_frames, aux_blocks.shape[0])

        coordinates=np.ascontiguousarray(coordinates)*length_units

        if in_place:
            return set(molecular_system, target='system', coordinates=coordinates, frame_indices=frame_indices)
        else:
            tmp_molecular_system = copy(molecular_system)
            set(tmp_molecular_system, target='system', coordinates=coordinates, frame_indices=frame_indices)
            return tmp_molecular_system

    else:

        raise NotImplementedError

max_stars_repo_name: uibcdf/MolSysMT

max_issues_repo_name: uibcdf/MolSysMT

max_forks_repo_name: uibcdf/MolSysMT


text:
import sys
import glob
import os
import numpy as np
import multiprocessing as mp
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

from scipy.interpolate import make_interp_spline,BSpline

from scipy.stats import zscore
from scipy.interpolate import UnivariateSpline

from skimage import transform

from PIL import Image
from imageio import imwrite

import yaml

def cal_hist(wgafile,num_images):
    print(wgafile)
    A = mpimg.imread(wgafile)
    hist,bins = np.histogram(A.ravel(),255,[1,255])
    return hist

def hist_match(source, template):
    """
    Adjust the pixel values of a grayscale image such that its histogram
    matches that of a target image

    Arguments:
    -----------
        source: np.ndarray
            Image to transform; the histogram is computed over the flattened
            array
        template: np.ndarray
            Template histogram; can have different dimensions to source
    Returns:
    -----------
        matched: np.ndarray
            The transformed output image
    """

    oldshape = source.shape
    source = source.ravel()
    # template = template.ravel()

    # get the set of unique pixel values and their corresponding indices and
    # counts
    s_values, bin_idx, s_counts = np.unique(source, return_inverse=True,
                                            return_counts=True)
    t_values = np.arange(0, template.shape[0])
    t_counts = template

    # take the cumsum of the counts and normalize by the number of pixels to
    # get the empirical cumulative distribution functions for the source and
    # template images (maps pixel value --> quantile)
    s_quantiles = np.cumsum(s_counts).astype(np.float64)
    s_quantiles /= s_quantiles[-1]
    t_quantiles = np.cumsum(t_counts).astype(np.float64)
    t_quantiles /= t_quantiles[-1]

    # interpolate linearly to find the pixel values in the template image
    # that correspond most closely to the quantiles in the source image
    interp_t_values = np.interp(s_quantiles, t_quantiles, t_values)

    return interp_t_values[bin_idx].reshape(oldshape)

def wga_norm_and_thresh(exp_folder, alignment_channel):

    # Assert alignment_channel is correct
    assert alignment_channel in [561, 488]

    print('Loading in analysis config')

    # Read in parameters from yaml file
    with open('./configs/bead_analysis_params.yml') as f:
            config = yaml.load(f)

    shape = (config['shape_h'], config['shape_w'])

    exp_folder = os.path.normpath(exp_folder) + "\\"
    storm_merged_path = exp_folder + 'unaligned\\storm_merged\\'
    conv_align_path = exp_folder + 'unaligned\\conv_{}\\'.format(str(alignment_channel))

    storm_merged_files = glob.glob(storm_merged_path + '*.tif')
    num_merged_images = len(storm_merged_files)

    wga_files = glob.glob(conv_align_path + '*.tif')
    num_wga_images = len(wga_files)

    assert num_merged_images == num_wga_images, "Number of images must match!"
    num_images = num_merged_images

    hy3c = np.zeros((num_images, 255))
    hy4c = np.zeros((num_images, 255))

    hy3cb = np.zeros((num_images, 255))
    hy4cb = np.zeros((num_images, 255))

    print('Calculating histograms!')

    print(num_images)
    for i in range(num_images):
        hy3c[i] = cal_hist(storm_merged_files[i], num_images) # storm_merged
        hy4c[i] = cal_hist(wga_files[i], num_images) # conv_561

    # Normalizing counts to 0-1 range
    hy3cb = hy3c / hy3c.sum(axis=1, keepdims=True)
    hy4cb = hy4c / hy4c.sum(axis=1, keepdims=True)

    chan = hy4cb
    varuse4 = np.zeros([num_images, 255])

    x_hist = np.arange(1,255)
    x_sections = np.arange(0, num_images)

    print('Thresholding!!')

    for i in range(255):
        zthresh = 3
        curr_param = chan[:, i] # Distribution of channel i values across all images

        mean = np.mean(curr_param, axis=0)
        sd = np.std(curr_param, axis=0)
        distance_from_mean = abs(chan[:, i] - mean)
        mask = distance_from_mean < zthresh * sd

        # Select which sections can be used for smooth interpolation
        currfitx = x_sections[mask]
        currfity = curr_param[mask]

        # currfitx = (currfitx - np.mean(currfitx)) / (np.std(currfitx) + 0.00001)
        # currfity = (currfity - np.mean(currfity)) / (np.std(currfity) + 0.00001)

        spl = UnivariateSpline(currfitx, currfity)
        spl.set_smoothing_factor(0.9)

        varuse4[:, i] = spl(np.arange(0,num_images))

    path4 = exp_folder + 'unaligned\\for_align\\'
    path4a = exp_folder + 'unaligned\\for_align_ds\\'

    print('Saving out new images!')

    if not os.path.exists(path4):
        os.mkdir(path4)
    if not os.path.exists(path4a):
        os.mkdir(path4a)

    for i in range(num_images):

        hgram4 = varuse4[i] / sum(varuse4[i]) # Normalize over the channels for each image

        # Read in the storm file
        A = mpimg.imread(wga_files[i])
        hist,bins = np.histogram(A.ravel(),256,[0,255])
        hist_cum = np.cumsum(hist)
        a = np.array(hist[0])
        b = hgram4*(sum(hist)-hist[0])
        hgram4a = np.concatenate((a,b),axis=None)

        out = hist_match(A, hgram4a)

        #Change: debug part commented out !!!!!!!!!!!!!!!!!!!
        #import pdb; pdb.set_trace()

        #out[A < 1] = 0

        out_align = out
        out_align_small = transform.rescale(out_align, 0.1)

        imwrite(path4 + wga_files[i].split('\\')[-1], out_align)
        imwrite(path4a + wga_files[i].split('\\')[-1], out_align_small)

    print('Done!')
    return True

max_stars_repo_name: SpeerLab/STORM-UI-

max_issues_repo_name: SpeerLab/STORM-UI

max_forks_repo_name: SpeerLab/STORM-UI

reason: import numpy,from scipy


text:
# -*- coding: utf-8 -*-
import numpy as np
np.random.seed(1337)
import numpy as np

from keras.models import Sequential
from keras.layers.embeddings import Embedding

import keras
from keras.layers.recurrent import LSTM
from keras.layers.core import Dense, Dropout
import matplotlib.pyplot as plt
from attention import SelfAttention
from ConfusionMetrics import precision,f1_score,recall
def train_lstm(p_n_symbols, p_embedding_weights, p_X_train, p_y_train, p_X_test, p_y_test,vocab_dim,input_length,batch_size,n_epoch):
    """
    模型建置
    """
    print ('build model...')
    model = Sequential()
    model.add(Embedding(output_dim=vocab_dim,
                        input_dim=p_n_symbols,
                        mask_zero=True,
                        weights=[p_embedding_weights],
                        input_length=input_length))
    model.add(Dropout(0.5))
    model.add(LSTM(output_dim=50))
    model.add(Dropout(0.5))
    model.add(SelfAttention())
    model.add(Dense(32,activation='tanh'))
    model.add(Dense(3,activation='softmax'))

    print ('processing...')
    #調整學習率
    adam=keras.optimizers.Adam(lr=0.00003)
    model.compile(loss='categorical_crossentropy',
                  optimizer=adam,
                  metrics=['accuracy', precision, recall, f1_score])

    print ("training...")
    history=model.fit(p_X_train, p_y_train, batch_size=batch_size, nb_epoch=n_epoch,
              validation_data=(p_X_test, p_y_test))

    #透過matplot繪圖顯示訓練過程
    print ("counting...")
    score, acc,prec,re,f1 = model.evaluate(p_X_test, p_y_test, batch_size=batch_size)
    print ('Test score:', score)
    print ('Test accuracy:', acc)
    print ('Test precision:', prec)
    print ('Test recall:', re)
    print ('Test f1-score:', f1)
    plt.subplot(211)
    plt.plot(history.history['acc'])
    plt.plot(history.history['val_acc'])
    plt.title('model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='best')

    plt.subplot(212)
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='best')
    plt.show()
    plt.savefig('lstm')
    #儲存模型
    model.save('lstm.h5')
    print('model is saved')


text:
import numpy as np
from numpy import random
from numpy import fft

numpy_pure_funcs = [
	np.arange, np.linspace, np.tile, np.ravel, np.exp, np.ceil, np.sqrt,
	np.log, np.reshape, np.conj, np.zeros, np.ones, np.asarray,
	np.multiply, np.squeeze, np.matmul,
	np.fft.fft, np.fft.fftshift, np.fft.ifft,
	np.random.randn
]

max_stars_repo_name: amphc/amphc

max_issues_repo_name: amphc/amphc

max_forks_repo_name: amphc/amphc

reason: import numpy,from numpy