Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nick/tenmat docs #294

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
SPTENMAT: Remove from_data. Merge with __init__
  • Loading branch information
ntjohnson1 committed Nov 24, 2023
commit 744e1cbc7caa4426df7598550b7c0708e39a27c0
73 changes: 45 additions & 28 deletions pyttb/sptenmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,8 @@ class sptenmat(object):

__slots__ = ("tshape", "rdims", "cdims", "subs", "vals")

def __init__(self):
"""
Construct an empty :class:`pyttb.sptenmat`
"""
self.tshape = ()
self.rdims = np.array([])
self.cdims = np.array([])
self.subs = np.array([], ndmin=2, dtype=int)
self.vals = np.array([], ndmin=2)

@classmethod
def from_data( # noqa: PLR0913
cls,
def __init__( # noqa: PLR0913
self,
subs: Optional[np.ndarray] = None,
vals: Optional[np.ndarray] = None,
rdims: Optional[np.ndarray] = None,
Expand All @@ -50,15 +39,45 @@ def from_data( # noqa: PLR0913
Parameters
----------
subs:
Location of non-zero entries
Location of non-zero entries, in sptenmat.
vals:
Values for non-zero entries
Values for non-zero entries, in sptenmat.
rdims:
Mapping of row indices
Mapping of row indices.
cdims:
Mapping of column indices
Mapping of column indices.
tshape:
Shape of the original tensor
Shape of the original tensor.

Examples
--------
Create an empty :class:`pyttb.sptenmat`:

>>> S = ttb.sptenmat()
>>> S # doctest: +NORMALIZE_WHITESPACE
sptenmat corresponding to a sptensor of shape () with 0 nonzeros
rdims = [ ] (modes of sptensor corresponding to rows)
cdims = [ ] (modes of sptensor corresponding to columns)

Create a :class:`pyttb.sptenmat` from subscripts, values, and unwrapping
dimensions:

>>> subs = np.array([[1, 6], [1, 7]])
>>> vals = np.array([[6], [7]])
>>> tshape = (4, 4, 4)
>>> S = ttb.sptenmat(\
subs,\
vals,\
rdims=np.array([0]),\
cdims=np.array([1,2]),\
tshape=tshape\
)
>>> S # doctest: +NORMALIZE_WHITESPACE
sptenmat corresponding to a sptensor of shape (4, 4, 4) with 2 nonzeros
rdims = [ 0 ] (modes of sptensor corresponding to rows)
cdims = [ 1, 2 ] (modes of sptensor corresponding to columns)
[1, 6] = 6
[1, 7] = 7
"""
if subs is None:
subs = np.array([], ndmin=2, dtype=int)
Expand Down Expand Up @@ -106,13 +125,11 @@ def from_data( # noqa: PLR0913
if newvals.size > 0:
newvals = newvals[:, None]

sptenmatInstance = cls()
sptenmatInstance.tshape = tshape
sptenmatInstance.rdims = rdims.copy().astype(int)
sptenmatInstance.cdims = cdims.copy().astype(int)
sptenmatInstance.subs = newsubs
sptenmatInstance.vals = newvals
return sptenmatInstance
self.tshape = tshape
self.rdims = rdims.copy().astype(int)
self.cdims = cdims.copy().astype(int)
self.subs = newsubs
self.vals = newvals

@classmethod
def from_tensor_type( # noqa: PLR0912
Expand Down Expand Up @@ -189,7 +206,7 @@ def from_tensor_type( # noqa: PLR0912
cidx = tt_sub2ind(csize, source.subs[:, cdims])
cidx = cidx.reshape((cidx.size, 1)).astype(int)

return cls().from_data(
return cls(
np.hstack([ridx, cidx], dtype=int),
source.vals.copy(),
rdims.astype(int),
Expand Down Expand Up @@ -231,7 +248,7 @@ def from_array(
f"Expected sparse matrix or array but received: {type(array)}"
)
subs = np.vstack(array.nonzero()).transpose()
return ttb.sptenmat.from_data(subs, vals, rdims, cdims, tshape)
return ttb.sptenmat(subs, vals, rdims, cdims, tshape)

def copy(self) -> sptenmat:
"""
Expand All @@ -253,7 +270,7 @@ def copy(self) -> sptenmat:
>>> ST1.to_sptensor().isequal(ST3.to_sptensor())
False
"""
return sptenmat().from_data(
return sptenmat(
self.subs.copy(),
self.vals.copy(),
self.rdims.copy(),
Expand Down
10 changes: 5 additions & 5 deletions tests/test_sptenmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def sample_sptenmat():
"cdims": cdims,
"tshape": tshape,
}
sptenmatInstance = ttb.sptenmat.from_data(subs, vals, rdims, cdims, tshape)
sptenmatInstance = ttb.sptenmat(subs, vals, rdims, cdims, tshape)
return data, sptenmatInstance


Expand Down Expand Up @@ -90,7 +90,7 @@ def test_sptenmat_initialization_from_data(sample_sptenmat):
shape = (np.prod(np.array(tshape)[rdims]), np.prod(np.array(tshape)[cdims]))

# Constructor from data: subs, vals, rdims, cdims, and tshape
S = ttb.sptenmat.from_data(subs, vals, rdims, cdims, tshape)
S = ttb.sptenmat(subs, vals, rdims, cdims, tshape)
np.testing.assert_array_equal(S.subs, subs)
np.testing.assert_array_equal(S.vals, vals)
np.testing.assert_array_equal(S.rdims, rdims)
Expand All @@ -99,7 +99,7 @@ def test_sptenmat_initialization_from_data(sample_sptenmat):
np.testing.assert_array_equal(S.shape, shape)

# Constructor from data: rdims, cdims, and tshape
S = ttb.sptenmat.from_data(rdims=rdims, cdims=cdims, tshape=tshape)
S = ttb.sptenmat(rdims=rdims, cdims=cdims, tshape=tshape)
np.testing.assert_array_equal(S.subs, np.array([]))
np.testing.assert_array_equal(S.vals, np.array([]))
np.testing.assert_array_equal(S.rdims, rdims)
Expand All @@ -110,7 +110,7 @@ def test_sptenmat_initialization_from_data(sample_sptenmat):
# Constructor from data: rdims, and tshape
all_rdims = np.arange(len(tshape))
rdims_shape = (np.prod(tshape), 1)
S = ttb.sptenmat.from_data(rdims=all_rdims, tshape=tshape)
S = ttb.sptenmat(rdims=all_rdims, tshape=tshape)
np.testing.assert_array_equal(S.subs, np.array([]))
np.testing.assert_array_equal(S.vals, np.array([]))
np.testing.assert_array_equal(S.rdims, all_rdims)
Expand All @@ -120,7 +120,7 @@ def test_sptenmat_initialization_from_data(sample_sptenmat):

# Constructor from data: cdims, and tshape
cdims_shape = (1, np.prod(tshape))
S = ttb.sptenmat.from_data(cdims=all_rdims, tshape=tshape)
S = ttb.sptenmat(cdims=all_rdims, tshape=tshape)
np.testing.assert_array_equal(S.subs, np.array([]))
np.testing.assert_array_equal(S.vals, np.array([]))
np.testing.assert_array_equal(S.rdims, np.array([]))
Expand Down