Beispiel #1
0
def inv(df, pre_clean=False):
    if isinstance(df.data, COO):
        if pre_clean:
            data = df.data
            assert df.ndim <= 2, ('Maximally two dimension supported for '
                                  'sparse inverse')
            mask = np.isin(np.arange(data.shape[0]), data.coords[0]) & \
                np.isin(np.arange(data.shape[1]), data.coords[1])
            subdf = df[mask][:, mask]
            return DataArray(
                as_coo(sp_inv(subdf.data.tocsc())),
                subdf.T.coords).reindex(**{
                    df.dims[0]: df.get_index(df.dims[0])
                },
                                        fill_value=0).reindex(**{
                                            df.dims[1]:
                                            df.get_index(df.dims[1])
                                        },
                                                              fill_value=0)
        return DataArray(as_coo(sp_inv(df.data.tocsc())), df.T.coords)
    if pre_clean:
        zeros_b = df == 0
        mask = tuple(df.get_index(d)[~zeros_b.all(d).data] for d in df.dims)
        subdf = df.loc[mask]
        return DataArray(np.linalg.inv(subdf), subdf.T.coords,
                         subdf.dims[::-1]).reindex(df.T.coords, fill_value=0)
    return DataArray(np.linalg.inv(df), df.T.coords)
Beispiel #2
0
 def __init__(self, data, shape=None):
     if type(data) is tuple:
         indices, values = data
         self._data = sparse.COO(indices, values, shape=shape)
         #import ipdb; ipdb.set_trace()
     elif isinstance(data, DTensor):
         self._data = sparse.as_coo(data._data, shape=shape)
     elif isinstance(data, STensor):
         self._data = data._data
     else:
         self._data = sparse.as_coo(data, shape=shape)
Beispiel #3
0
def Incidence(n, branch_components=None, sparse=False):
    """
    Calculate the Incidence matrix for a given networ with given branch components.

    Parameters
    ----------
    n : pypsa.Netowrk
    branch_components : list, optional
        List of branch components to be included in the Incidence matris.
        The default is None results in n.branch_components.
    sparse : bool, optional
        Whether the resulting data should be sparse or not. The default is False.

    Returns
    -------
    K : xr.DataArray
        Incidence matrix with dimensions N (#buses) x L (#branches).

    """
    branch_components = check_branch_comps(branch_components, n)
    if sparse:
        K = as_coo(n.incidence_matrix(branch_components))
    else:
        K = n.incidence_matrix(branch_components).todense()
    branches_i = get_branches_i(n, branch_components)
    return DataArray(K,
                     coords=(n.buses.index, branches_i),
                     dims=['bus', 'branch'])
Beispiel #4
0
def __ua_convert__(value, dispatch_type, coerce):
    if dispatch_type is ndarray:
        if not coerce:
            return value

        if value is None:
            return None

        if isinstance(value, sparse.SparseArray):
            return value

        return sparse.as_coo(np.asarray(value))

    if dispatch_type is ufunc:
        return getattr(np, value.name)

    return value
Beispiel #5
0
def assparse(xds_dense):
    import xarray as xr
    import numpy as np
    import sparse

    if isinstance(xds_dense, xr.DataArray):
        xds_sparse = xr.DataArray(
            sparse.as_coo(xds_dense.values, fill_value=np.NaN),
            coords=xds_dense.coords,
            dims=xds_dense.dims,
            attrs=xds_dense.attrs,
        )
        return xds_sparse

    elif isinstance(xds_dense, xr.Dataset):
        xds_sparse = xr.Dataset()
        for key in xds_dense:
            xds_sparse[key] = assparse(xds_dense[key])
        return xds_sparse