Chris-N-K / napari-time-series-plotter

A napari Plugin for visualisaton of pixel values over time (t+ nD) as graphs.
BSD 3-Clause "New" or "Revised" License
10 stars 5 forks source link

Drops in playback performance when loading napari-TSP #25

Open rjlopez2 opened 1 year ago

rjlopez2 commented 1 year ago

Hi Chris,

I have seen a slightly decrease in performance for rendering some relatively modest size timestacks in napari when using your pluging. I have been thinking for some time now how to address this problem but may be you could give me a better insight on this and may be take me to the right track on how to fix this.

Here is a way to reproduce the issue:

  1. create a clean test environment
conda create -n test_env python=3.9
conda activate test_env
python3 -m pip install "napari[all]"
pip install napari-time-series-plotter
  1. Try to add an image (approximately the same size of our use case images) directly in the napari interpreter or a Jupyter-notebook
from numpy.random import default_rng

rng = default_rng()

data = rng.standard_normal((500, 500, 1500))

viewer.add_image(data)
  1. set the playback configuration to 60 FPS (you should be able to see a responsive and no lagging playback)

image

  1. Load your plugin

  2. See the increased latency in the playback of the current image

  3. close your plugin

  4. playback speed comes back to normal

Do you have an idea what could be the reason of this drops in performance when I load your plugin?

best

Ruben

Chris-N-K commented 1 year ago

I think I can explain the hit to performance. Every time the slice changes a new draw event is triggered, as the plugin draws the current z slice. The current implementation of the draw function is relatively performance heavy as the whole canvas is cleared and redrawn. Matplotlib is not perfect for such an application.

I already have an improved version of the drawing machinery but it will still be somewhat resource heavy when combining it with the playback function. In the current version the data acquisition and the drawing is one function, in the new version they are separate. This might help.

I might have some time this week and will run some tests with the new functions.

Thanks for reporting. :) Anything else you noticed?

rjlopez2 commented 1 year ago

thanks for the fast reply!

Every time the slice changes a new draw event is triggered, as the plugin draws the current z slice. The current implementation of the draw function is relatively performance heavy as the whole canvas is cleared and redrawn. Matplotlib is not perfect for such an application.

on this, I don't know if this tips mentioned here by Juan-Nunez might be of interest.

I was also thinking on using/implemented some other recommendations from the Napari documentation such as multithreading those task that are computational consuming and are independent of the main thread e.g average a ROI. Do you think that that make sense?

Anyway, from now that's all. I look forward to see your results and check out the version you are working on.

Cheers

Ruben

Chris-N-K commented 1 year ago

@rjlopez2 I can reproduce your observations. The fps drop if the plugin is docked, it is not necessary to select a layer and actually plot. I think I have to review which event triggers the plotting. Seems like it causes redraws in situations when it shouldn't. Big thanks for reporting this.

Thanks for the pointer to Juan-Nunez's post, that's exactly the thing I try in the new implementation.

  1. Only draw plots anew if they change
  2. Try to keep the objects and only update them

It's somewhat tricky to keep track with as small an overhead as possible, but I think it will work. At the moment the changes are only in my local repository, will probably push them the moment the first draft works properly.

Chris-N-K commented 9 months ago

Hi @rjlopez2 , I fixed in my rework of the plugin, the playback performance is no longer affected at all. I will close this issue as soon as the changes go live.

rjlopez2 commented 9 months ago

Thanks @Chris-N-K! I look forward to check this out and thanks for your work. Cheers