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

Generalized deskewing for mantis #6

Merged
merged 30 commits into from
May 10, 2023
Merged

Generalized deskewing for mantis #6

merged 30 commits into from
May 10, 2023

Conversation

talonchandler
Copy link
Contributor

@talonchandler talonchandler commented Apr 1, 2023

@ieivanov and I discussed deskewing workflows yesterday, and I've now added a complete deskewing workflow to this PR.

  1. python estimate_deskew.py <path/to/mm-folder> will open the raw data in a napari window and prompt you to make two annotations:
  • a rectangle on a region that you expect to be square in the deskewed space, and
  • a line that you expect to be normal to the coverslip in the deskewed space

From these two annotations, this workflow will compute the two parameters that the deskewing routine needs (px_to_scan_ratio and theta_deg) and saves them in a deskew_params.yaml file (optional -o parameter to rename the output .yml file).

  1. python deskew.py </path/to/mmfolder/> </path/to/deskew_params.yml> -o </path/to/output.zarr> will load the data, apply the deskewing, save the result with metadata (including the deskewing parameters and new voxel sizes) using iohub, and optionally view the deskewed result with the -v flag.

I'm much happier with the interactive workflow (no iteration to find good deskewing parameters), and the results on the 03_30 data look much better than the data I showed on Monday (improvements in the data and deskewing).

The figure below shows single slices in each direction---the fact that the rings of the sphere appear in a single slice in all three directions and that the target appears circular is encouraging.

Screenshot 2023-04-04 at 5 55 50 PM

More deskewed data lives in /projects/mantis/2023_03_30_argolight/deskewed.


One puzzle remains: I'm consistently finding the best tilt angles to be between ~34-36 degrees. This is very likely because of an incorrect parametrization (trig error?, index of refraction error?). I've managed to keep my transformations consistent when I estimate from the raw data, so this puzzle does not affect the self-consistent interactive workflow here. I will keep an eye out.

Copy link
Collaborator

@mattersoflight mattersoflight left a comment

Choose a reason for hiding this comment

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

Thanks for a clean and readable implementation! My main input is around interpolation and fill value. LGTM once those are addressed.

mantis/analysis/mantis_deskew.py Outdated Show resolved Hide resolved
mantis/analysis/mantis_deskew.py Outdated Show resolved Hide resolved
@talonchandler talonchandler marked this pull request as ready for review April 5, 2023 00:57
@ieivanov
Copy link
Collaborator

ieivanov commented Apr 5, 2023

One puzzle remains: I'm consistently finding the best tilt angles to be between ~34-36 degrees. This is very likely because of an incorrect parametrization (trig error?, index of refraction error?).

We set the tilt angle roughly and by hand with the help of a protractor printout. It's quite possible that the real tilt angle is around 34-36 deg as you estimate. We should trust the data on this

@ieivanov
Copy link
Collaborator

ieivanov commented Apr 5, 2023

The Argolight target from 2023_03_30 looks good! I'll look closer and I'll let you know if I see anything else. One minor issue - currently the z axis in the deskewed volumes is flipped, it goes from deep in the sample at low z index and towards the coverslip at increasing z index

@talonchandler
Copy link
Contributor Author

currently the z axis in the deskewed volumes is flipped, it goes from deep in the sample at low z index and towards the coverslip at increasing z index

Good call, fixed. Compare the new /2023_03_30_argolight/deskewed_flipped/sphere_1.zarr to the old sphere_1 to see the proposed new convention with z=0 at the coverslip.

Copy link
Collaborator

@mattersoflight mattersoflight left a comment

Choose a reason for hiding this comment

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

@ieivanov , @talonchandler great idea to use an interactive workflow to estimate the skew angle from data. This strategy can be extended to the registration of channels.

mantis/analysis/deskew.py Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

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

Great idea to use yml file to track the parameters. I suggest renaming this to analysis_params.yml and creating two sections deskew and register.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that a single-step analyze pipeline is extremely useful, and I would suggest that we develop it in addition to, not instead of, the individual substeps (e.g. deskew, register, deconvolve).

I have two motivations:

  • debugging and developing individual steps is easier, and it's nice to be able to optimize the steps individually. (e.g. some steps might require an interactive estimate, others might require an optimization)
  • if modularity of the processing pipeline isn't a priority, then the number of options can get large and tangled very quickly (I think recOrder's earlier offline mode is a great example)

As the substeps reach maturity, we can start consolidating into a single mantis analyze <lf-path> <fl-path> -c analysis_params.yml pipeline like you're suggesting. On the technical side, pydantic can enable this as well...we can specify a higher-level data class that consists of the substeps' dataclasses, and this will create the multiple sections for deskew, register, deconvolve, etc. in our final parameter files.

I'm open to comments/experience here, since I know that keeping track of proliferating configuration files can be a major problem.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This module can be renamed analysis_settings.py and have data classes for all analysis steps.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My comment above is related to this suggestion.

I agree that this file can be renamed more generally, and I'm anticipating a DeskewSettings(BaseModel) class for now with RegisterSettings(BaseModel) and others to follow. Later we can have an AnalyzeSettings(BaseModel) class that has [DeskewSettings, RegisterSettings, ...] as its members.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The interactive workflow and use of orthogonal rings make a lot of sense. A similar workflow can be implemented for registering the channels too after this PR is merged.

@mattersoflight
Copy link
Collaborator

@talonchandler Looking at the data from the sphere target and the ring target, I think that deskewing is working well. We can merge and start to use it.

It will make sense to contribute this to dexp and depend on it.
After merging this PR, can you and @JoOkuma figure out how to go about that?

@JoOkuma
Copy link
Member

JoOkuma commented Apr 10, 2023

@mattersoflight Thanks for tagging me. dexp is going through a rework, I'll use this implementation as a reference and make sure the new dexp deskewing works on daxi and mantis.

However, It won't be implemented in the coming days.

@talonchandler
Copy link
Contributor Author

@mattersoflight thanks for reviewing and testing.

@JoOkuma thanks! We're in no major rush to move/contribute this to dexp, but we'd like to keep our groups' deskewing code consolidated and clean. Please let me know when dexp2 is ready, and we can work together to merge it.

@ieivanov feel free to merge this whenever you're ready, or let me know if you have other changes you'd like to make.

@ieivanov
Copy link
Collaborator

Thanks @talonchandler, I'll take it from here. I'm using this PR to deskew the cell data from last week, and can make changes as necessary.

When you were working on this, you likely made drawings/sketches of how the data should be transformed. It will be helpful to include those in a docs page with matching nomenclature to the variables in mantis_deskew

@mattersoflight
Copy link
Collaborator

@ieivanov please also address #9 while you are updating the deskewing code.

@talonchandler
Copy link
Contributor Author

I just added a commit that changes the default to "crop the corner". I renamed the suggested name --crop-corner to --keep-overhang, so that it could be a command-line flag (false by default).

The following figure demonstrates the new option and shows a situation where you might benefit from using this option.

Screenshot 2023-04-12 at 7 13 56 PM

@talonchandler talonchandler linked an issue Apr 13, 2023 that may be closed by this pull request
@ieivanov
Copy link
Collaborator

Thanks Talon! I had to restructure the code a bit to make it run for multiple positions (create_zeros was called inside the channels loop). I'll merge these changes today, and I'll merge this branch into main.

pyproject.toml Outdated Show resolved Hide resolved
@ieivanov
Copy link
Collaborator

Figured I'll finish what I started on the multiprocessing side, but I ran into bigger problems, see micro-manager/NDTiffStorage/#108

Copy link
Collaborator

@ieivanov ieivanov left a comment

Choose a reason for hiding this comment

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

@talonchandler @edyoshikun par_deskew.py shows my indented way to deskew data along the TPC dimensions in parallel, but it currently doesn't work because of a bug in ndtiff. This should work on MM ome.tiff dataset, it's worth a try

@ieivanov
Copy link
Collaborator

ieivanov commented May 1, 2023

We agreed that we will merge this PR with its single-process implementation and we'll work on multiprocessing in #33. @talonchandler it would be great if you could take another look at this before we merge.

@talonchandler
Copy link
Contributor Author

I just reread and tested the code, and everything looks good.

I found one bug where a numpy float was being passed to the .yaml dump, which resulted in a non-readable config file. Reinstating the float() in the validator fixed the issue.

Merging.

@talonchandler talonchandler merged commit 6085b04 into main May 10, 2023
@ieivanov ieivanov deleted the improve-deskew branch July 9, 2023 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

crop the corners after deskew
5 participants