Beispiel #1
0
    def __init__(
        self,
        initial_value=None,
        shape=None,
        name=None,
        base=None,
        readonly=False,
        offset=0,
    ):
        if self.assert_named_signals:
            assert name is not None
        self._name = name

        if initial_value is None:
            assert shape is not None
            initial_value = np.zeros(shape, dtype=rc.float_dtype)
        elif shape is not None:
            assert initial_value.shape == shape

        self._initial_value = initial_value
        if np.any(
            np.isnan(self._initial_value.data if self.sparse else self._initial_value)
        ):
            raise SignalError(f"{self!r} contains NaNs.")

        if self.sparse:
            assert initial_value.ndim == 2
            assert offset == 0
            assert base is None
            if npext.is_spmatrix(initial_value):
                self._initial_value.data.setflags(write=False)
        else:
            # To ensure we do not modify data passed into the signal,
            # we make a view of the data and mark it as not writeable.
            # Consumers (like SignalDict) are responsible for making copies
            # that can be modified, or using the readonly view appropriately.
            readonly_view = np.asarray(self._initial_value)
            if readonly_view.ndim > 0 and base is None:
                readonly_view = np.ascontiguousarray(readonly_view)
            # Ensure we have a view and aren't modifying the original's flags
            readonly_view = readonly_view.view()
            readonly_view.setflags(write=False)
            self._initial_value = readonly_view

        if base is not None:
            assert isinstance(base, Signal) and not base.is_view
            # make sure initial_value uses the same data as base.initial_value
            assert initial_value.base is base.initial_value.base
        self._base = base
        self._offset = offset

        self._readonly = bool(readonly)
Beispiel #2
0
def is_sparse(obj):
    """Check if ``obj`` is a sparse matrix."""
    return isinstance(obj, SparseMatrix) or npext.is_spmatrix(obj)
def is_sparse(obj):
    return isinstance(obj, SparseMatrix) or npext.is_spmatrix(obj)