Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip plotting line data if it's all NaNs #802

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

SolarDrew
Copy link
Contributor

@SolarDrew SolarDrew commented Jan 7, 2025

Closes DKISTDC/dkist#477

Over in the DKIST python tools I discovered that trying to line plot a slice of a dataset took many times longer if there was no actual data there. This skips the attempt to actually plot a line full of NaNs which should solve that and cause no other problems anywhere, probably.

@SolarDrew
Copy link
Contributor Author

SolarDrew commented Jan 7, 2025

This will break if the data aren't a dask array, so that's not ideal.

@nabobalis nabobalis marked this pull request as draft January 8, 2025 00:27
@SolarDrew SolarDrew marked this pull request as ready for review January 8, 2025 11:26
@@ -145,7 +145,9 @@ def _plot_1D_cube(self, wcs, axes=None, axes_coordinates=None, axes_units=None,
# We plot against pixel coordinates
axes.errorbar(np.arange(len(ydata)), ydata, yerr=yerror, **kwargs)
else:
axes.plot(ydata, **kwargs)
# compute() will break on numpy arrays to catch dask arrays by casting to bool instead
Copy link
Member

Choose a reason for hiding this comment

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

This comment should make more sense to those who aren't dask wonks ;)

Copy link
Member

@DanRyanIrish DanRyanIrish left a comment

Choose a reason for hiding this comment

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

Does an equivalent check need to be done for plotting a 2D image?

@@ -0,0 +1 @@
Skip plotting line data if all values are NaN
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Skip plotting line data if all values are NaN
Make `~ndcube.visualization.mpl_plotter.MatplotlibPlotter` only add data to line plots if at least one value of the data is unmasked and finite.

@@ -145,7 +145,9 @@ def _plot_1D_cube(self, wcs, axes=None, axes_coordinates=None, axes_units=None,
# We plot against pixel coordinates
axes.errorbar(np.arange(len(ydata)), ydata, yerr=yerror, **kwargs)
else:
axes.plot(ydata, **kwargs)
# compute() will break on numpy arrays to catch dask arrays by casting to bool instead
if not bool(np.isnan(ydata).all()):
Copy link
Member

Choose a reason for hiding this comment

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

Could something like

Suggested change
if not bool(np.isnan(ydata).all()):
if np.isfinite(ydata).any() and unmasked:

be used instead without causing dask problems? This way both NaN and Inf would be checked. In addition, it would check whether there are valid unmasked data because unmasked would be a bool that would indicate that at least one finite data value is unmasked and default to True. It would be calculated around here.

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.

Plotting a 1D dataset takes much longer without the data
2 participants