Repository: pyqtgraph/pyqtgraph

Issue:
GLVolumeItem example rendered image changed
<!-- In the following, please describe your issue in detail! -->
<!-- If some of the sections do not apply, just remove them. -->

### Short description
<!-- This should summarize the issue. -->
c4a1cf11a166201f3da1b99705d7cb8acea0d602 from PR #1724 changed significantly the rendered output of the hydrogen electron probability density function in GLVolumeItem.py

### Code to reproduce
<!-- Please provide a minimal working example that reproduces the issue in the code block below.
     Ideally, this should be a full example someone else could run without additional setup. -->
examples/GLVolumeItem.py

### Expected behavior
<!-- What should happen? -->

![old_good](https://user-images.githubusercontent.com/2657027/118205751-a9639500-b493-11eb-8666-bcfb0b625ebf.png)

### Real behavior
<!-- What happens? -->
![new_bad](https://user-images.githubusercontent.com/2657027/118205766-b2546680-b493-11eb-88e2-8f61083eb5ce.png)

### Tested environment(s)

 * PyQtGraph version:  master<!-- output of pyqtgraph.__version__ -->
 * Qt Python binding: PySide6 6.1.0 <!-- output of pyqtgraph.Qt.VERSION_INFO -->
 * Python version: 3.8.10
 * NumPy version: 1.20.3 <!-- output of numpy.__version__ -->
 * Operating system: Windows 10
 * Installation method: pip install -e . <!-- e.g. pip, conda, system packages, ... -->

### Additional context



Discussion:
Apparently, the earlier example code deliberately accepts 
"RuntimeWarning: divide by zero encountered in log"
to generate the desired plotting data. The epsilon offset added in #1724 avoids this, but seems to generate unwanted data where there were probably NaN values before.

Editing the lines in the example (somewhere around L42) to read
  `positive = np.log(fn.clip_array(data, 0, data.max())**2)`
  `negative = np.log(fn.clip_array(-data, 0, -data.min())**2)`
seems to bring back the previous image.

@pijyoi, if you want to turn that into a PR, go ahead. I probably won't get around to it for a few days, sorry.
>but seems to generate unwanted data where there were probably NaN values before.

There are no NaN values, only -Infs.
There is at least a reliance on the side-effect that casting non-finite values to uint8 results in 0.
This would suppress the warning locally:
```python
with np.errstate(divide = 'ignore'):
    positive = np.log(fn.clip_array(data, 0, data.max())**2)
    negative = np.log(fn.clip_array(-data, 0, -data.min())**2)
```
> This would suppress the warning locally:
> 
> ```python
> with np.errstate(divide = 'ignore'):
>     positive = np.log(fn.clip_array(data, 0, data.max())**2)
>     negative = np.log(fn.clip_array(-data, 0, -data.min())**2)
> ```

that would certainly be the way to do it.  I can submit the fix for this, given that I broke it...

[anytime someone posts a git commit hash on a new issue](https://en.meming.world/images/en/1/17/Monkey_Puppet.jpg)

![](https://en.meming.world/images/en/1/17/Monkey_Puppet.jpg)



Resolving patch:
diff --git a/examples/GLVolumeItem.py b/examples/GLVolumeItem.py
--- a/examples/GLVolumeItem.py
+++ b/examples/GLVolumeItem.py
@@ -18,8 +18,6 @@
 w.show()
 w.setWindowTitle('pyqtgraph example: GLVolumeItem')
 
-#b = gl.GLBoxItem()
-#w.addItem(b)
 g = gl.GLGridItem()
 g.scale(10, 10, 1)
 w.addItem(g)
@@ -32,13 +30,20 @@ def psi(i, j, k, offset=(50,50,100)):
     th = np.arctan2(z, np.hypot(x, y))
     r = np.sqrt(x**2 + y**2 + z **2)
     a0 = 2
-    ps = (1./81.) * 1./(6.*np.pi)**0.5 * (1./a0)**(3/2) * (r/a0)**2 * np.exp(-r/(3*a0)) * (3 * np.cos(th)**2 - 1)
-    return ps
+    return (
+        (1.0 / 81.0)
+        * 1.0 / (6.0 * np.pi) ** 0.5
+        * (1.0 / a0) ** (3 / 2)
+        * (r / a0) ** 2
+        * np.exp(-r / (3 * a0))
+        * (3 * np.cos(th) ** 2 - 1)
+    )
 
 
 data = np.fromfunction(psi, (100,100,200))
-positive = np.log(fn.clip_array(data, np.finfo(data.dtype).eps, data.max())**2)
-negative = np.log(fn.clip_array(-data, -np.finfo(data.dtype).eps, -data.min())**2)
+with np.errstate(divide = 'ignore'):
+    positive = np.log(fn.clip_array(data, 0, data.max())**2)
+    negative = np.log(fn.clip_array(-data, 0, -data.min())**2)
 
 d2 = np.empty(data.shape + (4,), dtype=np.ubyte)
 d2[..., 0] = positive * (255./positive.max())


Test patch: