Exemple #1
0
def empty(axes=None, dims=None, shape=None, dtype=float):
    """ Initialize an empty array

    axes and dims have the same meaning as DimArray's initializer
    shape, optional: can be provided in combination with `dims`
        if `axes=` is omitted.

    >>> a = empty([('time',[2000,2001]),('items',['a','b','c'])])
    >>> a.fill(3)
    >>> a
    dimarray: 6 non-null elements (0 null)
    dimensions: 'time', 'items'
    0 / time (2): 2000 to 2001
    1 / items (3): a to c
    array([[ 3.,  3.,  3.],
           [ 3.,  3.,  3.]])
    >>> b = empty(dims=('time','items'), shape=(2, 3))

    See also:
    ---------
    empty_like, ones, zeros, nans
    """
    axes = Axes._init(axes, dims=dims, shape=shape)

    if shape is None:
        shape = [ax.size for ax in axes]

    values = np.empty(shape, dtype=dtype)

    return DimArray(values, axes=axes, dims=dims)
Exemple #2
0
def empty(axes=None, dims=None, shape=None, dtype=float):
    """ Initialize an empty array

    axes and dims have the same meaning as DimArray's initializer
    shape, optional: can be provided in combination with `dims`
        if `axes=` is omitted.

    >>> a = empty([('time',[2000,2001]),('items',['a','b','c'])])
    >>> a.fill(3)
    >>> a
    dimarray: 6 non-null elements (0 null)
    dimensions: 'time', 'items'
    0 / time (2): 2000 to 2001
    1 / items (3): a to c
    array([[ 3.,  3.,  3.],
           [ 3.,  3.,  3.]])
    >>> b = empty(dims=('time','items'), shape=(2, 3))

    See also:
    ---------
    empty_like, ones, zeros, nans
    """
    axes = Axes._init(axes, dims=dims, shape=shape)

    if shape is None:
        shape = [ax.size for ax in axes]

    values = np.empty(shape, dtype=dtype)

    return DimArray(values, axes=axes, dims=dims)
Exemple #3
0
    def __init__(self, values=None, axes=None, dims=None, labels=None, copy=False, dtype=None, _indexing=None, _indexing_broadcast=None, **kwargs):
        """ Initialization. See help on DimArray.
        """
        # check if attached to values (e.g. DimArray object)
        if hasattr(values, "axes") and axes is None:
            axes = values.axes

        # default options
        if _indexing is None: _indexing = get_option('indexing.by')
        if _indexing_broadcast is None: _indexing_broadcast = get_option('indexing.broadcast')

        #
        # array values
        #
        # if masked array, replace mask by NaN
        if isinstance(values, np.ma.MaskedArray):
            try:
                values = values.filled(np.nan) # fill mask with nans

            # first convert to float
            except:
                values = np.ma.asarray(values, dtype=float).filled(np.nan) # fill mask with nans

        if values is not None:
            values = np.array(values, copy=copy, dtype=dtype)

        #
        # Initialize the axes
        # 
        if axes is None and labels is None:
            assert values is not None, "values= or/and axes=, labels= required to determine dimensions"

        axes = Axes._init(axes, dims=dims, labels=labels, shape=values.shape if values is not None else None)
        assert type(axes) is Axes

        # if values not provided, create empty data, filled with NaNs if dtype is float
        if values is None:
            values = np.empty([ax.size for ax in axes], dtype=dtype)
            if dtype in (float, None, np.dtype(float)):
                values.fill(np.nan)
            else:
                warnings.warn("no nan representation for {}, array left empty".format(repr(dtype)))

        #
        # store all fields
        #
        self.values = values
        self.axes = axes

        ## options
        self._indexing = _indexing
        self._indexing_broadcast = _indexing_broadcast

        #
        # metadata (see Metadata type in metadata.py)
        #
        #for k in kwargs:
        #    setncattr(self, k, kwargs[k]) # perform type-checking and store in self._metadata
        self._metadata = kwargs

        # Check consistency between axes and values
        inferred = tuple([ax.size for ax in self.axes])
        if inferred != self.values.shape:
            msg = """\
shape inferred from axes: {}
shape inferred from data: {}
mismatch between values and axes""".format(inferred, self.values.shape)
            raise Exception(msg)

        # If a general ordering relationship of the class is assumed,
        # always sort the class
        if self._order is not None and self.dims != tuple(dim for dim in self._order if dim in self.dims):
            present = filter(lambda x: x in self.dims, self._order)  # prescribed
            missing = filter(lambda x: x not in self._order, self.dims)  # not
            order = missing + present # prepend dimensions not found in ordering relationship
            obj = self.transpose(order)
            self.values = obj.values
            self.axes = obj.axes
Exemple #4
0
    def __init__(self,
                 values=None,
                 axes=None,
                 dims=None,
                 labels=None,
                 copy=False,
                 dtype=None,
                 _indexing=None,
                 _indexing_broadcast=None,
                 **kwargs):
        """ Initialization. See help on DimArray.
        """
        # check if attached to values (e.g. DimArray object)
        if hasattr(values, "axes") and axes is None:
            axes = values.axes

        # default options
        if _indexing is None: _indexing = get_option('indexing.by')
        if _indexing_broadcast is None:
            _indexing_broadcast = get_option('indexing.broadcast')

        #
        # array values
        #
        # if masked array, replace mask by NaN
        if isinstance(values, np.ma.MaskedArray):
            try:
                values = values.filled(np.nan)  # fill mask with nans

            # first convert to float
            except:
                values = np.ma.asarray(values, dtype=float).filled(
                    np.nan)  # fill mask with nans

        if values is not None:
            values = np.array(values, copy=copy, dtype=dtype)

        #
        # Initialize the axes
        #
        if axes is None and labels is None:
            assert values is not None, "values= or/and axes=, labels= required to determine dimensions"

        axes = Axes._init(axes,
                          dims=dims,
                          labels=labels,
                          shape=values.shape if values is not None else None)
        assert type(axes) is Axes

        # if values not provided, create empty data, filled with NaNs if dtype is float
        if values is None:
            values = np.empty([ax.size for ax in axes], dtype=dtype)
            if dtype in (float, None, np.dtype(float)):
                values.fill(np.nan)
            else:
                warnings.warn(
                    "no nan representation for {}, array left empty".format(
                        repr(dtype)))

        #
        # store all fields
        #
        self.values = values
        self.axes = axes

        ## options
        self._indexing = _indexing
        self._indexing_broadcast = _indexing_broadcast

        #
        # metadata (see Metadata type in metadata.py)
        #
        #for k in kwargs:
        #    setncattr(self, k, kwargs[k]) # perform type-checking and store in self._metadata
        self._metadata = kwargs

        # Check consistency between axes and values
        inferred = tuple([ax.size for ax in self.axes])
        if inferred != self.values.shape:
            msg = """\
shape inferred from axes: {}
shape inferred from data: {}
mismatch between values and axes""".format(inferred, self.values.shape)
            raise Exception(msg)

        # If a general ordering relationship of the class is assumed,
        # always sort the class
        if self._order is not None and self.dims != tuple(
                dim for dim in self._order if dim in self.dims):
            present = filter(lambda x: x in self.dims,
                             self._order)  # prescribed
            missing = filter(lambda x: x not in self._order, self.dims)  # not
            order = missing + present  # prepend dimensions not found in ordering relationship
            obj = self.transpose(order)
            self.values = obj.values
            self.axes = obj.axes