numpy.iterable - python examples

Here are the examples of the python api numpy.iterable taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

54 Examples 7

3 View Complete Implementation : stride_tricks.py
Copyright MIT License
Author : abhisuri97
def _broadcast_to(array, shape, subok, readonly):
    shape = tuple(shape) if np.iterable(shape) else (shape,)
    array = np.array(array, copy=False, subok=subok)
    if not shape and array.shape:
        raise ValueError('cannot broadcast a non-scalar to a scalar array')
    if any(size < 0 for size in shape):
        raise ValueError('all elements of broadcast shape must be non-'
                         'negative')
    needs_writeable = not readonly and array.flags.writeable
    extras = ['reduce_ok'] if needs_writeable else []
    op_flag = 'readwrite' if needs_writeable else 'readonly'
    broadcast = np.nditer(
        (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras,
        op_flags=[op_flag], itershape=shape, order='C').itviews[0]
    result = _maybe_view_as_subclast(array, broadcast)
    if needs_writeable and not result.flags.writeable:
        result.flags.writeable = True
    return result

3 View Complete Implementation : _numpy_compat.py
Copyright MIT License
Author : alvarob96
    def _broadcast_to(array, shape, subok, readonly):
        shape = tuple(shape) if np.iterable(shape) else (shape,)
        array = np.array(array, copy=False, subok=subok)
        if not shape and array.shape:
            raise ValueError('cannot broadcast a non-scalar to a scalar array')
        if any(size < 0 for size in shape):
            raise ValueError('all elements of broadcast shape must be non-'
                             'negative')
        broadcast = np.nditer(
            (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'],
            op_flags=['readonly'], itershape=shape, order='C').itviews[0]
        result = _maybe_view_as_subclast(array, broadcast)
        if not readonly and array.flags.writeable:
            result.flags.writeable = True
        return result

3 View Complete Implementation : mosaicplot.py
Copyright MIT License
Author : birforce
def _tuplify(obj):
    """convert an object in a tuple of strings (even if it is not iterable,
    like a single integer number, but keep the string healthy)
    """
    if np.iterable(obj) and not isinstance(obj, string_types):
        res = tuple(str(o) for o in obj)
    else:
        res = (str(obj),)
    return res

3 View Complete Implementation : EpochConverter.py
Copyright MIT License
Author : boris-kz
    @staticmethod
    def default_units(value, axis):
        """: Return the default unit for value, or None.

        = INPUT VARIABLES
        - value    The value or list of values that need units.

        = RETURN VALUE
        - Returns the default units to use for value.
        """
        frame = None
        if np.iterable(value) and not isinstance(value, str):
            return EpochConverter.default_units(value[0], axis)
        else:
            frame = value.frame()

        return frame

3 View Complete Implementation : UnitDblConverter.py
Copyright MIT License
Author : boris-kz
    @staticmethod
    def default_units(value, axis):
        """: Return the default unit for value, or None.

        = INPUT VARIABLES
        - value    The value or list of values that need units.

        = RETURN VALUE
        - Returns the default units to use for value.
        Return the default unit for value, or None.
        """

        # Determine the default units based on the user preferences set for
        # default units when printing a UnitDbl.
        if np.iterable(value) and not isinstance(value, str):
            return UnitDblConverter.default_units(value[0], axis)
        else:
            return UnitDblConverter.defaults[value.type()]

3 View Complete Implementation : units.py
Copyright MIT License
Author : boris-kz
    @staticmethod
    def is_numlike(x):
        """
        The Matplotlib datalim, autoscaling, locators etc work with scalars
        which are the units converted to floats given the current unit.  The
        converter may be pasted these floats, or arrays of them, even when
        units are set.
        """
        if np.iterable(x):
            for thisx in x:
                if thisx is ma.masked:
                    continue
                return isinstance(thisx, Number)
        else:
            return isinstance(x, Number)

3 View Complete Implementation : Vector.py
Copyright GNU Affero General Public License v3.0
Author : FabriceSalvaire
    def _check_arguments(self, args):

        size = len(args)
        if size == 1:
            array = args[0]
        elif size == 2:
            array = args
        else:
            raise ValueError("More than 2 arguments where given")

        if not (np.iterable(array) and len(array) == 2):
            raise ValueError("Argument must be iterable and of length 2")

        return array

3 View Complete Implementation : layer.py
Copyright GNU General Public License v2.0
Author : has2k1
def is_known_scalar(value):
    """
    Return True if value is a type we expect in a dataframe
    """
    def _is_datetime_or_timedelta(value):
        # Using pandas.Series helps catch python, numpy and pandas
        # versions of these types
        return pd.Series(value).dtype.kind in ('M', 'm')

    return not np.iterable(value) and (isinstance(value, numbers.Number) or
                                       _is_datetime_or_timedelta(value))

3 View Complete Implementation : scale.py
Copyright GNU General Public License v2.0
Author : has2k1
    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            if hasattr(self, k):
                setattr(self, k, v)
            else:
                msg = "{} could not recognise parameter `{}`"
                warn(msg.format(self.__clast__.__name__, k), PlotnineWarning)

        self.range = self._range_clast()

        if np.iterable(self.breaks) and np.iterable(self.labels):
            if len(self.breaks) != len(self.labels):
                raise PlotnineError(
                    "Breaks and labels have unequal lengths")

        if (self.breaks is None and
                not is_position_aes(self.aesthetics) and
                self.guide is not None):
            self.guide = None

3 View Complete Implementation : utils.py
Copyright GNU General Public License v2.0
Author : has2k1
def make_iterable(val):
    """
    Return [*val*] if *val* is not iterable

    Strings are not recognized as iterables
    """
    if np.iterable(val) and not is_string(val):
        return val
    return [val]