From 6b3b4326639389b0d8c4edf24ec27beb35ed6f2f Mon Sep 17 00:00:00 2001 From: Adam Lugowski Date: Wed, 25 Oct 2023 18:32:28 -0700 Subject: [PATCH] Support pydata/sparse 0.13 --- matspy/adapters/__init__.py | 5 ++++- matspy/adapters/graphblas_impl.py | 5 +---- matspy/adapters/numpy_impl.py | 5 +---- matspy/adapters/scipy_impl.py | 4 +--- matspy/adapters/sparse_impl.py | 9 +++++---- 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/matspy/adapters/__init__.py b/matspy/adapters/__init__.py index a33d0ee..5dac00a 100644 --- a/matspy/adapters/__init__.py +++ b/matspy/adapters/__init__.py @@ -8,7 +8,7 @@ import numpy as np -def describe(shape: tuple = None, nnz: int = None, nz_type=None, notes: str = None) -> str: +def describe(shape: tuple = None, nnz: int = None, nz_type=None, layout: str = None, notes: str = None) -> str: """ Create a simple description string from potentially interesting pieces of metadata. """ @@ -27,6 +27,9 @@ def describe(shape: tuple = None, nnz: int = None, nz_type=None, notes: str = No elif nz_type is not None: parts.append(f"'{str(nz_type)}' elements") + if layout is not None: + parts.append(str(layout)) + if notes: parts.append(notes) diff --git a/matspy/adapters/graphblas_impl.py b/matspy/adapters/graphblas_impl.py index 01ea9e1..be78387 100644 --- a/matspy/adapters/graphblas_impl.py +++ b/matspy/adapters/graphblas_impl.py @@ -56,12 +56,9 @@ def get_format(self, is_transposed=False): def describe(self) -> str: parts = [f"gb.{type(self.mat).__name__}", f"'{self.mat.dtype}'"] - fmt = self.get_format() - if fmt: - parts.append(fmt) - return describe(shape=self.mat.shape, nnz=self.mat.nvals, + layout=self.get_format(), notes=", ".join(parts)) def get_spy(self, spy_shape: tuple) -> np.array: diff --git a/matspy/adapters/numpy_impl.py b/matspy/adapters/numpy_impl.py index 97af8fb..e8fb75d 100644 --- a/matspy/adapters/numpy_impl.py +++ b/matspy/adapters/numpy_impl.py @@ -20,10 +20,7 @@ def get_shape(self) -> tuple: return self.arr.shape def describe(self) -> str: - format_name = "array" - - return describe(shape=self.arr.shape, nz_type=self.arr.dtype, - notes=f"{format_name}") + return describe(shape=self.arr.shape, nz_type=self.arr.dtype, layout="array") def get_spy(self, spy_shape: tuple) -> np.array: precision = self.get_option("precision", None) diff --git a/matspy/adapters/scipy_impl.py b/matspy/adapters/scipy_impl.py index b9c1a4b..c058f34 100644 --- a/matspy/adapters/scipy_impl.py +++ b/matspy/adapters/scipy_impl.py @@ -31,10 +31,8 @@ def get_shape(self) -> tuple: return self.mat.shape def describe(self) -> str: - format_name = self.mat.getformat() - return describe(shape=self.mat.shape, nnz=self.mat.nnz, nz_type=self.mat.dtype, - notes=f"{format_name}") + layout=self.mat.getformat()) def get_spy(self, spy_shape: tuple) -> np.array: # construct a triple product that will scale the matrix diff --git a/matspy/adapters/sparse_impl.py b/matspy/adapters/sparse_impl.py index 2682c57..079bd65 100644 --- a/matspy/adapters/sparse_impl.py +++ b/matspy/adapters/sparse_impl.py @@ -31,13 +31,14 @@ def get_shape(self) -> tuple: return self.mat.shape def describe(self) -> str: - parts = [ - self.mat.format, - ] + try: + fmt = self.mat.format + except AttributeError: + fmt = self.mat.__class__.__name__ return describe(shape=self.mat.shape, nnz=self.mat.nnz, nz_type=self.mat.dtype, - notes=", ".join(parts)) + layout=fmt) def get_spy(self, spy_shape: tuple) -> np.array: if isinstance(self.mat, sparse.DOK):