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

Add Fourier transform playground example #5872

Merged
merged 12 commits into from
Jun 6, 2023

Conversation

brisvag
Copy link
Contributor

@brisvag brisvag commented May 25, 2023

Description

Adds a new example to play around with 2D sine waves and fourier transforms. I made it to better visualise and explain how power spectrum and phase behave depending on the fourier components.

The example is also a nice showcase of two-way threading communication :P

Here's what it looks like:

ft_playground.mp4

@stefanv
Copy link
Contributor

stefanv commented May 26, 2023

Liking this very much!!

FFT visualization often benefits from taking the log, to avoid large changes in amplitude. Something like np.log10(c + abs(spectrum)) and then playing with the value of c, given the application.

@jni jni changed the title add fourer transform playground Add Fourier transform playground example May 26, 2023
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 have to say I was very confused about where the animation was coming from... Then I saw the sneaky time() call way inside wave_2d! That'll learn me to read docstrings 😂 Personally, I would prefer for wave_2d to be purely functional, take in the current time, and the higher-level loop (update_viewer) to pass in the time. But I wouldn't hold up the PR for it.

At any rate, I played with it, and it's so mesmerising.... I love it. 😍 😵‍💫 😃

examples/fourier_transform_playground.py Outdated Show resolved Hide resolved
examples/fourier_transform_playground.py Outdated Show resolved Hide resolved
examples/fourier_transform_playground.py Outdated Show resolved Hide resolved
examples/fourier_transform_playground.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented May 26, 2023

Codecov Report

Merging #5872 (255d828) into main (48626e8) will increase coverage by 0.02%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main    #5872      +/-   ##
==========================================
+ Coverage   89.99%   90.01%   +0.02%     
==========================================
  Files         618      618              
  Lines       52699    52831     +132     
==========================================
+ Hits        47424    47556     +132     
  Misses       5275     5275              

see 17 files with indirect coverage changes

@brisvag
Copy link
Contributor Author

brisvag commented May 26, 2023

FFT visualization often benefits from taking the log, to avoid large changes in amplitude. Something like np.log10(c + abs(spectrum)) and then playing with the value of c, given the application.

Nice! I settled on c = 10 for this application, it improves the readability of the phase.

Speaking of which, I fixed the phase, since I was doing it wrong before: I was taking only the imaginary part instead of getting the angle of the complex number.

I also moved out the time component from the base function, which I agree makes things cleaner. Should be good to go!

@GenevieveBuckley
Copy link
Contributor

This is great!

It reminds me very strongly of Grant Jensen's image Fourier transform talk, which is part of his incredible Cryo-EM lecture series (seriously you should go look, it's brilliant and there are several other fourier transform videos in that series as well as the one I linked above)

Can we add links to those in the text description as well? I think that would really help boost people's understanding.

@jni
Copy link
Member

jni commented May 29, 2023

The docs build is failing, I presume because some of the threading / interactive examples need to be excluded from running in the gallery. (But tbh I don't know, the fail log doesn't offer much of a clue...) I can't remember how this works... @melissawm any ideas? 😬 🙏

@brisvag
Copy link
Contributor Author

brisvag commented May 30, 2023

It reminds me very strongly of Grant Jensen's image Fourier transform talk, which is part of his incredible Cryo-EM lecture series

Yeah, that's part of the go-to lecture series we always suggest to newbies (me included :P) here :) Being able to interact with it is so much better though. Explaining aliasing by moving a slider and seing the peaks in the power spectrum "bounce" on the edges is so satisfying :)

@melissawm
Copy link
Member

I tested locally and couldn't find any issues (docs build just fine), but I restarted the CI job yesterday and was watching - it seems like the examples grow progressively slower after this new one, maybe something is leaking? Not sure how to debug locally as I don't see any issues but will try to poke around. Sorry I'm not too familiar with the threading stuff!

@brisvag
Copy link
Contributor Author

brisvag commented Jun 2, 2023

Adding thread.quit() seems to solve the docs bulding issue, but now there's some odd segfault in pyside6, which also seem to refer to threads... may be related?

viewer.window.add_dock_widget(wdg, area='bottom')
wdg()

napari.run()
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be protected by if __name__ == '__main__':, too? I'm a bit confused why it even works LOL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is that to define some of the callbacks, the thread needs to already be spawned... so I would have to stick a lot of stuff inside the if __name__ block... not sure that helps much :P

@brisvag brisvag added the ready to merge Last chance for comments! Will be merged in ~24h label Jun 2, 2023
@brisvag brisvag merged commit 5bced98 into napari:main Jun 6, 2023
31 checks passed
@brisvag brisvag removed the ready to merge Last chance for comments! Will be merged in ~24h label Jun 6, 2023
@Czaki Czaki mentioned this pull request Jun 7, 2023
@Czaki Czaki added this to the 0.4.18 milestone Jun 16, 2023
Czaki pushed a commit that referenced this pull request Jun 19, 2023
# Description

Adds a new example to play around with 2D sine waves and fourier
transforms. I made it to better visualise and explain how power spectrum
and phase behave depending on the fourier components.

The example is also a nice showcase of two-way threading communication
:P

Here's what it looks like:


https://github.com/napari/napari/assets/23482191/522f3e8d-cbe5-41c4-9c78-9da05d751adc
Czaki pushed a commit that referenced this pull request Jun 21, 2023
# Description

Adds a new example to play around with 2D sine waves and fourier
transforms. I made it to better visualise and explain how power spectrum
and phase behave depending on the fourier components.

The example is also a nice showcase of two-way threading communication
:P

Here's what it looks like:


https://github.com/napari/napari/assets/23482191/522f3e8d-cbe5-41c4-9c78-9da05d751adc
Czaki pushed a commit that referenced this pull request Jun 21, 2023
# Description

Adds a new example to play around with 2D sine waves and fourier
transforms. I made it to better visualise and explain how power spectrum
and phase behave depending on the fourier components.

The example is also a nice showcase of two-way threading communication
:P

Here's what it looks like:


https://github.com/napari/napari/assets/23482191/522f3e8d-cbe5-41c4-9c78-9da05d751adc
Czaki pushed a commit that referenced this pull request Jun 21, 2023
# Description

Adds a new example to play around with 2D sine waves and fourier
transforms. I made it to better visualise and explain how power spectrum
and phase behave depending on the fourier components.

The example is also a nice showcase of two-way threading communication
:P

Here's what it looks like:


https://github.com/napari/napari/assets/23482191/522f3e8d-cbe5-41c4-9c78-9da05d751adc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants