-
Notifications
You must be signed in to change notification settings - Fork 7
Fix 3D to 2D phase reconstruction #441
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
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
ec47bfb
debug jigs - revert before merging
talonchandler 9c311e7
debug jigs - revert before merging
talonchandler 830cd31
Merge branch 'debug-3d-to-2d' of https://github.com/mehta-lab/recOrde…
talonchandler 127cae7
Merge branch 'main' into debug-3d-to-2d
talonchandler 6e2eaef
turn off bg_filter
talonchandler 1d0df0f
fix accidental order swap
talonchandler 4c5bd29
SVD-based reconstruction
talonchandler 74030c9
better default regularization
talonchandler 9c06c29
save phase output
talonchandler e8c52ab
improved example for clearer phase+absorption debugging
talonchandler 164f612
add optional z_focus_offset to config file
talonchandler 8c9421a
no need to copy
talonchandler c64808f
output phase only, but prepare for outputting absorption after it's b…
talonchandler 606a810
remove testing jig
talonchandler fe9a32d
precalculate singular system
talonchandler a764174
add auto-focus option
talonchandler 99e5a4f
improved focus finding
talonchandler 54d96fa
fix z focus offset sign error
talonchandler 2f79557
Merge branch 'main' into debug-3d-to-2d
talonchandler 7eae8e0
style
talonchandler 107e746
black
talonchandler ddd381e
set default z_focus_offset to 0
talonchandler d11f41b
handle unused paramter in fluorescence recon
talonchandler a863c7c
example configs
talonchandler 2fdbb66
don't break birefringence recon
talonchandler 8a5f192
fix CLI test (?)
talonchandler 1d95a7a
fix 2d phase recon from birefrefingence
talonchandler 285ff8c
Revert "fix CLI test (?)"
talonchandler d1ae68d
`tensor` to `from_numpy`
talonchandler 1601296
fix sign flipping bug...allow `z_focus_offset` to interact with `inve…
talonchandler 9842af5
refactor z position list function
talonchandler 5e8d916
example gives same strength to phase and absorption
talonchandler 3702ee0
Update waveorder/focus.py
talonchandler cbf9301
Update waveorder/focus.py
talonchandler afd6e5b
Update waveorder/models/isotropic_thin_3d.py
talonchandler 80d13f3
docstrings
talonchandler 919ef40
fix docstring
talonchandler 396ab77
Update waveorder/models/isotropic_thin_3d.py
talonchandler 1f1f918
improve comment
talonchandler b31562b
remove chunks
talonchandler 3397de6
revert debug mode
talonchandler 92d84f7
Merge branch 'debug-3d-to-2d' of github.com:mehta-lab/waveorder into …
talonchandler 3a9c441
update docstring
talonchandler 0756ffa
from_numpy
talonchandler 37c1524
Merge branch 'main' into debug-3d-to-2d
talonchandler bea608d
Merge branch 'main' into debug-3d-to-2d
talonchandler f42b56a
test `_position_list_from_shape_scale_offset`
talonchandler 60bf93a
guardrail against na_ill >= na_det
talonchandler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| import numpy as np | ||
| from iohub.ngff import Position, open_ome_zarr | ||
|
|
||
| from waveorder import focus | ||
| from waveorder.cli.parsing import ( | ||
| config_filepath, | ||
| input_position_dirpaths, | ||
|
|
@@ -20,6 +21,22 @@ | |
| ) | ||
|
|
||
|
|
||
| def _position_list_from_shape_scale_offset( | ||
| shape: int, scale: float, offset: float | ||
| ) -> list: | ||
| """ | ||
| Generates a list of positions based on the given array shape, pixel size (scale), and offset. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> _position_list_from_shape_scale_offset(5, 1.0, 0.0) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be moved to tests? We don't have doctest set up. |
||
| [2.0, 1.0, 0.0, -1.0, -2.0] | ||
| >>> _position_list_from_shape_scale_offset(4, 0.5, 1.0) | ||
| [1.5, 1.0, 0.5, 0.0] | ||
| """ | ||
| return list((-np.arange(shape) + (shape // 2) + offset) * scale) | ||
|
|
||
|
|
||
| def generate_and_save_birefringence_transfer_function(settings, dataset): | ||
| """Generates and saves the birefringence transfer function to the dataset, based on the settings. | ||
|
|
||
|
|
@@ -61,18 +78,22 @@ def generate_and_save_phase_transfer_function( | |
| echo_headline("Generating phase transfer function with settings:") | ||
| echo_settings(settings.phase.transfer_function) | ||
|
|
||
| settings_dict = settings.phase.transfer_function.dict() | ||
| if settings.reconstruction_dimension == 2: | ||
| # Convert zyx_shape and z_pixel_size into yx_shape and z_position_list | ||
| settings_dict = settings.phase.transfer_function.dict() | ||
| settings_dict["yx_shape"] = [zyx_shape[1], zyx_shape[2]] | ||
| settings_dict["z_position_list"] = list( | ||
| -(np.arange(zyx_shape[0]) - zyx_shape[0] // 2) | ||
| * settings_dict["z_pixel_size"] | ||
| settings_dict["z_position_list"] = ( | ||
| _position_list_from_shape_scale_offset( | ||
| shape=zyx_shape[0], | ||
| scale=settings_dict["z_pixel_size"], | ||
| offset=settings_dict["z_focus_offset"], | ||
| ) | ||
| ) | ||
|
|
||
| # Remove unused parameters | ||
| settings_dict.pop("z_pixel_size") | ||
| settings_dict.pop("z_padding") | ||
| settings_dict.pop("z_focus_offset") | ||
|
|
||
| # Calculate transfer functions | ||
| ( | ||
|
|
@@ -82,26 +103,36 @@ def generate_and_save_phase_transfer_function( | |
| **settings_dict, | ||
| ) | ||
|
|
||
| # Calculate singular system | ||
| U, S, Vh = isotropic_thin_3d.calculate_singular_system( | ||
| absorption_transfer_function, | ||
| phase_transfer_function, | ||
| ) | ||
|
|
||
| # Save | ||
| dataset.create_image( | ||
| "absorption_transfer_function", | ||
| absorption_transfer_function.cpu().numpy()[None, None, ...], | ||
| chunks=(1, 1, 1, zyx_shape[1], zyx_shape[2]), | ||
| "singular_system_U", | ||
| U.cpu().numpy()[None], | ||
| ) | ||
| dataset.create_image( | ||
| "phase_transfer_function", | ||
| phase_transfer_function.cpu().numpy()[None, None, ...], | ||
| chunks=(1, 1, 1, zyx_shape[1], zyx_shape[2]), | ||
| "singular_system_S", | ||
| S.cpu().numpy()[None, None], | ||
| ) | ||
| dataset.create_image( | ||
| "singular_system_Vh", | ||
| Vh.cpu().numpy()[None], | ||
| ) | ||
|
|
||
| elif settings.reconstruction_dimension == 3: | ||
| settings_dict.pop("z_focus_offset") # not used in 3D | ||
|
|
||
| # Calculate transfer functions | ||
| ( | ||
| real_potential_transfer_function, | ||
| imaginary_potential_transfer_function, | ||
| ) = phase_thick_3d.calculate_transfer_function( | ||
| zyx_shape=zyx_shape, | ||
| **settings.phase.transfer_function.dict(), | ||
| **settings_dict, | ||
| ) | ||
| # Save | ||
| dataset.create_image( | ||
|
|
@@ -133,6 +164,9 @@ def generate_and_save_fluorescence_transfer_function( | |
| """ | ||
| echo_headline("Generating fluorescence transfer function with settings:") | ||
| echo_settings(settings.fluorescence.transfer_function) | ||
| # Remove unused parameters | ||
ziw-liu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| settings_dict = settings.fluorescence.transfer_function.dict() | ||
| settings_dict.pop("z_focus_offset") | ||
|
|
||
| if settings.reconstruction_dimension == 2: | ||
| raise NotImplementedError | ||
|
|
@@ -141,7 +175,7 @@ def generate_and_save_fluorescence_transfer_function( | |
| optical_transfer_function = ( | ||
| isotropic_fluorescent_thick_3d.calculate_transfer_function( | ||
| zyx_shape=zyx_shape, | ||
| **settings.fluorescence.transfer_function.dict(), | ||
| **settings_dict, | ||
| ) | ||
| ) | ||
| # Save | ||
|
|
@@ -182,9 +216,40 @@ def compute_transfer_function_cli( | |
| f"Each of the input_channel_names = {settings.input_channel_names} in {config_filepath} must appear in the dataset {input_position_dirpaths[0]} which currently contains channel_names = {input_dataset.channel_names}." | ||
| ) | ||
|
|
||
| # Find in-focus slices for 2D reconstruction in "auto" mode | ||
| if ( | ||
| settings.phase is not None | ||
| and settings.reconstruction_dimension == 2 | ||
| and settings.phase.transfer_function.z_focus_offset == "auto" | ||
| ): | ||
|
|
||
| c_idx = input_dataset.get_channel_index( | ||
| settings.input_channel_names[0] | ||
| ) | ||
| zyx_array = input_dataset["0"][0, c_idx] | ||
|
|
||
| in_focus_index = focus.focus_from_transverse_band( | ||
| zyx_array, | ||
| NA_det=settings.phase.transfer_function.numerical_aperture_detection, | ||
| lambda_ill=settings.phase.transfer_function.wavelength_illumination, | ||
| pixel_size=settings.phase.transfer_function.yx_pixel_size, | ||
| mode="min", | ||
| polynomial_fit_order=4, | ||
| ) | ||
|
|
||
| z_focus_offset = in_focus_index - (zyx_shape[0] // 2) | ||
| settings.phase.transfer_function.z_focus_offset = z_focus_offset | ||
| print("Found z_focus_offset:", z_focus_offset) | ||
|
|
||
| # Prepare output dataset | ||
| num_channels = ( | ||
| 2 if settings.reconstruction_dimension == 2 else 1 | ||
| ) # space for SVD | ||
| output_dataset = open_ome_zarr( | ||
| output_dirpath, layout="fov", mode="w", channel_names=["None"] | ||
| output_dirpath, | ||
| layout="fov", | ||
| mode="w", | ||
| channel_names=num_channels * ["None"], | ||
| ) | ||
|
|
||
| # Pass settings to appropriate calculate_transfer_function and save | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.