10000 Use blobs instead of random integers by Czaki · Pull Request #6527 · napari/napari · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use blobs instead of random integers #6527

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

Merged
merged 22 commits into from
Jan 3, 2024
Merged

Conversation

Czaki
Copy link
Collaborator
@Czaki Czaki commented Dec 11, 2023

Description

During investigation of scikit-image/scikit-image#7262 I have created a blobs function that is quite fast and produces data more realistic than just np.random.randint.

So I suggest adopting it in benchmarks.

It is slightly faster on my machine

main:

[50.00%] ·· ======================================================== ================
                                   benchmark                          total duration
            -------------------------------------------------------- ----------------
             benchmark_labels_layer.LabelsDrawing2DSuite.time_draw        33.9s
                 benchmark_labels_layer.Labels3DSuite.mem_layer           16.7s
             benchmark_labels_layer.Labels3DSuite.time_create_layer       16.5s
               benchmark_labels_layer.Labels3DSuite.time_refresh          16.5s
                 benchmark_labels_layer.Labels3DSuite.mem_data            16.5s
                                      ...                                  ...
                                     total                                6.92m
            ======================================================== ================           

This branch:

[50.00%] ·· ============================================================ ================
                                     benchmark                            total duration
            ------------------------------------------------------------ ----------------
               benchmark_labels_layer.LabelsDrawing2DSuite.time_draw          33.6s
                   benchmark_labels_layer.Labels2DSuite.time_fill             20.4s
             benchmark_labels_layer.Labels2DColorDirectSuite.time_fill        18.1s
               benchmark_labels_layer.Labels3DSuite.time_create_layer         13.5s
             benchmark_labels_layer.Labels3DSuite.time_raw_to_displayed       13.4s
                                        ...                                    ...
                                       total                                  6.56m
            ============================================================ ================

It also adds Balls 2D and Balls 3D example data with importing from napari.examples.utils

@Czaki Czaki added the run-benchmarks Add this label to trigger a full benchmark run in a PR label Dec 11, 2023
@Czaki Czaki added this to the 0.5.0 milestone Dec 11, 2023
@Czaki Czaki added the maintenance PR with maintance changes, label Dec 11, 2023
Copy link
codecov bot commented Dec 11, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (7810736) 92.26% compared to head (8cc86a9) 92.21%.
Report is 17 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6527      +/-   ##
==========================================
- Coverage   92.26%   92.21%   -0.06%     
==========================================
  Files         601      603       +2     
  Lines       53427    53766     +339     
==========================================
+ Hits        49294    49579     +285     
- Misses       4133     4187      +54     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member
@psobolewskiPhD psobolewskiPhD left a comment

Choose a reason for hiding this comment

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

gen_blobs seems generally useful as an alternative to skimage binary_blobs.
In fact I really like it! I think it could be used to generate nice sample data using say np.uint8 as an alternative to binary blobs.
Is it worth moving to a more general location in the code base?

@Czaki
Copy link
Collaborator Author
Czaki commented Dec 26, 2023

Is it worth moving to a more general location in the code base?

yes and no

It may be a useful example, but without cherry-picking this to 0.4.19, we need to use only utils.py content in benchmark to be able to compare main against the latest release.

@psobolewskiPhD
Copy link
Member

Ok. Maybe we make it a sample or something in 0.5.0. Can make a task issue once merged.

@Czaki
Copy link
Collaborator Author
Czaki commented Dec 26, 2023

Ok. Maybe we make it a sample or something in 0.5.0. Can make a task issue once merged.

We could make it sample now. But it will require to have two copies of code. Or we agree on cherry pick it to 0.4.19.

Copy link
Member
@jni jni left a comment

Choose a reason for hiding this comment

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

I like this function. I've left some suggestions for the API. I also suggest that sizes should be configurable.



@lru_cache
def gen_blobs(shape, dtype, blob_count=144):
Copy link
Member

Choose a reason for hiding this comment

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

We definitely need a docstring, including the limitation that this is 2D or 3D only. Personally, I feel like a huge value-add would be to make this n-dimensional, for example with 3D blobs moving about in the added dimensions with brownian motion. That could come in a later PR but just thinking out loud about the kinds of limitations I find when working with skimage binary blobs.

I would also suggest a more descriptive name. At a minimum, generate_blobs, but, perhaps better, something like labeled_particles?

Also, I think everything other than shape should be a kwarg, dtype should be optional and default to the smallest dtype for the given number of blobs. I would also rename blob_count to just n=.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see below that the values are obtained from the dtype. That's also ok but then there should be a default dtype.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could you check now?

@psobolewskiPhD
Copy link
Member

The samples are amazing, especially with points and labels!
😍
I think that following the existing Samples, these should be after astronaught as Balls and Balls (3D)

BTW The 3D one has some issue with background in mip though:
image

Some locations arn't set a value perhaps? or are those just the only 0 values...

@Czaki
Copy link
Collaborator Author

BTW The 3D one has some issue with background in mip though:

It is not an issue. The background is visible only for 0 value.
I have improved it a little.

@psobolewskiPhD psobolewskiPhD added enhancement highlight PR that should be mentioned in next release notes labels Dec 28, 2023
Co-authored-by: Juan Nunez-Iglesias <jni@fastmail.com>
Comment on lines 58 to 60
"""
helper function to update data with structure at given coordinates
"""
Copy link
Member

Choose a reason for hiding this comment

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

What is structure? What is assign_operator? Can you write a complete docstring here? Even though it's private, it will help others understand the code in the future.

Copy link
Collaborator Au 8000 thor

Choose a reason for hiding this comment

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

added

Copy link
Member
@jni jni left a comment

Choose a reason for hiding this comment

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

@Czaki I've pushed a bunch of changes including a significant refactor. I think the code is cleaner (for example, it uses standard numpy reductions such as np.maximum), and it is a bit faster (about 10%) too.

I have also made the seed part of the sample layer metadata so that people could reproduce a specific configuration if needed.

@jni jni added the ready to merge Last chance for comments! Will be merged in ~24h label Jan 2, 2024
@github-actions github-actions bot added the tests Something related to our tests label Jan 2, 2024
@Czaki
Copy link
Collaborator Author
Czaki commented Jan 2, 2024

I have added tests

@Czaki
Copy link
Collaborator Author
Czaki commented Jan 2, 2024

And found that the code was not python 3.8 compatible.

@jni
Copy link
Member
jni commented Jan 3, 2024

@Czaki this is for 0.5.0 so 3.8 compatibility is not important?

@jni
Copy link
Member
jni commented Jan 3, 2024

I see that the CI still uses 3.8, I guess because we wanted to keep it until 0.4.19. I'm a little shocked that the walrus operator there is reported as a syntax error, since it was introduced in 3.8? Is it because it's not allowed in square brackets due to implementation details?

@Czaki
Copy link
Collaborator Author
Czaki commented Jan 3, 2024

It looks like this.

@jni jni merged commit 55d1a21 into napari:main Jan 3, 2024
@jni jni deleted the better_performance branch January 3, 2024 05:48
kne42 added a commit to kne42/napari that referenced this pull request Jan 4, 2024
* main:
  Use blobs instead of random integers (napari#6527)
  Set default dtype for empty `_ImageSliceResponse` (napari#6552)
  Add creating image from clipboard (napari#6532)
  Fix check if plugin is available from conda (napari#6545)
  Fix generation of layer creation functions docstrings (napari#6558)
  Print a whole stack if throttler is not finished (napari#6549)
  Update `app-model`, `babel`, `coverage`, `dask`, `fsspec`, `hypothesis`, `imageio`, `ipython`, `lxml`, `magicgui`, `pandas`, `pint`, `psutil`, `pydantic`, `pyqt6`, `pytest-qt`, `tensorstore`, `tifffile`, `virtualenv`, `xarray` (napari#6478)
  Minimal changes for mlx (and jax) compatibility for Image layers (napari#6553)
  [pre-commit.ci] pre-commit autoupdate (napari#6528)
  Moving IntensityVisualizationMixin from _ImageBase to Image (napari#6548)
@Czaki Czaki removed the ready to merge Last chance for comments! Will be merged in ~24h label Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement highlight PR that should be mentioned in next release notes maintenance PR with maintance changes, run-benchmarks Add this label to trigger a full benchmark run in a PR tests Something related to our tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0