text:
import numpy as np
from sklearn.neighbors import NearestNeighbors

class MLSFS(object):
    """
        Performs the Semi-supervised sparse feature selection algorithm based on multi-view
Laplacian regularization.

        Parameters
        ----------
        p : int
            Number of features to select.
        labled : int
            Amount of first labled features.
        ds : list of int
            List of amount of features to each view.
        neighbor : int
            Amount of nearest neighbors to use while building the graph.
        mu : float, optional
            Parameter in the objective function.
        lam : float, optional
            Regularization parameter in the objective function.
        gamma : float, optional
            Parameter in the objective function that keeps view weights positive.
            Gamma should be greater than one.
        max_iterations : int, optional
            Maximum amount of iterations to perform.
        epsilon : positive float, optional
            Specifies the needed residual between the target functions from consecutive iterations. If the residual
            is smaller than epsilon, the algorithm is considered to have converged.

        See Also
        --------
        https://www.sciencedirect.com/science/article/abs/pii/S0262885615000748

        Examples
        --------

    """

    def __init__(self, p, labled, ds, neighbors, mu=1, lam=1, gamma=1.1, max_iterations=1000, epsilon=1e-5):
        self.p = p
        self.labled = labled
        self.ds = ds
        self.mu = mu
        self.lam = lam
        if gamma <= 1:
            raise ValueError("Gamma should be greater than one, %d passed" % gamma)
        self.gamma = gamma
        self.neighbors = neighbors
        self.max_iterations = max_iterations
        if epsilon < 0:
            raise ValueError("Epsilon should be positive, %d passed" % epsilon)
        self.epsilon = epsilon

    def run(self, X, y=None):
        """
            Fits filter

            Parameters
            ----------
            X : numpy array, shape (n_samples, n_features)
                The training input samples.
            y : numpy array, shape (n_samples) or (n_samples, n_classes), optional
                The target values of one or zero to first labled samples and zeros to other

            Returns
            ----------
            G : array-like, shape (n_features, c)
                Projection matrix.

            See Also
            --------

            Examples
            --------
            >>> from ITMO_FS.filters.sparse.MLSFS import MLSFS
            >>> import numpy as np
            >>> X = np.array([[1, 2, 3, 3, 1],[2, 2, 3, 3, 2], [1, 3, 3, 1, 3],\
[3, 1, 3, 1, 4],[4, 4, 3, 1, 5]], dtype = np.integer)
            >>> y = np.array([1, 0, 1, 0, 0], dtype=np.integer)
            >>> model = MLSFS(p=3, labled=3, ds=[2, 3], neighbors=3)
            >>> weights = model.run(X)
            >>> model.feature_ranking(weights)
        """

        n_samples, n_features = X.shape
        n_views = len(self.ds)

        if len(y.shape) == 1:
            y = np.array([[y_el] for y_el in y])
        n_classes = y.shape[1]

        view_matrices = []
        cur_index = 0
        for i in range(n_views):
            matrix = []
            for x in X:
                matrix.append([x[j] for j in range(cur_index, cur_index + self.ds[i])])
            cur_index += self.ds[i]
            view_matrices.append(np.array(matrix).T)

        X = X.T

        S = [NearestNeighbors(n_neighbors=self.neighbors).fit(view.T).kneighbors_graph(view.T).toarray()
             for view in view_matrices]

        D = self.neighbors * np.eye(n_samples)
        Lv = [D - s for s in S]

        U = np.zeros((n_samples, n_samples))
        for i in range(n_samples):
            if i < self.labled:
                U[i, i] = 1e99
            else:
                U[i, i] = 1

        t = 0

        alphas = np.array([1 / n_views for i in range(n_views)])

        G = np.random.rand(n_features, n_classes)

        for iteration in range(self.max_iterations):
            L = np.sum(np.array([alphas[i] ** self.gamma * Lv[i] for i in range(n_views)]), 0)
            P = np.linalg.inv(L + U + self.mu * np.eye(n_samples))
            Q = U.dot(y) + self.mu * X.T.dot(G)
            F = P.dot(Q)
            A = X.dot(self.mu * np.eye(n_samples) - self.mu ** 2 * P.T).dot(X.T)
            B = self.mu * X.dot(P).dot(U).dot(y)
            W = np.zeros((n_features, n_features))
            for i in range(n_features):
                W[i, i] = (G[i].dot(G[i]) ** 1.5) / 4
            newG = np.linalg.inv(A + 4 * self.lam * W).dot(B)
            sum_for_alphas = sum([pow((1 / np.trace(F.T.dot(Lv[i]).dot(F))), (1 / (self.gamma - 1)))
                                  for i in range(n_views)])
            alphas = [pow((1 / np.trace(F.T.dot(Lv[i]).dot(F))), (1 / (self.gamma - 1))) / sum_for_alphas
                      for i in range(n_views)]

            diff = np.sum(np.abs(G - newG))
            print("diff ", diff)
            if (diff < self.epsilon):
                break
            G = newG

        return G

    def feature_ranking(self, G):
        """
            Choose p features.

            Parameters
            ----------
            G : array-like, shape (n_features, c)
                Feature weight matrix.

            p : amount of features to select

            Returns
            -------
            indices : array-like, shape(p)
                Indices of p selected features.
        """
        features_weights = np.sum(np.abs(G), 1)
        features_weights = [(i, features_weights[i]) for i in range(len(features_weights))]
        features_weights = sorted(features_weights, key=lambda el: el[1])
        return list(map(lambda el: el[0], features_weights[-self.p:]))


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

#################################################
## test_rklearn_perceptron_binary_mnist_CV.py  ##
#################################################

"""
This program is an application of the Perceptron algorithm to the MNIST dataset.
Since the Perceptron is only capable of doing binary classification, we'll just use it to
identify one digit 5. Thus, it will predict if a digit is 5 (Y) or not 5 (N).

All the data viz part is removed from this program. The interested user should confer to the
first program of the series for Data Viz details.

Usage:
$ cd <top_dir>
$ python rklearn/tests/test_rklearn_perceptron_binary_mnist_CV.py --conf=rklearn/tests/config/config-mnist.yaml

"""

#############
## Imports ##
#############

import unittest

import os
import sys
import time
import argparse
import yaml
import numpy as np ; np.random.seed(1) # ; np.set_printoptions(threshold=np.inf)

from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import accuracy_score

from rktools.loggers import init_logger

from rklearn.perceptron import Perceptron
from rklearn.open_data_loaders import load_mnist_sklearn

#############################
## binarize_mnist_labels() ##
#############################

def binarize_mnist_labels(y, pos_class):
    """
    Create the 2 classes in the whole set: pos_class set to 1 and others set to 0
    """
    return np.where(y == pos_class, 1, 0) # pos_class => 1, all others => 0

#############
## main()  ##
#############

def main(config):

    try:

        start = time.time()

        logger.info("")
        logger.info("===========================")
        logger.info("Load the MNIST data...")
        logger.info("===========================")
        logger.info("")

        os.makedirs(config["mnist_data"]["data_home"], exist_ok=True)

        start_prep = time.time()
        X,y = load_mnist_sklearn(data_home=config["mnist_data"]["data_home"], shuffle = True, logger = logger)
        end_prep = time.time()
        logger.info("Data loaded in {} seconds".format(end_prep - start_prep))

        logger.info("X.shape = {}, y.shape = {}".format(X.shape, y.shape))
        logger.debug("X.sample = \n{}, \ny.sample = {}".format(X[:5,], y[:5]))

        logger.info("")
        logger.info("===========================")
        logger.info("Labels binarization...")
        logger.info("===========================")
        logger.info("")

        # get the index of the first element = pos_class sample
        pos_class = config["mnist_data"]["pos_class"]
        logger.info("Positive class = {} for this use case. All over are negatives.".format(pos_class))

        # get the indices of one sample of pos_class, and another of a negative class

        try:
            index_pos_sample = y.tolist().index(pos_class)
        except:
            raise Exception("No element equal to {} (e.g. pos_class) was found in the target vector. Abort!".format(pos_class))

        # we can safely assume that the index before is a neg class

        if index_pos_sample > 0:
            index_neg_sample = index_pos_sample - 1
        else:
            index_neg_sample = index_pos_sample + 1

        logger.info("index_pos_sample = {} => label = {}".format(index_pos_sample, y[index_pos_sample]))
        logger.info("index_neg_sample = {} => label = {}".format(index_neg_sample, y[index_neg_sample]))

        # Labels binarization
        y_bin = binarize_mnist_labels(y, pos_class)

        logger.info("(after binarization) Labels samples, y_bin[:10] = {}".format(y_bin[:10]))

        logger.info("")
        logger.info("=============================")
        logger.info("Train-test split the data")
        logger.info("=============================")
        logger.info("")

        # train: 0 -> 60K - 1 / test: 60K -> 70K => ratio test vs train = 15%
        X_train, X_test, y_train, y_test = train_test_split(X, y_bin, train_size=60000, random_state=1, stratify=y_bin)

        logger.info("X_train.shape = {}, y_train.shape = {}".format(X_train.shape, y_train.shape))
        logger.info("X_test.shape = {}, y_test.shape = {}".format(X_test.shape, y_test.shape))

        logger.info("")
        logger.info("Check the stratifications in both train and test sets:")

        bin_count = np.bincount(y_bin)
        logger.info("Labels counts in y_bin = {} => percentage neg/pos = {:.2f}%".format(bin_count, ((bin_count[1]/bin_count[0])*100)))

        bin_count = np.bincount(y_train)
        logger.info("Labels counts in y_train = {} => percentage neg/pos = {:.2f}%".format(bin_count, ((bin_count[1]/bin_count[0])*100)))

        bin_count = np.bincount(y_test)
        logger.info("Labels counts in y_test = {} => percentage neg/pos = {:.2f}%".format(bin_count, ((bin_count[1]/bin_count[0])*100)))

        logger.info("")
        logger.info("=============================")
        logger.info("Data preparation")
        logger.info("=============================")
        logger.info("")

        # simple normalization (e.g. scale values between [0,1]
        X_prep = X_train/255
        y_prep = y_train

        # just a part of the data to speed the training
        # since the data is shuffled, we can safely slice it
        # X_prep = X_prep[:1000]
        # y_prep = y_prep[:1000]

        logger.info("")
        logger.info("==================================================")
        logger.info("Cross-validate the rklearn perceptron")
        logger.info("==================================================")
        logger.info("")

        # hyperparams
        lr = config["perceptron_hyper"]["lr"]
        n_epochs = config["perceptron_hyper"]["n_epochs"]
        k = config["perceptron_hyper"]["k_fold_cv"]

        logger.info("Hyperparameters:")
        logger.info("\tlr = {}".format(lr))
        logger.info("\tnb epochs = {}".format(n_epochs))
        logger.info("\tk (-folds CV) = {}".format(k))

        logger.info("")
        start_cv = time.time()
        ppn = Perceptron(lr=lr, n_epochs=n_epochs)
        cv_scores = cross_val_score(ppn, X_prep, y_prep, cv=k, scoring="accuracy")
        avg_score = np.mean(cv_scores)
        end_cv = time.time()
        logger.info("")
        logger.info("K-fold CV duration = {} secs.".format(end_cv - start_cv))

        logger.info("")
        logger.info("===========================")
        logger.info("Training CV Accuracy")
        logger.info("===========================")
        logger.info("")
        logger.info("Cross-val Classification Accuracy, CV = {}: {}".format(k, cv_scores))
        logger.info("=> Mean accuracy = {:.3f} ({:.2f}%) ".format(avg_score, (avg_score * 100)))

        logger.info("")
        logger.info("===========================")
        logger.info("Testing Accuracy")
        logger.info("===========================")
        logger.info("")

        logger.info("Fitting the model...")
        logger.info("")
        # fit before (indeed, CV has trained a clone of ppn, not ppn)
        start_fit = time.time()
        ppn.fit(X_prep, y_prep)
        end_fit = time.time()
        logger.info("")
        logger.info("Done done in {} seconds".format(end_fit - start_fit))
        logger.info("")

        y_pred = ppn.predict(X_test)
        acc_score = accuracy_score(y_test, y_pred)
        logger.info("Misclassified examples: {} on {} samples ".format((y_test != y_pred).sum(), len(y_test)))
        logger.info("Classification Accuracy: {:.3f} ({:.2f}%) ".format(acc_score, (acc_score * 100)))

        end = time.time()
        logger.info("Total duration = {} secs.".format(end - start))

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        logger.error("error msg = {}, error type = {}, error file = {}, error line = {}".format(e, exc_type, fname, exc_tb.tb_lineno))

###################
## parse_args()  ##
###################

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--conf", help="Path to the YAML configuration file", required=True,)
    ns, args = parser.parse_known_args(namespace=unittest)
    return ns, sys.argv[:1] + args

###############
## __main__  ##
###############

if __name__ == '__main__':

    logger = None
    FLAGS = None
    config = None

    # Parse cmd line arguments
    FLAGS, argv = parse_args()
    sys.argv[:] = argv

    with open(FLAGS.conf, 'r') as ymlfile:
        config = yaml.load(ymlfile, Loader=yaml.FullLoader)

    assert(config is not None)

    logger = init_logger(name="test_mnist_perceptron", config = config)

    logger.info("")
    logger.info("#############################################")
    logger.info("## test_rklearn_perceptron_binary_mnist.py ##")
    logger.info("#############################################")
    logger.info("")

    main(config)

ext: py

max_stars_repo_name: rejux/rklearn-lib

max_issues_repo_name: rejux/rklearn-lib

max_forks_repo_name: rejux/rklearn-lib

reason: import numpy


text:
# -*- coding: utf-8 -*-
""" RLBook.Chapter8.TicTacToe

Tic Tac Toe game implementation

"""
from itertools import cycle

import numpy as np

from RLBook.Chapter8.DefaultPlayers import DEFAULT_PLAYERS

class Game:
    """ TicTacToe game implementation to be used by Monte Carlo Tree Search

        https://en.wikipedia.org/wiki/Tic-tac-toe

    """
    DICTIONARY = {(0, 0): 0,
                  (0, 1): 1,
                  (0, 2): 2,
                  (1, 0): 3,
                  (1, 1): 4,
                  (1, 2): 5,
                  (2, 0): 6,
                  (2, 1): 7,
                  (2, 2): 8}
    INDEX = {1: 0,
             0: 1}

    players = DEFAULT_PLAYERS

    def __init__(self, board_size=3, players=None, using_nn=None, nn_player=0):
        """

            :param board_size:          3x3 by Default will be the Board size used
            :param players:             Optional: Pass
            :param using_nn:            Flag to indicate if a Neural Network is being utilised
            :param nn_player:           Identification of which agent is the Neural Network

        """
        # Game attributes
        self.board_size = board_size
        self.state = np.zeros((board_size, board_size), dtype=int)
        self.last_play = None
        self.sums = np.array([])

        # players attributes
        if players is not None:
            self.players = players

        self.players_values = list([p.value for p in self.players])
        self.players_gen = cycle(self.players)
        self.current_player = next(self.players_gen)
        self.history = [(self.state.copy(), None, self.current_player.value, None)]

        # Using a Neural Network
        self.nn_player = nn_player
        self.using_nn = using_nn

    def __repr__(self):
        return "< TicTacToe > "

    def __str__(self):
        return "< TicTacToe > "

    def legal_plays(self):
        """ Takes a sequence of game states representing the full game history

            :return:        the list of moves tuples that are legal to play for the current player

        """
        legal_plays = []
        if self.winner is None:
            free_spaces = np.isin(self.state, self.players_values, invert=True)
            legal_plays = np.argwhere(free_spaces)

            # convert numpy array to list of tuples
            legal_plays = list(map(tuple, legal_plays))

        return legal_plays

    @property
    def winner(self):
        """ Return the winner player. If game is tied, return None

            :return:        Player or None

        """
        for player in self.players:
            # one axis is full of this player plays (= win)
            if self.board_size * player.value in self.sums:
                return player

        # no winner found
        return None

    def show_board(self, state_number=-1, return_string=False):
        """ Display the game board

            :param state_number:        the state to show
            :param return_string:       whether to return a string or to print it
            :return:                    board representation as a string or nothing

        """
        # creates the string representation of the game
        lines = []
        no_player_display = '.'
        for line in self.history[state_number][0]:
            elements = []
            for element in line:
                if element in self.players_values:
                    for player in self.players:
                        if element == player.value:
                            elements.append(player.display)
                else:
                    elements.append(no_player_display)

            lines.append('|'.join(elements))
        board_representation = '\n'.join(lines)

        if return_string:
            return board_representation
        else:
            print(board_representation)

    def play(self, move=None, action_prob=1):
        """ Play a move

            :param move:            selected move to play. If None it is chosen randomly from legal plays
            :param action_prob:     Action probabilities from the Agent

        """
        legal_plays = self.legal_plays()

        # If input move is provided check that it is legal
        if move is not None:
            if move in legal_plays:
                selected_move = move
            else:
                raise ValueError('Selected move is illegal')
        # Select a move randomly
        else:
            selected_move = legal_plays[np.random.choice(len(legal_plays), 1)[0]]

        # Updates states and players info
        self.state[selected_move] = self.current_player.value

        # Copy() needed to avoid appending a reference
        # noinspection PyTypeChecker
        self.history.append((self.state.copy(), self.translate(selected_move), self.current_player.value, action_prob))
        self.current_player = next(self.players_gen)
        self.last_play = selected_move

        # Updates sums that are used to check for winner
        self.sums = np.concatenate(
            (np.sum(self.state, axis=0),  # vertical
             np.sum(self.state, axis=1),  # horizontal
             np.array([np.sum(np.diag(self.state)),  # diagonal
                       np.sum(np.diag(self.state[::-1]))])))

    def translate(self, position):
        """ Translate tuple to Index

            :param position:    Tuple of the Position
            :return:            Index in the Array (0-8)

        """
        return self.DICTIONARY.get(position)

    def reset(self):
        # Game attributes
        self.state = np.zeros((self.board_size, self.board_size), dtype=int)
        self.history = [self.state.copy()]  # copy() needed to avoid appending a reference
        self.last_play = None
        self.sums = np.array([])

    @property
    def nn_index(self):
        """ Get the Neural Network Players Coin choice and index in the Player

            :return:        NN Player, Index in List of Players, Player

        """
        if not self.using_nn:
            return None
        else:
            return self.players[self.nn_player], self.nn_player

    @property
    def player(self):
        return self.current_player

    @property
    def competing_player(self):
        return self.players[self.INDEX[self.nn_player]]


text:
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
from __future__ import absolute_import
import unittest
import numpy as np
from audioled import effects

class Test_Effects(unittest.TestCase):
    def test_effectDoesntProcessNullBuffers(self):
        effect = effects.Mirror()
        effect.process()
        self.assertIsNone(effect._inputBuffer)
    # Disabled because implementation has changed and test is out of scope for now
    #
    # def test_mirrorEffect(self):
    #     n=3
    #     rgb = np.linspace(0,n,n) * np.array([[1.0],[2.0],[3.0]])
    #     print(rgb)
    #     mapMask = np.array([[[0,i] for i in range(0,n)],
    #                         [[1,i] for i in range(0,n)],
    #                         [[2,i] for i in range(0,n)]],dtype=np.int64)
    #     print(mapMask)
    #     # revert
    #     mapMask = mapMask[:,::-1,:]
    #     # revert again
    #     mapMask = mapMask[:,::-1,:]
    #     print(mapMask)
    #     mapped = rgb[mapMask[:,:,0], mapMask[:,:,1]]
    #     print(mapped)
    #     np.testing.assert_array_equal(mapped, rgb)

    #     mirror = effects.Mirror(mirror_lower = True, recursion = 0)
    #     mirror._inputBuffer = []
    #     mirror._outputBuffer = []
    #     mirror._inputBuffer.append((np.ones(4) * np.array([[0],[0],[0]])))
    #     mirror.update(0)
    #     mirror._outputBuffer.append((np.ones(4) * np.array([[0],[0],[0]])))
    #     mirror.process()
    #     print(mirror._mirrorLower)
    #     np.testing.assert_array_equal(mirror._mirrorLower[0,:,1],np.array([0,1,1,0]))

    #     mirror = effects.Mirror(mirror_lower = True,recursion = 1)
    #     np.testing.assert_array_equal(mirror._mirrorLower[0,:,1],np.array([0,1,1,0,0,1,1,0]))

    #     mirror = effects.Mirror(mirror_lower = True,recursion = 2)
    #     np.testing.assert_array_equal(mirror._mirrorLower[0,:,1],np.array([0,1,1,0,0,1,1,0,3, 2, 2, 3, 3, 2, 2, 3]))


text:
import keras
import cv2
import math
import os
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
from snsdl.evaluation import Eval

"""
For a big dataset that doesn't fit in the memory, you need to train the model
using a generator.

This code is an example of using the Keras ImageDataGenerator to feed the training
processes with batches of images.

At the end, some reports are saved in the output directory.
"""

# Configuration
imageW = 64
imageH = 64
batch_size = 32
epochs = 2

idg = ImageDataGenerator(rescale=1. / 255)

train_generator = idg.flow_from_directory(
    directory="/tmp/dataset/output/train",
    target_size=(imageH, imageW),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    shuffle=True,
    seed=42
)

test_generator = idg.flow_from_directory(
    directory="/tmp/dataset/output/test",
    target_size=(imageH, imageW),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    shuffle=True,
    seed=42
)

val_generator = idg.flow_from_directory(
    directory="/tmp/dataset/output/val",
    target_size=(imageH, imageW),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    shuffle=True,
    seed=42
)

# Model parameters
input_shape = (imageH, imageW, 3)
num_classes = train_generator.num_classes

# CNN Model
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

# Compile model
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])

# Train model on dataset
history = model.fit_generator(
            generator=train_generator,
            validation_data=val_generator,
            use_multiprocessing=False,
            workers=1,
            epochs=epochs)

# Save model
os.makedirs('saved_models', exist_ok=True)
model.save('saved_models/cnn_example.model')

# Predict new samples
steps = math.ceil(test_generator.n / batch_size)
predict = model.predict_generator(test_generator, steps=steps)

# Predicted labels
labels = test_generator.class_indices
labels = dict((v,k) for k,v in labels.items())
y_predict = [labels[k] for k in list(np.argmax(predict, axis=-1))]

# GT labels
y_true = [labels[k] for k in test_generator.classes]

# Evaluate results
class_names = sorted(test_generator.class_indices.items(), key=lambda kv: kv[1])
class_names = [item[0] for item in class_names]
Eval.plot_history(history, png_output='output/', show=False)
Eval.full_multiclass_report(y_true, y_predict, class_names, png_output='output/', show=False)

max_stars_repo_name: kleysonr/snsdl

max_issues_repo_name: kleysonr/snsdl

max_forks_repo_name: kleysonr/snsdl


text:
import numpy as np
import math
import scipy.io as sio

def scaleAwayParameters(unscaled_params, datapoints):
    '''
    :param unscaled_params:
    :param datapoints:
    :return:
    '''
    assert type(unscaled_params) is np.ndarray
    assert unscaled_params.ndim == 2
    assert unscaled_params.shape[1] == 3
    quad_ids = np.unique(unscaled_params[:, 0])

    scaledParams = unscaled_params[
                   (unscaled_params[:, 2] >= 0) * (unscaled_params[:, 1] >= 0) * (unscaled_params[:, 1] <= 1) * (
                       unscaled_params[:, 2] <= 1), :]
    throwndata = datapoints[(unscaled_params[:, 2] >= 0) * (unscaled_params[:, 1] >= 0) * (
        unscaled_params[:, 1] <= 1) * (unscaled_params[:, 2] <= 1), :]

    for i in range(len(quad_ids)):
        if not (scaledParams[:,0] == quad_ids[i]).any():
            print "++++++++++++++++++++coarse quad without fine verts detected+++++++++++++++++++++++++++++"
            continue
        print "quad %d of %d" % (i,len(quad_ids))
        umax = np.max(scaledParams[(scaledParams[:, 0] == quad_ids[i]), 1])
        umin = np.min(scaledParams[(scaledParams[:, 0] == quad_ids[i]), 1])
        vmax = np.max(scaledParams[(scaledParams[:, 0] == quad_ids[i]), 2])
        vmin = np.min(scaledParams[(scaledParams[:, 0] == quad_ids[i]), 2])

        uscale = umax - umin
        vscale = vmax - vmin

        if (uscale):
            scaledParams[(scaledParams[:, 0] == quad_ids[i]), 1] = (scaledParams[(
                                                                                scaledParams[:, 0] == quad_ids[
                                                                                i]), 1] - umin) / uscale

        if (vscale):
            scaledParams[(scaledParams[:, 0] == quad_ids[i]), 2] = (scaledParams[(
                                                                                scaledParams[:, 0] == quad_ids[
                                                                                i]), 2] - vmin) / vscale

    return scaledParams, throwndata

max_stars_repo_name: mlaradji/CADO

max_issues_repo_name: mlaradji/CADO

max_forks_repo_name: mlaradji/CADO

reason: import numpy,import scipy


text:
#!/usr/bin/env python3

from typing import List

import numpy as np

from .interp import linear_interp

def interp2d(
    Q: List[np.ndarray],
    dy: float,
    dx: float,
    mode: str = "bilinear",
) -> np.ndarray:
    """Naive Interpolation
    (y,x): target pixel
    mode: interpolation mode
    """
    q00, q10, q01, q11 = Q
    if mode == "bilinear":
        f0 = linear_interp(q00, q01, dx, 1)
        f1 = linear_interp(q10, q11, dx, 1)
        return linear_interp(f0, f1, dy, 1)
    else:
        return NotImplementedError

def grid_sample(
    img: np.ndarray,
    grid: np.ndarray,
    mode: str = "bilinear",
) -> np.ndarray:
    """Naive grid sample algorithm"""
    channels, h_in, w_in = img.shape
    _, h_out, w_out = grid.shape

    # Image conversion values
    if img.dtype == np.uint8:
        _min = 0
        _max = 255
        _dtype = np.uint8
    elif img.dtype == np.float64:
        _min = 0.0
        _max = 1.0
        _dtype = np.float64
    else:
        raise ValueError("{} is not supported".format(img.dtype))

    # Initialize output image
    out = np.zeros((channels, h_out, w_out), dtype=_dtype)

    if mode == "bilinear":
        min_grid = np.floor(grid).astype(np.uint64)
        # NOTE: uint8 convertion causes truncation, so use uint64
        max_grid = min_grid + 1
        d_grid = grid - min_grid

        max_grid[0, :, :] = np.where(
            max_grid[0, :, :] >= h_in,
            max_grid[0, :, :] - h_in,
            max_grid[0, :, :],
        )
        max_grid[1, :, :] = np.where(
            max_grid[1, :, :] >= w_in,
            max_grid[1, :, :] - w_in,
            max_grid[1, :, :],
        )

        for y in range(h_out):
            for x in range(w_out):
                dy, dx = d_grid[:, y, x]
                y0, x0 = min_grid[:, y, x]
                y1, x1 = max_grid[:, y, x]

                q00 = img[:, y0, x0]
                q10 = img[:, y1, x0]
                q01 = img[:, y0, x1]
                q11 = img[:, y1, x1]

                out[:, y, x] = interp2d([q00, q10, q01, q11], dy, dx, mode=mode)

    elif mode == "nearest":
        round_grid = np.rint(grid).astype(np.uint64)

        for y in range(h_out):
            for x in range(w_out):
                _y, _x = round_grid[:, y, x]
                out[:, y, x] = img[:, _y, _x]

    else:
        raise ValueError("{} is not available".format(mode))

    out = np.where(out >= _max, _max, out)
    out = np.where(out < _min, _min, out)
    return out.astype(_dtype)


text:
import sys, os
sys.path.append(os.pardir)
from dataset.mnist import load_mnist
from PIL import Image

import numpy as np

(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False)

print(x_train.shape)
print(t_train.shape)
print(x_test.shape)
print(t_test.shape)

def img_show(img):
    pil_img = Image.fromarray(np.uint8(img))
    pil_img.show()

img = x_train[0]
label = t_train[0]
print(label)

print(img.shape)
img = img.reshape(28, 28)
print(img.shape)

img_show(img)

max_stars_repo_name: ijufumi/demo-python

max_issues_repo_name: ijufumi/demo-python

max_forks_repo_name: ijufumi/demo-python


text:
# -*- coding: utf-8 -*-
import numpy as np
from matplotlib import pyplot as plt
from anaflow import theis

time = np.linspace(10, 200)
rad = [1, 5]

# Q(t) = Q * characteristic([0, T])
lap_kwargs = {"cond": 3, "cond_kw": {"a": 100}}
head = theis(
    time=time,
    rad=rad,
    storage=1e-4,
    transmissivity=1e-4,
    rate=-1e-4,
    lap_kwargs=lap_kwargs,
)

for i, step in enumerate(rad):
    plt.plot(time, head[:, i], label="Theis(r={})".format(step))

plt.title("The Stehfest algorithm is not suitable for this!")
plt.legend()
plt.tight_layout()
plt.show()

max_stars_repo_name: JarnoHerr/AnaFlow

max_issues_repo_name: JarnoHerr/AnaFlow

max_forks_repo_name: JarnoHerr/AnaFlow


text:
'''
Defines the ParameterizedUnitaryDefinitions class.
'''
import numpy as np

from .unitary_definitions import UnitaryDefinitions
from .parameterized_unitary_parameter import ParameterizedUnitaryParameter
from .parameterized_unitary import ParameterizedUnitary

class ParameterizedUnitaryDefinitions:
    '''
    Provides methods to create several commonly-used
    parameterized unitaries.
    '''
    @staticmethod
    def rotation_xy() -> ParameterizedUnitary:
        '''
        Creates a parameterized rotation around an axis in the x-y plane with
        parameters theta (rotation angle) and phi (polar angle defining
        the axis of rotation).

        :return: The parameterized unitary object.
        :rtype: ParameterizedUnitary
        '''
        qubit_dimension = 2

        def rotation_matrix(
            theta: float,
            phi: float
        ) -> np.ndarray:
            return UnitaryDefinitions.rphi(theta, phi).get_matrix()

        min_value = 0
        max_value = 2 * np.pi
        parameters = [ParameterizedUnitaryParameter(
                      "theta", min_value, max_value, is_angle=True),
                      ParameterizedUnitaryParameter(
                      "phi", min_value, max_value, is_angle=True)]
        operation_name = "Rxy"
        return ParameterizedUnitary(
            qubit_dimension, rotation_matrix, parameters, operation_name)

    @staticmethod
    def rotation_xyz() -> ParameterizedUnitary:
        '''
        Creates a parameterized rotation around an arbitrary axis, where the
        rotation is defined by parameters alpha, beta, and gamma.

        :return: The parameterized unitary object.
        :rtype: ParameterizedUnitary
        '''
        qubit_dimension = 2

        def rotation_matrix(
            alpha: float,
            beta: float,
            gamma: float
        ) -> np.ndarray:
            return np.array(
                [[np.cos(beta / 2) * np.exp(-1j * (alpha + gamma) / 2),
                    -np.sin(beta / 2) * np.exp(-1j * (alpha - gamma) / 2)],
                 [np.sin(beta / 2) * np.exp(1j * (alpha - gamma) / 2),
                    np.cos(beta / 2) * np.exp(1j * (alpha + gamma) / 2)]])

        min_value = 0
        max_value = 2 * np.pi
        parameters = [ParameterizedUnitaryParameter(
                      "alpha", min_value, max_value, is_angle=True),
                      ParameterizedUnitaryParameter(
                      "beta", min_value, max_value, is_angle=True),
                      ParameterizedUnitaryParameter(
                      "gamma", min_value, max_value, is_angle=True)]
        operation_name = "Rxyz"
        return ParameterizedUnitary(
            qubit_dimension, rotation_matrix, parameters, operation_name)

    @staticmethod
    def xx() -> ParameterizedUnitary:
        '''
        Creates a parameterized two-qubit XX gate, where the rotation
        angle is defined by the parameter theta. A theta value of pi/4
        produces the traditional XX or Molmer-Sorensen gate.

        :return: The parameterized unitary object.
        :rtype: ParameterizedUnitary
        '''
        qubit_dimension = 2

        def xx_matrix(
            theta: float
        ) -> ParameterizedUnitary:
            return UnitaryDefinitions.xx(theta).get_matrix()

        min_value = 0
        max_value = 2 * np.pi
        parameters = [ParameterizedUnitaryParameter(
                      "theta", min_value, max_value, is_angle=True)]
        operation_name = "XX"
        return ParameterizedUnitary(
            qubit_dimension ** 2, xx_matrix, parameters, operation_name)

    @staticmethod
    def gms(
        num_qubits: int
    ) -> ParameterizedUnitary:
        '''
        Creates a parameterized n-qubit global Molmer-Sorensen gate,
        where the rotation angle is defined by the parameter theta.
        A theta value of pi/4 produces the traditional n-qubit global
        Molmer-Sorensen maximally-entangling gate.

        :param num_qubits: The number of qubits to use.
        :type num_qubits: int
        :return: The parameterized unitary object.
        :rtype: ParameterizedUnitary
        '''
        qubit_dimension = 2

        def gms_matrix(
            theta: float
        ) -> np.ndarray:
            return UnitaryDefinitions.gms(num_qubits, theta).get_matrix()

        min_value = 0
        max_value = 2 * np.pi
        parameters = [ParameterizedUnitaryParameter(
                      "theta", min_value, max_value, is_angle=True)]
        operation_name = f"GMS{num_qubits}"
        return ParameterizedUnitary(
            qubit_dimension ** num_qubits, gms_matrix,
            parameters, operation_name)

    @staticmethod
    def time_evolution(
        h_matrix: np.ndarray,
        t_min: float,
        t_max: float,
        h_suffix: str = ""
    ) -> ParameterizedUnitary:
        '''
        Creates a parameterized time-evolution unitary
        for the specified Hamiltonian matrix, where the
        parameter is the time for which to evolve the system.

        :param h_matrix: The Hamiltonian matrix to use for time evolution.
        :type h_matrix: np.ndarray
        :param t_min: The minimum allowed value for the time evolution
            parameter. This may be negative if time-reversal is allowed.
        :type t_min: float
        :param t_max: The maximum allowed value for the time evolution
            parameter.
        :type t_max: float
        :param h_suffix: A suffix used for display purposes to identify this
            Hamiltonian, defaults to "".
        :type h_suffix: str, optional
        :return: The parameterized unitary object.
        :rtype: ParameterizedUnitary
        '''
        assert isinstance(h_matrix, np.ndarray)
        assert np.allclose(h_matrix, h_matrix.T.conj())
        assert t_min <= t_max

        dimension = h_matrix.shape[0]

        def u_matrix(
            t: float
        ) -> np.ndarray:
            return UnitaryDefinitions.time_evolution(
                h_matrix, t, h_suffix).get_matrix()

        min_value = t_min
        max_value = t_max
        parameters = [ParameterizedUnitaryParameter(
                      "t", min_value, max_value, is_angle=False)]
        operation_name = "H" + h_suffix
        return ParameterizedUnitary(
            dimension, u_matrix, parameters, operation_name)

max_stars_repo_name: rmshaffer/stoq-compiler

max_issues_repo_name: rmshaffer/stoq-compiler

max_forks_repo_name: rmshaffer/stoq-compiler


text:
from circular_buffer_numpy import __version__
from circular_buffer_numpy.circular_buffer import CircularBuffer

from numpy import random, asarray, where, savetxt
from matplotlib import pyplot as plt
import timeit

print('circular buffer numpy version: {}'.format(__version__))
data_dim = 1000
buffer = CircularBuffer(shape=(1000000, data_dim))
data = random.randint(2**16, size=(1000, data_dim))

def write():
    global buffer
    buffer.append(data)

def read():
    global buffer
    data = buffer.get_last_N(N)
    return data

header = ['N', 'data_dim', 'number', 'write time', 'read time']
number_lst = [1000000, 100000, 10000, 1000, 100]
N_lst = [1, 10, 100, 1000, 10000]
data_dim_lst = [10, 100, 1000]
t_lst = []
for data_dim in data_dim_lst:
    buffer = CircularBuffer(shape=(1000000, data_dim))
    for N in N_lst:
        number = number_lst[N_lst.index(N)]
        data = random.randint(2**16, size=(N, data_dim))
        t_write = timeit.timeit(write, number=number)
        t_read = timeit.timeit(read, number=number)
        t_lst.append([N, data_dim, number, t_write/number, t_read/number])
        print('{} per write and {} per read of {}x{} array'.format(t_write/number, t_read/number, N, data_dim))
    del buffer

t_arr = asarray(t_lst)
fig = plt.figure()

plt.loglog(t_arr[where(t_arr[:, 1] == 10), 0], t_arr[where(t_arr[:, 1] == 10), 3], '-or')
plt.loglog(t_arr[where(t_arr[:, 1] == 100), 0], t_arr[where(t_arr[:, 1] == 100), 3], '-og')
plt.loglog(t_arr[where(t_arr[:, 1] == 1000), 0], t_arr[where(t_arr[:, 1] == 1000), 3], '-ob')
plt.xlabel('N, circular dimension length')
plt.ylabel('time to write, seconds')
title = 'Time to write vs data dimensions (length x width) \n'
title += 'where width equal to red: 10; green: 100; blue: 1000'
plt.title(title)
fig.savefig("write-benchmarks-{}.jpg".format(__version__), bbox_inches='tight')

fig = plt.figure()
from matplotlib import pyplot as plt
plt.loglog(t_arr[where(t_arr[:, 1] == 10), 0], t_arr[where(t_arr[:, 1] == 10), 4], '-or')
plt.loglog(t_arr[where(t_arr[:, 1] == 100), 0], t_arr[where(t_arr[:, 1] == 100), 4], '-og')
plt.loglog(t_arr[where(t_arr[:, 1] == 1000), 0], t_arr[where(t_arr[:, 1] == 1000), 4], '-ob')
plt.xlabel('N, circular dimension length')
plt.ylabel('time to read, seconds')
title = 'Time to write vs data dimensions (length x width) \n'
title += 'where width equal to red: 10; green: 100; blue: 1000'
plt.title(title)
fig.savefig("read-benchmarks-{}.jpg".format(__version__), bbox_inches='tight')

savetxt("write-read-benchmarks-{}.txt".format(__version__), t_arr)

max_stars_repo_path: examples/write-read benchmarks.py

max_issues_repo_path: examples/write-read benchmarks.py

max_forks_repo_path: examples/write-read benchmarks.py

reason: from numpy


text:
import os
import matplotlib as mpl
mpl.use('Qt5Agg')
import matplotlib.pyplot as plt
import numpy as np
import time
from . import console

def plot_all(mesh_df, layers_df, nusselt_df, verbose=True, save=False, show=True):
    temperature_distribution(mesh_df=mesh_df, verbose=verbose, save=save, show=show)
    nusselt_distrbution(layers_df=layers_df, mesh_df=mesh_df, verbose=verbose, save=save, show=show)
    nusselt_evolution(nusselt_df=nusselt_df, verbose=verbose, save=save, show=show)

def temperature_distribution(mesh_df, verbose=True, save=False, show=True):
    """
    Plots variables related to the temperature distribution of the linear Chimera model.
    :param mesh:
    :param verbose:
    :return:
    """
    console.event("Constructing temperature distribution plot...", verbose=verbose)

    t = time.time()
    font = {'size': 10}
    mpl.rc('font', **font)

    objects = mesh_df['object'].tolist()
    coordinates = mesh_df['coords'].tolist()
    temperatures = mesh_df['temperature'].tolist()
    conductivities = mesh_df['conductivity'].tolist()
    dT_dts = mesh_df['dT_dt'].tolist()

    fig1 = plt.figure(figsize=(8.0, 5.0))  # depth vs heat flux, depth vs temperature

    ax1 = fig1.add_subplot(111)
    ax1.plot(coordinates, dT_dts, color='r', linewidth=1.4, linestyle='--')
    ax1.set_xlabel("Depth (m)")
    ax1.set_ylabel("Heat Flux (degK/s)")
    ax1.tick_params('y', colors='r')

    ax2 = ax1.twinx()
    ax2.plot(coordinates, temperatures, color='b', linewidth=2, linestyle='-')
    ax2.set_ylabel("Temperature (degK)")
    ax2.tick_params('y', colors='b')

    fig2 = plt.figure(figsize=(8.0, 5.0))  # depth vs heat flux, depth vs thermal conductivity

    ax3 = fig2.add_subplot(111)
    ax3.plot(coordinates, dT_dts, color='r', linewidth=1.4, linestyle='--')
    ax3.set_xlabel('Depth (m)')
    ax3.set_ylabel('Heat Flux (degK/s)')
    ax3.tick_params('y', colors='r')

    ax4 = ax3.twinx()
    ax4.plot(coordinates, conductivities, color='m', linewidth=2, linestyle='-')
    ax4.set_ylabel('Thermal Conductivity')
    ax4.tick_params('y', colors='m')

    object_dict = {}
    for index, object in enumerate(objects):
        if object.lower() != 'boundary':
            if object not in object_dict.keys():
                object_dict.update({object: [coordinates[index]]})
            else:
                object_dict[object].append(coordinates[index])
    for object in object_dict.keys():
        min_coord = min(object_dict[object])
        max_coord = max(object_dict[object])
        color = np.random.rand(3, )
        ax1.axvspan(xmin=min_coord, xmax=max_coord, color=color, alpha=0.2, label=str(object))
        ax3.axvspan(xmin=min_coord, xmax=max_coord, color=color, alpha=0.2, label=str(object))

    ax1.set_title("Temperature Distribution Over Depth")
    ax3.set_title("Thermal Conductivity Over Depth")
    ax1.grid()
    ax1.legend(loc='lower left')
    ax3.grid()
    ax3.legend(loc='lower left')

    console.event("Finished constructing temperature distribution plot! (task took {}s)".format(
        time.time() - t), verbose=verbose)

    if show is True:
        plt.show()
    if save is True:
        fig1.tight_layout()
        fig2.tight_layout()
        fig1_name = "temp_distrib_fig1.png"
        fig2_name = "temp_distrib_fig2.png"
        if fig1_name in os.listdir(os.getcwd()):
            os.remove(fig1_name)
        if fig2_name in os.listdir(os.getcwd()):
            os.remove(fig2_name)
        fig1.savefig(fig1_name, format='png')
        fig2.savefig(fig2_name, format='png')

def nusselt_distrbution(layers_df, mesh_df, verbose=True, save=False, show=True):
    """
    Plots variables related to the Nusselt Number distribution of the linear Chimera model.
    :param layers:
    :param mesh:
    :param verbose:
    :return:
    """
    console.event("Constructing Nusselt distribution plot...", verbose=verbose)

    t = time.time()
    font = {'size': 10}
    mpl.rc('font', **font)

    objects = mesh_df['object'].tolist()
    coordinates_full = mesh_df['coords'].tolist()
    conductivities = mesh_df['conductivity'].tolist()
    dT_dts = mesh_df['dT_dt'].tolist()
    coordinates = []
    nusselt_nos = []
    coordinates_min_z = layers_df['min_z'].tolist()
    nusselt_nos_list = layers_df['nusselt'].tolist()
    for index, coord in enumerate(coordinates_min_z):
        coordinates.append(coord)
        nusselt_nos.append(nusselt_nos_list[index])

    fig1 = plt.figure(figsize=(8.0, 5.0))  # depth vs nusselt number, depth vs heat flux

    ax1 = fig1.add_subplot(111)
    ax1.plot(coordinates, nusselt_nos, color='b', linewidth=2, linestyle='-')
    ax1.scatter(coordinates, nusselt_nos, color='b')
    ax1.set_xlabel("Depth (m)")
    ax1.set_ylabel("Nusselt Number")
    ax1.tick_params('y', colors='b')

    ax2 = ax1.twinx()
    ax2.plot(coordinates_full, dT_dts, color='r', linewidth=1.4, linestyle='--')
    ax2.set_ylabel("Heat Flux (degK/s)")
    ax2.tick_params('y', colors='r')

    fig2 = plt.figure(figsize=(8.0, 5.0))  # depth vs nusselt number, depth vs thermal conductivity

    ax3 = fig2.add_subplot(111)
    ax3.plot(coordinates, nusselt_nos, color='b', linewidth=2, linestyle='-')
    ax3.scatter(coordinates, nusselt_nos, color='b')
    ax3.set_xlabel("Depth (m)")
    ax3.set_ylabel("Nusselt Number")
    ax3.tick_params('y', colors='b')

    ax4 = ax3.twinx()
    ax4.plot(coordinates_full, conductivities, color='m', linewidth=1.4, linestyle='--')
    ax4.set_ylabel("Thermal Conductivity")
    ax4.tick_params('y', colors='m')

    object_dict = {}
    for index, object in enumerate(objects):
        if object.lower() != 'boundary':
            if object not in object_dict.keys():
                object_dict.update({object: [coordinates_full[index]]})
            else:
                object_dict[object].append(coordinates_full[index])
    for object in object_dict.keys():
        min_coord = min(object_dict[object])
        max_coord = max(object_dict[object])
        color = np.random.rand(3, )
        ax1.axvspan(xmin=min_coord, xmax=max_coord, color=color, alpha=0.2, label=str(object))
        ax3.axvspan(xmin=min_coord, xmax=max_coord, color=color, alpha=0.2, label=str(object))

    ax1.set_title("Nusselt No. Distribution Over Depth")
    ax1.grid()
    ax1.legend(loc='lower left')
    ax3.set_title("Nusselt No. Distribution Over Depth")
    ax3.grid()
    ax3.legend(loc='lower left')

    console.event("Finished constructing Nusselt distribution plot! (task took {}s)".format(
        time.time() - t), verbose=verbose)

    if show is True:
        plt.show()
    if save is True:
        fig1.tight_layout()
        fig2.tight_layout()
        fig1_name = "nusselt_distrib_fig1.png"
        fig2_name = "nusselt_distrib_fig2.png"
        if fig1_name in os.listdir(os.getcwd()):
            os.remove(fig1_name)
        if fig2_name in os.listdir(os.getcwd()):
            os.remove(fig2_name)
        fig1.savefig(fig1_name, format='png')
        fig2.savefig(fig2_name, format='png')

def nusselt_evolution(nusselt_df, verbose=True, save=False, show=True):
    """
    Plot the temporal evolution of the nusselt number for each layer.
    :param df:
    :return:
    """
    fig = plt.figure(figsize=(8.0, 5.0))  # time vs nusselt number evolution

    ax = fig.add_subplot(111)

    for key in nusselt_df.keys():
        if key != 'time':
            c = np.random.rand(3, )
            ax.plot(nusselt_df['time'], nusselt_df[key], color=c, linewidth=1.4, label=key)

    ax.invert_xaxis()
    ax.grid()
    ax.legend(loc='upper right')
    ax.set_xlabel("Time (s)")
    ax.set_ylabel("Nusselt Number")
    ax.set_title("Temporal Evolution of the Nusselt Number")

    if show is True:
        plt.show()
    if save is True:
        fig.tight_layout()
        fig_name = "nusselt_evolution.png"
        if fig_name in os.listdir(os.getcwd()):
            os.remove(fig_name)
        fig.savefig(fig_name, format='png')

max_stars_repo_name: ScottHull/Chimera

max_issues_repo_name: ScottHull/Chimera

max_forks_repo_name: ScottHull/Chimera


text:
# -*- coding: utf-8 -*-
"""
Created on Thu Jan  4 00:27:44 2018

@author: bokorn
"""

from model_renderer.pose_renderer import BpyRenderer

import os
import time
import numpy as np

from se3_distributions.datasets.ycb_dataset import YCBDataset, ycbRenderTransform

def main():
    from argparse import ArgumentParser
    parser = ArgumentParser()

    parser.add_argument('--benchmark_folder', type=str, default='/scratch/bokorn/data/benchmarks/ycb/YCB_Video_Dataset')
    args = parser.parse_args()

    dataset = YCBDataset(data_dir=args.benchmark_folder,
                         image_set='valid_split',
                         img_size=(224, 224),
                         use_syn_data=True,
                         )
    for cls_idx in range(1, len(dataset.classes)):
        print('Rendering {}'.format(cls_idx))
        dataset.setObject(cls_idx)
        dataset.generateRenderedImages()

if __name__=='__main__':
    main()


text:
# .. ................................................................................... .. #
# .. Proyecto: UrbanLab - Plataforma de ayuda para micro y pequeñas empresas             .. #
# .. Archivo: proceso.py - funciones de procesamiento general de datos                   .. #
# .. Desarrolla: ITERA LABS, SAPI de CV                                                  .. #
# .. Licencia: Todos los derechos reservados                                             .. #
# .. Repositorio: https://github.com/IFFranciscoME/Urban_Lab.git                         .. #
# .. ................................................................................... .. #

# Importar librerias
import math
import pandas as pd
import numpy as np
import warnings

warnings.filterwarnings('ignore')

import statistics
import statsmodels.api as sm
from scipy.stats import shapiro
from statsmodels.stats.diagnostic import het_arch
from statsmodels.stats.diagnostic import acorr_ljungbox

from sklearn.metrics import r2_score
from sklearn import linear_model
from itertools import chain

# -- ------------------------------------------------------------------------------------ -- #
# -- Function: Calcular metrica con diccionario
# -- ------------------------------------------------------------------------------------ -- #
def metric_quantification(p_df_data, p_conditions, p_metric):
    """
	Funcion que pasa un diccionario con condiciones por columna al dataframe de pymes
	que regresa la suma de la metrica de acuerdo a las condiciones que se le de

    Parameters
    ---------
    p_df_data: DataFrame : datos de pymes en Dataframe
	p_conditions: dict : diccionario con condiciones
	p_metric: str : nombre de la metrica

    Returns
    ---------
    df: dict : Diccionario con metrica en el df original y como matriz

    Debuggin
    ---------
	p_df_data = datos.clean_data_pymes((datos.read_file(
							'Base_de_datos.xlsx', 'IIEG_E_1'))
	p_conditions = entradas.conditions_stress
	p_metric = 'Estres'

	"""
    # Nombre de columnas
    list_columns = list(p_conditions.keys())
    # Condiciones (dicts)
    list_dict_conditions = list(p_conditions.values())
    # Lista de lista con resultados
    answer = [[round(
        f_condition(
		    list_dict_conditions[k],
            p_df_data[list_columns[k]][i]
        ),
    3)
        for i in range(len(p_df_data))
    ]
        for k in range(len(list_columns))
    ]
    # DataFrame con la matriz de todo
    metric = pd.DataFrame(answer)

    # --------------------------
    # Columna con suma
    metric_sum = round(metric.sum(), 3)

    # Nombre de variables para columnas
    col = list(p_conditions.keys())

    # Transponer tabla de metrica
    metric_table = metric.T

    # Asignar nombres a las columnas
    metric_table.columns = col

    # Agregar columna de suma total
    metric_table['Total'] = metric_sum

    # --------------------------

    # Dataframe copia
    df = p_df_data.copy()

    # Dataframe con columna de metrica
    df[p_metric] = metric_sum

    return {'df_prices': df, 'metric_table': metric_table}

# -- ------------------------------------------------------------------------------------ -- #
# -- Function: Regresa el resultado de un dato de acuerdo a las condiciones
# -- ------------------------------------------------------------------------------------ -- #
def f_condition(p_dict_condition, p_data):
    """
	Funcion que checa en el diccionario de condiciones para las metricas
	el diccionario de condiciones contiene dentro otros diccionarios, donde:
		el value del dict dentro del dict es p_codition (la condicion que se debe de cumplir)
		y el key es el resultado que se asigna si cumple la condicion
	esto lo checa para cada dato de acuerdo a la columna (key del dict) en otra funcion

    Parameters
    ---------
    p_dict_condition: dict : diccionario con condiciones
	p_data: int or str: dato a comparar

	Returns
    ---------
    int: valor de acuerdo a la condicion

    Debuggin
    ---------
	p_dict_condition = list(entradas.conditions_stress.values())[0]
	p_data = datos.clean_data_pymes((datos.read_file(
							'Base_de_datos.xlsx', 'IIEG_E_1'))['ventas_porcentaje'][0]

	"""
    # Valores que se podrian poner
    posible_results = list(p_dict_condition.keys())

    # lista de condiciones
    list_conditions = list(p_dict_condition.values())

    # Utilizando la funcion para cada condicion
    answer = [f_type_verification(list_conditions[i], posible_results[i],
                                  p_data) for i in range(len(list_conditions))]

    # Si todos son cero, quiere decir que p_data es nan
    if answer == [0] * len(answer):
        return 0

    # Si no
    else:
        # Quitar los nan de la lista answer
        lista = list(filter(None.__ne__, answer))
        if len(lista) == 0:
            return np.nan
        else:
            # Regresar el resultado
            return lista[0]

# -- ------------------------------------------------------------------------------------ -- #
# -- Function: Checar la condicion de acuerdo a su tipo
# -- ------------------------------------------------------------------------------------ -- #
def f_type_verification(p_condition, p_result, p_data):
    """
	Funcion que de acuerdo a los 3 parametros te regresa un resultado,
		p_data puede ser string o int, pero si p_condition es una lista se busca en tal
		si esta se devuelve p_result, si p_data no es string entonces es numerico
		si es nan devuelve 0 si es tupla, debe de estar entre ambos numeros de la tupla

    Parameters
    ---------
    p_condition: tuple or list : contiene las condiciones
	p_result: int : numero si se cumple la condicion es el resultado
	p_data: int or str: dato que se esta comparando para cuantificar

    Returns
    ---------
    p_result: int : numero de la metrica

    Debuggin
    ---------
	p_condition = (0, 25)
	p_result = 1
	p_data = 10

	"""
    # Si es lista tiene que estar en tal
    if type(p_condition) == list:
        if p_data in p_condition:
            return p_result

    # Si es numerico
    if type(p_data) != str:
        if math.isnan(p_data):
            return 0
        # Si es tuple, tiene que estar entre ambos numeros
        if type(p_condition) == tuple:
            if p_condition[0] < p_data and p_data <= p_condition[1]:
                return p_result

# -- ------------------------------------------------------------------------------------ -- #
# -- Function: Separar series de tiempo del data frame de precios
# -- ------------------------------------------------------------------------------------ -- #
def f_time_series(p_df_prices, p_clase):
    """
    Funcion que separa las serie de tiempo de acuerdo a la clase que se le pida

    Parameters
    ---------
    p_df_prices: DataFrame : data en un DF
    p_clase: str : clase que se requieren los productos

    Returns
    ---------
    series_tiempo: list : todas las series de tiempo

    Debuggin
    ---------
    p_df_prices = read_file('Precios_INEGI.xlsx', 'Datos_acomodados')
    p_clase = 'Accesorios y utensilios'

    """
    # Agrupar por clase
    clases = list(p_df_prices.groupby('Clase'))

    # Busqueda de dataframe para la clase que se necesita
    search = [clases[i][1] for i in range(len(clases)) if clases[i][0] == p_clase][0]
    search.reset_index(inplace=True, drop=True)

    # Agrupar por generico
    generico = list(search.groupby('Generico'))

    # Series de tiempo por Generico
    series_tiempo = [generico[i][1].median().rename(generico[i][0],
                                                    inplace=True) for i in range(len(generico))]

    return series_tiempo

# -- ------------------------------------------------------------------------------------ -- #
# -- Function: lista de grupos con cada clase de productos
# -- ------------------------------------------------------------------------------------ -- #
def f_clases(p_df_prices):
    """
    Funcion que regresa una lista con el nombre de todos los grupos y dentro de la misma
    otra lista con el nombre de todas las clases por grupo

    Parameters
    ---------
    p_df_data: DataFrame : data en un DataFrame

    Returns
    ---------
    list_clases: list : todas las clases por grupo

    Debuggin
    ---------
    p_df_prices = read_file('Precios_INEGI.xlsx', 'Datos_acomodados')

    """
    # Separar por grupo
    group_by_group = list(p_df_prices.groupby('Grupo'))

    # lista por grupo y clases por grupo
    list_clases = [[grupo[0], grupo[1]['Clase'].unique().tolist()] for grupo in group_by_group]

    return list_clases

# -- ------------------------------------------------------------------------------------ -- #
# -- Function: Predecir serie de tiempo
# -- ------------------------------------------------------------------------------------ -- #
def f_predict_time_series(p_serie_tiempo):
    """
	Funcion que modela una serie de tiempo utilizando desde una regresion lineal hasta
	un modelo sarima con metodo de box jenkins para predecir cual seria el cambio
	en los precios

    Parameters
    ---------
    p_serie_tiempo: DataFrame : serie de tiempo a modelar

    Returns
    ---------
    cambio_porc: float : cambio porcentual del ultimo precio a el que se predice

    Debuggin
    ---------
	p_serie_tiempo = datos.clean_data_prices(
							datos.read_file('Precios_INEGI.xlsx', 'Datos_acomodados'))

	"""

    # Meses en el futuro, predecir
    meses = 6

    # Ultimo precio
    ultimo_precio = p_serie_tiempo[len(p_serie_tiempo) - 1]

    # Reiniciar indice
    serie_tiempo = p_serie_tiempo.copy()
    serie_tiempo.reset_index(drop=True, inplace=True)

    # ------------------------------------------ #
    # Primero: intentar con una regresión lineal
    # ------------------------------------------ #

    # Separar la informacion que se tiene de la serie de tiempo en y
    y_o = np.array(serie_tiempo)
    x_o = np.arange(len(serie_tiempo))

    # Acomodarla de la forma que el modelo necesita
    x = x_o.reshape((len(x_o), 1))
    y = y_o.reshape((len(y_o), 1))

    # Crear el modelo
    modelo = linear_model.LinearRegression()

    # Pasar nuestros datos por el modelo
    modelo.fit(x, y)

    # De acuerdo al modelo, calcular y
    y_pred = modelo.predict(x)

    # R2 de sus residuales
    r_2 = r2_score(y, y_pred)

    if r_2 > 0.9:
        # sumar a la x ultima
        value = x_o[-1] + meses
        # predecir
        prediction = modelo.predict(value.reshape((1, 1)))
        # cambio porcentual
        cambio_porc = (ultimo_precio - prediction[0][0]) / ultimo_precio

        return cambio_porc

    else:
        # ------------------------------------------ #
        # Segundo: intentar modelar con ARIMA
        # ------------------------------------------ #

        # Empezar checando si es estacionaria
        def check_stationarity(data):
            # Usar dicky fuller
            test_results = sm.tsa.stattools.adfuller(data)
            # Cuando se cumple esto es estacionaria la serie orginal
            if test_results[0] < 0 and test_results[1] <= 0.05:
                lags = 0

            # Cuando no se cumple se debe diferenciar para que sea estacionaria
            else:
                for i in range(3):
                    # Diferenciar datos
                    new_data = np.diff(data)

                    # Volver a calcular test dicky fuller
                    new_results = sm.tsa.stattools.adfuller(new_data)

                    # Volver a comparar para decidir si es o no estacionaria
                    if new_results[0] < 0 and new_results[1] <= 0.05:
                        # rezagos necesarios para volverlo estacionario
                        lags = i
                        break

                    else:
                        data = new_data
                        # solo permitimos 3 rezagos, si aún no lo es, no se modela
                        lags = np.nan
            return lags

        # Checar estacionariedad
        d = check_stationarity(serie_tiempo)

        if np.isnan(d):
            return 0

        else:

            # lambda para tomar los coef significativos
            all_significant_coef = lambda x: x if abs(x) > 0.5 else None

            def significat_lag(all_coef):
                # Tomar los indices de los rezagos
                ind_c = all_coef.index.values
                # Solo los rezagos menores a 7
                sig_i = ind_c[ind_c < 7]
                # Nuevos coeficientes
                new_coef = all_coef[all_coef.index.isin(list(sig_i))]
                if len(new_coef) > 1:
                    # Tomar los valores absolutos
                    abs_coef = new_coef[1:].abs()
                    # Buscar el maximo
                    max_coef = abs_coef.max()
                    # El indice es el rezago al que pertenece
                    answer = abs_coef[abs_coef == max_coef[0]].dropna().index[0]
                    return answer
                else:
                    return 1

            # Calcular coeficientes de fac parcial
            facp = sm.tsa.stattools.pacf(serie_tiempo)

            # Pasar lambda y quitar los que no son significativos
            p_s = pd.DataFrame(all_significant_coef(facp[i]) for i in range(len(facp))).dropna()

            # Tomar el primero que sea signiticativo, sera la p de nuestro modelo
            p = significat_lag(p_s)

            # --- #

            # Calcular coeficientes de fac
            fac = sm.tsa.stattools.acf(serie_tiempo, fft=False)

            # Pasar lambda y quitar los que no son significativos
            q_s = pd.DataFrame(all_significant_coef(fac[i]) for i in range(len(fac))).dropna()

            # Tomar el primero que sea signiticativo, sera la p de nuestro modelo
            q = significat_lag(q_s)

            # Modelo
            arima = sm.tsa.statespace.SARIMAX(serie_tiempo,
                                              order=(p, d, q),
                                              trend='c',
                                              enforce_stationarity=True,
                                              enforce_invertibility=True)
            arima_fitted = arima.fit()

            def check_resid(model_fit):
                # estadístico Ljung – Box.
                colineal = acorr_ljungbox(model_fit.resid, lags=[10])
                # se necesita aceptar H0, es decir p_value debe ser mayor a .05
                colin = True if colineal[1] > 0.05 else False

                # shapiro test
                normalidad = shapiro(model_fit.resid)
                # si el p-value es menor a alpha, rechazamos la hipotesis de normalidad
                norm = True if normalidad[1] > 0.05 else False

                # arch test
                heterosced = het_arch(model_fit.resid)
                # p-value menor a 0.05 y concluir que no hay efecto de heteroscedasticidad
                heter = True if heterosced[1] > 0.05 else False

                return colin, norm, heter

            # test en los residuales
            resid_test = check_resid(arima_fitted)

            # predecir siguientes 6 meses
            future_prices = arima_fitted.forecast(meses, alpha=0.05)

            # Prediccion
            prediction = future_prices[len(serie_tiempo) + meses - 1]

            cambio_porc = (ultimo_precio - prediction) / ultimo_precio

            if abs(cambio_porc) < 0.4 and True in resid_test:
                return cambio_porc

            else:
                return np.nan

# -- ------------------------------------------------------------------------------------ -- #
# -- Function: Predecir todas las series de una clase
# -- ------------------------------------------------------------------------------------ -- #
def f_predict_clase(df_prices, clase):
    """
	Funcion que regresa todas las predicciones

    Parameters
    ---------
    df_data: DataFrame : datos limpios de precios
	p_clase: str : clase que se requieren la prediccion

    Returns
    ---------
    med_predict: float : porcentaje medio de la clase

    Debuggin
    ---------
	df_data = df_prices
	p_clase = 'Accesorios y utensilios'

	"""
    # Fragmentar por series de tiemo
    time_series = f_time_series(df_prices, clase)

    # Predicciones de la clase (por producto)
    predictions = [f_predict_time_series(time_series[s]) for s in range(len(time_series))]

    # Series
    df_predict = pd.Series(predictions)
    df_predict.rename(clase, inplace=True)

    return df_predict

# -- ------------------------------------------------------------------------------------ -- #
# -- Function: Predecir todas las clases de todos los grupos y generar el semaforo
# -- ------------------------------------------------------------------------------------ -- #
def semaforo_precios(df_prices):
    """
	Funcion que genera el dataframe de los precios con ayuda de las funciones:
		f_predict_clase y a su vez f_predict_time_series

    Parameters
    ---------
    df_data: DataFrame : datos limpios de precios

    Returns
    ---------
    med_predict: float : porcentaje medio de la clase

    Debuggin
    ---------
	df_data = df_prices

	"""
    # Clases del dataframe por grupo
    grupo_clases = f_clases(df_prices)

    # Solo nombres de clases
    clases_all = [
        [grupo_clases[i][1][j] for j in range(len(
            grupo_clases[i][1]))
         ] for i in range(len(grupo_clases))
    ]

    # Medias de predicciones
    predictions = [
        [f_predict_clase(df_prices, clases_all[i][j]
                         ).median() for j in range(len(clases_all[i]))
         ] for i in range(len(clases_all))
    ]

    # Ultimos precios
    last_prices = [
        [round(df_prices[df_prices['Clase'] == clases_all[i][j]
                         ]['may 2020'].median(), 2) for j in range(len(clases_all[i]))
         ] for i in range(len(clases_all))
    ]

    # Precios futuros
    pred_prices = [[[
        last_prices[i][j],
        round(last_prices[i][j] * (1 + predictions[i][j]), 4)
    ]
        for j in range(len(clases_all[i]))
    ] for i in range(len(clases_all))
    ]

    # Nombres de indices
    tuplas_2d = [[(grupo_clases[i][0], grupo_clases[i][1][j]) for j in range(len(grupo_clases[i][1]))
                  ] for i in range(len(grupo_clases))]
    # Aplanar lista
    tuplas = list(chain.from_iterable(tuplas_2d))

    # Generar el multi index
    ind = pd.MultiIndex.from_tuples(tuplas)

    # Valores para DataFrame
    values = np.array(list(chain.from_iterable(pred_prices)))

    # Dataframe con precios
    df = pd.DataFrame(values, index=ind)

    cols = ['Ultimo precio', 'Precio para Nov 2020']
    df.columns = cols

    # Crear tabla
    semaforo = pd.DataFrame()
    for i in range(len(grupo_clases)):
        mean_group = statistics.median(predictions[i])
        if mean_group < -0.01:
            result = 'verde'
        elif mean_group > 0.01:
            result = 'rojo'
        else:
            result = 'amarillo'
        semaforo[grupo_clases[i][0]] = [result, round(mean_group * 100, 3)]

    return {'semaforo': semaforo.T, 'predicciones': df}

reason: import numpy,from scipy,import statsmodels,from statsmodels


text:
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import glob, os
import numpy as np
import cv2
import argparse

from plyfile import PlyData, PlyElement

# params
parser = argparse.ArgumentParser()
# data paths
parser.add_argument('--input_path', required=True, help='path to sens file to read')
parser.add_argument('--output_path', required=True, help='path to output folder')
parser.add_argument('--save_npz', action='store_true')
opt = parser.parse_args()
print(opt)

if not os.path.exists(opt.output_path):
    os.mkdir(opt.output_path)

# Load Depth Camera Intrinsic
depth_intrinsic = np.loadtxt(opt.input_path + '/intrinsic/intrinsic_depth.txt')
print('Depth intrinsic: ')
print(depth_intrinsic)

# Compute Camrea Distance (just for demo, so you can choose the camera distance in frame sampling)
poses = sorted(glob.glob(opt.input_path + '/pose/*.txt'), key=lambda a: int(os.path.basename(a).split('.')[0]))
depths = sorted(glob.glob(opt.input_path + '/depth/*.png'), key=lambda a: int(os.path.basename(a).split('.')[0]))
colors = sorted(glob.glob(opt.input_path + '/color/*.png'), key=lambda a: int(os.path.basename(a).split('.')[0]))

# # Get Aligned Point Clouds.
for ind, (pose, depth, color) in enumerate(zip(poses, depths, colors)):
    name = os.path.basename(pose).split('.')[0]

    if os.path.exists(opt.output_path + '/{}.npz'.format(name)):
        continue

    try:
        print('='*50, ': {}'.format(pose))
        depth_img = cv2.imread(depth, -1) # read 16bit grayscale image
        mask = (depth_img != 0)
        color_image = cv2.imread(color)
        color_image = cv2.resize(color_image, (640, 480))
        color_image = np.reshape(color_image[mask], [-1,3])
        colors = np.zeros_like(color_image)
        colors[:,0] = color_image[:,2]
        colors[:,1] = color_image[:,1]
        colors[:,2] = color_image[:,0]

        pose = np.loadtxt(poses[ind])
        print('Camera pose: ')
        print(pose)

        depth_shift = 1000.0
        x,y = np.meshgrid(np.linspace(0,depth_img.shape[1]-1,depth_img.shape[1]), np.linspace(0,depth_img.shape[0]-1,depth_img.shape[0]))
        uv_depth = np.zeros((depth_img.shape[0], depth_img.shape[1], 3))
        uv_depth[:,:,0] = x
        uv_depth[:,:,1] = y
        uv_depth[:,:,2] = depth_img/depth_shift
        uv_depth = np.reshape(uv_depth, [-1,3])
        uv_depth = uv_depth[np.where(uv_depth[:,2]!=0),:].squeeze()

        intrinsic_inv = np.linalg.inv(depth_intrinsic)
        fx = depth_intrinsic[0,0]
        fy = depth_intrinsic[1,1]
        cx = depth_intrinsic[0,2]
        cy = depth_intrinsic[1,2]
        bx = depth_intrinsic[0,3]
        by = depth_intrinsic[1,3]
        point_list = []
        n = uv_depth.shape[0]
        points = np.ones((n,4))
        X = (uv_depth[:,0]-cx)*uv_depth[:,2]/fx + bx
        Y = (uv_depth[:,1]-cy)*uv_depth[:,2]/fy + by
        points[:,0] = X
        points[:,1] = Y
        points[:,2] = uv_depth[:,2]
        points_world = np.dot(points, np.transpose(pose))
        print(points_world.shape)

        pcd_save = np.zeros((points_world.shape[0], 7))
        pcd_save[:,:3] = points_world[:,:3]
        pcd_save[:,3:6] = colors

        print('Saving npz file...')
        np.savez(opt.output_path + '/{}.npz'.format(name), pcd=pcd_save)
    except:
        continue

max_stars_repo_name: ut-amrl/ContrastiveSceneContexts

max_issues_repo_name: ut-amrl/ContrastiveSceneContexts

max_forks_repo_name: ut-amrl/ContrastiveSceneContexts


text:
from os import read
import numpy as np
import serial
import configparser
import time
import datetime
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import tkinter
import tkinter.ttk as ttk
import threading
import sys

config_pass = "./config.ini"
log_pass = "./log"

class root():

    def __init__(self):

        #コンフィグ読取
        self.config_read()

        # 変数定義
        self.serial_valid = 0 #シリアル通信継続用
        self.serial_task_mode = 0 # シリアルのタスクモード
        self.log_valid = 0 # ログの継続用

        self.calibration_value = None # キャリブレーション用の値

        self.root = tkinter.Tk()
        self.root.title("mini4tools")
        self.root.geometry("800x900")

        #メニューバー
        self.men = tkinter.Menu(self.root)
        self.root.config(menu=self.men)

        #メニューに親メニュー（ファイル）を作成する
        self.menu_file = tkinter.Menu(self.root)
        self.men.add_cascade(label='ファイル', menu=self.menu_file)

        #親メニューに子メニュー（開く・閉じる）を追加する
        self.menu_file.add_command(label='開く', command=self.open_file)
        self.menu_file.add_separator()
        self.menu_file.add_command(label='閉じる', command=self.close_disp)

        #ボタン関係
        self.connect_bt_text = tkinter.StringVar()
        self.connect_bt_text.set("接続")
        self.connect_bt = tkinter.Button(self.root, textvariable=self.connect_bt_text, width=5)
        self.connect_bt.place(x=200, y=0)
        self.connect_bt.bind("<Button-1>",self.serial_start)

        self.calibration_bt = tkinter.Button(self.root,text="実行", width=5)
        self.calibration_bt.bind("<Button-1>", self.calibration)
        self.calibration_bt.place(x=200, y=30)

        self.log_bt_text = tkinter.StringVar()
        self.log_bt_text.set('スタート')
        self.log_bt = tkinter.Button(self.root,textvariable=self.log_bt_text, width=8)
        self.log_bt.bind("<Button-1>", self.log_task)
        self.log_bt.place(x=500, y=50)

        # テキスト表示(静的)
        self.serial_info = tkinter.Label(self.root, text=u'シリアル接続')
        self.serial_info.place(x=10, y=0)

        self.calibration_info = tkinter.Label(self.root, text=u'キャリブレーション')
        self.calibration_info.place(x=10, y=30)

        self.current_info = tkinter.Label(self.root, text=u'電流(A)', font=('','16'))
        self.current_info.place(x=10, y=80)

        self.rpm_info = tkinter.Label(self.root, text=u'回転数(RPM)', font=('','16'))
        self.rpm_info.place(x=10, y=110)

        self.loadsell_info = tkinter.Label(self.root, text=u'トルク(g/cm)', font=('','16'))
        self.loadsell_info.place(x=10, y=140)

        self.trig_info = tkinter.Label(self.root, text=u'トリガ', font=('','16'))
        self.trig_info.place(x=400, y=80)

        self.trig_mode_info = tkinter.Label(self.root, text=u'種類', font=('','16'))
        self.trig_mode_info.place(x=400, y=110)

        self.trig_mode_info = tkinter.Label(self.root, text=u'ログ', font=('','16'))
        self.trig_mode_info.place(x=400, y=50)

        # テキスト表示(動的)
        self.calibration_label_text = tkinter.StringVar()
        self.calibration_label_text.set("未完了")
        self.calibration_label = tkinter.Label(self.root, textvariable=self.calibration_label_text)
        self.calibration_label.place(x=300, y=30)

        self.loadsell_calibration_label_text = tkinter.StringVar()
        self.loadsell_calibration_label_text.set("未完了")
        self.loadsell_calibration_label = tkinter.Label(self.root, textvariable=self.loadsell_calibration_label_text)
        self.loadsell_calibration_label.place(x=400, y=30)

        # 入力欄
        self.current_entry = tkinter.Entry(self.root,text="",width=10, font=('','16'),justify=tkinter.RIGHT)
        self.current_entry.place(x=200, y=80)
        self.current_entry.configure(state='normal')
        self.current_entry.delete(0,tkinter.END)
        self.current_entry.insert('end','-')
        self.current_entry.configure(state='readonly')

        self.rpm_entry = tkinter.Entry(self.root,text="",width=10, font=('','16'),justify=tkinter.RIGHT)
        self.rpm_entry.place(x=200, y=110)
        self.rpm_entry.configure(state='normal')
        self.rpm_entry.delete(0,tkinter.END)
        self.rpm_entry.insert('end','-')
        self.rpm_entry.configure(state='readonly')

        self.loadsell_entry = tkinter.Entry(self.root,text="",width=10, font=('','16'),justify=tkinter.RIGHT)
        self.loadsell_entry.place(x=200, y=140)
        self.loadsell_entry.configure(state='normal')
        self.loadsell_entry.delete(0,tkinter.END)
        self.loadsell_entry.insert('end','-')
        self.loadsell_entry.configure(state='readonly')

        self.trig_entry = tkinter.Entry(self.root,text="",width=10, font=('','16'),justify=tkinter.RIGHT)
        self.trig_entry.place(x=500, y=80)
        self.trig_entry.configure(state='normal')
        self.trig_entry.delete(0,tkinter.END)
        self.trig_entry.insert('end',self.oscilloscope_trig)

        #コンボボックス
        self.v = tkinter.StringVar()
        #self.v = "テスト"
        self.trig_mode_list = ('両方','立上り','立下り')
        self.trig_mode_combo = ttk.Combobox(self.root, height=3,width=10, font=('','16'),state="readonly", textvariable= self.v,values=self.trig_mode_list)
        self.trig_mode_combo.set(self.oscilloscope_trig_mode)
        #self.rpm_entry.insert()
        #self.trig_mode_combo.bind("<<ComboboxSelected>>",self.get_trig_mode) # コンボボックスが変化したら値を取得する
        self.trig_mode_combo.place(x=500, y=110)

        # グラフ表示
        x1 = np.linspace(0.0, 5.0)
        y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
        x2 = np.linspace(0.0, 3.0)
        y2 = np.cos(2 * np.pi * x2) * np.exp(-x1)

        self.fig = plt.figure(figsize=(8,6),dpi=100)
        # ax1
        self.ax1 = self.fig.add_subplot(2,2,1)
        #ax1.plot(x1, y1)
        self.line1, = self.ax1.plot(0, 0, color='blue')
        self.ax1.set_title('line plot')
        self.ax1.set_ylabel('Damped oscillation')

        # ax2
        self.ax2 = self.fig.add_subplot(2,2,2)
        self.line2, = self.ax2.plot(0, 0, color='blue')
        #ax2.scatter(x1, y1, marker='o')
        self.ax2.set_title('Scatter plot')

        # ax3
        ax3 = self.fig.add_subplot(2,2,3)
        #ax3.plot(x2, y2)
        ax3.set_ylabel('Damped oscillation')
        ax3.set_xlabel('time (s)')

        # ax4
        ax4 = self.fig.add_subplot(2,2,4)
        #ax4.scatter(x2, y2, marker='o')
        ax4.set_xlabel('time (s)')
        #self.fig, ax_current = plt.subplots(1, 1)
        #plt.plot(buf)
        #line, = ax.plot( buf, color='blue')
        #plt.pause(0.01)
        #line.remove()
        self.canvas = FigureCanvasTkAgg(self.fig, master=self.root)  # Generate canvas instance, Embedding fig in root
        self.canvas.draw()
        self.canvas.get_tk_widget().place(x=0, y=200)

        #anim = FuncAnimation(fig, update, frames=range(8), interval=1000)

        #ツールバーを表示
        self.toolbar=NavigationToolbar2Tk(self.canvas, self.root)
        self.toolbar.place(x=10, y=800)

        # ウインドウを閉じたときの処理
        self.root.protocol("WM_DELETE_WINDOW", self.on_closing)

        # Gui起動
        self.root.mainloop()

    # コンボボックスの値取得用
    #def get_trig_mode(self):
    #    print(self.trig_mode_combo.get())

    #    self.oscilloscope_trig_mode = self.v.get()

    # ウインドウが閉じたらプログラムも終了する
    def on_closing(self):
        self.config_write() # コンフィグを書き込む
        self.serial_valid = 0 # シリアル通信 切断用
        self.root.destroy() # 画面を閉じる
        time.sleep(3) # 終了まで3秒待つ(シリアル通信が切れるまで)
        sys.exit(0) # プログラム自体を終了する

    def changeText(self):
        self.text.set("Updated Text")

    def open_file(self):
        pass
    def close_disp(self):
        pass

    # シリアルの接続と切断
    def serial_start(self,event):

        # 別スレッドを立ててシリアル通信を実行する
        if self.serial_valid == 0 :

            self.serial_valid = 1 # シリアル通信 継続用
            self.connect_bt_text.set("切断")
            self.serial_thread = threading.Thread(target=self.serial_read)
            self.serial_thread.start()

        # 別スレッドのシリアル通信を終了する
        else:
            self.serial_valid = 0 # シリアル通信 切断用
            self.connect_bt_text.set("接続")

    # 電流と回転数の表示
    def culc(self,buf,loadsell):

        # 電流計算
        current_np = np.array(buf,dtype=float)
        current_np = ((current_np - self.calibration_value) * float(self.current_gain) /100 + float(self.current_offset)) /200 # 電流計算
        self.current_average = abs(np.average(current_np))

        # 電流表示
        self.current_entry.configure(state='normal')
        self.current_entry.delete(0,tkinter.END)
        self.current_entry.insert('end','{:.3f}'.format(self.current_average))
        self.current_entry.configure(state='readonly')

        # 回転数計算
        current_fft = np.fft.fft(current_np)
        current_fft_abs = np.abs(current_fft)
        current_fft_abs = current_fft_abs[1:int(len(current_fft_abs)/2)]
        fft_peak = np.argmax(current_fft_abs)
        fft_rpm = int(self.raw_frequency)/int(self.raw_sample) * (fft_peak + 1) * 10

        self.rpm_entry.configure(state='normal')
        self.rpm_entry.delete(0,tkinter.END)
        self.rpm_entry.insert('end','{:.3f}'.format(fft_rpm))
        self.rpm_entry.configure(state='readonly')

        # 電流波形計算
        current_graph = current_np[:200]
        self.line1.remove()
        self.line1, = self.ax1.plot(current_graph, color='blue')

        # FFTグラフ計算
        self.line2.remove()
        self.line2, = self.ax2.plot(current_fft_abs, color='blue')
        self.canvas.draw()

        # トルク計算
        loadsell_np = np.array(loadsell,dtype=float)
        loadsell_np = ((loadsell_np - self.loadsell_calibration_value) * float(self.loadsell_gain) /100 + float(self.loadsell_offset)) /200
        self.loadsell_average = abs(np.average(loadsell_np))

        # トルク表示
        self.loadsell_entry.configure(state='normal')
        self.loadsell_entry.delete(0,tkinter.END)
        self.loadsell_entry.insert('end','{:.3f}'.format(self.loadsell_average))
        self.loadsell_entry.configure(state='readonly')

        # ログの保存
        if self.log_valid == 1:
            self.log_current.append(self.current_average) # 電流ログ
            self.log_rpm.append(fft_rpm) # 回転数ログ
            self.line3, = self.ax3.plot(self.log_current, self.log_rpm, color='blue') # グラフ用

            with open(log_pass + self.log_title + '.csv', 'a') as f:
                print(fft_rpm + ',' + self.current_average + ',' + self.loadsell_average, file=f)

    def calibration(self,event):
        self.serial_task_mode = 1

    def calibration_task(self,buf,loadsell):
        buf_np = np.array(buf,dtype=float)
        self.calibration_value = np.average( buf_np )
        self.calibration_label_text.set(str(self.calibration_value))

        loadsell_np = np.array(loadsell,dtype=float)
        self.loadsell_calibration_value = np.average( loadsell_np )
        self.loadsell_calibration_label_text.set(str(self.loadsell_calibration_value))

    # ログボタンの処理
    def log_task(self,event):

        if self.log_valid == 0:

            # self.log_list = [] #ログデータを空にする
            self.log_current = [] # 電流用
            self.log_rpm = [] # 回転数用

            tdatetime = datetime.datetime.now()
            self.log_title = tdatetime.strftime('%Y/%m/%d_%H:%M:%S')
            self.log_valid = 1
            self.log_bt_text.set("ストップ")

            with open(log_pass + self.log_title + '.csv', 'w') as f:
                print(self.log_title + ' log start', file=f)

        else:
            self.log_valid = 0
            self.log_bt_text.set("スタート")

    # コンフィグ読込
    def config_read(self):

        config_ini = configparser.ConfigParser()
        config_ini.read(config_pass, encoding='utf-8')

        try:
            serial_config = config_ini['serial']
            self.serial_port = serial_config.get('port')
            self.serial_bps = serial_config.get('bps')
        except KeyError:
            self.serial_port = None
            self.serial_bps = None

        if self.serial_port == None:
            self.serial_port = 'com1'
        if self.serial_bps == None:
            self.serial_bps = '115200'

        try:
            raw_config = config_ini['raw']
            self.raw_sample = raw_config.get('sample')
            self.raw_frequency = raw_config.get('sampling_frequency')
            self.loadsell_sample = raw_config.get('loadsell_sample')
        except KeyError:
            self.raw_sample = None
            self.raw_frequency = None

        if self.raw_sample == None:
            self.raw_sample = '2048'
        if self.raw_frequency == None:
            self.raw_frequency = '10000'
        if self.loadsell_sample == None:
            self.loadsell_sample = '64'

        try:
            current_config = config_ini['current']
            self.current_gain = current_config.get('gain')
            self.current_offset = current_config.get('offset')
        except KeyError:
            self.current_gain = None
            self.current_offset = None

        if self.current_gain == None:
            self.current_gain = '100'
        if self.current_offset == None:
            self.current_offset = '0'

        try:
            loadsell_config = config_ini['loadsell']
            self.loadsell_gain = loadsell_config.get('gain')
            self.loadsell_offset = loadsell_config.get('offset')
        except KeyError:
            self.loadsell_gain = None
            self.loadsell_offset = None

        if self.loadsell_gain == None:
            self.loadsell_gain = '100'
        if self.loadsell_offset == None:
            self.loadsell_offset = '0'

        try:
            oscilloscope_config = config_ini['oscilloscope']
            self.oscilloscope_trig = current_config.get('trig')
            self.oscilloscope_trig_mode = current_config.get('trig_mode')
        except KeyError:
            self.oscilloscope_trig = None
            self.oscilloscope_trig_mode = None

        if self.oscilloscope_trig == None:
            self.oscilloscope_trig = '0'
        if self.oscilloscope_trig_mode == None:
            self.oscilloscope_trig_mode = '両方'

    # iniファイル書き込み
    def config_write(self):

        config = configparser.ConfigParser()
        section_serial = 'serial'
        config.add_section(section_serial)
        config.set(section_serial, 'port', self.serial_port)
        config.set(section_serial, 'bps', self.serial_bps)

        section_raw = 'raw'
        config.add_section(section_raw)
        config.set(section_raw, 'sample', self.raw_sample)
        config.set(section_raw, 'sampling_frequency', self.raw_frequency)
        config.set(section_raw, 'loadsell_sample', self.raw_sample)

        section_current = 'current'
        config.add_section(section_current)
        config.set(section_current, 'gain', self.current_gain)
        config.set(section_current, 'offset', self.current_offset)

        section_loadsell = 'loadsell'
        config.add_section(section_loadsell)
        config.set(section_loadsell, 'gain', self.loadsell_gain)
        config.set(section_loadsell, 'offset', self.loadsell_offset)

        section_oscilloscope = 'oscilloscope'
        config.add_section(section_oscilloscope)
        config.set(section_oscilloscope, 'trig', self.trig_entry.get())
        config.set(section_oscilloscope, 'trig_mode', self.v.get())

        with open(config_pass, 'w',encoding='utf-8') as configfile:
            # 指定したconfigファイルを書き込み
            config.write(configfile)

    def serial_read(self):

        read_trig = 0 #sirial読み込み用トリガ

        buf_read = []
        buf_read.clear()
        buf_loadsell = []
        buf_loadsell.clear()

        with serial.Serial(self.serial_port, self.serial_bps) as ser:  # timeoutを秒で設定（default:None)ボーレートはデフォルトで9600
            while(self.serial_valid):
                #buf_read.append(ser.read())
                while(ser.in_waiting > 0): # シリアルの受信が無くなるまで繰り返し
                    #buf_one = int.from_bytes(ser.read(1),'big')

                    if read_trig == 0:
                        #ヘッダー検出
                        if int.from_bytes(ser.read(1),'big') == 0xff:
                            read_byte = int.from_bytes(ser.read(1),'big')

                            # 送信完了
                            if read_byte == 0xff:
                                read_trig = 1 # 計算スタート

                             # 電流値送信
                            if read_byte == 0xfe:
                                buf_read.clear() # 読み込み用bufを空にする
                                read_trig = 2 #シリアル読み込みスタート

                            # トルク送信
                            if read_byte == 0xfd:
                                buf_loadsell.clear() # 読み込み用bufを空にする
                                read_trig = 3 #シリアル読み込みスタート

                    # 電流値計算
                    if read_trig == 2:
                        # シリアル読み込み
                        buf_read.append(int.from_bytes(ser.read(1),'big'))
                        if len(buf_read) >= int(self.raw_sample)*2:
                            read_trig = 0

                    # トルク値計算
                    if read_trig == 2:
                        # シリアル読み込み
                        buf_loadsell.append(int.from_bytes(ser.read(1),'big'))
                        if len(buf_loadsell) >= int(self.loadsell_sample)*2:
                            read_trig = 0

                     # 計算処理
                    if read_trig == 1:

                        buf_culc = []
                        buf_culc.clear()
                        for i in range(int(len(buf_read)/2)):
                            buf_culc.append((buf_read[i*2] << 8)  + buf_read[i*2+1])

                        loadsell_culc = []
                        loadsell_culc.clear()
                        for i in range(int(len(buf_loadsell)/4)):
                            loadsell_culc.append((loadsell_read[i*2] << 24) + (loadsell_read[i*2+1] << 16) + (loadsell_read[i*2+2] << 8) + loadsell_read[i*2+3])

                        print('start')

                        # 常時行う処理
                        if self.calibration_value != None:
                            self.culc(buf_culc,loadsell_culc) # 電流と回転数の表示

                        if self.serial_task_mode == 0: # パスモード
                            pass

                        elif self.serial_task_mode == 1: # キャリブレーション
                            print('calibration')
                            self.calibration_task(buf_culc,loadsell_culc)
                            self.serial_task_mode = 0

                        elif self.serial_task_mode == 2: # 各種表示
                            cur_list = (buf_culc - self.calibration_value) * self.current_gain / 200 + self.current_offset # 電流計算

                        read_trig = 0

                time.sleep(0.1)

if __name__ == '__main__':
    app = root()


text:
import numpy as np

import torch
from torch import nn
from torch.nn import functional as F
from torch.autograd import Variable

class VAE(nn.Module):
	def __init__(self, encoder, decoder,args):
		super(VAE, self).__init__()
		self.args = args
		self.latent_dim = args.latent_dim
		self.encoder = encoder
		self.decoder = decoder

	def sample_latent_noise(self, mean, log_var):

		std = torch.exp(0.5 * log_var)
		e = torch.randn(mean.shape).cuda()

		return mean + e * std # reparameter trick

	def forward(self, x):
		mean, log_var = self.encoder(x)
		noise = self.sample_latent_noise(mean, log_var)
		output = self.decoder(noise)
		return output, mean, log_var

	def inference(self, number):
		normal_noise = Variable(torch.randn([number, self.latent_dim])).cuda()
		return self.decoder(normal_noise)

max_stars_repo_name: BLUECARVIN/several-VAE

max_issues_repo_name: BLUECARVIN/several-VAE

max_forks_repo_name: BLUECARVIN/several-VAE


text:
#!/usr/bin/env python
"""
Created on Thu Oct 16 13:13:31 2014

Author: Oren Freifeld
Email: freifeld@csail.mit.edu
"""

import numpy as np
from of.utils import *
from numpy.random import multivariate_normal as draw_from_mvnormal
AND = np.logical_and
OR = np.logical_or
class Proposal(object):
    def __init__(self,ms,msp,level,scale=0.01,use_local=False,
                 scale_local_proposal=None):
        self.level=level
        self.ms = ms
        self.msp = msp
        self.scale = scale
        self.use_local=use_local
        if scale_local_proposal is None:
            scale_local_proposal=10
        scale_local_proposal=float(scale_local_proposal)
        self.scale_local_proposal=scale_local_proposal

    def __call__(self,theta_current,theta_proposed,prob_larger=0.1*0,
                 nullify_cells=None):
        """
        Modifies theta_proposed
        """
        if nullify_cells:
            raise ValueError("That was a stupid idea. Don't do it.")
        theta = theta_proposed
        level=self.level
        ms = self.ms
        msp = self.msp
        cpa_space = ms.L_cpa_space[level]

        scale=self.scale
        if prob_larger==0.0:
            pass
        else:
            u = np.random.random()
            if u>1-prob_larger:
                scale = self.scale * 10
            else:
                pass

        if nullify_cells is None:
            if 0:
                msp.sample_normal_in_one_level(level,cpa_space.Avees,theta,
                                          mu=theta_current,scale=scale)
            else:

                if self.use_local == False:
                    covs=msp.L_cpa_space_covs[level]
                    cpa_cov = covs.cpa_cov
                    cpa_cov_diagonal = np.diag(cpa_cov.diagonal())
    #                ipshell('hi')
    #                1/0
                    theta[:]=draw_from_mvnormal(mean=theta_current,cov=scale**2 * cpa_cov_diagonal)
                else:
                    if cpa_space.dim_domain!=1 and cpa_space.tess !='I':
                        raise NotImplementedError(cpa_space.tess)

                    cpa_space.theta2Avees(theta_current)
                    Avees=cpa_space.Avees
                    dim_range = cpa_space.dim_range

                    velTess = cpa_space.zeros_velTess()
                    cpa_space.Avees2velTess(Avees=Avees,velTess=velTess)

#                    vals +=1000*np.random.standard_normal(vals.shape)

                    # TODO: switch to a more intelligent proposal

                    V = cpa_space.local_stuff.vert_tess[:,:cpa_space.dim_domain]
                    scale_local_proposal=self.scale_local_proposal

                    for i in range(1):
                        idx = np.random.random_integers(0,len(velTess)-1)
#                        1/0
#                        velTess[idx]+=1000*np.random.standard_normal(size=dim_range)

                        value = np.random.standard_normal(size=dim_range)
                        if cpa_space.dim_domain>1:
                            s=scale_local_proposal*cpa_space.XMAXS.min()

                            velTess[idx]+=s*value
                        else:

                            velTess[idx]+=.01*value

#                        velTess[idx]+=1*1.0*cpa_space.XMAXS.min()*np.random.standard_normal(size=dim_range)

#                        velTess[idx]+=1.0*cpa_space.XMAXS.min()*np.random.standard_normal(size=dim_range)

                        if any(cpa_space.zero_v_across_bdry):

                            # in principle, the constraint is only about
                            # normals to the bdry.
                            # however, if the variance is too large,
                            # the tangential comp can cause problems,
                            # in the sense it will drive everything
                            # to the corner. So use a hack: force zero.
                            # the projection later may still give values,
                            # but these are faily benign
                            # that are nonzero. Like I said, a hack.
                            for coo in range(cpa_space.dim_domain):
                                if V[idx,coo] in (0,cpa_space.XMAXS[coo]):
                                    velTess[idx]=0
    #                                ipshell('hi')
    #                                1/0

                    cpa_space.project_velTess(velTess,velTess)
#                    ipshell('hi')
#                    1/0

                    cpa_space.velTess2Avees(velTess=velTess,Avees=Avees)
                    cpa_space.Avees2theta(theta=theta)

        if nullify_cells is not None:
            raise NotImplementedError
            covs=msp.L_cpa_space_covs[level]
            cpa_cov = covs.cpa_cov
            pa_cov = covs.pa_cov
            w = np.ones(cpa_space.d_no_constraints)
            w.reshape(cpa_space.nC,-1)[nullify_cells]=0
            print w
            pa_cov2=np.diag(w).dot(pa_cov).dot(np.diag(w))
            cpa_cov2=cpa_space.B.T.dot(pa_cov2).dot(cpa_space.B)

            theta[:]=draw_from_mvnormal(mean=theta_current*0,cov=scale**2 * cpa_cov2 )
#            ipshell('hi')
#            13/0

if __name__ == "__main__":
    pass

max_stars_repo_name: freifeld/cpabDiffeo

max_issues_repo_name: freifeld/cpabDiffeo

max_forks_repo_name: freifeld/cpabDiffeo

reason: import numpy,from numpy