Skip to content

Fill in non-finite plot values for Qt versions >= 5.12.3#1287

Merged
j9ac9k merged 2 commits intopyqtgraph:masterfrom
campagnola:fix-nan-plot
Nov 23, 2020
Merged

Fill in non-finite plot values for Qt versions >= 5.12.3#1287
j9ac9k merged 2 commits intopyqtgraph:masterfrom
campagnola:fix-nan-plot

Conversation

@campagnola
Copy link
Copy Markdown
Member

Fixes #1057

I have only manually tested this for a few cases; it would be nice to have some test coverage here before merging.

@campagnola
Copy link
Copy Markdown
Member Author

Wow, ok.. turns out this has been attempted before in #1058 and #1286.

Comment on lines +1513 to +1514
idx[:first] = first
arr[1:-1] = arr[1:-1][idx]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
idx[:first] = first
arr[1:-1] = arr[1:-1][idx]
if first < len(x):
# Replace all non-finite entries from beginning of arr with the first finite one
idx[:first] = first
arr[1:-1] = arr[1:-1][idx]

Fixes the case where the whole buffer contains non-finite items in x or y (or both).

@abrauchli
Copy link
Copy Markdown

I'm still hitting a case where everything just freezes with my all Y values being NaN buffer, so I can't really validate the code.. I'm not sure if it's related to the arr[2:]['c'] = isfinite which was patched in b61c7c1 .. will have to investigate more. Running on Py3.8 with PySide2 5.15.1 with pyqtgraph 0.11.0 + this patch and the mentioned commit.

Also, could it be that with the fill-in solution, that for time-series, where the X-axis is generally the (finite) timestamp, filling-in values will overwrite the timestamps of the points that aren't rendered and thus cause auto-scaling to behave differently since the NaN-cleaned version of the buffer will have a different starting point?
To solve that, I suppose we'd have to clean-up only the non-finite Y value with something like the code below. Since the NaN-turned-zero values are non-connect they shouldn't appear.

    if qtver >= [5, 12, 3]:
        isfinite = np.isfinite(x) & np.isfinite(y)
        isfinite_x = np.isfinite(x)
        isfinite_y = np.isfinite(y)
        if not np.all(isfinite_x):
            # credit: Divakar https://stackoverflow.com/a/41191127/643629
            mask = ~isfinite_x
            idx = np.arange(len(x))
            idx[mask] = -1
            np.maximum.accumulate(idx, out=idx)
            first = np.searchsorted(idx, 0)
            if first < len(x):
                # Replace all non-finite X-axis points from beginning of arr with the first finite one
                idx[:first] = first # np.max(first, len(x) - 1)
                arr[1:-1] = arr[1:-1][idx]
        if not np.all(isfinite_y):
            mask = ~isfinite_y
            # Replace all non-finite Y-axis points with 0
            arr[1:-1]['y'][mask] = 0

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.

plots not showing with NaN

3 participants