text:
# Standard imports
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pdb

# Initialize global counts
global_success_counter = 0
global_fail_counter = 0

# Common success and fail lists
bool_fail_list = [0, -1, 'True', 'x', 1]
bool_success_list = [False, True]

# MAVE-NN imports
from mavenn.src.error_handling import check, handle_errors
from mavenn.src.validate import validate_alphabet, validate_seqs

# helper method for functional test_for_mistake
def test_for_mistake(func, *args, **kw):
    """
    Run a function with the specified parameters and register whether
    success or failure was a mistake

    parameters
    ----------

    func: (function or class constructor)
        An executable function to which *args and **kwargs are passed.

    return
    ------

    None.
    """

    global global_fail_counter
    global global_success_counter

    # print test number
    test_num = global_fail_counter + global_success_counter
    print('Test # %d: ' % test_num, end='')

    # Run function
    obj = func(*args, **kw)
    # Increment appropriate counter
    if obj.mistake:
        global_fail_counter += 1
    else:
        global_success_counter += 1

def test_parameter_values(func,
                          var_name=None,
                          fail_list=[],
                          success_list=[],
                          **kwargs):
    """
    Tests predictable success & failure of different values for a
    specified parameter when passed to a specified function

    parameters
    ----------

    func: (function)
        Executable to test. Can be function or class constructor.

    var_name: (str)
        Name of variable to test. If not specified, function is
        tested for success in the absence of any passed parameters.

    fail_list: (list)
        List of values for specified variable that should fail

    success_list: (list)
        List of values for specified variable that should succeed

    **kwargs:
        Other keyword variables to pass onto func.

    return
    ------

    None.

    """

    # If variable name is specified, test each value in fail_list
    # and success_list
    if var_name is not None:

        # User feedback
        print("Testing %s() parameter %s ..." % (func.__name__, var_name))

        # Test parameter values that should fail
        for x in fail_list:
            kwargs[var_name] = x
            test_for_mistake(func=func, should_fail=True, **kwargs)

        # Test parameter values that should succeed
        for x in success_list:
            kwargs[var_name] = x
            test_for_mistake(func=func, should_fail=False, **kwargs)

        print("Tests passed: %d. Tests failed: %d.\n" %
              (global_success_counter, global_fail_counter))

    # Otherwise, make sure function without parameters succeeds
    else:

        # User feedback
        print("Testing %s() without parameters." % func.__name__)

        # Test function
        test_for_mistake(func=func, should_fail=False, **kwargs)

    # close all figures that might have been generated
    plt.close('all')

@handle_errors
def additive_model_features(seqs, alphabet, restrict_seqs_to_alphabet=True):
    """
    Compute additive model features from a list of sequences.
    For sequences of length L and an alphabet of length C,
        K = 1 + L*C
    features are encoded, representing constant and
    additive features.

    parameters
    ----------

    seqs: (str or array of str)
        Array of N sequences to encode.

    alphabet: (array of characters)
        Array of C characters from which to build features

    restrict_seqs_to_alphabet: (bool)
        Whether to throw an error if seqs contains characters
        not in alphabet. If False, characters in seqs
        that are not in alphabet will have feature value 0 for
        features that reference that character's position. This
        might cause problems to arise during gauge fixing.

    returns
    -------

    x: (2D np.ndarray)
        A binary numpy array of shape (N,K)

    names: (list of str)
        A list of feature names
    """

    # Validate seqs
    seqs = validate_seqs(seqs, alphabet, restrict_seqs_to_alphabet)

    # Get constant features
    N = len(seqs)
    x_0 = np.ones(N).reshape(N, 1)
    features_0 = ['x_0']

    # Get additive features
    x_lc, features_lc = _seqs_to_x_lc(seqs, alphabet, return_features=True)

    # Concatenate features and feature names
    x = np.hstack([x_0, x_lc])
    names = features_0 + features_lc
    return x, names

@handle_errors
def _seqs_to_x_lc(seqs, alphabet,
                  return_features=False,
                  verbose=False,
                  seq_to_print=0,
                  features_to_print=20):
    # Get N, L, C
    N = len(seqs)
    L = len(seqs[0])
    C = len(alphabet)

    # Get vectors of unique lengths and characters
    l_uniq = np.arange(L).astype(int)
    c_uniq = np.array(list(alphabet))

    # Get (N,L) matrix of sequence characters
    seq_mat = np.array([list(seq) for seq in seqs])

    # Create (L,C) grids of positions and characters
    l_add_grid = np.tile(np.reshape(l_uniq, [L, 1]), [1, C])
    c_add_grid = np.tile(np.reshape(c_uniq, [1, C]), [L, 1])

    # Create (N,L,C) grid of characters in sequences
    seq_add_grid = np.tile(np.reshape(seq_mat, [N, L, 1]), [1, 1, C])

    # Compute (N,L,C) grid of one-hot encoded values
    x_add_grid = (seq_add_grid == c_add_grid[np.newaxis, :, :])

    # Compute number of features K
    K = L * C

    # Compute flattened lists positions and characters
    l_add = l_add_grid.reshape(K)
    c_add = c_add_grid.reshape(K)

    # Create one-hot encoded matrix to return
    x_add = x_add_grid.reshape(N, K)

    # Print features if requested
    if verbose:
        n = seq_to_print
        print(f'x[{n}] = {seqs[n]}')
        ix = x_add[n, :]
        cs = c_add[ix]
        ls = l_add[ix]
        k_max = min(ix.sum(), features_to_print)
        for k in range(k_max):
            name = f"x[{n}]_{ls[k]}:{cs[k]} = True"
            print(name)

    # If return features, create list of feature names and return with x_add
    if return_features:
        feature_names = [f'x_{l_add[k]}:{c_add[k]}' for k in range(K)]
        return x_add, feature_names

    # Otherwise, just return x_add
    else:
        return x_add

@handle_errors
def _seqs_to_x_lclc(seqs, alphabet,
                    return_features=False,
                    verbose=False,
                    seq_to_print=0,
                    features_to_print=20,
                    feature_mask='pairwise'):
    # Get N, L, C
    N = len(seqs)
    L = len(seqs[0])
    C = len(alphabet)

    # Get vectors of unique lengths and characters
    l_uniq = np.arange(L).astype(int)
    c_uniq = np.array(list(alphabet))

    # Get (N,L) matrix of sequence characters
    seq_mat = np.array([list(seq) for seq in seqs])

    # Get additive x_ohe
    x_add = _seqs_to_x_lc(seqs, alphabet)

    # Create (L,C) grids of positions and characters
    l1_grid = np.tile(np.reshape(l_uniq, [L, 1, 1, 1]), [1, C, L, C])
    c1_grid = np.tile(np.reshape(c_uniq, [1, C, 1, 1]), [L, 1, L, C])
    l2_grid = np.tile(np.reshape(l_uniq, [1, 1, L, 1]), [L, C, 1, C])
    c2_grid = np.tile(np.reshape(c_uniq, [1, 1, 1, C]), [L, C, L, 1])

    # Get indices for collapsing dimensions
    if feature_mask == 'neighbor':
        keep = (l1_grid == l2_grid - 1)
        K = int((C ** 2) * (L - 1))

    elif feature_mask == 'pairwise':
        keep = (l1_grid < l2_grid)
        K = int((C ** 2) * L * (L - 1) / 2)

    else:
        print(f'Invalid feature_mask={feature_mask}')

    check(K == keep.ravel().sum(),
          f"This shouldnt ever happen."
          f"K={K} doesn't match keep.ravel().sum()={keep.ravel().sum()}")

    if verbose:
        print(f"K = {K} features")

    # Compute x_ohe for features
    x_add1 = x_add.reshape(N, L, C, 1, 1)
    x_add2 = x_add.reshape(N, 1, 1, L, C)
    x_pair = (x_add1 * x_add2)[:, keep]

    # Print parameters
    l1_pair = l1_grid[keep]
    l2_pair = l2_grid[keep]
    c1_pair = c1_grid[keep]
    c2_pair = c2_grid[keep]

    # Print features if requested
    if verbose:
        n = seq_to_print
        print(f'x[{n}] = {seqs[n]}')
        ix = x_pair[n, :]
        c1s = c1_pair[ix]
        l1s = l1_pair[ix]
        c2s = c2_pair[ix]
        l2s = l2_pair[ix]
        k_max = min(ix.sum(), features_to_print)
        for k in range(k_max):
            name = f"x[{n}]_{l1s[k]}:{c1s[k]},{l2s[k]}:{c2s[k]} = True"
            print(name)

    # If return_features, create a list of feature names and return with x_pair
    if return_features:
        feature_names = [
            f'x_{l1_pair[k]}:{c1_pair[k]},{l2_pair[k]}:{c2_pair[k]}' for k in
            range(K)]
        return x_pair, feature_names
    # Otherwise, just return x_pair
    else:
        return x_pair

@handle_errors
def neighbor_model_features(seqs, alphabet, restrict_seqs_to_alphabet=True):
    """
    Compute neighbor model features from a list of sequences.
    For sequences of length L and an alphabet of length C,
        K = 1 + L*C + (L-1)*C*C
    features are encoded, representing constant, additive,
    and neighbor features.

    parameters
    ----------

    seqs: (str or array of str)
        Array of N sequences to encode.

    alphabet: (array of characters)
        Array of C characters from which to build features

    restrict_seqs_to_alphabet: (bool)
        Whether to throw an error if seqs contains characters
        not in alphabet. If False, characters in seqs
        that are not in alphabet will have feature value 0 for
        features that reference that character's position. This
        might cause problems to arise during gauge fixing.

    returns
    -------

    x: (2D np.ndarray)
        A binary numpy array of shape (N,K)

    names: (list of str)
        A list of feature names
    """

    # Validate seqs
    seqs = validate_seqs(seqs, alphabet, restrict_seqs_to_alphabet)

    # Get constant features
    N = len(seqs)
    x_0 = np.ones(N).reshape(N, 1)
    features_0 = ['x_0']

    # Get additive features
    x_lc, features_lc = _seqs_to_x_lc(seqs, alphabet, return_features=True)

    # Get additive features
    x_lclc, features_lclc = _seqs_to_x_lclc(seqs, alphabet,
                                            return_features=True,
                                            feature_mask="neighbor")

    # Concatenate features
    x = np.hstack([x_0, x_lc, x_lclc])
    names = features_0 + features_lc + features_lclc
    return x, names

@handle_errors
def pairwise_model_features(seqs, alphabet, restrict_seqs_to_alphabet=True):
    """
    Compute pairwise model features from a list of sequences.
    For sequences of length L and an alphabet of length C,
        K = 1 + L*C + (L*(L-1)/2)*C*C
    features are encoded, representing constant, additive,
    and unique pairwise features.

    parameters
    ----------

    seqs: (str or array of str)
        Array of N sequences to encode.

    alphabet: (array of characters)
        Array of C characters from which to build features

    restrict_seqs_to_alphabet: (bool)
        Whether to throw an error if seqs contains characters
        not in alphabet. If False, characters in seqs
        that are not in alphabet will have feature value 0 for
        features that reference that character's position. This
        might cause problems to arise during gauge fixing.

    returns
    -------

    x: (2D np.ndarray)
        A binary numpy array of shape (N,K)

    names: (list of str)
        A list of feature names
    """
    # Validate seqs
    seqs = validate_seqs(seqs, alphabet, restrict_seqs_to_alphabet=True)

    # Get constant features
    N = len(seqs)
    x_0 = np.ones(N).reshape(N, 1)
    features_0 = ['x_0']

    # Get additive features
    x_lc, features_lc = _seqs_to_x_lc(seqs, alphabet, return_features=True)

    # Get additive features
    x_lclc, features_lclc = _seqs_to_x_lclc(seqs, alphabet,
                                            return_features=True,
                                            feature_mask="pairwise")

    # Concatenate features
    x = np.hstack([x_0, x_lc, x_lclc])
    names = features_0 + features_lc + features_lclc
    return x, names

max_stars_repo_name: jbkinney/mave-nn


text:
# -*- coding: utf-8 -*-
from itertools import product

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from statsmodels.graphics.mosaicplot import mosaic

# The most simple use case is to take a dictionary and plot the result
data = {'a': 10, 'b': 15, 'c': 16}
mosaic(data, title='basic dictionary')
plt.show()

# A more useful example is given by a dictionary with multiple indices.  In
# this case we use a wider gap to a better visual separation of the resulting
# plot
data = {('a', 'b'): 1, ('a', 'c'): 2, ('d', 'b'): 3, ('d', 'c'): 4}
mosaic(data, gap=0.05, title='complete dictionary')
plt.show()

# The same data can be given as a simple or hierarchical indexed Series
rand = np.random.random
tuples = list(product(['bar', 'baz', 'foo', 'qux'], ['one', 'two']))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
data = pd.Series(rand(8), index=index)
mosaic(data, title='hierarchical index series')
plt.show()

# The third accepted data structure is the np array, for which a very simple
# index will be created.
rand = np.random.random
data = 1+rand((2, 2))
mosaic(data, title='random non-labeled array')
plt.show()

# If you need to modify the labeling and the coloring you can give a function
# to create the labels and one with the graphical properties starting from the
# key tuple

def props(key):
    return {'color': 'r' if 'a' in key else 'gray'}

def labelizer(key):
    return {('a',): 'first', ('b',): 'second', ('c',): 'third'}[key]

data = {'a': 10, 'b': 15, 'c': 16}
mosaic(data, title='colored dictionary', properties=props, labelizer=labelizer)
plt.show()

# Using a DataFrame as source, specifying the name of the columns of interest
gender = ['male', 'male', 'male', 'female', 'female', 'female']
pet = ['cat', 'dog', 'dog', 'cat', 'dog', 'cat']
data = pd.DataFrame({'gender': gender, 'pet': pet})
mosaic(data, ['pet', 'gender'], title='DataFrame as Source')
plt.show()

ext: py

reason: import numpy,from statsmodels


text:
"""Contains the classes that represent the AWS Step Functions Choice State"""
import ast
from typing import Any, Dict, TYPE_CHECKING

from fluxio_parser.exceptions import assert_supported_operation, UnsupportedOperation
from fluxio_parser.states.base import State, StateMachineFragment
from fluxio_parser.util import convert_input_data_ref, hash_node

if TYPE_CHECKING:
    import networkx as nx

# Map of either:
# * tuple of:
#   ** AST comparison operator
#   ** Python data type
# * AST boolean operator
# to the Amazon States Language string representation
OP_TO_KEY = {
    (ast.Eq, str): "StringEquals",
    (ast.Lt, str): "StringLessThan",
    (ast.Gt, str): "StringGreaterThan",
    (ast.LtE, str): "StringLessThanEquals",
    (ast.GtE, str): "StringGreaterThanEquals",
    (ast.Eq, float): "NumericEquals",
    (ast.Lt, float): "NumericLessThan",
    (ast.Gt, float): "NumericGreaterThan",
    (ast.LtE, float): "NumericLessThanEquals",
    (ast.GtE, float): "NumericGreaterThanEquals",
    (ast.Eq, int): "NumericEquals",
    (ast.Lt, int): "NumericLessThan",
    (ast.Gt, int): "NumericGreaterThan",
    (ast.LtE, int): "NumericLessThanEquals",
    (ast.GtE, int): "NumericGreaterThanEquals",
    (ast.Eq, bool): "BooleanEquals",
    ast.Or: "Or",
    ast.And: "And",
    ast.Not: "Not",
}

# Map of the stringify built-in function name to the actual built-in funciton
TYPE_TO_CLASS = {"str": str, "int": int, "float": float, "bool": bool}

class ChoiceBranch(StateMachineFragment):
    """Choice branch state machine fragment

    Within a Choice state, this represents a single item in the array of Choices. Check
    out the ChoiceState class below for an example.
    """

    def _serialize(self, node: Any) -> Any:
        """Recursive function to serialize a part of the choice branch AST node.

        This looks at each part of the conditional statement's AST to determine the
        correct ASL representation.
        """
        if isinstance(node, ast.BoolOp):
            # e.g. ``___ and ___``
            return {
                OP_TO_KEY[node.op.__class__]: [
                    self._serialize(value) for value in node.values
                ]
            }
        elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not):
            # e.g. ``not bool(data["foo"])``
            if isinstance(node.operand, ast.Call) and node.operand.func.id == "bool":
                value = {
                    "Variable": self._serialize(node.operand),
                    OP_TO_KEY[(ast.Eq, bool)]: True,
                }
            else:
                value = self._serialize(node.operand)
            return {"Not": value}
        elif isinstance(node, ast.Compare):
            # e.g. ``data["foo"] > 0``
            assert_supported_operation(
                len(node.ops) == 1,
                "Only 1 comparison operator at a time is allowed",
                node,
            )
            assert_supported_operation(
                len(node.comparators) == 1,
                "Only 1 comparator at a time is allowed",
                node,
            )
            op = node.ops[0]
            comparator = node.comparators[0]
            # Determine the data type of the choice
            type_ = None
            if isinstance(node.left, ast.Call):
                type_ = node.left.func.id
            if isinstance(comparator, ast.Call):
                assert_supported_operation(
                    type_ is None
                    or (type_ is not None and comparator.func.id == type_),
                    f"Value types must match. Found: {type_} {op.__class__}"
                    f" {comparator.func.id}",
                    node,
                )
                if type_ is None:
                    type_ = comparator.func.id
            elif isinstance(comparator, ast.Str):
                type_ = "str"
            elif isinstance(comparator, ast.Num):
                type_ = "float"
            elif isinstance(comparator, ast.NameConstant) and comparator.value in (
                True,
                False,
            ):
                type_ = "bool"
            elif isinstance(comparator, ast.Subscript):
                raise UnsupportedOperation(
                    "Input data cannot be used as the comparator (right side of operation)",
                    comparator,
                )
            else:
                raise UnsupportedOperation(
                    "Could not determine data type for choice variable", comparator
                )

            type_class = TYPE_TO_CLASS[type_]
            if isinstance(op, ast.NotEq):
                return {
                    "Not": {
                        "Variable": self._serialize(node.left),
                        OP_TO_KEY[(ast.Eq, type_class)]: self._serialize(comparator),
                    }
                }

            return {
                "Variable": self._serialize(node.left),
                OP_TO_KEY[(op.__class__, type_class)]: self._serialize(comparator),
            }

        elif isinstance(node, ast.Subscript):
            # e.g. ``data["foo"]``
            return convert_input_data_ref(node)
        elif isinstance(node, ast.Call):
            # e.g. ``int(data["foo"])``
            assert_supported_operation(
                node.func.id in TYPE_TO_CLASS,
                f"Function {node.func.id} is not supported. Allowed built-ins: "
                ", ".join(TYPE_TO_CLASS.keys()),
                node,
            )
            assert_supported_operation(
                len(node.args) == 1,
                "Data type casting functions only accept 1 positional argument",
                node,
            )
            if node.func.id == "bool":
                return {
                    "Variable": self._serialize(node.args[0]),
                    OP_TO_KEY[(ast.Eq, bool)]: True,
                }
            else:
                return self._serialize(node.args[0])
        elif isinstance(node, ast.NameConstant):
            assert_supported_operation(
                node.value is not None,
                "The value `None` is not allowed in Choice states",
                node,
            )
            return node.value
        elif isinstance(node, ast.Str):
            return node.s
        elif isinstance(node, ast.Num):
            return node.n

        raise UnsupportedOperation("Unsupported choice branch logic", node)

    def to_dict(self) -> Dict:
        """Return a serialized representation of the ChoiceBranch"""
        data = {}
        self._set_end_or_next(data)
        branch = self._serialize(self.ast_node.test)
        assert_supported_operation(
            isinstance(branch, dict),
            "Invalid conditional statement. Most likely there is no comparison or"
            " boolean logic present.",
            self.ast_node.test,
        )
        data.update(branch)
        return data

class ChoiceState(State):
    """Choice state

    A Choice state checks one or more boolean comparisons against the input data in
    order to determine the next state to transition to. Choice states are generated by
    using if/elif/else statements in an .sfn file.

    See: https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-choice-state.html

    For example::

        if data["foo"] >= 10:
            MyTask(key="at-least-10")
        elif data["foo"] < 0:
            MyTask(key="negative")
        else:
            MyTask(key="other")

    resolves to::

        {
            "Type": "Choice",
            "Choices": [
                {
                    "Variable": "$['foo']",
                    "NumericGreaterThanEquals": 10,
                    "Next": "at-least-10"
                },
                {
                    "Variable": "$['foo']",
                    "NumericLessThan": 0,
                    "Next": "negative"
                },
            ],
            "Default": "other"
        }
    """

    def __init__(self, state_graph: "nx.DiGraph", key: str, ast_node: Any) -> None:
        """Initializer

        Args:
            state_graph: DAG of state machine fragments with edges between them
            key: Key of the fragment in the state machine's States value. This only
                really applies to States, not generic fragments.
            ast_node: AST node for this fragment in the .sfn file

        """
        super().__init__(state_graph, key, ast_node)
        self.choice_branches = []
        self.current_choice_branch = None
        self.add_choice_branch(ast_node)

    def add_choice_branch(self, node: Any) -> ChoiceBranch:
        """Build a new choice branch and add it to the list of branches.

        This also updates the current choice branch pointer.

        Args:
            node: AST node representing a choice branch

        Returns:
            new ChoiceBranch instance

        """
        choice_branch = ChoiceBranch(
            self.state_graph, f"ChoiceBranch-{hash_node(node)}", node
        )
        self.choice_branches.append(choice_branch)
        self.current_choice_branch = choice_branch
        return choice_branch

    def shape(self) -> None:
        """Shape the graph for this Choice state node."""
        # Collect a list of nodes that are adjacent to this choice node in the graph
        # but are not linked via an edge with the ``in_else`` flag set to true. These
        # nodes represent the next state *after* the choice state.
        non_else_nodes = [
            node
            for node, edge_attrs in self.state_graph.adj[self].items()
            if not edge_attrs.get("in_else")
        ]
        assert_supported_operation(
            len(non_else_nodes) <= 1,
            "A maximum of 1 state can be downstream from a Choice state",
            self.ast_node,
        )
        if len(non_else_nodes) == 1:
            # For each choice branch, if it ends with a non-terminal state then add an
            # edge to the next state *after* the choice state.
            for branch in self.choice_branches:
                descendants = branch.descendants
                if len(descendants) > 0 and not descendants[-1].TERMINAL:
                    self.state_graph.add_edge(descendants[-1], non_else_nodes[0])

    def to_dict(self):
        """Return a serialized representation of the Choice state."""
        data = {
            "Type": "Choice",
            "Choices": [c.to_dict() for c in self.choice_branches],
        }

        # Collect a list of nodes that are adjacent to this choice node in the graph
        # that are linked via an edge with the ``in_else`` flag set to true. These
        # nodes represent the Default choice if none of the branches trigger.
        else_nodes = [
            node
            for node, edge_attrs in self.state_graph.adj[self].items()
            if edge_attrs.get("in_else")
        ]
        assert_supported_operation(
            len(else_nodes) <= 1,
            "A maximum of 1 state can be included in an `else` clause",
            self.ast_node,
        )
        if len(else_nodes) == 1:
            data["Default"] = else_nodes[0].key

        return data

max_stars_repo_name: NarrativeScience/fluxio-parser

max_issues_repo_name: NarrativeScience/fluxio-parser

max_forks_repo_name: NarrativeScience/fluxio-parser

reason: import networkx


text:
# tests.test_style.test_palettes
# Tests the palettes module of the yellowbrick library.
#
# Author:   Benjamin Bengfort <bbengfort@districtdatalabs.com>
# Created:  Tue Oct 04 16:21:58 2016 -0400
#
# Copyright (C) 2016 District Data Labs
# For license information, see LICENSE.txt
#
# ID: test_palettes.py [c6aff34] benjamin@bengfort.com $

"""
Tests the palettes module of the yellowbrick library.
"""

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

import unittest
import numpy as np
import matplotlib as mpl

from yellowbrick.exceptions import *
from yellowbrick.style.palettes import *
from yellowbrick.style.colors import get_color_cycle
from yellowbrick.style.rcmod import set_aesthetic, set_palette
from yellowbrick.style.palettes import color_sequence, color_palette
from yellowbrick.style.palettes import ColorPalette, PALETTES, SEQUENCES

from tests.base import VisualTestCase

##########################################################################
## Color Palette Tests
##########################################################################

class ColorPaletteObjectTests(VisualTestCase):
    """
    Tests the ColorPalette object
    """

    def test_init_palette_by_name(self):
        """
        Test that a palette can be initialized by name
        """

        # Try all the names in the palettes
        for name, value in PALETTES.items():
            try:
                palette = ColorPalette(name)
            except YellowbrickValueError:
                self.fail(
                    "Could not instantiate {} color palette by name".format(name)
                )

            self.assertEqual(value, palette)

        # Try a name not in PALETTES
        with self.assertRaises(YellowbrickValueError):
            self.assertNotIn('foo', PALETTES, "Cannot test bad name 'foo' it is in PALETTES!")
            palette = ColorPalette('foo')

    def test_init_palette_by_list(self):
        """
        Test that a palette can be initialized by a list
        """

        # Try all the values in the palettes (HEX)
        for value in PALETTES.values():
            palette = ColorPalette(value)
            self.assertEqual(len(value), len(palette))

        # Try all the values converted to RGB
        for value in PALETTES.values():
            palette = ColorPalette(map(mpl.colors.colorConverter.to_rgb, value))
            self.assertEqual(len(value), len(palette))

    def test_color_palette_context(self):
        """
        Test ColorPalette context management
        """
        default = color_palette()
        context = color_palette('dark')

        with ColorPalette('dark') as palette:
            self.assertIsInstance(palette, ColorPalette)
            self.assertEqual(get_color_cycle(), context)

        self.assertEqual(get_color_cycle(), default)

    def test_as_hex_as_rgb(self):
        """
        Test the conversion of a ColorPalette to hex values and back to rgb
        """
        palette   = color_palette('flatui')
        expected  = PALETTES['flatui']
        morgified = palette.as_hex()

        self.assertIsNot(morgified, palette)
        self.assertIsInstance(morgified, ColorPalette)
        self.assertEqual(morgified, expected)

        remorgified = morgified.as_rgb()
        self.assertIsNot(remorgified, morgified)
        self.assertIsNot(remorgified, palette)
        self.assertEqual(remorgified, palette)

    @unittest.skip("not implemented yet")
    def test_plot_color_palette(self):
        """
        Test the plotting of a color palette for color visualization
        """
        raise NotImplementedError(
            "Not quite sure how to implement this yet"
        )

class ColorPaletteFunctionTests(VisualTestCase):
    """
    Tests the color_palette function.
    """

    def test_current_palette(self):
        """
        Test modifying the current palette with a simple palette
        """
        pal = color_palette(["red", "blue", "green"], 3)
        set_palette(pal, 3)
        self.assertEqual(pal, get_color_cycle())

        # Reset the palette
        set_aesthetic()

    def test_palette_context(self):
        """
        Test the context manager for the color_palette function
        """

        default_pal = color_palette()
        context_pal = color_palette("muted")

        with color_palette(context_pal):
            self.assertEqual(get_color_cycle(), context_pal)

        self.assertEqual(get_color_cycle(), default_pal)

    def test_big_palette_context(self):
        """
        Test that the context manager also resets the number of colors
        """

        original_pal = color_palette("accent", n_colors=8)
        context_pal = color_palette("bold", 10)

        set_palette(original_pal)
        with color_palette(context_pal, 10):
            self.assertEqual(get_color_cycle(), context_pal)

        self.assertEqual(get_color_cycle(), original_pal)

        # Reset default
        set_aesthetic()

    def test_yellowbrick_palettes(self):
        """
        Test the yellowbrick palettes have length 6 (bgrmyck)
        """
        pals = ["accent", "dark", "pastel", "bold", "muted"]
        for name in pals:
            pal_out = color_palette(name)
            self.assertEqual(len(pal_out), 6, "{} is not of len 6".format(name))

    def test_seaborn_palettes(self):
        """
        Test the seaborn palettes have length 6 (bgrmyck)
        """
        pals = ["sns_deep", "sns_muted", "sns_pastel",
                "sns_bright", "sns_dark", "sns_colorblind"]
        for name in pals:
            pal_out = color_palette(name)
            self.assertEqual(len(pal_out), 6)

    def test_other_palettes(self):
        """
        Test that the other palettes exist
        """
        pals = ["flatui", "paired", "neural_paint", "set1"]
        for name in pals:
            pal_out = color_palette(name)
            self.assertTrue(pal_out)

    def test_bad_palette_name(self):
        """
        Test that a bad palette name raises an exception
        """

        with self.assertRaises(ValueError):
            color_palette("IAmNotAPalette")

        with self.assertRaises(YellowbrickValueError):
            color_palette("IAmNotAPalette")

    def test_bad_palette_colors(self):
        """
        Test that bad color names raise an exception
        """

        pal = ["red", "blue", "iamnotacolor"]
        with self.assertRaises(ValueError):
            color_palette(pal)

        with self.assertRaises(YellowbrickValueError):
            color_palette(pal)

    def test_palette_is_list_of_tuples(self):
        """
        Assert that color_palette returns a list of RGB tuples
        """

        pal_in = np.array(["red", "blue", "green"])
        pal_out = color_palette(pal_in, 3)

        self.assertIsInstance(pal_out, list)
        self.assertIsInstance(pal_out[0], tuple)
        self.assertIsInstance(pal_out[0][0], float)
        self.assertEqual(len(pal_out[0]), 3)

    def test_palette_cycles(self):
        """
        Test that the color palette cycles for more colors
        """
        accent = color_palette("accent")
        double_accent = color_palette("accent", 12)
        self.assertEqual(double_accent, accent + accent)

    @unittest.skip("Discovered this commented out, don't know why")
    def test_cbrewer_qual(self):
        """
        Test colorbrewer qualitative palettes
        """
        pal_short = mpl_palette("Set1", 4)
        pal_long = mpl_palette("Set1", 6)
        self.assertEqual(pal_short, pal_long[:4])

        pal_full = palettes.mpl_palette("Set2", 8)
        pal_long = palettes.mpl_palette("Set2", 10)
        self.assertEqual(pal_full, pal_long[:8])

    def test_color_codes(self):
        """
        Test the setting of color codes
        """
        set_color_codes("accent")
        colors = color_palette("accent") + ["0.06666666666666667"]
        for code, color in zip("bgrmyck", colors):
            rgb_want = mpl.colors.colorConverter.to_rgb(color)
            rgb_got = mpl.colors.colorConverter.to_rgb(code)
            self.assertEqual(rgb_want, rgb_got)
        set_color_codes("reset")

    def test_as_hex(self):
        """
        Test converting a color palette to hex and back to rgb.
        """
        pal = color_palette("accent")
        for rgb, hex in zip(pal, pal.as_hex()):
            self.assertEqual(mpl.colors.rgb2hex(rgb), hex)

        for rgb_e, rgb_v in zip(pal, pal.as_hex().as_rgb()):
            self.assertEqual(rgb_e, rgb_v)

    def test_preserved_palette_length(self):
        """
        Test palette length is preserved when modified
        """
        pal_in = color_palette("Set1", 10)
        pal_out = color_palette(pal_in)
        self.assertEqual(pal_in, pal_out)

    def test_color_sequence(self):
        """
        Ensure the color sequence returns listed colors.
        """
        for name, ncols in SEQUENCES.items():
            for n in ncols.keys():
                cmap = color_sequence(name, n)
                self.assertEqual(name, cmap.name)
                self.assertEqual(n, cmap.N)

    def test_color_sequence_default(self):
        """
        Assert the default color sequence is RdBu
        """
        cmap = color_sequence()
        self.assertEqual(cmap.name, "RdBu")
        self.assertEqual(cmap.N, 11)

    def test_color_sequence_unrecocognized(self):
        """
        Test value errors for unrecognized sequences
        """
        with self.assertRaises(YellowbrickValueError):
            color_sequence('PepperBucks', 3)

    def test_color_sequence_bounds(self):
        """
        Test color sequence out of bounds value error
        """
        with self.assertRaises(YellowbrickValueError):
            color_sequence('RdBu', 18)

        with self.assertRaises(YellowbrickValueError):
            color_sequence('RdBu', 2)

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

max_stars_repo_name: souravsingh/yellowbrick

max_issues_repo_name: souravsingh/yellowbrick

max_forks_repo_name: souravsingh/yellowbrick

reason: import numpy


text:
import numpy as np
import cv2

def perspective_transform(scene, added_width):
    if added_width == 0:
        raise UserWarning('perspective_transform only needed if added_width > 0')

    offset = int(added_width / 2)

    matrix = cv2.getPerspectiveTransform(
        np.float32([[0, 0],      [scene.shape[1], 0],          [scene.shape[1], scene.shape[0]], [0, scene.shape[0]]]),
        np.float32([[offset, 0], [scene.shape[1] - offset, 0], [scene.shape[1], scene.shape[0]], [0, scene.shape[0]]])
    )

    transformed_background = cv2.warpPerspective(scene, matrix, (scene.shape[1], scene.shape[0]))

    if len(scene.shape) == 3:
        transformed_background = transformed_background[:, offset:-offset, :]
    else:
        transformed_background = transformed_background[:, offset:-offset]

    return transformed_background

def bbox_perspective_transform(bbox, added_width, scene_size):
    if added_width == 0:
        raise UserWarning('perspective_transform only needed if added_width > 0')

    offset = int(added_width / 2)

    matrix = cv2.getPerspectiveTransform(
        np.float32([[0, 0],      [scene_size[1], 0],          [scene_size[1], scene_size[0]], [0, scene_size[0]]]),
        np.float32([[offset, 0], [scene_size[1] - offset, 0], [scene_size[1], scene_size[0]], [0, scene_size[0]]])
    )

    [[(x_min1, y_max1), (x_min2, y_min1), (x_max1, y_min2), (x_max2, y_max2), (_, _)]] = cv2.perspectiveTransform(bbox, matrix).astype(int)
    x_min = min(x_min1, x_min2)
    y_min = min(y_min1, y_min2)
    x_max = max(x_max1, x_max2)
    y_max = max(y_max1, y_max2)

    return [(x_min, y_max), (x_min, y_min), (x_max, y_min), (x_max, y_max), (x_min, y_max)]
    # return cv2.perspectiveTransform(bbox, matrix).astype(int)[0]


text:
import numpy as np
import pickle
from netCDF4 import Dataset
from grid import Grid
from rsw import RSW
import plotting

def domovie(expdir, varname, extraparam={}):
    """Generate a movie from a history file

    Parameters
    ----------
    expdir: str, the experiment path

    varname: str, the variable nickname (h, vor, pv etc)

    extraparam: dict, plotting parameters. By default, it uses the
    parameters saved in the param.pkl file

    Example
    -------
    >>> domovie("/home/roullet/data/pyRSW/myexp2", "vor", {"colorscheme": "imposed", "cax": [-0.005, 0.008]})
    """

    paramfile = f"{expdir}/param.pkl"
    with open(paramfile, "rb") as fid:
        param = pickle.load(fid)

    for key, val in extraparam.items():
        setattr(param, key, val)

    param.generate_mp4 = True
    param.plotvar = varname

    grid = Grid(param)
    model = RSW(param, grid)

    hisfile = f"{expdir}/history_00.nc"
    with Dataset(hisfile) as nc:
        nt = len(nc.dimensions["t"])

        data = model.state.get(varname).view("i")

        kt = 0
        data[:] = nc.variables[varname][kt]
        time = nc.variables["t"][kt]
        fig = plotting.Figure(param, grid, model.state, time)

        for kt in range(1, nt):
            data[:] = nc.variables[varname][kt]
            time = nc.variables["t"][kt]
            print(f"\rkt={kt}  t={time:.2}", end="")
            fig.update(time, model.state)

        fig.finalize()

max_stars_repo_name: pvthinker/pyRSW

max_issues_repo_name: pvthinker/pyRSW

max_forks_repo_name: pvthinker/pyRSW


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

"""
from __future__ import division

import numpy as np

import pycity_calc.toolbox.modifiers.mod_city_sh_dem as modsh

class TestModCity(object):

    def test_sh_curve_summer_off(self):

        sh_array = np.ones(365 * 24) * 10000

        sh_dem_before = sum(sh_array) * 3600 / (1000 * 3600)

        sh_array_after = modsh.sh_curve_summer_off(sh_array=sh_array, resc=1)
        sh_dem_after = sum(sh_array_after) * 3600 / (1000 * 3600)

        assert abs(sh_dem_after - sh_dem_before) <= 0.001

        sh_array_after = modsh.sh_curve_summer_off(sh_array=sh_array, resc=0.5)
        sh_dem_after = sum(sh_array_after) * 3600 / (1000 * 3600)

        assert sh_dem_before > sh_dem_after


text:
#!/usr/bin/env python3

from typing import Any, Dict, Iterator, List, Optional, Type, Union

import habitat_sim as hsim
import numpy as np
from gym.spaces import Dict as SpaceDict
from habitat.config import Config
from habitat.core.dataset import Dataset, Episode
from habitat.core.embodied_task import EmbodiedTask
from habitat.core.env import Env
from habitat.core.simulator import Observations, Simulator

class PhysicsEnv(Env):
    r"""Fundamental environment class for :ref:`habitat`.

    :data observation_space: ``SpaceDict`` object corresponding to sensor in
        sim and task.
    :data action_space: ``gym.space`` object corresponding to valid actions.

    All the information  needed for working on embodied tasks with simulator
    is abstracted inside :ref:`Env`. Acts as a base for other derived
    environment classes. :ref:`Env` consists of three major components:
    ``dataset`` (`episodes`), ``simulator`` (:ref:`sim`) and :ref:`task` and
    connects all the three components together.
    """

    observation_space: SpaceDict
    action_space: SpaceDict
    _config: Config
    _dataset: Optional[Dataset]
    number_of_episodes: Optional[int]
    _episodes: List[Type[Episode]]
    _current_episode_index: Optional[int]
    _current_episode: Optional[Type[Episode]]
    _episode_iterator: Optional[Iterator]
    _sim: Simulator
    _task: EmbodiedTask
    _max_episode_seconds: int
    _max_episode_steps: int
    _elapsed_steps: int
    _episode_start_time: Optional[float]
    _episode_over: bool

    def __init__(self, config: Config, dataset: Optional[Dataset] = None) -> None:
        """Constructor

        :param config: config for the environment. Should contain id for
            simulator and ``task_name`` which are passed into ``make_sim`` and
            ``make_task``.
        :param dataset: reference to dataset for task instance level
            information. Can be defined as :py:`None` in which case
            ``_episodes`` should be populated from outside.
        """
        super().__init__(config, dataset)

        # declare the physics object attributes manager;
        # this should be (re)-instantiated on every reset
        self.obj_templates_mgr = None

        # declare the rigid object manager;
        # this should be (re)-instantiated on every reset
        self.rigid_obj_mgr = None

        # declare the agent object
        self.agent_object = None

    def step_physics(
        self,
        action: Union[int, str, Dict[str, Any]] = None,
        time_step=1.0 / 60.0,
        control_period=1.0,
        **kwargs
    ) -> Observations:
        r"""Perform an action in the environment, with physics enabled, and
        return observations.

        :param action: action (belonging to :ref:`action_space`) to be
            performed inside the environment. Action is a name or index of
            allowed task's action and action arguments (belonging to action's
            :ref:`action_space`) to support parametrized and continuous
            actions. If Action is none, then iterate by one time step only.
        :param time_step: time step for physics simulation
        :param control_period:
        :return: observations after taking action in environment.
        """
        assert (
            self._episode_start_time is not None
        ), "Cannot call step before calling reset"
        # NOTE: we relax the episode-is-not-over condition due
        # to the roam mode
        # assert (
        #    self._episode_over is False
        # ), "Episode over, call reset before calling step"

        # Support simpler interface as well
        if action is not None and (
            isinstance(action, str) or isinstance(action, (int, np.integer))
        ):
            action = {"action": action}

        # Step with physics
        # double-check to make sure the agent object is already define by this point
        assert self.agent_object is not None
        observations = self.task.step_physics(
            action=action,
            episode=self.current_episode,
            time_step=time_step,
            control_period=control_period,
            agent_object=self.agent_object,
        )

        self._task.measurements.update_measures(
            episode=self.current_episode, action=action, task=self.task
        )

        self._update_step_stats()

        return observations

    def reset(self) -> Observations:
        r"""Resets the environments and returns the initial observations.
        Also initialize physics stuff on the basis of the tutorial from
        https://aihabitat.org/docs/habitat-sim/managed-rigid-object-tutorial.html.

        :return: initial observations from the environment.
        """
        self._reset_stats()

        assert len(self.episodes) > 0, "Episodes list is empty"
        # Delete the shortest path cache of the current episode
        # Caching it for the next time we see this episode isn't really worth
        # it
        if self._current_episode is not None:
            self._current_episode._shortest_path_cache = None

        self._current_episode = next(self._episode_iterator)

        # remove all objects in the scene, but keep their scene nodes
        obj_handles = self._sim.get_rigid_object_manager().get_object_handles()
        for obj_handle in obj_handles:
            self.rigid_obj_mgr.remove_object_by_handle(
                obj_handle, delete_object_node=False, delete_visual_node=False
            )

        # restart the simulator instance
        self.reconfigure(self._config)

        observations = self.task.reset(episode=self.current_episode)
        self._task.measurements.reset_measures(
            episode=self.current_episode, task=self.task
        )

        # re-instantiate the physics object attributes manager
        self.obj_templates_mgr = self._sim.get_object_template_manager()
        # re-instantiate the rigid object manager
        self.rigid_obj_mgr = self._sim.get_rigid_object_manager()

        # load locobot asset and attach it to the agent's scene node
        locobot_template_id = self.obj_templates_mgr.load_configs(
            "data/objects/locobot_merged"
        )[0]
        self.obj_templates_mgr.get_template_by_id(
            locobot_template_id
        ).angular_damping = 0.0
        self.agent_object = self.rigid_obj_mgr.add_object_by_template_id(
            locobot_template_id, self._sim.agents[0].scene_node
        )
        assert self.agent_object is not None

        # set all objects in the scene to be dynamic and collidable
        obj_handles = self.rigid_obj_mgr.get_object_handles()
        for obj_handle in obj_handles:
            obj = self.rigid_obj_mgr.get_object_by_handle(obj_handle)
            obj.motion_type = hsim.physics.MotionType.DYNAMIC
            obj.collidable = True

        return observations

    def set_agent_velocities(
        self, linear_vel: np.ndarray, angular_vel: np.ndarray
    ) -> None:
        r"""
        Set linear and angular velocity for the agent in the environment.
        Can only be called when physics is turned on.
        :param linear_vel: linear velocity
        :param angular_vel: angular velocity
        """
        vel_control = self.agent_object.velocity_control
        vel_control.controlling_lin_vel = True
        vel_control.controlling_ang_vel = True
        vel_control.lin_vel_is_local = True
        vel_control.ang_vel_is_local = True
        vel_control.linear_velocity = linear_vel
        vel_control.angular_velocity = angular_vel


text:
import numpy as np
import tensorflow as tf

from neuralmonkey.dataset import Dataset
from neuralmonkey.encoders.sentence_encoder import SentenceEncoder
from neuralmonkey.decoders.decoder import Decoder
from neuralmonkey.logging import warn
from neuralmonkey.model.model_part import ModelPart, FeedDict

class WordAlignmentDecoder(ModelPart):
    """
    A decoder that computes soft alignment from an attentive encoder. Loss is
    computed as cross-entropy against a reference alignment.
    """

    def __init__(self,
                 encoder: SentenceEncoder,
                 decoder: Decoder,
                 data_id: str,
                 name: str) -> None:
        ModelPart.__init__(self, name, None, None)

        self.encoder = encoder
        self.decoder = decoder
        self.data_id = data_id

        self.ref_alignment = tf.placeholder(
            tf.float32,
            [None, self.decoder.max_output_len, self.encoder.max_input_len],
            name="ref_alignment")

        # shape will be [max_output_len, batch_size, max_input_len]
        self.alignment_target = tf.transpose(self.ref_alignment,
                                             perm=[1, 0, 2])

        _, self.train_loss = self._make_decoder(runtime_mode=False)
        self.decoded, self.runtime_loss = self._make_decoder(runtime_mode=True)

        tf.summary.scalar("alignment_train_xent", self.train_loss,
                          collections=["summary_train"])

    def _make_decoder(self, runtime_mode=False):
        attn_obj = self.decoder.get_attention_object(self.encoder,
                                                     runtime_mode)

        alignment_logits = tf.stack(attn_obj.logits_in_time,
                                    name="alignment_logits")

        if runtime_mode:
            # make batch_size the first dimension
            alignment = tf.transpose(tf.nn.softmax(alignment_logits),
                                     perm=[1, 0, 2])
            loss = tf.constant(0)
        else:
            alignment = None

            xent = tf.nn.softmax_cross_entropy_with_logits(
                labels=self.alignment_target, logits=alignment_logits)
            loss = tf.reduce_sum(xent * self.decoder.train_padding)

        return alignment, loss

    @property
    def cost(self) -> tf.Tensor:
        return self.train_loss

    def feed_dict(self, dataset: Dataset, train: bool = False) -> FeedDict:
        fd = {}

        alignment = dataset.get_series(self.data_id, allow_none=True)
        if alignment is None:
            if train:
                warn("Training alignment not present!")

            alignment = np.zeros((len(dataset),
                                  self.decoder.max_output_len,
                                  self.encoder.max_input_len), np.float32)

        fd[self.ref_alignment] = alignment

        return fd


text:
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import math, copy, time
from torch.autograd import Variable
from Model import subsequent_mask
import matplotlib.pyplot as plt
import seaborn
from HyperParameter import epoches_of_loss_record, epoches_of_model_save, beam_search_number, A_end_index, B_start_index,chunk_len

# =============================================================================
#
# Training : 模型训练
#
# =============================================================================

# 接下来，我们创建一个通用的训练和评分功能来跟踪损失。我们传递一个通用的损失计算函数，该函数还处理参数更新。

def run_epoch(stage, data_iter, model, loss_compute, nbatches, epoch=0):
    "Standard Training and Logging Function"
    start = time.time()
    total_tokens = 0
    total_loss = 0
    tokens = 0
    for i, batch in enumerate(data_iter):
        out = model.forward(batch.src, batch.trg, batch.src_mask, batch.trg_mask)
        loss = loss_compute(out, batch.trg_y, batch.ntokens)

        total_loss += loss.detach().cpu().numpy()
        total_tokens += batch.ntokens.cpu().numpy()
        tokens += batch.ntokens.cpu().numpy()
        if i == nbatches - 1:
            elapsed = time.time() - start
            print("%s  Loss: %f Time: %f Tokens per Sec: %f" % (
            stage, loss.detach().cpu().numpy() / batch.ntokens.cpu().numpy(), elapsed, tokens / elapsed))
            if epoch % epoches_of_loss_record == 0:
                f = open("procedure.txt", "a+")
                f.write("%s  Loss: %f Time: %f Tokens per Sec: %f" % (
                stage, loss.detach().cpu().numpy() / batch.ntokens.cpu().numpy(), elapsed, tokens / elapsed))
                f.close()
            start = time.time()
            tokens = 0
    if epoch != 0 and epoch % epoches_of_model_save == 0:
        torch.save(model,"transformer_" + "{}".format(epoch) + "_" + str(chunk_len) + "_" + str(A_end_index) + "_" + str(B_start_index) + ".model")

# 损失计算
class SimpleLossCompute:
    "一个简单的损失计算和训练函数"

    def __init__(self, generator, criterion, opt=None):
        self.generator = generator
        self.criterion = criterion
        self.opt = opt

    def __call__(self, x, y, norm):
        x = self.generator(x)
        loss = self.criterion(x.contiguous().view(-1, x.size(-1)), y.contiguous().view(-1)) / norm
        loss.backward()
        if self.opt is not None:
            self.opt.step()
            self.opt.optimizer.zero_grad()
        return loss.data.item() * norm.float()

# 贪婪解码
# 为了简单起见，此代码使用贪婪解码来预测。
def greedy_decode(model, src, src_mask, max_len):
    memory = model.encode(src, src_mask)
    ys = torch.ones(1, 1).fill_(src[0][-1].cpu().numpy().item()).type_as(src.data)
    for i in range(max_len):
        out = model.decode(memory, src_mask,Variable(ys),Variable(subsequent_mask(ys.size(1)).type_as(src.data)))
        prob = model.generator(out[:, -1])
        _, next_word = torch.max(prob, dim=1)
        next_word = next_word.data[0]
        ys = torch.cat([ys,
                        torch.ones(1, 1).type_as(src.data).fill_(next_word)], dim=1)
    return ys

def beam_search_decode(model, src, src_mask, max_len):
    memory = model.encode(src, src_mask)
    ys = [0,torch.ones(1, 1).fill_(src[0][-1].cpu().numpy().item()).type_as(src.data)]
    reserved_options = choose_options(model, memory, src, src_mask, ys)
    for i in range(max_len-1):
        tmp_options=[]
        for j in range(len(reserved_options)):
            tmp_options+=choose_options(model,memory,src,src_mask,reserved_options[j])
        tmp_options=sorted(tmp_options,reverse=True)[:beam_search_number]
        reserved_options=tmp_options
    return reserved_options

def choose_options(model,memory,src,src_mask,ys):
    out = model.decode(memory, src_mask, Variable(ys[1]), Variable(subsequent_mask(ys[1].size(1)).type_as(src.data)))
    prob = model.generator(out[:, -1])
    dict = {}
    for j in range(prob.size()[-1]):
        dict[j] = prob[0][j].item()
    sort_dict = sorted(zip(dict.values(), dict.keys()), reverse=True)
    options = sort_dict[:beam_search_number]
    result=[]
    for i in range(beam_search_number):
        result.append([ys[0]+options[i][0],torch.cat([ys[1], torch.ones(1, 1).type_as(src.data).fill_(options[i][1])], dim=1)])
    return result

def run_epoch_kg(stage, data_iter, model, loss_compute, nbatches, epoch=0):
    "Standard Training and Logging Function"
    start = time.time()
    total_tokens = 0
    total_loss = 0
    tokens = 0
    for i, batch in enumerate(data_iter):
        out = model.forward(batch.src, batch.ent, batch.trg, batch.src_mask, batch.ent_mask, batch.trg_mask)
        loss = loss_compute(out, batch.trg_y, batch.ntokens)

        total_loss += loss.detach().cpu().numpy()
        total_tokens += batch.ntokens.cpu().numpy()
        tokens += batch.ntokens.cpu().numpy()
        if i == nbatches - 1:
            elapsed = time.time() - start
            print("%s  Loss: %f Time: %f Tokens per Sec: %f" % (
            stage, loss.detach().cpu().numpy() / batch.ntokens.cpu().numpy(), elapsed, tokens / elapsed))
            if epoch % epoches_of_loss_record == 0:
                f = open("procedure.txt", "a+")
                f.write("%s  Loss: %f Time: %f Tokens per Sec: %f" % (
                stage, loss.detach().cpu().numpy() / batch.ntokens.cpu().numpy(), elapsed, tokens / elapsed))
                f.close()
            start = time.time()
            tokens = 0
    if epoch != 0 and epoch % epoches_of_model_save == 0:
        torch.save(model,"transformer_kg_" + "{}".format(epoch) + "_" + str(chunk_len) + "_" + str(A_end_index) + "_" + str(B_start_index) + ".model")

def beam_search_decode_kg(model, src, src_mask, ent, ent_mask, max_len):
    memory = model.encode(src, src_mask, ent, ent_mask)
    ys = [0, torch.ones(1, 1).fill_(src[0][-1].cpu().numpy().item()).type_as(src.data)]
    reserved_options = choose_options(model, memory, src, src_mask, ys)
    for i in range(max_len-1):
        tmp_options = []
        for j in range(len(reserved_options)):
            tmp_options += choose_options(model, memory, src, src_mask, reserved_options[j])
        tmp_options = sorted(tmp_options,reverse=True)[:beam_search_number]
        reserved_options = tmp_options
    return reserved_options

max_stars_repo_name: RongTouchTouch/AutoComplete

max_issues_repo_name: RongTouchTouch/AutoComplete

max_forks_repo_name: RongTouchTouch/AutoComplete


text:
import pytest
import requests
import scipy.io.wavfile
import numpy

def test_swagger():

    model_endpoint = 'http://localhost:5000/swagger.json'

    r = requests.get(url=model_endpoint)
    assert r.status_code == 200
    assert r.headers['Content-Type'] == 'application/json'

    json = r.json()
    assert 'swagger' in json
    assert json.get('info') and json.get('info').get('title') == 'Model Asset Exchange Server'

def test_metadata():

    model_endpoint = 'http://localhost:5000/model/metadata'

    r = requests.get(url=model_endpoint)
    assert r.status_code == 200

    metadata = r.json()
    assert metadata['id'] == 'wavegan'
    assert metadata['name'] == 'WaveGAN audio generation model'
    assert metadata['description'] == 'Generative Adversarial Network, trained using TensorFlow on spoken commands '\
                                      'and lo-fi instrumental music'
    assert metadata['license'] == 'Apache2'

def test_predict():

    model_endpoint = 'http://localhost:5000/model/predict'

    models = ["lofi-instrumentals", "up", "down", "left", "right", "stop", "go"]

    for model in models:
        print("Testing " + model)
        r = requests.get(url=model_endpoint + "?model=" + model)
        assert r.status_code == 200
        f = open("/tmp/max-testing-" + model + ".wav", 'wb')
        f.write(r.content)
        f.close()

        audio = scipy.io.wavfile.read("/tmp/max-testing-" + model + ".wav")

        assert audio[0] == 16000  # 16k sample rate

        audio_len = len(audio[1]) / audio[0]  # length in seconds = samples / sample rate
        assert 1.5 > audio_len > 0.5  # all the files are around 1 second

        assert numpy.max(audio[1]) > 0
        assert numpy.min(audio[1]) < 0

        audio_range = numpy.max(audio[1]) - numpy.min(audio[1])
        assert audio_range > 0.05  # this is a very small range, the model should be improved in the future

if __name__ == '__main__':
    pytest.main([__file__])

reason: import numpy,import scipy


text:
from __future__ import print_function
import argparse
import os
import random
import numpy as np
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from torch.autograd import Variable
from datasets import PartDataset
from pointnet import PointNetCls
from pointnet import FoldingNet
from pointnet import ChamferLoss
import torch.nn.functional as F
from visdom import Visdom
import time
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

vis = Visdom()
line = vis.line(np.arange(10))

parser = argparse.ArgumentParser()
parser.add_argument('--batchSize', type=int, default=8, help='input batch size')
parser.add_argument('--num_points', type=int, default=2500, help='input batch size')
parser.add_argument('--workers', type=int, help='number of data loading workers', default=4)
parser.add_argument('--nepoch', type=int, default=25, help='number of epochs to train for')
parser.add_argument('--outf', type=str, default='cls',  help='output folder')
parser.add_argument('--model', type=str, default = '',  help='model path')

opt = parser.parse_args()
opt.nepoch = 200            # yw add
print(opt)

blue = lambda x:'\033[94m' + x + '\033[0m'

opt.manualSeed = random.randint(1, 10000) # fix seed
print("Random Seed: ", opt.manualSeed)
random.seed(opt.manualSeed)
torch.manual_seed(opt.manualSeed)

dataset = PartDataset(root = 'shapenetcore_partanno_segmentation_benchmark_v0', classification = True, npoints = opt.num_points)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,
                                          shuffle=True, num_workers=int(opt.workers))

test_dataset = PartDataset(root = 'shapenetcore_partanno_segmentation_benchmark_v0', classification = True, train = False, npoints = opt.num_points)
testdataloader = torch.utils.data.DataLoader(test_dataset, batch_size=opt.batchSize,
                                          shuffle=True, num_workers=int(opt.workers))

print(len(dataset), len(test_dataset))
num_classes = len(dataset.classes)
print('classes', num_classes)

try:
    os.makedirs(opt.outf)
except OSError:
    pass

#classifier = PointNetCls(k = num_classes)
foldingnet = FoldingNet()

if opt.model != '':
    foldingnet.load_state_dict(torch.load(opt.model))

#optimizer = optim.SGD(foldingnet.parameters(), lr=0.01, momentum=0.9)
optimizer = optim.Adam(foldingnet.parameters(),lr = 0.0001,weight_decay=1e-6)
foldingnet.cuda()

num_batch = len(dataset)/opt.batchSize

chamferloss = ChamferLoss()
chamferloss.cuda()

start_time = time.time()
time_p, loss_p, loss_m = [],[],[]

for epoch in range(opt.nepoch):
    sum_loss = 0
    sum_step = 0
    sum_mid_loss = 0
    for i, data in enumerate(dataloader, 0):
        points, target = data

        #print(points.size())

        points, target = Variable(points), Variable(target[:,0])
        points = points.transpose(2,1)
        points, target = points.cuda(), target.cuda()
        optimizer.zero_grad()
        foldingnet = foldingnet.train()
        recon_pc, mid_pc, _ = foldingnet(points)

        loss = chamferloss(points.transpose(2,1),recon_pc.transpose(2,1))
        loss.backward()
        optimizer.step()

        mid_loss = chamferloss(points.transpose(2,1),mid_pc.transpose(2,1))

        # store loss and step
        sum_loss += loss.item()*points.size(0)
        sum_mid_loss += mid_loss.item()*points.size(0)
        sum_step += points.size(0)

        print('[%d: %d/%d] train loss: %f middle loss: %f' %(epoch, i, num_batch, loss.item(),mid_loss.item()))

        if i % 100 == 0:
            j, data = next(enumerate(testdataloader, 0))
            points, target = data
            points, target = Variable(points), Variable(target[:,0])
            points = points.transpose(2,1)
            points, target = points.cuda(), target.cuda()
            foldingnet = foldingnet.eval()
            recon_pc, mid_pc, _ = foldingnet(points)
            loss = chamferloss(points.transpose(2,1),recon_pc.transpose(2,1))

            mid_loss = chamferloss(points.transpose(2,1),mid_pc.transpose(2,1))

            # prepare show result
            points_show = points.cpu().detach().numpy()
            #points_show = points_show[0]
            re_show = recon_pc.cpu().detach().numpy()
            #re_show = re_show[0]

            fig_ori = plt.figure()
            a1 = fig_ori.add_subplot(111,projection='3d')
            a1.scatter(points_show[0,0,:],points_show[0,1,:],points_show[0,2,:])
            plt.savefig('points_show.png')

            fig_re = plt.figure()
            a2 = fig_re.add_subplot(111,projection='3d')
            a2.scatter(re_show[0,0,:],re_show[0,1,:],re_show[0,2,:])
            plt.savefig('re_show.png')

            # plot results
            time_p.append(time.time()-start_time)
            loss_p.append(sum_loss/sum_step)
            loss_m.append(sum_mid_loss/sum_step)
            vis.line(X=np.array(time_p),
                     Y=np.array(loss_p),
                     win=line,
                     opts=dict(legend=["Loss"]))

            print('[%d: %d/%d] %s test loss: %f middle test loss: %f' %(epoch, i, num_batch, blue('test'), loss.item(), mid_loss.item()))
            sum_step = 0
            sum_loss = 0
            sum_mid_loss = 0

    torch.save(foldingnet.state_dict(), '%s/foldingnet_model_%d.pth' % (opt.outf, epoch))


text:
import os
import random as rnd
import random

from PIL import Image, ImageFilter
import cv2
from .utils import load_existed_folder
from skimage.util import random_noise
import numpy as np

from trdg import (
    computer_text_generator,
    background_generator,
    distorsion_generator,
)

try:
    from trdg import handwritten_text_generator
except ImportError as e:
    print("Missing modules for handwritten text generation.")

class FakeTextDataGenerator(object):
    @classmethod
    def generate_from_tuple(cls, t):
        """
            Same as generate, but takes all parameters as one tuple
        """

        cls.generate(*t)

    @classmethod
    def generate(
        cls,
        index,
        text,
        font,
        out_dir,
        size,
        extension,
        skewing_angle,
        random_skew,
        blur,
        random_blur,
        background_type,
        distorsion_type,
        distorsion_orientation,
        is_handwritten,
        name_format,
        width,
        alignment,
        text_color,
        orientation,
        space_width,
        margins,
        fit,
    ):
        image = None

        margin_top, margin_left, margin_bottom, margin_right = margins

        margin_top = rnd.randint(0, 7)
        margin_bottom = rnd.randint(0, 7-margin_top)
        horizontal_margin = margin_left + margin_right

        if rnd.randint(0, 10) == 0:
            margin_top = 0

        if rnd.randint(0, 10) == 0:
            margin_bottom = 0

        vertical_margin = margin_top + margin_bottom
        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            if orientation == 1:
                raise ValueError("Vertical handwritten text is unavailable")
            image = handwritten_text_generator.generate(text, text_color)
        else:
            image = computer_text_generator.generate(
                text, font, text_color, size, orientation, space_width, fit
            )

        # random_angle = rnd.randint(0 - skewing_angle, skewing_angle)

        # rotated_img = image.rotate(
        #     skewing_angle if not random_skew else random_angle, expand=1
        # )

        rotated_img = image

        #############################
        # Apply distorsion to image #
        #############################
        if distorsion_type == 0:
            distorted_img = rotated_img  # Mind = blown
        elif distorsion_type == 1:
            distorted_img = distorsion_generator.sin(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2),
            )
        elif distorsion_type == 2:
            distorted_img = distorsion_generator.cos(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2),
            )
        else:
            distorted_img = distorsion_generator.random(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2),
            )

        ##################################
        # Resize image to desired format #
        ##################################

        # Horizontal text
        if orientation == 0:
            new_width = int(
                distorted_img.size[0]
                * (float(size - vertical_margin) / float(distorted_img.size[1]))
            )
            resized_img = distorted_img.resize(
                (new_width, size - vertical_margin), Image.ANTIALIAS
            )
            background_width = width if width > 0 else new_width + horizontal_margin
            background_height = size
        # Vertical text
        elif orientation == 1:
            new_height = int(
                float(distorted_img.size[1])
                * (float(size - horizontal_margin) / float(distorted_img.size[0]))
            )
            resized_img = distorted_img.resize(
                (size - horizontal_margin, new_height), Image.ANTIALIAS
            )
            background_width = size
            background_height = new_height + vertical_margin
        else:
            raise ValueError("Invalid orientation")

        #############################
        # Generate background image #
        #############################
        if background_type == 0:
            background = background_generator.gaussian_noise(
                background_height, background_width
            )
        elif background_type == 1:
            background = background_generator.plain_white(
                background_height, background_width
            )
        elif background_type == 2:
            background = background_generator.quasicrystal(
                background_height, background_width
            )
        else:
            background = background_generator.picture(
                background_height, background_width
            )

        #############################
        # Place text with alignment #
        #############################

        new_text_width, _ = resized_img.size

        if alignment == 0 or width == -1:
            background.paste(resized_img, (margin_left, margin_top), resized_img)
        elif alignment == 1:
            background.paste(
                resized_img,
                (int(background_width / 2 - new_text_width / 2), margin_top),
                resized_img,
            )
        else:
            background.paste(
                resized_img,
                (background_width - new_text_width - margin_right, margin_top),
                resized_img,
            )
        ##################################
        # Add Noise                      #
        ##################################
        """
         - 'gaussian'  Gaussian-distributed additive noise.
        - 'localvar'  Gaussian-distributed additive noise, with specified
                      local variance at each point of `image`.
        - 'poisson'   Poisson-distributed noise generated from the data.
        - 'salt'      Replaces random pixels with 1.
        - 'pepper'    Replaces random pixels with 0 (for unsigned images) or
                      -1 (for signed images).
        - 's&p'       Replaces random pixels with either 1 or `low_val`, where
                      `low_val` is 0 for unsigned images or -1 for signed
                      images.
        - 'speckle'   Multiplicative noise using out = image + n*image, where
                      n is uniform noise with specified mean & variance.
        """

        # im_arr = np.asarray(background)
        # NOISES = ['gaussian', 'localvar', 'poisson', 'salt', 'pepper', 's&p', 'speckle']

        # if random.choice([1]) == 1:
        #     final_image = random_noise(im_arr, mode=random.choice(NOISES[:4]))
        #     final_image = (255 * final_image).astype(np.uint8)
        #     final_image = Image.fromarray(final_image)
        #     if random.choice([0, 1]) == 1:
        #         final_image = final_image.filter(
        #             ImageFilter.GaussianBlur(
        #                 radius=(blur if not random_blur else random.randint(2, 3))
        #             )
        #         )
        # elif random.choice([0, 1, 1, 1]) == 0:
        #     final_image = background
        #     ##################################
        #     # Apply gaussian blur            #
        #     ##################################
        #     final_image = final_image.filter(
        #         ImageFilter.GaussianBlur(
        #             radius=(blur if not random_blur else random.randint(0, blur))
        #         )
        #     )
        # else:
        #     final_image = background

        final_image = background
        ##################################
        # Apply gaussian blur #
        ##################################

        final_image = background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else rnd.randint(0, blur))
            )
        )

        #####################################
        # Generate name for resulting image #
        #####################################
        if name_format == 0:
            image_name = "{}_{}.{}".format(text, str(index), extension)
        elif name_format == 1:
            image_name = "{}_{}.{}".format(str(index), text, extension)
        elif name_format == 2:
            image_name = "{}/{}.{}".format(str(index//1000), str(index), extension)
        else:
            print("{} is not a valid name format. Using default.".format(name_format))
            image_name = "{}_{}.{}".format(text, str(index), extension)
        # Save the image
        if out_dir is not None:
            image_path = os.path.join(out_dir, image_name)
            image_dir_path = os.path.dirname(image_path)
            os.makedirs(image_dir_path, exist_ok=True)
            final_image.convert("RGB").save(image_path)

            # cv2.imwrite(os.path.join(image_dir_path, image_name), final_image)
        else:
            return final_image.convert("RGB")


text:
from __future__ import print_function, division
import numpy
import sys
import time
import networkx
import igraph

def generate_subsets_reference(data_mask, live_pointsp, graph, _):
	# generate data subsets which share points.
	firstmember = numpy.where(data_mask)[0][0]
	if len(live_pointsp[:,firstmember]) == len(numpy.unique(live_pointsp[:,data_mask].flatten())):
		# trivial case: all live points are the same across data sets
		yield data_mask, live_pointsp[:,firstmember]
		return

	to_handle = data_mask.copy()
	while to_handle.any():
		firstmember = numpy.where(to_handle)[0][0]
		to_handle[firstmember] = False
		members = [firstmember]
		# get live points of this member
		member_live_pointsp = live_pointsp[:,firstmember].tolist()
		# look through to_handle for entries and check if they have the points
		i = 0
		while True:
			if i >= len(member_live_pointsp) or not to_handle.any():
				break
			p = member_live_pointsp[i]
			sharing = (live_pointsp[:,to_handle] == p).any(axis=0)
			#assert len(sharing) == to_handle.sum()
			newmembers = numpy.where(to_handle)[0][sharing]
			#assert numpy.all(newmembers == numpy.arange(len(to_handle))[to_handle][sharing])

			#print 'new members:', newmembers
			members += newmembers.tolist()
			for newp in numpy.unique(live_pointsp[:,newmembers]):
				if newp not in member_live_pointsp:
					member_live_pointsp.append(newp)
			to_handle[newmembers] = False
			i = i + 1

		# now we have our members and live points
		member_data_mask = numpy.zeros(len(data_mask), dtype=bool)
		member_data_mask[members] = True
		#print 'returning:', member_data_mask, member_live_pointsp
		yield member_data_mask, member_live_pointsp

def generate_subsets_graph_simple(data_mask, live_pointsp, graph, _):
	# generate data subsets which share points.
	firstmember = numpy.where(data_mask)[0][0]
	# then identify disjoint subgraphs
	for subgraph in networkx.connected_component_subgraphs(graph, copy=False):
		member_data_mask = numpy.zeros(len(data_mask), dtype=bool)
		member_live_pointsp = []
		for nodetype, i in subgraph.nodes():
			if nodetype == 0:
				member_data_mask[i] = True
			else:
				member_live_pointsp.append(i)
		yield member_data_mask, member_live_pointsp

def generate_subsets_graph(data_mask, live_pointsp, graph, _):
	# generate data subsets which share points.
	firstmember = numpy.where(data_mask)[0][0]
	allp = numpy.unique(live_pointsp[:,data_mask].flatten())
	if len(live_pointsp[:,firstmember]) == len(allp):
		# trivial case: all live points are the same across data sets
		yield data_mask, live_pointsp[:,firstmember]
		return

	subgraphs = list(networkx.connected_component_subgraphs(graph, copy=False))
	if len(subgraphs) == 1:
		yield data_mask, allp
		return

	# then identify disjoint subgraphs
	for subgraph in subgraphs:
		print('networkx subgraph:', subgraph.nodes())
		member_data_mask = numpy.zeros(len(data_mask), dtype=bool)
		member_live_pointsp = []
		for nodetype, i in subgraph.nodes():
			if nodetype == 0:
				member_data_mask[i] = True
			else:
				member_live_pointsp.append(i)
		yield member_data_mask, member_live_pointsp

def generate_subsets_igraph(data_mask, live_pointsp, _graph, graph):
	# generate data subsets which share points.
	firstmember = numpy.where(data_mask)[0][0]
	allp = numpy.unique(live_pointsp[:,data_mask].flatten())
	if len(live_pointsp[:,firstmember]) == len(allp):
		# trivial case: all live points are the same across data sets
		yield data_mask, live_pointsp[:,firstmember]
		return

	subgraphs = graph.clusters()
	if len(subgraphs) == 1:
		yield data_mask, allp
		return
	# then identify disjoint subgraphs
	for subgraph in subgraphs:
		#print 'igraph subgraph:', subgraph
		member_data_mask = numpy.zeros(len(data_mask), dtype=bool)
		member_live_pointsp = []
		for vi in subgraph:
			att = graph.vs[vi].attributes()
			#print '    ', att
			if att['vtype'] == 0:
				i = att['nodeid']
				member_data_mask[i] = True
			else:
				p = att['pointid']
				member_live_pointsp.append(p)
		if member_data_mask.any():
			yield member_data_mask, member_live_pointsp

data_sets = []

t0 = time.time()
for filename in sys.argv[1:]:
	data = numpy.load(filename)
	data_mask, live_pointsp = data['data_mask'], data['live_pointsp']
	# create graph
	graph = networkx.Graph()
	graph2 = igraph.Graph()
	# pointing from live_point to member

	for p in numpy.unique(live_pointsp):
		graph2.add_vertex("p%d" % p, pointid=p, vtype=1)
	for i in numpy.where(data_mask)[0]:
		graph.add_edges_from((((0, i), (1, p)) for p in live_pointsp[:,i]))
		graph2.add_vertex("n%d" % i, nodeid=i, vtype=0)
		graph2.add_edges([("n%d" % i, "p%d" % p) for p in live_pointsp[:,i]])

	data_sets.append((data_mask, live_pointsp, graph, graph2))
t1 = time.time()
print('loading took %fs' % (t1 - t0))

prev_output = []
for implementation in [generate_subsets_reference, generate_subsets_graph_simple, generate_subsets_graph, generate_subsets_igraph]:
	print('running', implementation)
	output = []
	t0 = time.time()
	for a, b, graph, graph2 in data_sets:
		out = list(implementation(a, b, graph, graph2))
		output.append(out)
	t1 = time.time()
	print('   took %fs' % (t1 - t0))
	#for a, b in  zip(output,
	if prev_output != []:
		print('checking for correctness...')
		for memberlist1, memberlist2 in zip(output, prev_output):
			memberlist1 = sorted(memberlist1, key=lambda ml: (len(ml), ml[0][0]))
			memberlist2 = sorted(memberlist2, key=lambda ml: (len(ml), ml[0][0]))
			for (md, ml), (md2, ml2) in zip(memberlist1, memberlist2):
				#print len(md), md.sum(), len(md2), md2.sum(), len(ml), len(ml2)
				assert numpy.all(md == md2), (numpy.where(md), numpy.where(md2))
				assert sorted(ml) == sorted(ml2)
			assert len(memberlist1) == len(memberlist2), (len(memberlist1), len(memberlist2))
	prev_output = output

max_stars_repo_name: JohannesBuchner/massivedatans

max_issues_repo_name: JohannesBuchner/massivedatans

max_forks_repo_name: JohannesBuchner/massivedatans

reason: import numpy,import networkx


text:
import numpy as np

from numba import types
from numba import unittest_support as unittest

class TestTypeNames(unittest.TestCase):
    def test_numpy_integers(self):
        expect = getattr(types, "int%d" % (np.dtype("int").itemsize * 8))
        self.assertEqual(types.int_, expect)

        expect = getattr(types, "uint%d" % (np.dtype("uint").itemsize * 8))
        self.assertEqual(types.uint, expect)

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

max_stars_repo_name: tolysz/numba

max_issues_repo_name: tolysz/numba

max_forks_repo_name: asodeur/numba

reason: import numpy,from numba


text:
import numpy as np
from keras import layers
from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.models import Model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
import pydot
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
from kt_utils import *

import keras.backend as K
K.set_image_data_format('channels_last')
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow

#%matplotlib inline

X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

# Normalize image vectors
X_train = X_train_orig/255.
X_test = X_test_orig/255.

# Reshape
Y_train = Y_train_orig.T
Y_test = Y_test_orig.T

print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))

#  HappyModel

def HappyModel(input_shape):

    X_input = Input (input_shape)

    X = ZeroPadding2D((3, 3))(X_input)

    X = Conv2D(32 , (7,7) , strides = (1,1) , name = "conv0")(X)

    X = BatchNormalization(axis=3 , name = "bn0")(X)

    X = Activation ('relu')(X)

    X = MaxPooling2D((2,2) ,name = "max_pool") (X)

    X =Flatten()(X)

    X = Dense( 1 , activation= "sigmoid" , name ="fc" )(X)

    model = Model(inputs= X_input , outputs= X, name= "HappyHome")

    return model

# create model
happyModel = HappyModel(input_shape = (64 , 64 , 3))

#compile model
happyModel.compile(optimizer= "Adam" ,metrics= ["accuracy"] ,loss= "mean_squared_error")

# train (fit)
happyModel.fit(x = X_train , y= Y_train , epochs = 5 , batch_size= 64)

#test

preds = happyModel.evaluate(x= X_test , y = Y_test)
print()
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))

#test for your image

img_path = 'images/my_image.jpg'
img = image.load_img(img_path, target_size=(64, 64))
imshow(img)

x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

print(happyModel.predict(x))

max_stars_repo_name: NikhilMunna/Deep-Learning-Projects

max_issues_repo_name: NikhilMunna/Deep-Learning-Projects

max_forks_repo_name: NikhilMunna/Deep-Learning-Projects


text:
import os
import numpy as np
import csv
import pickle

def get_adjacency_matrix(distance_df_filename, num_of_vertices, id_filename=None):
    '''
    Parameters
    ----------
    distance_df_filename: str, path of the csv file contains edges information

    num_of_vertices: int, the number of vertices

    Returns
    ----------
    A: np.ndarray, adjacency matrix

    '''
    if 'npy' in distance_df_filename:
        adj_mx = np.load(distance_df_filename)
        return adj_mx, None
    else:
        A = np.zeros((int(num_of_vertices), int(num_of_vertices)),
                     dtype=np.float32)
        distaneA = np.zeros((int(num_of_vertices), int(num_of_vertices)),
                            dtype=np.float32)
        # distance file中的id并不是从0开始的 所以要进行重新的映射；id_filename是节点的顺序
        if id_filename:
            with open(id_filename, 'r') as f:
                id_dict = {int(i): idx for idx, i in enumerate(f.read().strip().split('\n'))}  # 把节点id（idx）映射成从0开始的索引
            with open(distance_df_filename, 'r') as f:
                f.readline()  # 略过表头那一行
                reader = csv.reader(f)
                for row in reader:
                    if len(row) != 3:
                        continue
                    i, j, distance = int(row[0]), int(row[1]), float(row[2])
                    A[id_dict[i], id_dict[j]] = 1
                    distaneA[id_dict[i], id_dict[j]] = distance
            return A, distaneA
        else:  # distance file中的id直接从0开始
            with open(distance_df_filename, 'r') as f:
                f.readline()
                reader = csv.reader(f)
                for row in reader:
                    if len(row) != 3:
                        continue
                    i, j, distance = int(row[0]), int(row[1]), float(row[2])
                    A[i, j] = 1
                    distaneA[i, j] = distance
            return A, distaneA

def get_adjacency_matrix_2direction(distance_df_filename, num_of_vertices, id_filename=None):
    '''
    Parameters
    ----------
    distance_df_filename: str, path of the csv file contains edges information
    num_of_vertices: int, the number of vertices

    Returns
    ----------
    A: np.ndarray, adjacency matrix
    '''
    if 'npy' in distance_df_filename:
        adj_mx = np.load(distance_df_filename)
        return adj_mx, None
    else:
        A = np.zeros((int(num_of_vertices), int(num_of_vertices)), dtype=np.float32)
        distaneA = np.zeros((int(num_of_vertices), int(num_of_vertices)),
                            dtype=np.float32)
        # distance file中的id并不是从0开始的 所以要进行重新的映射；id_filename是节点的顺序
        if id_filename:
            with open(id_filename, 'r') as f:
                id_dict = {int(i): idx for idx, i in enumerate(f.read().strip().split('\n'))}  # 把节点id（idx）映射成从0开始的索引
            with open(distance_df_filename, 'r') as f:
                f.readline()  # 略过表头那一行
                reader = csv.reader(f)
                for row in reader:
                    if len(row) != 3:
                        continue
                    i, j, distance = int(row[0]), int(row[1]), float(row[2])
                    A[id_dict[i], id_dict[j]] = 1
                    A[id_dict[j], id_dict[i]] = 1
                    distaneA[id_dict[i], id_dict[j]] = distance
                    distaneA[id_dict[j], id_dict[i]] = distance
            return A, distaneA
        else:  # distance file中的id直接从0开始
            with open(distance_df_filename, 'r') as f:
                f.readline()
                reader = csv.reader(f)
                for row in reader:
                    if len(row) != 3:
                        continue
                    i, j, distance = int(row[0]), int(row[1]), float(row[2])
                    A[i, j] = 1
                    A[j, i] = 1
                    distaneA[i, j] = distance
                    distaneA[j, i] = distance
            return A, distaneA

def get_adjacency_matrix_2direction(distance_df_filename, num_of_vertices, id_filename=None):
    '''
    Parameters
    ----------
    distance_df_filename: str, path of the csv file contains edges information

    num_of_vertices: int, the number of vertices

    Returns
    ----------
    A: np.ndarray, adjacency matrix

    '''
    if 'npy' in distance_df_filename:
        adj_mx = np.load(distance_df_filename)
        return adj_mx, None
    else:
        A = np.zeros((int(num_of_vertices), int(num_of_vertices)),
                     dtype=np.float32)
        distaneA = np.zeros((int(num_of_vertices), int(num_of_vertices)),
                            dtype=np.float32)
        # distance file中的id并不是从0开始的 所以要进行重新的映射；id_filename是节点的顺序
        if id_filename:
            with open(id_filename, 'r') as f:
                id_dict = {int(i): idx for idx, i in enumerate(f.read().strip().split('\n'))}  # 把节点id（idx）映射成从0开始的索引
            with open(distance_df_filename, 'r') as f:
                f.readline()  # 略过表头那一行
                reader = csv.reader(f)
                for row in reader:
                    if len(row) != 3:
                        continue
                    i, j, distance = int(row[0]), int(row[1]), float(row[2])
                    A[id_dict[i], id_dict[j]] = 1
                    A[id_dict[j], id_dict[i]] = 1
                    distaneA[id_dict[i], id_dict[j]] = distance
                    distaneA[id_dict[j], id_dict[i]] = distance
            return A, distaneA
        else:  # distance file中的id直接从0开始
            with open(distance_df_filename, 'r') as f:
                f.readline()
                reader = csv.reader(f)
                for row in reader:
                    if len(row) != 3:
                        continue
                    i, j, distance = int(row[0]), int(row[1]), float(row[2])
                    A[i, j] = 1
                    A[j, i] = 1
                    distaneA[i, j] = distance
                    distaneA[j, i] = distance
            return A, distaneA

def generate_adj_PEMS03():
    direction = True
    distance_df_filename, num_of_vertices = "datasets/raw_data/PEMS03/PEMS03.csv", 358
    if os.path.exists(distance_df_filename.split(".")[0] + ".txt"):
        id_filename = distance_df_filename.split(".")[0] + ".txt"
    else:
        id_filename = None
    if direction:
        adj_mx, distance_mx = get_adjacency_matrix_2direction(distance_df_filename, num_of_vertices, id_filename=id_filename)
    else:
        adj_mx, distance_mx = get_adjacency_matrix(distance_df_filename, num_of_vertices, id_filename=id_filename)
    # TODO: the self loop is missing
    add_self_loop = False
    if add_self_loop:
        adj_mx = adj_mx + np.identity(adj_mx.shape[0])
        distance_mx = distance_mx + np.identity(distance_mx.shape[0])
    pickle.dump(adj_mx, open("datasets/raw_data/PEMS03/adj_PEMS03.pkl", 'wb'))
    pickle.dump(distance_mx, open("datasets/raw_data/PEMS03/adj_PEMS03_distance.pkl", 'wb'))


text:
# Uses tree error defined in CALDER paper. This assumes a single solution tree
# and a single true tree. It just computes the fraction of all mutation pairs
# in the candidate tree that have the same relation as in the true tree.

import numpy as np
import argparse

import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'lib'))
import mutrel
from common import Models

MISSING = -1

def load_mutrels(mutrel_args):
  mutrels = {}
  for mutrel_arg in mutrel_args:
    mutrel_name, mutrel_path = mutrel_arg.split('=', 1)
    assert mutrel_name not in mutrels, '%s is duplicate' % mutrel_name
    if os.path.exists(mutrel_path):
      mrel = np.load(mutrel_path)
      mutrels[mutrel_name] = mutrel.Mutrel(vids=mrel['vids'], rels=mrel['rels'])
    else:
      mutrels[mutrel_name] = None
  return mutrels

def _ensure_hard_relations(mrel):
  assert np.logical_or(mrel.rels == 0., mrel.rels == 1.)

def _score_treeror(candidate, truth):
  M, _, num_models = truth.rels.shape
  assert M >= 2
  for R in (candidate, truth):
    _ensure_hard_relations(R)
    assert R.rels.shape == (M,M,num_models)

  _extract_rel = lambda R: np.tril(np.argmax(R.rels, axis=2), -1)
  correct = _extract_rel(candidate)
  soln = _extract_rel(truth)
  num_pairs = 0.5*M*(M-1)
  prop_correct = sum(correct == soln) / num_pairs
  assert 0 <= prop_correct <= 1
  trerror = 1 - prop_correct
  return trerror

def compare(mutrels):
  assert 'truth' in mutrels
  M, _, num_models = mutrels['truth'].rels.shape
  assert mutrels['truth'].rels.shape == (M, M, num_models)

  names = sorted(mutrels.keys())
  scores = {}

  for name in names:
    mrel = mutrels[name]
    if mrel is None:
      scores[name] = MISSING
      continue
    assert mrel.rels.shape == (M, M, num_models)
    assert np.array_equal(mrel.vids, mutrels['truth'].vids)
    mutrel.check_posterior_sanity(mrel.rels)

    scores[name] = _score_trerror(mrel.rels, mutrels['truth'].rels)
    if name == 'truth':
      assert np.isclose(0, scores[name])

  print(*names, sep=',')
  print(*[scores[name] for name in names], sep=',')

def main():
  parser = argparse.ArgumentParser(
    description='LOL HI THERE',
    formatter_class=argparse.ArgumentDefaultsHelpFormatter
  )
  parser.add_argument('mutrels', nargs='+')

  mutrels = load_mutrels(args.mutrels)
  compare(mutrels)

if __name__ == '__main__':
  main()

max_stars_repo_name: ethanumn/pairtree

max_issues_repo_name: ethanumn/pairtree

max_forks_repo_name: ethanumn/pairtree