Skip to content

pcmi: refactor setData()#2876

Merged
j9ac9k merged 5 commits intopyqtgraph:masterfrom
pijyoi:pcmi-refactor-setdata
Nov 18, 2023
Merged

pcmi: refactor setData()#2876
j9ac9k merged 5 commits intopyqtgraph:masterfrom
pijyoi:pcmi-refactor-setdata

Conversation

@pijyoi
Copy link
Copy Markdown
Contributor

@pijyoi pijyoi commented Nov 11, 2023

PColorMeshItem::setData() was a giant function that was also being called by setLevels() and setLookupTable().

This PR refactors setData() to _rerender() and _drawPicture() while removing _updateDisplayWithCurrentState()

_rerender() is for redrawing when only levels or colors have changed.
i.e. data bounds have not changed.
i.e. after a call to setLevels() or setLookupTable().

may fix #2875

It is not clear to me why prepareGeometryChange() is being called unconditionally, but care is taken to call informViewBoundsChanged() only if bounds have been detected to have changed.

self.prepareGeometryChange()
if boundsChanged:
      self.informViewBoundsChanged()

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Nov 11, 2023

If memory serves the prepareGeometryChange call is left over from a debug effort of when PColorMesh item was first being introduced but there were visible artifacts left over when the mesh changed in some way. It may indeed not be needed.

@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Nov 11, 2023

If memory serves the prepareGeometryChange call is left over from a debug effort of when PColorMesh item was first being introduced but there were visible artifacts left over when the mesh changed in some way. It may indeed not be needed.

I believe both calls are needed, but both should either be called together, or both not called.

@pijyoi pijyoi marked this pull request as ready for review November 11, 2023 09:16
@pijyoi pijyoi marked this pull request as draft November 11, 2023 21:38
@pijyoi pijyoi force-pushed the pcmi-refactor-setdata branch from 18c96a3 to 50bbc75 Compare November 11, 2023 21:48
@pijyoi pijyoi marked this pull request as ready for review November 11, 2023 21:50
@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Nov 13, 2023

It seems rather unsatisfactory to me that there should be an enableAutoLevels state and yet the user is allowed to disable it on a case-by-case basis in a call to setData().

Furthermore, one can only override an enabled state to disabled, but not the other way round.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Nov 15, 2023

It seems rather unsatisfactory to me that there should be an enableAutoLevels state and yet the user is allowed to disable it on a case-by-case basis in a call to setData().

Furthermore, one can only override an enabled state to disabled, but not the other way round.

I agree. That doesn't sound ideal.

@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Nov 16, 2023

Hello @SimenZhor, I am doing some refactoring of PCMI in #2879 (merged) and this PR, which would possibly modify the changes introduced in #2463.

What is the use case for having both an overall enableAutoLevels state and a per-data-update autoLevels setting?
The PCMI example exercises only the overall enableAutoLevels state setting.

I would propose to remove the per-data-update autoLevels setting.

@SimenZhor
Copy link
Copy Markdown
Contributor

Hi @pijyoi , thanks for taking care of the PColorMeshItem!
I don't remember the exact reason behind keeping both enableAutoLevels and autoLevels. But reading back the comments in my PR it looks like I somewhat reluctantly implemented autoLevels to keep the API consistent with ImageItem after feedback provided in code review. See this comment for more info: #2463 (comment)

I don't have an issue with changing this behavior.

@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Nov 16, 2023

If the per-update autoLevels was a requested addition, then instead of removing it, I would propose to change its behavior to be tri-state.
If:

  1. not set (i.e. equal to None), then defer to the overall enableAutoLevels state
  2. set (to either True / False), then autoLevels over-rides enableAutoLevels.

@SimenZhor
Copy link
Copy Markdown
Contributor

It seems to me like we all agree that this part of the API is not intuitive or easy to understand. Since it was not clear to you why this solution was implemented, and since this confusion arrived so quickly after my PR, I'm inclined to take that as a sign that the API needs an overhaul (both for PColorMeshItem and ImageItem). Maybe @NilsNemitz wants to weigh in here, since he requested that I implement autoLevels in the first place?

If a breaking API change is not desired, I think your tri-state alternative sounds reasonable.

previously, autoLevels=True would not override enableautolevels=False.
the docs don't match the code.
what the code does:
    if levels is None, auto-scaling occurs
    _and_ levels will be automatically set.
    i.e. in the next cycle, levels will no
    longer be None
@pijyoi pijyoi force-pushed the pcmi-refactor-setdata branch from 50bbc75 to a5e6f71 Compare November 16, 2023 11:50
@NilsNemitz
Copy link
Copy Markdown
Contributor

NilsNemitz commented Nov 16, 2023

I certainly won't oppose a nicely thought out improvement to the API.

The autoLevels parameter exists for compatibility with the setImage method of ImageItem, which is functionally very similar.

But having to call that at every update is inconvenient, so enableAutoLevels serves to set a persistent state.

If given, autoLevels should (for this call only) override the persistent state. If not given, the persistent state should be used. I would consider it a bug that it doesn't work like that right now - the "tristate" logic looks more correct. I think we simply didn't work that out properly in the previous PR. (And I didn't check later on, my apologies!)

I think we should add the same enableAutoLevels logic to ImageItem, too. This sets one additional constraint:
If we make enableAutoLevels default to True, then we'll have

  • a consistent data setting API across ImageItem and PColorMeshItem
  • a convenient persistent setting
  • backwards compatibility in ImageItem

@NilsNemitz
Copy link
Copy Markdown
Contributor

The naming of enableAutoLevels parallels enableAutoRange inViewbox, which I believe has a similar function and functionality for axis scaling.
https://pyqtgraph.readthedocs.io/en/latest/api_reference/graphicsItems/viewbox.html#pyqtgraph.ViewBox.enableAutoRange

I am not saying it cannot be simplified, but that was the reasoning in the previous discusion.

@pijyoi pijyoi force-pushed the pcmi-refactor-setdata branch 2 times, most recently from 5bbd9b2 to 6f812f0 Compare November 18, 2023 02:29
@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Nov 18, 2023

changes to ImageItem have been moved to #2883

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Nov 18, 2023

I know enableAutoLevels and disableAutoLevels pre-date this PR, but looking at this PR I'm wondering why we just didn't do it like any other setter/getter attribute with setAutoLeveling() but that's outside the scope of this PR 👍🏻

Thanks @pijyoi this LGTM, merging.

@j9ac9k j9ac9k merged commit 818583d into pyqtgraph:master Nov 18, 2023
@pijyoi pijyoi deleted the pcmi-refactor-setdata branch November 18, 2023 21:47
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.

pg.PColorMeshItem crashes when x, y array shapes are changed (setData(x, y, z...)).

4 participants