From 44e434dc14d24c35aac6bfe29c1091dfa1f020f5 Mon Sep 17 00:00:00 2001 From: Adam Lugowski Date: Thu, 7 Sep 2023 14:08:33 -0700 Subject: [PATCH] Use matrepr for rich `__str__` and `_repr_html_` (#605) --- sparse/_compressed/compressed.py | 6 +++-- sparse/_coo/core.py | 3 ++- sparse/_dok.py | 3 ++- sparse/_sparse_array.py | 39 +++++++++++++++++++++++++++++++- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/sparse/_compressed/compressed.py b/sparse/_compressed/compressed.py index 7bb21014..70f4125d 100644 --- a/sparse/_compressed/compressed.py +++ b/sparse/_compressed/compressed.py @@ -344,9 +344,10 @@ def T(self): return self.transpose() def __str__(self): - return "".format( + summary = "".format( self.shape, self.dtype, self.nnz, self.fill_value, self.compressed_axes ) + return self._str_impl(summary) __repr__ = __str__ @@ -864,13 +865,14 @@ def __init__( ) def __str__(self): - return "<{}: shape={}, dtype={}, nnz={}, fill_value={}>".format( + summary = "<{}: shape={}, dtype={}, nnz={}, fill_value={}>".format( type(self).__name__, self.shape, self.dtype, self.nnz, self.fill_value, ) + return self._str_impl(summary) __repr__ = __str__ diff --git a/sparse/_coo/core.py b/sparse/_coo/core.py index 01e07c0e..1579c011 100644 --- a/sparse/_coo/core.py +++ b/sparse/_coo/core.py @@ -702,9 +702,10 @@ def __sizeof__(self): __getitem__ = getitem def __str__(self): - return "".format( + summary = "".format( self.shape, self.dtype, self.nnz, self.fill_value ) + return self._str_impl(summary) __repr__ = __str__ diff --git a/sparse/_dok.py b/sparse/_dok.py index 1aa330b7..36b382cf 100644 --- a/sparse/_dok.py +++ b/sparse/_dok.py @@ -451,9 +451,10 @@ def _setitem(self, key_list, value): del self.data[key] def __str__(self): - return "".format( + summary = "".format( self.shape, self.dtype, self.nnz, self.fill_value ) + return self._str_impl(summary) __repr__ = __str__ diff --git a/sparse/_sparse_array.py b/sparse/_sparse_array.py index 546addc7..deb90b1b 100644 --- a/sparse/_sparse_array.py +++ b/sparse/_sparse_array.py @@ -170,7 +170,44 @@ def _repr_html_(self): Diagnostic report about this array. Renders in Jupyter. """ - return html_table(self) + try: + from matrepr import to_html + from matrepr.adapters.sparse_driver import PyDataSparseDriver + + return to_html(PyDataSparseDriver.adapt(self), notebook=True) + except ImportError: + return html_table(self) + + def _str_impl(self, summary): + """ + A human-readable representation of this array, including a metadata summary + and a tabular view of the array values. + + Values view only included if `matrepr` is available. + + Parameters + ---------- + summary + A type-specific summary of this array, used as the first line of return value. + + Returns + ------- + str + A human-readable representation of this array. + """ + try: + from matrepr import to_str + from matrepr.adapters.sparse_driver import PyDataSparseDriver + + values = to_str( + PyDataSparseDriver.adapt(self), + title=False, # disable matrepr description + width_str=0, # autodetect terminal width + max_cols=9999, + ) + return "\n".join([summary, values]) + except ImportError: + return summary @abstractmethod def asformat(self, format):