Repository: dask/dask

Issue:
Hide nbytes in HTML repr with sparse blocks?
The `nbytes` reported by the HTML array repr is incorrect with we have sparse blocks inside an array.

This issue was discussed in https://github.com/dask/dask/issues/5313. In general, knowing the `.nbytes` correctly isn't possible with sparse blocks. I think we should not include the memory usage when the `_meta` is sparse.

<table>
<tr>
<td>
<table>
  <thead>
    <tr><td> </td><th> Array </th><th> Chunk </th></tr>
  </thead>
  <tbody>
    <tr><th> Bytes </th><td> 800 B </td> <td> 800 B </td></tr>
    <tr><th> Shape </th><td> (10, 10) </td> <td> (10, 10) </td></tr>
    <tr><th> Count </th><td> 2 Tasks </td><td> 1 Chunks </td></tr>
    <tr><th> Type </th><td> float64 </td><td> sparse.COO </td></tr>
  </tbody>
</table>
</td>
<td>
<svg width="170" height="170" style="stroke:rgb(0,0,0);stroke-width:1" >

  <!-- Horizontal lines -->
  <line x1="0" y1="0" x2="120" y2="0" style="stroke-width:2" />
  <line x1="0" y1="120" x2="120" y2="120" style="stroke-width:2" />

  <!-- Vertical lines -->
  <line x1="0" y1="0" x2="0" y2="120" style="stroke-width:2" />
  <line x1="120" y1="0" x2="120" y2="120" style="stroke-width:2" />

  <!-- Colored Rectangle -->
  <polygon points="0.000000,0.000000 120.000000,0.000000 120.000000,120.000000 0.000000,120.000000" style="fill:#ECB172A0;stroke-width:0"/>

  <!-- Text -->
  <text x="60.000000" y="140.000000" font-size="1.0rem" font-weight="100" text-anchor="middle" >10</text>
  <text x="140.000000" y="60.000000" font-size="1.0rem" font-weight="100" text-anchor="middle" transform="rotate(0,140.000000,60.000000)">10</text>
</svg>
</td>
</tr>
</table>




Discussion:
This sounds good to me.  What would the check here be, `"sparse" in `typename(x)`?
That seems like a reasonable check. We should also use `dask.utils.typename` in the HTML repr, I don't think we currently are

https://github.com/dask/dask/blob/0871699c0e4b021fca4a3104042d1adf7c5fd5d5/dask/array/core.py#L1269-L1270

Resolving patch:
diff --git a/dask/array/core.py b/dask/array/core.py
--- a/dask/array/core.py
+++ b/dask/array/core.py
@@ -54,6 +54,7 @@
     M,
     ndimlist,
     format_bytes,
+    typename,
 )
 from ..core import quote
 from ..delayed import delayed, Delayed
@@ -1244,7 +1245,10 @@ def _repr_html_(self):
         return "\n".join(both)
 
     def _repr_html_table(self):
-        if not math.isnan(self.nbytes):
+        if "sparse" in typename(type(self._meta)):
+            nbytes = None
+            cbytes = None
+        elif not math.isnan(self.nbytes):
             nbytes = format_bytes(self.nbytes)
             cbytes = format_bytes(np.prod(self.chunksize) * self.dtype.itemsize)
         else:
@@ -1258,7 +1262,9 @@ def _repr_html_table(self):
             "  </thead>",
             "  <tbody>",
             "    <tr><th> Bytes </th><td> %s </td> <td> %s </td></tr>"
-            % (nbytes, cbytes),
+            % (nbytes, cbytes)
+            if nbytes is not None
+            else "",
             "    <tr><th> Shape </th><td> %s </td> <td> %s </td></tr>"
             % (str(self.shape), str(self.chunksize)),
             "    <tr><th> Count </th><td> %d Tasks </td><td> %d Chunks </td></tr>"


Test patch:
diff --git a/dask/array/tests/test_sparse.py b/dask/array/tests/test_sparse.py
--- a/dask/array/tests/test_sparse.py
+++ b/dask/array/tests/test_sparse.py
@@ -184,6 +184,7 @@ def test_html_repr():
 
     assert "COO" in text
     assert "sparse" in text
+    assert "Bytes" not in text
 
 
 def test_from_delayed_meta():