Skip to content

Handle NaN in display devices#17

Draft
D4vidH4mm3r wants to merge 9 commits intophilsmt:fix/forward-compatfrom
D4vidH4mm3r:fix/display-handle-nan
Draft

Handle NaN in display devices#17
D4vidH4mm3r wants to merge 9 commits intophilsmt:fix/forward-compatfrom
D4vidH4mm3r:fix/display-handle-nan

Conversation

@D4vidH4mm3r
Copy link

As I'm not very familiar with the code base yet, I'll just post this to record which parts I've gotten to so far. In any plot, one should decide between replacing nan with some value or dropping it. Same goes for +-infinity values.

Have so far looked at:

  • fast_plot
    • ndarray with ndim==1
    • ndarray with ndim>1
    • ndarray with ndim>1 and self.with_x
    • xr.DataArray
  • hist1d
  • hist2d
  • image
    • Already just works (on my machine)
  • plot
  • polar_plot
  • sorted
  • value
  • waveform

Developing against fix/forward-compat as that allowed me to install locally for testing, but can (rebase and) switch to point to master later.

Comment on lines +1238 to +1242
nan_replacement = 0
if isinstance(self.ch_data, xr.DataArray):
self.idx_data = self.ch_data.data[self.index]
if "replace_nan" in self.ch_data.attrs:
nan_replacement = self.ch_data.attrs["replace_nan"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One could also allow specifying and utilizing the various dropna / fillna etc. available for DataArray. They don't handle inf, though. Also, dropping is tricky.

Comment on lines 1274 to 1276
self.idx_data = np.nan_to_num(self.idx_data, copy=True,
nan=nan_replacement)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: would like to avoid copy.
Cannot replace in-place as destination is not writable.
Ideally, would only copy if any value needs replacing - but first checking that and then copying doesn't seem clever either.

Also: I considered if we could drop instead of replacing.
However, if self.idx_data.shape[0] > 1, then we cannot do a simple 1d mask and np.compress on x and ys.
And stacking seems tricky.
Unless we drop by logical OR across axis=0.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, this is likely due to the no-copy mechanism used with pyzmq. Generally though, I'd also like to avoid encoding metropc-specific details in the display devices.

We could either implement it directly in the rendering code, replacing NaN by something else, or move this specific into the euxfel.metropc device. Another advantage of the rendering code would be that we can actually render gaps rather than replace it. And one could present np.inf by an infinite slope before and after.

Comment on lines +240 to +242
if not numpy.isfinite(d):
return

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it's easy to just drop a given value. The plot was almost working as-is: NaN values would cause an exception and just not get plotted, so there would be gap. The curve would break, though.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to fast_plot, I wonder if it would be useful to visualize this by a gap?

@philsmt philsmt force-pushed the fix/forward-compat branch from 6aef942 to 1e250a9 Compare January 8, 2025 07:52
This was just part of preparation before adding clipping to draw_line, but I got
significant performance boost from, presumably, the noexcept. We can drop the C++ part
if it's not wanted; I just found references easier to work with (syntactically) than
more pointers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants