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

Refactor the single mode fiber injection unit #192

Merged
merged 20 commits into from
May 2, 2023

Conversation

ehpor
Copy link
Owner

@ehpor ehpor commented Apr 28, 2023

Converts the single-mode fiber detector into a single-mode fiber injection optical element.

  • Add back the single-mode fiber detector and make it deprecated.
  • Add pytest.
  • Add new class and function to __init__.py.

Fixes #191.

@ehpor ehpor added enhancement New feature or request bugfix labels Apr 28, 2023
@ehpor ehpor requested a review from syhaffert April 28, 2023 18:57
@ehpor ehpor self-assigned this Apr 28, 2023
@ehpor ehpor force-pushed the feature/single_mode_fiber_injection_refactor branch from 45435bd to 2b6e32a Compare April 28, 2023 19:23
@codecov
Copy link

codecov bot commented Apr 28, 2023

Codecov Report

Merging #192 (2dd584b) into master (e319391) will increase coverage by 0.15%.
The diff coverage is 71.73%.

@@            Coverage Diff             @@
##           master     #192      +/-   ##
==========================================
+ Coverage   81.40%   81.56%   +0.15%     
==========================================
  Files          97       97              
  Lines        7266     7334      +68     
==========================================
+ Hits         5915     5982      +67     
- Misses       1351     1352       +1     
Impacted Files Coverage Δ
hcipy/optics/__init__.py 100.00% <ø> (ø)
hcipy/optics/fiber.py 72.26% <71.73%> (+4.95%) ⬆️

... and 2 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Collaborator

@syhaffert syhaffert 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 two comments that need to be addressed.

hcipy/optics/fiber.py Outdated Show resolved Hide resolved
hcipy/optics/fiber.py Outdated Show resolved Hide resolved
@ehpor
Copy link
Owner Author

ehpor commented Apr 28, 2023

@syhaffert This wasn't ready for review. Now it is. Both were fixed already.

@ehpor ehpor marked this pull request as ready for review April 28, 2023 19:41
@ehpor ehpor requested a review from syhaffert April 28, 2023 19:41
Copy link
Collaborator

@syhaffert syhaffert left a comment

Choose a reason for hiding this comment

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

I only have 1 comment.


# Compute and normalize the Gaussian function.
res = np.exp(-r2 / ((0.5 * mode_field_diameter)**2))
res /= np.sum(np.abs(res)**2 * grid.weights)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We know theoretically what the normalization factor should be, right? It's 1/sqrt(2*pi * sigma^2) with some correction for the pixel size. Wouldn't that be a better normalization? I ran into issues before where I couldn't rely on numerical normalization due to a finite grid size.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Fixed. There is no correction for pixel size. However, there is a change inside the exponential itself, since we are normalizing for the square, not the Gaussian itself.

BTW, should we then also remove the normalization inside the SingleModeFiberInjection element itself? That feels wrong though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah I think we need to leave the normalization inside that class just in case some weird mode is used there.

Copy link

Choose a reason for hiding this comment

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

The normalisation is important, otherwise, the throughput will look oddly too high. Also, the mode_field_diameter requires a clear definition. It can change in textbooks. Usually, I use the same as Thorlabs: 1/e2 on the power (https://www.thorlabs.com/NewGroupPage9_PF.cfm?ObjectGroup_ID=949). So it would be:

	res = np.exp(-r2 / ((mode_field_diameter)**2))
	res *= np.sqrt(2/np.pi)/mode_field_diameter

(from heart, but please double check!)

Copy link
Owner Author

Choose a reason for hiding this comment

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

I kept the numerical normalization inside the SingleModeFiberInjection class.

@sylacour Thanks. I indeed had a wrong definition of the MFD. (I intended to use the 1/e2 one, but I made a mistake in evaluating the Gaussian.) Your from-heart formula was actually correct, except for exchanging MFD with the mode field radius. I also added a test to make sure that the 1/e definition is always checked for this function. The normalization was already tested.

@ehpor ehpor requested a review from syhaffert April 29, 2023 01:15
Copy link
Collaborator

@syhaffert syhaffert left a comment

Choose a reason for hiding this comment

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

I think this is the last issue.

hcipy/optics/fiber.py Outdated Show resolved Hide resolved
hcipy/optics/fiber.py Show resolved Hide resolved
@ehpor
Copy link
Owner Author

ehpor commented May 2, 2023

@syhaffert Good catch. That bug has been there for a while then. I put the conjugate on the input field. I added an imaginary mode in the test as well. The test doesn't pass without the conjugation.

Copy link
Collaborator

@syhaffert syhaffert left a comment

Choose a reason for hiding this comment

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

I found another missing conjugate.

Yeah I think it has been here for a while. That just shows that this part of the code is not used that much. Do we need to add a np.real to the outcome of the dot product? Intensity is by definition real and adding that np.real will make sure it is compliant.

edit: That was a stupid comment from me. The dot product is the complex amplitude of the mode. no need to force realness here!

hcipy/optics/fiber.py Show resolved Hide resolved
ehpor added 4 commits May 2, 2023 15:29
This correctly deals with fiber modes with a nonzero imaginary component.
The square should have been taken over the absolute value.
@ehpor ehpor requested a review from syhaffert May 2, 2023 20:12
@syhaffert syhaffert merged commit 2b707bc into master May 2, 2023
@ehpor ehpor deleted the feature/single_mode_fiber_injection_refactor branch May 2, 2023 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'gaussian_mode' is not defined
3 participants