text:
import collections
import contextlib
import copy
import warnings

import numpy
import six

from chainer.backends import cuda
from chainer.backends import intel64
from chainer import initializers
from chainer import variable

def _is_shape(value):
    if value is None:
        return True
    elif isinstance(value, collections.Sequence):
        try:
            return all(int(x) for x in value)
        except TypeError:
            return False
    try:
        return int(value)
    except TypeError:
        return False

def _ensure_shape_dtype(value):
    # Return value paired with dtype FP32 if it is a shape.
    if _is_shape(value):
        return value, 'f'
    # Otherwise, returns it with assuming a shape-dtype pair.
    else:
        return value

class Link(object):

    """Building block of model definitions.

    Link is a building block of neural network models that support various
    features like handling parameters, defining network fragments,
    serialization, etc.

    Link is the primitive structure for the model definitions. It supports
    management of parameter variables and *persistent values* that should be
    incorporated to serialization.

    Parameter is an instance of :class:`~chainer.Parameter` registered to a
    link. A :class:`~chainer.Parameter` object can be registered as a
    parameter of the link by assigning it to an attribute within *an
    initialization scope*, which is a code surrounded by a
    :meth:`init_scope` context manager using the ``with`` statement.

    Persistent values are arrays, scalars, or any other serializable values
    registered via :meth:`register_persistent` or :meth:`add_persistent`.

    .. note::
       Whereas arbitrary serializable objects can be registered as persistent
       values, it is strongly recommended to just register values that should
       be treated as results of learning. A typical example of persistent
       values is ones computed during training and required for testing, e.g.
       running statistics for batch normalization.

    Parameters and persistent values are referred by their names. They can be
    accessed as attributes of the links. Link class itself manages the lists
    of names of parameters and persistent values to distinguish parameters and
    persistent values from other attributes.

    Link can be composed into more complex models. This composition feature is
    supported by child classes like :class:`Chain` and :class:`ChainList`. One
    can create a chain by combining one or more links. See the documents for
    these classes for details.

    As noted above, Link supports the serialization protocol of the
    :class:`~chainer.Serializer` class. **Note that only parameters and
    persistent values are saved and loaded.** Other attributes are considered
    as a part of user program (i.e. a part of network definition). In order to
    construct a link from saved file, other attributes must be identically
    reconstructed by user codes.

    .. admonition:: Example

       This is a simple example of custom link definition. Chainer itself also
       provides many links defined under the :mod:`~chainer.links` module. They
       might serve as examples, too.

       Consider we want to define a simple primitive link that implements a
       fully-connected layer based on the :func:`~functions.linear` function.
       Note that this function takes input units, a weight variable, and a bias
       variable as arguments. Then, the fully-connected layer can be defined as
       follows::

          import chainer
          import chainer.functions as F
          from chainer import initializers
          import numpy as np

          class LinearLayer(chainer.Link):

              def __init__(self, n_in, n_out):
                  super(LinearLayer, self).__init__()
                  with self.init_scope():
                      self.W = chainer.Parameter(
                          initializers.Normal(), (n_out, n_in))
                      self.b = chainer.Parameter(
                          initializers.Zero(), (n_out,))

              def __call__(self, x):
                  return F.linear(x, self.W, self.b)

       This example shows that a user can define arbitrary parameters and use
       them in any methods. Links typically implement the ``__call__``
       operator, although they can also provide other methods to implement the
       forward propagation.

    Args:
        params: *(deprecated since v2.0.0)* Names, shapes, and optional dtypes
            of initial parameters. The keywords are used as the parameter
            names and the corresponding values consist either of the shape or
            a tuple of shape and a dtype ``(shape, dtype)``. If only the shape
            is supplied, the default dtype will be used.

    Attributes:
        ~Link.name (str): Name of this link, given by the parent chain (if
            exists).

    """

    def __init__(self, **params):
        self._params = set()
        self._persistent = set()
        self._cpu = True
        self._device_id = None
        self._within_init_scope = False
        self.name = None

        for name, value in six.iteritems(params):
            # Note: deprecation warning will be raised in add_param
            shape, dtype = _ensure_shape_dtype(value)
            self.add_param(name, shape, dtype=dtype)

    @property
    def xp(self):
        """Array module for this link.

        Depending on which of CPU/GPU this link is on, this property returns
        :mod:`numpy` or :mod:`cupy`.

        """
        return numpy if self._cpu else cuda.cupy

    @property
    def within_init_scope(self):
        """True if the current code is inside of an initialization scope.

        See :meth:`init_scope` for the details of the initialization scope.

        """
        return getattr(self, '_within_init_scope', False)

    @contextlib.contextmanager
    def init_scope(self):
        """Creates an initialization scope.

        This method returns a context manager object that enables registration
        of parameters (and links for :class:`~chainer.Chain`) by an assignment.
        A :class:`~chainer.Parameter` object can be automatically registered
        by assigning it to an attribute under this context manager.

        .. admonition:: Example

           In most cases, the parameter registration is done in the
           initializer method. Using the ``init_scope`` method, we can
           simply assign a :class:`~chainer.Parameter` object to register
           it to the link.

           .. code-block:: python

              class MyLink(chainer.Link):
                  def __init__(self):
                      super().__init__()
                      with self.init_scope():
                          self.W = chainer.Parameter(0, (10, 5))
                          self.b = chainer.Parameter(0, (5,))

        """
        old_flag = self.within_init_scope
        self._within_init_scope = True
        try:
            yield
        finally:
            self._within_init_scope = old_flag

    def __setattr__(self, name, value):
        if self.within_init_scope and isinstance(value, variable.Parameter):
            value.name = name
            if not self._cpu:
                value.to_gpu(self._device_id)
            self._params.add(name)
            self._persistent.discard(name)
        super(Link, self).__setattr__(name, value)

    def __delattr__(self, name):
        self._params.discard(name)
        self._persistent.discard(name)
        super(Link, self).__delattr__(name)

    def add_param(self, name, shape=None, dtype=numpy.float32,
                  initializer=None):
        """Registers a parameter to the link.

        .. deprecated:: v2.0.0

           Assign a :class:`~chainer.Parameter` object directly to an
           attribute within :meth:`~chainer.Link.init_scope` instead.
           For example, the following code

           .. code-block:: python

               link.add_param('W', shape=(5, 3))

           can be replaced by the following assignment.

           .. code-block:: python

               with link.init_scope():
                   link.W = chainer.Parameter(None, (5, 3))

           The latter is easier for IDEs to keep track of the attribute's
           type.

        Args:
            name (str): Name of the parameter. This name is also used as the
                attribute name.
            shape (int or tuple of ints): Shape of the parameter array. If it
                is omitted, the parameter variable is left uninitialized.
            dtype: Data type of the parameter array.
            initializer: If it is not ``None``, the data is initialized with
                the given initializer. If it is an array, the data is directly
                initialized by it. If it is callable, it is used as a weight
                initializer. Note that in these cases, ``dtype`` argument is
                ignored.

        """
        warnings.warn('''\
Parameter registeration via Link.__init__ and Link.add_param are deprecated.
Assign a Parameter object directly to an attribute within a \
"with Link.init_scope():" block instead.
''', DeprecationWarning)
        if name in self.__dict__:
            raise AttributeError(
                'cannot register a new parameter %s: attribute exists'
                % name)
        if initializer is None:
            initializer = initializers.NaN(dtype)
        param = variable.Parameter(initializer, shape)
        with self.init_scope():
            setattr(self, name, param)

    def add_persistent(self, name, value):
        """Registers a persistent value to the link.

        The registered value is saved and loaded on serialization and
        deserialization. The value is set to an attribute of the link.

        Args:
            name (str): Name of the persistent value. This name is also used
                for the attribute name.
            value: Value to be registered.

        """
        d = self.__dict__
        if name in d:
            raise AttributeError(
                'cannot register a new persistent value %s: attribute exists'
                % name)
        self._persistent.add(name)
        self._params.discard(name)
        d[name] = value

    def register_persistent(self, name):
        """Registers an attribute of a given name as a persistent value.

        This is a convenient method to register an existing attribute as a
        persistent value. If ``name`` has been already registered as a
        parameter, this method removes it from the list of parameter names
        and re-registers it as a persistent value.

        Args:
            name (str): Name of the attribute to be registered.

        """
        if not hasattr(self, name):
            raise AttributeError(
                'cannot register non-existent attribute %s as a persistent '
                'value' % name)
        self._persistent.add(name)
        self._params.discard(name)

    def copy(self):
        """Copies the link hierarchy to new one.

        The whole hierarchy rooted by this link is copied. The copy is
        basically shallow, except that the parameter variables are also
        shallowly copied. It means that the parameter variables of copied one
        are different from ones of original link, while they share the data and
        gradient arrays.

        The name of the link is reset on the copy, since the copied instance
        does not belong to the original parent chain (even if exists).

        Returns:
            Link: Copied link object.

        """
        ret = copy.copy(self)
        ret._params = set(self._params)
        ret._persistent = set(self._persistent)
        ret.name = None
        d = ret.__dict__
        for name in ret._params:
            d[name] = copy.copy(d[name])
            d[name].grad = None
        return ret

    def to_cpu(self):
        """Copies parameter variables and persistent values to CPU.

        This method does not handle non-registered attributes. If some of such
        attributes must be copied to CPU, the link implementation must
        override this method to do so.

        Returns: self

        """
        d = self.__dict__
        for name in self._params:
            d[name].to_cpu()
        for name in self._persistent:
            value = d[name]
            if isinstance(value, cuda.ndarray):
                d[name] = value.get()
            elif isinstance(value, intel64.mdarray):
                d[name] = numpy.ndarray(value)
        self._cpu = True
        self._device_id = None
        return self

    def to_gpu(self, device=None):
        """Copies parameter variables and persistent values to GPU.

        This method does not handle non-registered attributes. If some of such
        attributes must be copied to GPU, the link implementation must
        override this method to do so.

        Args:
            device: Target device specifier. If omitted, the current device is
                used.

        Returns: self

        """
        cuda.check_cuda_available()
        if not self._cpu:
            return self
        d = self.__dict__
        with cuda._get_device(device):
            for name in self._params:
                d[name].to_gpu()
            for name in self._persistent:
                value = d[name]
                if isinstance(value, numpy.ndarray):
                    d[name] = cuda.to_gpu(value)
            self._device_id = cuda.cupy.cuda.get_device_id()
        self._cpu = False
        return self

    def to_intel64(self):
        """Copies parameter variables and persistent values to CPU."""
        intel64.check_ideep_available()
        d = self.__dict__
        for name in self._params:
            d[name].to_intel64()
        for name in self._persistent:
            value = d[name]
            if isinstance(value, numpy.ndarray):
                d[name] = intel64.ideep.array(
                    value, itype=intel64.ideep4.wgt_array)
        self._cpu = True
        return self

    def params(self, include_uninit=True):
        """Returns a generator of all parameters under the link hierarchy.

        Args:
            include_uninit (bool): If ``True``, it also generates uninitialized
                parameters.

        Returns:
            A generator object that generates all parameters.

        """
        d = self.__dict__
        for name in self._params:
            if include_uninit or d[name].data is not None:
                yield d[name]

    def namedparams(self, include_uninit=True):
        """Returns a generator of all (path, param) pairs under the hierarchy.

        Args:
            include_uninit (bool): If ``True``, it also generates uninitialized
                parameters.

        Returns:
            A generator object that generates all (path, parameter) pairs. The
            paths are relative from this link.

        """
        d = self.__dict__
        for name in self._params:
            if include_uninit or d[name].data is not None:
                yield '/' + name, d[name]

    def links(self, skipself=False):
        """Returns a generator of all links under the hierarchy.

        Args:
            skipself (bool): If ``True``, then the generator skips this link
                and starts with the first child link.

        Returns:
            A generator object that generates all links.

        """
        if not skipself:
            yield self

    def namedlinks(self, skipself=False):
        """Returns a generator of all (path, link) pairs under the hierarchy.

        Args:
            skipself (bool): If ``True``, then the generator skips this link
                and starts with the first child link.

        Returns:
            A generator object that generates all (path, link) pairs.

        """
        if not skipself:
            yield '/', self

    def children(self):
        """Returns a generator of all child links.

        Returns:
            A generator object that generates all child links.

        """
        if 0:
            yield

    def copyparams(self, link):
        """Copies all parameters from given link.

        This method copies data arrays of all parameters in the hierarchy. The
        copy is even done across the host and devices. Note that this method
        does not copy the gradient arrays.

        Args:
            link (Link): Source link object.

        """
        src = link.__dict__
        dst = self.__dict__
        for name in self._params:
            dst[name].copydata(src[name])

    def cleargrads(self):
        """Clears all gradient arrays.

        This method should be called before the backward computation at every
        iteration of the optimization.

        """
        for param in self.params():
            param.cleargrad()

    def zerograds(self):
        """Initializes all gradient arrays by zero.

        This method can be used for the same purpose of cleargrads, but less
        efficient. This method is left for backward compatibility.

        .. deprecated:: v1.15
           Use :meth:`cleargrads` instead.

        """
        warnings.warn(
            'Link.zerograds is deprecated. Use Link.cleargrads instead.',
            DeprecationWarning)
        for param in self.params():
            param.zerograd()

    def addgrads(self, link):
        """Accumulates gradient values from given link.

        This method adds each gradient array of the given link to corresponding
        gradient array of this link. The accumulation is even done across
        host and different devices.

        Args:
            link (Link): Source link object.

        """
        src = link.__dict__
        dst = self.__dict__
        for name in self._params:
            dst[name].addgrad(src[name])

    def enable_update(self):
        """Enables update rules of all parameters under the link hierarchy.

        This method sets the :attr:`~chainer.UpdateRule.enabled` flag of the
        update rule of each parameter variable to ``True``.

        """
        for param in self.params():
            rule = param.update_rule
            if rule is not None:
                rule.enabled = True

    def disable_update(self):
        """Disables update rules of all parameters under the link hierarchy.

        This method sets the :attr:`~chainer.UpdateRule.enabled` flag of the
        update rule of each parameter variable to ``False``.

        """
        for param in self.params():
            rule = param.update_rule
            if rule is not None:
                rule.enabled = False

    @property
    def update_enabled(self):
        """``True`` if at least one parameter has an update rule enabled."""
        for param in self.params():
            rule = param.update_rule
            if rule is not None and rule.enabled:
                return True
        return False

    def serialize(self, serializer):
        """Serializes the link object.

        Args:
            serializer (~chainer.AbstractSerializer): Serializer object.

        """
        d = self.__dict__
        for name in self._params:
            param = d[name]
            data = serializer(name, param.data)
            if param.data is None and data is not None:
                # Initialize the parameter here
                param.initialize(data.shape)
                if isinstance(param.data, numpy.ndarray):
                    numpy.copyto(param.data, data)
                else:
                    param.data.set(numpy.asarray(data))
        for name in self._persistent:
            d[name] = serializer(name, d[name])

class Chain(Link):

    """Composable link with object-like interface.

    Composability is one of the most important features of neural nets. Neural
    net models consist of many reusable fragments, and each model itself might
    be embedded into a larger learnable system. Chain enables us to write a
    neural net based on composition, without bothering about routine works like
    collecting parameters, serialization, copying the structure with parameters
    shared, etc.

    This class actually provides a way to compose one or more links into one
    structure. A chain can contain one or more *child links*. Child link is a
    link registered to the chain with its own name. The child link is stored to
    an attribute of the chain with the name. User can write a whole model or a
    fragment of neural nets as a child class of Chain.

    Each chain itself is also a link. Therefore, one can combine chains into
    higher-level chains. In this way, links and chains construct a *link
    hierarchy*. Link hierarchy forms a tree structure, where each node is
    identified by the path from the root. The path is represented by a string
    like a file path in UNIX, consisting of names of nodes on the path, joined
    by slashes ``/``.

    A child link can be added just by assigning it to an attribute of the
    chain within :meth:`~chainer.Chain.init_scope`.

    The registered child link is saved and loaded on serialization and
    deserialization, and involved in the optimization. The registered link
    is called a child. The child link is accessible via :meth:`children`
    generator, which returns a generator running through the children in
    registered order.

    On registration of a child link, its :attr:`~Link.name` attribute is also
    set (or overwritten if the link has already been registered to another
    chain).

    .. admonition:: Example

       This is a simple example of custom chain definition. Chainer itself also
       provides some chains defined under the :mod:`~chainer.links` module.
       They might serve as examples, too.

       Consider we want to define a multi-layer perceptron consisting of two
       hidden layers with rectifiers as activation functions. We can use the
       :class:`~chainer.links.Linear` link as a building block::

          import chainer
          import chainer.functions as F
          import chainer.links as L

          class MultiLayerPerceptron(chainer.Chain):

              def __init__(self, n_in, n_hidden, n_out):
                  super(MultilayerPerceptron, self).__init__()
                  with self.init_scope():
                      self.layer1 = L.Linear(n_in, n_hidden)
                      self.layer2 = L.Linear(n_hidden, n_hidden)
                      self.layer3 = L.Linear(n_hidden, n_out)

              def __call__(self, x):
                  # Forward propagation
                  h1 = F.relu(self.layer1(x))
                  h2 = F.relu(self.layer2(h1))
                  return self.layer3(h2)

       Child links are registered via the assignment within a
       ``with self.init_scope():`` block. The forward propagation is often
       implemented as the ``__call__`` operator as the above example, though
       it is not mandatory.

    Args:
        links: Child links. The keywords are used as their names. The names are
            also set to the links.

            .. deprecated:: v2.0.0

               Assign child links directly to attributes instead.

    """

    def __init__(self, **links):
        super(Chain, self).__init__()
        self._children = set()

        for name, link in six.iteritems(links):
            self.add_link(name, link)

    def __getitem__(self, name):
        """Equivalent to getattr."""
        return getattr(self, name)

    def __setattr__(self, name, value):
        if self.within_init_scope and isinstance(value, Link):
            if hasattr(self, name):
                raise AttributeError(
                    'cannot register a new link %s: attribute exists' % name)
            value.name = name
            self._children.add(name)
        super(Chain, self).__setattr__(name, value)

    def __delattr__(self, name):
        self._children.discard(name)
        super(Chain, self).__delattr__(name)

    def add_link(self, name, link):
        """Registers a child link to this chain.

        .. deprecated:: v2.0.0

           Assign the child link directly to an attribute within
           :meth:`~chainer.Chain.init_scope` instead.
           For example, the following code

           .. code-block:: python

              chain.add_link('l1', L.Linear(3, 5))

           can be replaced by the following line.

           .. code-block:: python

              with chain.init_scope():
                  chain.l1 = L.Linear(3, 5)

           The latter is easier for IDEs to keep track of the attribute's
           type.

        Args:
            name (str): Name of the child link. This name is also used as the
                attribute name.
            link (Link): The link object to be registered.

        """
        warnings.warn('''\
Child link registeration via Chain.__init__ and Chain.add_link are deprecated.
Assign a Link object directly to an attribute within a \
"with link.init_scope():" block instead.
        ''', DeprecationWarning)
        if name in self.__dict__:
            raise AttributeError(
                'cannot register a new link %s: attribute exists' % name)
        if not isinstance(link, Link):
            raise TypeError('cannot register a non-link object as a child')
        with self.init_scope():
            setattr(self, name, link)

    def copy(self):
        ret = super(Chain, self).copy()
        ret._children = set(ret._children)
        d = ret.__dict__
        for name in ret._children:
            # copy child links recursively
            copied = d[name].copy()
            copied.name = name
            d[name] = copied
        return ret

    def to_cpu(self):
        super(Chain, self).to_cpu()
        d = self.__dict__
        for name in self._children:
            d[name].to_cpu()
        return self

    def to_gpu(self, device=None):
        with cuda._get_device(device):
            super(Chain, self).to_gpu()
            d = self.__dict__
            for name in self._children:
                d[name].to_gpu()
        return self

    def to_intel64(self):
        super(Chain, self).to_intel64()
        d = self.__dict__
        for name in self._children:
            d[name].to_intel64()
        return self

    def params(self, include_uninit=True):
        for param in super(Chain, self).params(include_uninit):
            yield param
        d = self.__dict__
        for name in self._children:
            for param in d[name].params(include_uninit):
                yield param

    def namedparams(self, include_uninit=True):
        for ret in super(Chain, self).namedparams(include_uninit):
            yield ret
        d = self.__dict__
        for name in self._children:
            prefix = '/' + name
            for path, param in d[name].namedparams(include_uninit):
                yield prefix + path, param

    def links(self, skipself=False):
        if not skipself:
            yield self
        d = self.__dict__
        for name in self._children:
            for link in d[name].links():
                yield link

    def namedlinks(self, skipself=False):
        if not skipself:
            yield '/', self
        d = self.__dict__
        for name in self._children:
            child = d[name]
            prefix = '/' + name
            yield prefix, child
            for path, link in d[name].namedlinks(True):
                yield prefix + path, link

    def children(self):
        d = self.__dict__
        for name in self._children:
            yield d[name]

    def copyparams(self, link):
        super(Chain, self).copyparams(link)
        src = link.__dict__
        dst = self.__dict__
        for name in self._children:
            dst[name].copyparams(src[name])

    def addgrads(self, link):
        super(Chain, self).addgrads(link)
        src = link.__dict__
        dst = self.__dict__
        for name in self._children:
            dst[name].addgrads(src[name])

    def serialize(self, serializer):
        super(Chain, self).serialize(serializer)
        d = self.__dict__
        for name in self._children:
            d[name].serialize(serializer[name])

class ChainList(Link):

    """Composable link with list-like interface.

    This is another example of compositional link. Unlike :class:`Chain`, this
    class can be used like a list of child links. Each child link is indexed by
    a non-negative integer, and it maintains the current number of registered
    child links. The :meth:`add_link` method inserts a new link at the end of
    the list. It is useful to write a chain with arbitrary number of child
    links, e.g. an arbitrarily deep multi-layer perceptron.

    Note that this class does not implement all methods of :class:`list`.

    Args:
        links: Initial child links.

    """

    def __init__(self, *links):
        super(ChainList, self).__init__()
        self._children = []

        for link in links:
            self.add_link(link)

    def __setattr__(self, name, value):
        if self.within_init_scope and isinstance(value, Link):
            raise TypeError(
                'cannot register a new link'
                ' within a "with chainlist.init_scope():" block.')
        super(ChainList, self).__setattr__(name, value)

    def __getitem__(self, index):
        """Returns the child at given index.

        Args:
            index (int): Index of the child in the list.

        Returns:
            Link: The ``index``-th child link.

        """
        return self._children[index]

    def __iter__(self):
        return iter(self._children)

    def __len__(self):
        """Returns the number of children."""
        return len(self._children)

    def append(self, link):
        """Registers a child link and adds it to the tail of the list.

        This is equivalent to :meth:`add_link`. This method has been added to
        emulate the ``list`` interface.

        Args:
            link (Link): The link object to be regsitered.

        """
        self.add_link(link)

    def add_link(self, link):
        """Registers a child link and adds it to the tail of the list.

        Args:
            link (Link): The link object to be registered.

        """
        link.name = str(len(self._children))
        self._children.append(link)

    def copy(self):
        ret = super(ChainList, self).copy()
        ret._children = list(ret._children)  # copy
        children = ret._children
        for i, child in enumerate(children):
            child = child.copy()
            child.name = str(i)
            children[i] = child
        return ret

    def to_cpu(self):
        super(ChainList, self).to_cpu()
        for link in self._children:
            link.to_cpu()
        return self

    def to_gpu(self, device=None):
        with cuda._get_device(device):
            super(ChainList, self).to_gpu()
            for link in self._children:
                link.to_gpu()
        return self

    def to_intel64(self):
        super(ChainList, self).to_intel64()
        for link in self._children:
            link.to_intel64()
        return self

    def params(self, include_uninit=True):
        for param in super(ChainList, self).params(include_uninit):
            yield param
        for link in self._children:
            for param in link.params(include_uninit):
                yield param

    def namedparams(self, include_uninit=True):
        for ret in super(ChainList, self).namedparams(include_uninit):
            yield ret
        for idx, link in enumerate(self._children):
            prefix = '/%d' % idx
            for path, param in link.namedparams(include_uninit):
                yield prefix + path, param

    def links(self, skipself=False):
        if not skipself:
            yield self
        for child in self._children:
            for link in child.links():
                yield link

    def namedlinks(self, skipself=False):
        if not skipself:
            yield '/', self
        for idx, child in enumerate(self._children):
            prefix = '/%d' % idx
            yield prefix, child
            for path, link in child.namedlinks(True):
                yield prefix + path, link

    def children(self):
        for child in self._children:
            yield child

    def copyparams(self, link):
        super(ChainList, self).copyparams(link)
        for idx, child in enumerate(self._children):
            child.copyparams(link[idx])

    def addgrads(self, link):
        super(ChainList, self).addgrads(link)
        for idx, child in enumerate(self._children):
            child.addgrads(link[idx])

    def serialize(self, serializer):
        super(ChainList, self).serialize(serializer)
        for idx, child in enumerate(self._children):
            child.serialize(serializer['%d' % idx])


text:
#-*- coding: utf-8 -*-
import cv2 as cv
import numpy as np
import scipy.signal as sp
import scipy.interpolate as ipl
import scipy.io.wavfile as wavfile

def loadWav(filename): # -> samprate, wave in float64
    samprate, w = wavfile.read(filename)
    if(w.dtype == np.int8):
        w = w.astype(np.float64) / 128.0
    elif(w.dtype == np.short):
        w = w.astype(np.float64) / 32768.0
    elif(w.dtype == np.int32):
        w = w.astype(np.float64) / 2147483648.0
    elif(w.dtype == np.float32):
        w = w.astype(np.float64)
    elif(w.dtype == np.float64):
        pass
    else:
        raise ValueError("Unsupported sample format: %s" % (str(w.dtype)))
    return samprate, w

def saveWav(filename, data, samprate):
    wavfile.write(filename, samprate, data)

def getFrameRange(inputLen, center, size):
    leftSize = int(size / 2)
    rightSize = size - leftSize # for odd size

    inputBegin = min(inputLen, max(center - leftSize, 0))
    inputEnd = max(0, min(center + rightSize, inputLen))

    outBegin = max(leftSize - center, 0)
    outEnd = outBegin + (inputEnd - inputBegin)

    return outBegin, outEnd, inputBegin, inputEnd

fftSize = 2048
sr = 48000
magnMin = -80.0
magnMax = 0.0

b, g, r = cv.imread("ts.jpg").transpose(2, 0, 1)
v = 0.299 * r + 0.587 * g + 0.114 * b
v = v.T
v /= 255.0
w, h = v.shape
del b, g ,r

fftSize = h if(fftSize is None) else fftSize
x = np.arange(h) / (h - 1)
fftRange = np.arange(fftSize // 2 + 1) / fftSize

out = np.zeros(fftSize * w // 2)
window = np.hanning(fftSize)
basePhase = 0.0
for i in range(w):
    print("%d / %d" % (i, w))
    ob, oe, ib, ie = getFrameRange(len(out), i * (fftSize / 2), fftSize)
    magn = 1.0 - ipl.interp1d(x, v[i], kind='linear')(fftRange[::-1])
    magn = np.power(10, (magn * (magnMax - magnMin) + magnMin) / 20.0)
    magn *= fftSize // 2
    phase = np.linspace(0, 2 * np.pi, fftSize // 2 + 1)
    ffted = np.zeros(fftSize // 2 + 1, dtype = np.complex128)
    ffted.real = magn * np.cos(phase)
    ffted.imag = magn * np.sin(phase)
    out[ib:ie] += (np.fft.irfft(ffted) * window)[ob:oe]

saveWav("out.wav", out, sr)

ext: py

max_stars_repo_name: tuxzz/codebase

max_issues_repo_name: tuxzz/codebase

max_forks_repo_name: tuxzz/codebase

reason: import numpy,import scipy


text:
import os

import numpy as np
from dtcwt.opencl.lowlevel import colifilt
from dtcwt.numpy.lowlevel import colifilt as colifilt_gold
from dtcwt.coeffs import biort, qshift

from pytest import raises

from .util import assert_almost_equal, skip_if_no_cl
import tests.datasets as datasets

def setup():
    global mandrill
    mandrill = datasets.mandrill()

def test_mandrill_loaded():
    assert mandrill.shape == (512, 512)
    assert mandrill.min() >= 0
    assert mandrill.max() <= 1
    assert mandrill.dtype == np.float32

@skip_if_no_cl
def test_odd_filter():
    with raises(ValueError):
        colifilt(mandrill, (-1,2,-1), (-1,2,1))

@skip_if_no_cl
def test_different_size_h():
    with raises(ValueError):
        colifilt(mandrill, (-1,2,1), (-0.5,-1,2,-1,0.5))

@skip_if_no_cl
def test_zero_input():
    Y = colifilt(np.zeros_like(mandrill), (-1,1), (1,-1))
    assert np.all(Y[:0] == 0)

@skip_if_no_cl
def test_bad_input_size():
    with raises(ValueError):
        colifilt(mandrill[:511,:], (-1,1), (1,-1))

@skip_if_no_cl
def test_good_input_size():
    Y = colifilt(mandrill[:,:511], (-1,1), (1,-1))
    Z = colifilt_gold(mandrill[:,:511], (-1,1), (1,-1))
    assert_almost_equal(Y,Z)

@skip_if_no_cl
def test_output_size():
    Y = colifilt(mandrill, (-1,1), (1,-1))
    assert Y.shape == (mandrill.shape[0]*2, mandrill.shape[1])
    Z = colifilt_gold(mandrill, (-1,1), (1,-1))
    assert_almost_equal(Y,Z)

@skip_if_no_cl
def test_non_orthogonal_input():
    Y = colifilt(mandrill, (1,1), (1,1))
    assert Y.shape == (mandrill.shape[0]*2, mandrill.shape[1])
    Z = colifilt_gold(mandrill, (1,1), (1,1))
    assert_almost_equal(Y,Z)

@skip_if_no_cl
def test_output_size_non_mult_4():
    Y = colifilt(mandrill, (-1,0,0,1), (1,0,0,-1))
    assert Y.shape == (mandrill.shape[0]*2, mandrill.shape[1])
    Z = colifilt_gold(mandrill, (-1,0,0,1), (1,0,0,-1))
    assert_almost_equal(Y,Z)

@skip_if_no_cl
def test_non_orthogonal_input_non_mult_4():
    Y = colifilt(mandrill, (1,0,0,1), (1,0,0,1))
    assert Y.shape == (mandrill.shape[0]*2, mandrill.shape[1])
    Z = colifilt_gold(mandrill, (1,0,0,1), (1,0,0,1))
    assert_almost_equal(Y,Z)

@skip_if_no_cl
def test_qshift():
    h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d')
    y = colifilt(mandrill, h1b, h1b)
    z = colifilt_gold(mandrill, h1b, h1a)
    assert_almost_equal(y, z)

# This test fails. I'm not sure if that's expected or not because it is using
# colifilt in an odd way.
#
# @skip_if_no_cl
# def test_qshift_odd_len_input_1():
#     h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d')
#     h1a = h1a[:-2]
#     h1b = h1a[::-1]
#     y = colifilt(mandrill, h1a, h1b)
#     z = colifilt_gold(mandrill, h1a, h1b)
#     assert_almost_equal(y, z)

@skip_if_no_cl
def test_qshift_odd_len_input_2():
    h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d')
    y = colifilt(mandrill, h1a[1:-1], h1b[1:-1])
    z = colifilt_gold(mandrill, h1a[1:-1], h1b[1:-1])
    assert_almost_equal(y, z)

@skip_if_no_cl
def test_qshift_even_input():
    h1b = np.array((-0.25, 0.5, 0.5, -0.25))
    h1a = h1b[::-1]
    y = colifilt(mandrill, h1b, h1a)
    z = colifilt_gold(mandrill, h1b, h1a)
    assert_almost_equal(y, z)

# vim:sw=4:sts=4:et

reason: import numpy


text:
import numpy as np
import tensorflow as tf

class CVAE(tf.keras.Model):
    """
    Convolutional variational autoencoder.

    Helpful discussion here:
    https://towardsdatascience.com/understanding-variational-autoencoders-vaes-f70510919f73
    """

    def __init__(self, latent_dim: int, sample: tf.Tensor):
        super(CVAE, self).__init__()

        self.optimizer = tf.keras.optimizers.Adam(1e-4)

        # In an oridinary autoencoder the latent space does not necessarily have any regular structure to it that is
        # useful for generating new images.  A variational autoencoder seeks to solve this by regularizing the latent
        # space.

        # "The reason why an input is encoded as a distribution with some variance instead of a single point
        # is that it makes possible to express very naturally the latent space regularisation: the distributions
        # returned by the encoder are enforced to be close to a standard normal distribution."

        # "Without a well defined regularisation term, the model can learn, in order to minimise its reconstruction
        # error, to 'ignore' the fact that distributions are returned and behave almost like classic autoencoders
        # (leading to overfitting)."

        # The encoder takes an input image and outputs a mean and log variance which describes a Gaussian distribution.
        # The input does not have an exact representation in the latent/code space which needs to be fed to the decoder,
        # so we sample the distribution and feed this sampled latent representation (or code) to the decoder.

        # When taking into account the reparameterization z = mean + epsilon * std_dev, this distribution approximates
        # the possible z values the input could have (where epsilon is randomly sampled from a normal distribution).
        # There may be multiple z parameters in the 'z encoding'.
        # Sampling the distribution gets you a z encoding.
        # The encoder describes the conditional distribution of p(z|x) (probability of z given x (input)).

        self.latent_dim = latent_dim
        self.encoder = tf.keras.Sequential([
            tf.keras.layers.InputLayer(input_shape=sample.shape),
            tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=(2, 2), activation="relu"),
            tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=(2, 2), activation="relu"),
            tf.keras.layers.Flatten(),
            tf.keras.layers.Dense(latent_dim + latent_dim),  # No activation
        ])

        # Output is 4 variables: 2 means and 2 logvariances, one for each direction in a 2D Gaussian

        # This decoder takes the sampled z value and reconstructs the input.
        # It seems to also be possible to have the decoder output parameters (mean and logvar) for p(x|z),
        # the probability of x given a z encoding.  Then you would sample from this distribution to get a prediction.
        # It may also be possible that the literature refers to the decoder sampling a distribution internally.
        # A bit confusing really.

        # Sample shape will be reconstructed from reparameterized latent space.  Will start at 1/4 of sample image size.
        x_shape = sample.shape[0] // 4
        y_shape = sample.shape[1] // 4
        n_filters = 32

        self.decoder = tf.keras.Sequential([
            tf.keras.layers.InputLayer(input_shape=(latent_dim,)),
            tf.keras.layers.Dense(units=x_shape*y_shape*n_filters, activation=tf.nn.relu),
            tf.keras.layers.Reshape(target_shape=(x_shape, y_shape, n_filters)),
            tf.keras.layers.Conv2DTranspose(filters=64, kernel_size=3, strides=2, padding="same", activation="relu"),
            tf.keras.layers.Conv2DTranspose(filters=32, kernel_size=3, strides=2, padding="same", activation="relu"),
            tf.keras.layers.Conv2DTranspose(filters=1, kernel_size=3, strides=1, padding="same"),  # No activation
        ])

    @tf.function
    def sample(self, eps=None):
        if eps is None:
            eps = tf.random.normal(shape=(100, self.latent_dim))
        return self.decode(eps, apply_sigmoid=True)

    def encode(self, x):
        mean, logvar = tf.split(self.encoder(x), num_or_size_splits=2, axis=1)
        return mean, logvar

    @staticmethod
    def reparameterize(mean, logvar):
        """Samples our prior for z ( p(z) ) which is a Gaussian"""
        eps = tf.random.normal(shape=mean.shape)
        return eps * tf.exp(logvar * .5) + mean

    def decode(self, z, apply_sigmoid: bool = False):
        logits = self.decoder(z)
        if apply_sigmoid:
            probs = tf.sigmoid(logits)
            return probs
        return logits

    @staticmethod
    def log_normal_pdf(sample: tf.Tensor, mean: tf.Tensor, logvar: tf.Tensor, raxis: int = 1):
        """This function computes the natural logarithm of the Gaussian distribution"""
        log2pi = tf.math.log(2. * np.pi)
        ln_gaussian = -.5 * ((sample - mean) ** 2. * tf.exp(-logvar) + logvar + log2pi)
        return tf.reduce_sum(ln_gaussian, axis=raxis)

    def compute_loss(self, inputs: tf.Tensor):
        # Encode the input (which is a set of variables/pixels) to a distribution
        mean, logvar = self.encode(inputs)
        # We need to keep the trainable variables connected between the layers (no random sample layer separating them)
        # so we reparameterize to obtain z (which is the encoded representation).  Returns 2 z values.
        z = self.reparameterize(mean, logvar)
        # Reconstruct the input
        x_logit = self.decode(z)
        # Calculate difference between reconstruction and inputs
        cross_ent = tf.nn.sigmoid_cross_entropy_with_logits(logits=x_logit, labels=inputs)
        logpx_z = -tf.reduce_sum(cross_ent, axis=[1, 2, 3])
        # Compute log of normal distribution
        logpz = self.log_normal_pdf(z, tf.constant(0.), tf.constant(0.))
        # Compute log of distribution output by encoder
        logqz_x = self.log_normal_pdf(z, mean, logvar)
        # Loss has two parts: difference between input image and output image, and difference of encoder-output
        # distribution from a normal distribution.
        return -tf.reduce_mean(logpx_z + logpz - logqz_x)

    @tf.function
    def train_step(self, x):
        """Executes one training step and returns the loss.

        This function computes the loss and gradients, and uses the latter to
        update the model's parameters.
        """
        with tf.GradientTape() as tape:
            loss = self.compute_loss(x)
        gradients = tape.gradient(loss, self.trainable_variables)
        self.optimizer.apply_gradients(zip(gradients, self.trainable_variables))

max_stars_repo_name: ultraglorious/cvae

max_issues_repo_name: ultraglorious/cvae

max_forks_repo_name: ultraglorious/cvae


text:
# tweaking datatypes of pandas dataFrame
# BUG HERE

import pandas as pd
import numpy as np
df=pd.read_csv('data.csv',dtype={'salary':np.float64})
print(df.dtypes)

max_stars_repo_name: Razdeep/PythonSnippets

max_issues_repo_name: Razdeep/PythonSnippets

max_forks_repo_name: Razdeep/PythonSnippets


text:
import unittest
import numpy as np

from carl.envs import *

class TestInitEnvs(unittest.TestCase):
    def test_init_all_envs(self):
        global_vars = globals().copy()
        mustinclude = "CARL"
        forbidden = ["defaults", "bounds"]
        for varname, var in global_vars.items():
            if mustinclude in varname and not np.any([f in varname for f in forbidden]):
                try:
                    env = var()
                except Exception as e:
                    print(f"Cannot instantiate {var} environment.")
                    raise e

max_stars_repo_name: automl/genRL

max_issues_repo_name: automl/genRL

max_forks_repo_name: automl/genRL


text:
'''
实验：
用ngram来分类
其中ngram为单一数据训练模型
ngram-batch为带batch的ngram训练模型
训练取了10epoch
训练好后将原始数据通过ngram得到的特征向量保存到了./data/ngram_featrue_x.npy
0.5219466871716006
'''
import numpy as np
from sklearn.linear_model import LogisticRegression
import pandas as pd

x_train = np.array(np.load('./data/ngram_featrue_x.npy'))
y_train = np.load('./data/y_train.npy')
print(x_train.shape)
print(y_train.shape)

logist = LogisticRegression()
logist.fit(x_train,y_train)

predicted = logist.predict(x_train)
print(np.mean(predicted == y_train))

max_stars_repo_name: ClarenceHoo/NLP-beginner-task

max_issues_repo_name: ClarenceHoo/NLP-beginner-task

max_forks_repo_name: ClarenceHoo/NLP-beginner-task


text:
import src.Planning.ProbabilisticRoadmap.RandomConfigSampler as rcs
import src.Planning.ProbabilisticRoadmap.RoadmapNode as rn
import kdtree as kd
import heapq as pq
import numpy as np

def _kdtree_distance(node, point):
    return _distance(node.get_config(), point)

def _distance(config_start, config_end):
    left_x, left_y, left_theta = config_start
    right_x, right_y, right_theta = config_end

    dx = np.abs(left_x - right_x)
    dy = np.abs(left_y - right_y)
    dtheta = np.abs((left_theta - right_theta + np.pi) % (2 * np.pi) - np.pi)

    return np.sqrt(dx**2 + dy**2 + dtheta*2)

class ProbabilisticRoadmap:
    """
    local_planner: It checks if a configuration is in collision
        or there is connection between two configurations.
    k_closest: K closest neighbours used during connection step.
    distance_upper_bound: K closest neighbours will need to be within this upper bound distance.
    num_nodes: Total number of road map nodes to generate.
    """
    def __init__(self, local_planner, k_closest, distance_upper_bound, num_nodes):
        self.local_planner = local_planner
        self.k_closest = k_closest
        self.distance_upper_bound = distance_upper_bound
        self.sampler = rcs.RandomConfigSampler(local_planner)
        self.nodes = []
        self.num_nodes = num_nodes
        self.kd_tree = None

    ''' Compute the probabilistic roadmap. '''
    def compute_roadmap(self):
        # Randomly create nodes in configuration space.
        for idx in range(self.num_nodes):
            free_config = self.sampler.uniform_collision_free_sample()
            self.nodes.append(rn.RoadmapNode(idx, free_config))

        # Construct kd tree.
        self.kd_tree = kd.create(point_list=self.nodes, dimensions=3)

        # Check for connection for each node.
        for node in self.nodes:
            results = self.kd_tree.search_knn(node.get_config(), self.k_closest, _kdtree_distance)
            for (kdnode, distance) in results:
                neighbor_node = kdnode.data

                # If neighbor node is the current node, or
                # if distance is too large, discard it.
                if neighbor_node.get_id() == node.get_id() \
                        or distance > self.distance_upper_bound:
                    continue

                # Check connectivity. If it is connected to
                # the neighbour, add them as neighbour.
                connected = self.local_planner.check_connection(node.get_config(), neighbor_node.get_config())
                if connected:
                    node.add_neighbor(neighbor_node)

        return None

    ''' Return a list of collision free configuration from config_init to config_goal. '''
    def query(self, config_start, config_goal):
        if self.kd_tree is None:
            print("Run compute function first")
            return None

        start_node = self._find_k_closest_connected_config(config_start)
        if start_node is None:
            print("Cannot find a node in roadmap that connect to the start")
            return None

        goal_node = self._find_k_closest_connected_config(config_goal)
        if goal_node is None:
            print("Cannot find a node in roadmap that connect to the goal")
            return None

        # Perform dijkstra on the roadmap.
        # Dictionary that store previous node of a node in the path.
        path_dict = {}
        dist_dict = {}
        queue = []
        pq.heappush(queue, [0, start_node.get_id()])
        while len(queue) > 0:
            top = pq.heappop(queue)
            dist = top[0]
            curr_node = self.nodes[top[1]]

            if curr_node.get_id() == goal_node.get_id():
                break

            # Expand the search frontier to the neighboring nodes.
            neighbor_nodes = curr_node.get_neighbors()
            for neighbor_node in neighbor_nodes:
                # Caculate distance to neighbor.
                neighbor_dist = _distance(curr_node.get_config(), neighbor_node.get_config())
                total_dist = neighbor_dist + dist

                # Ignore this neighbor if this neighbor is visited
                # before and has shorter distance from other path.
                if neighbor_node.get_id() in dist_dict.keys() \
                    and total_dist > dist_dict[neighbor_node.get_id()]:
                        continue

                # add distance to the neighbor, push it into heap and record the path.
                dist_dict[neighbor_node.get_id()] = total_dist
                pq.heappush(queue, [total_dist, neighbor_node.get_id()])
                path_dict[neighbor_node.get_id()] = curr_node.get_id()

        # A path is found, reconstruct that path.
        if goal_node.get_id() in path_dict.keys():
            path = [config_start]
            path += self._reconstruct_path(path_dict, start_node.get_id(), goal_node.get_id())
            path.append(config_goal)
            return self._post_process_path(path)
        else:
            return None

    ''' Return a list of all configurations in the roadmap. '''
    def get_all_configs(self):
        return [n.get_config() for n in self.nodes]

    ''' Return a list of pair of connections. '''
    def get_all_connections(self):
        connections = []
        visited = set()
        for node in self.nodes:
            if node.get_id() in visited:
                continue

            curr_config = node.get_config()
            neighbors = node.get_neighbors()
            for neighbor_node in neighbors:
                if neighbor_node.get_id() in visited:
                    continue
                connections.append([curr_config, neighbor_node.get_config()])

            visited.add(node.get_id())

        return connections
    '''
    Return one of the k closest configuration node
        that is connected to given configuration.
    Return None if nothing is found.
    '''
    def _find_k_closest_connected_config(self, config):
        results = self.kd_tree.search_knn(config, self.k_closest, _kdtree_distance)
        for (kdnode, distance) in results:
            neighbor_node = kdnode.data
            neighbor_config = neighbor_node.get_config()
            if self.local_planner.check_connection(config, neighbor_config):
                return neighbor_node

        return None

    def _reconstruct_path(self, path_dict, start_id, goal_id):
        path = [goal_id]
        curr_id = goal_id
        while curr_id != start_id:
            curr_id = path_dict[curr_id]
            path.append(curr_id)

        path_config = []
        for id in path:
            path_config.append(self.nodes[id].get_config())
        path_config.reverse()

        return path_config

    def _post_process_path(self, path):
        if len(path) < 2:
            return path

        curr_idx = 0
        processed_path = []
        while curr_idx < len(path):
            lookahead_idx = curr_idx + 1
            while lookahead_idx < len(path) and \
                    self.local_planner.check_connection(path[curr_idx], path[lookahead_idx]):
                lookahead_idx += 1
            lookahead_idx -= 1
            processed_path.append(path[curr_idx])

            if lookahead_idx != curr_idx:
                curr_idx = lookahead_idx
            else:
                curr_idx += 1

        return processed_path


text:
import numpy as np
import random

from .constants import *

## --- Hand class ---

class Hand(tuple):
    '''Hand class. Hand() is a random hand. Cards are held sorted.'''

    def __new__(self, *args):
        if len(args) == 0:
            # Return random hand
            return Hand(random.sample(deck, len(deck) // 4))
        else:
            if isinstance(args[0], str):
                # From PBN
                new = [Card(s+c) for s, cds in zip(reversed(suits), args[0].split('.')) \
                            for c in cds]
            else:
                if len(args[0]) != len(deck) // 4: raise ValueError("Too many/few cards supplied ("+str(len(args[0]))+")")
                new = args[0]
            return super().__new__(self, sorted(new))

    def __repr__(self):
        return "Hand object with pbn: " + self.pbn

    memos = {'hcp', 'dist', 'pbn', 'mask'}
    def __getattr__(self, attr):
        if attr in Hand.memos:
            self.__setattr__(attr, self.__getattribute__('_'+attr)())
            return self.__getattribute__(attr)
        return super().__getattribute__(attr)

    def _hcp(self):
        hcp = 0
        for cd in self:
            if 0 <= int(cd) % len(card_vals) <= 3: hcp += 4 - (int(cd) % len(card_vals))
        return hcp

    def _dist(self):
        dist = [0, 0, 0, 0]
        for cd in self: dist[((cd-1) // 13)] += 1
        return tuple(dist)

    def _pbn(self):
        names = [Card(x).name for x in self]
        out_ch = []
        for su in reversed(suits):
            out_ch += [cd[1] for cd in names if cd[0] == su] + ['.']
        return ''.join(out_ch[:-1])

    def _mask(self):
        msk = np.zeros(len(deck))
        for cd in self: msk[int(cd)] = 1
        return msk

    def permute_suits(self, sh=0):
        # Permutes the suits in the order s -> h -> d -> c
        return Hand([((x - 1 + 13*sh) % len(deck)) + 1 for x in self])

## --- Deal class

class Deal(tuple):
    '''A deal is a tuple of four hand objects'''
    def __new__(self, *args, **kwargs):
        if len(args) == 0:
            # Return random deal
            sh_deck = deck.copy()
            random.shuffle(sh_deck)
            return Deal([Hand(sh_deck[13*t:13*(t+1)]) for t in range(4)])
        elif isinstance(args[0], str):
            return Deal([Hand(pbn) for pbn in args[0].split(' ')])
        elif len(args[0]) < 4:
            # Generate missing hands randomly
            for hd in args[0]:
                if not isinstance(hd, Hand): raise ValueError("Arguments must be Hands")
            msk = np.ones(len(deck))
            for hd in args[0]: msk -= hd.mask
            remaining = [Card(i) for i in range(len(deck)) if msk[i] == 1]
            random.shuffle(remaining)
            if len(remaining) + 13 * len(args[0]) != 52: raise ValueError("Duplicate cards supplied")
            return Deal(args[0] + [Hand(remaining[13*t:13*(t+1)]) for t in range(4 - len(args[0]))])
        else:
            return super().__new__(self, args[0], **kwargs)

    def __init__(self, *args, dealer=None, vul=None):

        if dealer: self.dlr = dealer
        else: self.dlr = random.choice(players)

        if vul: self.vul = vul
        else: self.vul = (random.choice([0, 1]), random.choice([0, 1]))

    def __repr__(self):
        return "Deal object with pbn: " + self.pbn

    memos = {'pbn', 'mask', 'N', 'E', 'S', 'W'}
    def __getattr__(self, attr):
        if attr in Deal.memos:
            self.__setattr__(attr, self.__getattribute__('_'+attr)())
            return self.__getattribute__(attr)
        return super().__getattribute__(attr)

    def _pbn(self):
        return ' '.join([hd.pbn for hd in self])

    def _N(self): return self[0]
    def _E(self): return self[1]
    def _S(self): return self[2]
    def _W(self): return self[3]

    def _mask(self):
        return np.stack([hd.mask for hd in self])

    def from_NS(self, handN, handS):
        newd = Deal([handN, handS])
        return Deal([newd[0], newd[2], newd[1], newd[3]])

    def permute_suits(self, sh=0):
        # Permutes the suits in the order s -> h -> d -> c
        return Deal([hd.permute_suits(sh) for hd in self])

    def permute_players(self, sh=0):
        return Deal(self[(sh % 4):] + self[:(sh % 4)])

max_stars_repo_name: FabioPru/bridge-lib

max_issues_repo_name: FabioPru/bridge-lib

max_forks_repo_name: FabioPru/bridge-lib


text:
"""GO Ontology data methods."""
import logging
import os
import json
import datetime
import time
import pickle
from collections import defaultdict
from multiprocessing import Pool

import numpy as np
from scipy.stats import spearmanr
import pandas as pd
from tqdm import tqdm
from goatools.obo_parser import GODag
from goatools.associations import read_ncbi_gene2go
from goatools.go_enrichment import GOEnrichmentStudy

def load_go_annotations(proteins,
                        levels=None,
                        obodag_path="data/go/go-basic.obo",
                        entrez_to_go_path="data/go/gene2go.txt"):
    """
    args:
    @proteins    (iterable)   proteins to get annotations for
    @levels  (list(int)) the levels of the ontology
    @obodag     (str)   path obo file
    @entrez_to_go_path (str) path to mapping from entrez ids to go doids

    return:
    @term_to_proteins (dict) map from term
    """
    obodag = GODag(obodag_path)
    entrez_to_go = read_ncbi_gene2go(entrez_to_go_path, taxids=[9606])

    def get_annotations(protein, levels):
        """
        """
        terms = set()
        doids = entrez_to_go[protein]
        for doid in doids:
            for parent in obodag[doid].get_all_parents():
                if levels is None or obodag[parent].level in levels:
                    terms.add(obodag[parent].name)

        return terms

    term_to_proteins = defaultdict(set)
    for protein in proteins:
        terms = get_annotations(protein, levels)
        for term in terms:
            term_to_proteins[term].add(protein)

    return term_to_proteins

max_stars_repo_name: seyuboglu/milieu

max_issues_repo_name: seyuboglu/milieu

max_forks_repo_name: seyuboglu/milieu

reason: import numpy,from scipy


text:
import numpy

example_list = [5,2,5,6,1,2,6,7,2,6,3,5,5]
x = numpy.mean(example_list)
print(x)